[PATCH 3/57][Arm][GAS] Add support for MVE instructions: vabs and vneg
[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 };
471
472 /* The maximum number of operands we need.  */
473 #define ARM_IT_MAX_OPERANDS 6
474 #define ARM_IT_MAX_RELOCS 3
475
476 struct arm_it
477 {
478   const char *  error;
479   unsigned long instruction;
480   int           size;
481   int           size_req;
482   int           cond;
483   /* "uncond_value" is set to the value in place of the conditional field in
484      unconditional versions of the instruction, or -1 if nothing is
485      appropriate.  */
486   int           uncond_value;
487   struct neon_type vectype;
488   /* This does not indicate an actual NEON instruction, only that
489      the mnemonic accepts neon-style type suffixes.  */
490   int           is_neon;
491   /* Set to the opcode if the instruction needs relaxation.
492      Zero if the instruction is not relaxed.  */
493   unsigned long relax;
494   struct
495   {
496     bfd_reloc_code_real_type type;
497     expressionS              exp;
498     int                      pc_rel;
499   } relocs[ARM_IT_MAX_RELOCS];
500
501   enum pred_instruction_type pred_insn_type;
502
503   struct
504   {
505     unsigned reg;
506     signed int imm;
507     struct neon_type_el vectype;
508     unsigned present    : 1;  /* Operand present.  */
509     unsigned isreg      : 1;  /* Operand was a register.  */
510     unsigned immisreg   : 1;  /* .imm field is a second register.  */
511     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
512     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
513     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
514     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
515        instructions. This allows us to disambiguate ARM <-> vector insns.  */
516     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
517     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
518     unsigned isquad     : 1;  /* Operand is SIMD quad register.  */
519     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
520     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
521     unsigned writeback  : 1;  /* Operand has trailing !  */
522     unsigned preind     : 1;  /* Preindexed address.  */
523     unsigned postind    : 1;  /* Postindexed address.  */
524     unsigned negative   : 1;  /* Index register was negated.  */
525     unsigned shifted    : 1;  /* Shift applied to operation.  */
526     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
527   } operands[ARM_IT_MAX_OPERANDS];
528 };
529
530 static struct arm_it inst;
531
532 #define NUM_FLOAT_VALS 8
533
534 const char * fp_const[] =
535 {
536   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
537 };
538
539 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
540
541 #define FAIL    (-1)
542 #define SUCCESS (0)
543
544 #define SUFF_S 1
545 #define SUFF_D 2
546 #define SUFF_E 3
547 #define SUFF_P 4
548
549 #define CP_T_X   0x00008000
550 #define CP_T_Y   0x00400000
551
552 #define CONDS_BIT        0x00100000
553 #define LOAD_BIT         0x00100000
554
555 #define DOUBLE_LOAD_FLAG 0x00000001
556
557 struct asm_cond
558 {
559   const char *   template_name;
560   unsigned long  value;
561 };
562
563 #define COND_ALWAYS 0xE
564
565 struct asm_psr
566 {
567   const char *   template_name;
568   unsigned long  field;
569 };
570
571 struct asm_barrier_opt
572 {
573   const char *    template_name;
574   unsigned long   value;
575   const arm_feature_set arch;
576 };
577
578 /* The bit that distinguishes CPSR and SPSR.  */
579 #define SPSR_BIT   (1 << 22)
580
581 /* The individual PSR flag bits.  */
582 #define PSR_c   (1 << 16)
583 #define PSR_x   (1 << 17)
584 #define PSR_s   (1 << 18)
585 #define PSR_f   (1 << 19)
586
587 struct reloc_entry
588 {
589   const char *              name;
590   bfd_reloc_code_real_type  reloc;
591 };
592
593 enum vfp_reg_pos
594 {
595   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
596   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
597 };
598
599 enum vfp_ldstm_type
600 {
601   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
602 };
603
604 /* Bits for DEFINED field in neon_typed_alias.  */
605 #define NTA_HASTYPE  1
606 #define NTA_HASINDEX 2
607
608 struct neon_typed_alias
609 {
610   unsigned char        defined;
611   unsigned char        index;
612   struct neon_type_el  eltype;
613 };
614
615 /* ARM register categories.  This includes coprocessor numbers and various
616    architecture extensions' registers.  Each entry should have an error message
617    in reg_expected_msgs below.  */
618 enum arm_reg_type
619 {
620   REG_TYPE_RN,
621   REG_TYPE_CP,
622   REG_TYPE_CN,
623   REG_TYPE_FN,
624   REG_TYPE_VFS,
625   REG_TYPE_VFD,
626   REG_TYPE_NQ,
627   REG_TYPE_VFSD,
628   REG_TYPE_NDQ,
629   REG_TYPE_NSD,
630   REG_TYPE_NSDQ,
631   REG_TYPE_VFC,
632   REG_TYPE_MVF,
633   REG_TYPE_MVD,
634   REG_TYPE_MVFX,
635   REG_TYPE_MVDX,
636   REG_TYPE_MVAX,
637   REG_TYPE_MQ,
638   REG_TYPE_DSPSC,
639   REG_TYPE_MMXWR,
640   REG_TYPE_MMXWC,
641   REG_TYPE_MMXWCG,
642   REG_TYPE_XSCALE,
643   REG_TYPE_RNB,
644 };
645
646 /* Structure for a hash table entry for a register.
647    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
648    information which states whether a vector type or index is specified (for a
649    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
650 struct reg_entry
651 {
652   const char *               name;
653   unsigned int               number;
654   unsigned char              type;
655   unsigned char              builtin;
656   struct neon_typed_alias *  neon;
657 };
658
659 /* Diagnostics used when we don't get a register of the expected type.  */
660 const char * const reg_expected_msgs[] =
661 {
662   [REG_TYPE_RN]     = N_("ARM register expected"),
663   [REG_TYPE_CP]     = N_("bad or missing co-processor number"),
664   [REG_TYPE_CN]     = N_("co-processor register expected"),
665   [REG_TYPE_FN]     = N_("FPA register expected"),
666   [REG_TYPE_VFS]    = N_("VFP single precision register expected"),
667   [REG_TYPE_VFD]    = N_("VFP/Neon double precision register expected"),
668   [REG_TYPE_NQ]     = N_("Neon quad precision register expected"),
669   [REG_TYPE_VFSD]   = N_("VFP single or double precision register expected"),
670   [REG_TYPE_NDQ]    = N_("Neon double or quad precision register expected"),
671   [REG_TYPE_NSD]    = N_("Neon single or double precision register expected"),
672   [REG_TYPE_NSDQ]   = N_("VFP single, double or Neon quad precision register"
673                          " expected"),
674   [REG_TYPE_VFC]    = N_("VFP system register expected"),
675   [REG_TYPE_MVF]    = N_("Maverick MVF register expected"),
676   [REG_TYPE_MVD]    = N_("Maverick MVD register expected"),
677   [REG_TYPE_MVFX]   = N_("Maverick MVFX register expected"),
678   [REG_TYPE_MVDX]   = N_("Maverick MVDX register expected"),
679   [REG_TYPE_MVAX]   = N_("Maverick MVAX register expected"),
680   [REG_TYPE_DSPSC]  = N_("Maverick DSPSC register expected"),
681   [REG_TYPE_MMXWR]  = N_("iWMMXt data register expected"),
682   [REG_TYPE_MMXWC]  = N_("iWMMXt control register expected"),
683   [REG_TYPE_MMXWCG] = N_("iWMMXt scalar register expected"),
684   [REG_TYPE_XSCALE] = N_("XScale accumulator register expected"),
685   [REG_TYPE_MQ]     = N_("MVE vector register expected"),
686   [REG_TYPE_RNB]    = N_("")
687 };
688
689 /* Some well known registers that we refer to directly elsewhere.  */
690 #define REG_R12 12
691 #define REG_SP  13
692 #define REG_LR  14
693 #define REG_PC  15
694
695 /* ARM instructions take 4bytes in the object file, Thumb instructions
696    take 2:  */
697 #define INSN_SIZE       4
698
699 struct asm_opcode
700 {
701   /* Basic string to match.  */
702   const char * template_name;
703
704   /* Parameters to instruction.  */
705   unsigned int operands[8];
706
707   /* Conditional tag - see opcode_lookup.  */
708   unsigned int tag : 4;
709
710   /* Basic instruction code.  */
711   unsigned int avalue : 28;
712
713   /* Thumb-format instruction code.  */
714   unsigned int tvalue;
715
716   /* Which architecture variant provides this instruction.  */
717   const arm_feature_set * avariant;
718   const arm_feature_set * tvariant;
719
720   /* Function to call to encode instruction in ARM format.  */
721   void (* aencode) (void);
722
723   /* Function to call to encode instruction in Thumb format.  */
724   void (* tencode) (void);
725
726   /* Indicates whether this instruction may be vector predicated.  */
727   unsigned int mayBeVecPred : 1;
728 };
729
730 /* Defines for various bits that we will want to toggle.  */
731 #define INST_IMMEDIATE  0x02000000
732 #define OFFSET_REG      0x02000000
733 #define HWOFFSET_IMM    0x00400000
734 #define SHIFT_BY_REG    0x00000010
735 #define PRE_INDEX       0x01000000
736 #define INDEX_UP        0x00800000
737 #define WRITE_BACK      0x00200000
738 #define LDM_TYPE_2_OR_3 0x00400000
739 #define CPSI_MMOD       0x00020000
740
741 #define LITERAL_MASK    0xf000f000
742 #define OPCODE_MASK     0xfe1fffff
743 #define V4_STR_BIT      0x00000020
744 #define VLDR_VMOV_SAME  0x0040f000
745
746 #define T2_SUBS_PC_LR   0xf3de8f00
747
748 #define DATA_OP_SHIFT   21
749 #define SBIT_SHIFT      20
750
751 #define T2_OPCODE_MASK  0xfe1fffff
752 #define T2_DATA_OP_SHIFT 21
753 #define T2_SBIT_SHIFT    20
754
755 #define A_COND_MASK         0xf0000000
756 #define A_PUSH_POP_OP_MASK  0x0fff0000
757
758 /* Opcodes for pushing/poping registers to/from the stack.  */
759 #define A1_OPCODE_PUSH    0x092d0000
760 #define A2_OPCODE_PUSH    0x052d0004
761 #define A2_OPCODE_POP     0x049d0004
762
763 /* Codes to distinguish the arithmetic instructions.  */
764 #define OPCODE_AND      0
765 #define OPCODE_EOR      1
766 #define OPCODE_SUB      2
767 #define OPCODE_RSB      3
768 #define OPCODE_ADD      4
769 #define OPCODE_ADC      5
770 #define OPCODE_SBC      6
771 #define OPCODE_RSC      7
772 #define OPCODE_TST      8
773 #define OPCODE_TEQ      9
774 #define OPCODE_CMP      10
775 #define OPCODE_CMN      11
776 #define OPCODE_ORR      12
777 #define OPCODE_MOV      13
778 #define OPCODE_BIC      14
779 #define OPCODE_MVN      15
780
781 #define T2_OPCODE_AND   0
782 #define T2_OPCODE_BIC   1
783 #define T2_OPCODE_ORR   2
784 #define T2_OPCODE_ORN   3
785 #define T2_OPCODE_EOR   4
786 #define T2_OPCODE_ADD   8
787 #define T2_OPCODE_ADC   10
788 #define T2_OPCODE_SBC   11
789 #define T2_OPCODE_SUB   13
790 #define T2_OPCODE_RSB   14
791
792 #define T_OPCODE_MUL 0x4340
793 #define T_OPCODE_TST 0x4200
794 #define T_OPCODE_CMN 0x42c0
795 #define T_OPCODE_NEG 0x4240
796 #define T_OPCODE_MVN 0x43c0
797
798 #define T_OPCODE_ADD_R3 0x1800
799 #define T_OPCODE_SUB_R3 0x1a00
800 #define T_OPCODE_ADD_HI 0x4400
801 #define T_OPCODE_ADD_ST 0xb000
802 #define T_OPCODE_SUB_ST 0xb080
803 #define T_OPCODE_ADD_SP 0xa800
804 #define T_OPCODE_ADD_PC 0xa000
805 #define T_OPCODE_ADD_I8 0x3000
806 #define T_OPCODE_SUB_I8 0x3800
807 #define T_OPCODE_ADD_I3 0x1c00
808 #define T_OPCODE_SUB_I3 0x1e00
809
810 #define T_OPCODE_ASR_R  0x4100
811 #define T_OPCODE_LSL_R  0x4080
812 #define T_OPCODE_LSR_R  0x40c0
813 #define T_OPCODE_ROR_R  0x41c0
814 #define T_OPCODE_ASR_I  0x1000
815 #define T_OPCODE_LSL_I  0x0000
816 #define T_OPCODE_LSR_I  0x0800
817
818 #define T_OPCODE_MOV_I8 0x2000
819 #define T_OPCODE_CMP_I8 0x2800
820 #define T_OPCODE_CMP_LR 0x4280
821 #define T_OPCODE_MOV_HR 0x4600
822 #define T_OPCODE_CMP_HR 0x4500
823
824 #define T_OPCODE_LDR_PC 0x4800
825 #define T_OPCODE_LDR_SP 0x9800
826 #define T_OPCODE_STR_SP 0x9000
827 #define T_OPCODE_LDR_IW 0x6800
828 #define T_OPCODE_STR_IW 0x6000
829 #define T_OPCODE_LDR_IH 0x8800
830 #define T_OPCODE_STR_IH 0x8000
831 #define T_OPCODE_LDR_IB 0x7800
832 #define T_OPCODE_STR_IB 0x7000
833 #define T_OPCODE_LDR_RW 0x5800
834 #define T_OPCODE_STR_RW 0x5000
835 #define T_OPCODE_LDR_RH 0x5a00
836 #define T_OPCODE_STR_RH 0x5200
837 #define T_OPCODE_LDR_RB 0x5c00
838 #define T_OPCODE_STR_RB 0x5400
839
840 #define T_OPCODE_PUSH   0xb400
841 #define T_OPCODE_POP    0xbc00
842
843 #define T_OPCODE_BRANCH 0xe000
844
845 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
846 #define THUMB_PP_PC_LR 0x0100
847 #define THUMB_LOAD_BIT 0x0800
848 #define THUMB2_LOAD_BIT 0x00100000
849
850 #define BAD_SYNTAX      _("syntax error")
851 #define BAD_ARGS        _("bad arguments to instruction")
852 #define BAD_SP          _("r13 not allowed here")
853 #define BAD_PC          _("r15 not allowed here")
854 #define BAD_COND        _("instruction cannot be conditional")
855 #define BAD_OVERLAP     _("registers may not be the same")
856 #define BAD_HIREG       _("lo register required")
857 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
858 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
859 #define BAD_BRANCH      _("branch must be last instruction in IT block")
860 #define BAD_BRANCH_OFF  _("branch out of range or not a multiple of 2")
861 #define BAD_NOT_IT      _("instruction not allowed in IT block")
862 #define BAD_NOT_VPT     _("instruction missing MVE vector predication code")
863 #define BAD_FPU         _("selected FPU does not support instruction")
864 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
865 #define BAD_OUT_VPT     \
866         _("vector predicated instruction should be in VPT/VPST block")
867 #define BAD_IT_COND     _("incorrect condition in IT block")
868 #define BAD_VPT_COND    _("incorrect condition in VPT/VPST block")
869 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
870 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
871 #define BAD_PC_ADDRESSING \
872         _("cannot use register index with PC-relative addressing")
873 #define BAD_PC_WRITEBACK \
874         _("cannot use writeback with PC-relative addressing")
875 #define BAD_RANGE       _("branch out of range")
876 #define BAD_FP16        _("selected processor does not support fp16 instruction")
877 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
878 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
879 #define MVE_NOT_IT      _("Warning: instruction is UNPREDICTABLE in an IT " \
880                           "block")
881 #define MVE_NOT_VPT     _("Warning: instruction is UNPREDICTABLE in a VPT " \
882                           "block")
883 #define MVE_BAD_PC      _("Warning: instruction is UNPREDICTABLE with PC" \
884                           " operand")
885 #define MVE_BAD_SP      _("Warning: instruction is UNPREDICTABLE with SP" \
886                           " operand")
887
888 static struct hash_control * arm_ops_hsh;
889 static struct hash_control * arm_cond_hsh;
890 static struct hash_control * arm_vcond_hsh;
891 static struct hash_control * arm_shift_hsh;
892 static struct hash_control * arm_psr_hsh;
893 static struct hash_control * arm_v7m_psr_hsh;
894 static struct hash_control * arm_reg_hsh;
895 static struct hash_control * arm_reloc_hsh;
896 static struct hash_control * arm_barrier_opt_hsh;
897
898 /* Stuff needed to resolve the label ambiguity
899    As:
900      ...
901      label:   <insn>
902    may differ from:
903      ...
904      label:
905               <insn>  */
906
907 symbolS *  last_label_seen;
908 static int label_is_thumb_function_name = FALSE;
909
910 /* Literal pool structure.  Held on a per-section
911    and per-sub-section basis.  */
912
913 #define MAX_LITERAL_POOL_SIZE 1024
914 typedef struct literal_pool
915 {
916   expressionS            literals [MAX_LITERAL_POOL_SIZE];
917   unsigned int           next_free_entry;
918   unsigned int           id;
919   symbolS *              symbol;
920   segT                   section;
921   subsegT                sub_section;
922 #ifdef OBJ_ELF
923   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
924 #endif
925   struct literal_pool *  next;
926   unsigned int           alignment;
927 } literal_pool;
928
929 /* Pointer to a linked list of literal pools.  */
930 literal_pool * list_of_pools = NULL;
931
932 typedef enum asmfunc_states
933 {
934   OUTSIDE_ASMFUNC,
935   WAITING_ASMFUNC_NAME,
936   WAITING_ENDASMFUNC
937 } asmfunc_states;
938
939 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
940
941 #ifdef OBJ_ELF
942 #  define now_pred seg_info (now_seg)->tc_segment_info_data.current_pred
943 #else
944 static struct current_pred now_pred;
945 #endif
946
947 static inline int
948 now_pred_compatible (int cond)
949 {
950   return (cond & ~1) == (now_pred.cc & ~1);
951 }
952
953 static inline int
954 conditional_insn (void)
955 {
956   return inst.cond != COND_ALWAYS;
957 }
958
959 static int in_pred_block (void);
960
961 static int handle_pred_state (void);
962
963 static void force_automatic_it_block_close (void);
964
965 static void it_fsm_post_encode (void);
966
967 #define set_pred_insn_type(type)                        \
968   do                                            \
969     {                                           \
970       inst.pred_insn_type = type;                       \
971       if (handle_pred_state () == FAIL)         \
972         return;                                 \
973     }                                           \
974   while (0)
975
976 #define set_pred_insn_type_nonvoid(type, failret) \
977   do                                            \
978     {                                           \
979       inst.pred_insn_type = type;                       \
980       if (handle_pred_state () == FAIL)         \
981         return failret;                         \
982     }                                           \
983   while(0)
984
985 #define set_pred_insn_type_last()                               \
986   do                                                    \
987     {                                                   \
988       if (inst.cond == COND_ALWAYS)                     \
989         set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);    \
990       else                                              \
991         set_pred_insn_type (INSIDE_IT_LAST_INSN);               \
992     }                                                   \
993   while (0)
994
995 /* Pure syntax.  */
996
997 /* This array holds the chars that always start a comment.  If the
998    pre-processor is disabled, these aren't very useful.  */
999 char arm_comment_chars[] = "@";
1000
1001 /* This array holds the chars that only start a comment at the beginning of
1002    a line.  If the line seems to have the form '# 123 filename'
1003    .line and .file directives will appear in the pre-processed output.  */
1004 /* Note that input_file.c hand checks for '#' at the beginning of the
1005    first line of the input file.  This is because the compiler outputs
1006    #NO_APP at the beginning of its output.  */
1007 /* Also note that comments like this one will always work.  */
1008 const char line_comment_chars[] = "#";
1009
1010 char arm_line_separator_chars[] = ";";
1011
1012 /* Chars that can be used to separate mant
1013    from exp in floating point numbers.  */
1014 const char EXP_CHARS[] = "eE";
1015
1016 /* Chars that mean this number is a floating point constant.  */
1017 /* As in 0f12.456  */
1018 /* or    0d1.2345e12  */
1019
1020 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
1021
1022 /* Prefix characters that indicate the start of an immediate
1023    value.  */
1024 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
1025
1026 /* Separator character handling.  */
1027
1028 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
1029
1030 static inline int
1031 skip_past_char (char ** str, char c)
1032 {
1033   /* PR gas/14987: Allow for whitespace before the expected character.  */
1034   skip_whitespace (*str);
1035
1036   if (**str == c)
1037     {
1038       (*str)++;
1039       return SUCCESS;
1040     }
1041   else
1042     return FAIL;
1043 }
1044
1045 #define skip_past_comma(str) skip_past_char (str, ',')
1046
1047 /* Arithmetic expressions (possibly involving symbols).  */
1048
1049 /* Return TRUE if anything in the expression is a bignum.  */
1050
1051 static bfd_boolean
1052 walk_no_bignums (symbolS * sp)
1053 {
1054   if (symbol_get_value_expression (sp)->X_op == O_big)
1055     return TRUE;
1056
1057   if (symbol_get_value_expression (sp)->X_add_symbol)
1058     {
1059       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
1060               || (symbol_get_value_expression (sp)->X_op_symbol
1061                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
1062     }
1063
1064   return FALSE;
1065 }
1066
1067 static bfd_boolean in_my_get_expression = FALSE;
1068
1069 /* Third argument to my_get_expression.  */
1070 #define GE_NO_PREFIX 0
1071 #define GE_IMM_PREFIX 1
1072 #define GE_OPT_PREFIX 2
1073 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1074    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1075 #define GE_OPT_PREFIX_BIG 3
1076
1077 static int
1078 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1079 {
1080   char * save_in;
1081
1082   /* In unified syntax, all prefixes are optional.  */
1083   if (unified_syntax)
1084     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1085                   : GE_OPT_PREFIX;
1086
1087   switch (prefix_mode)
1088     {
1089     case GE_NO_PREFIX: break;
1090     case GE_IMM_PREFIX:
1091       if (!is_immediate_prefix (**str))
1092         {
1093           inst.error = _("immediate expression requires a # prefix");
1094           return FAIL;
1095         }
1096       (*str)++;
1097       break;
1098     case GE_OPT_PREFIX:
1099     case GE_OPT_PREFIX_BIG:
1100       if (is_immediate_prefix (**str))
1101         (*str)++;
1102       break;
1103     default:
1104       abort ();
1105     }
1106
1107   memset (ep, 0, sizeof (expressionS));
1108
1109   save_in = input_line_pointer;
1110   input_line_pointer = *str;
1111   in_my_get_expression = TRUE;
1112   expression (ep);
1113   in_my_get_expression = FALSE;
1114
1115   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1116     {
1117       /* We found a bad or missing expression in md_operand().  */
1118       *str = input_line_pointer;
1119       input_line_pointer = save_in;
1120       if (inst.error == NULL)
1121         inst.error = (ep->X_op == O_absent
1122                       ? _("missing expression") :_("bad expression"));
1123       return 1;
1124     }
1125
1126   /* Get rid of any bignums now, so that we don't generate an error for which
1127      we can't establish a line number later on.  Big numbers are never valid
1128      in instructions, which is where this routine is always called.  */
1129   if (prefix_mode != GE_OPT_PREFIX_BIG
1130       && (ep->X_op == O_big
1131           || (ep->X_add_symbol
1132               && (walk_no_bignums (ep->X_add_symbol)
1133                   || (ep->X_op_symbol
1134                       && walk_no_bignums (ep->X_op_symbol))))))
1135     {
1136       inst.error = _("invalid constant");
1137       *str = input_line_pointer;
1138       input_line_pointer = save_in;
1139       return 1;
1140     }
1141
1142   *str = input_line_pointer;
1143   input_line_pointer = save_in;
1144   return SUCCESS;
1145 }
1146
1147 /* Turn a string in input_line_pointer into a floating point constant
1148    of type TYPE, and store the appropriate bytes in *LITP.  The number
1149    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1150    returned, or NULL on OK.
1151
1152    Note that fp constants aren't represent in the normal way on the ARM.
1153    In big endian mode, things are as expected.  However, in little endian
1154    mode fp constants are big-endian word-wise, and little-endian byte-wise
1155    within the words.  For example, (double) 1.1 in big endian mode is
1156    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1157    the byte sequence 99 99 f1 3f 9a 99 99 99.
1158
1159    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1160
1161 const char *
1162 md_atof (int type, char * litP, int * sizeP)
1163 {
1164   int prec;
1165   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1166   char *t;
1167   int i;
1168
1169   switch (type)
1170     {
1171     case 'f':
1172     case 'F':
1173     case 's':
1174     case 'S':
1175       prec = 2;
1176       break;
1177
1178     case 'd':
1179     case 'D':
1180     case 'r':
1181     case 'R':
1182       prec = 4;
1183       break;
1184
1185     case 'x':
1186     case 'X':
1187       prec = 5;
1188       break;
1189
1190     case 'p':
1191     case 'P':
1192       prec = 5;
1193       break;
1194
1195     default:
1196       *sizeP = 0;
1197       return _("Unrecognized or unsupported floating point constant");
1198     }
1199
1200   t = atof_ieee (input_line_pointer, type, words);
1201   if (t)
1202     input_line_pointer = t;
1203   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1204
1205   if (target_big_endian)
1206     {
1207       for (i = 0; i < prec; i++)
1208         {
1209           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1210           litP += sizeof (LITTLENUM_TYPE);
1211         }
1212     }
1213   else
1214     {
1215       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1216         for (i = prec - 1; i >= 0; i--)
1217           {
1218             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1219             litP += sizeof (LITTLENUM_TYPE);
1220           }
1221       else
1222         /* For a 4 byte float the order of elements in `words' is 1 0.
1223            For an 8 byte float the order is 1 0 3 2.  */
1224         for (i = 0; i < prec; i += 2)
1225           {
1226             md_number_to_chars (litP, (valueT) words[i + 1],
1227                                 sizeof (LITTLENUM_TYPE));
1228             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1229                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1230             litP += 2 * sizeof (LITTLENUM_TYPE);
1231           }
1232     }
1233
1234   return NULL;
1235 }
1236
1237 /* We handle all bad expressions here, so that we can report the faulty
1238    instruction in the error message.  */
1239
1240 void
1241 md_operand (expressionS * exp)
1242 {
1243   if (in_my_get_expression)
1244     exp->X_op = O_illegal;
1245 }
1246
1247 /* Immediate values.  */
1248
1249 #ifdef OBJ_ELF
1250 /* Generic immediate-value read function for use in directives.
1251    Accepts anything that 'expression' can fold to a constant.
1252    *val receives the number.  */
1253
1254 static int
1255 immediate_for_directive (int *val)
1256 {
1257   expressionS exp;
1258   exp.X_op = O_illegal;
1259
1260   if (is_immediate_prefix (*input_line_pointer))
1261     {
1262       input_line_pointer++;
1263       expression (&exp);
1264     }
1265
1266   if (exp.X_op != O_constant)
1267     {
1268       as_bad (_("expected #constant"));
1269       ignore_rest_of_line ();
1270       return FAIL;
1271     }
1272   *val = exp.X_add_number;
1273   return SUCCESS;
1274 }
1275 #endif
1276
1277 /* Register parsing.  */
1278
1279 /* Generic register parser.  CCP points to what should be the
1280    beginning of a register name.  If it is indeed a valid register
1281    name, advance CCP over it and return the reg_entry structure;
1282    otherwise return NULL.  Does not issue diagnostics.  */
1283
1284 static struct reg_entry *
1285 arm_reg_parse_multi (char **ccp)
1286 {
1287   char *start = *ccp;
1288   char *p;
1289   struct reg_entry *reg;
1290
1291   skip_whitespace (start);
1292
1293 #ifdef REGISTER_PREFIX
1294   if (*start != REGISTER_PREFIX)
1295     return NULL;
1296   start++;
1297 #endif
1298 #ifdef OPTIONAL_REGISTER_PREFIX
1299   if (*start == OPTIONAL_REGISTER_PREFIX)
1300     start++;
1301 #endif
1302
1303   p = start;
1304   if (!ISALPHA (*p) || !is_name_beginner (*p))
1305     return NULL;
1306
1307   do
1308     p++;
1309   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1310
1311   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1312
1313   if (!reg)
1314     return NULL;
1315
1316   *ccp = p;
1317   return reg;
1318 }
1319
1320 static int
1321 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1322                     enum arm_reg_type type)
1323 {
1324   /* Alternative syntaxes are accepted for a few register classes.  */
1325   switch (type)
1326     {
1327     case REG_TYPE_MVF:
1328     case REG_TYPE_MVD:
1329     case REG_TYPE_MVFX:
1330     case REG_TYPE_MVDX:
1331       /* Generic coprocessor register names are allowed for these.  */
1332       if (reg && reg->type == REG_TYPE_CN)
1333         return reg->number;
1334       break;
1335
1336     case REG_TYPE_CP:
1337       /* For backward compatibility, a bare number is valid here.  */
1338       {
1339         unsigned long processor = strtoul (start, ccp, 10);
1340         if (*ccp != start && processor <= 15)
1341           return processor;
1342       }
1343       /* Fall through.  */
1344
1345     case REG_TYPE_MMXWC:
1346       /* WC includes WCG.  ??? I'm not sure this is true for all
1347          instructions that take WC registers.  */
1348       if (reg && reg->type == REG_TYPE_MMXWCG)
1349         return reg->number;
1350       break;
1351
1352     default:
1353       break;
1354     }
1355
1356   return FAIL;
1357 }
1358
1359 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1360    return value is the register number or FAIL.  */
1361
1362 static int
1363 arm_reg_parse (char **ccp, enum arm_reg_type type)
1364 {
1365   char *start = *ccp;
1366   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1367   int ret;
1368
1369   /* Do not allow a scalar (reg+index) to parse as a register.  */
1370   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1371     return FAIL;
1372
1373   if (reg && reg->type == type)
1374     return reg->number;
1375
1376   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1377     return ret;
1378
1379   *ccp = start;
1380   return FAIL;
1381 }
1382
1383 /* Parse a Neon type specifier. *STR should point at the leading '.'
1384    character. Does no verification at this stage that the type fits the opcode
1385    properly. E.g.,
1386
1387      .i32.i32.s16
1388      .s32.f32
1389      .u16
1390
1391    Can all be legally parsed by this function.
1392
1393    Fills in neon_type struct pointer with parsed information, and updates STR
1394    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1395    type, FAIL if not.  */
1396
1397 static int
1398 parse_neon_type (struct neon_type *type, char **str)
1399 {
1400   char *ptr = *str;
1401
1402   if (type)
1403     type->elems = 0;
1404
1405   while (type->elems < NEON_MAX_TYPE_ELS)
1406     {
1407       enum neon_el_type thistype = NT_untyped;
1408       unsigned thissize = -1u;
1409
1410       if (*ptr != '.')
1411         break;
1412
1413       ptr++;
1414
1415       /* Just a size without an explicit type.  */
1416       if (ISDIGIT (*ptr))
1417         goto parsesize;
1418
1419       switch (TOLOWER (*ptr))
1420         {
1421         case 'i': thistype = NT_integer; break;
1422         case 'f': thistype = NT_float; break;
1423         case 'p': thistype = NT_poly; break;
1424         case 's': thistype = NT_signed; break;
1425         case 'u': thistype = NT_unsigned; break;
1426         case 'd':
1427           thistype = NT_float;
1428           thissize = 64;
1429           ptr++;
1430           goto done;
1431         default:
1432           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1433           return FAIL;
1434         }
1435
1436       ptr++;
1437
1438       /* .f is an abbreviation for .f32.  */
1439       if (thistype == NT_float && !ISDIGIT (*ptr))
1440         thissize = 32;
1441       else
1442         {
1443         parsesize:
1444           thissize = strtoul (ptr, &ptr, 10);
1445
1446           if (thissize != 8 && thissize != 16 && thissize != 32
1447               && thissize != 64)
1448             {
1449               as_bad (_("bad size %d in type specifier"), thissize);
1450               return FAIL;
1451             }
1452         }
1453
1454       done:
1455       if (type)
1456         {
1457           type->el[type->elems].type = thistype;
1458           type->el[type->elems].size = thissize;
1459           type->elems++;
1460         }
1461     }
1462
1463   /* Empty/missing type is not a successful parse.  */
1464   if (type->elems == 0)
1465     return FAIL;
1466
1467   *str = ptr;
1468
1469   return SUCCESS;
1470 }
1471
1472 /* Errors may be set multiple times during parsing or bit encoding
1473    (particularly in the Neon bits), but usually the earliest error which is set
1474    will be the most meaningful. Avoid overwriting it with later (cascading)
1475    errors by calling this function.  */
1476
1477 static void
1478 first_error (const char *err)
1479 {
1480   if (!inst.error)
1481     inst.error = err;
1482 }
1483
1484 /* Parse a single type, e.g. ".s32", leading period included.  */
1485 static int
1486 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1487 {
1488   char *str = *ccp;
1489   struct neon_type optype;
1490
1491   if (*str == '.')
1492     {
1493       if (parse_neon_type (&optype, &str) == SUCCESS)
1494         {
1495           if (optype.elems == 1)
1496             *vectype = optype.el[0];
1497           else
1498             {
1499               first_error (_("only one type should be specified for operand"));
1500               return FAIL;
1501             }
1502         }
1503       else
1504         {
1505           first_error (_("vector type expected"));
1506           return FAIL;
1507         }
1508     }
1509   else
1510     return FAIL;
1511
1512   *ccp = str;
1513
1514   return SUCCESS;
1515 }
1516
1517 /* Special meanings for indices (which have a range of 0-7), which will fit into
1518    a 4-bit integer.  */
1519
1520 #define NEON_ALL_LANES          15
1521 #define NEON_INTERLEAVE_LANES   14
1522
1523 /* Record a use of the given feature.  */
1524 static void
1525 record_feature_use (const arm_feature_set *feature)
1526 {
1527   if (thumb_mode)
1528     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
1529   else
1530     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
1531 }
1532
1533 /* If the given feature available in the selected CPU, mark it as used.
1534    Returns TRUE iff feature is available.  */
1535 static bfd_boolean
1536 mark_feature_used (const arm_feature_set *feature)
1537 {
1538   /* Ensure the option is valid on the current architecture.  */
1539   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
1540     return FALSE;
1541
1542   /* Add the appropriate architecture feature for the barrier option used.
1543      */
1544   record_feature_use (feature);
1545
1546   return TRUE;
1547 }
1548
1549 /* Parse either a register or a scalar, with an optional type. Return the
1550    register number, and optionally fill in the actual type of the register
1551    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1552    type/index information in *TYPEINFO.  */
1553
1554 static int
1555 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1556                            enum arm_reg_type *rtype,
1557                            struct neon_typed_alias *typeinfo)
1558 {
1559   char *str = *ccp;
1560   struct reg_entry *reg = arm_reg_parse_multi (&str);
1561   struct neon_typed_alias atype;
1562   struct neon_type_el parsetype;
1563
1564   atype.defined = 0;
1565   atype.index = -1;
1566   atype.eltype.type = NT_invtype;
1567   atype.eltype.size = -1;
1568
1569   /* Try alternate syntax for some types of register. Note these are mutually
1570      exclusive with the Neon syntax extensions.  */
1571   if (reg == NULL)
1572     {
1573       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1574       if (altreg != FAIL)
1575         *ccp = str;
1576       if (typeinfo)
1577         *typeinfo = atype;
1578       return altreg;
1579     }
1580
1581   /* Undo polymorphism when a set of register types may be accepted.  */
1582   if ((type == REG_TYPE_NDQ
1583        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1584       || (type == REG_TYPE_VFSD
1585           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1586       || (type == REG_TYPE_NSDQ
1587           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1588               || reg->type == REG_TYPE_NQ))
1589       || (type == REG_TYPE_NSD
1590           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1591       || (type == REG_TYPE_MMXWC
1592           && (reg->type == REG_TYPE_MMXWCG)))
1593     type = (enum arm_reg_type) reg->type;
1594
1595   if (type == REG_TYPE_MQ)
1596     {
1597       if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
1598         return FAIL;
1599
1600       if (!reg || reg->type != REG_TYPE_NQ)
1601         return FAIL;
1602
1603       if (reg->number > 14 && !mark_feature_used (&fpu_vfp_ext_d32))
1604         {
1605           first_error (_("expected MVE register [q0..q7]"));
1606           return FAIL;
1607         }
1608       type = REG_TYPE_NQ;
1609     }
1610   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
1611            && (type == REG_TYPE_NQ))
1612     return FAIL;
1613
1614
1615   if (type != reg->type)
1616     return FAIL;
1617
1618   if (reg->neon)
1619     atype = *reg->neon;
1620
1621   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1622     {
1623       if ((atype.defined & NTA_HASTYPE) != 0)
1624         {
1625           first_error (_("can't redefine type for operand"));
1626           return FAIL;
1627         }
1628       atype.defined |= NTA_HASTYPE;
1629       atype.eltype = parsetype;
1630     }
1631
1632   if (skip_past_char (&str, '[') == SUCCESS)
1633     {
1634       if (type != REG_TYPE_VFD
1635           && !(type == REG_TYPE_VFS
1636                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2)))
1637         {
1638           first_error (_("only D registers may be indexed"));
1639           return FAIL;
1640         }
1641
1642       if ((atype.defined & NTA_HASINDEX) != 0)
1643         {
1644           first_error (_("can't change index for operand"));
1645           return FAIL;
1646         }
1647
1648       atype.defined |= NTA_HASINDEX;
1649
1650       if (skip_past_char (&str, ']') == SUCCESS)
1651         atype.index = NEON_ALL_LANES;
1652       else
1653         {
1654           expressionS exp;
1655
1656           my_get_expression (&exp, &str, GE_NO_PREFIX);
1657
1658           if (exp.X_op != O_constant)
1659             {
1660               first_error (_("constant expression required"));
1661               return FAIL;
1662             }
1663
1664           if (skip_past_char (&str, ']') == FAIL)
1665             return FAIL;
1666
1667           atype.index = exp.X_add_number;
1668         }
1669     }
1670
1671   if (typeinfo)
1672     *typeinfo = atype;
1673
1674   if (rtype)
1675     *rtype = type;
1676
1677   *ccp = str;
1678
1679   return reg->number;
1680 }
1681
1682 /* Like arm_reg_parse, but also allow the following extra features:
1683     - If RTYPE is non-zero, return the (possibly restricted) type of the
1684       register (e.g. Neon double or quad reg when either has been requested).
1685     - If this is a Neon vector type with additional type information, fill
1686       in the struct pointed to by VECTYPE (if non-NULL).
1687    This function will fault on encountering a scalar.  */
1688
1689 static int
1690 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1691                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1692 {
1693   struct neon_typed_alias atype;
1694   char *str = *ccp;
1695   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1696
1697   if (reg == FAIL)
1698     return FAIL;
1699
1700   /* Do not allow regname(... to parse as a register.  */
1701   if (*str == '(')
1702     return FAIL;
1703
1704   /* Do not allow a scalar (reg+index) to parse as a register.  */
1705   if ((atype.defined & NTA_HASINDEX) != 0)
1706     {
1707       first_error (_("register operand expected, but got scalar"));
1708       return FAIL;
1709     }
1710
1711   if (vectype)
1712     *vectype = atype.eltype;
1713
1714   *ccp = str;
1715
1716   return reg;
1717 }
1718
1719 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1720 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1721
1722 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1723    have enough information to be able to do a good job bounds-checking. So, we
1724    just do easy checks here, and do further checks later.  */
1725
1726 static int
1727 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1728 {
1729   int reg;
1730   char *str = *ccp;
1731   struct neon_typed_alias atype;
1732   enum arm_reg_type reg_type = REG_TYPE_VFD;
1733
1734   if (elsize == 4)
1735     reg_type = REG_TYPE_VFS;
1736
1737   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1738
1739   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1740     return FAIL;
1741
1742   if (atype.index == NEON_ALL_LANES)
1743     {
1744       first_error (_("scalar must have an index"));
1745       return FAIL;
1746     }
1747   else if (atype.index >= 64 / elsize)
1748     {
1749       first_error (_("scalar index out of range"));
1750       return FAIL;
1751     }
1752
1753   if (type)
1754     *type = atype.eltype;
1755
1756   *ccp = str;
1757
1758   return reg * 16 + atype.index;
1759 }
1760
1761 /* Types of registers in a list.  */
1762
1763 enum reg_list_els
1764 {
1765   REGLIST_RN,
1766   REGLIST_CLRM,
1767   REGLIST_VFP_S,
1768   REGLIST_VFP_S_VPR,
1769   REGLIST_VFP_D,
1770   REGLIST_VFP_D_VPR,
1771   REGLIST_NEON_D
1772 };
1773
1774 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1775
1776 static long
1777 parse_reg_list (char ** strp, enum reg_list_els etype)
1778 {
1779   char *str = *strp;
1780   long range = 0;
1781   int another_range;
1782
1783   gas_assert (etype == REGLIST_RN || etype == REGLIST_CLRM);
1784
1785   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1786   do
1787     {
1788       skip_whitespace (str);
1789
1790       another_range = 0;
1791
1792       if (*str == '{')
1793         {
1794           int in_range = 0;
1795           int cur_reg = -1;
1796
1797           str++;
1798           do
1799             {
1800               int reg;
1801               const char apsr_str[] = "apsr";
1802               int apsr_str_len = strlen (apsr_str);
1803
1804               reg = arm_reg_parse (&str, REGLIST_RN);
1805               if (etype == REGLIST_CLRM)
1806                 {
1807                   if (reg == REG_SP || reg == REG_PC)
1808                     reg = FAIL;
1809                   else if (reg == FAIL
1810                            && !strncasecmp (str, apsr_str, apsr_str_len)
1811                            && !ISALPHA (*(str + apsr_str_len)))
1812                     {
1813                       reg = 15;
1814                       str += apsr_str_len;
1815                     }
1816
1817                   if (reg == FAIL)
1818                     {
1819                       first_error (_("r0-r12, lr or APSR expected"));
1820                       return FAIL;
1821                     }
1822                 }
1823               else /* etype == REGLIST_RN.  */
1824                 {
1825                   if (reg == FAIL)
1826                     {
1827                       first_error (_(reg_expected_msgs[REGLIST_RN]));
1828                       return FAIL;
1829                     }
1830                 }
1831
1832               if (in_range)
1833                 {
1834                   int i;
1835
1836                   if (reg <= cur_reg)
1837                     {
1838                       first_error (_("bad range in register list"));
1839                       return FAIL;
1840                     }
1841
1842                   for (i = cur_reg + 1; i < reg; i++)
1843                     {
1844                       if (range & (1 << i))
1845                         as_tsktsk
1846                           (_("Warning: duplicated register (r%d) in register list"),
1847                            i);
1848                       else
1849                         range |= 1 << i;
1850                     }
1851                   in_range = 0;
1852                 }
1853
1854               if (range & (1 << reg))
1855                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1856                            reg);
1857               else if (reg <= cur_reg)
1858                 as_tsktsk (_("Warning: register range not in ascending order"));
1859
1860               range |= 1 << reg;
1861               cur_reg = reg;
1862             }
1863           while (skip_past_comma (&str) != FAIL
1864                  || (in_range = 1, *str++ == '-'));
1865           str--;
1866
1867           if (skip_past_char (&str, '}') == FAIL)
1868             {
1869               first_error (_("missing `}'"));
1870               return FAIL;
1871             }
1872         }
1873       else if (etype == REGLIST_RN)
1874         {
1875           expressionS exp;
1876
1877           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1878             return FAIL;
1879
1880           if (exp.X_op == O_constant)
1881             {
1882               if (exp.X_add_number
1883                   != (exp.X_add_number & 0x0000ffff))
1884                 {
1885                   inst.error = _("invalid register mask");
1886                   return FAIL;
1887                 }
1888
1889               if ((range & exp.X_add_number) != 0)
1890                 {
1891                   int regno = range & exp.X_add_number;
1892
1893                   regno &= -regno;
1894                   regno = (1 << regno) - 1;
1895                   as_tsktsk
1896                     (_("Warning: duplicated register (r%d) in register list"),
1897                      regno);
1898                 }
1899
1900               range |= exp.X_add_number;
1901             }
1902           else
1903             {
1904               if (inst.relocs[0].type != 0)
1905                 {
1906                   inst.error = _("expression too complex");
1907                   return FAIL;
1908                 }
1909
1910               memcpy (&inst.relocs[0].exp, &exp, sizeof (expressionS));
1911               inst.relocs[0].type = BFD_RELOC_ARM_MULTI;
1912               inst.relocs[0].pc_rel = 0;
1913             }
1914         }
1915
1916       if (*str == '|' || *str == '+')
1917         {
1918           str++;
1919           another_range = 1;
1920         }
1921     }
1922   while (another_range);
1923
1924   *strp = str;
1925   return range;
1926 }
1927
1928 /* Parse a VFP register list.  If the string is invalid return FAIL.
1929    Otherwise return the number of registers, and set PBASE to the first
1930    register.  Parses registers of type ETYPE.
1931    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1932      - Q registers can be used to specify pairs of D registers
1933      - { } can be omitted from around a singleton register list
1934          FIXME: This is not implemented, as it would require backtracking in
1935          some cases, e.g.:
1936            vtbl.8 d3,d4,d5
1937          This could be done (the meaning isn't really ambiguous), but doesn't
1938          fit in well with the current parsing framework.
1939      - 32 D registers may be used (also true for VFPv3).
1940    FIXME: Types are ignored in these register lists, which is probably a
1941    bug.  */
1942
1943 static int
1944 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype,
1945                     bfd_boolean *partial_match)
1946 {
1947   char *str = *ccp;
1948   int base_reg;
1949   int new_base;
1950   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1951   int max_regs = 0;
1952   int count = 0;
1953   int warned = 0;
1954   unsigned long mask = 0;
1955   int i;
1956   bfd_boolean vpr_seen = FALSE;
1957   bfd_boolean expect_vpr =
1958     (etype == REGLIST_VFP_S_VPR) || (etype == REGLIST_VFP_D_VPR);
1959
1960   if (skip_past_char (&str, '{') == FAIL)
1961     {
1962       inst.error = _("expecting {");
1963       return FAIL;
1964     }
1965
1966   switch (etype)
1967     {
1968     case REGLIST_VFP_S:
1969     case REGLIST_VFP_S_VPR:
1970       regtype = REG_TYPE_VFS;
1971       max_regs = 32;
1972       break;
1973
1974     case REGLIST_VFP_D:
1975     case REGLIST_VFP_D_VPR:
1976       regtype = REG_TYPE_VFD;
1977       break;
1978
1979     case REGLIST_NEON_D:
1980       regtype = REG_TYPE_NDQ;
1981       break;
1982
1983     default:
1984       gas_assert (0);
1985     }
1986
1987   if (etype != REGLIST_VFP_S && etype != REGLIST_VFP_S_VPR)
1988     {
1989       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1990       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1991         {
1992           max_regs = 32;
1993           if (thumb_mode)
1994             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1995                                     fpu_vfp_ext_d32);
1996           else
1997             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1998                                     fpu_vfp_ext_d32);
1999         }
2000       else
2001         max_regs = 16;
2002     }
2003
2004   base_reg = max_regs;
2005   *partial_match = FALSE;
2006
2007   do
2008     {
2009       int setmask = 1, addregs = 1;
2010       const char vpr_str[] = "vpr";
2011       int vpr_str_len = strlen (vpr_str);
2012
2013       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
2014
2015       if (expect_vpr)
2016         {
2017           if (new_base == FAIL
2018               && !strncasecmp (str, vpr_str, vpr_str_len)
2019               && !ISALPHA (*(str + vpr_str_len))
2020               && !vpr_seen)
2021             {
2022               vpr_seen = TRUE;
2023               str += vpr_str_len;
2024               if (count == 0)
2025                 base_reg = 0; /* Canonicalize VPR only on d0 with 0 regs.  */
2026             }
2027           else if (vpr_seen)
2028             {
2029               first_error (_("VPR expected last"));
2030               return FAIL;
2031             }
2032           else if (new_base == FAIL)
2033             {
2034               if (regtype == REG_TYPE_VFS)
2035                 first_error (_("VFP single precision register or VPR "
2036                                "expected"));
2037               else /* regtype == REG_TYPE_VFD.  */
2038                 first_error (_("VFP/Neon double precision register or VPR "
2039                                "expected"));
2040               return FAIL;
2041             }
2042         }
2043       else if (new_base == FAIL)
2044         {
2045           first_error (_(reg_expected_msgs[regtype]));
2046           return FAIL;
2047         }
2048
2049       *partial_match = TRUE;
2050       if (vpr_seen)
2051         continue;
2052
2053       if (new_base >= max_regs)
2054         {
2055           first_error (_("register out of range in list"));
2056           return FAIL;
2057         }
2058
2059       /* Note: a value of 2 * n is returned for the register Q<n>.  */
2060       if (regtype == REG_TYPE_NQ)
2061         {
2062           setmask = 3;
2063           addregs = 2;
2064         }
2065
2066       if (new_base < base_reg)
2067         base_reg = new_base;
2068
2069       if (mask & (setmask << new_base))
2070         {
2071           first_error (_("invalid register list"));
2072           return FAIL;
2073         }
2074
2075       if ((mask >> new_base) != 0 && ! warned && !vpr_seen)
2076         {
2077           as_tsktsk (_("register list not in ascending order"));
2078           warned = 1;
2079         }
2080
2081       mask |= setmask << new_base;
2082       count += addregs;
2083
2084       if (*str == '-') /* We have the start of a range expression */
2085         {
2086           int high_range;
2087
2088           str++;
2089
2090           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
2091               == FAIL)
2092             {
2093               inst.error = gettext (reg_expected_msgs[regtype]);
2094               return FAIL;
2095             }
2096
2097           if (high_range >= max_regs)
2098             {
2099               first_error (_("register out of range in list"));
2100               return FAIL;
2101             }
2102
2103           if (regtype == REG_TYPE_NQ)
2104             high_range = high_range + 1;
2105
2106           if (high_range <= new_base)
2107             {
2108               inst.error = _("register range not in ascending order");
2109               return FAIL;
2110             }
2111
2112           for (new_base += addregs; new_base <= high_range; new_base += addregs)
2113             {
2114               if (mask & (setmask << new_base))
2115                 {
2116                   inst.error = _("invalid register list");
2117                   return FAIL;
2118                 }
2119
2120               mask |= setmask << new_base;
2121               count += addregs;
2122             }
2123         }
2124     }
2125   while (skip_past_comma (&str) != FAIL);
2126
2127   str++;
2128
2129   /* Sanity check -- should have raised a parse error above.  */
2130   if ((!vpr_seen && count == 0) || count > max_regs)
2131     abort ();
2132
2133   *pbase = base_reg;
2134
2135   if (expect_vpr && !vpr_seen)
2136     {
2137       first_error (_("VPR expected last"));
2138       return FAIL;
2139     }
2140
2141   /* Final test -- the registers must be consecutive.  */
2142   mask >>= base_reg;
2143   for (i = 0; i < count; i++)
2144     {
2145       if ((mask & (1u << i)) == 0)
2146         {
2147           inst.error = _("non-contiguous register range");
2148           return FAIL;
2149         }
2150     }
2151
2152   *ccp = str;
2153
2154   return count;
2155 }
2156
2157 /* True if two alias types are the same.  */
2158
2159 static bfd_boolean
2160 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
2161 {
2162   if (!a && !b)
2163     return TRUE;
2164
2165   if (!a || !b)
2166     return FALSE;
2167
2168   if (a->defined != b->defined)
2169     return FALSE;
2170
2171   if ((a->defined & NTA_HASTYPE) != 0
2172       && (a->eltype.type != b->eltype.type
2173           || a->eltype.size != b->eltype.size))
2174     return FALSE;
2175
2176   if ((a->defined & NTA_HASINDEX) != 0
2177       && (a->index != b->index))
2178     return FALSE;
2179
2180   return TRUE;
2181 }
2182
2183 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2184    The base register is put in *PBASE.
2185    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2186    the return value.
2187    The register stride (minus one) is put in bit 4 of the return value.
2188    Bits [6:5] encode the list length (minus one).
2189    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2190
2191 #define NEON_LANE(X)            ((X) & 0xf)
2192 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2193 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2194
2195 static int
2196 parse_neon_el_struct_list (char **str, unsigned *pbase,
2197                            struct neon_type_el *eltype)
2198 {
2199   char *ptr = *str;
2200   int base_reg = -1;
2201   int reg_incr = -1;
2202   int count = 0;
2203   int lane = -1;
2204   int leading_brace = 0;
2205   enum arm_reg_type rtype = REG_TYPE_NDQ;
2206   const char *const incr_error = _("register stride must be 1 or 2");
2207   const char *const type_error = _("mismatched element/structure types in list");
2208   struct neon_typed_alias firsttype;
2209   firsttype.defined = 0;
2210   firsttype.eltype.type = NT_invtype;
2211   firsttype.eltype.size = -1;
2212   firsttype.index = -1;
2213
2214   if (skip_past_char (&ptr, '{') == SUCCESS)
2215     leading_brace = 1;
2216
2217   do
2218     {
2219       struct neon_typed_alias atype;
2220       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2221
2222       if (getreg == FAIL)
2223         {
2224           first_error (_(reg_expected_msgs[rtype]));
2225           return FAIL;
2226         }
2227
2228       if (base_reg == -1)
2229         {
2230           base_reg = getreg;
2231           if (rtype == REG_TYPE_NQ)
2232             {
2233               reg_incr = 1;
2234             }
2235           firsttype = atype;
2236         }
2237       else if (reg_incr == -1)
2238         {
2239           reg_incr = getreg - base_reg;
2240           if (reg_incr < 1 || reg_incr > 2)
2241             {
2242               first_error (_(incr_error));
2243               return FAIL;
2244             }
2245         }
2246       else if (getreg != base_reg + reg_incr * count)
2247         {
2248           first_error (_(incr_error));
2249           return FAIL;
2250         }
2251
2252       if (! neon_alias_types_same (&atype, &firsttype))
2253         {
2254           first_error (_(type_error));
2255           return FAIL;
2256         }
2257
2258       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2259          modes.  */
2260       if (ptr[0] == '-')
2261         {
2262           struct neon_typed_alias htype;
2263           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2264           if (lane == -1)
2265             lane = NEON_INTERLEAVE_LANES;
2266           else if (lane != NEON_INTERLEAVE_LANES)
2267             {
2268               first_error (_(type_error));
2269               return FAIL;
2270             }
2271           if (reg_incr == -1)
2272             reg_incr = 1;
2273           else if (reg_incr != 1)
2274             {
2275               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2276               return FAIL;
2277             }
2278           ptr++;
2279           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2280           if (hireg == FAIL)
2281             {
2282               first_error (_(reg_expected_msgs[rtype]));
2283               return FAIL;
2284             }
2285           if (! neon_alias_types_same (&htype, &firsttype))
2286             {
2287               first_error (_(type_error));
2288               return FAIL;
2289             }
2290           count += hireg + dregs - getreg;
2291           continue;
2292         }
2293
2294       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2295       if (rtype == REG_TYPE_NQ)
2296         {
2297           count += 2;
2298           continue;
2299         }
2300
2301       if ((atype.defined & NTA_HASINDEX) != 0)
2302         {
2303           if (lane == -1)
2304             lane = atype.index;
2305           else if (lane != atype.index)
2306             {
2307               first_error (_(type_error));
2308               return FAIL;
2309             }
2310         }
2311       else if (lane == -1)
2312         lane = NEON_INTERLEAVE_LANES;
2313       else if (lane != NEON_INTERLEAVE_LANES)
2314         {
2315           first_error (_(type_error));
2316           return FAIL;
2317         }
2318       count++;
2319     }
2320   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2321
2322   /* No lane set by [x]. We must be interleaving structures.  */
2323   if (lane == -1)
2324     lane = NEON_INTERLEAVE_LANES;
2325
2326   /* Sanity check.  */
2327   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2328       || (count > 1 && reg_incr == -1))
2329     {
2330       first_error (_("error parsing element/structure list"));
2331       return FAIL;
2332     }
2333
2334   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2335     {
2336       first_error (_("expected }"));
2337       return FAIL;
2338     }
2339
2340   if (reg_incr == -1)
2341     reg_incr = 1;
2342
2343   if (eltype)
2344     *eltype = firsttype.eltype;
2345
2346   *pbase = base_reg;
2347   *str = ptr;
2348
2349   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2350 }
2351
2352 /* Parse an explicit relocation suffix on an expression.  This is
2353    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2354    arm_reloc_hsh contains no entries, so this function can only
2355    succeed if there is no () after the word.  Returns -1 on error,
2356    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2357
2358 static int
2359 parse_reloc (char **str)
2360 {
2361   struct reloc_entry *r;
2362   char *p, *q;
2363
2364   if (**str != '(')
2365     return BFD_RELOC_UNUSED;
2366
2367   p = *str + 1;
2368   q = p;
2369
2370   while (*q && *q != ')' && *q != ',')
2371     q++;
2372   if (*q != ')')
2373     return -1;
2374
2375   if ((r = (struct reloc_entry *)
2376        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2377     return -1;
2378
2379   *str = q + 1;
2380   return r->reloc;
2381 }
2382
2383 /* Directives: register aliases.  */
2384
2385 static struct reg_entry *
2386 insert_reg_alias (char *str, unsigned number, int type)
2387 {
2388   struct reg_entry *new_reg;
2389   const char *name;
2390
2391   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2392     {
2393       if (new_reg->builtin)
2394         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2395
2396       /* Only warn about a redefinition if it's not defined as the
2397          same register.  */
2398       else if (new_reg->number != number || new_reg->type != type)
2399         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2400
2401       return NULL;
2402     }
2403
2404   name = xstrdup (str);
2405   new_reg = XNEW (struct reg_entry);
2406
2407   new_reg->name = name;
2408   new_reg->number = number;
2409   new_reg->type = type;
2410   new_reg->builtin = FALSE;
2411   new_reg->neon = NULL;
2412
2413   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2414     abort ();
2415
2416   return new_reg;
2417 }
2418
2419 static void
2420 insert_neon_reg_alias (char *str, int number, int type,
2421                        struct neon_typed_alias *atype)
2422 {
2423   struct reg_entry *reg = insert_reg_alias (str, number, type);
2424
2425   if (!reg)
2426     {
2427       first_error (_("attempt to redefine typed alias"));
2428       return;
2429     }
2430
2431   if (atype)
2432     {
2433       reg->neon = XNEW (struct neon_typed_alias);
2434       *reg->neon = *atype;
2435     }
2436 }
2437
2438 /* Look for the .req directive.  This is of the form:
2439
2440         new_register_name .req existing_register_name
2441
2442    If we find one, or if it looks sufficiently like one that we want to
2443    handle any error here, return TRUE.  Otherwise return FALSE.  */
2444
2445 static bfd_boolean
2446 create_register_alias (char * newname, char *p)
2447 {
2448   struct reg_entry *old;
2449   char *oldname, *nbuf;
2450   size_t nlen;
2451
2452   /* The input scrubber ensures that whitespace after the mnemonic is
2453      collapsed to single spaces.  */
2454   oldname = p;
2455   if (strncmp (oldname, " .req ", 6) != 0)
2456     return FALSE;
2457
2458   oldname += 6;
2459   if (*oldname == '\0')
2460     return FALSE;
2461
2462   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2463   if (!old)
2464     {
2465       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2466       return TRUE;
2467     }
2468
2469   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2470      the desired alias name, and p points to its end.  If not, then
2471      the desired alias name is in the global original_case_string.  */
2472 #ifdef TC_CASE_SENSITIVE
2473   nlen = p - newname;
2474 #else
2475   newname = original_case_string;
2476   nlen = strlen (newname);
2477 #endif
2478
2479   nbuf = xmemdup0 (newname, nlen);
2480
2481   /* Create aliases under the new name as stated; an all-lowercase
2482      version of the new name; and an all-uppercase version of the new
2483      name.  */
2484   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2485     {
2486       for (p = nbuf; *p; p++)
2487         *p = TOUPPER (*p);
2488
2489       if (strncmp (nbuf, newname, nlen))
2490         {
2491           /* If this attempt to create an additional alias fails, do not bother
2492              trying to create the all-lower case alias.  We will fail and issue
2493              a second, duplicate error message.  This situation arises when the
2494              programmer does something like:
2495                foo .req r0
2496                Foo .req r1
2497              The second .req creates the "Foo" alias but then fails to create
2498              the artificial FOO alias because it has already been created by the
2499              first .req.  */
2500           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2501             {
2502               free (nbuf);
2503               return TRUE;
2504             }
2505         }
2506
2507       for (p = nbuf; *p; p++)
2508         *p = TOLOWER (*p);
2509
2510       if (strncmp (nbuf, newname, nlen))
2511         insert_reg_alias (nbuf, old->number, old->type);
2512     }
2513
2514   free (nbuf);
2515   return TRUE;
2516 }
2517
2518 /* Create a Neon typed/indexed register alias using directives, e.g.:
2519      X .dn d5.s32[1]
2520      Y .qn 6.s16
2521      Z .dn d7
2522      T .dn Z[0]
2523    These typed registers can be used instead of the types specified after the
2524    Neon mnemonic, so long as all operands given have types. Types can also be
2525    specified directly, e.g.:
2526      vadd d0.s32, d1.s32, d2.s32  */
2527
2528 static bfd_boolean
2529 create_neon_reg_alias (char *newname, char *p)
2530 {
2531   enum arm_reg_type basetype;
2532   struct reg_entry *basereg;
2533   struct reg_entry mybasereg;
2534   struct neon_type ntype;
2535   struct neon_typed_alias typeinfo;
2536   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2537   int namelen;
2538
2539   typeinfo.defined = 0;
2540   typeinfo.eltype.type = NT_invtype;
2541   typeinfo.eltype.size = -1;
2542   typeinfo.index = -1;
2543
2544   nameend = p;
2545
2546   if (strncmp (p, " .dn ", 5) == 0)
2547     basetype = REG_TYPE_VFD;
2548   else if (strncmp (p, " .qn ", 5) == 0)
2549     basetype = REG_TYPE_NQ;
2550   else
2551     return FALSE;
2552
2553   p += 5;
2554
2555   if (*p == '\0')
2556     return FALSE;
2557
2558   basereg = arm_reg_parse_multi (&p);
2559
2560   if (basereg && basereg->type != basetype)
2561     {
2562       as_bad (_("bad type for register"));
2563       return FALSE;
2564     }
2565
2566   if (basereg == NULL)
2567     {
2568       expressionS exp;
2569       /* Try parsing as an integer.  */
2570       my_get_expression (&exp, &p, GE_NO_PREFIX);
2571       if (exp.X_op != O_constant)
2572         {
2573           as_bad (_("expression must be constant"));
2574           return FALSE;
2575         }
2576       basereg = &mybasereg;
2577       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2578                                                   : exp.X_add_number;
2579       basereg->neon = 0;
2580     }
2581
2582   if (basereg->neon)
2583     typeinfo = *basereg->neon;
2584
2585   if (parse_neon_type (&ntype, &p) == SUCCESS)
2586     {
2587       /* We got a type.  */
2588       if (typeinfo.defined & NTA_HASTYPE)
2589         {
2590           as_bad (_("can't redefine the type of a register alias"));
2591           return FALSE;
2592         }
2593
2594       typeinfo.defined |= NTA_HASTYPE;
2595       if (ntype.elems != 1)
2596         {
2597           as_bad (_("you must specify a single type only"));
2598           return FALSE;
2599         }
2600       typeinfo.eltype = ntype.el[0];
2601     }
2602
2603   if (skip_past_char (&p, '[') == SUCCESS)
2604     {
2605       expressionS exp;
2606       /* We got a scalar index.  */
2607
2608       if (typeinfo.defined & NTA_HASINDEX)
2609         {
2610           as_bad (_("can't redefine the index of a scalar alias"));
2611           return FALSE;
2612         }
2613
2614       my_get_expression (&exp, &p, GE_NO_PREFIX);
2615
2616       if (exp.X_op != O_constant)
2617         {
2618           as_bad (_("scalar index must be constant"));
2619           return FALSE;
2620         }
2621
2622       typeinfo.defined |= NTA_HASINDEX;
2623       typeinfo.index = exp.X_add_number;
2624
2625       if (skip_past_char (&p, ']') == FAIL)
2626         {
2627           as_bad (_("expecting ]"));
2628           return FALSE;
2629         }
2630     }
2631
2632   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2633      the desired alias name, and p points to its end.  If not, then
2634      the desired alias name is in the global original_case_string.  */
2635 #ifdef TC_CASE_SENSITIVE
2636   namelen = nameend - newname;
2637 #else
2638   newname = original_case_string;
2639   namelen = strlen (newname);
2640 #endif
2641
2642   namebuf = xmemdup0 (newname, namelen);
2643
2644   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2645                          typeinfo.defined != 0 ? &typeinfo : NULL);
2646
2647   /* Insert name in all uppercase.  */
2648   for (p = namebuf; *p; p++)
2649     *p = TOUPPER (*p);
2650
2651   if (strncmp (namebuf, newname, namelen))
2652     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2653                            typeinfo.defined != 0 ? &typeinfo : NULL);
2654
2655   /* Insert name in all lowercase.  */
2656   for (p = namebuf; *p; p++)
2657     *p = TOLOWER (*p);
2658
2659   if (strncmp (namebuf, newname, namelen))
2660     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2661                            typeinfo.defined != 0 ? &typeinfo : NULL);
2662
2663   free (namebuf);
2664   return TRUE;
2665 }
2666
2667 /* Should never be called, as .req goes between the alias and the
2668    register name, not at the beginning of the line.  */
2669
2670 static void
2671 s_req (int a ATTRIBUTE_UNUSED)
2672 {
2673   as_bad (_("invalid syntax for .req directive"));
2674 }
2675
2676 static void
2677 s_dn (int a ATTRIBUTE_UNUSED)
2678 {
2679   as_bad (_("invalid syntax for .dn directive"));
2680 }
2681
2682 static void
2683 s_qn (int a ATTRIBUTE_UNUSED)
2684 {
2685   as_bad (_("invalid syntax for .qn directive"));
2686 }
2687
2688 /* The .unreq directive deletes an alias which was previously defined
2689    by .req.  For example:
2690
2691        my_alias .req r11
2692        .unreq my_alias    */
2693
2694 static void
2695 s_unreq (int a ATTRIBUTE_UNUSED)
2696 {
2697   char * name;
2698   char saved_char;
2699
2700   name = input_line_pointer;
2701
2702   while (*input_line_pointer != 0
2703          && *input_line_pointer != ' '
2704          && *input_line_pointer != '\n')
2705     ++input_line_pointer;
2706
2707   saved_char = *input_line_pointer;
2708   *input_line_pointer = 0;
2709
2710   if (!*name)
2711     as_bad (_("invalid syntax for .unreq directive"));
2712   else
2713     {
2714       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2715                                                               name);
2716
2717       if (!reg)
2718         as_bad (_("unknown register alias '%s'"), name);
2719       else if (reg->builtin)
2720         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2721                  name);
2722       else
2723         {
2724           char * p;
2725           char * nbuf;
2726
2727           hash_delete (arm_reg_hsh, name, FALSE);
2728           free ((char *) reg->name);
2729           if (reg->neon)
2730             free (reg->neon);
2731           free (reg);
2732
2733           /* Also locate the all upper case and all lower case versions.
2734              Do not complain if we cannot find one or the other as it
2735              was probably deleted above.  */
2736
2737           nbuf = strdup (name);
2738           for (p = nbuf; *p; p++)
2739             *p = TOUPPER (*p);
2740           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2741           if (reg)
2742             {
2743               hash_delete (arm_reg_hsh, nbuf, FALSE);
2744               free ((char *) reg->name);
2745               if (reg->neon)
2746                 free (reg->neon);
2747               free (reg);
2748             }
2749
2750           for (p = nbuf; *p; p++)
2751             *p = TOLOWER (*p);
2752           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2753           if (reg)
2754             {
2755               hash_delete (arm_reg_hsh, nbuf, FALSE);
2756               free ((char *) reg->name);
2757               if (reg->neon)
2758                 free (reg->neon);
2759               free (reg);
2760             }
2761
2762           free (nbuf);
2763         }
2764     }
2765
2766   *input_line_pointer = saved_char;
2767   demand_empty_rest_of_line ();
2768 }
2769
2770 /* Directives: Instruction set selection.  */
2771
2772 #ifdef OBJ_ELF
2773 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2774    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2775    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2776    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2777
2778 /* Create a new mapping symbol for the transition to STATE.  */
2779
2780 static void
2781 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2782 {
2783   symbolS * symbolP;
2784   const char * symname;
2785   int type;
2786
2787   switch (state)
2788     {
2789     case MAP_DATA:
2790       symname = "$d";
2791       type = BSF_NO_FLAGS;
2792       break;
2793     case MAP_ARM:
2794       symname = "$a";
2795       type = BSF_NO_FLAGS;
2796       break;
2797     case MAP_THUMB:
2798       symname = "$t";
2799       type = BSF_NO_FLAGS;
2800       break;
2801     default:
2802       abort ();
2803     }
2804
2805   symbolP = symbol_new (symname, now_seg, value, frag);
2806   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2807
2808   switch (state)
2809     {
2810     case MAP_ARM:
2811       THUMB_SET_FUNC (symbolP, 0);
2812       ARM_SET_THUMB (symbolP, 0);
2813       ARM_SET_INTERWORK (symbolP, support_interwork);
2814       break;
2815
2816     case MAP_THUMB:
2817       THUMB_SET_FUNC (symbolP, 1);
2818       ARM_SET_THUMB (symbolP, 1);
2819       ARM_SET_INTERWORK (symbolP, support_interwork);
2820       break;
2821
2822     case MAP_DATA:
2823     default:
2824       break;
2825     }
2826
2827   /* Save the mapping symbols for future reference.  Also check that
2828      we do not place two mapping symbols at the same offset within a
2829      frag.  We'll handle overlap between frags in
2830      check_mapping_symbols.
2831
2832      If .fill or other data filling directive generates zero sized data,
2833      the mapping symbol for the following code will have the same value
2834      as the one generated for the data filling directive.  In this case,
2835      we replace the old symbol with the new one at the same address.  */
2836   if (value == 0)
2837     {
2838       if (frag->tc_frag_data.first_map != NULL)
2839         {
2840           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2841           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2842         }
2843       frag->tc_frag_data.first_map = symbolP;
2844     }
2845   if (frag->tc_frag_data.last_map != NULL)
2846     {
2847       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2848       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2849         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2850     }
2851   frag->tc_frag_data.last_map = symbolP;
2852 }
2853
2854 /* We must sometimes convert a region marked as code to data during
2855    code alignment, if an odd number of bytes have to be padded.  The
2856    code mapping symbol is pushed to an aligned address.  */
2857
2858 static void
2859 insert_data_mapping_symbol (enum mstate state,
2860                             valueT value, fragS *frag, offsetT bytes)
2861 {
2862   /* If there was already a mapping symbol, remove it.  */
2863   if (frag->tc_frag_data.last_map != NULL
2864       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2865     {
2866       symbolS *symp = frag->tc_frag_data.last_map;
2867
2868       if (value == 0)
2869         {
2870           know (frag->tc_frag_data.first_map == symp);
2871           frag->tc_frag_data.first_map = NULL;
2872         }
2873       frag->tc_frag_data.last_map = NULL;
2874       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2875     }
2876
2877   make_mapping_symbol (MAP_DATA, value, frag);
2878   make_mapping_symbol (state, value + bytes, frag);
2879 }
2880
2881 static void mapping_state_2 (enum mstate state, int max_chars);
2882
2883 /* Set the mapping state to STATE.  Only call this when about to
2884    emit some STATE bytes to the file.  */
2885
2886 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2887 void
2888 mapping_state (enum mstate state)
2889 {
2890   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2891
2892   if (mapstate == state)
2893     /* The mapping symbol has already been emitted.
2894        There is nothing else to do.  */
2895     return;
2896
2897   if (state == MAP_ARM || state == MAP_THUMB)
2898     /*  PR gas/12931
2899         All ARM instructions require 4-byte alignment.
2900         (Almost) all Thumb instructions require 2-byte alignment.
2901
2902         When emitting instructions into any section, mark the section
2903         appropriately.
2904
2905         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2906         but themselves require 2-byte alignment; this applies to some
2907         PC- relative forms.  However, these cases will involve implicit
2908         literal pool generation or an explicit .align >=2, both of
2909         which will cause the section to me marked with sufficient
2910         alignment.  Thus, we don't handle those cases here.  */
2911     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2912
2913   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2914     /* This case will be evaluated later.  */
2915     return;
2916
2917   mapping_state_2 (state, 0);
2918 }
2919
2920 /* Same as mapping_state, but MAX_CHARS bytes have already been
2921    allocated.  Put the mapping symbol that far back.  */
2922
2923 static void
2924 mapping_state_2 (enum mstate state, int max_chars)
2925 {
2926   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2927
2928   if (!SEG_NORMAL (now_seg))
2929     return;
2930
2931   if (mapstate == state)
2932     /* The mapping symbol has already been emitted.
2933        There is nothing else to do.  */
2934     return;
2935
2936   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2937           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2938     {
2939       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2940       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2941
2942       if (add_symbol)
2943         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2944     }
2945
2946   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2947   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2948 }
2949 #undef TRANSITION
2950 #else
2951 #define mapping_state(x) ((void)0)
2952 #define mapping_state_2(x, y) ((void)0)
2953 #endif
2954
2955 /* Find the real, Thumb encoded start of a Thumb function.  */
2956
2957 #ifdef OBJ_COFF
2958 static symbolS *
2959 find_real_start (symbolS * symbolP)
2960 {
2961   char *       real_start;
2962   const char * name = S_GET_NAME (symbolP);
2963   symbolS *    new_target;
2964
2965   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2966 #define STUB_NAME ".real_start_of"
2967
2968   if (name == NULL)
2969     abort ();
2970
2971   /* The compiler may generate BL instructions to local labels because
2972      it needs to perform a branch to a far away location. These labels
2973      do not have a corresponding ".real_start_of" label.  We check
2974      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2975      the ".real_start_of" convention for nonlocal branches.  */
2976   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2977     return symbolP;
2978
2979   real_start = concat (STUB_NAME, name, NULL);
2980   new_target = symbol_find (real_start);
2981   free (real_start);
2982
2983   if (new_target == NULL)
2984     {
2985       as_warn (_("Failed to find real start of function: %s\n"), name);
2986       new_target = symbolP;
2987     }
2988
2989   return new_target;
2990 }
2991 #endif
2992
2993 static void
2994 opcode_select (int width)
2995 {
2996   switch (width)
2997     {
2998     case 16:
2999       if (! thumb_mode)
3000         {
3001           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
3002             as_bad (_("selected processor does not support THUMB opcodes"));
3003
3004           thumb_mode = 1;
3005           /* No need to force the alignment, since we will have been
3006              coming from ARM mode, which is word-aligned.  */
3007           record_alignment (now_seg, 1);
3008         }
3009       break;
3010
3011     case 32:
3012       if (thumb_mode)
3013         {
3014           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
3015             as_bad (_("selected processor does not support ARM opcodes"));
3016
3017           thumb_mode = 0;
3018
3019           if (!need_pass_2)
3020             frag_align (2, 0, 0);
3021
3022           record_alignment (now_seg, 1);
3023         }
3024       break;
3025
3026     default:
3027       as_bad (_("invalid instruction size selected (%d)"), width);
3028     }
3029 }
3030
3031 static void
3032 s_arm (int ignore ATTRIBUTE_UNUSED)
3033 {
3034   opcode_select (32);
3035   demand_empty_rest_of_line ();
3036 }
3037
3038 static void
3039 s_thumb (int ignore ATTRIBUTE_UNUSED)
3040 {
3041   opcode_select (16);
3042   demand_empty_rest_of_line ();
3043 }
3044
3045 static void
3046 s_code (int unused ATTRIBUTE_UNUSED)
3047 {
3048   int temp;
3049
3050   temp = get_absolute_expression ();
3051   switch (temp)
3052     {
3053     case 16:
3054     case 32:
3055       opcode_select (temp);
3056       break;
3057
3058     default:
3059       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
3060     }
3061 }
3062
3063 static void
3064 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
3065 {
3066   /* If we are not already in thumb mode go into it, EVEN if
3067      the target processor does not support thumb instructions.
3068      This is used by gcc/config/arm/lib1funcs.asm for example
3069      to compile interworking support functions even if the
3070      target processor should not support interworking.  */
3071   if (! thumb_mode)
3072     {
3073       thumb_mode = 2;
3074       record_alignment (now_seg, 1);
3075     }
3076
3077   demand_empty_rest_of_line ();
3078 }
3079
3080 static void
3081 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
3082 {
3083   s_thumb (0);
3084
3085   /* The following label is the name/address of the start of a Thumb function.
3086      We need to know this for the interworking support.  */
3087   label_is_thumb_function_name = TRUE;
3088 }
3089
3090 /* Perform a .set directive, but also mark the alias as
3091    being a thumb function.  */
3092
3093 static void
3094 s_thumb_set (int equiv)
3095 {
3096   /* XXX the following is a duplicate of the code for s_set() in read.c
3097      We cannot just call that code as we need to get at the symbol that
3098      is created.  */
3099   char *    name;
3100   char      delim;
3101   char *    end_name;
3102   symbolS * symbolP;
3103
3104   /* Especial apologies for the random logic:
3105      This just grew, and could be parsed much more simply!
3106      Dean - in haste.  */
3107   delim     = get_symbol_name (& name);
3108   end_name  = input_line_pointer;
3109   (void) restore_line_pointer (delim);
3110
3111   if (*input_line_pointer != ',')
3112     {
3113       *end_name = 0;
3114       as_bad (_("expected comma after name \"%s\""), name);
3115       *end_name = delim;
3116       ignore_rest_of_line ();
3117       return;
3118     }
3119
3120   input_line_pointer++;
3121   *end_name = 0;
3122
3123   if (name[0] == '.' && name[1] == '\0')
3124     {
3125       /* XXX - this should not happen to .thumb_set.  */
3126       abort ();
3127     }
3128
3129   if ((symbolP = symbol_find (name)) == NULL
3130       && (symbolP = md_undefined_symbol (name)) == NULL)
3131     {
3132 #ifndef NO_LISTING
3133       /* When doing symbol listings, play games with dummy fragments living
3134          outside the normal fragment chain to record the file and line info
3135          for this symbol.  */
3136       if (listing & LISTING_SYMBOLS)
3137         {
3138           extern struct list_info_struct * listing_tail;
3139           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
3140
3141           memset (dummy_frag, 0, sizeof (fragS));
3142           dummy_frag->fr_type = rs_fill;
3143           dummy_frag->line = listing_tail;
3144           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
3145           dummy_frag->fr_symbol = symbolP;
3146         }
3147       else
3148 #endif
3149         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
3150
3151 #ifdef OBJ_COFF
3152       /* "set" symbols are local unless otherwise specified.  */
3153       SF_SET_LOCAL (symbolP);
3154 #endif /* OBJ_COFF  */
3155     }                           /* Make a new symbol.  */
3156
3157   symbol_table_insert (symbolP);
3158
3159   * end_name = delim;
3160
3161   if (equiv
3162       && S_IS_DEFINED (symbolP)
3163       && S_GET_SEGMENT (symbolP) != reg_section)
3164     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
3165
3166   pseudo_set (symbolP);
3167
3168   demand_empty_rest_of_line ();
3169
3170   /* XXX Now we come to the Thumb specific bit of code.  */
3171
3172   THUMB_SET_FUNC (symbolP, 1);
3173   ARM_SET_THUMB (symbolP, 1);
3174 #if defined OBJ_ELF || defined OBJ_COFF
3175   ARM_SET_INTERWORK (symbolP, support_interwork);
3176 #endif
3177 }
3178
3179 /* Directives: Mode selection.  */
3180
3181 /* .syntax [unified|divided] - choose the new unified syntax
3182    (same for Arm and Thumb encoding, modulo slight differences in what
3183    can be represented) or the old divergent syntax for each mode.  */
3184 static void
3185 s_syntax (int unused ATTRIBUTE_UNUSED)
3186 {
3187   char *name, delim;
3188
3189   delim = get_symbol_name (& name);
3190
3191   if (!strcasecmp (name, "unified"))
3192     unified_syntax = TRUE;
3193   else if (!strcasecmp (name, "divided"))
3194     unified_syntax = FALSE;
3195   else
3196     {
3197       as_bad (_("unrecognized syntax mode \"%s\""), name);
3198       return;
3199     }
3200   (void) restore_line_pointer (delim);
3201   demand_empty_rest_of_line ();
3202 }
3203
3204 /* Directives: sectioning and alignment.  */
3205
3206 static void
3207 s_bss (int ignore ATTRIBUTE_UNUSED)
3208 {
3209   /* We don't support putting frags in the BSS segment, we fake it by
3210      marking in_bss, then looking at s_skip for clues.  */
3211   subseg_set (bss_section, 0);
3212   demand_empty_rest_of_line ();
3213
3214 #ifdef md_elf_section_change_hook
3215   md_elf_section_change_hook ();
3216 #endif
3217 }
3218
3219 static void
3220 s_even (int ignore ATTRIBUTE_UNUSED)
3221 {
3222   /* Never make frag if expect extra pass.  */
3223   if (!need_pass_2)
3224     frag_align (1, 0, 0);
3225
3226   record_alignment (now_seg, 1);
3227
3228   demand_empty_rest_of_line ();
3229 }
3230
3231 /* Directives: CodeComposer Studio.  */
3232
3233 /*  .ref  (for CodeComposer Studio syntax only).  */
3234 static void
3235 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3236 {
3237   if (codecomposer_syntax)
3238     ignore_rest_of_line ();
3239   else
3240     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3241 }
3242
3243 /*  If name is not NULL, then it is used for marking the beginning of a
3244     function, whereas if it is NULL then it means the function end.  */
3245 static void
3246 asmfunc_debug (const char * name)
3247 {
3248   static const char * last_name = NULL;
3249
3250   if (name != NULL)
3251     {
3252       gas_assert (last_name == NULL);
3253       last_name = name;
3254
3255       if (debug_type == DEBUG_STABS)
3256          stabs_generate_asm_func (name, name);
3257     }
3258   else
3259     {
3260       gas_assert (last_name != NULL);
3261
3262       if (debug_type == DEBUG_STABS)
3263         stabs_generate_asm_endfunc (last_name, last_name);
3264
3265       last_name = NULL;
3266     }
3267 }
3268
3269 static void
3270 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3271 {
3272   if (codecomposer_syntax)
3273     {
3274       switch (asmfunc_state)
3275         {
3276         case OUTSIDE_ASMFUNC:
3277           asmfunc_state = WAITING_ASMFUNC_NAME;
3278           break;
3279
3280         case WAITING_ASMFUNC_NAME:
3281           as_bad (_(".asmfunc repeated."));
3282           break;
3283
3284         case WAITING_ENDASMFUNC:
3285           as_bad (_(".asmfunc without function."));
3286           break;
3287         }
3288       demand_empty_rest_of_line ();
3289     }
3290   else
3291     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3292 }
3293
3294 static void
3295 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3296 {
3297   if (codecomposer_syntax)
3298     {
3299       switch (asmfunc_state)
3300         {
3301         case OUTSIDE_ASMFUNC:
3302           as_bad (_(".endasmfunc without a .asmfunc."));
3303           break;
3304
3305         case WAITING_ASMFUNC_NAME:
3306           as_bad (_(".endasmfunc without function."));
3307           break;
3308
3309         case WAITING_ENDASMFUNC:
3310           asmfunc_state = OUTSIDE_ASMFUNC;
3311           asmfunc_debug (NULL);
3312           break;
3313         }
3314       demand_empty_rest_of_line ();
3315     }
3316   else
3317     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3318 }
3319
3320 static void
3321 s_ccs_def (int name)
3322 {
3323   if (codecomposer_syntax)
3324     s_globl (name);
3325   else
3326     as_bad (_(".def pseudo-op only available with -mccs flag."));
3327 }
3328
3329 /* Directives: Literal pools.  */
3330
3331 static literal_pool *
3332 find_literal_pool (void)
3333 {
3334   literal_pool * pool;
3335
3336   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3337     {
3338       if (pool->section == now_seg
3339           && pool->sub_section == now_subseg)
3340         break;
3341     }
3342
3343   return pool;
3344 }
3345
3346 static literal_pool *
3347 find_or_make_literal_pool (void)
3348 {
3349   /* Next literal pool ID number.  */
3350   static unsigned int latest_pool_num = 1;
3351   literal_pool *      pool;
3352
3353   pool = find_literal_pool ();
3354
3355   if (pool == NULL)
3356     {
3357       /* Create a new pool.  */
3358       pool = XNEW (literal_pool);
3359       if (! pool)
3360         return NULL;
3361
3362       pool->next_free_entry = 0;
3363       pool->section         = now_seg;
3364       pool->sub_section     = now_subseg;
3365       pool->next            = list_of_pools;
3366       pool->symbol          = NULL;
3367       pool->alignment       = 2;
3368
3369       /* Add it to the list.  */
3370       list_of_pools = pool;
3371     }
3372
3373   /* New pools, and emptied pools, will have a NULL symbol.  */
3374   if (pool->symbol == NULL)
3375     {
3376       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3377                                     (valueT) 0, &zero_address_frag);
3378       pool->id = latest_pool_num ++;
3379     }
3380
3381   /* Done.  */
3382   return pool;
3383 }
3384
3385 /* Add the literal in the global 'inst'
3386    structure to the relevant literal pool.  */
3387
3388 static int
3389 add_to_lit_pool (unsigned int nbytes)
3390 {
3391 #define PADDING_SLOT 0x1
3392 #define LIT_ENTRY_SIZE_MASK 0xFF
3393   literal_pool * pool;
3394   unsigned int entry, pool_size = 0;
3395   bfd_boolean padding_slot_p = FALSE;
3396   unsigned imm1 = 0;
3397   unsigned imm2 = 0;
3398
3399   if (nbytes == 8)
3400     {
3401       imm1 = inst.operands[1].imm;
3402       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3403                : inst.relocs[0].exp.X_unsigned ? 0
3404                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3405       if (target_big_endian)
3406         {
3407           imm1 = imm2;
3408           imm2 = inst.operands[1].imm;
3409         }
3410     }
3411
3412   pool = find_or_make_literal_pool ();
3413
3414   /* Check if this literal value is already in the pool.  */
3415   for (entry = 0; entry < pool->next_free_entry; entry ++)
3416     {
3417       if (nbytes == 4)
3418         {
3419           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3420               && (inst.relocs[0].exp.X_op == O_constant)
3421               && (pool->literals[entry].X_add_number
3422                   == inst.relocs[0].exp.X_add_number)
3423               && (pool->literals[entry].X_md == nbytes)
3424               && (pool->literals[entry].X_unsigned
3425                   == inst.relocs[0].exp.X_unsigned))
3426             break;
3427
3428           if ((pool->literals[entry].X_op == inst.relocs[0].exp.X_op)
3429               && (inst.relocs[0].exp.X_op == O_symbol)
3430               && (pool->literals[entry].X_add_number
3431                   == inst.relocs[0].exp.X_add_number)
3432               && (pool->literals[entry].X_add_symbol
3433                   == inst.relocs[0].exp.X_add_symbol)
3434               && (pool->literals[entry].X_op_symbol
3435                   == inst.relocs[0].exp.X_op_symbol)
3436               && (pool->literals[entry].X_md == nbytes))
3437             break;
3438         }
3439       else if ((nbytes == 8)
3440                && !(pool_size & 0x7)
3441                && ((entry + 1) != pool->next_free_entry)
3442                && (pool->literals[entry].X_op == O_constant)
3443                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3444                && (pool->literals[entry].X_unsigned
3445                    == inst.relocs[0].exp.X_unsigned)
3446                && (pool->literals[entry + 1].X_op == O_constant)
3447                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3448                && (pool->literals[entry + 1].X_unsigned
3449                    == inst.relocs[0].exp.X_unsigned))
3450         break;
3451
3452       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3453       if (padding_slot_p && (nbytes == 4))
3454         break;
3455
3456       pool_size += 4;
3457     }
3458
3459   /* Do we need to create a new entry?  */
3460   if (entry == pool->next_free_entry)
3461     {
3462       if (entry >= MAX_LITERAL_POOL_SIZE)
3463         {
3464           inst.error = _("literal pool overflow");
3465           return FAIL;
3466         }
3467
3468       if (nbytes == 8)
3469         {
3470           /* For 8-byte entries, we align to an 8-byte boundary,
3471              and split it into two 4-byte entries, because on 32-bit
3472              host, 8-byte constants are treated as big num, thus
3473              saved in "generic_bignum" which will be overwritten
3474              by later assignments.
3475
3476              We also need to make sure there is enough space for
3477              the split.
3478
3479              We also check to make sure the literal operand is a
3480              constant number.  */
3481           if (!(inst.relocs[0].exp.X_op == O_constant
3482                 || inst.relocs[0].exp.X_op == O_big))
3483             {
3484               inst.error = _("invalid type for literal pool");
3485               return FAIL;
3486             }
3487           else if (pool_size & 0x7)
3488             {
3489               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3490                 {
3491                   inst.error = _("literal pool overflow");
3492                   return FAIL;
3493                 }
3494
3495               pool->literals[entry] = inst.relocs[0].exp;
3496               pool->literals[entry].X_op = O_constant;
3497               pool->literals[entry].X_add_number = 0;
3498               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3499               pool->next_free_entry += 1;
3500               pool_size += 4;
3501             }
3502           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3503             {
3504               inst.error = _("literal pool overflow");
3505               return FAIL;
3506             }
3507
3508           pool->literals[entry] = inst.relocs[0].exp;
3509           pool->literals[entry].X_op = O_constant;
3510           pool->literals[entry].X_add_number = imm1;
3511           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3512           pool->literals[entry++].X_md = 4;
3513           pool->literals[entry] = inst.relocs[0].exp;
3514           pool->literals[entry].X_op = O_constant;
3515           pool->literals[entry].X_add_number = imm2;
3516           pool->literals[entry].X_unsigned = inst.relocs[0].exp.X_unsigned;
3517           pool->literals[entry].X_md = 4;
3518           pool->alignment = 3;
3519           pool->next_free_entry += 1;
3520         }
3521       else
3522         {
3523           pool->literals[entry] = inst.relocs[0].exp;
3524           pool->literals[entry].X_md = 4;
3525         }
3526
3527 #ifdef OBJ_ELF
3528       /* PR ld/12974: Record the location of the first source line to reference
3529          this entry in the literal pool.  If it turns out during linking that the
3530          symbol does not exist we will be able to give an accurate line number for
3531          the (first use of the) missing reference.  */
3532       if (debug_type == DEBUG_DWARF2)
3533         dwarf2_where (pool->locs + entry);
3534 #endif
3535       pool->next_free_entry += 1;
3536     }
3537   else if (padding_slot_p)
3538     {
3539       pool->literals[entry] = inst.relocs[0].exp;
3540       pool->literals[entry].X_md = nbytes;
3541     }
3542
3543   inst.relocs[0].exp.X_op             = O_symbol;
3544   inst.relocs[0].exp.X_add_number = pool_size;
3545   inst.relocs[0].exp.X_add_symbol = pool->symbol;
3546
3547   return SUCCESS;
3548 }
3549
3550 bfd_boolean
3551 tc_start_label_without_colon (void)
3552 {
3553   bfd_boolean ret = TRUE;
3554
3555   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3556     {
3557       const char *label = input_line_pointer;
3558
3559       while (!is_end_of_line[(int) label[-1]])
3560         --label;
3561
3562       if (*label == '.')
3563         {
3564           as_bad (_("Invalid label '%s'"), label);
3565           ret = FALSE;
3566         }
3567
3568       asmfunc_debug (label);
3569
3570       asmfunc_state = WAITING_ENDASMFUNC;
3571     }
3572
3573   return ret;
3574 }
3575
3576 /* Can't use symbol_new here, so have to create a symbol and then at
3577    a later date assign it a value. That's what these functions do.  */
3578
3579 static void
3580 symbol_locate (symbolS *    symbolP,
3581                const char * name,       /* It is copied, the caller can modify.  */
3582                segT         segment,    /* Segment identifier (SEG_<something>).  */
3583                valueT       valu,       /* Symbol value.  */
3584                fragS *      frag)       /* Associated fragment.  */
3585 {
3586   size_t name_length;
3587   char * preserved_copy_of_name;
3588
3589   name_length = strlen (name) + 1;   /* +1 for \0.  */
3590   obstack_grow (&notes, name, name_length);
3591   preserved_copy_of_name = (char *) obstack_finish (&notes);
3592
3593 #ifdef tc_canonicalize_symbol_name
3594   preserved_copy_of_name =
3595     tc_canonicalize_symbol_name (preserved_copy_of_name);
3596 #endif
3597
3598   S_SET_NAME (symbolP, preserved_copy_of_name);
3599
3600   S_SET_SEGMENT (symbolP, segment);
3601   S_SET_VALUE (symbolP, valu);
3602   symbol_clear_list_pointers (symbolP);
3603
3604   symbol_set_frag (symbolP, frag);
3605
3606   /* Link to end of symbol chain.  */
3607   {
3608     extern int symbol_table_frozen;
3609
3610     if (symbol_table_frozen)
3611       abort ();
3612   }
3613
3614   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3615
3616   obj_symbol_new_hook (symbolP);
3617
3618 #ifdef tc_symbol_new_hook
3619   tc_symbol_new_hook (symbolP);
3620 #endif
3621
3622 #ifdef DEBUG_SYMS
3623   verify_symbol_chain (symbol_rootP, symbol_lastP);
3624 #endif /* DEBUG_SYMS  */
3625 }
3626
3627 static void
3628 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3629 {
3630   unsigned int entry;
3631   literal_pool * pool;
3632   char sym_name[20];
3633
3634   pool = find_literal_pool ();
3635   if (pool == NULL
3636       || pool->symbol == NULL
3637       || pool->next_free_entry == 0)
3638     return;
3639
3640   /* Align pool as you have word accesses.
3641      Only make a frag if we have to.  */
3642   if (!need_pass_2)
3643     frag_align (pool->alignment, 0, 0);
3644
3645   record_alignment (now_seg, 2);
3646
3647 #ifdef OBJ_ELF
3648   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3649   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3650 #endif
3651   sprintf (sym_name, "$$lit_\002%x", pool->id);
3652
3653   symbol_locate (pool->symbol, sym_name, now_seg,
3654                  (valueT) frag_now_fix (), frag_now);
3655   symbol_table_insert (pool->symbol);
3656
3657   ARM_SET_THUMB (pool->symbol, thumb_mode);
3658
3659 #if defined OBJ_COFF || defined OBJ_ELF
3660   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3661 #endif
3662
3663   for (entry = 0; entry < pool->next_free_entry; entry ++)
3664     {
3665 #ifdef OBJ_ELF
3666       if (debug_type == DEBUG_DWARF2)
3667         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3668 #endif
3669       /* First output the expression in the instruction to the pool.  */
3670       emit_expr (&(pool->literals[entry]),
3671                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3672     }
3673
3674   /* Mark the pool as empty.  */
3675   pool->next_free_entry = 0;
3676   pool->symbol = NULL;
3677 }
3678
3679 #ifdef OBJ_ELF
3680 /* Forward declarations for functions below, in the MD interface
3681    section.  */
3682 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3683 static valueT create_unwind_entry (int);
3684 static void start_unwind_section (const segT, int);
3685 static void add_unwind_opcode (valueT, int);
3686 static void flush_pending_unwind (void);
3687
3688 /* Directives: Data.  */
3689
3690 static void
3691 s_arm_elf_cons (int nbytes)
3692 {
3693   expressionS exp;
3694
3695 #ifdef md_flush_pending_output
3696   md_flush_pending_output ();
3697 #endif
3698
3699   if (is_it_end_of_statement ())
3700     {
3701       demand_empty_rest_of_line ();
3702       return;
3703     }
3704
3705 #ifdef md_cons_align
3706   md_cons_align (nbytes);
3707 #endif
3708
3709   mapping_state (MAP_DATA);
3710   do
3711     {
3712       int reloc;
3713       char *base = input_line_pointer;
3714
3715       expression (& exp);
3716
3717       if (exp.X_op != O_symbol)
3718         emit_expr (&exp, (unsigned int) nbytes);
3719       else
3720         {
3721           char *before_reloc = input_line_pointer;
3722           reloc = parse_reloc (&input_line_pointer);
3723           if (reloc == -1)
3724             {
3725               as_bad (_("unrecognized relocation suffix"));
3726               ignore_rest_of_line ();
3727               return;
3728             }
3729           else if (reloc == BFD_RELOC_UNUSED)
3730             emit_expr (&exp, (unsigned int) nbytes);
3731           else
3732             {
3733               reloc_howto_type *howto = (reloc_howto_type *)
3734                   bfd_reloc_type_lookup (stdoutput,
3735                                          (bfd_reloc_code_real_type) reloc);
3736               int size = bfd_get_reloc_size (howto);
3737
3738               if (reloc == BFD_RELOC_ARM_PLT32)
3739                 {
3740                   as_bad (_("(plt) is only valid on branch targets"));
3741                   reloc = BFD_RELOC_UNUSED;
3742                   size = 0;
3743                 }
3744
3745               if (size > nbytes)
3746                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3747                                   "%s relocations do not fit in %d bytes",
3748                                   nbytes),
3749                         howto->name, nbytes);
3750               else
3751                 {
3752                   /* We've parsed an expression stopping at O_symbol.
3753                      But there may be more expression left now that we
3754                      have parsed the relocation marker.  Parse it again.
3755                      XXX Surely there is a cleaner way to do this.  */
3756                   char *p = input_line_pointer;
3757                   int offset;
3758                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3759
3760                   memcpy (save_buf, base, input_line_pointer - base);
3761                   memmove (base + (input_line_pointer - before_reloc),
3762                            base, before_reloc - base);
3763
3764                   input_line_pointer = base + (input_line_pointer-before_reloc);
3765                   expression (&exp);
3766                   memcpy (base, save_buf, p - base);
3767
3768                   offset = nbytes - size;
3769                   p = frag_more (nbytes);
3770                   memset (p, 0, nbytes);
3771                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3772                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3773                   free (save_buf);
3774                 }
3775             }
3776         }
3777     }
3778   while (*input_line_pointer++ == ',');
3779
3780   /* Put terminator back into stream.  */
3781   input_line_pointer --;
3782   demand_empty_rest_of_line ();
3783 }
3784
3785 /* Emit an expression containing a 32-bit thumb instruction.
3786    Implementation based on put_thumb32_insn.  */
3787
3788 static void
3789 emit_thumb32_expr (expressionS * exp)
3790 {
3791   expressionS exp_high = *exp;
3792
3793   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3794   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3795   exp->X_add_number &= 0xffff;
3796   emit_expr (exp, (unsigned int) THUMB_SIZE);
3797 }
3798
3799 /*  Guess the instruction size based on the opcode.  */
3800
3801 static int
3802 thumb_insn_size (int opcode)
3803 {
3804   if ((unsigned int) opcode < 0xe800u)
3805     return 2;
3806   else if ((unsigned int) opcode >= 0xe8000000u)
3807     return 4;
3808   else
3809     return 0;
3810 }
3811
3812 static bfd_boolean
3813 emit_insn (expressionS *exp, int nbytes)
3814 {
3815   int size = 0;
3816
3817   if (exp->X_op == O_constant)
3818     {
3819       size = nbytes;
3820
3821       if (size == 0)
3822         size = thumb_insn_size (exp->X_add_number);
3823
3824       if (size != 0)
3825         {
3826           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3827             {
3828               as_bad (_(".inst.n operand too big. "\
3829                         "Use .inst.w instead"));
3830               size = 0;
3831             }
3832           else
3833             {
3834               if (now_pred.state == AUTOMATIC_PRED_BLOCK)
3835                 set_pred_insn_type_nonvoid (OUTSIDE_PRED_INSN, 0);
3836               else
3837                 set_pred_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3838
3839               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3840                 emit_thumb32_expr (exp);
3841               else
3842                 emit_expr (exp, (unsigned int) size);
3843
3844               it_fsm_post_encode ();
3845             }
3846         }
3847       else
3848         as_bad (_("cannot determine Thumb instruction size. "   \
3849                   "Use .inst.n/.inst.w instead"));
3850     }
3851   else
3852     as_bad (_("constant expression required"));
3853
3854   return (size != 0);
3855 }
3856
3857 /* Like s_arm_elf_cons but do not use md_cons_align and
3858    set the mapping state to MAP_ARM/MAP_THUMB.  */
3859
3860 static void
3861 s_arm_elf_inst (int nbytes)
3862 {
3863   if (is_it_end_of_statement ())
3864     {
3865       demand_empty_rest_of_line ();
3866       return;
3867     }
3868
3869   /* Calling mapping_state () here will not change ARM/THUMB,
3870      but will ensure not to be in DATA state.  */
3871
3872   if (thumb_mode)
3873     mapping_state (MAP_THUMB);
3874   else
3875     {
3876       if (nbytes != 0)
3877         {
3878           as_bad (_("width suffixes are invalid in ARM mode"));
3879           ignore_rest_of_line ();
3880           return;
3881         }
3882
3883       nbytes = 4;
3884
3885       mapping_state (MAP_ARM);
3886     }
3887
3888   do
3889     {
3890       expressionS exp;
3891
3892       expression (& exp);
3893
3894       if (! emit_insn (& exp, nbytes))
3895         {
3896           ignore_rest_of_line ();
3897           return;
3898         }
3899     }
3900   while (*input_line_pointer++ == ',');
3901
3902   /* Put terminator back into stream.  */
3903   input_line_pointer --;
3904   demand_empty_rest_of_line ();
3905 }
3906
3907 /* Parse a .rel31 directive.  */
3908
3909 static void
3910 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3911 {
3912   expressionS exp;
3913   char *p;
3914   valueT highbit;
3915
3916   highbit = 0;
3917   if (*input_line_pointer == '1')
3918     highbit = 0x80000000;
3919   else if (*input_line_pointer != '0')
3920     as_bad (_("expected 0 or 1"));
3921
3922   input_line_pointer++;
3923   if (*input_line_pointer != ',')
3924     as_bad (_("missing comma"));
3925   input_line_pointer++;
3926
3927 #ifdef md_flush_pending_output
3928   md_flush_pending_output ();
3929 #endif
3930
3931 #ifdef md_cons_align
3932   md_cons_align (4);
3933 #endif
3934
3935   mapping_state (MAP_DATA);
3936
3937   expression (&exp);
3938
3939   p = frag_more (4);
3940   md_number_to_chars (p, highbit, 4);
3941   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3942                BFD_RELOC_ARM_PREL31);
3943
3944   demand_empty_rest_of_line ();
3945 }
3946
3947 /* Directives: AEABI stack-unwind tables.  */
3948
3949 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3950
3951 static void
3952 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3953 {
3954   demand_empty_rest_of_line ();
3955   if (unwind.proc_start)
3956     {
3957       as_bad (_("duplicate .fnstart directive"));
3958       return;
3959     }
3960
3961   /* Mark the start of the function.  */
3962   unwind.proc_start = expr_build_dot ();
3963
3964   /* Reset the rest of the unwind info.  */
3965   unwind.opcode_count = 0;
3966   unwind.table_entry = NULL;
3967   unwind.personality_routine = NULL;
3968   unwind.personality_index = -1;
3969   unwind.frame_size = 0;
3970   unwind.fp_offset = 0;
3971   unwind.fp_reg = REG_SP;
3972   unwind.fp_used = 0;
3973   unwind.sp_restored = 0;
3974 }
3975
3976
3977 /* Parse a handlerdata directive.  Creates the exception handling table entry
3978    for the function.  */
3979
3980 static void
3981 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3982 {
3983   demand_empty_rest_of_line ();
3984   if (!unwind.proc_start)
3985     as_bad (MISSING_FNSTART);
3986
3987   if (unwind.table_entry)
3988     as_bad (_("duplicate .handlerdata directive"));
3989
3990   create_unwind_entry (1);
3991 }
3992
3993 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3994
3995 static void
3996 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3997 {
3998   long where;
3999   char *ptr;
4000   valueT val;
4001   unsigned int marked_pr_dependency;
4002
4003   demand_empty_rest_of_line ();
4004
4005   if (!unwind.proc_start)
4006     {
4007       as_bad (_(".fnend directive without .fnstart"));
4008       return;
4009     }
4010
4011   /* Add eh table entry.  */
4012   if (unwind.table_entry == NULL)
4013     val = create_unwind_entry (0);
4014   else
4015     val = 0;
4016
4017   /* Add index table entry.  This is two words.  */
4018   start_unwind_section (unwind.saved_seg, 1);
4019   frag_align (2, 0, 0);
4020   record_alignment (now_seg, 2);
4021
4022   ptr = frag_more (8);
4023   memset (ptr, 0, 8);
4024   where = frag_now_fix () - 8;
4025
4026   /* Self relative offset of the function start.  */
4027   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
4028            BFD_RELOC_ARM_PREL31);
4029
4030   /* Indicate dependency on EHABI-defined personality routines to the
4031      linker, if it hasn't been done already.  */
4032   marked_pr_dependency
4033     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
4034   if (unwind.personality_index >= 0 && unwind.personality_index < 3
4035       && !(marked_pr_dependency & (1 << unwind.personality_index)))
4036     {
4037       static const char *const name[] =
4038         {
4039           "__aeabi_unwind_cpp_pr0",
4040           "__aeabi_unwind_cpp_pr1",
4041           "__aeabi_unwind_cpp_pr2"
4042         };
4043       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
4044       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
4045       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
4046         |= 1 << unwind.personality_index;
4047     }
4048
4049   if (val)
4050     /* Inline exception table entry.  */
4051     md_number_to_chars (ptr + 4, val, 4);
4052   else
4053     /* Self relative offset of the table entry.  */
4054     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
4055              BFD_RELOC_ARM_PREL31);
4056
4057   /* Restore the original section.  */
4058   subseg_set (unwind.saved_seg, unwind.saved_subseg);
4059
4060   unwind.proc_start = NULL;
4061 }
4062
4063
4064 /* Parse an unwind_cantunwind directive.  */
4065
4066 static void
4067 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
4068 {
4069   demand_empty_rest_of_line ();
4070   if (!unwind.proc_start)
4071     as_bad (MISSING_FNSTART);
4072
4073   if (unwind.personality_routine || unwind.personality_index != -1)
4074     as_bad (_("personality routine specified for cantunwind frame"));
4075
4076   unwind.personality_index = -2;
4077 }
4078
4079
4080 /* Parse a personalityindex directive.  */
4081
4082 static void
4083 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
4084 {
4085   expressionS exp;
4086
4087   if (!unwind.proc_start)
4088     as_bad (MISSING_FNSTART);
4089
4090   if (unwind.personality_routine || unwind.personality_index != -1)
4091     as_bad (_("duplicate .personalityindex directive"));
4092
4093   expression (&exp);
4094
4095   if (exp.X_op != O_constant
4096       || exp.X_add_number < 0 || exp.X_add_number > 15)
4097     {
4098       as_bad (_("bad personality routine number"));
4099       ignore_rest_of_line ();
4100       return;
4101     }
4102
4103   unwind.personality_index = exp.X_add_number;
4104
4105   demand_empty_rest_of_line ();
4106 }
4107
4108
4109 /* Parse a personality directive.  */
4110
4111 static void
4112 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
4113 {
4114   char *name, *p, c;
4115
4116   if (!unwind.proc_start)
4117     as_bad (MISSING_FNSTART);
4118
4119   if (unwind.personality_routine || unwind.personality_index != -1)
4120     as_bad (_("duplicate .personality directive"));
4121
4122   c = get_symbol_name (& name);
4123   p = input_line_pointer;
4124   if (c == '"')
4125     ++ input_line_pointer;
4126   unwind.personality_routine = symbol_find_or_make (name);
4127   *p = c;
4128   demand_empty_rest_of_line ();
4129 }
4130
4131
4132 /* Parse a directive saving core registers.  */
4133
4134 static void
4135 s_arm_unwind_save_core (void)
4136 {
4137   valueT op;
4138   long range;
4139   int n;
4140
4141   range = parse_reg_list (&input_line_pointer, REGLIST_RN);
4142   if (range == FAIL)
4143     {
4144       as_bad (_("expected register list"));
4145       ignore_rest_of_line ();
4146       return;
4147     }
4148
4149   demand_empty_rest_of_line ();
4150
4151   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
4152      into .unwind_save {..., sp...}.  We aren't bothered about the value of
4153      ip because it is clobbered by calls.  */
4154   if (unwind.sp_restored && unwind.fp_reg == 12
4155       && (range & 0x3000) == 0x1000)
4156     {
4157       unwind.opcode_count--;
4158       unwind.sp_restored = 0;
4159       range = (range | 0x2000) & ~0x1000;
4160       unwind.pending_offset = 0;
4161     }
4162
4163   /* Pop r4-r15.  */
4164   if (range & 0xfff0)
4165     {
4166       /* See if we can use the short opcodes.  These pop a block of up to 8
4167          registers starting with r4, plus maybe r14.  */
4168       for (n = 0; n < 8; n++)
4169         {
4170           /* Break at the first non-saved register.      */
4171           if ((range & (1 << (n + 4))) == 0)
4172             break;
4173         }
4174       /* See if there are any other bits set.  */
4175       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4176         {
4177           /* Use the long form.  */
4178           op = 0x8000 | ((range >> 4) & 0xfff);
4179           add_unwind_opcode (op, 2);
4180         }
4181       else
4182         {
4183           /* Use the short form.  */
4184           if (range & 0x4000)
4185             op = 0xa8; /* Pop r14.      */
4186           else
4187             op = 0xa0; /* Do not pop r14.  */
4188           op |= (n - 1);
4189           add_unwind_opcode (op, 1);
4190         }
4191     }
4192
4193   /* Pop r0-r3.  */
4194   if (range & 0xf)
4195     {
4196       op = 0xb100 | (range & 0xf);
4197       add_unwind_opcode (op, 2);
4198     }
4199
4200   /* Record the number of bytes pushed.  */
4201   for (n = 0; n < 16; n++)
4202     {
4203       if (range & (1 << n))
4204         unwind.frame_size += 4;
4205     }
4206 }
4207
4208
4209 /* Parse a directive saving FPA registers.  */
4210
4211 static void
4212 s_arm_unwind_save_fpa (int reg)
4213 {
4214   expressionS exp;
4215   int num_regs;
4216   valueT op;
4217
4218   /* Get Number of registers to transfer.  */
4219   if (skip_past_comma (&input_line_pointer) != FAIL)
4220     expression (&exp);
4221   else
4222     exp.X_op = O_illegal;
4223
4224   if (exp.X_op != O_constant)
4225     {
4226       as_bad (_("expected , <constant>"));
4227       ignore_rest_of_line ();
4228       return;
4229     }
4230
4231   num_regs = exp.X_add_number;
4232
4233   if (num_regs < 1 || num_regs > 4)
4234     {
4235       as_bad (_("number of registers must be in the range [1:4]"));
4236       ignore_rest_of_line ();
4237       return;
4238     }
4239
4240   demand_empty_rest_of_line ();
4241
4242   if (reg == 4)
4243     {
4244       /* Short form.  */
4245       op = 0xb4 | (num_regs - 1);
4246       add_unwind_opcode (op, 1);
4247     }
4248   else
4249     {
4250       /* Long form.  */
4251       op = 0xc800 | (reg << 4) | (num_regs - 1);
4252       add_unwind_opcode (op, 2);
4253     }
4254   unwind.frame_size += num_regs * 12;
4255 }
4256
4257
4258 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4259
4260 static void
4261 s_arm_unwind_save_vfp_armv6 (void)
4262 {
4263   int count;
4264   unsigned int start;
4265   valueT op;
4266   int num_vfpv3_regs = 0;
4267   int num_regs_below_16;
4268   bfd_boolean partial_match;
4269
4270   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D,
4271                               &partial_match);
4272   if (count == FAIL)
4273     {
4274       as_bad (_("expected register list"));
4275       ignore_rest_of_line ();
4276       return;
4277     }
4278
4279   demand_empty_rest_of_line ();
4280
4281   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4282      than FSTMX/FLDMX-style ones).  */
4283
4284   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4285   if (start >= 16)
4286     num_vfpv3_regs = count;
4287   else if (start + count > 16)
4288     num_vfpv3_regs = start + count - 16;
4289
4290   if (num_vfpv3_regs > 0)
4291     {
4292       int start_offset = start > 16 ? start - 16 : 0;
4293       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4294       add_unwind_opcode (op, 2);
4295     }
4296
4297   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4298   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4299   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4300   if (num_regs_below_16 > 0)
4301     {
4302       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4303       add_unwind_opcode (op, 2);
4304     }
4305
4306   unwind.frame_size += count * 8;
4307 }
4308
4309
4310 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4311
4312 static void
4313 s_arm_unwind_save_vfp (void)
4314 {
4315   int count;
4316   unsigned int reg;
4317   valueT op;
4318   bfd_boolean partial_match;
4319
4320   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D,
4321                               &partial_match);
4322   if (count == FAIL)
4323     {
4324       as_bad (_("expected register list"));
4325       ignore_rest_of_line ();
4326       return;
4327     }
4328
4329   demand_empty_rest_of_line ();
4330
4331   if (reg == 8)
4332     {
4333       /* Short form.  */
4334       op = 0xb8 | (count - 1);
4335       add_unwind_opcode (op, 1);
4336     }
4337   else
4338     {
4339       /* Long form.  */
4340       op = 0xb300 | (reg << 4) | (count - 1);
4341       add_unwind_opcode (op, 2);
4342     }
4343   unwind.frame_size += count * 8 + 4;
4344 }
4345
4346
4347 /* Parse a directive saving iWMMXt data registers.  */
4348
4349 static void
4350 s_arm_unwind_save_mmxwr (void)
4351 {
4352   int reg;
4353   int hi_reg;
4354   int i;
4355   unsigned mask = 0;
4356   valueT op;
4357
4358   if (*input_line_pointer == '{')
4359     input_line_pointer++;
4360
4361   do
4362     {
4363       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4364
4365       if (reg == FAIL)
4366         {
4367           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4368           goto error;
4369         }
4370
4371       if (mask >> reg)
4372         as_tsktsk (_("register list not in ascending order"));
4373       mask |= 1 << reg;
4374
4375       if (*input_line_pointer == '-')
4376         {
4377           input_line_pointer++;
4378           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4379           if (hi_reg == FAIL)
4380             {
4381               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4382               goto error;
4383             }
4384           else if (reg >= hi_reg)
4385             {
4386               as_bad (_("bad register range"));
4387               goto error;
4388             }
4389           for (; reg < hi_reg; reg++)
4390             mask |= 1 << reg;
4391         }
4392     }
4393   while (skip_past_comma (&input_line_pointer) != FAIL);
4394
4395   skip_past_char (&input_line_pointer, '}');
4396
4397   demand_empty_rest_of_line ();
4398
4399   /* Generate any deferred opcodes because we're going to be looking at
4400      the list.  */
4401   flush_pending_unwind ();
4402
4403   for (i = 0; i < 16; i++)
4404     {
4405       if (mask & (1 << i))
4406         unwind.frame_size += 8;
4407     }
4408
4409   /* Attempt to combine with a previous opcode.  We do this because gcc
4410      likes to output separate unwind directives for a single block of
4411      registers.  */
4412   if (unwind.opcode_count > 0)
4413     {
4414       i = unwind.opcodes[unwind.opcode_count - 1];
4415       if ((i & 0xf8) == 0xc0)
4416         {
4417           i &= 7;
4418           /* Only merge if the blocks are contiguous.  */
4419           if (i < 6)
4420             {
4421               if ((mask & 0xfe00) == (1 << 9))
4422                 {
4423                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4424                   unwind.opcode_count--;
4425                 }
4426             }
4427           else if (i == 6 && unwind.opcode_count >= 2)
4428             {
4429               i = unwind.opcodes[unwind.opcode_count - 2];
4430               reg = i >> 4;
4431               i &= 0xf;
4432
4433               op = 0xffff << (reg - 1);
4434               if (reg > 0
4435                   && ((mask & op) == (1u << (reg - 1))))
4436                 {
4437                   op = (1 << (reg + i + 1)) - 1;
4438                   op &= ~((1 << reg) - 1);
4439                   mask |= op;
4440                   unwind.opcode_count -= 2;
4441                 }
4442             }
4443         }
4444     }
4445
4446   hi_reg = 15;
4447   /* We want to generate opcodes in the order the registers have been
4448      saved, ie. descending order.  */
4449   for (reg = 15; reg >= -1; reg--)
4450     {
4451       /* Save registers in blocks.  */
4452       if (reg < 0
4453           || !(mask & (1 << reg)))
4454         {
4455           /* We found an unsaved reg.  Generate opcodes to save the
4456              preceding block.   */
4457           if (reg != hi_reg)
4458             {
4459               if (reg == 9)
4460                 {
4461                   /* Short form.  */
4462                   op = 0xc0 | (hi_reg - 10);
4463                   add_unwind_opcode (op, 1);
4464                 }
4465               else
4466                 {
4467                   /* Long form.  */
4468                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4469                   add_unwind_opcode (op, 2);
4470                 }
4471             }
4472           hi_reg = reg - 1;
4473         }
4474     }
4475
4476   return;
4477 error:
4478   ignore_rest_of_line ();
4479 }
4480
4481 static void
4482 s_arm_unwind_save_mmxwcg (void)
4483 {
4484   int reg;
4485   int hi_reg;
4486   unsigned mask = 0;
4487   valueT op;
4488
4489   if (*input_line_pointer == '{')
4490     input_line_pointer++;
4491
4492   skip_whitespace (input_line_pointer);
4493
4494   do
4495     {
4496       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4497
4498       if (reg == FAIL)
4499         {
4500           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4501           goto error;
4502         }
4503
4504       reg -= 8;
4505       if (mask >> reg)
4506         as_tsktsk (_("register list not in ascending order"));
4507       mask |= 1 << reg;
4508
4509       if (*input_line_pointer == '-')
4510         {
4511           input_line_pointer++;
4512           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4513           if (hi_reg == FAIL)
4514             {
4515               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4516               goto error;
4517             }
4518           else if (reg >= hi_reg)
4519             {
4520               as_bad (_("bad register range"));
4521               goto error;
4522             }
4523           for (; reg < hi_reg; reg++)
4524             mask |= 1 << reg;
4525         }
4526     }
4527   while (skip_past_comma (&input_line_pointer) != FAIL);
4528
4529   skip_past_char (&input_line_pointer, '}');
4530
4531   demand_empty_rest_of_line ();
4532
4533   /* Generate any deferred opcodes because we're going to be looking at
4534      the list.  */
4535   flush_pending_unwind ();
4536
4537   for (reg = 0; reg < 16; reg++)
4538     {
4539       if (mask & (1 << reg))
4540         unwind.frame_size += 4;
4541     }
4542   op = 0xc700 | mask;
4543   add_unwind_opcode (op, 2);
4544   return;
4545 error:
4546   ignore_rest_of_line ();
4547 }
4548
4549
4550 /* Parse an unwind_save directive.
4551    If the argument is non-zero, this is a .vsave directive.  */
4552
4553 static void
4554 s_arm_unwind_save (int arch_v6)
4555 {
4556   char *peek;
4557   struct reg_entry *reg;
4558   bfd_boolean had_brace = FALSE;
4559
4560   if (!unwind.proc_start)
4561     as_bad (MISSING_FNSTART);
4562
4563   /* Figure out what sort of save we have.  */
4564   peek = input_line_pointer;
4565
4566   if (*peek == '{')
4567     {
4568       had_brace = TRUE;
4569       peek++;
4570     }
4571
4572   reg = arm_reg_parse_multi (&peek);
4573
4574   if (!reg)
4575     {
4576       as_bad (_("register expected"));
4577       ignore_rest_of_line ();
4578       return;
4579     }
4580
4581   switch (reg->type)
4582     {
4583     case REG_TYPE_FN:
4584       if (had_brace)
4585         {
4586           as_bad (_("FPA .unwind_save does not take a register list"));
4587           ignore_rest_of_line ();
4588           return;
4589         }
4590       input_line_pointer = peek;
4591       s_arm_unwind_save_fpa (reg->number);
4592       return;
4593
4594     case REG_TYPE_RN:
4595       s_arm_unwind_save_core ();
4596       return;
4597
4598     case REG_TYPE_VFD:
4599       if (arch_v6)
4600         s_arm_unwind_save_vfp_armv6 ();
4601       else
4602         s_arm_unwind_save_vfp ();
4603       return;
4604
4605     case REG_TYPE_MMXWR:
4606       s_arm_unwind_save_mmxwr ();
4607       return;
4608
4609     case REG_TYPE_MMXWCG:
4610       s_arm_unwind_save_mmxwcg ();
4611       return;
4612
4613     default:
4614       as_bad (_(".unwind_save does not support this kind of register"));
4615       ignore_rest_of_line ();
4616     }
4617 }
4618
4619
4620 /* Parse an unwind_movsp directive.  */
4621
4622 static void
4623 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4624 {
4625   int reg;
4626   valueT op;
4627   int offset;
4628
4629   if (!unwind.proc_start)
4630     as_bad (MISSING_FNSTART);
4631
4632   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4633   if (reg == FAIL)
4634     {
4635       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4636       ignore_rest_of_line ();
4637       return;
4638     }
4639
4640   /* Optional constant.  */
4641   if (skip_past_comma (&input_line_pointer) != FAIL)
4642     {
4643       if (immediate_for_directive (&offset) == FAIL)
4644         return;
4645     }
4646   else
4647     offset = 0;
4648
4649   demand_empty_rest_of_line ();
4650
4651   if (reg == REG_SP || reg == REG_PC)
4652     {
4653       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4654       return;
4655     }
4656
4657   if (unwind.fp_reg != REG_SP)
4658     as_bad (_("unexpected .unwind_movsp directive"));
4659
4660   /* Generate opcode to restore the value.  */
4661   op = 0x90 | reg;
4662   add_unwind_opcode (op, 1);
4663
4664   /* Record the information for later.  */
4665   unwind.fp_reg = reg;
4666   unwind.fp_offset = unwind.frame_size - offset;
4667   unwind.sp_restored = 1;
4668 }
4669
4670 /* Parse an unwind_pad directive.  */
4671
4672 static void
4673 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4674 {
4675   int offset;
4676
4677   if (!unwind.proc_start)
4678     as_bad (MISSING_FNSTART);
4679
4680   if (immediate_for_directive (&offset) == FAIL)
4681     return;
4682
4683   if (offset & 3)
4684     {
4685       as_bad (_("stack increment must be multiple of 4"));
4686       ignore_rest_of_line ();
4687       return;
4688     }
4689
4690   /* Don't generate any opcodes, just record the details for later.  */
4691   unwind.frame_size += offset;
4692   unwind.pending_offset += offset;
4693
4694   demand_empty_rest_of_line ();
4695 }
4696
4697 /* Parse an unwind_setfp directive.  */
4698
4699 static void
4700 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4701 {
4702   int sp_reg;
4703   int fp_reg;
4704   int offset;
4705
4706   if (!unwind.proc_start)
4707     as_bad (MISSING_FNSTART);
4708
4709   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4710   if (skip_past_comma (&input_line_pointer) == FAIL)
4711     sp_reg = FAIL;
4712   else
4713     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4714
4715   if (fp_reg == FAIL || sp_reg == FAIL)
4716     {
4717       as_bad (_("expected <reg>, <reg>"));
4718       ignore_rest_of_line ();
4719       return;
4720     }
4721
4722   /* Optional constant.  */
4723   if (skip_past_comma (&input_line_pointer) != FAIL)
4724     {
4725       if (immediate_for_directive (&offset) == FAIL)
4726         return;
4727     }
4728   else
4729     offset = 0;
4730
4731   demand_empty_rest_of_line ();
4732
4733   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4734     {
4735       as_bad (_("register must be either sp or set by a previous"
4736                 "unwind_movsp directive"));
4737       return;
4738     }
4739
4740   /* Don't generate any opcodes, just record the information for later.  */
4741   unwind.fp_reg = fp_reg;
4742   unwind.fp_used = 1;
4743   if (sp_reg == REG_SP)
4744     unwind.fp_offset = unwind.frame_size - offset;
4745   else
4746     unwind.fp_offset -= offset;
4747 }
4748
4749 /* Parse an unwind_raw directive.  */
4750
4751 static void
4752 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4753 {
4754   expressionS exp;
4755   /* This is an arbitrary limit.         */
4756   unsigned char op[16];
4757   int count;
4758
4759   if (!unwind.proc_start)
4760     as_bad (MISSING_FNSTART);
4761
4762   expression (&exp);
4763   if (exp.X_op == O_constant
4764       && skip_past_comma (&input_line_pointer) != FAIL)
4765     {
4766       unwind.frame_size += exp.X_add_number;
4767       expression (&exp);
4768     }
4769   else
4770     exp.X_op = O_illegal;
4771
4772   if (exp.X_op != O_constant)
4773     {
4774       as_bad (_("expected <offset>, <opcode>"));
4775       ignore_rest_of_line ();
4776       return;
4777     }
4778
4779   count = 0;
4780
4781   /* Parse the opcode.  */
4782   for (;;)
4783     {
4784       if (count >= 16)
4785         {
4786           as_bad (_("unwind opcode too long"));
4787           ignore_rest_of_line ();
4788         }
4789       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4790         {
4791           as_bad (_("invalid unwind opcode"));
4792           ignore_rest_of_line ();
4793           return;
4794         }
4795       op[count++] = exp.X_add_number;
4796
4797       /* Parse the next byte.  */
4798       if (skip_past_comma (&input_line_pointer) == FAIL)
4799         break;
4800
4801       expression (&exp);
4802     }
4803
4804   /* Add the opcode bytes in reverse order.  */
4805   while (count--)
4806     add_unwind_opcode (op[count], 1);
4807
4808   demand_empty_rest_of_line ();
4809 }
4810
4811
4812 /* Parse a .eabi_attribute directive.  */
4813
4814 static void
4815 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4816 {
4817   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4818
4819   if (tag >= 0 && tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4820     attributes_set_explicitly[tag] = 1;
4821 }
4822
4823 /* Emit a tls fix for the symbol.  */
4824
4825 static void
4826 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4827 {
4828   char *p;
4829   expressionS exp;
4830 #ifdef md_flush_pending_output
4831   md_flush_pending_output ();
4832 #endif
4833
4834 #ifdef md_cons_align
4835   md_cons_align (4);
4836 #endif
4837
4838   /* Since we're just labelling the code, there's no need to define a
4839      mapping symbol.  */
4840   expression (&exp);
4841   p = obstack_next_free (&frchain_now->frch_obstack);
4842   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4843                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4844                : BFD_RELOC_ARM_TLS_DESCSEQ);
4845 }
4846 #endif /* OBJ_ELF */
4847
4848 static void s_arm_arch (int);
4849 static void s_arm_object_arch (int);
4850 static void s_arm_cpu (int);
4851 static void s_arm_fpu (int);
4852 static void s_arm_arch_extension (int);
4853
4854 #ifdef TE_PE
4855
4856 static void
4857 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4858 {
4859   expressionS exp;
4860
4861   do
4862     {
4863       expression (&exp);
4864       if (exp.X_op == O_symbol)
4865         exp.X_op = O_secrel;
4866
4867       emit_expr (&exp, 4);
4868     }
4869   while (*input_line_pointer++ == ',');
4870
4871   input_line_pointer--;
4872   demand_empty_rest_of_line ();
4873 }
4874 #endif /* TE_PE */
4875
4876 /* This table describes all the machine specific pseudo-ops the assembler
4877    has to support.  The fields are:
4878      pseudo-op name without dot
4879      function to call to execute this pseudo-op
4880      Integer arg to pass to the function.  */
4881
4882 const pseudo_typeS md_pseudo_table[] =
4883 {
4884   /* Never called because '.req' does not start a line.  */
4885   { "req",         s_req,         0 },
4886   /* Following two are likewise never called.  */
4887   { "dn",          s_dn,          0 },
4888   { "qn",          s_qn,          0 },
4889   { "unreq",       s_unreq,       0 },
4890   { "bss",         s_bss,         0 },
4891   { "align",       s_align_ptwo,  2 },
4892   { "arm",         s_arm,         0 },
4893   { "thumb",       s_thumb,       0 },
4894   { "code",        s_code,        0 },
4895   { "force_thumb", s_force_thumb, 0 },
4896   { "thumb_func",  s_thumb_func,  0 },
4897   { "thumb_set",   s_thumb_set,   0 },
4898   { "even",        s_even,        0 },
4899   { "ltorg",       s_ltorg,       0 },
4900   { "pool",        s_ltorg,       0 },
4901   { "syntax",      s_syntax,      0 },
4902   { "cpu",         s_arm_cpu,     0 },
4903   { "arch",        s_arm_arch,    0 },
4904   { "object_arch", s_arm_object_arch,   0 },
4905   { "fpu",         s_arm_fpu,     0 },
4906   { "arch_extension", s_arm_arch_extension, 0 },
4907 #ifdef OBJ_ELF
4908   { "word",             s_arm_elf_cons, 4 },
4909   { "long",             s_arm_elf_cons, 4 },
4910   { "inst.n",           s_arm_elf_inst, 2 },
4911   { "inst.w",           s_arm_elf_inst, 4 },
4912   { "inst",             s_arm_elf_inst, 0 },
4913   { "rel31",            s_arm_rel31,      0 },
4914   { "fnstart",          s_arm_unwind_fnstart,   0 },
4915   { "fnend",            s_arm_unwind_fnend,     0 },
4916   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4917   { "personality",      s_arm_unwind_personality, 0 },
4918   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4919   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4920   { "save",             s_arm_unwind_save,      0 },
4921   { "vsave",            s_arm_unwind_save,      1 },
4922   { "movsp",            s_arm_unwind_movsp,     0 },
4923   { "pad",              s_arm_unwind_pad,       0 },
4924   { "setfp",            s_arm_unwind_setfp,     0 },
4925   { "unwind_raw",       s_arm_unwind_raw,       0 },
4926   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4927   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4928 #else
4929   { "word",        cons, 4},
4930
4931   /* These are used for dwarf.  */
4932   {"2byte", cons, 2},
4933   {"4byte", cons, 4},
4934   {"8byte", cons, 8},
4935   /* These are used for dwarf2.  */
4936   { "file", dwarf2_directive_file, 0 },
4937   { "loc",  dwarf2_directive_loc,  0 },
4938   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4939 #endif
4940   { "extend",      float_cons, 'x' },
4941   { "ldouble",     float_cons, 'x' },
4942   { "packed",      float_cons, 'p' },
4943 #ifdef TE_PE
4944   {"secrel32", pe_directive_secrel, 0},
4945 #endif
4946
4947   /* These are for compatibility with CodeComposer Studio.  */
4948   {"ref",          s_ccs_ref,        0},
4949   {"def",          s_ccs_def,        0},
4950   {"asmfunc",      s_ccs_asmfunc,    0},
4951   {"endasmfunc",   s_ccs_endasmfunc, 0},
4952
4953   { 0, 0, 0 }
4954 };
4955 \f
4956 /* Parser functions used exclusively in instruction operands.  */
4957
4958 /* Generic immediate-value read function for use in insn parsing.
4959    STR points to the beginning of the immediate (the leading #);
4960    VAL receives the value; if the value is outside [MIN, MAX]
4961    issue an error.  PREFIX_OPT is true if the immediate prefix is
4962    optional.  */
4963
4964 static int
4965 parse_immediate (char **str, int *val, int min, int max,
4966                  bfd_boolean prefix_opt)
4967 {
4968   expressionS exp;
4969
4970   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4971   if (exp.X_op != O_constant)
4972     {
4973       inst.error = _("constant expression required");
4974       return FAIL;
4975     }
4976
4977   if (exp.X_add_number < min || exp.X_add_number > max)
4978     {
4979       inst.error = _("immediate value out of range");
4980       return FAIL;
4981     }
4982
4983   *val = exp.X_add_number;
4984   return SUCCESS;
4985 }
4986
4987 /* Less-generic immediate-value read function with the possibility of loading a
4988    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4989    instructions. Puts the result directly in inst.operands[i].  */
4990
4991 static int
4992 parse_big_immediate (char **str, int i, expressionS *in_exp,
4993                      bfd_boolean allow_symbol_p)
4994 {
4995   expressionS exp;
4996   expressionS *exp_p = in_exp ? in_exp : &exp;
4997   char *ptr = *str;
4998
4999   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
5000
5001   if (exp_p->X_op == O_constant)
5002     {
5003       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
5004       /* If we're on a 64-bit host, then a 64-bit number can be returned using
5005          O_constant.  We have to be careful not to break compilation for
5006          32-bit X_add_number, though.  */
5007       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
5008         {
5009           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
5010           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
5011                                   & 0xffffffff);
5012           inst.operands[i].regisimm = 1;
5013         }
5014     }
5015   else if (exp_p->X_op == O_big
5016            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
5017     {
5018       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
5019
5020       /* Bignums have their least significant bits in
5021          generic_bignum[0]. Make sure we put 32 bits in imm and
5022          32 bits in reg,  in a (hopefully) portable way.  */
5023       gas_assert (parts != 0);
5024
5025       /* Make sure that the number is not too big.
5026          PR 11972: Bignums can now be sign-extended to the
5027          size of a .octa so check that the out of range bits
5028          are all zero or all one.  */
5029       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
5030         {
5031           LITTLENUM_TYPE m = -1;
5032
5033           if (generic_bignum[parts * 2] != 0
5034               && generic_bignum[parts * 2] != m)
5035             return FAIL;
5036
5037           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
5038             if (generic_bignum[j] != generic_bignum[j-1])
5039               return FAIL;
5040         }
5041
5042       inst.operands[i].imm = 0;
5043       for (j = 0; j < parts; j++, idx++)
5044         inst.operands[i].imm |= generic_bignum[idx]
5045                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5046       inst.operands[i].reg = 0;
5047       for (j = 0; j < parts; j++, idx++)
5048         inst.operands[i].reg |= generic_bignum[idx]
5049                                 << (LITTLENUM_NUMBER_OF_BITS * j);
5050       inst.operands[i].regisimm = 1;
5051     }
5052   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
5053     return FAIL;
5054
5055   *str = ptr;
5056
5057   return SUCCESS;
5058 }
5059
5060 /* Returns the pseudo-register number of an FPA immediate constant,
5061    or FAIL if there isn't a valid constant here.  */
5062
5063 static int
5064 parse_fpa_immediate (char ** str)
5065 {
5066   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5067   char *         save_in;
5068   expressionS    exp;
5069   int            i;
5070   int            j;
5071
5072   /* First try and match exact strings, this is to guarantee
5073      that some formats will work even for cross assembly.  */
5074
5075   for (i = 0; fp_const[i]; i++)
5076     {
5077       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
5078         {
5079           char *start = *str;
5080
5081           *str += strlen (fp_const[i]);
5082           if (is_end_of_line[(unsigned char) **str])
5083             return i + 8;
5084           *str = start;
5085         }
5086     }
5087
5088   /* Just because we didn't get a match doesn't mean that the constant
5089      isn't valid, just that it is in a format that we don't
5090      automatically recognize.  Try parsing it with the standard
5091      expression routines.  */
5092
5093   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
5094
5095   /* Look for a raw floating point number.  */
5096   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
5097       && is_end_of_line[(unsigned char) *save_in])
5098     {
5099       for (i = 0; i < NUM_FLOAT_VALS; i++)
5100         {
5101           for (j = 0; j < MAX_LITTLENUMS; j++)
5102             {
5103               if (words[j] != fp_values[i][j])
5104                 break;
5105             }
5106
5107           if (j == MAX_LITTLENUMS)
5108             {
5109               *str = save_in;
5110               return i + 8;
5111             }
5112         }
5113     }
5114
5115   /* Try and parse a more complex expression, this will probably fail
5116      unless the code uses a floating point prefix (eg "0f").  */
5117   save_in = input_line_pointer;
5118   input_line_pointer = *str;
5119   if (expression (&exp) == absolute_section
5120       && exp.X_op == O_big
5121       && exp.X_add_number < 0)
5122     {
5123       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
5124          Ditto for 15.  */
5125 #define X_PRECISION 5
5126 #define E_PRECISION 15L
5127       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
5128         {
5129           for (i = 0; i < NUM_FLOAT_VALS; i++)
5130             {
5131               for (j = 0; j < MAX_LITTLENUMS; j++)
5132                 {
5133                   if (words[j] != fp_values[i][j])
5134                     break;
5135                 }
5136
5137               if (j == MAX_LITTLENUMS)
5138                 {
5139                   *str = input_line_pointer;
5140                   input_line_pointer = save_in;
5141                   return i + 8;
5142                 }
5143             }
5144         }
5145     }
5146
5147   *str = input_line_pointer;
5148   input_line_pointer = save_in;
5149   inst.error = _("invalid FPA immediate expression");
5150   return FAIL;
5151 }
5152
5153 /* Returns 1 if a number has "quarter-precision" float format
5154    0baBbbbbbc defgh000 00000000 00000000.  */
5155
5156 static int
5157 is_quarter_float (unsigned imm)
5158 {
5159   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
5160   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
5161 }
5162
5163
5164 /* Detect the presence of a floating point or integer zero constant,
5165    i.e. #0.0 or #0.  */
5166
5167 static bfd_boolean
5168 parse_ifimm_zero (char **in)
5169 {
5170   int error_code;
5171
5172   if (!is_immediate_prefix (**in))
5173     {
5174       /* In unified syntax, all prefixes are optional.  */
5175       if (!unified_syntax)
5176         return FALSE;
5177     }
5178   else
5179     ++*in;
5180
5181   /* Accept #0x0 as a synonym for #0.  */
5182   if (strncmp (*in, "0x", 2) == 0)
5183     {
5184       int val;
5185       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5186         return FALSE;
5187       return TRUE;
5188     }
5189
5190   error_code = atof_generic (in, ".", EXP_CHARS,
5191                              &generic_floating_point_number);
5192
5193   if (!error_code
5194       && generic_floating_point_number.sign == '+'
5195       && (generic_floating_point_number.low
5196           > generic_floating_point_number.leader))
5197     return TRUE;
5198
5199   return FALSE;
5200 }
5201
5202 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5203    0baBbbbbbc defgh000 00000000 00000000.
5204    The zero and minus-zero cases need special handling, since they can't be
5205    encoded in the "quarter-precision" float format, but can nonetheless be
5206    loaded as integer constants.  */
5207
5208 static unsigned
5209 parse_qfloat_immediate (char **ccp, int *immed)
5210 {
5211   char *str = *ccp;
5212   char *fpnum;
5213   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5214   int found_fpchar = 0;
5215
5216   skip_past_char (&str, '#');
5217
5218   /* We must not accidentally parse an integer as a floating-point number. Make
5219      sure that the value we parse is not an integer by checking for special
5220      characters '.' or 'e'.
5221      FIXME: This is a horrible hack, but doing better is tricky because type
5222      information isn't in a very usable state at parse time.  */
5223   fpnum = str;
5224   skip_whitespace (fpnum);
5225
5226   if (strncmp (fpnum, "0x", 2) == 0)
5227     return FAIL;
5228   else
5229     {
5230       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5231         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5232           {
5233             found_fpchar = 1;
5234             break;
5235           }
5236
5237       if (!found_fpchar)
5238         return FAIL;
5239     }
5240
5241   if ((str = atof_ieee (str, 's', words)) != NULL)
5242     {
5243       unsigned fpword = 0;
5244       int i;
5245
5246       /* Our FP word must be 32 bits (single-precision FP).  */
5247       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5248         {
5249           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5250           fpword |= words[i];
5251         }
5252
5253       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5254         *immed = fpword;
5255       else
5256         return FAIL;
5257
5258       *ccp = str;
5259
5260       return SUCCESS;
5261     }
5262
5263   return FAIL;
5264 }
5265
5266 /* Shift operands.  */
5267 enum shift_kind
5268 {
5269   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5270 };
5271
5272 struct asm_shift_name
5273 {
5274   const char      *name;
5275   enum shift_kind  kind;
5276 };
5277
5278 /* Third argument to parse_shift.  */
5279 enum parse_shift_mode
5280 {
5281   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5282   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5283   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5284   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5285   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5286 };
5287
5288 /* Parse a <shift> specifier on an ARM data processing instruction.
5289    This has three forms:
5290
5291      (LSL|LSR|ASL|ASR|ROR) Rs
5292      (LSL|LSR|ASL|ASR|ROR) #imm
5293      RRX
5294
5295    Note that ASL is assimilated to LSL in the instruction encoding, and
5296    RRX to ROR #0 (which cannot be written as such).  */
5297
5298 static int
5299 parse_shift (char **str, int i, enum parse_shift_mode mode)
5300 {
5301   const struct asm_shift_name *shift_name;
5302   enum shift_kind shift;
5303   char *s = *str;
5304   char *p = s;
5305   int reg;
5306
5307   for (p = *str; ISALPHA (*p); p++)
5308     ;
5309
5310   if (p == *str)
5311     {
5312       inst.error = _("shift expression expected");
5313       return FAIL;
5314     }
5315
5316   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5317                                                             p - *str);
5318
5319   if (shift_name == NULL)
5320     {
5321       inst.error = _("shift expression expected");
5322       return FAIL;
5323     }
5324
5325   shift = shift_name->kind;
5326
5327   switch (mode)
5328     {
5329     case NO_SHIFT_RESTRICT:
5330     case SHIFT_IMMEDIATE:   break;
5331
5332     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5333       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5334         {
5335           inst.error = _("'LSL' or 'ASR' required");
5336           return FAIL;
5337         }
5338       break;
5339
5340     case SHIFT_LSL_IMMEDIATE:
5341       if (shift != SHIFT_LSL)
5342         {
5343           inst.error = _("'LSL' required");
5344           return FAIL;
5345         }
5346       break;
5347
5348     case SHIFT_ASR_IMMEDIATE:
5349       if (shift != SHIFT_ASR)
5350         {
5351           inst.error = _("'ASR' required");
5352           return FAIL;
5353         }
5354       break;
5355
5356     default: abort ();
5357     }
5358
5359   if (shift != SHIFT_RRX)
5360     {
5361       /* Whitespace can appear here if the next thing is a bare digit.  */
5362       skip_whitespace (p);
5363
5364       if (mode == NO_SHIFT_RESTRICT
5365           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5366         {
5367           inst.operands[i].imm = reg;
5368           inst.operands[i].immisreg = 1;
5369         }
5370       else if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5371         return FAIL;
5372     }
5373   inst.operands[i].shift_kind = shift;
5374   inst.operands[i].shifted = 1;
5375   *str = p;
5376   return SUCCESS;
5377 }
5378
5379 /* Parse a <shifter_operand> for an ARM data processing instruction:
5380
5381       #<immediate>
5382       #<immediate>, <rotate>
5383       <Rm>
5384       <Rm>, <shift>
5385
5386    where <shift> is defined by parse_shift above, and <rotate> is a
5387    multiple of 2 between 0 and 30.  Validation of immediate operands
5388    is deferred to md_apply_fix.  */
5389
5390 static int
5391 parse_shifter_operand (char **str, int i)
5392 {
5393   int value;
5394   expressionS exp;
5395
5396   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5397     {
5398       inst.operands[i].reg = value;
5399       inst.operands[i].isreg = 1;
5400
5401       /* parse_shift will override this if appropriate */
5402       inst.relocs[0].exp.X_op = O_constant;
5403       inst.relocs[0].exp.X_add_number = 0;
5404
5405       if (skip_past_comma (str) == FAIL)
5406         return SUCCESS;
5407
5408       /* Shift operation on register.  */
5409       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5410     }
5411
5412   if (my_get_expression (&inst.relocs[0].exp, str, GE_IMM_PREFIX))
5413     return FAIL;
5414
5415   if (skip_past_comma (str) == SUCCESS)
5416     {
5417       /* #x, y -- ie explicit rotation by Y.  */
5418       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5419         return FAIL;
5420
5421       if (exp.X_op != O_constant || inst.relocs[0].exp.X_op != O_constant)
5422         {
5423           inst.error = _("constant expression expected");
5424           return FAIL;
5425         }
5426
5427       value = exp.X_add_number;
5428       if (value < 0 || value > 30 || value % 2 != 0)
5429         {
5430           inst.error = _("invalid rotation");
5431           return FAIL;
5432         }
5433       if (inst.relocs[0].exp.X_add_number < 0
5434           || inst.relocs[0].exp.X_add_number > 255)
5435         {
5436           inst.error = _("invalid constant");
5437           return FAIL;
5438         }
5439
5440       /* Encode as specified.  */
5441       inst.operands[i].imm = inst.relocs[0].exp.X_add_number | value << 7;
5442       return SUCCESS;
5443     }
5444
5445   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
5446   inst.relocs[0].pc_rel = 0;
5447   return SUCCESS;
5448 }
5449
5450 /* Group relocation information.  Each entry in the table contains the
5451    textual name of the relocation as may appear in assembler source
5452    and must end with a colon.
5453    Along with this textual name are the relocation codes to be used if
5454    the corresponding instruction is an ALU instruction (ADD or SUB only),
5455    an LDR, an LDRS, or an LDC.  */
5456
5457 struct group_reloc_table_entry
5458 {
5459   const char *name;
5460   int alu_code;
5461   int ldr_code;
5462   int ldrs_code;
5463   int ldc_code;
5464 };
5465
5466 typedef enum
5467 {
5468   /* Varieties of non-ALU group relocation.  */
5469
5470   GROUP_LDR,
5471   GROUP_LDRS,
5472   GROUP_LDC
5473 } group_reloc_type;
5474
5475 static struct group_reloc_table_entry group_reloc_table[] =
5476   { /* Program counter relative: */
5477     { "pc_g0_nc",
5478       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5479       0,                                /* LDR */
5480       0,                                /* LDRS */
5481       0 },                              /* LDC */
5482     { "pc_g0",
5483       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5484       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5485       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5486       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5487     { "pc_g1_nc",
5488       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5489       0,                                /* LDR */
5490       0,                                /* LDRS */
5491       0 },                              /* LDC */
5492     { "pc_g1",
5493       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5494       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5495       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5496       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5497     { "pc_g2",
5498       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5499       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5500       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5501       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5502     /* Section base relative */
5503     { "sb_g0_nc",
5504       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5505       0,                                /* LDR */
5506       0,                                /* LDRS */
5507       0 },                              /* LDC */
5508     { "sb_g0",
5509       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5510       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5511       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5512       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5513     { "sb_g1_nc",
5514       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5515       0,                                /* LDR */
5516       0,                                /* LDRS */
5517       0 },                              /* LDC */
5518     { "sb_g1",
5519       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5520       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5521       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5522       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5523     { "sb_g2",
5524       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5525       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5526       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5527       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5528     /* Absolute thumb alu relocations.  */
5529     { "lower0_7",
5530       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5531       0,                                /* LDR.  */
5532       0,                                /* LDRS.  */
5533       0 },                              /* LDC.  */
5534     { "lower8_15",
5535       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5536       0,                                /* LDR.  */
5537       0,                                /* LDRS.  */
5538       0 },                              /* LDC.  */
5539     { "upper0_7",
5540       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5541       0,                                /* LDR.  */
5542       0,                                /* LDRS.  */
5543       0 },                              /* LDC.  */
5544     { "upper8_15",
5545       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5546       0,                                /* LDR.  */
5547       0,                                /* LDRS.  */
5548       0 } };                            /* LDC.  */
5549
5550 /* Given the address of a pointer pointing to the textual name of a group
5551    relocation as may appear in assembler source, attempt to find its details
5552    in group_reloc_table.  The pointer will be updated to the character after
5553    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5554    otherwise.  On success, *entry will be updated to point at the relevant
5555    group_reloc_table entry. */
5556
5557 static int
5558 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5559 {
5560   unsigned int i;
5561   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5562     {
5563       int length = strlen (group_reloc_table[i].name);
5564
5565       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5566           && (*str)[length] == ':')
5567         {
5568           *out = &group_reloc_table[i];
5569           *str += (length + 1);
5570           return SUCCESS;
5571         }
5572     }
5573
5574   return FAIL;
5575 }
5576
5577 /* Parse a <shifter_operand> for an ARM data processing instruction
5578    (as for parse_shifter_operand) where group relocations are allowed:
5579
5580       #<immediate>
5581       #<immediate>, <rotate>
5582       #:<group_reloc>:<expression>
5583       <Rm>
5584       <Rm>, <shift>
5585
5586    where <group_reloc> is one of the strings defined in group_reloc_table.
5587    The hashes are optional.
5588
5589    Everything else is as for parse_shifter_operand.  */
5590
5591 static parse_operand_result
5592 parse_shifter_operand_group_reloc (char **str, int i)
5593 {
5594   /* Determine if we have the sequence of characters #: or just :
5595      coming next.  If we do, then we check for a group relocation.
5596      If we don't, punt the whole lot to parse_shifter_operand.  */
5597
5598   if (((*str)[0] == '#' && (*str)[1] == ':')
5599       || (*str)[0] == ':')
5600     {
5601       struct group_reloc_table_entry *entry;
5602
5603       if ((*str)[0] == '#')
5604         (*str) += 2;
5605       else
5606         (*str)++;
5607
5608       /* Try to parse a group relocation.  Anything else is an error.  */
5609       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5610         {
5611           inst.error = _("unknown group relocation");
5612           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5613         }
5614
5615       /* We now have the group relocation table entry corresponding to
5616          the name in the assembler source.  Next, we parse the expression.  */
5617       if (my_get_expression (&inst.relocs[0].exp, str, GE_NO_PREFIX))
5618         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5619
5620       /* Record the relocation type (always the ALU variant here).  */
5621       inst.relocs[0].type = (bfd_reloc_code_real_type) entry->alu_code;
5622       gas_assert (inst.relocs[0].type != 0);
5623
5624       return PARSE_OPERAND_SUCCESS;
5625     }
5626   else
5627     return parse_shifter_operand (str, i) == SUCCESS
5628            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5629
5630   /* Never reached.  */
5631 }
5632
5633 /* Parse a Neon alignment expression.  Information is written to
5634    inst.operands[i].  We assume the initial ':' has been skipped.
5635
5636    align        .imm = align << 8, .immisalign=1, .preind=0  */
5637 static parse_operand_result
5638 parse_neon_alignment (char **str, int i)
5639 {
5640   char *p = *str;
5641   expressionS exp;
5642
5643   my_get_expression (&exp, &p, GE_NO_PREFIX);
5644
5645   if (exp.X_op != O_constant)
5646     {
5647       inst.error = _("alignment must be constant");
5648       return PARSE_OPERAND_FAIL;
5649     }
5650
5651   inst.operands[i].imm = exp.X_add_number << 8;
5652   inst.operands[i].immisalign = 1;
5653   /* Alignments are not pre-indexes.  */
5654   inst.operands[i].preind = 0;
5655
5656   *str = p;
5657   return PARSE_OPERAND_SUCCESS;
5658 }
5659
5660 /* Parse all forms of an ARM address expression.  Information is written
5661    to inst.operands[i] and/or inst.relocs[0].
5662
5663    Preindexed addressing (.preind=1):
5664
5665    [Rn, #offset]       .reg=Rn .relocs[0].exp=offset
5666    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5667    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5668                        .shift_kind=shift .relocs[0].exp=shift_imm
5669
5670    These three may have a trailing ! which causes .writeback to be set also.
5671
5672    Postindexed addressing (.postind=1, .writeback=1):
5673
5674    [Rn], #offset       .reg=Rn .relocs[0].exp=offset
5675    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5676    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5677                        .shift_kind=shift .relocs[0].exp=shift_imm
5678
5679    Unindexed addressing (.preind=0, .postind=0):
5680
5681    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5682
5683    Other:
5684
5685    [Rn]{!}             shorthand for [Rn,#0]{!}
5686    =immediate          .isreg=0 .relocs[0].exp=immediate
5687    label               .reg=PC .relocs[0].pc_rel=1 .relocs[0].exp=label
5688
5689   It is the caller's responsibility to check for addressing modes not
5690   supported by the instruction, and to set inst.relocs[0].type.  */
5691
5692 static parse_operand_result
5693 parse_address_main (char **str, int i, int group_relocations,
5694                     group_reloc_type group_type)
5695 {
5696   char *p = *str;
5697   int reg;
5698
5699   if (skip_past_char (&p, '[') == FAIL)
5700     {
5701       if (skip_past_char (&p, '=') == FAIL)
5702         {
5703           /* Bare address - translate to PC-relative offset.  */
5704           inst.relocs[0].pc_rel = 1;
5705           inst.operands[i].reg = REG_PC;
5706           inst.operands[i].isreg = 1;
5707           inst.operands[i].preind = 1;
5708
5709           if (my_get_expression (&inst.relocs[0].exp, &p, GE_OPT_PREFIX_BIG))
5710             return PARSE_OPERAND_FAIL;
5711         }
5712       else if (parse_big_immediate (&p, i, &inst.relocs[0].exp,
5713                                     /*allow_symbol_p=*/TRUE))
5714         return PARSE_OPERAND_FAIL;
5715
5716       *str = p;
5717       return PARSE_OPERAND_SUCCESS;
5718     }
5719
5720   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5721   skip_whitespace (p);
5722
5723   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5724     {
5725       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5726       return PARSE_OPERAND_FAIL;
5727     }
5728   inst.operands[i].reg = reg;
5729   inst.operands[i].isreg = 1;
5730
5731   if (skip_past_comma (&p) == SUCCESS)
5732     {
5733       inst.operands[i].preind = 1;
5734
5735       if (*p == '+') p++;
5736       else if (*p == '-') p++, inst.operands[i].negative = 1;
5737
5738       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5739         {
5740           inst.operands[i].imm = reg;
5741           inst.operands[i].immisreg = 1;
5742
5743           if (skip_past_comma (&p) == SUCCESS)
5744             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5745               return PARSE_OPERAND_FAIL;
5746         }
5747       else if (skip_past_char (&p, ':') == SUCCESS)
5748         {
5749           /* FIXME: '@' should be used here, but it's filtered out by generic
5750              code before we get to see it here. This may be subject to
5751              change.  */
5752           parse_operand_result result = parse_neon_alignment (&p, i);
5753
5754           if (result != PARSE_OPERAND_SUCCESS)
5755             return result;
5756         }
5757       else
5758         {
5759           if (inst.operands[i].negative)
5760             {
5761               inst.operands[i].negative = 0;
5762               p--;
5763             }
5764
5765           if (group_relocations
5766               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5767             {
5768               struct group_reloc_table_entry *entry;
5769
5770               /* Skip over the #: or : sequence.  */
5771               if (*p == '#')
5772                 p += 2;
5773               else
5774                 p++;
5775
5776               /* Try to parse a group relocation.  Anything else is an
5777                  error.  */
5778               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5779                 {
5780                   inst.error = _("unknown group relocation");
5781                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5782                 }
5783
5784               /* We now have the group relocation table entry corresponding to
5785                  the name in the assembler source.  Next, we parse the
5786                  expression.  */
5787               if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
5788                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5789
5790               /* Record the relocation type.  */
5791               switch (group_type)
5792                 {
5793                   case GROUP_LDR:
5794                     inst.relocs[0].type
5795                         = (bfd_reloc_code_real_type) entry->ldr_code;
5796                     break;
5797
5798                   case GROUP_LDRS:
5799                     inst.relocs[0].type
5800                         = (bfd_reloc_code_real_type) entry->ldrs_code;
5801                     break;
5802
5803                   case GROUP_LDC:
5804                     inst.relocs[0].type
5805                         = (bfd_reloc_code_real_type) entry->ldc_code;
5806                     break;
5807
5808                   default:
5809                     gas_assert (0);
5810                 }
5811
5812               if (inst.relocs[0].type == 0)
5813                 {
5814                   inst.error = _("this group relocation is not allowed on this instruction");
5815                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5816                 }
5817             }
5818           else
5819             {
5820               char *q = p;
5821
5822               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5823                 return PARSE_OPERAND_FAIL;
5824               /* If the offset is 0, find out if it's a +0 or -0.  */
5825               if (inst.relocs[0].exp.X_op == O_constant
5826                   && inst.relocs[0].exp.X_add_number == 0)
5827                 {
5828                   skip_whitespace (q);
5829                   if (*q == '#')
5830                     {
5831                       q++;
5832                       skip_whitespace (q);
5833                     }
5834                   if (*q == '-')
5835                     inst.operands[i].negative = 1;
5836                 }
5837             }
5838         }
5839     }
5840   else if (skip_past_char (&p, ':') == SUCCESS)
5841     {
5842       /* FIXME: '@' should be used here, but it's filtered out by generic code
5843          before we get to see it here. This may be subject to change.  */
5844       parse_operand_result result = parse_neon_alignment (&p, i);
5845
5846       if (result != PARSE_OPERAND_SUCCESS)
5847         return result;
5848     }
5849
5850   if (skip_past_char (&p, ']') == FAIL)
5851     {
5852       inst.error = _("']' expected");
5853       return PARSE_OPERAND_FAIL;
5854     }
5855
5856   if (skip_past_char (&p, '!') == SUCCESS)
5857     inst.operands[i].writeback = 1;
5858
5859   else if (skip_past_comma (&p) == SUCCESS)
5860     {
5861       if (skip_past_char (&p, '{') == SUCCESS)
5862         {
5863           /* [Rn], {expr} - unindexed, with option */
5864           if (parse_immediate (&p, &inst.operands[i].imm,
5865                                0, 255, TRUE) == FAIL)
5866             return PARSE_OPERAND_FAIL;
5867
5868           if (skip_past_char (&p, '}') == FAIL)
5869             {
5870               inst.error = _("'}' expected at end of 'option' field");
5871               return PARSE_OPERAND_FAIL;
5872             }
5873           if (inst.operands[i].preind)
5874             {
5875               inst.error = _("cannot combine index with option");
5876               return PARSE_OPERAND_FAIL;
5877             }
5878           *str = p;
5879           return PARSE_OPERAND_SUCCESS;
5880         }
5881       else
5882         {
5883           inst.operands[i].postind = 1;
5884           inst.operands[i].writeback = 1;
5885
5886           if (inst.operands[i].preind)
5887             {
5888               inst.error = _("cannot combine pre- and post-indexing");
5889               return PARSE_OPERAND_FAIL;
5890             }
5891
5892           if (*p == '+') p++;
5893           else if (*p == '-') p++, inst.operands[i].negative = 1;
5894
5895           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5896             {
5897               /* We might be using the immediate for alignment already. If we
5898                  are, OR the register number into the low-order bits.  */
5899               if (inst.operands[i].immisalign)
5900                 inst.operands[i].imm |= reg;
5901               else
5902                 inst.operands[i].imm = reg;
5903               inst.operands[i].immisreg = 1;
5904
5905               if (skip_past_comma (&p) == SUCCESS)
5906                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5907                   return PARSE_OPERAND_FAIL;
5908             }
5909           else
5910             {
5911               char *q = p;
5912
5913               if (inst.operands[i].negative)
5914                 {
5915                   inst.operands[i].negative = 0;
5916                   p--;
5917                 }
5918               if (my_get_expression (&inst.relocs[0].exp, &p, GE_IMM_PREFIX))
5919                 return PARSE_OPERAND_FAIL;
5920               /* If the offset is 0, find out if it's a +0 or -0.  */
5921               if (inst.relocs[0].exp.X_op == O_constant
5922                   && inst.relocs[0].exp.X_add_number == 0)
5923                 {
5924                   skip_whitespace (q);
5925                   if (*q == '#')
5926                     {
5927                       q++;
5928                       skip_whitespace (q);
5929                     }
5930                   if (*q == '-')
5931                     inst.operands[i].negative = 1;
5932                 }
5933             }
5934         }
5935     }
5936
5937   /* If at this point neither .preind nor .postind is set, we have a
5938      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5939   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5940     {
5941       inst.operands[i].preind = 1;
5942       inst.relocs[0].exp.X_op = O_constant;
5943       inst.relocs[0].exp.X_add_number = 0;
5944     }
5945   *str = p;
5946   return PARSE_OPERAND_SUCCESS;
5947 }
5948
5949 static int
5950 parse_address (char **str, int i)
5951 {
5952   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5953          ? SUCCESS : FAIL;
5954 }
5955
5956 static parse_operand_result
5957 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5958 {
5959   return parse_address_main (str, i, 1, type);
5960 }
5961
5962 /* Parse an operand for a MOVW or MOVT instruction.  */
5963 static int
5964 parse_half (char **str)
5965 {
5966   char * p;
5967
5968   p = *str;
5969   skip_past_char (&p, '#');
5970   if (strncasecmp (p, ":lower16:", 9) == 0)
5971     inst.relocs[0].type = BFD_RELOC_ARM_MOVW;
5972   else if (strncasecmp (p, ":upper16:", 9) == 0)
5973     inst.relocs[0].type = BFD_RELOC_ARM_MOVT;
5974
5975   if (inst.relocs[0].type != BFD_RELOC_UNUSED)
5976     {
5977       p += 9;
5978       skip_whitespace (p);
5979     }
5980
5981   if (my_get_expression (&inst.relocs[0].exp, &p, GE_NO_PREFIX))
5982     return FAIL;
5983
5984   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
5985     {
5986       if (inst.relocs[0].exp.X_op != O_constant)
5987         {
5988           inst.error = _("constant expression expected");
5989           return FAIL;
5990         }
5991       if (inst.relocs[0].exp.X_add_number < 0
5992           || inst.relocs[0].exp.X_add_number > 0xffff)
5993         {
5994           inst.error = _("immediate value out of range");
5995           return FAIL;
5996         }
5997     }
5998   *str = p;
5999   return SUCCESS;
6000 }
6001
6002 /* Miscellaneous. */
6003
6004 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
6005    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
6006 static int
6007 parse_psr (char **str, bfd_boolean lhs)
6008 {
6009   char *p;
6010   unsigned long psr_field;
6011   const struct asm_psr *psr;
6012   char *start;
6013   bfd_boolean is_apsr = FALSE;
6014   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
6015
6016   /* PR gas/12698:  If the user has specified -march=all then m_profile will
6017      be TRUE, but we want to ignore it in this case as we are building for any
6018      CPU type, including non-m variants.  */
6019   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
6020     m_profile = FALSE;
6021
6022   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
6023      feature for ease of use and backwards compatibility.  */
6024   p = *str;
6025   if (strncasecmp (p, "SPSR", 4) == 0)
6026     {
6027       if (m_profile)
6028         goto unsupported_psr;
6029
6030       psr_field = SPSR_BIT;
6031     }
6032   else if (strncasecmp (p, "CPSR", 4) == 0)
6033     {
6034       if (m_profile)
6035         goto unsupported_psr;
6036
6037       psr_field = 0;
6038     }
6039   else if (strncasecmp (p, "APSR", 4) == 0)
6040     {
6041       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
6042          and ARMv7-R architecture CPUs.  */
6043       is_apsr = TRUE;
6044       psr_field = 0;
6045     }
6046   else if (m_profile)
6047     {
6048       start = p;
6049       do
6050         p++;
6051       while (ISALNUM (*p) || *p == '_');
6052
6053       if (strncasecmp (start, "iapsr", 5) == 0
6054           || strncasecmp (start, "eapsr", 5) == 0
6055           || strncasecmp (start, "xpsr", 4) == 0
6056           || strncasecmp (start, "psr", 3) == 0)
6057         p = start + strcspn (start, "rR") + 1;
6058
6059       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
6060                                                   p - start);
6061
6062       if (!psr)
6063         return FAIL;
6064
6065       /* If APSR is being written, a bitfield may be specified.  Note that
6066          APSR itself is handled above.  */
6067       if (psr->field <= 3)
6068         {
6069           psr_field = psr->field;
6070           is_apsr = TRUE;
6071           goto check_suffix;
6072         }
6073
6074       *str = p;
6075       /* M-profile MSR instructions have the mask field set to "10", except
6076          *PSR variants which modify APSR, which may use a different mask (and
6077          have been handled already).  Do that by setting the PSR_f field
6078          here.  */
6079       return psr->field | (lhs ? PSR_f : 0);
6080     }
6081   else
6082     goto unsupported_psr;
6083
6084   p += 4;
6085 check_suffix:
6086   if (*p == '_')
6087     {
6088       /* A suffix follows.  */
6089       p++;
6090       start = p;
6091
6092       do
6093         p++;
6094       while (ISALNUM (*p) || *p == '_');
6095
6096       if (is_apsr)
6097         {
6098           /* APSR uses a notation for bits, rather than fields.  */
6099           unsigned int nzcvq_bits = 0;
6100           unsigned int g_bit = 0;
6101           char *bit;
6102
6103           for (bit = start; bit != p; bit++)
6104             {
6105               switch (TOLOWER (*bit))
6106                 {
6107                 case 'n':
6108                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
6109                   break;
6110
6111                 case 'z':
6112                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
6113                   break;
6114
6115                 case 'c':
6116                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
6117                   break;
6118
6119                 case 'v':
6120                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
6121                   break;
6122
6123                 case 'q':
6124                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
6125                   break;
6126
6127                 case 'g':
6128                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
6129                   break;
6130
6131                 default:
6132                   inst.error = _("unexpected bit specified after APSR");
6133                   return FAIL;
6134                 }
6135             }
6136
6137           if (nzcvq_bits == 0x1f)
6138             psr_field |= PSR_f;
6139
6140           if (g_bit == 0x1)
6141             {
6142               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
6143                 {
6144                   inst.error = _("selected processor does not "
6145                                  "support DSP extension");
6146                   return FAIL;
6147                 }
6148
6149               psr_field |= PSR_s;
6150             }
6151
6152           if ((nzcvq_bits & 0x20) != 0
6153               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
6154               || (g_bit & 0x2) != 0)
6155             {
6156               inst.error = _("bad bitmask specified after APSR");
6157               return FAIL;
6158             }
6159         }
6160       else
6161         {
6162           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
6163                                                       p - start);
6164           if (!psr)
6165             goto error;
6166
6167           psr_field |= psr->field;
6168         }
6169     }
6170   else
6171     {
6172       if (ISALNUM (*p))
6173         goto error;    /* Garbage after "[CS]PSR".  */
6174
6175       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
6176          is deprecated, but allow it anyway.  */
6177       if (is_apsr && lhs)
6178         {
6179           psr_field |= PSR_f;
6180           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6181                        "deprecated"));
6182         }
6183       else if (!m_profile)
6184         /* These bits are never right for M-profile devices: don't set them
6185            (only code paths which read/write APSR reach here).  */
6186         psr_field |= (PSR_c | PSR_f);
6187     }
6188   *str = p;
6189   return psr_field;
6190
6191  unsupported_psr:
6192   inst.error = _("selected processor does not support requested special "
6193                  "purpose register");
6194   return FAIL;
6195
6196  error:
6197   inst.error = _("flag for {c}psr instruction expected");
6198   return FAIL;
6199 }
6200
6201 static int
6202 parse_sys_vldr_vstr (char **str)
6203 {
6204   unsigned i;
6205   int val = FAIL;
6206   struct {
6207     const char *name;
6208     int regl;
6209     int regh;
6210   } sysregs[] = {
6211     {"FPSCR",           0x1, 0x0},
6212     {"FPSCR_nzcvqc",    0x2, 0x0},
6213     {"VPR",             0x4, 0x1},
6214     {"P0",              0x5, 0x1},
6215     {"FPCXTNS",         0x6, 0x1},
6216     {"FPCXTS",          0x7, 0x1}
6217   };
6218   char *op_end = strchr (*str, ',');
6219   size_t op_strlen = op_end - *str;
6220
6221   for (i = 0; i < sizeof (sysregs) / sizeof (sysregs[0]); i++)
6222     {
6223       if (!strncmp (*str, sysregs[i].name, op_strlen))
6224         {
6225           val = sysregs[i].regl | (sysregs[i].regh << 3);
6226           *str = op_end;
6227           break;
6228         }
6229     }
6230
6231   return val;
6232 }
6233
6234 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6235    value suitable for splatting into the AIF field of the instruction.  */
6236
6237 static int
6238 parse_cps_flags (char **str)
6239 {
6240   int val = 0;
6241   int saw_a_flag = 0;
6242   char *s = *str;
6243
6244   for (;;)
6245     switch (*s++)
6246       {
6247       case '\0': case ',':
6248         goto done;
6249
6250       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6251       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6252       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6253
6254       default:
6255         inst.error = _("unrecognized CPS flag");
6256         return FAIL;
6257       }
6258
6259  done:
6260   if (saw_a_flag == 0)
6261     {
6262       inst.error = _("missing CPS flags");
6263       return FAIL;
6264     }
6265
6266   *str = s - 1;
6267   return val;
6268 }
6269
6270 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6271    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6272
6273 static int
6274 parse_endian_specifier (char **str)
6275 {
6276   int little_endian;
6277   char *s = *str;
6278
6279   if (strncasecmp (s, "BE", 2))
6280     little_endian = 0;
6281   else if (strncasecmp (s, "LE", 2))
6282     little_endian = 1;
6283   else
6284     {
6285       inst.error = _("valid endian specifiers are be or le");
6286       return FAIL;
6287     }
6288
6289   if (ISALNUM (s[2]) || s[2] == '_')
6290     {
6291       inst.error = _("valid endian specifiers are be or le");
6292       return FAIL;
6293     }
6294
6295   *str = s + 2;
6296   return little_endian;
6297 }
6298
6299 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6300    value suitable for poking into the rotate field of an sxt or sxta
6301    instruction, or FAIL on error.  */
6302
6303 static int
6304 parse_ror (char **str)
6305 {
6306   int rot;
6307   char *s = *str;
6308
6309   if (strncasecmp (s, "ROR", 3) == 0)
6310     s += 3;
6311   else
6312     {
6313       inst.error = _("missing rotation field after comma");
6314       return FAIL;
6315     }
6316
6317   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6318     return FAIL;
6319
6320   switch (rot)
6321     {
6322     case  0: *str = s; return 0x0;
6323     case  8: *str = s; return 0x1;
6324     case 16: *str = s; return 0x2;
6325     case 24: *str = s; return 0x3;
6326
6327     default:
6328       inst.error = _("rotation can only be 0, 8, 16, or 24");
6329       return FAIL;
6330     }
6331 }
6332
6333 /* Parse a conditional code (from conds[] below).  The value returned is in the
6334    range 0 .. 14, or FAIL.  */
6335 static int
6336 parse_cond (char **str)
6337 {
6338   char *q;
6339   const struct asm_cond *c;
6340   int n;
6341   /* Condition codes are always 2 characters, so matching up to
6342      3 characters is sufficient.  */
6343   char cond[3];
6344
6345   q = *str;
6346   n = 0;
6347   while (ISALPHA (*q) && n < 3)
6348     {
6349       cond[n] = TOLOWER (*q);
6350       q++;
6351       n++;
6352     }
6353
6354   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6355   if (!c)
6356     {
6357       inst.error = _("condition required");
6358       return FAIL;
6359     }
6360
6361   *str = q;
6362   return c->value;
6363 }
6364
6365 /* Parse an option for a barrier instruction.  Returns the encoding for the
6366    option, or FAIL.  */
6367 static int
6368 parse_barrier (char **str)
6369 {
6370   char *p, *q;
6371   const struct asm_barrier_opt *o;
6372
6373   p = q = *str;
6374   while (ISALPHA (*q))
6375     q++;
6376
6377   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6378                                                     q - p);
6379   if (!o)
6380     return FAIL;
6381
6382   if (!mark_feature_used (&o->arch))
6383     return FAIL;
6384
6385   *str = q;
6386   return o->value;
6387 }
6388
6389 /* Parse the operands of a table branch instruction.  Similar to a memory
6390    operand.  */
6391 static int
6392 parse_tb (char **str)
6393 {
6394   char * p = *str;
6395   int reg;
6396
6397   if (skip_past_char (&p, '[') == FAIL)
6398     {
6399       inst.error = _("'[' expected");
6400       return FAIL;
6401     }
6402
6403   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6404     {
6405       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6406       return FAIL;
6407     }
6408   inst.operands[0].reg = reg;
6409
6410   if (skip_past_comma (&p) == FAIL)
6411     {
6412       inst.error = _("',' expected");
6413       return FAIL;
6414     }
6415
6416   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6417     {
6418       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6419       return FAIL;
6420     }
6421   inst.operands[0].imm = reg;
6422
6423   if (skip_past_comma (&p) == SUCCESS)
6424     {
6425       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6426         return FAIL;
6427       if (inst.relocs[0].exp.X_add_number != 1)
6428         {
6429           inst.error = _("invalid shift");
6430           return FAIL;
6431         }
6432       inst.operands[0].shifted = 1;
6433     }
6434
6435   if (skip_past_char (&p, ']') == FAIL)
6436     {
6437       inst.error = _("']' expected");
6438       return FAIL;
6439     }
6440   *str = p;
6441   return SUCCESS;
6442 }
6443
6444 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6445    information on the types the operands can take and how they are encoded.
6446    Up to four operands may be read; this function handles setting the
6447    ".present" field for each read operand itself.
6448    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6449    else returns FAIL.  */
6450
6451 static int
6452 parse_neon_mov (char **str, int *which_operand)
6453 {
6454   int i = *which_operand, val;
6455   enum arm_reg_type rtype;
6456   char *ptr = *str;
6457   struct neon_type_el optype;
6458
6459   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6460     {
6461       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6462       inst.operands[i].reg = val;
6463       inst.operands[i].isscalar = 1;
6464       inst.operands[i].vectype = optype;
6465       inst.operands[i++].present = 1;
6466
6467       if (skip_past_comma (&ptr) == FAIL)
6468         goto wanted_comma;
6469
6470       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6471         goto wanted_arm;
6472
6473       inst.operands[i].reg = val;
6474       inst.operands[i].isreg = 1;
6475       inst.operands[i].present = 1;
6476     }
6477   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6478            != FAIL)
6479     {
6480       /* Cases 0, 1, 2, 3, 5 (D only).  */
6481       if (skip_past_comma (&ptr) == FAIL)
6482         goto wanted_comma;
6483
6484       inst.operands[i].reg = val;
6485       inst.operands[i].isreg = 1;
6486       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6487       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6488       inst.operands[i].isvec = 1;
6489       inst.operands[i].vectype = optype;
6490       inst.operands[i++].present = 1;
6491
6492       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6493         {
6494           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6495              Case 13: VMOV <Sd>, <Rm>  */
6496           inst.operands[i].reg = val;
6497           inst.operands[i].isreg = 1;
6498           inst.operands[i].present = 1;
6499
6500           if (rtype == REG_TYPE_NQ)
6501             {
6502               first_error (_("can't use Neon quad register here"));
6503               return FAIL;
6504             }
6505           else if (rtype != REG_TYPE_VFS)
6506             {
6507               i++;
6508               if (skip_past_comma (&ptr) == FAIL)
6509                 goto wanted_comma;
6510               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6511                 goto wanted_arm;
6512               inst.operands[i].reg = val;
6513               inst.operands[i].isreg = 1;
6514               inst.operands[i].present = 1;
6515             }
6516         }
6517       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6518                                            &optype)) != FAIL)
6519         {
6520           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6521              Case 1: VMOV<c><q> <Dd>, <Dm>
6522              Case 8: VMOV.F32 <Sd>, <Sm>
6523              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6524
6525           inst.operands[i].reg = val;
6526           inst.operands[i].isreg = 1;
6527           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6528           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6529           inst.operands[i].isvec = 1;
6530           inst.operands[i].vectype = optype;
6531           inst.operands[i].present = 1;
6532
6533           if (skip_past_comma (&ptr) == SUCCESS)
6534             {
6535               /* Case 15.  */
6536               i++;
6537
6538               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6539                 goto wanted_arm;
6540
6541               inst.operands[i].reg = val;
6542               inst.operands[i].isreg = 1;
6543               inst.operands[i++].present = 1;
6544
6545               if (skip_past_comma (&ptr) == FAIL)
6546                 goto wanted_comma;
6547
6548               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6549                 goto wanted_arm;
6550
6551               inst.operands[i].reg = val;
6552               inst.operands[i].isreg = 1;
6553               inst.operands[i].present = 1;
6554             }
6555         }
6556       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6557           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6558              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6559              Case 10: VMOV.F32 <Sd>, #<imm>
6560              Case 11: VMOV.F64 <Dd>, #<imm>  */
6561         inst.operands[i].immisfloat = 1;
6562       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6563                == SUCCESS)
6564           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6565              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6566         ;
6567       else
6568         {
6569           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6570           return FAIL;
6571         }
6572     }
6573   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6574     {
6575       /* Cases 6, 7.  */
6576       inst.operands[i].reg = val;
6577       inst.operands[i].isreg = 1;
6578       inst.operands[i++].present = 1;
6579
6580       if (skip_past_comma (&ptr) == FAIL)
6581         goto wanted_comma;
6582
6583       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6584         {
6585           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6586           inst.operands[i].reg = val;
6587           inst.operands[i].isscalar = 1;
6588           inst.operands[i].present = 1;
6589           inst.operands[i].vectype = optype;
6590         }
6591       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6592         {
6593           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6594           inst.operands[i].reg = val;
6595           inst.operands[i].isreg = 1;
6596           inst.operands[i++].present = 1;
6597
6598           if (skip_past_comma (&ptr) == FAIL)
6599             goto wanted_comma;
6600
6601           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6602               == FAIL)
6603             {
6604               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6605               return FAIL;
6606             }
6607
6608           inst.operands[i].reg = val;
6609           inst.operands[i].isreg = 1;
6610           inst.operands[i].isvec = 1;
6611           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6612           inst.operands[i].vectype = optype;
6613           inst.operands[i].present = 1;
6614
6615           if (rtype == REG_TYPE_VFS)
6616             {
6617               /* Case 14.  */
6618               i++;
6619               if (skip_past_comma (&ptr) == FAIL)
6620                 goto wanted_comma;
6621               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6622                                               &optype)) == FAIL)
6623                 {
6624                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6625                   return FAIL;
6626                 }
6627               inst.operands[i].reg = val;
6628               inst.operands[i].isreg = 1;
6629               inst.operands[i].isvec = 1;
6630               inst.operands[i].issingle = 1;
6631               inst.operands[i].vectype = optype;
6632               inst.operands[i].present = 1;
6633             }
6634         }
6635       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6636                != FAIL)
6637         {
6638           /* Case 13.  */
6639           inst.operands[i].reg = val;
6640           inst.operands[i].isreg = 1;
6641           inst.operands[i].isvec = 1;
6642           inst.operands[i].issingle = 1;
6643           inst.operands[i].vectype = optype;
6644           inst.operands[i].present = 1;
6645         }
6646     }
6647   else
6648     {
6649       first_error (_("parse error"));
6650       return FAIL;
6651     }
6652
6653   /* Successfully parsed the operands. Update args.  */
6654   *which_operand = i;
6655   *str = ptr;
6656   return SUCCESS;
6657
6658  wanted_comma:
6659   first_error (_("expected comma"));
6660   return FAIL;
6661
6662  wanted_arm:
6663   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6664   return FAIL;
6665 }
6666
6667 /* Use this macro when the operand constraints are different
6668    for ARM and THUMB (e.g. ldrd).  */
6669 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6670         ((arm_operand) | ((thumb_operand) << 16))
6671
6672 /* Matcher codes for parse_operands.  */
6673 enum operand_parse_code
6674 {
6675   OP_stop,      /* end of line */
6676
6677   OP_RR,        /* ARM register */
6678   OP_RRnpc,     /* ARM register, not r15 */
6679   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6680   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6681   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6682                    optional trailing ! */
6683   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6684   OP_RCP,       /* Coprocessor number */
6685   OP_RCN,       /* Coprocessor register */
6686   OP_RF,        /* FPA register */
6687   OP_RVS,       /* VFP single precision register */
6688   OP_RVD,       /* VFP double precision register (0..15) */
6689   OP_RND,       /* Neon double precision register (0..31) */
6690   OP_RNDMQ,     /* Neon double precision (0..31) or MVE vector register.  */
6691   OP_RNDMQR,    /* Neon double precision (0..31), MVE vector or ARM register.
6692                  */
6693   OP_RNQ,       /* Neon quad precision register */
6694   OP_RNQMQ,     /* Neon quad or MVE vector register.  */
6695   OP_RVSD,      /* VFP single or double precision register */
6696   OP_RNSD,      /* Neon single or double precision register */
6697   OP_RNDQ,      /* Neon double or quad precision register */
6698   OP_RNDQMQ,     /* Neon double, quad or MVE vector register.  */
6699   OP_RNSDQ,     /* Neon single, double or quad precision register */
6700   OP_RNSC,      /* Neon scalar D[X] */
6701   OP_RVC,       /* VFP control register */
6702   OP_RMF,       /* Maverick F register */
6703   OP_RMD,       /* Maverick D register */
6704   OP_RMFX,      /* Maverick FX register */
6705   OP_RMDX,      /* Maverick DX register */
6706   OP_RMAX,      /* Maverick AX register */
6707   OP_RMDS,      /* Maverick DSPSC register */
6708   OP_RIWR,      /* iWMMXt wR register */
6709   OP_RIWC,      /* iWMMXt wC register */
6710   OP_RIWG,      /* iWMMXt wCG register */
6711   OP_RXA,       /* XScale accumulator register */
6712
6713   OP_RNSDQMQ,   /* Neon single, double or quad register or MVE vector register
6714                  */
6715   OP_RNSDQMQR,  /* Neon single, double or quad register, MVE vector register or
6716                    GPR (no SP/SP)  */
6717   /* New operands for Armv8.1-M Mainline.  */
6718   OP_LR,        /* ARM LR register */
6719   OP_RRnpcsp_I32, /* ARM register (no BadReg) or literal 1 .. 32 */
6720
6721   OP_REGLST,    /* ARM register list */
6722   OP_CLRMLST,   /* CLRM register list */
6723   OP_VRSLST,    /* VFP single-precision register list */
6724   OP_VRDLST,    /* VFP double-precision register list */
6725   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6726   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6727   OP_NSTRLST,   /* Neon element/structure list */
6728   OP_VRSDVLST,  /* VFP single or double-precision register list and VPR */
6729
6730   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6731   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6732   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6733   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6734   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
6735   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6736   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6737   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6738   OP_VMOV,      /* Neon VMOV operands.  */
6739   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6740   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6741   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6742   OP_VLDR,      /* VLDR operand.  */
6743
6744   OP_I0,        /* immediate zero */
6745   OP_I7,        /* immediate value 0 .. 7 */
6746   OP_I15,       /*                 0 .. 15 */
6747   OP_I16,       /*                 1 .. 16 */
6748   OP_I16z,      /*                 0 .. 16 */
6749   OP_I31,       /*                 0 .. 31 */
6750   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6751   OP_I32,       /*                 1 .. 32 */
6752   OP_I32z,      /*                 0 .. 32 */
6753   OP_I63,       /*                 0 .. 63 */
6754   OP_I63s,      /*               -64 .. 63 */
6755   OP_I64,       /*                 1 .. 64 */
6756   OP_I64z,      /*                 0 .. 64 */
6757   OP_I255,      /*                 0 .. 255 */
6758
6759   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6760   OP_I7b,       /*                             0 .. 7 */
6761   OP_I15b,      /*                             0 .. 15 */
6762   OP_I31b,      /*                             0 .. 31 */
6763
6764   OP_SH,        /* shifter operand */
6765   OP_SHG,       /* shifter operand with possible group relocation */
6766   OP_ADDR,      /* Memory address expression (any mode) */
6767   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6768   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6769   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6770   OP_EXP,       /* arbitrary expression */
6771   OP_EXPi,      /* same, with optional immediate prefix */
6772   OP_EXPr,      /* same, with optional relocation suffix */
6773   OP_EXPs,      /* same, with optional non-first operand relocation suffix */
6774   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6775   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
6776   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
6777
6778   OP_CPSF,      /* CPS flags */
6779   OP_ENDI,      /* Endianness specifier */
6780   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6781   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6782   OP_COND,      /* conditional code */
6783   OP_TB,        /* Table branch.  */
6784
6785   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6786
6787   OP_RRnpc_I0,  /* ARM register or literal 0 */
6788   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
6789   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6790   OP_RF_IF,     /* FPA register or immediate */
6791   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6792   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6793
6794   /* Optional operands.  */
6795   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6796   OP_oI31b,      /*                             0 .. 31 */
6797   OP_oI32b,      /*                             1 .. 32 */
6798   OP_oI32z,      /*                             0 .. 32 */
6799   OP_oIffffb,    /*                             0 .. 65535 */
6800   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6801
6802   OP_oRR,        /* ARM register */
6803   OP_oLR,        /* ARM LR register */
6804   OP_oRRnpc,     /* ARM register, not the PC */
6805   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6806   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6807   OP_oRND,       /* Optional Neon double precision register */
6808   OP_oRNQ,       /* Optional Neon quad precision register */
6809   OP_oRNDQMQ,     /* Optional Neon double, quad or MVE vector register.  */
6810   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6811   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6812   OP_oRNSDQMQ,   /* Optional single, double or quad register or MVE vector
6813                     register.  */
6814   OP_oSHll,      /* LSL immediate */
6815   OP_oSHar,      /* ASR immediate */
6816   OP_oSHllar,    /* LSL or ASR immediate */
6817   OP_oROR,       /* ROR 0/8/16/24 */
6818   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6819
6820   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6821   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6822   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6823   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6824
6825   OP_FIRST_OPTIONAL = OP_oI7b
6826 };
6827
6828 /* Generic instruction operand parser.  This does no encoding and no
6829    semantic validation; it merely squirrels values away in the inst
6830    structure.  Returns SUCCESS or FAIL depending on whether the
6831    specified grammar matched.  */
6832 static int
6833 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6834 {
6835   unsigned const int *upat = pattern;
6836   char *backtrack_pos = 0;
6837   const char *backtrack_error = 0;
6838   int i, val = 0, backtrack_index = 0;
6839   enum arm_reg_type rtype;
6840   parse_operand_result result;
6841   unsigned int op_parse_code;
6842   bfd_boolean partial_match;
6843
6844 #define po_char_or_fail(chr)                    \
6845   do                                            \
6846     {                                           \
6847       if (skip_past_char (&str, chr) == FAIL)   \
6848         goto bad_args;                          \
6849     }                                           \
6850   while (0)
6851
6852 #define po_reg_or_fail(regtype)                                 \
6853   do                                                            \
6854     {                                                           \
6855       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6856                                  & inst.operands[i].vectype);   \
6857       if (val == FAIL)                                          \
6858         {                                                       \
6859           first_error (_(reg_expected_msgs[regtype]));          \
6860           goto failure;                                         \
6861         }                                                       \
6862       inst.operands[i].reg = val;                               \
6863       inst.operands[i].isreg = 1;                               \
6864       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6865       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6866       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6867                              || rtype == REG_TYPE_VFD           \
6868                              || rtype == REG_TYPE_NQ);          \
6869     }                                                           \
6870   while (0)
6871
6872 #define po_reg_or_goto(regtype, label)                          \
6873   do                                                            \
6874     {                                                           \
6875       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6876                                  & inst.operands[i].vectype);   \
6877       if (val == FAIL)                                          \
6878         goto label;                                             \
6879                                                                 \
6880       inst.operands[i].reg = val;                               \
6881       inst.operands[i].isreg = 1;                               \
6882       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6883       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6884       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6885                              || rtype == REG_TYPE_VFD           \
6886                              || rtype == REG_TYPE_NQ);          \
6887     }                                                           \
6888   while (0)
6889
6890 #define po_imm_or_fail(min, max, popt)                          \
6891   do                                                            \
6892     {                                                           \
6893       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6894         goto failure;                                           \
6895       inst.operands[i].imm = val;                               \
6896     }                                                           \
6897   while (0)
6898
6899 #define po_scalar_or_goto(elsz, label)                                  \
6900   do                                                                    \
6901     {                                                                   \
6902       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6903       if (val == FAIL)                                                  \
6904         goto label;                                                     \
6905       inst.operands[i].reg = val;                                       \
6906       inst.operands[i].isscalar = 1;                                    \
6907     }                                                                   \
6908   while (0)
6909
6910 #define po_misc_or_fail(expr)                   \
6911   do                                            \
6912     {                                           \
6913       if (expr)                                 \
6914         goto failure;                           \
6915     }                                           \
6916   while (0)
6917
6918 #define po_misc_or_fail_no_backtrack(expr)              \
6919   do                                                    \
6920     {                                                   \
6921       result = expr;                                    \
6922       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6923         backtrack_pos = 0;                              \
6924       if (result != PARSE_OPERAND_SUCCESS)              \
6925         goto failure;                                   \
6926     }                                                   \
6927   while (0)
6928
6929 #define po_barrier_or_imm(str)                             \
6930   do                                                       \
6931     {                                                      \
6932       val = parse_barrier (&str);                          \
6933       if (val == FAIL && ! ISALPHA (*str))                 \
6934         goto immediate;                                    \
6935       if (val == FAIL                                      \
6936           /* ISB can only take SY as an option.  */        \
6937           || ((inst.instruction & 0xf0) == 0x60            \
6938                && val != 0xf))                             \
6939         {                                                  \
6940            inst.error = _("invalid barrier type");         \
6941            backtrack_pos = 0;                              \
6942            goto failure;                                   \
6943         }                                                  \
6944     }                                                      \
6945   while (0)
6946
6947   skip_whitespace (str);
6948
6949   for (i = 0; upat[i] != OP_stop; i++)
6950     {
6951       op_parse_code = upat[i];
6952       if (op_parse_code >= 1<<16)
6953         op_parse_code = thumb ? (op_parse_code >> 16)
6954                                 : (op_parse_code & ((1<<16)-1));
6955
6956       if (op_parse_code >= OP_FIRST_OPTIONAL)
6957         {
6958           /* Remember where we are in case we need to backtrack.  */
6959           gas_assert (!backtrack_pos);
6960           backtrack_pos = str;
6961           backtrack_error = inst.error;
6962           backtrack_index = i;
6963         }
6964
6965       if (i > 0 && (i > 1 || inst.operands[0].present))
6966         po_char_or_fail (',');
6967
6968       switch (op_parse_code)
6969         {
6970           /* Registers */
6971         case OP_oRRnpc:
6972         case OP_oRRnpcsp:
6973         case OP_RRnpc:
6974         case OP_RRnpcsp:
6975         case OP_oRR:
6976         case OP_LR:
6977         case OP_oLR:
6978         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6979         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6980         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6981         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6982         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6983         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6984         case OP_oRND:
6985         case OP_RNDMQR:
6986           po_reg_or_goto (REG_TYPE_RN, try_rndmq);
6987           break;
6988         try_rndmq:
6989         case OP_RNDMQ:
6990           po_reg_or_goto (REG_TYPE_MQ, try_rnd);
6991           break;
6992         try_rnd:
6993         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6994         case OP_RVC:
6995           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6996           break;
6997           /* Also accept generic coprocessor regs for unknown registers.  */
6998           coproc_reg:
6999           po_reg_or_fail (REG_TYPE_CN);
7000           break;
7001         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
7002         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
7003         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
7004         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
7005         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
7006         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
7007         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
7008         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
7009         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
7010         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
7011         case OP_oRNQ:
7012         case OP_RNQMQ:
7013           po_reg_or_goto (REG_TYPE_MQ, try_nq);
7014           break;
7015         try_nq:
7016         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
7017         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
7018         case OP_oRNDQMQ:
7019         case OP_RNDQMQ:
7020           po_reg_or_goto (REG_TYPE_MQ, try_rndq);
7021           break;
7022         try_rndq:
7023         case OP_oRNDQ:
7024         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
7025         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
7026         case OP_oRNSDQ:
7027         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
7028         case OP_RNSDQMQR:
7029           po_reg_or_goto (REG_TYPE_RN, try_mq);
7030           break;
7031           try_mq:
7032         case OP_oRNSDQMQ:
7033         case OP_RNSDQMQ:
7034           po_reg_or_goto (REG_TYPE_MQ, try_nsdq2);
7035           break;
7036           try_nsdq2:
7037           po_reg_or_fail (REG_TYPE_NSDQ);
7038           inst.error = 0;
7039           break;
7040         /* Neon scalar. Using an element size of 8 means that some invalid
7041            scalars are accepted here, so deal with those in later code.  */
7042         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
7043
7044         case OP_RNDQ_I0:
7045           {
7046             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
7047             break;
7048             try_imm0:
7049             po_imm_or_fail (0, 0, TRUE);
7050           }
7051           break;
7052
7053         case OP_RVSD_I0:
7054           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
7055           break;
7056
7057         case OP_RSVD_FI0:
7058           {
7059             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
7060             break;
7061             try_ifimm0:
7062             if (parse_ifimm_zero (&str))
7063               inst.operands[i].imm = 0;
7064             else
7065             {
7066               inst.error
7067                 = _("only floating point zero is allowed as immediate value");
7068               goto failure;
7069             }
7070           }
7071           break;
7072
7073         case OP_RR_RNSC:
7074           {
7075             po_scalar_or_goto (8, try_rr);
7076             break;
7077             try_rr:
7078             po_reg_or_fail (REG_TYPE_RN);
7079           }
7080           break;
7081
7082         case OP_RNSDQ_RNSC:
7083           {
7084             po_scalar_or_goto (8, try_nsdq);
7085             break;
7086             try_nsdq:
7087             po_reg_or_fail (REG_TYPE_NSDQ);
7088           }
7089           break;
7090
7091         case OP_RNSD_RNSC:
7092           {
7093             po_scalar_or_goto (8, try_s_scalar);
7094             break;
7095             try_s_scalar:
7096             po_scalar_or_goto (4, try_nsd);
7097             break;
7098             try_nsd:
7099             po_reg_or_fail (REG_TYPE_NSD);
7100           }
7101           break;
7102
7103         case OP_RNDQ_RNSC:
7104           {
7105             po_scalar_or_goto (8, try_ndq);
7106             break;
7107             try_ndq:
7108             po_reg_or_fail (REG_TYPE_NDQ);
7109           }
7110           break;
7111
7112         case OP_RND_RNSC:
7113           {
7114             po_scalar_or_goto (8, try_vfd);
7115             break;
7116             try_vfd:
7117             po_reg_or_fail (REG_TYPE_VFD);
7118           }
7119           break;
7120
7121         case OP_VMOV:
7122           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
7123              not careful then bad things might happen.  */
7124           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
7125           break;
7126
7127         case OP_RNDQ_Ibig:
7128           {
7129             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
7130             break;
7131             try_immbig:
7132             /* There's a possibility of getting a 64-bit immediate here, so
7133                we need special handling.  */
7134             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
7135                 == FAIL)
7136               {
7137                 inst.error = _("immediate value is out of range");
7138                 goto failure;
7139               }
7140           }
7141           break;
7142
7143         case OP_RNDQ_I63b:
7144           {
7145             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
7146             break;
7147             try_shimm:
7148             po_imm_or_fail (0, 63, TRUE);
7149           }
7150           break;
7151
7152         case OP_RRnpcb:
7153           po_char_or_fail ('[');
7154           po_reg_or_fail  (REG_TYPE_RN);
7155           po_char_or_fail (']');
7156           break;
7157
7158         case OP_RRnpctw:
7159         case OP_RRw:
7160         case OP_oRRw:
7161           po_reg_or_fail (REG_TYPE_RN);
7162           if (skip_past_char (&str, '!') == SUCCESS)
7163             inst.operands[i].writeback = 1;
7164           break;
7165
7166           /* Immediates */
7167         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
7168         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
7169         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
7170         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
7171         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
7172         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
7173         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
7174         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
7175         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
7176         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
7177         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
7178         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
7179
7180         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
7181         case OP_oI7b:
7182         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
7183         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
7184         case OP_oI31b:
7185         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
7186         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
7187         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
7188         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
7189
7190           /* Immediate variants */
7191         case OP_oI255c:
7192           po_char_or_fail ('{');
7193           po_imm_or_fail (0, 255, TRUE);
7194           po_char_or_fail ('}');
7195           break;
7196
7197         case OP_I31w:
7198           /* The expression parser chokes on a trailing !, so we have
7199              to find it first and zap it.  */
7200           {
7201             char *s = str;
7202             while (*s && *s != ',')
7203               s++;
7204             if (s[-1] == '!')
7205               {
7206                 s[-1] = '\0';
7207                 inst.operands[i].writeback = 1;
7208               }
7209             po_imm_or_fail (0, 31, TRUE);
7210             if (str == s - 1)
7211               str = s;
7212           }
7213           break;
7214
7215           /* Expressions */
7216         case OP_EXPi:   EXPi:
7217           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7218                                               GE_OPT_PREFIX));
7219           break;
7220
7221         case OP_EXP:
7222           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7223                                               GE_NO_PREFIX));
7224           break;
7225
7226         case OP_EXPr:   EXPr:
7227           po_misc_or_fail (my_get_expression (&inst.relocs[0].exp, &str,
7228                                               GE_NO_PREFIX));
7229           if (inst.relocs[0].exp.X_op == O_symbol)
7230             {
7231               val = parse_reloc (&str);
7232               if (val == -1)
7233                 {
7234                   inst.error = _("unrecognized relocation suffix");
7235                   goto failure;
7236                 }
7237               else if (val != BFD_RELOC_UNUSED)
7238                 {
7239                   inst.operands[i].imm = val;
7240                   inst.operands[i].hasreloc = 1;
7241                 }
7242             }
7243           break;
7244
7245         case OP_EXPs:
7246           po_misc_or_fail (my_get_expression (&inst.relocs[i].exp, &str,
7247                                               GE_NO_PREFIX));
7248           if (inst.relocs[i].exp.X_op == O_symbol)
7249             {
7250               inst.operands[i].hasreloc = 1;
7251             }
7252           else if (inst.relocs[i].exp.X_op == O_constant)
7253             {
7254               inst.operands[i].imm = inst.relocs[i].exp.X_add_number;
7255               inst.operands[i].hasreloc = 0;
7256             }
7257           break;
7258
7259           /* Operand for MOVW or MOVT.  */
7260         case OP_HALF:
7261           po_misc_or_fail (parse_half (&str));
7262           break;
7263
7264           /* Register or expression.  */
7265         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7266         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7267
7268           /* Register or immediate.  */
7269         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7270         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7271
7272         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7273         IF:
7274           if (!is_immediate_prefix (*str))
7275             goto bad_args;
7276           str++;
7277           val = parse_fpa_immediate (&str);
7278           if (val == FAIL)
7279             goto failure;
7280           /* FPA immediates are encoded as registers 8-15.
7281              parse_fpa_immediate has already applied the offset.  */
7282           inst.operands[i].reg = val;
7283           inst.operands[i].isreg = 1;
7284           break;
7285
7286         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7287         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7288
7289           /* Two kinds of register.  */
7290         case OP_RIWR_RIWC:
7291           {
7292             struct reg_entry *rege = arm_reg_parse_multi (&str);
7293             if (!rege
7294                 || (rege->type != REG_TYPE_MMXWR
7295                     && rege->type != REG_TYPE_MMXWC
7296                     && rege->type != REG_TYPE_MMXWCG))
7297               {
7298                 inst.error = _("iWMMXt data or control register expected");
7299                 goto failure;
7300               }
7301             inst.operands[i].reg = rege->number;
7302             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7303           }
7304           break;
7305
7306         case OP_RIWC_RIWG:
7307           {
7308             struct reg_entry *rege = arm_reg_parse_multi (&str);
7309             if (!rege
7310                 || (rege->type != REG_TYPE_MMXWC
7311                     && rege->type != REG_TYPE_MMXWCG))
7312               {
7313                 inst.error = _("iWMMXt control register expected");
7314                 goto failure;
7315               }
7316             inst.operands[i].reg = rege->number;
7317             inst.operands[i].isreg = 1;
7318           }
7319           break;
7320
7321           /* Misc */
7322         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7323         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7324         case OP_oROR:    val = parse_ror (&str);                break;
7325         case OP_COND:    val = parse_cond (&str);               break;
7326         case OP_oBARRIER_I15:
7327           po_barrier_or_imm (str); break;
7328           immediate:
7329           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7330             goto failure;
7331           break;
7332
7333         case OP_wPSR:
7334         case OP_rPSR:
7335           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7336           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7337             {
7338               inst.error = _("Banked registers are not available with this "
7339                              "architecture.");
7340               goto failure;
7341             }
7342           break;
7343           try_psr:
7344           val = parse_psr (&str, op_parse_code == OP_wPSR);
7345           break;
7346
7347         case OP_VLDR:
7348           po_reg_or_goto (REG_TYPE_VFSD, try_sysreg);
7349           break;
7350         try_sysreg:
7351           val = parse_sys_vldr_vstr (&str);
7352           break;
7353
7354         case OP_APSR_RR:
7355           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7356           break;
7357           try_apsr:
7358           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7359              instruction).  */
7360           if (strncasecmp (str, "APSR_", 5) == 0)
7361             {
7362               unsigned found = 0;
7363               str += 5;
7364               while (found < 15)
7365                 switch (*str++)
7366                   {
7367                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7368                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7369                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7370                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7371                   default: found = 16;
7372                   }
7373               if (found != 15)
7374                 goto failure;
7375               inst.operands[i].isvec = 1;
7376               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7377               inst.operands[i].reg = REG_PC;
7378             }
7379           else
7380             goto failure;
7381           break;
7382
7383         case OP_TB:
7384           po_misc_or_fail (parse_tb (&str));
7385           break;
7386
7387           /* Register lists.  */
7388         case OP_REGLST:
7389           val = parse_reg_list (&str, REGLIST_RN);
7390           if (*str == '^')
7391             {
7392               inst.operands[i].writeback = 1;
7393               str++;
7394             }
7395           break;
7396
7397         case OP_CLRMLST:
7398           val = parse_reg_list (&str, REGLIST_CLRM);
7399           break;
7400
7401         case OP_VRSLST:
7402           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S,
7403                                     &partial_match);
7404           break;
7405
7406         case OP_VRDLST:
7407           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D,
7408                                     &partial_match);
7409           break;
7410
7411         case OP_VRSDLST:
7412           /* Allow Q registers too.  */
7413           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7414                                     REGLIST_NEON_D, &partial_match);
7415           if (val == FAIL)
7416             {
7417               inst.error = NULL;
7418               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7419                                         REGLIST_VFP_S, &partial_match);
7420               inst.operands[i].issingle = 1;
7421             }
7422           break;
7423
7424         case OP_VRSDVLST:
7425           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7426                                     REGLIST_VFP_D_VPR, &partial_match);
7427           if (val == FAIL && !partial_match)
7428             {
7429               inst.error = NULL;
7430               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7431                                         REGLIST_VFP_S_VPR, &partial_match);
7432               inst.operands[i].issingle = 1;
7433             }
7434           break;
7435
7436         case OP_NRDLST:
7437           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7438                                     REGLIST_NEON_D, &partial_match);
7439           break;
7440
7441         case OP_NSTRLST:
7442           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7443                                            &inst.operands[i].vectype);
7444           break;
7445
7446           /* Addressing modes */
7447         case OP_ADDR:
7448           po_misc_or_fail (parse_address (&str, i));
7449           break;
7450
7451         case OP_ADDRGLDR:
7452           po_misc_or_fail_no_backtrack (
7453             parse_address_group_reloc (&str, i, GROUP_LDR));
7454           break;
7455
7456         case OP_ADDRGLDRS:
7457           po_misc_or_fail_no_backtrack (
7458             parse_address_group_reloc (&str, i, GROUP_LDRS));
7459           break;
7460
7461         case OP_ADDRGLDC:
7462           po_misc_or_fail_no_backtrack (
7463             parse_address_group_reloc (&str, i, GROUP_LDC));
7464           break;
7465
7466         case OP_SH:
7467           po_misc_or_fail (parse_shifter_operand (&str, i));
7468           break;
7469
7470         case OP_SHG:
7471           po_misc_or_fail_no_backtrack (
7472             parse_shifter_operand_group_reloc (&str, i));
7473           break;
7474
7475         case OP_oSHll:
7476           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7477           break;
7478
7479         case OP_oSHar:
7480           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7481           break;
7482
7483         case OP_oSHllar:
7484           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7485           break;
7486
7487         default:
7488           as_fatal (_("unhandled operand code %d"), op_parse_code);
7489         }
7490
7491       /* Various value-based sanity checks and shared operations.  We
7492          do not signal immediate failures for the register constraints;
7493          this allows a syntax error to take precedence.  */
7494       switch (op_parse_code)
7495         {
7496         case OP_oRRnpc:
7497         case OP_RRnpc:
7498         case OP_RRnpcb:
7499         case OP_RRw:
7500         case OP_oRRw:
7501         case OP_RRnpc_I0:
7502           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7503             inst.error = BAD_PC;
7504           break;
7505
7506         case OP_oRRnpcsp:
7507         case OP_RRnpcsp:
7508           if (inst.operands[i].isreg)
7509             {
7510               if (inst.operands[i].reg == REG_PC)
7511                 inst.error = BAD_PC;
7512               else if (inst.operands[i].reg == REG_SP
7513                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7514                           relaxed since ARMv8-A.  */
7515                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7516                 {
7517                   gas_assert (thumb);
7518                   inst.error = BAD_SP;
7519                 }
7520             }
7521           break;
7522
7523         case OP_RRnpctw:
7524           if (inst.operands[i].isreg
7525               && inst.operands[i].reg == REG_PC
7526               && (inst.operands[i].writeback || thumb))
7527             inst.error = BAD_PC;
7528           break;
7529
7530         case OP_VLDR:
7531           if (inst.operands[i].isreg)
7532             break;
7533         /* fall through.  */
7534         case OP_CPSF:
7535         case OP_ENDI:
7536         case OP_oROR:
7537         case OP_wPSR:
7538         case OP_rPSR:
7539         case OP_COND:
7540         case OP_oBARRIER_I15:
7541         case OP_REGLST:
7542         case OP_CLRMLST:
7543         case OP_VRSLST:
7544         case OP_VRDLST:
7545         case OP_VRSDLST:
7546         case OP_VRSDVLST:
7547         case OP_NRDLST:
7548         case OP_NSTRLST:
7549           if (val == FAIL)
7550             goto failure;
7551           inst.operands[i].imm = val;
7552           break;
7553
7554         case OP_LR:
7555         case OP_oLR:
7556           if (inst.operands[i].reg != REG_LR)
7557             inst.error = _("operand must be LR register");
7558           break;
7559
7560         default:
7561           break;
7562         }
7563
7564       /* If we get here, this operand was successfully parsed.  */
7565       inst.operands[i].present = 1;
7566       continue;
7567
7568     bad_args:
7569       inst.error = BAD_ARGS;
7570
7571     failure:
7572       if (!backtrack_pos)
7573         {
7574           /* The parse routine should already have set inst.error, but set a
7575              default here just in case.  */
7576           if (!inst.error)
7577             inst.error = BAD_SYNTAX;
7578           return FAIL;
7579         }
7580
7581       /* Do not backtrack over a trailing optional argument that
7582          absorbed some text.  We will only fail again, with the
7583          'garbage following instruction' error message, which is
7584          probably less helpful than the current one.  */
7585       if (backtrack_index == i && backtrack_pos != str
7586           && upat[i+1] == OP_stop)
7587         {
7588           if (!inst.error)
7589             inst.error = BAD_SYNTAX;
7590           return FAIL;
7591         }
7592
7593       /* Try again, skipping the optional argument at backtrack_pos.  */
7594       str = backtrack_pos;
7595       inst.error = backtrack_error;
7596       inst.operands[backtrack_index].present = 0;
7597       i = backtrack_index;
7598       backtrack_pos = 0;
7599     }
7600
7601   /* Check that we have parsed all the arguments.  */
7602   if (*str != '\0' && !inst.error)
7603     inst.error = _("garbage following instruction");
7604
7605   return inst.error ? FAIL : SUCCESS;
7606 }
7607
7608 #undef po_char_or_fail
7609 #undef po_reg_or_fail
7610 #undef po_reg_or_goto
7611 #undef po_imm_or_fail
7612 #undef po_scalar_or_fail
7613 #undef po_barrier_or_imm
7614
7615 /* Shorthand macro for instruction encoding functions issuing errors.  */
7616 #define constraint(expr, err)                   \
7617   do                                            \
7618     {                                           \
7619       if (expr)                                 \
7620         {                                       \
7621           inst.error = err;                     \
7622           return;                               \
7623         }                                       \
7624     }                                           \
7625   while (0)
7626
7627 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7628    instructions are unpredictable if these registers are used.  This
7629    is the BadReg predicate in ARM's Thumb-2 documentation.
7630
7631    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
7632    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
7633 #define reject_bad_reg(reg)                                     \
7634   do                                                            \
7635    if (reg == REG_PC)                                           \
7636      {                                                          \
7637        inst.error = BAD_PC;                                     \
7638        return;                                                  \
7639      }                                                          \
7640    else if (reg == REG_SP                                       \
7641             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
7642      {                                                          \
7643        inst.error = BAD_SP;                                     \
7644        return;                                                  \
7645      }                                                          \
7646   while (0)
7647
7648 /* If REG is R13 (the stack pointer), warn that its use is
7649    deprecated.  */
7650 #define warn_deprecated_sp(reg)                 \
7651   do                                            \
7652     if (warn_on_deprecated && reg == REG_SP)    \
7653        as_tsktsk (_("use of r13 is deprecated"));       \
7654   while (0)
7655
7656 /* Functions for operand encoding.  ARM, then Thumb.  */
7657
7658 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7659
7660 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7661
7662    The only binary encoding difference is the Coprocessor number.  Coprocessor
7663    9 is used for half-precision calculations or conversions.  The format of the
7664    instruction is the same as the equivalent Coprocessor 10 instruction that
7665    exists for Single-Precision operation.  */
7666
7667 static void
7668 do_scalar_fp16_v82_encode (void)
7669 {
7670   if (inst.cond < COND_ALWAYS)
7671     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7672                " the behaviour is UNPREDICTABLE"));
7673   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7674               _(BAD_FP16));
7675
7676   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7677   mark_feature_used (&arm_ext_fp16);
7678 }
7679
7680 /* If VAL can be encoded in the immediate field of an ARM instruction,
7681    return the encoded form.  Otherwise, return FAIL.  */
7682
7683 static unsigned int
7684 encode_arm_immediate (unsigned int val)
7685 {
7686   unsigned int a, i;
7687
7688   if (val <= 0xff)
7689     return val;
7690
7691   for (i = 2; i < 32; i += 2)
7692     if ((a = rotate_left (val, i)) <= 0xff)
7693       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7694
7695   return FAIL;
7696 }
7697
7698 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7699    return the encoded form.  Otherwise, return FAIL.  */
7700 static unsigned int
7701 encode_thumb32_immediate (unsigned int val)
7702 {
7703   unsigned int a, i;
7704
7705   if (val <= 0xff)
7706     return val;
7707
7708   for (i = 1; i <= 24; i++)
7709     {
7710       a = val >> i;
7711       if ((val & ~(0xff << i)) == 0)
7712         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7713     }
7714
7715   a = val & 0xff;
7716   if (val == ((a << 16) | a))
7717     return 0x100 | a;
7718   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7719     return 0x300 | a;
7720
7721   a = val & 0xff00;
7722   if (val == ((a << 16) | a))
7723     return 0x200 | (a >> 8);
7724
7725   return FAIL;
7726 }
7727 /* Encode a VFP SP or DP register number into inst.instruction.  */
7728
7729 static void
7730 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7731 {
7732   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7733       && reg > 15)
7734     {
7735       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7736         {
7737           if (thumb_mode)
7738             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7739                                     fpu_vfp_ext_d32);
7740           else
7741             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7742                                     fpu_vfp_ext_d32);
7743         }
7744       else
7745         {
7746           first_error (_("D register out of range for selected VFP version"));
7747           return;
7748         }
7749     }
7750
7751   switch (pos)
7752     {
7753     case VFP_REG_Sd:
7754       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7755       break;
7756
7757     case VFP_REG_Sn:
7758       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7759       break;
7760
7761     case VFP_REG_Sm:
7762       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7763       break;
7764
7765     case VFP_REG_Dd:
7766       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7767       break;
7768
7769     case VFP_REG_Dn:
7770       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7771       break;
7772
7773     case VFP_REG_Dm:
7774       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7775       break;
7776
7777     default:
7778       abort ();
7779     }
7780 }
7781
7782 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7783    if any, is handled by md_apply_fix.   */
7784 static void
7785 encode_arm_shift (int i)
7786 {
7787   /* register-shifted register.  */
7788   if (inst.operands[i].immisreg)
7789     {
7790       int op_index;
7791       for (op_index = 0; op_index <= i; ++op_index)
7792         {
7793           /* Check the operand only when it's presented.  In pre-UAL syntax,
7794              if the destination register is the same as the first operand, two
7795              register form of the instruction can be used.  */
7796           if (inst.operands[op_index].present && inst.operands[op_index].isreg
7797               && inst.operands[op_index].reg == REG_PC)
7798             as_warn (UNPRED_REG ("r15"));
7799         }
7800
7801       if (inst.operands[i].imm == REG_PC)
7802         as_warn (UNPRED_REG ("r15"));
7803     }
7804
7805   if (inst.operands[i].shift_kind == SHIFT_RRX)
7806     inst.instruction |= SHIFT_ROR << 5;
7807   else
7808     {
7809       inst.instruction |= inst.operands[i].shift_kind << 5;
7810       if (inst.operands[i].immisreg)
7811         {
7812           inst.instruction |= SHIFT_BY_REG;
7813           inst.instruction |= inst.operands[i].imm << 8;
7814         }
7815       else
7816         inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
7817     }
7818 }
7819
7820 static void
7821 encode_arm_shifter_operand (int i)
7822 {
7823   if (inst.operands[i].isreg)
7824     {
7825       inst.instruction |= inst.operands[i].reg;
7826       encode_arm_shift (i);
7827     }
7828   else
7829     {
7830       inst.instruction |= INST_IMMEDIATE;
7831       if (inst.relocs[0].type != BFD_RELOC_ARM_IMMEDIATE)
7832         inst.instruction |= inst.operands[i].imm;
7833     }
7834 }
7835
7836 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7837 static void
7838 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7839 {
7840   /* PR 14260:
7841      Generate an error if the operand is not a register.  */
7842   constraint (!inst.operands[i].isreg,
7843               _("Instruction does not support =N addresses"));
7844
7845   inst.instruction |= inst.operands[i].reg << 16;
7846
7847   if (inst.operands[i].preind)
7848     {
7849       if (is_t)
7850         {
7851           inst.error = _("instruction does not accept preindexed addressing");
7852           return;
7853         }
7854       inst.instruction |= PRE_INDEX;
7855       if (inst.operands[i].writeback)
7856         inst.instruction |= WRITE_BACK;
7857
7858     }
7859   else if (inst.operands[i].postind)
7860     {
7861       gas_assert (inst.operands[i].writeback);
7862       if (is_t)
7863         inst.instruction |= WRITE_BACK;
7864     }
7865   else /* unindexed - only for coprocessor */
7866     {
7867       inst.error = _("instruction does not accept unindexed addressing");
7868       return;
7869     }
7870
7871   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7872       && (((inst.instruction & 0x000f0000) >> 16)
7873           == ((inst.instruction & 0x0000f000) >> 12)))
7874     as_warn ((inst.instruction & LOAD_BIT)
7875              ? _("destination register same as write-back base")
7876              : _("source register same as write-back base"));
7877 }
7878
7879 /* inst.operands[i] was set up by parse_address.  Encode it into an
7880    ARM-format mode 2 load or store instruction.  If is_t is true,
7881    reject forms that cannot be used with a T instruction (i.e. not
7882    post-indexed).  */
7883 static void
7884 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7885 {
7886   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7887
7888   encode_arm_addr_mode_common (i, is_t);
7889
7890   if (inst.operands[i].immisreg)
7891     {
7892       constraint ((inst.operands[i].imm == REG_PC
7893                    || (is_pc && inst.operands[i].writeback)),
7894                   BAD_PC_ADDRESSING);
7895       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7896       inst.instruction |= inst.operands[i].imm;
7897       if (!inst.operands[i].negative)
7898         inst.instruction |= INDEX_UP;
7899       if (inst.operands[i].shifted)
7900         {
7901           if (inst.operands[i].shift_kind == SHIFT_RRX)
7902             inst.instruction |= SHIFT_ROR << 5;
7903           else
7904             {
7905               inst.instruction |= inst.operands[i].shift_kind << 5;
7906               inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
7907             }
7908         }
7909     }
7910   else /* immediate offset in inst.relocs[0] */
7911     {
7912       if (is_pc && !inst.relocs[0].pc_rel)
7913         {
7914           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7915
7916           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7917              cannot use PC in addressing.
7918              PC cannot be used in writeback addressing, either.  */
7919           constraint ((is_t || inst.operands[i].writeback),
7920                       BAD_PC_ADDRESSING);
7921
7922           /* Use of PC in str is deprecated for ARMv7.  */
7923           if (warn_on_deprecated
7924               && !is_load
7925               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7926             as_tsktsk (_("use of PC in this instruction is deprecated"));
7927         }
7928
7929       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
7930         {
7931           /* Prefer + for zero encoded value.  */
7932           if (!inst.operands[i].negative)
7933             inst.instruction |= INDEX_UP;
7934           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM;
7935         }
7936     }
7937 }
7938
7939 /* inst.operands[i] was set up by parse_address.  Encode it into an
7940    ARM-format mode 3 load or store instruction.  Reject forms that
7941    cannot be used with such instructions.  If is_t is true, reject
7942    forms that cannot be used with a T instruction (i.e. not
7943    post-indexed).  */
7944 static void
7945 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7946 {
7947   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7948     {
7949       inst.error = _("instruction does not accept scaled register index");
7950       return;
7951     }
7952
7953   encode_arm_addr_mode_common (i, is_t);
7954
7955   if (inst.operands[i].immisreg)
7956     {
7957       constraint ((inst.operands[i].imm == REG_PC
7958                    || (is_t && inst.operands[i].reg == REG_PC)),
7959                   BAD_PC_ADDRESSING);
7960       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7961                   BAD_PC_WRITEBACK);
7962       inst.instruction |= inst.operands[i].imm;
7963       if (!inst.operands[i].negative)
7964         inst.instruction |= INDEX_UP;
7965     }
7966   else /* immediate offset in inst.relocs[0] */
7967     {
7968       constraint ((inst.operands[i].reg == REG_PC && !inst.relocs[0].pc_rel
7969                    && inst.operands[i].writeback),
7970                   BAD_PC_WRITEBACK);
7971       inst.instruction |= HWOFFSET_IMM;
7972       if (inst.relocs[0].type == BFD_RELOC_UNUSED)
7973         {
7974           /* Prefer + for zero encoded value.  */
7975           if (!inst.operands[i].negative)
7976             inst.instruction |= INDEX_UP;
7977
7978           inst.relocs[0].type = BFD_RELOC_ARM_OFFSET_IMM8;
7979         }
7980     }
7981 }
7982
7983 /* Write immediate bits [7:0] to the following locations:
7984
7985   |28/24|23     19|18 16|15                    4|3     0|
7986   |  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|
7987
7988   This function is used by VMOV/VMVN/VORR/VBIC.  */
7989
7990 static void
7991 neon_write_immbits (unsigned immbits)
7992 {
7993   inst.instruction |= immbits & 0xf;
7994   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7995   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7996 }
7997
7998 /* Invert low-order SIZE bits of XHI:XLO.  */
7999
8000 static void
8001 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
8002 {
8003   unsigned immlo = xlo ? *xlo : 0;
8004   unsigned immhi = xhi ? *xhi : 0;
8005
8006   switch (size)
8007     {
8008     case 8:
8009       immlo = (~immlo) & 0xff;
8010       break;
8011
8012     case 16:
8013       immlo = (~immlo) & 0xffff;
8014       break;
8015
8016     case 64:
8017       immhi = (~immhi) & 0xffffffff;
8018       /* fall through.  */
8019
8020     case 32:
8021       immlo = (~immlo) & 0xffffffff;
8022       break;
8023
8024     default:
8025       abort ();
8026     }
8027
8028   if (xlo)
8029     *xlo = immlo;
8030
8031   if (xhi)
8032     *xhi = immhi;
8033 }
8034
8035 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
8036    A, B, C, D.  */
8037
8038 static int
8039 neon_bits_same_in_bytes (unsigned imm)
8040 {
8041   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
8042          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
8043          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
8044          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
8045 }
8046
8047 /* For immediate of above form, return 0bABCD.  */
8048
8049 static unsigned
8050 neon_squash_bits (unsigned imm)
8051 {
8052   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
8053          | ((imm & 0x01000000) >> 21);
8054 }
8055
8056 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
8057
8058 static unsigned
8059 neon_qfloat_bits (unsigned imm)
8060 {
8061   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
8062 }
8063
8064 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
8065    the instruction. *OP is passed as the initial value of the op field, and
8066    may be set to a different value depending on the constant (i.e.
8067    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
8068    MVN).  If the immediate looks like a repeated pattern then also
8069    try smaller element sizes.  */
8070
8071 static int
8072 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
8073                          unsigned *immbits, int *op, int size,
8074                          enum neon_el_type type)
8075 {
8076   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
8077      float.  */
8078   if (type == NT_float && !float_p)
8079     return FAIL;
8080
8081   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
8082     {
8083       if (size != 32 || *op == 1)
8084         return FAIL;
8085       *immbits = neon_qfloat_bits (immlo);
8086       return 0xf;
8087     }
8088
8089   if (size == 64)
8090     {
8091       if (neon_bits_same_in_bytes (immhi)
8092           && neon_bits_same_in_bytes (immlo))
8093         {
8094           if (*op == 1)
8095             return FAIL;
8096           *immbits = (neon_squash_bits (immhi) << 4)
8097                      | neon_squash_bits (immlo);
8098           *op = 1;
8099           return 0xe;
8100         }
8101
8102       if (immhi != immlo)
8103         return FAIL;
8104     }
8105
8106   if (size >= 32)
8107     {
8108       if (immlo == (immlo & 0x000000ff))
8109         {
8110           *immbits = immlo;
8111           return 0x0;
8112         }
8113       else if (immlo == (immlo & 0x0000ff00))
8114         {
8115           *immbits = immlo >> 8;
8116           return 0x2;
8117         }
8118       else if (immlo == (immlo & 0x00ff0000))
8119         {
8120           *immbits = immlo >> 16;
8121           return 0x4;
8122         }
8123       else if (immlo == (immlo & 0xff000000))
8124         {
8125           *immbits = immlo >> 24;
8126           return 0x6;
8127         }
8128       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
8129         {
8130           *immbits = (immlo >> 8) & 0xff;
8131           return 0xc;
8132         }
8133       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
8134         {
8135           *immbits = (immlo >> 16) & 0xff;
8136           return 0xd;
8137         }
8138
8139       if ((immlo & 0xffff) != (immlo >> 16))
8140         return FAIL;
8141       immlo &= 0xffff;
8142     }
8143
8144   if (size >= 16)
8145     {
8146       if (immlo == (immlo & 0x000000ff))
8147         {
8148           *immbits = immlo;
8149           return 0x8;
8150         }
8151       else if (immlo == (immlo & 0x0000ff00))
8152         {
8153           *immbits = immlo >> 8;
8154           return 0xa;
8155         }
8156
8157       if ((immlo & 0xff) != (immlo >> 8))
8158         return FAIL;
8159       immlo &= 0xff;
8160     }
8161
8162   if (immlo == (immlo & 0x000000ff))
8163     {
8164       /* Don't allow MVN with 8-bit immediate.  */
8165       if (*op == 1)
8166         return FAIL;
8167       *immbits = immlo;
8168       return 0xe;
8169     }
8170
8171   return FAIL;
8172 }
8173
8174 #if defined BFD_HOST_64_BIT
8175 /* Returns TRUE if double precision value V may be cast
8176    to single precision without loss of accuracy.  */
8177
8178 static bfd_boolean
8179 is_double_a_single (bfd_int64_t v)
8180 {
8181   int exp = (int)((v >> 52) & 0x7FF);
8182   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8183
8184   return (exp == 0 || exp == 0x7FF
8185           || (exp >= 1023 - 126 && exp <= 1023 + 127))
8186     && (mantissa & 0x1FFFFFFFl) == 0;
8187 }
8188
8189 /* Returns a double precision value casted to single precision
8190    (ignoring the least significant bits in exponent and mantissa).  */
8191
8192 static int
8193 double_to_single (bfd_int64_t v)
8194 {
8195   int sign = (int) ((v >> 63) & 1l);
8196   int exp = (int) ((v >> 52) & 0x7FF);
8197   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
8198
8199   if (exp == 0x7FF)
8200     exp = 0xFF;
8201   else
8202     {
8203       exp = exp - 1023 + 127;
8204       if (exp >= 0xFF)
8205         {
8206           /* Infinity.  */
8207           exp = 0x7F;
8208           mantissa = 0;
8209         }
8210       else if (exp < 0)
8211         {
8212           /* No denormalized numbers.  */
8213           exp = 0;
8214           mantissa = 0;
8215         }
8216     }
8217   mantissa >>= 29;
8218   return (sign << 31) | (exp << 23) | mantissa;
8219 }
8220 #endif /* BFD_HOST_64_BIT */
8221
8222 enum lit_type
8223 {
8224   CONST_THUMB,
8225   CONST_ARM,
8226   CONST_VEC
8227 };
8228
8229 static void do_vfp_nsyn_opcode (const char *);
8230
8231 /* inst.relocs[0].exp describes an "=expr" load pseudo-operation.
8232    Determine whether it can be performed with a move instruction; if
8233    it can, convert inst.instruction to that move instruction and
8234    return TRUE; if it can't, convert inst.instruction to a literal-pool
8235    load and return FALSE.  If this is not a valid thing to do in the
8236    current context, set inst.error and return TRUE.
8237
8238    inst.operands[i] describes the destination register.  */
8239
8240 static bfd_boolean
8241 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
8242 {
8243   unsigned long tbit;
8244   bfd_boolean thumb_p = (t == CONST_THUMB);
8245   bfd_boolean arm_p   = (t == CONST_ARM);
8246
8247   if (thumb_p)
8248     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
8249   else
8250     tbit = LOAD_BIT;
8251
8252   if ((inst.instruction & tbit) == 0)
8253     {
8254       inst.error = _("invalid pseudo operation");
8255       return TRUE;
8256     }
8257
8258   if (inst.relocs[0].exp.X_op != O_constant
8259       && inst.relocs[0].exp.X_op != O_symbol
8260       && inst.relocs[0].exp.X_op != O_big)
8261     {
8262       inst.error = _("constant expression expected");
8263       return TRUE;
8264     }
8265
8266   if (inst.relocs[0].exp.X_op == O_constant
8267       || inst.relocs[0].exp.X_op == O_big)
8268     {
8269 #if defined BFD_HOST_64_BIT
8270       bfd_int64_t v;
8271 #else
8272       offsetT v;
8273 #endif
8274       if (inst.relocs[0].exp.X_op == O_big)
8275         {
8276           LITTLENUM_TYPE w[X_PRECISION];
8277           LITTLENUM_TYPE * l;
8278
8279           if (inst.relocs[0].exp.X_add_number == -1)
8280             {
8281               gen_to_words (w, X_PRECISION, E_PRECISION);
8282               l = w;
8283               /* FIXME: Should we check words w[2..5] ?  */
8284             }
8285           else
8286             l = generic_bignum;
8287
8288 #if defined BFD_HOST_64_BIT
8289           v =
8290             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8291                   << LITTLENUM_NUMBER_OF_BITS)
8292                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8293                 << LITTLENUM_NUMBER_OF_BITS)
8294                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8295               << LITTLENUM_NUMBER_OF_BITS)
8296              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8297 #else
8298           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8299             |  (l[0] & LITTLENUM_MASK);
8300 #endif
8301         }
8302       else
8303         v = inst.relocs[0].exp.X_add_number;
8304
8305       if (!inst.operands[i].issingle)
8306         {
8307           if (thumb_p)
8308             {
8309               /* LDR should not use lead in a flag-setting instruction being
8310                  chosen so we do not check whether movs can be used.  */
8311
8312               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8313                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8314                   && inst.operands[i].reg != 13
8315                   && inst.operands[i].reg != 15)
8316                 {
8317                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8318                      movw instruction.  */
8319                   unsigned int newimm;
8320                   bfd_boolean isNegated;
8321
8322                   newimm = encode_thumb32_immediate (v);
8323                   if (newimm != (unsigned int) FAIL)
8324                     isNegated = FALSE;
8325                   else
8326                     {
8327                       newimm = encode_thumb32_immediate (~v);
8328                       if (newimm != (unsigned int) FAIL)
8329                         isNegated = TRUE;
8330                     }
8331
8332                   /* The number can be loaded with a mov.w or mvn
8333                      instruction.  */
8334                   if (newimm != (unsigned int) FAIL
8335                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8336                     {
8337                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8338                                           | (inst.operands[i].reg << 8));
8339                       /* Change to MOVN.  */
8340                       inst.instruction |= (isNegated ? 0x200000 : 0);
8341                       inst.instruction |= (newimm & 0x800) << 15;
8342                       inst.instruction |= (newimm & 0x700) << 4;
8343                       inst.instruction |= (newimm & 0x0ff);
8344                       return TRUE;
8345                     }
8346                   /* The number can be loaded with a movw instruction.  */
8347                   else if ((v & ~0xFFFF) == 0
8348                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8349                     {
8350                       int imm = v & 0xFFFF;
8351
8352                       inst.instruction = 0xf2400000;  /* MOVW.  */
8353                       inst.instruction |= (inst.operands[i].reg << 8);
8354                       inst.instruction |= (imm & 0xf000) << 4;
8355                       inst.instruction |= (imm & 0x0800) << 15;
8356                       inst.instruction |= (imm & 0x0700) << 4;
8357                       inst.instruction |= (imm & 0x00ff);
8358                       return TRUE;
8359                     }
8360                 }
8361             }
8362           else if (arm_p)
8363             {
8364               int value = encode_arm_immediate (v);
8365
8366               if (value != FAIL)
8367                 {
8368                   /* This can be done with a mov instruction.  */
8369                   inst.instruction &= LITERAL_MASK;
8370                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8371                   inst.instruction |= value & 0xfff;
8372                   return TRUE;
8373                 }
8374
8375               value = encode_arm_immediate (~ v);
8376               if (value != FAIL)
8377                 {
8378                   /* This can be done with a mvn instruction.  */
8379                   inst.instruction &= LITERAL_MASK;
8380                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8381                   inst.instruction |= value & 0xfff;
8382                   return TRUE;
8383                 }
8384             }
8385           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8386             {
8387               int op = 0;
8388               unsigned immbits = 0;
8389               unsigned immlo = inst.operands[1].imm;
8390               unsigned immhi = inst.operands[1].regisimm
8391                 ? inst.operands[1].reg
8392                 : inst.relocs[0].exp.X_unsigned
8393                 ? 0
8394                 : ((bfd_int64_t)((int) immlo)) >> 32;
8395               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8396                                                    &op, 64, NT_invtype);
8397
8398               if (cmode == FAIL)
8399                 {
8400                   neon_invert_size (&immlo, &immhi, 64);
8401                   op = !op;
8402                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8403                                                    &op, 64, NT_invtype);
8404                 }
8405
8406               if (cmode != FAIL)
8407                 {
8408                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8409                     | (1 << 23)
8410                     | (cmode << 8)
8411                     | (op << 5)
8412                     | (1 << 4);
8413
8414                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8415                   if (thumb_mode)
8416                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8417                   else
8418                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8419                   neon_write_immbits (immbits);
8420                   return TRUE;
8421                 }
8422             }
8423         }
8424
8425       if (t == CONST_VEC)
8426         {
8427           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8428           if (inst.operands[i].issingle
8429               && is_quarter_float (inst.operands[1].imm)
8430               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8431             {
8432               inst.operands[1].imm =
8433                 neon_qfloat_bits (v);
8434               do_vfp_nsyn_opcode ("fconsts");
8435               return TRUE;
8436             }
8437
8438           /* If our host does not support a 64-bit type then we cannot perform
8439              the following optimization.  This mean that there will be a
8440              discrepancy between the output produced by an assembler built for
8441              a 32-bit-only host and the output produced from a 64-bit host, but
8442              this cannot be helped.  */
8443 #if defined BFD_HOST_64_BIT
8444           else if (!inst.operands[1].issingle
8445                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8446             {
8447               if (is_double_a_single (v)
8448                   && is_quarter_float (double_to_single (v)))
8449                 {
8450                   inst.operands[1].imm =
8451                     neon_qfloat_bits (double_to_single (v));
8452                   do_vfp_nsyn_opcode ("fconstd");
8453                   return TRUE;
8454                 }
8455             }
8456 #endif
8457         }
8458     }
8459
8460   if (add_to_lit_pool ((!inst.operands[i].isvec
8461                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8462     return TRUE;
8463
8464   inst.operands[1].reg = REG_PC;
8465   inst.operands[1].isreg = 1;
8466   inst.operands[1].preind = 1;
8467   inst.relocs[0].pc_rel = 1;
8468   inst.relocs[0].type = (thumb_p
8469                      ? BFD_RELOC_ARM_THUMB_OFFSET
8470                      : (mode_3
8471                         ? BFD_RELOC_ARM_HWLITERAL
8472                         : BFD_RELOC_ARM_LITERAL));
8473   return FALSE;
8474 }
8475
8476 /* inst.operands[i] was set up by parse_address.  Encode it into an
8477    ARM-format instruction.  Reject all forms which cannot be encoded
8478    into a coprocessor load/store instruction.  If wb_ok is false,
8479    reject use of writeback; if unind_ok is false, reject use of
8480    unindexed addressing.  If reloc_override is not 0, use it instead
8481    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8482    (in which case it is preserved).  */
8483
8484 static int
8485 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8486 {
8487   if (!inst.operands[i].isreg)
8488     {
8489       /* PR 18256 */
8490       if (! inst.operands[0].isvec)
8491         {
8492           inst.error = _("invalid co-processor operand");
8493           return FAIL;
8494         }
8495       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8496         return SUCCESS;
8497     }
8498
8499   inst.instruction |= inst.operands[i].reg << 16;
8500
8501   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8502
8503   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8504     {
8505       gas_assert (!inst.operands[i].writeback);
8506       if (!unind_ok)
8507         {
8508           inst.error = _("instruction does not support unindexed addressing");
8509           return FAIL;
8510         }
8511       inst.instruction |= inst.operands[i].imm;
8512       inst.instruction |= INDEX_UP;
8513       return SUCCESS;
8514     }
8515
8516   if (inst.operands[i].preind)
8517     inst.instruction |= PRE_INDEX;
8518
8519   if (inst.operands[i].writeback)
8520     {
8521       if (inst.operands[i].reg == REG_PC)
8522         {
8523           inst.error = _("pc may not be used with write-back");
8524           return FAIL;
8525         }
8526       if (!wb_ok)
8527         {
8528           inst.error = _("instruction does not support writeback");
8529           return FAIL;
8530         }
8531       inst.instruction |= WRITE_BACK;
8532     }
8533
8534   if (reloc_override)
8535     inst.relocs[0].type = (bfd_reloc_code_real_type) reloc_override;
8536   else if ((inst.relocs[0].type < BFD_RELOC_ARM_ALU_PC_G0_NC
8537             || inst.relocs[0].type > BFD_RELOC_ARM_LDC_SB_G2)
8538            && inst.relocs[0].type != BFD_RELOC_ARM_LDR_PC_G0)
8539     {
8540       if (thumb_mode)
8541         inst.relocs[0].type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8542       else
8543         inst.relocs[0].type = BFD_RELOC_ARM_CP_OFF_IMM;
8544     }
8545
8546   /* Prefer + for zero encoded value.  */
8547   if (!inst.operands[i].negative)
8548     inst.instruction |= INDEX_UP;
8549
8550   return SUCCESS;
8551 }
8552
8553 /* Functions for instruction encoding, sorted by sub-architecture.
8554    First some generics; their names are taken from the conventional
8555    bit positions for register arguments in ARM format instructions.  */
8556
8557 static void
8558 do_noargs (void)
8559 {
8560 }
8561
8562 static void
8563 do_rd (void)
8564 {
8565   inst.instruction |= inst.operands[0].reg << 12;
8566 }
8567
8568 static void
8569 do_rn (void)
8570 {
8571   inst.instruction |= inst.operands[0].reg << 16;
8572 }
8573
8574 static void
8575 do_rd_rm (void)
8576 {
8577   inst.instruction |= inst.operands[0].reg << 12;
8578   inst.instruction |= inst.operands[1].reg;
8579 }
8580
8581 static void
8582 do_rm_rn (void)
8583 {
8584   inst.instruction |= inst.operands[0].reg;
8585   inst.instruction |= inst.operands[1].reg << 16;
8586 }
8587
8588 static void
8589 do_rd_rn (void)
8590 {
8591   inst.instruction |= inst.operands[0].reg << 12;
8592   inst.instruction |= inst.operands[1].reg << 16;
8593 }
8594
8595 static void
8596 do_rn_rd (void)
8597 {
8598   inst.instruction |= inst.operands[0].reg << 16;
8599   inst.instruction |= inst.operands[1].reg << 12;
8600 }
8601
8602 static void
8603 do_tt (void)
8604 {
8605   inst.instruction |= inst.operands[0].reg << 8;
8606   inst.instruction |= inst.operands[1].reg << 16;
8607 }
8608
8609 static bfd_boolean
8610 check_obsolete (const arm_feature_set *feature, const char *msg)
8611 {
8612   if (ARM_CPU_IS_ANY (cpu_variant))
8613     {
8614       as_tsktsk ("%s", msg);
8615       return TRUE;
8616     }
8617   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8618     {
8619       as_bad ("%s", msg);
8620       return TRUE;
8621     }
8622
8623   return FALSE;
8624 }
8625
8626 static void
8627 do_rd_rm_rn (void)
8628 {
8629   unsigned Rn = inst.operands[2].reg;
8630   /* Enforce restrictions on SWP instruction.  */
8631   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8632     {
8633       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8634                   _("Rn must not overlap other operands"));
8635
8636       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8637        */
8638       if (!check_obsolete (&arm_ext_v8,
8639                            _("swp{b} use is obsoleted for ARMv8 and later"))
8640           && warn_on_deprecated
8641           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8642         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8643     }
8644
8645   inst.instruction |= inst.operands[0].reg << 12;
8646   inst.instruction |= inst.operands[1].reg;
8647   inst.instruction |= Rn << 16;
8648 }
8649
8650 static void
8651 do_rd_rn_rm (void)
8652 {
8653   inst.instruction |= inst.operands[0].reg << 12;
8654   inst.instruction |= inst.operands[1].reg << 16;
8655   inst.instruction |= inst.operands[2].reg;
8656 }
8657
8658 static void
8659 do_rm_rd_rn (void)
8660 {
8661   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8662   constraint (((inst.relocs[0].exp.X_op != O_constant
8663                 && inst.relocs[0].exp.X_op != O_illegal)
8664                || inst.relocs[0].exp.X_add_number != 0),
8665               BAD_ADDR_MODE);
8666   inst.instruction |= inst.operands[0].reg;
8667   inst.instruction |= inst.operands[1].reg << 12;
8668   inst.instruction |= inst.operands[2].reg << 16;
8669 }
8670
8671 static void
8672 do_imm0 (void)
8673 {
8674   inst.instruction |= inst.operands[0].imm;
8675 }
8676
8677 static void
8678 do_rd_cpaddr (void)
8679 {
8680   inst.instruction |= inst.operands[0].reg << 12;
8681   encode_arm_cp_address (1, TRUE, TRUE, 0);
8682 }
8683
8684 /* ARM instructions, in alphabetical order by function name (except
8685    that wrapper functions appear immediately after the function they
8686    wrap).  */
8687
8688 /* This is a pseudo-op of the form "adr rd, label" to be converted
8689    into a relative address of the form "add rd, pc, #label-.-8".  */
8690
8691 static void
8692 do_adr (void)
8693 {
8694   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8695
8696   /* Frag hacking will turn this into a sub instruction if the offset turns
8697      out to be negative.  */
8698   inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
8699   inst.relocs[0].pc_rel = 1;
8700   inst.relocs[0].exp.X_add_number -= 8;
8701
8702   if (support_interwork
8703       && inst.relocs[0].exp.X_op == O_symbol
8704       && inst.relocs[0].exp.X_add_symbol != NULL
8705       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
8706       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
8707     inst.relocs[0].exp.X_add_number |= 1;
8708 }
8709
8710 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8711    into a relative address of the form:
8712    add rd, pc, #low(label-.-8)"
8713    add rd, rd, #high(label-.-8)"  */
8714
8715 static void
8716 do_adrl (void)
8717 {
8718   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8719
8720   /* Frag hacking will turn this into a sub instruction if the offset turns
8721      out to be negative.  */
8722   inst.relocs[0].type          = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8723   inst.relocs[0].pc_rel        = 1;
8724   inst.size                    = INSN_SIZE * 2;
8725   inst.relocs[0].exp.X_add_number -= 8;
8726
8727   if (support_interwork
8728       && inst.relocs[0].exp.X_op == O_symbol
8729       && inst.relocs[0].exp.X_add_symbol != NULL
8730       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
8731       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
8732     inst.relocs[0].exp.X_add_number |= 1;
8733 }
8734
8735 static void
8736 do_arit (void)
8737 {
8738   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
8739               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
8740               THUMB1_RELOC_ONLY);
8741   if (!inst.operands[1].present)
8742     inst.operands[1].reg = inst.operands[0].reg;
8743   inst.instruction |= inst.operands[0].reg << 12;
8744   inst.instruction |= inst.operands[1].reg << 16;
8745   encode_arm_shifter_operand (2);
8746 }
8747
8748 static void
8749 do_barrier (void)
8750 {
8751   if (inst.operands[0].present)
8752     inst.instruction |= inst.operands[0].imm;
8753   else
8754     inst.instruction |= 0xf;
8755 }
8756
8757 static void
8758 do_bfc (void)
8759 {
8760   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8761   constraint (msb > 32, _("bit-field extends past end of register"));
8762   /* The instruction encoding stores the LSB and MSB,
8763      not the LSB and width.  */
8764   inst.instruction |= inst.operands[0].reg << 12;
8765   inst.instruction |= inst.operands[1].imm << 7;
8766   inst.instruction |= (msb - 1) << 16;
8767 }
8768
8769 static void
8770 do_bfi (void)
8771 {
8772   unsigned int msb;
8773
8774   /* #0 in second position is alternative syntax for bfc, which is
8775      the same instruction but with REG_PC in the Rm field.  */
8776   if (!inst.operands[1].isreg)
8777     inst.operands[1].reg = REG_PC;
8778
8779   msb = inst.operands[2].imm + inst.operands[3].imm;
8780   constraint (msb > 32, _("bit-field extends past end of register"));
8781   /* The instruction encoding stores the LSB and MSB,
8782      not the LSB and width.  */
8783   inst.instruction |= inst.operands[0].reg << 12;
8784   inst.instruction |= inst.operands[1].reg;
8785   inst.instruction |= inst.operands[2].imm << 7;
8786   inst.instruction |= (msb - 1) << 16;
8787 }
8788
8789 static void
8790 do_bfx (void)
8791 {
8792   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8793               _("bit-field extends past end of register"));
8794   inst.instruction |= inst.operands[0].reg << 12;
8795   inst.instruction |= inst.operands[1].reg;
8796   inst.instruction |= inst.operands[2].imm << 7;
8797   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8798 }
8799
8800 /* ARM V5 breakpoint instruction (argument parse)
8801      BKPT <16 bit unsigned immediate>
8802      Instruction is not conditional.
8803         The bit pattern given in insns[] has the COND_ALWAYS condition,
8804         and it is an error if the caller tried to override that.  */
8805
8806 static void
8807 do_bkpt (void)
8808 {
8809   /* Top 12 of 16 bits to bits 19:8.  */
8810   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8811
8812   /* Bottom 4 of 16 bits to bits 3:0.  */
8813   inst.instruction |= inst.operands[0].imm & 0xf;
8814 }
8815
8816 static void
8817 encode_branch (int default_reloc)
8818 {
8819   if (inst.operands[0].hasreloc)
8820     {
8821       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8822                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8823                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8824       inst.relocs[0].type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8825         ? BFD_RELOC_ARM_PLT32
8826         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8827     }
8828   else
8829     inst.relocs[0].type = (bfd_reloc_code_real_type) default_reloc;
8830   inst.relocs[0].pc_rel = 1;
8831 }
8832
8833 static void
8834 do_branch (void)
8835 {
8836 #ifdef OBJ_ELF
8837   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8838     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8839   else
8840 #endif
8841     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8842 }
8843
8844 static void
8845 do_bl (void)
8846 {
8847 #ifdef OBJ_ELF
8848   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8849     {
8850       if (inst.cond == COND_ALWAYS)
8851         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8852       else
8853         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8854     }
8855   else
8856 #endif
8857     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8858 }
8859
8860 /* ARM V5 branch-link-exchange instruction (argument parse)
8861      BLX <target_addr>          ie BLX(1)
8862      BLX{<condition>} <Rm>      ie BLX(2)
8863    Unfortunately, there are two different opcodes for this mnemonic.
8864    So, the insns[].value is not used, and the code here zaps values
8865         into inst.instruction.
8866    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8867
8868 static void
8869 do_blx (void)
8870 {
8871   if (inst.operands[0].isreg)
8872     {
8873       /* Arg is a register; the opcode provided by insns[] is correct.
8874          It is not illegal to do "blx pc", just useless.  */
8875       if (inst.operands[0].reg == REG_PC)
8876         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8877
8878       inst.instruction |= inst.operands[0].reg;
8879     }
8880   else
8881     {
8882       /* Arg is an address; this instruction cannot be executed
8883          conditionally, and the opcode must be adjusted.
8884          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8885          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8886       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8887       inst.instruction = 0xfa000000;
8888       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8889     }
8890 }
8891
8892 static void
8893 do_bx (void)
8894 {
8895   bfd_boolean want_reloc;
8896
8897   if (inst.operands[0].reg == REG_PC)
8898     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8899
8900   inst.instruction |= inst.operands[0].reg;
8901   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8902      it is for ARMv4t or earlier.  */
8903   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8904   if (!ARM_FEATURE_ZERO (selected_object_arch)
8905       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
8906       want_reloc = TRUE;
8907
8908 #ifdef OBJ_ELF
8909   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8910 #endif
8911     want_reloc = FALSE;
8912
8913   if (want_reloc)
8914     inst.relocs[0].type = BFD_RELOC_ARM_V4BX;
8915 }
8916
8917
8918 /* ARM v5TEJ.  Jump to Jazelle code.  */
8919
8920 static void
8921 do_bxj (void)
8922 {
8923   if (inst.operands[0].reg == REG_PC)
8924     as_tsktsk (_("use of r15 in bxj is not really useful"));
8925
8926   inst.instruction |= inst.operands[0].reg;
8927 }
8928
8929 /* Co-processor data operation:
8930       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8931       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8932 static void
8933 do_cdp (void)
8934 {
8935   inst.instruction |= inst.operands[0].reg << 8;
8936   inst.instruction |= inst.operands[1].imm << 20;
8937   inst.instruction |= inst.operands[2].reg << 12;
8938   inst.instruction |= inst.operands[3].reg << 16;
8939   inst.instruction |= inst.operands[4].reg;
8940   inst.instruction |= inst.operands[5].imm << 5;
8941 }
8942
8943 static void
8944 do_cmp (void)
8945 {
8946   inst.instruction |= inst.operands[0].reg << 16;
8947   encode_arm_shifter_operand (1);
8948 }
8949
8950 /* Transfer between coprocessor and ARM registers.
8951    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8952    MRC2
8953    MCR{cond}
8954    MCR2
8955
8956    No special properties.  */
8957
8958 struct deprecated_coproc_regs_s
8959 {
8960   unsigned cp;
8961   int opc1;
8962   unsigned crn;
8963   unsigned crm;
8964   int opc2;
8965   arm_feature_set deprecated;
8966   arm_feature_set obsoleted;
8967   const char *dep_msg;
8968   const char *obs_msg;
8969 };
8970
8971 #define DEPR_ACCESS_V8 \
8972   N_("This coprocessor register access is deprecated in ARMv8")
8973
8974 /* Table of all deprecated coprocessor registers.  */
8975 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8976 {
8977     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8978      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8979      DEPR_ACCESS_V8, NULL},
8980     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8981      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8982      DEPR_ACCESS_V8, NULL},
8983     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8984      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8985      DEPR_ACCESS_V8, NULL},
8986     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8987      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8988      DEPR_ACCESS_V8, NULL},
8989     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8990      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8991      DEPR_ACCESS_V8, NULL},
8992 };
8993
8994 #undef DEPR_ACCESS_V8
8995
8996 static const size_t deprecated_coproc_reg_count =
8997   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8998
8999 static void
9000 do_co_reg (void)
9001 {
9002   unsigned Rd;
9003   size_t i;
9004
9005   Rd = inst.operands[2].reg;
9006   if (thumb_mode)
9007     {
9008       if (inst.instruction == 0xee000010
9009           || inst.instruction == 0xfe000010)
9010         /* MCR, MCR2  */
9011         reject_bad_reg (Rd);
9012       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9013         /* MRC, MRC2  */
9014         constraint (Rd == REG_SP, BAD_SP);
9015     }
9016   else
9017     {
9018       /* MCR */
9019       if (inst.instruction == 0xe000010)
9020         constraint (Rd == REG_PC, BAD_PC);
9021     }
9022
9023     for (i = 0; i < deprecated_coproc_reg_count; ++i)
9024       {
9025         const struct deprecated_coproc_regs_s *r =
9026           deprecated_coproc_regs + i;
9027
9028         if (inst.operands[0].reg == r->cp
9029             && inst.operands[1].imm == r->opc1
9030             && inst.operands[3].reg == r->crn
9031             && inst.operands[4].reg == r->crm
9032             && inst.operands[5].imm == r->opc2)
9033           {
9034             if (! ARM_CPU_IS_ANY (cpu_variant)
9035                 && warn_on_deprecated
9036                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
9037               as_tsktsk ("%s", r->dep_msg);
9038           }
9039       }
9040
9041   inst.instruction |= inst.operands[0].reg << 8;
9042   inst.instruction |= inst.operands[1].imm << 21;
9043   inst.instruction |= Rd << 12;
9044   inst.instruction |= inst.operands[3].reg << 16;
9045   inst.instruction |= inst.operands[4].reg;
9046   inst.instruction |= inst.operands[5].imm << 5;
9047 }
9048
9049 /* Transfer between coprocessor register and pair of ARM registers.
9050    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
9051    MCRR2
9052    MRRC{cond}
9053    MRRC2
9054
9055    Two XScale instructions are special cases of these:
9056
9057      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
9058      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
9059
9060    Result unpredictable if Rd or Rn is R15.  */
9061
9062 static void
9063 do_co_reg2c (void)
9064 {
9065   unsigned Rd, Rn;
9066
9067   Rd = inst.operands[2].reg;
9068   Rn = inst.operands[3].reg;
9069
9070   if (thumb_mode)
9071     {
9072       reject_bad_reg (Rd);
9073       reject_bad_reg (Rn);
9074     }
9075   else
9076     {
9077       constraint (Rd == REG_PC, BAD_PC);
9078       constraint (Rn == REG_PC, BAD_PC);
9079     }
9080
9081   /* Only check the MRRC{2} variants.  */
9082   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
9083     {
9084        /* If Rd == Rn, error that the operation is
9085           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
9086        constraint (Rd == Rn, BAD_OVERLAP);
9087     }
9088
9089   inst.instruction |= inst.operands[0].reg << 8;
9090   inst.instruction |= inst.operands[1].imm << 4;
9091   inst.instruction |= Rd << 12;
9092   inst.instruction |= Rn << 16;
9093   inst.instruction |= inst.operands[4].reg;
9094 }
9095
9096 static void
9097 do_cpsi (void)
9098 {
9099   inst.instruction |= inst.operands[0].imm << 6;
9100   if (inst.operands[1].present)
9101     {
9102       inst.instruction |= CPSI_MMOD;
9103       inst.instruction |= inst.operands[1].imm;
9104     }
9105 }
9106
9107 static void
9108 do_dbg (void)
9109 {
9110   inst.instruction |= inst.operands[0].imm;
9111 }
9112
9113 static void
9114 do_div (void)
9115 {
9116   unsigned Rd, Rn, Rm;
9117
9118   Rd = inst.operands[0].reg;
9119   Rn = (inst.operands[1].present
9120         ? inst.operands[1].reg : Rd);
9121   Rm = inst.operands[2].reg;
9122
9123   constraint ((Rd == REG_PC), BAD_PC);
9124   constraint ((Rn == REG_PC), BAD_PC);
9125   constraint ((Rm == REG_PC), BAD_PC);
9126
9127   inst.instruction |= Rd << 16;
9128   inst.instruction |= Rn << 0;
9129   inst.instruction |= Rm << 8;
9130 }
9131
9132 static void
9133 do_it (void)
9134 {
9135   /* There is no IT instruction in ARM mode.  We
9136      process it to do the validation as if in
9137      thumb mode, just in case the code gets
9138      assembled for thumb using the unified syntax.  */
9139
9140   inst.size = 0;
9141   if (unified_syntax)
9142     {
9143       set_pred_insn_type (IT_INSN);
9144       now_pred.mask = (inst.instruction & 0xf) | 0x10;
9145       now_pred.cc = inst.operands[0].imm;
9146     }
9147 }
9148
9149 /* If there is only one register in the register list,
9150    then return its register number.  Otherwise return -1.  */
9151 static int
9152 only_one_reg_in_list (int range)
9153 {
9154   int i = ffs (range) - 1;
9155   return (i > 15 || range != (1 << i)) ? -1 : i;
9156 }
9157
9158 static void
9159 encode_ldmstm(int from_push_pop_mnem)
9160 {
9161   int base_reg = inst.operands[0].reg;
9162   int range = inst.operands[1].imm;
9163   int one_reg;
9164
9165   inst.instruction |= base_reg << 16;
9166   inst.instruction |= range;
9167
9168   if (inst.operands[1].writeback)
9169     inst.instruction |= LDM_TYPE_2_OR_3;
9170
9171   if (inst.operands[0].writeback)
9172     {
9173       inst.instruction |= WRITE_BACK;
9174       /* Check for unpredictable uses of writeback.  */
9175       if (inst.instruction & LOAD_BIT)
9176         {
9177           /* Not allowed in LDM type 2.  */
9178           if ((inst.instruction & LDM_TYPE_2_OR_3)
9179               && ((range & (1 << REG_PC)) == 0))
9180             as_warn (_("writeback of base register is UNPREDICTABLE"));
9181           /* Only allowed if base reg not in list for other types.  */
9182           else if (range & (1 << base_reg))
9183             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
9184         }
9185       else /* STM.  */
9186         {
9187           /* Not allowed for type 2.  */
9188           if (inst.instruction & LDM_TYPE_2_OR_3)
9189             as_warn (_("writeback of base register is UNPREDICTABLE"));
9190           /* Only allowed if base reg not in list, or first in list.  */
9191           else if ((range & (1 << base_reg))
9192                    && (range & ((1 << base_reg) - 1)))
9193             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
9194         }
9195     }
9196
9197   /* If PUSH/POP has only one register, then use the A2 encoding.  */
9198   one_reg = only_one_reg_in_list (range);
9199   if (from_push_pop_mnem && one_reg >= 0)
9200     {
9201       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
9202
9203       if (is_push && one_reg == 13 /* SP */)
9204         /* PR 22483: The A2 encoding cannot be used when
9205            pushing the stack pointer as this is UNPREDICTABLE.  */
9206         return;
9207
9208       inst.instruction &= A_COND_MASK;
9209       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
9210       inst.instruction |= one_reg << 12;
9211     }
9212 }
9213
9214 static void
9215 do_ldmstm (void)
9216 {
9217   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
9218 }
9219
9220 /* ARMv5TE load-consecutive (argument parse)
9221    Mode is like LDRH.
9222
9223      LDRccD R, mode
9224      STRccD R, mode.  */
9225
9226 static void
9227 do_ldrd (void)
9228 {
9229   constraint (inst.operands[0].reg % 2 != 0,
9230               _("first transfer register must be even"));
9231   constraint (inst.operands[1].present
9232               && inst.operands[1].reg != inst.operands[0].reg + 1,
9233               _("can only transfer two consecutive registers"));
9234   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9235   constraint (!inst.operands[2].isreg, _("'[' expected"));
9236
9237   if (!inst.operands[1].present)
9238     inst.operands[1].reg = inst.operands[0].reg + 1;
9239
9240   /* encode_arm_addr_mode_3 will diagnose overlap between the base
9241      register and the first register written; we have to diagnose
9242      overlap between the base and the second register written here.  */
9243
9244   if (inst.operands[2].reg == inst.operands[1].reg
9245       && (inst.operands[2].writeback || inst.operands[2].postind))
9246     as_warn (_("base register written back, and overlaps "
9247                "second transfer register"));
9248
9249   if (!(inst.instruction & V4_STR_BIT))
9250     {
9251       /* For an index-register load, the index register must not overlap the
9252         destination (even if not write-back).  */
9253       if (inst.operands[2].immisreg
9254               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
9255               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
9256         as_warn (_("index register overlaps transfer register"));
9257     }
9258   inst.instruction |= inst.operands[0].reg << 12;
9259   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
9260 }
9261
9262 static void
9263 do_ldrex (void)
9264 {
9265   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
9266               || inst.operands[1].postind || inst.operands[1].writeback
9267               || inst.operands[1].immisreg || inst.operands[1].shifted
9268               || inst.operands[1].negative
9269               /* This can arise if the programmer has written
9270                    strex rN, rM, foo
9271                  or if they have mistakenly used a register name as the last
9272                  operand,  eg:
9273                    strex rN, rM, rX
9274                  It is very difficult to distinguish between these two cases
9275                  because "rX" might actually be a label. ie the register
9276                  name has been occluded by a symbol of the same name. So we
9277                  just generate a general 'bad addressing mode' type error
9278                  message and leave it up to the programmer to discover the
9279                  true cause and fix their mistake.  */
9280               || (inst.operands[1].reg == REG_PC),
9281               BAD_ADDR_MODE);
9282
9283   constraint (inst.relocs[0].exp.X_op != O_constant
9284               || inst.relocs[0].exp.X_add_number != 0,
9285               _("offset must be zero in ARM encoding"));
9286
9287   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9288
9289   inst.instruction |= inst.operands[0].reg << 12;
9290   inst.instruction |= inst.operands[1].reg << 16;
9291   inst.relocs[0].type = BFD_RELOC_UNUSED;
9292 }
9293
9294 static void
9295 do_ldrexd (void)
9296 {
9297   constraint (inst.operands[0].reg % 2 != 0,
9298               _("even register required"));
9299   constraint (inst.operands[1].present
9300               && inst.operands[1].reg != inst.operands[0].reg + 1,
9301               _("can only load two consecutive registers"));
9302   /* If op 1 were present and equal to PC, this function wouldn't
9303      have been called in the first place.  */
9304   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9305
9306   inst.instruction |= inst.operands[0].reg << 12;
9307   inst.instruction |= inst.operands[2].reg << 16;
9308 }
9309
9310 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9311    which is not a multiple of four is UNPREDICTABLE.  */
9312 static void
9313 check_ldr_r15_aligned (void)
9314 {
9315   constraint (!(inst.operands[1].immisreg)
9316               && (inst.operands[0].reg == REG_PC
9317               && inst.operands[1].reg == REG_PC
9318               && (inst.relocs[0].exp.X_add_number & 0x3)),
9319               _("ldr to register 15 must be 4-byte aligned"));
9320 }
9321
9322 static void
9323 do_ldst (void)
9324 {
9325   inst.instruction |= inst.operands[0].reg << 12;
9326   if (!inst.operands[1].isreg)
9327     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9328       return;
9329   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9330   check_ldr_r15_aligned ();
9331 }
9332
9333 static void
9334 do_ldstt (void)
9335 {
9336   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9337      reject [Rn,...].  */
9338   if (inst.operands[1].preind)
9339     {
9340       constraint (inst.relocs[0].exp.X_op != O_constant
9341                   || inst.relocs[0].exp.X_add_number != 0,
9342                   _("this instruction requires a post-indexed address"));
9343
9344       inst.operands[1].preind = 0;
9345       inst.operands[1].postind = 1;
9346       inst.operands[1].writeback = 1;
9347     }
9348   inst.instruction |= inst.operands[0].reg << 12;
9349   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9350 }
9351
9352 /* Halfword and signed-byte load/store operations.  */
9353
9354 static void
9355 do_ldstv4 (void)
9356 {
9357   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9358   inst.instruction |= inst.operands[0].reg << 12;
9359   if (!inst.operands[1].isreg)
9360     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9361       return;
9362   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9363 }
9364
9365 static void
9366 do_ldsttv4 (void)
9367 {
9368   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9369      reject [Rn,...].  */
9370   if (inst.operands[1].preind)
9371     {
9372       constraint (inst.relocs[0].exp.X_op != O_constant
9373                   || inst.relocs[0].exp.X_add_number != 0,
9374                   _("this instruction requires a post-indexed address"));
9375
9376       inst.operands[1].preind = 0;
9377       inst.operands[1].postind = 1;
9378       inst.operands[1].writeback = 1;
9379     }
9380   inst.instruction |= inst.operands[0].reg << 12;
9381   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9382 }
9383
9384 /* Co-processor register load/store.
9385    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9386 static void
9387 do_lstc (void)
9388 {
9389   inst.instruction |= inst.operands[0].reg << 8;
9390   inst.instruction |= inst.operands[1].reg << 12;
9391   encode_arm_cp_address (2, TRUE, TRUE, 0);
9392 }
9393
9394 static void
9395 do_mlas (void)
9396 {
9397   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9398   if (inst.operands[0].reg == inst.operands[1].reg
9399       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9400       && !(inst.instruction & 0x00400000))
9401     as_tsktsk (_("Rd and Rm should be different in mla"));
9402
9403   inst.instruction |= inst.operands[0].reg << 16;
9404   inst.instruction |= inst.operands[1].reg;
9405   inst.instruction |= inst.operands[2].reg << 8;
9406   inst.instruction |= inst.operands[3].reg << 12;
9407 }
9408
9409 static void
9410 do_mov (void)
9411 {
9412   constraint (inst.relocs[0].type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9413               && inst.relocs[0].type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9414               THUMB1_RELOC_ONLY);
9415   inst.instruction |= inst.operands[0].reg << 12;
9416   encode_arm_shifter_operand (1);
9417 }
9418
9419 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9420 static void
9421 do_mov16 (void)
9422 {
9423   bfd_vma imm;
9424   bfd_boolean top;
9425
9426   top = (inst.instruction & 0x00400000) != 0;
9427   constraint (top && inst.relocs[0].type == BFD_RELOC_ARM_MOVW,
9428               _(":lower16: not allowed in this instruction"));
9429   constraint (!top && inst.relocs[0].type == BFD_RELOC_ARM_MOVT,
9430               _(":upper16: not allowed in this instruction"));
9431   inst.instruction |= inst.operands[0].reg << 12;
9432   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
9433     {
9434       imm = inst.relocs[0].exp.X_add_number;
9435       /* The value is in two pieces: 0:11, 16:19.  */
9436       inst.instruction |= (imm & 0x00000fff);
9437       inst.instruction |= (imm & 0x0000f000) << 4;
9438     }
9439 }
9440
9441 static int
9442 do_vfp_nsyn_mrs (void)
9443 {
9444   if (inst.operands[0].isvec)
9445     {
9446       if (inst.operands[1].reg != 1)
9447         first_error (_("operand 1 must be FPSCR"));
9448       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9449       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9450       do_vfp_nsyn_opcode ("fmstat");
9451     }
9452   else if (inst.operands[1].isvec)
9453     do_vfp_nsyn_opcode ("fmrx");
9454   else
9455     return FAIL;
9456
9457   return SUCCESS;
9458 }
9459
9460 static int
9461 do_vfp_nsyn_msr (void)
9462 {
9463   if (inst.operands[0].isvec)
9464     do_vfp_nsyn_opcode ("fmxr");
9465   else
9466     return FAIL;
9467
9468   return SUCCESS;
9469 }
9470
9471 static void
9472 do_vmrs (void)
9473 {
9474   unsigned Rt = inst.operands[0].reg;
9475
9476   if (thumb_mode && Rt == REG_SP)
9477     {
9478       inst.error = BAD_SP;
9479       return;
9480     }
9481
9482   /* MVFR2 is only valid at ARMv8-A.  */
9483   if (inst.operands[1].reg == 5)
9484     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9485                 _(BAD_FPU));
9486
9487   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9488   if (!inst.operands[0].isvec && Rt == REG_PC)
9489     {
9490       inst.error = BAD_PC;
9491       return;
9492     }
9493
9494   /* If we get through parsing the register name, we just insert the number
9495      generated into the instruction without further validation.  */
9496   inst.instruction |= (inst.operands[1].reg << 16);
9497   inst.instruction |= (Rt << 12);
9498 }
9499
9500 static void
9501 do_vmsr (void)
9502 {
9503   unsigned Rt = inst.operands[1].reg;
9504
9505   if (thumb_mode)
9506     reject_bad_reg (Rt);
9507   else if (Rt == REG_PC)
9508     {
9509       inst.error = BAD_PC;
9510       return;
9511     }
9512
9513   /* MVFR2 is only valid for ARMv8-A.  */
9514   if (inst.operands[0].reg == 5)
9515     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9516                 _(BAD_FPU));
9517
9518   /* If we get through parsing the register name, we just insert the number
9519      generated into the instruction without further validation.  */
9520   inst.instruction |= (inst.operands[0].reg << 16);
9521   inst.instruction |= (Rt << 12);
9522 }
9523
9524 static void
9525 do_mrs (void)
9526 {
9527   unsigned br;
9528
9529   if (do_vfp_nsyn_mrs () == SUCCESS)
9530     return;
9531
9532   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9533   inst.instruction |= inst.operands[0].reg << 12;
9534
9535   if (inst.operands[1].isreg)
9536     {
9537       br = inst.operands[1].reg;
9538       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
9539         as_bad (_("bad register for mrs"));
9540     }
9541   else
9542     {
9543       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9544       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9545                   != (PSR_c|PSR_f),
9546                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9547       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9548     }
9549
9550   inst.instruction |= br;
9551 }
9552
9553 /* Two possible forms:
9554       "{C|S}PSR_<field>, Rm",
9555       "{C|S}PSR_f, #expression".  */
9556
9557 static void
9558 do_msr (void)
9559 {
9560   if (do_vfp_nsyn_msr () == SUCCESS)
9561     return;
9562
9563   inst.instruction |= inst.operands[0].imm;
9564   if (inst.operands[1].isreg)
9565     inst.instruction |= inst.operands[1].reg;
9566   else
9567     {
9568       inst.instruction |= INST_IMMEDIATE;
9569       inst.relocs[0].type = BFD_RELOC_ARM_IMMEDIATE;
9570       inst.relocs[0].pc_rel = 0;
9571     }
9572 }
9573
9574 static void
9575 do_mul (void)
9576 {
9577   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9578
9579   if (!inst.operands[2].present)
9580     inst.operands[2].reg = inst.operands[0].reg;
9581   inst.instruction |= inst.operands[0].reg << 16;
9582   inst.instruction |= inst.operands[1].reg;
9583   inst.instruction |= inst.operands[2].reg << 8;
9584
9585   if (inst.operands[0].reg == inst.operands[1].reg
9586       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9587     as_tsktsk (_("Rd and Rm should be different in mul"));
9588 }
9589
9590 /* Long Multiply Parser
9591    UMULL RdLo, RdHi, Rm, Rs
9592    SMULL RdLo, RdHi, Rm, Rs
9593    UMLAL RdLo, RdHi, Rm, Rs
9594    SMLAL RdLo, RdHi, Rm, Rs.  */
9595
9596 static void
9597 do_mull (void)
9598 {
9599   inst.instruction |= inst.operands[0].reg << 12;
9600   inst.instruction |= inst.operands[1].reg << 16;
9601   inst.instruction |= inst.operands[2].reg;
9602   inst.instruction |= inst.operands[3].reg << 8;
9603
9604   /* rdhi and rdlo must be different.  */
9605   if (inst.operands[0].reg == inst.operands[1].reg)
9606     as_tsktsk (_("rdhi and rdlo must be different"));
9607
9608   /* rdhi, rdlo and rm must all be different before armv6.  */
9609   if ((inst.operands[0].reg == inst.operands[2].reg
9610       || inst.operands[1].reg == inst.operands[2].reg)
9611       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9612     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9613 }
9614
9615 static void
9616 do_nop (void)
9617 {
9618   if (inst.operands[0].present
9619       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9620     {
9621       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9622       inst.instruction &= 0xf0000000;
9623       inst.instruction |= 0x0320f000;
9624       if (inst.operands[0].present)
9625         inst.instruction |= inst.operands[0].imm;
9626     }
9627 }
9628
9629 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9630    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9631    Condition defaults to COND_ALWAYS.
9632    Error if Rd, Rn or Rm are R15.  */
9633
9634 static void
9635 do_pkhbt (void)
9636 {
9637   inst.instruction |= inst.operands[0].reg << 12;
9638   inst.instruction |= inst.operands[1].reg << 16;
9639   inst.instruction |= inst.operands[2].reg;
9640   if (inst.operands[3].present)
9641     encode_arm_shift (3);
9642 }
9643
9644 /* ARM V6 PKHTB (Argument Parse).  */
9645
9646 static void
9647 do_pkhtb (void)
9648 {
9649   if (!inst.operands[3].present)
9650     {
9651       /* If the shift specifier is omitted, turn the instruction
9652          into pkhbt rd, rm, rn. */
9653       inst.instruction &= 0xfff00010;
9654       inst.instruction |= inst.operands[0].reg << 12;
9655       inst.instruction |= inst.operands[1].reg;
9656       inst.instruction |= inst.operands[2].reg << 16;
9657     }
9658   else
9659     {
9660       inst.instruction |= inst.operands[0].reg << 12;
9661       inst.instruction |= inst.operands[1].reg << 16;
9662       inst.instruction |= inst.operands[2].reg;
9663       encode_arm_shift (3);
9664     }
9665 }
9666
9667 /* ARMv5TE: Preload-Cache
9668    MP Extensions: Preload for write
9669
9670     PLD(W) <addr_mode>
9671
9672   Syntactically, like LDR with B=1, W=0, L=1.  */
9673
9674 static void
9675 do_pld (void)
9676 {
9677   constraint (!inst.operands[0].isreg,
9678               _("'[' expected after PLD mnemonic"));
9679   constraint (inst.operands[0].postind,
9680               _("post-indexed expression used in preload instruction"));
9681   constraint (inst.operands[0].writeback,
9682               _("writeback used in preload instruction"));
9683   constraint (!inst.operands[0].preind,
9684               _("unindexed addressing used in preload instruction"));
9685   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9686 }
9687
9688 /* ARMv7: PLI <addr_mode>  */
9689 static void
9690 do_pli (void)
9691 {
9692   constraint (!inst.operands[0].isreg,
9693               _("'[' expected after PLI mnemonic"));
9694   constraint (inst.operands[0].postind,
9695               _("post-indexed expression used in preload instruction"));
9696   constraint (inst.operands[0].writeback,
9697               _("writeback used in preload instruction"));
9698   constraint (!inst.operands[0].preind,
9699               _("unindexed addressing used in preload instruction"));
9700   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9701   inst.instruction &= ~PRE_INDEX;
9702 }
9703
9704 static void
9705 do_push_pop (void)
9706 {
9707   constraint (inst.operands[0].writeback,
9708               _("push/pop do not support {reglist}^"));
9709   inst.operands[1] = inst.operands[0];
9710   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9711   inst.operands[0].isreg = 1;
9712   inst.operands[0].writeback = 1;
9713   inst.operands[0].reg = REG_SP;
9714   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9715 }
9716
9717 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9718    word at the specified address and the following word
9719    respectively.
9720    Unconditionally executed.
9721    Error if Rn is R15.  */
9722
9723 static void
9724 do_rfe (void)
9725 {
9726   inst.instruction |= inst.operands[0].reg << 16;
9727   if (inst.operands[0].writeback)
9728     inst.instruction |= WRITE_BACK;
9729 }
9730
9731 /* ARM V6 ssat (argument parse).  */
9732
9733 static void
9734 do_ssat (void)
9735 {
9736   inst.instruction |= inst.operands[0].reg << 12;
9737   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9738   inst.instruction |= inst.operands[2].reg;
9739
9740   if (inst.operands[3].present)
9741     encode_arm_shift (3);
9742 }
9743
9744 /* ARM V6 usat (argument parse).  */
9745
9746 static void
9747 do_usat (void)
9748 {
9749   inst.instruction |= inst.operands[0].reg << 12;
9750   inst.instruction |= inst.operands[1].imm << 16;
9751   inst.instruction |= inst.operands[2].reg;
9752
9753   if (inst.operands[3].present)
9754     encode_arm_shift (3);
9755 }
9756
9757 /* ARM V6 ssat16 (argument parse).  */
9758
9759 static void
9760 do_ssat16 (void)
9761 {
9762   inst.instruction |= inst.operands[0].reg << 12;
9763   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9764   inst.instruction |= inst.operands[2].reg;
9765 }
9766
9767 static void
9768 do_usat16 (void)
9769 {
9770   inst.instruction |= inst.operands[0].reg << 12;
9771   inst.instruction |= inst.operands[1].imm << 16;
9772   inst.instruction |= inst.operands[2].reg;
9773 }
9774
9775 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9776    preserving the other bits.
9777
9778    setend <endian_specifier>, where <endian_specifier> is either
9779    BE or LE.  */
9780
9781 static void
9782 do_setend (void)
9783 {
9784   if (warn_on_deprecated
9785       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9786       as_tsktsk (_("setend use is deprecated for ARMv8"));
9787
9788   if (inst.operands[0].imm)
9789     inst.instruction |= 0x200;
9790 }
9791
9792 static void
9793 do_shift (void)
9794 {
9795   unsigned int Rm = (inst.operands[1].present
9796                      ? inst.operands[1].reg
9797                      : inst.operands[0].reg);
9798
9799   inst.instruction |= inst.operands[0].reg << 12;
9800   inst.instruction |= Rm;
9801   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9802     {
9803       inst.instruction |= inst.operands[2].reg << 8;
9804       inst.instruction |= SHIFT_BY_REG;
9805       /* PR 12854: Error on extraneous shifts.  */
9806       constraint (inst.operands[2].shifted,
9807                   _("extraneous shift as part of operand to shift insn"));
9808     }
9809   else
9810     inst.relocs[0].type = BFD_RELOC_ARM_SHIFT_IMM;
9811 }
9812
9813 static void
9814 do_smc (void)
9815 {
9816   inst.relocs[0].type = BFD_RELOC_ARM_SMC;
9817   inst.relocs[0].pc_rel = 0;
9818 }
9819
9820 static void
9821 do_hvc (void)
9822 {
9823   inst.relocs[0].type = BFD_RELOC_ARM_HVC;
9824   inst.relocs[0].pc_rel = 0;
9825 }
9826
9827 static void
9828 do_swi (void)
9829 {
9830   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
9831   inst.relocs[0].pc_rel = 0;
9832 }
9833
9834 static void
9835 do_setpan (void)
9836 {
9837   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9838               _("selected processor does not support SETPAN instruction"));
9839
9840   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9841 }
9842
9843 static void
9844 do_t_setpan (void)
9845 {
9846   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9847               _("selected processor does not support SETPAN instruction"));
9848
9849   inst.instruction |= (inst.operands[0].imm << 3);
9850 }
9851
9852 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9853    SMLAxy{cond} Rd,Rm,Rs,Rn
9854    SMLAWy{cond} Rd,Rm,Rs,Rn
9855    Error if any register is R15.  */
9856
9857 static void
9858 do_smla (void)
9859 {
9860   inst.instruction |= inst.operands[0].reg << 16;
9861   inst.instruction |= inst.operands[1].reg;
9862   inst.instruction |= inst.operands[2].reg << 8;
9863   inst.instruction |= inst.operands[3].reg << 12;
9864 }
9865
9866 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9867    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9868    Error if any register is R15.
9869    Warning if Rdlo == Rdhi.  */
9870
9871 static void
9872 do_smlal (void)
9873 {
9874   inst.instruction |= inst.operands[0].reg << 12;
9875   inst.instruction |= inst.operands[1].reg << 16;
9876   inst.instruction |= inst.operands[2].reg;
9877   inst.instruction |= inst.operands[3].reg << 8;
9878
9879   if (inst.operands[0].reg == inst.operands[1].reg)
9880     as_tsktsk (_("rdhi and rdlo must be different"));
9881 }
9882
9883 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9884    SMULxy{cond} Rd,Rm,Rs
9885    Error if any register is R15.  */
9886
9887 static void
9888 do_smul (void)
9889 {
9890   inst.instruction |= inst.operands[0].reg << 16;
9891   inst.instruction |= inst.operands[1].reg;
9892   inst.instruction |= inst.operands[2].reg << 8;
9893 }
9894
9895 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9896    the same for both ARM and Thumb-2.  */
9897
9898 static void
9899 do_srs (void)
9900 {
9901   int reg;
9902
9903   if (inst.operands[0].present)
9904     {
9905       reg = inst.operands[0].reg;
9906       constraint (reg != REG_SP, _("SRS base register must be r13"));
9907     }
9908   else
9909     reg = REG_SP;
9910
9911   inst.instruction |= reg << 16;
9912   inst.instruction |= inst.operands[1].imm;
9913   if (inst.operands[0].writeback || inst.operands[1].writeback)
9914     inst.instruction |= WRITE_BACK;
9915 }
9916
9917 /* ARM V6 strex (argument parse).  */
9918
9919 static void
9920 do_strex (void)
9921 {
9922   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9923               || inst.operands[2].postind || inst.operands[2].writeback
9924               || inst.operands[2].immisreg || inst.operands[2].shifted
9925               || inst.operands[2].negative
9926               /* See comment in do_ldrex().  */
9927               || (inst.operands[2].reg == REG_PC),
9928               BAD_ADDR_MODE);
9929
9930   constraint (inst.operands[0].reg == inst.operands[1].reg
9931               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9932
9933   constraint (inst.relocs[0].exp.X_op != O_constant
9934               || inst.relocs[0].exp.X_add_number != 0,
9935               _("offset must be zero in ARM encoding"));
9936
9937   inst.instruction |= inst.operands[0].reg << 12;
9938   inst.instruction |= inst.operands[1].reg;
9939   inst.instruction |= inst.operands[2].reg << 16;
9940   inst.relocs[0].type = BFD_RELOC_UNUSED;
9941 }
9942
9943 static void
9944 do_t_strexbh (void)
9945 {
9946   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9947               || inst.operands[2].postind || inst.operands[2].writeback
9948               || inst.operands[2].immisreg || inst.operands[2].shifted
9949               || inst.operands[2].negative,
9950               BAD_ADDR_MODE);
9951
9952   constraint (inst.operands[0].reg == inst.operands[1].reg
9953               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9954
9955   do_rm_rd_rn ();
9956 }
9957
9958 static void
9959 do_strexd (void)
9960 {
9961   constraint (inst.operands[1].reg % 2 != 0,
9962               _("even register required"));
9963   constraint (inst.operands[2].present
9964               && inst.operands[2].reg != inst.operands[1].reg + 1,
9965               _("can only store two consecutive registers"));
9966   /* If op 2 were present and equal to PC, this function wouldn't
9967      have been called in the first place.  */
9968   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9969
9970   constraint (inst.operands[0].reg == inst.operands[1].reg
9971               || inst.operands[0].reg == inst.operands[1].reg + 1
9972               || inst.operands[0].reg == inst.operands[3].reg,
9973               BAD_OVERLAP);
9974
9975   inst.instruction |= inst.operands[0].reg << 12;
9976   inst.instruction |= inst.operands[1].reg;
9977   inst.instruction |= inst.operands[3].reg << 16;
9978 }
9979
9980 /* ARM V8 STRL.  */
9981 static void
9982 do_stlex (void)
9983 {
9984   constraint (inst.operands[0].reg == inst.operands[1].reg
9985               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9986
9987   do_rd_rm_rn ();
9988 }
9989
9990 static void
9991 do_t_stlex (void)
9992 {
9993   constraint (inst.operands[0].reg == inst.operands[1].reg
9994               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9995
9996   do_rm_rd_rn ();
9997 }
9998
9999 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
10000    extends it to 32-bits, and adds the result to a value in another
10001    register.  You can specify a rotation by 0, 8, 16, or 24 bits
10002    before extracting the 16-bit value.
10003    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
10004    Condition defaults to COND_ALWAYS.
10005    Error if any register uses R15.  */
10006
10007 static void
10008 do_sxtah (void)
10009 {
10010   inst.instruction |= inst.operands[0].reg << 12;
10011   inst.instruction |= inst.operands[1].reg << 16;
10012   inst.instruction |= inst.operands[2].reg;
10013   inst.instruction |= inst.operands[3].imm << 10;
10014 }
10015
10016 /* ARM V6 SXTH.
10017
10018    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
10019    Condition defaults to COND_ALWAYS.
10020    Error if any register uses R15.  */
10021
10022 static void
10023 do_sxth (void)
10024 {
10025   inst.instruction |= inst.operands[0].reg << 12;
10026   inst.instruction |= inst.operands[1].reg;
10027   inst.instruction |= inst.operands[2].imm << 10;
10028 }
10029 \f
10030 /* VFP instructions.  In a logical order: SP variant first, monad
10031    before dyad, arithmetic then move then load/store.  */
10032
10033 static void
10034 do_vfp_sp_monadic (void)
10035 {
10036   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10037   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10038 }
10039
10040 static void
10041 do_vfp_sp_dyadic (void)
10042 {
10043   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10044   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10045   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10046 }
10047
10048 static void
10049 do_vfp_sp_compare_z (void)
10050 {
10051   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10052 }
10053
10054 static void
10055 do_vfp_dp_sp_cvt (void)
10056 {
10057   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10058   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
10059 }
10060
10061 static void
10062 do_vfp_sp_dp_cvt (void)
10063 {
10064   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10065   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10066 }
10067
10068 static void
10069 do_vfp_reg_from_sp (void)
10070 {
10071   inst.instruction |= inst.operands[0].reg << 12;
10072   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
10073 }
10074
10075 static void
10076 do_vfp_reg2_from_sp2 (void)
10077 {
10078   constraint (inst.operands[2].imm != 2,
10079               _("only two consecutive VFP SP registers allowed here"));
10080   inst.instruction |= inst.operands[0].reg << 12;
10081   inst.instruction |= inst.operands[1].reg << 16;
10082   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
10083 }
10084
10085 static void
10086 do_vfp_sp_from_reg (void)
10087 {
10088   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
10089   inst.instruction |= inst.operands[1].reg << 12;
10090 }
10091
10092 static void
10093 do_vfp_sp2_from_reg2 (void)
10094 {
10095   constraint (inst.operands[0].imm != 2,
10096               _("only two consecutive VFP SP registers allowed here"));
10097   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
10098   inst.instruction |= inst.operands[1].reg << 12;
10099   inst.instruction |= inst.operands[2].reg << 16;
10100 }
10101
10102 static void
10103 do_vfp_sp_ldst (void)
10104 {
10105   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10106   encode_arm_cp_address (1, FALSE, TRUE, 0);
10107 }
10108
10109 static void
10110 do_vfp_dp_ldst (void)
10111 {
10112   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10113   encode_arm_cp_address (1, FALSE, TRUE, 0);
10114 }
10115
10116
10117 static void
10118 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
10119 {
10120   if (inst.operands[0].writeback)
10121     inst.instruction |= WRITE_BACK;
10122   else
10123     constraint (ldstm_type != VFP_LDSTMIA,
10124                 _("this addressing mode requires base-register writeback"));
10125   inst.instruction |= inst.operands[0].reg << 16;
10126   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
10127   inst.instruction |= inst.operands[1].imm;
10128 }
10129
10130 static void
10131 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
10132 {
10133   int count;
10134
10135   if (inst.operands[0].writeback)
10136     inst.instruction |= WRITE_BACK;
10137   else
10138     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
10139                 _("this addressing mode requires base-register writeback"));
10140
10141   inst.instruction |= inst.operands[0].reg << 16;
10142   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10143
10144   count = inst.operands[1].imm << 1;
10145   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
10146     count += 1;
10147
10148   inst.instruction |= count;
10149 }
10150
10151 static void
10152 do_vfp_sp_ldstmia (void)
10153 {
10154   vfp_sp_ldstm (VFP_LDSTMIA);
10155 }
10156
10157 static void
10158 do_vfp_sp_ldstmdb (void)
10159 {
10160   vfp_sp_ldstm (VFP_LDSTMDB);
10161 }
10162
10163 static void
10164 do_vfp_dp_ldstmia (void)
10165 {
10166   vfp_dp_ldstm (VFP_LDSTMIA);
10167 }
10168
10169 static void
10170 do_vfp_dp_ldstmdb (void)
10171 {
10172   vfp_dp_ldstm (VFP_LDSTMDB);
10173 }
10174
10175 static void
10176 do_vfp_xp_ldstmia (void)
10177 {
10178   vfp_dp_ldstm (VFP_LDSTMIAX);
10179 }
10180
10181 static void
10182 do_vfp_xp_ldstmdb (void)
10183 {
10184   vfp_dp_ldstm (VFP_LDSTMDBX);
10185 }
10186
10187 static void
10188 do_vfp_dp_rd_rm (void)
10189 {
10190   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10191   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
10192 }
10193
10194 static void
10195 do_vfp_dp_rn_rd (void)
10196 {
10197   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
10198   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10199 }
10200
10201 static void
10202 do_vfp_dp_rd_rn (void)
10203 {
10204   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10205   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10206 }
10207
10208 static void
10209 do_vfp_dp_rd_rn_rm (void)
10210 {
10211   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10212   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
10213   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
10214 }
10215
10216 static void
10217 do_vfp_dp_rd (void)
10218 {
10219   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10220 }
10221
10222 static void
10223 do_vfp_dp_rm_rd_rn (void)
10224 {
10225   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
10226   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
10227   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
10228 }
10229
10230 /* VFPv3 instructions.  */
10231 static void
10232 do_vfp_sp_const (void)
10233 {
10234   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10235   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10236   inst.instruction |= (inst.operands[1].imm & 0x0f);
10237 }
10238
10239 static void
10240 do_vfp_dp_const (void)
10241 {
10242   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10243   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
10244   inst.instruction |= (inst.operands[1].imm & 0x0f);
10245 }
10246
10247 static void
10248 vfp_conv (int srcsize)
10249 {
10250   int immbits = srcsize - inst.operands[1].imm;
10251
10252   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
10253     {
10254       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
10255          i.e. immbits must be in range 0 - 16.  */
10256       inst.error = _("immediate value out of range, expected range [0, 16]");
10257       return;
10258     }
10259   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
10260     {
10261       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
10262          i.e. immbits must be in range 0 - 31.  */
10263       inst.error = _("immediate value out of range, expected range [1, 32]");
10264       return;
10265     }
10266
10267   inst.instruction |= (immbits & 1) << 5;
10268   inst.instruction |= (immbits >> 1);
10269 }
10270
10271 static void
10272 do_vfp_sp_conv_16 (void)
10273 {
10274   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10275   vfp_conv (16);
10276 }
10277
10278 static void
10279 do_vfp_dp_conv_16 (void)
10280 {
10281   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10282   vfp_conv (16);
10283 }
10284
10285 static void
10286 do_vfp_sp_conv_32 (void)
10287 {
10288   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10289   vfp_conv (32);
10290 }
10291
10292 static void
10293 do_vfp_dp_conv_32 (void)
10294 {
10295   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10296   vfp_conv (32);
10297 }
10298 \f
10299 /* FPA instructions.  Also in a logical order.  */
10300
10301 static void
10302 do_fpa_cmp (void)
10303 {
10304   inst.instruction |= inst.operands[0].reg << 16;
10305   inst.instruction |= inst.operands[1].reg;
10306 }
10307
10308 static void
10309 do_fpa_ldmstm (void)
10310 {
10311   inst.instruction |= inst.operands[0].reg << 12;
10312   switch (inst.operands[1].imm)
10313     {
10314     case 1: inst.instruction |= CP_T_X;          break;
10315     case 2: inst.instruction |= CP_T_Y;          break;
10316     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10317     case 4:                                      break;
10318     default: abort ();
10319     }
10320
10321   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10322     {
10323       /* The instruction specified "ea" or "fd", so we can only accept
10324          [Rn]{!}.  The instruction does not really support stacking or
10325          unstacking, so we have to emulate these by setting appropriate
10326          bits and offsets.  */
10327       constraint (inst.relocs[0].exp.X_op != O_constant
10328                   || inst.relocs[0].exp.X_add_number != 0,
10329                   _("this instruction does not support indexing"));
10330
10331       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10332         inst.relocs[0].exp.X_add_number = 12 * inst.operands[1].imm;
10333
10334       if (!(inst.instruction & INDEX_UP))
10335         inst.relocs[0].exp.X_add_number = -inst.relocs[0].exp.X_add_number;
10336
10337       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10338         {
10339           inst.operands[2].preind = 0;
10340           inst.operands[2].postind = 1;
10341         }
10342     }
10343
10344   encode_arm_cp_address (2, TRUE, TRUE, 0);
10345 }
10346 \f
10347 /* iWMMXt instructions: strictly in alphabetical order.  */
10348
10349 static void
10350 do_iwmmxt_tandorc (void)
10351 {
10352   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10353 }
10354
10355 static void
10356 do_iwmmxt_textrc (void)
10357 {
10358   inst.instruction |= inst.operands[0].reg << 12;
10359   inst.instruction |= inst.operands[1].imm;
10360 }
10361
10362 static void
10363 do_iwmmxt_textrm (void)
10364 {
10365   inst.instruction |= inst.operands[0].reg << 12;
10366   inst.instruction |= inst.operands[1].reg << 16;
10367   inst.instruction |= inst.operands[2].imm;
10368 }
10369
10370 static void
10371 do_iwmmxt_tinsr (void)
10372 {
10373   inst.instruction |= inst.operands[0].reg << 16;
10374   inst.instruction |= inst.operands[1].reg << 12;
10375   inst.instruction |= inst.operands[2].imm;
10376 }
10377
10378 static void
10379 do_iwmmxt_tmia (void)
10380 {
10381   inst.instruction |= inst.operands[0].reg << 5;
10382   inst.instruction |= inst.operands[1].reg;
10383   inst.instruction |= inst.operands[2].reg << 12;
10384 }
10385
10386 static void
10387 do_iwmmxt_waligni (void)
10388 {
10389   inst.instruction |= inst.operands[0].reg << 12;
10390   inst.instruction |= inst.operands[1].reg << 16;
10391   inst.instruction |= inst.operands[2].reg;
10392   inst.instruction |= inst.operands[3].imm << 20;
10393 }
10394
10395 static void
10396 do_iwmmxt_wmerge (void)
10397 {
10398   inst.instruction |= inst.operands[0].reg << 12;
10399   inst.instruction |= inst.operands[1].reg << 16;
10400   inst.instruction |= inst.operands[2].reg;
10401   inst.instruction |= inst.operands[3].imm << 21;
10402 }
10403
10404 static void
10405 do_iwmmxt_wmov (void)
10406 {
10407   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10408   inst.instruction |= inst.operands[0].reg << 12;
10409   inst.instruction |= inst.operands[1].reg << 16;
10410   inst.instruction |= inst.operands[1].reg;
10411 }
10412
10413 static void
10414 do_iwmmxt_wldstbh (void)
10415 {
10416   int reloc;
10417   inst.instruction |= inst.operands[0].reg << 12;
10418   if (thumb_mode)
10419     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10420   else
10421     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10422   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10423 }
10424
10425 static void
10426 do_iwmmxt_wldstw (void)
10427 {
10428   /* RIWR_RIWC clears .isreg for a control register.  */
10429   if (!inst.operands[0].isreg)
10430     {
10431       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10432       inst.instruction |= 0xf0000000;
10433     }
10434
10435   inst.instruction |= inst.operands[0].reg << 12;
10436   encode_arm_cp_address (1, TRUE, TRUE, 0);
10437 }
10438
10439 static void
10440 do_iwmmxt_wldstd (void)
10441 {
10442   inst.instruction |= inst.operands[0].reg << 12;
10443   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10444       && inst.operands[1].immisreg)
10445     {
10446       inst.instruction &= ~0x1a000ff;
10447       inst.instruction |= (0xfU << 28);
10448       if (inst.operands[1].preind)
10449         inst.instruction |= PRE_INDEX;
10450       if (!inst.operands[1].negative)
10451         inst.instruction |= INDEX_UP;
10452       if (inst.operands[1].writeback)
10453         inst.instruction |= WRITE_BACK;
10454       inst.instruction |= inst.operands[1].reg << 16;
10455       inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
10456       inst.instruction |= inst.operands[1].imm;
10457     }
10458   else
10459     encode_arm_cp_address (1, TRUE, FALSE, 0);
10460 }
10461
10462 static void
10463 do_iwmmxt_wshufh (void)
10464 {
10465   inst.instruction |= inst.operands[0].reg << 12;
10466   inst.instruction |= inst.operands[1].reg << 16;
10467   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10468   inst.instruction |= (inst.operands[2].imm & 0x0f);
10469 }
10470
10471 static void
10472 do_iwmmxt_wzero (void)
10473 {
10474   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10475   inst.instruction |= inst.operands[0].reg;
10476   inst.instruction |= inst.operands[0].reg << 12;
10477   inst.instruction |= inst.operands[0].reg << 16;
10478 }
10479
10480 static void
10481 do_iwmmxt_wrwrwr_or_imm5 (void)
10482 {
10483   if (inst.operands[2].isreg)
10484     do_rd_rn_rm ();
10485   else {
10486     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10487                 _("immediate operand requires iWMMXt2"));
10488     do_rd_rn ();
10489     if (inst.operands[2].imm == 0)
10490       {
10491         switch ((inst.instruction >> 20) & 0xf)
10492           {
10493           case 4:
10494           case 5:
10495           case 6:
10496           case 7:
10497             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10498             inst.operands[2].imm = 16;
10499             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10500             break;
10501           case 8:
10502           case 9:
10503           case 10:
10504           case 11:
10505             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10506             inst.operands[2].imm = 32;
10507             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10508             break;
10509           case 12:
10510           case 13:
10511           case 14:
10512           case 15:
10513             {
10514               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10515               unsigned long wrn;
10516               wrn = (inst.instruction >> 16) & 0xf;
10517               inst.instruction &= 0xff0fff0f;
10518               inst.instruction |= wrn;
10519               /* Bail out here; the instruction is now assembled.  */
10520               return;
10521             }
10522           }
10523       }
10524     /* Map 32 -> 0, etc.  */
10525     inst.operands[2].imm &= 0x1f;
10526     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10527   }
10528 }
10529 \f
10530 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10531    operations first, then control, shift, and load/store.  */
10532
10533 /* Insns like "foo X,Y,Z".  */
10534
10535 static void
10536 do_mav_triple (void)
10537 {
10538   inst.instruction |= inst.operands[0].reg << 16;
10539   inst.instruction |= inst.operands[1].reg;
10540   inst.instruction |= inst.operands[2].reg << 12;
10541 }
10542
10543 /* Insns like "foo W,X,Y,Z".
10544     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10545
10546 static void
10547 do_mav_quad (void)
10548 {
10549   inst.instruction |= inst.operands[0].reg << 5;
10550   inst.instruction |= inst.operands[1].reg << 12;
10551   inst.instruction |= inst.operands[2].reg << 16;
10552   inst.instruction |= inst.operands[3].reg;
10553 }
10554
10555 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10556 static void
10557 do_mav_dspsc (void)
10558 {
10559   inst.instruction |= inst.operands[1].reg << 12;
10560 }
10561
10562 /* Maverick shift immediate instructions.
10563    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10564    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10565
10566 static void
10567 do_mav_shift (void)
10568 {
10569   int imm = inst.operands[2].imm;
10570
10571   inst.instruction |= inst.operands[0].reg << 12;
10572   inst.instruction |= inst.operands[1].reg << 16;
10573
10574   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10575      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10576      Bit 4 should be 0.  */
10577   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10578
10579   inst.instruction |= imm;
10580 }
10581 \f
10582 /* XScale instructions.  Also sorted arithmetic before move.  */
10583
10584 /* Xscale multiply-accumulate (argument parse)
10585      MIAcc   acc0,Rm,Rs
10586      MIAPHcc acc0,Rm,Rs
10587      MIAxycc acc0,Rm,Rs.  */
10588
10589 static void
10590 do_xsc_mia (void)
10591 {
10592   inst.instruction |= inst.operands[1].reg;
10593   inst.instruction |= inst.operands[2].reg << 12;
10594 }
10595
10596 /* Xscale move-accumulator-register (argument parse)
10597
10598      MARcc   acc0,RdLo,RdHi.  */
10599
10600 static void
10601 do_xsc_mar (void)
10602 {
10603   inst.instruction |= inst.operands[1].reg << 12;
10604   inst.instruction |= inst.operands[2].reg << 16;
10605 }
10606
10607 /* Xscale move-register-accumulator (argument parse)
10608
10609      MRAcc   RdLo,RdHi,acc0.  */
10610
10611 static void
10612 do_xsc_mra (void)
10613 {
10614   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10615   inst.instruction |= inst.operands[0].reg << 12;
10616   inst.instruction |= inst.operands[1].reg << 16;
10617 }
10618 \f
10619 /* Encoding functions relevant only to Thumb.  */
10620
10621 /* inst.operands[i] is a shifted-register operand; encode
10622    it into inst.instruction in the format used by Thumb32.  */
10623
10624 static void
10625 encode_thumb32_shifted_operand (int i)
10626 {
10627   unsigned int value = inst.relocs[0].exp.X_add_number;
10628   unsigned int shift = inst.operands[i].shift_kind;
10629
10630   constraint (inst.operands[i].immisreg,
10631               _("shift by register not allowed in thumb mode"));
10632   inst.instruction |= inst.operands[i].reg;
10633   if (shift == SHIFT_RRX)
10634     inst.instruction |= SHIFT_ROR << 4;
10635   else
10636     {
10637       constraint (inst.relocs[0].exp.X_op != O_constant,
10638                   _("expression too complex"));
10639
10640       constraint (value > 32
10641                   || (value == 32 && (shift == SHIFT_LSL
10642                                       || shift == SHIFT_ROR)),
10643                   _("shift expression is too large"));
10644
10645       if (value == 0)
10646         shift = SHIFT_LSL;
10647       else if (value == 32)
10648         value = 0;
10649
10650       inst.instruction |= shift << 4;
10651       inst.instruction |= (value & 0x1c) << 10;
10652       inst.instruction |= (value & 0x03) << 6;
10653     }
10654 }
10655
10656
10657 /* inst.operands[i] was set up by parse_address.  Encode it into a
10658    Thumb32 format load or store instruction.  Reject forms that cannot
10659    be used with such instructions.  If is_t is true, reject forms that
10660    cannot be used with a T instruction; if is_d is true, reject forms
10661    that cannot be used with a D instruction.  If it is a store insn,
10662    reject PC in Rn.  */
10663
10664 static void
10665 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10666 {
10667   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10668
10669   constraint (!inst.operands[i].isreg,
10670               _("Instruction does not support =N addresses"));
10671
10672   inst.instruction |= inst.operands[i].reg << 16;
10673   if (inst.operands[i].immisreg)
10674     {
10675       constraint (is_pc, BAD_PC_ADDRESSING);
10676       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10677       constraint (inst.operands[i].negative,
10678                   _("Thumb does not support negative register indexing"));
10679       constraint (inst.operands[i].postind,
10680                   _("Thumb does not support register post-indexing"));
10681       constraint (inst.operands[i].writeback,
10682                   _("Thumb does not support register indexing with writeback"));
10683       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10684                   _("Thumb supports only LSL in shifted register indexing"));
10685
10686       inst.instruction |= inst.operands[i].imm;
10687       if (inst.operands[i].shifted)
10688         {
10689           constraint (inst.relocs[0].exp.X_op != O_constant,
10690                       _("expression too complex"));
10691           constraint (inst.relocs[0].exp.X_add_number < 0
10692                       || inst.relocs[0].exp.X_add_number > 3,
10693                       _("shift out of range"));
10694           inst.instruction |= inst.relocs[0].exp.X_add_number << 4;
10695         }
10696       inst.relocs[0].type = BFD_RELOC_UNUSED;
10697     }
10698   else if (inst.operands[i].preind)
10699     {
10700       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10701       constraint (is_t && inst.operands[i].writeback,
10702                   _("cannot use writeback with this instruction"));
10703       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10704                   BAD_PC_ADDRESSING);
10705
10706       if (is_d)
10707         {
10708           inst.instruction |= 0x01000000;
10709           if (inst.operands[i].writeback)
10710             inst.instruction |= 0x00200000;
10711         }
10712       else
10713         {
10714           inst.instruction |= 0x00000c00;
10715           if (inst.operands[i].writeback)
10716             inst.instruction |= 0x00000100;
10717         }
10718       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10719     }
10720   else if (inst.operands[i].postind)
10721     {
10722       gas_assert (inst.operands[i].writeback);
10723       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10724       constraint (is_t, _("cannot use post-indexing with this instruction"));
10725
10726       if (is_d)
10727         inst.instruction |= 0x00200000;
10728       else
10729         inst.instruction |= 0x00000900;
10730       inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10731     }
10732   else /* unindexed - only for coprocessor */
10733     inst.error = _("instruction does not accept unindexed addressing");
10734 }
10735
10736 /* Table of Thumb instructions which exist in both 16- and 32-bit
10737    encodings (the latter only in post-V6T2 cores).  The index is the
10738    value used in the insns table below.  When there is more than one
10739    possible 16-bit encoding for the instruction, this table always
10740    holds variant (1).
10741    Also contains several pseudo-instructions used during relaxation.  */
10742 #define T16_32_TAB                              \
10743   X(_adc,   4140, eb400000),                    \
10744   X(_adcs,  4140, eb500000),                    \
10745   X(_add,   1c00, eb000000),                    \
10746   X(_adds,  1c00, eb100000),                    \
10747   X(_addi,  0000, f1000000),                    \
10748   X(_addis, 0000, f1100000),                    \
10749   X(_add_pc,000f, f20f0000),                    \
10750   X(_add_sp,000d, f10d0000),                    \
10751   X(_adr,   000f, f20f0000),                    \
10752   X(_and,   4000, ea000000),                    \
10753   X(_ands,  4000, ea100000),                    \
10754   X(_asr,   1000, fa40f000),                    \
10755   X(_asrs,  1000, fa50f000),                    \
10756   X(_b,     e000, f000b000),                    \
10757   X(_bcond, d000, f0008000),                    \
10758   X(_bf,    0000, f040e001),                    \
10759   X(_bfcsel,0000, f000e001),                    \
10760   X(_bfx,   0000, f060e001),                    \
10761   X(_bfl,   0000, f000c001),                    \
10762   X(_bflx,  0000, f070e001),                    \
10763   X(_bic,   4380, ea200000),                    \
10764   X(_bics,  4380, ea300000),                    \
10765   X(_cmn,   42c0, eb100f00),                    \
10766   X(_cmp,   2800, ebb00f00),                    \
10767   X(_cpsie, b660, f3af8400),                    \
10768   X(_cpsid, b670, f3af8600),                    \
10769   X(_cpy,   4600, ea4f0000),                    \
10770   X(_dec_sp,80dd, f1ad0d00),                    \
10771   X(_dls,   0000, f040e001),                    \
10772   X(_eor,   4040, ea800000),                    \
10773   X(_eors,  4040, ea900000),                    \
10774   X(_inc_sp,00dd, f10d0d00),                    \
10775   X(_ldmia, c800, e8900000),                    \
10776   X(_ldr,   6800, f8500000),                    \
10777   X(_ldrb,  7800, f8100000),                    \
10778   X(_ldrh,  8800, f8300000),                    \
10779   X(_ldrsb, 5600, f9100000),                    \
10780   X(_ldrsh, 5e00, f9300000),                    \
10781   X(_ldr_pc,4800, f85f0000),                    \
10782   X(_ldr_pc2,4800, f85f0000),                   \
10783   X(_ldr_sp,9800, f85d0000),                    \
10784   X(_le,    0000, f00fc001),                    \
10785   X(_lsl,   0000, fa00f000),                    \
10786   X(_lsls,  0000, fa10f000),                    \
10787   X(_lsr,   0800, fa20f000),                    \
10788   X(_lsrs,  0800, fa30f000),                    \
10789   X(_mov,   2000, ea4f0000),                    \
10790   X(_movs,  2000, ea5f0000),                    \
10791   X(_mul,   4340, fb00f000),                     \
10792   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10793   X(_mvn,   43c0, ea6f0000),                    \
10794   X(_mvns,  43c0, ea7f0000),                    \
10795   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10796   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10797   X(_orr,   4300, ea400000),                    \
10798   X(_orrs,  4300, ea500000),                    \
10799   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10800   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10801   X(_rev,   ba00, fa90f080),                    \
10802   X(_rev16, ba40, fa90f090),                    \
10803   X(_revsh, bac0, fa90f0b0),                    \
10804   X(_ror,   41c0, fa60f000),                    \
10805   X(_rors,  41c0, fa70f000),                    \
10806   X(_sbc,   4180, eb600000),                    \
10807   X(_sbcs,  4180, eb700000),                    \
10808   X(_stmia, c000, e8800000),                    \
10809   X(_str,   6000, f8400000),                    \
10810   X(_strb,  7000, f8000000),                    \
10811   X(_strh,  8000, f8200000),                    \
10812   X(_str_sp,9000, f84d0000),                    \
10813   X(_sub,   1e00, eba00000),                    \
10814   X(_subs,  1e00, ebb00000),                    \
10815   X(_subi,  8000, f1a00000),                    \
10816   X(_subis, 8000, f1b00000),                    \
10817   X(_sxtb,  b240, fa4ff080),                    \
10818   X(_sxth,  b200, fa0ff080),                    \
10819   X(_tst,   4200, ea100f00),                    \
10820   X(_uxtb,  b2c0, fa5ff080),                    \
10821   X(_uxth,  b280, fa1ff080),                    \
10822   X(_nop,   bf00, f3af8000),                    \
10823   X(_yield, bf10, f3af8001),                    \
10824   X(_wfe,   bf20, f3af8002),                    \
10825   X(_wfi,   bf30, f3af8003),                    \
10826   X(_wls,   0000, f040c001),                    \
10827   X(_sev,   bf40, f3af8004),                    \
10828   X(_sevl,  bf50, f3af8005),                    \
10829   X(_udf,   de00, f7f0a000)
10830
10831 /* To catch errors in encoding functions, the codes are all offset by
10832    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10833    as 16-bit instructions.  */
10834 #define X(a,b,c) T_MNEM##a
10835 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10836 #undef X
10837
10838 #define X(a,b,c) 0x##b
10839 static const unsigned short thumb_op16[] = { T16_32_TAB };
10840 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10841 #undef X
10842
10843 #define X(a,b,c) 0x##c
10844 static const unsigned int thumb_op32[] = { T16_32_TAB };
10845 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10846 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10847 #undef X
10848 #undef T16_32_TAB
10849
10850 /* Thumb instruction encoders, in alphabetical order.  */
10851
10852 /* ADDW or SUBW.  */
10853
10854 static void
10855 do_t_add_sub_w (void)
10856 {
10857   int Rd, Rn;
10858
10859   Rd = inst.operands[0].reg;
10860   Rn = inst.operands[1].reg;
10861
10862   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10863      is the SP-{plus,minus}-immediate form of the instruction.  */
10864   if (Rn == REG_SP)
10865     constraint (Rd == REG_PC, BAD_PC);
10866   else
10867     reject_bad_reg (Rd);
10868
10869   inst.instruction |= (Rn << 16) | (Rd << 8);
10870   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
10871 }
10872
10873 /* Parse an add or subtract instruction.  We get here with inst.instruction
10874    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10875
10876 static void
10877 do_t_add_sub (void)
10878 {
10879   int Rd, Rs, Rn;
10880
10881   Rd = inst.operands[0].reg;
10882   Rs = (inst.operands[1].present
10883         ? inst.operands[1].reg    /* Rd, Rs, foo */
10884         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10885
10886   if (Rd == REG_PC)
10887     set_pred_insn_type_last ();
10888
10889   if (unified_syntax)
10890     {
10891       bfd_boolean flags;
10892       bfd_boolean narrow;
10893       int opcode;
10894
10895       flags = (inst.instruction == T_MNEM_adds
10896                || inst.instruction == T_MNEM_subs);
10897       if (flags)
10898         narrow = !in_pred_block ();
10899       else
10900         narrow = in_pred_block ();
10901       if (!inst.operands[2].isreg)
10902         {
10903           int add;
10904
10905           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10906             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10907
10908           add = (inst.instruction == T_MNEM_add
10909                  || inst.instruction == T_MNEM_adds);
10910           opcode = 0;
10911           if (inst.size_req != 4)
10912             {
10913               /* Attempt to use a narrow opcode, with relaxation if
10914                  appropriate.  */
10915               if (Rd == REG_SP && Rs == REG_SP && !flags)
10916                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10917               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10918                 opcode = T_MNEM_add_sp;
10919               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10920                 opcode = T_MNEM_add_pc;
10921               else if (Rd <= 7 && Rs <= 7 && narrow)
10922                 {
10923                   if (flags)
10924                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10925                   else
10926                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10927                 }
10928               if (opcode)
10929                 {
10930                   inst.instruction = THUMB_OP16(opcode);
10931                   inst.instruction |= (Rd << 4) | Rs;
10932                   if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10933                       || (inst.relocs[0].type
10934                           > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC))
10935                   {
10936                     if (inst.size_req == 2)
10937                       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
10938                     else
10939                       inst.relax = opcode;
10940                   }
10941                 }
10942               else
10943                 constraint (inst.size_req == 2, BAD_HIREG);
10944             }
10945           if (inst.size_req == 4
10946               || (inst.size_req != 2 && !opcode))
10947             {
10948               constraint ((inst.relocs[0].type
10949                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
10950                           && (inst.relocs[0].type
10951                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
10952                           THUMB1_RELOC_ONLY);
10953               if (Rd == REG_PC)
10954                 {
10955                   constraint (add, BAD_PC);
10956                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10957                              _("only SUBS PC, LR, #const allowed"));
10958                   constraint (inst.relocs[0].exp.X_op != O_constant,
10959                               _("expression too complex"));
10960                   constraint (inst.relocs[0].exp.X_add_number < 0
10961                               || inst.relocs[0].exp.X_add_number > 0xff,
10962                              _("immediate value out of range"));
10963                   inst.instruction = T2_SUBS_PC_LR
10964                                      | inst.relocs[0].exp.X_add_number;
10965                   inst.relocs[0].type = BFD_RELOC_UNUSED;
10966                   return;
10967                 }
10968               else if (Rs == REG_PC)
10969                 {
10970                   /* Always use addw/subw.  */
10971                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10972                   inst.relocs[0].type = BFD_RELOC_ARM_T32_IMM12;
10973                 }
10974               else
10975                 {
10976                   inst.instruction = THUMB_OP32 (inst.instruction);
10977                   inst.instruction = (inst.instruction & 0xe1ffffff)
10978                                      | 0x10000000;
10979                   if (flags)
10980                     inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
10981                   else
10982                     inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_IMM;
10983                 }
10984               inst.instruction |= Rd << 8;
10985               inst.instruction |= Rs << 16;
10986             }
10987         }
10988       else
10989         {
10990           unsigned int value = inst.relocs[0].exp.X_add_number;
10991           unsigned int shift = inst.operands[2].shift_kind;
10992
10993           Rn = inst.operands[2].reg;
10994           /* See if we can do this with a 16-bit instruction.  */
10995           if (!inst.operands[2].shifted && inst.size_req != 4)
10996             {
10997               if (Rd > 7 || Rs > 7 || Rn > 7)
10998                 narrow = FALSE;
10999
11000               if (narrow)
11001                 {
11002                   inst.instruction = ((inst.instruction == T_MNEM_adds
11003                                        || inst.instruction == T_MNEM_add)
11004                                       ? T_OPCODE_ADD_R3
11005                                       : T_OPCODE_SUB_R3);
11006                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11007                   return;
11008                 }
11009
11010               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
11011                 {
11012                   /* Thumb-1 cores (except v6-M) require at least one high
11013                      register in a narrow non flag setting add.  */
11014                   if (Rd > 7 || Rn > 7
11015                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
11016                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
11017                     {
11018                       if (Rd == Rn)
11019                         {
11020                           Rn = Rs;
11021                           Rs = Rd;
11022                         }
11023                       inst.instruction = T_OPCODE_ADD_HI;
11024                       inst.instruction |= (Rd & 8) << 4;
11025                       inst.instruction |= (Rd & 7);
11026                       inst.instruction |= Rn << 3;
11027                       return;
11028                     }
11029                 }
11030             }
11031
11032           constraint (Rd == REG_PC, BAD_PC);
11033           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11034             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
11035           constraint (Rs == REG_PC, BAD_PC);
11036           reject_bad_reg (Rn);
11037
11038           /* If we get here, it can't be done in 16 bits.  */
11039           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
11040                       _("shift must be constant"));
11041           inst.instruction = THUMB_OP32 (inst.instruction);
11042           inst.instruction |= Rd << 8;
11043           inst.instruction |= Rs << 16;
11044           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
11045                       _("shift value over 3 not allowed in thumb mode"));
11046           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
11047                       _("only LSL shift allowed in thumb mode"));
11048           encode_thumb32_shifted_operand (2);
11049         }
11050     }
11051   else
11052     {
11053       constraint (inst.instruction == T_MNEM_adds
11054                   || inst.instruction == T_MNEM_subs,
11055                   BAD_THUMB32);
11056
11057       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
11058         {
11059           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
11060                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
11061                       BAD_HIREG);
11062
11063           inst.instruction = (inst.instruction == T_MNEM_add
11064                               ? 0x0000 : 0x8000);
11065           inst.instruction |= (Rd << 4) | Rs;
11066           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11067           return;
11068         }
11069
11070       Rn = inst.operands[2].reg;
11071       constraint (inst.operands[2].shifted, _("unshifted register required"));
11072
11073       /* We now have Rd, Rs, and Rn set to registers.  */
11074       if (Rd > 7 || Rs > 7 || Rn > 7)
11075         {
11076           /* Can't do this for SUB.      */
11077           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
11078           inst.instruction = T_OPCODE_ADD_HI;
11079           inst.instruction |= (Rd & 8) << 4;
11080           inst.instruction |= (Rd & 7);
11081           if (Rs == Rd)
11082             inst.instruction |= Rn << 3;
11083           else if (Rn == Rd)
11084             inst.instruction |= Rs << 3;
11085           else
11086             constraint (1, _("dest must overlap one source register"));
11087         }
11088       else
11089         {
11090           inst.instruction = (inst.instruction == T_MNEM_add
11091                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
11092           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
11093         }
11094     }
11095 }
11096
11097 static void
11098 do_t_adr (void)
11099 {
11100   unsigned Rd;
11101
11102   Rd = inst.operands[0].reg;
11103   reject_bad_reg (Rd);
11104
11105   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
11106     {
11107       /* Defer to section relaxation.  */
11108       inst.relax = inst.instruction;
11109       inst.instruction = THUMB_OP16 (inst.instruction);
11110       inst.instruction |= Rd << 4;
11111     }
11112   else if (unified_syntax && inst.size_req != 2)
11113     {
11114       /* Generate a 32-bit opcode.  */
11115       inst.instruction = THUMB_OP32 (inst.instruction);
11116       inst.instruction |= Rd << 8;
11117       inst.relocs[0].type = BFD_RELOC_ARM_T32_ADD_PC12;
11118       inst.relocs[0].pc_rel = 1;
11119     }
11120   else
11121     {
11122       /* Generate a 16-bit opcode.  */
11123       inst.instruction = THUMB_OP16 (inst.instruction);
11124       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_ADD;
11125       inst.relocs[0].exp.X_add_number -= 4; /* PC relative adjust.  */
11126       inst.relocs[0].pc_rel = 1;
11127       inst.instruction |= Rd << 4;
11128     }
11129
11130   if (inst.relocs[0].exp.X_op == O_symbol
11131       && inst.relocs[0].exp.X_add_symbol != NULL
11132       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11133       && THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11134     inst.relocs[0].exp.X_add_number += 1;
11135 }
11136
11137 /* Arithmetic instructions for which there is just one 16-bit
11138    instruction encoding, and it allows only two low registers.
11139    For maximal compatibility with ARM syntax, we allow three register
11140    operands even when Thumb-32 instructions are not available, as long
11141    as the first two are identical.  For instance, both "sbc r0,r1" and
11142    "sbc r0,r0,r1" are allowed.  */
11143 static void
11144 do_t_arit3 (void)
11145 {
11146   int Rd, Rs, Rn;
11147
11148   Rd = inst.operands[0].reg;
11149   Rs = (inst.operands[1].present
11150         ? inst.operands[1].reg    /* Rd, Rs, foo */
11151         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11152   Rn = inst.operands[2].reg;
11153
11154   reject_bad_reg (Rd);
11155   reject_bad_reg (Rs);
11156   if (inst.operands[2].isreg)
11157     reject_bad_reg (Rn);
11158
11159   if (unified_syntax)
11160     {
11161       if (!inst.operands[2].isreg)
11162         {
11163           /* For an immediate, we always generate a 32-bit opcode;
11164              section relaxation will shrink it later if possible.  */
11165           inst.instruction = THUMB_OP32 (inst.instruction);
11166           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11167           inst.instruction |= Rd << 8;
11168           inst.instruction |= Rs << 16;
11169           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11170         }
11171       else
11172         {
11173           bfd_boolean narrow;
11174
11175           /* See if we can do this with a 16-bit instruction.  */
11176           if (THUMB_SETS_FLAGS (inst.instruction))
11177             narrow = !in_pred_block ();
11178           else
11179             narrow = in_pred_block ();
11180
11181           if (Rd > 7 || Rn > 7 || Rs > 7)
11182             narrow = FALSE;
11183           if (inst.operands[2].shifted)
11184             narrow = FALSE;
11185           if (inst.size_req == 4)
11186             narrow = FALSE;
11187
11188           if (narrow
11189               && Rd == Rs)
11190             {
11191               inst.instruction = THUMB_OP16 (inst.instruction);
11192               inst.instruction |= Rd;
11193               inst.instruction |= Rn << 3;
11194               return;
11195             }
11196
11197           /* If we get here, it can't be done in 16 bits.  */
11198           constraint (inst.operands[2].shifted
11199                       && inst.operands[2].immisreg,
11200                       _("shift must be constant"));
11201           inst.instruction = THUMB_OP32 (inst.instruction);
11202           inst.instruction |= Rd << 8;
11203           inst.instruction |= Rs << 16;
11204           encode_thumb32_shifted_operand (2);
11205         }
11206     }
11207   else
11208     {
11209       /* On its face this is a lie - the instruction does set the
11210          flags.  However, the only supported mnemonic in this mode
11211          says it doesn't.  */
11212       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11213
11214       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11215                   _("unshifted register required"));
11216       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11217       constraint (Rd != Rs,
11218                   _("dest and source1 must be the same register"));
11219
11220       inst.instruction = THUMB_OP16 (inst.instruction);
11221       inst.instruction |= Rd;
11222       inst.instruction |= Rn << 3;
11223     }
11224 }
11225
11226 /* Similarly, but for instructions where the arithmetic operation is
11227    commutative, so we can allow either of them to be different from
11228    the destination operand in a 16-bit instruction.  For instance, all
11229    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
11230    accepted.  */
11231 static void
11232 do_t_arit3c (void)
11233 {
11234   int Rd, Rs, Rn;
11235
11236   Rd = inst.operands[0].reg;
11237   Rs = (inst.operands[1].present
11238         ? inst.operands[1].reg    /* Rd, Rs, foo */
11239         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
11240   Rn = inst.operands[2].reg;
11241
11242   reject_bad_reg (Rd);
11243   reject_bad_reg (Rs);
11244   if (inst.operands[2].isreg)
11245     reject_bad_reg (Rn);
11246
11247   if (unified_syntax)
11248     {
11249       if (!inst.operands[2].isreg)
11250         {
11251           /* For an immediate, we always generate a 32-bit opcode;
11252              section relaxation will shrink it later if possible.  */
11253           inst.instruction = THUMB_OP32 (inst.instruction);
11254           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11255           inst.instruction |= Rd << 8;
11256           inst.instruction |= Rs << 16;
11257           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
11258         }
11259       else
11260         {
11261           bfd_boolean narrow;
11262
11263           /* See if we can do this with a 16-bit instruction.  */
11264           if (THUMB_SETS_FLAGS (inst.instruction))
11265             narrow = !in_pred_block ();
11266           else
11267             narrow = in_pred_block ();
11268
11269           if (Rd > 7 || Rn > 7 || Rs > 7)
11270             narrow = FALSE;
11271           if (inst.operands[2].shifted)
11272             narrow = FALSE;
11273           if (inst.size_req == 4)
11274             narrow = FALSE;
11275
11276           if (narrow)
11277             {
11278               if (Rd == Rs)
11279                 {
11280                   inst.instruction = THUMB_OP16 (inst.instruction);
11281                   inst.instruction |= Rd;
11282                   inst.instruction |= Rn << 3;
11283                   return;
11284                 }
11285               if (Rd == Rn)
11286                 {
11287                   inst.instruction = THUMB_OP16 (inst.instruction);
11288                   inst.instruction |= Rd;
11289                   inst.instruction |= Rs << 3;
11290                   return;
11291                 }
11292             }
11293
11294           /* If we get here, it can't be done in 16 bits.  */
11295           constraint (inst.operands[2].shifted
11296                       && inst.operands[2].immisreg,
11297                       _("shift must be constant"));
11298           inst.instruction = THUMB_OP32 (inst.instruction);
11299           inst.instruction |= Rd << 8;
11300           inst.instruction |= Rs << 16;
11301           encode_thumb32_shifted_operand (2);
11302         }
11303     }
11304   else
11305     {
11306       /* On its face this is a lie - the instruction does set the
11307          flags.  However, the only supported mnemonic in this mode
11308          says it doesn't.  */
11309       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11310
11311       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11312                   _("unshifted register required"));
11313       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11314
11315       inst.instruction = THUMB_OP16 (inst.instruction);
11316       inst.instruction |= Rd;
11317
11318       if (Rd == Rs)
11319         inst.instruction |= Rn << 3;
11320       else if (Rd == Rn)
11321         inst.instruction |= Rs << 3;
11322       else
11323         constraint (1, _("dest must overlap one source register"));
11324     }
11325 }
11326
11327 static void
11328 do_t_bfc (void)
11329 {
11330   unsigned Rd;
11331   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11332   constraint (msb > 32, _("bit-field extends past end of register"));
11333   /* The instruction encoding stores the LSB and MSB,
11334      not the LSB and width.  */
11335   Rd = inst.operands[0].reg;
11336   reject_bad_reg (Rd);
11337   inst.instruction |= Rd << 8;
11338   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11339   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11340   inst.instruction |= msb - 1;
11341 }
11342
11343 static void
11344 do_t_bfi (void)
11345 {
11346   int Rd, Rn;
11347   unsigned int msb;
11348
11349   Rd = inst.operands[0].reg;
11350   reject_bad_reg (Rd);
11351
11352   /* #0 in second position is alternative syntax for bfc, which is
11353      the same instruction but with REG_PC in the Rm field.  */
11354   if (!inst.operands[1].isreg)
11355     Rn = REG_PC;
11356   else
11357     {
11358       Rn = inst.operands[1].reg;
11359       reject_bad_reg (Rn);
11360     }
11361
11362   msb = inst.operands[2].imm + inst.operands[3].imm;
11363   constraint (msb > 32, _("bit-field extends past end of register"));
11364   /* The instruction encoding stores the LSB and MSB,
11365      not the LSB and width.  */
11366   inst.instruction |= Rd << 8;
11367   inst.instruction |= Rn << 16;
11368   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11369   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11370   inst.instruction |= msb - 1;
11371 }
11372
11373 static void
11374 do_t_bfx (void)
11375 {
11376   unsigned Rd, Rn;
11377
11378   Rd = inst.operands[0].reg;
11379   Rn = inst.operands[1].reg;
11380
11381   reject_bad_reg (Rd);
11382   reject_bad_reg (Rn);
11383
11384   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11385               _("bit-field extends past end of register"));
11386   inst.instruction |= Rd << 8;
11387   inst.instruction |= Rn << 16;
11388   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11389   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11390   inst.instruction |= inst.operands[3].imm - 1;
11391 }
11392
11393 /* ARM V5 Thumb BLX (argument parse)
11394         BLX <target_addr>       which is BLX(1)
11395         BLX <Rm>                which is BLX(2)
11396    Unfortunately, there are two different opcodes for this mnemonic.
11397    So, the insns[].value is not used, and the code here zaps values
11398         into inst.instruction.
11399
11400    ??? How to take advantage of the additional two bits of displacement
11401    available in Thumb32 mode?  Need new relocation?  */
11402
11403 static void
11404 do_t_blx (void)
11405 {
11406   set_pred_insn_type_last ();
11407
11408   if (inst.operands[0].isreg)
11409     {
11410       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11411       /* We have a register, so this is BLX(2).  */
11412       inst.instruction |= inst.operands[0].reg << 3;
11413     }
11414   else
11415     {
11416       /* No register.  This must be BLX(1).  */
11417       inst.instruction = 0xf000e800;
11418       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11419     }
11420 }
11421
11422 static void
11423 do_t_branch (void)
11424 {
11425   int opcode;
11426   int cond;
11427   bfd_reloc_code_real_type reloc;
11428
11429   cond = inst.cond;
11430   set_pred_insn_type (IF_INSIDE_IT_LAST_INSN);
11431
11432   if (in_pred_block ())
11433     {
11434       /* Conditional branches inside IT blocks are encoded as unconditional
11435          branches.  */
11436       cond = COND_ALWAYS;
11437     }
11438   else
11439     cond = inst.cond;
11440
11441   if (cond != COND_ALWAYS)
11442     opcode = T_MNEM_bcond;
11443   else
11444     opcode = inst.instruction;
11445
11446   if (unified_syntax
11447       && (inst.size_req == 4
11448           || (inst.size_req != 2
11449               && (inst.operands[0].hasreloc
11450                   || inst.relocs[0].exp.X_op == O_constant))))
11451     {
11452       inst.instruction = THUMB_OP32(opcode);
11453       if (cond == COND_ALWAYS)
11454         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11455       else
11456         {
11457           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11458                       _("selected architecture does not support "
11459                         "wide conditional branch instruction"));
11460
11461           gas_assert (cond != 0xF);
11462           inst.instruction |= cond << 22;
11463           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11464         }
11465     }
11466   else
11467     {
11468       inst.instruction = THUMB_OP16(opcode);
11469       if (cond == COND_ALWAYS)
11470         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11471       else
11472         {
11473           inst.instruction |= cond << 8;
11474           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11475         }
11476       /* Allow section relaxation.  */
11477       if (unified_syntax && inst.size_req != 2)
11478         inst.relax = opcode;
11479     }
11480   inst.relocs[0].type = reloc;
11481   inst.relocs[0].pc_rel = 1;
11482 }
11483
11484 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11485    between the two is the maximum immediate allowed - which is passed in
11486    RANGE.  */
11487 static void
11488 do_t_bkpt_hlt1 (int range)
11489 {
11490   constraint (inst.cond != COND_ALWAYS,
11491               _("instruction is always unconditional"));
11492   if (inst.operands[0].present)
11493     {
11494       constraint (inst.operands[0].imm > range,
11495                   _("immediate value out of range"));
11496       inst.instruction |= inst.operands[0].imm;
11497     }
11498
11499   set_pred_insn_type (NEUTRAL_IT_INSN);
11500 }
11501
11502 static void
11503 do_t_hlt (void)
11504 {
11505   do_t_bkpt_hlt1 (63);
11506 }
11507
11508 static void
11509 do_t_bkpt (void)
11510 {
11511   do_t_bkpt_hlt1 (255);
11512 }
11513
11514 static void
11515 do_t_branch23 (void)
11516 {
11517   set_pred_insn_type_last ();
11518   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11519
11520   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11521      this file.  We used to simply ignore the PLT reloc type here --
11522      the branch encoding is now needed to deal with TLSCALL relocs.
11523      So if we see a PLT reloc now, put it back to how it used to be to
11524      keep the preexisting behaviour.  */
11525   if (inst.relocs[0].type == BFD_RELOC_ARM_PLT32)
11526     inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11527
11528 #if defined(OBJ_COFF)
11529   /* If the destination of the branch is a defined symbol which does not have
11530      the THUMB_FUNC attribute, then we must be calling a function which has
11531      the (interfacearm) attribute.  We look for the Thumb entry point to that
11532      function and change the branch to refer to that function instead.  */
11533   if (   inst.relocs[0].exp.X_op == O_symbol
11534       && inst.relocs[0].exp.X_add_symbol != NULL
11535       && S_IS_DEFINED (inst.relocs[0].exp.X_add_symbol)
11536       && ! THUMB_IS_FUNC (inst.relocs[0].exp.X_add_symbol))
11537     inst.relocs[0].exp.X_add_symbol
11538       = find_real_start (inst.relocs[0].exp.X_add_symbol);
11539 #endif
11540 }
11541
11542 static void
11543 do_t_bx (void)
11544 {
11545   set_pred_insn_type_last ();
11546   inst.instruction |= inst.operands[0].reg << 3;
11547   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11548      should cause the alignment to be checked once it is known.  This is
11549      because BX PC only works if the instruction is word aligned.  */
11550 }
11551
11552 static void
11553 do_t_bxj (void)
11554 {
11555   int Rm;
11556
11557   set_pred_insn_type_last ();
11558   Rm = inst.operands[0].reg;
11559   reject_bad_reg (Rm);
11560   inst.instruction |= Rm << 16;
11561 }
11562
11563 static void
11564 do_t_clz (void)
11565 {
11566   unsigned Rd;
11567   unsigned Rm;
11568
11569   Rd = inst.operands[0].reg;
11570   Rm = inst.operands[1].reg;
11571
11572   reject_bad_reg (Rd);
11573   reject_bad_reg (Rm);
11574
11575   inst.instruction |= Rd << 8;
11576   inst.instruction |= Rm << 16;
11577   inst.instruction |= Rm;
11578 }
11579
11580 static void
11581 do_t_csdb (void)
11582 {
11583   set_pred_insn_type (OUTSIDE_PRED_INSN);
11584 }
11585
11586 static void
11587 do_t_cps (void)
11588 {
11589   set_pred_insn_type (OUTSIDE_PRED_INSN);
11590   inst.instruction |= inst.operands[0].imm;
11591 }
11592
11593 static void
11594 do_t_cpsi (void)
11595 {
11596   set_pred_insn_type (OUTSIDE_PRED_INSN);
11597   if (unified_syntax
11598       && (inst.operands[1].present || inst.size_req == 4)
11599       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11600     {
11601       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11602       inst.instruction = 0xf3af8000;
11603       inst.instruction |= imod << 9;
11604       inst.instruction |= inst.operands[0].imm << 5;
11605       if (inst.operands[1].present)
11606         inst.instruction |= 0x100 | inst.operands[1].imm;
11607     }
11608   else
11609     {
11610       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11611                   && (inst.operands[0].imm & 4),
11612                   _("selected processor does not support 'A' form "
11613                     "of this instruction"));
11614       constraint (inst.operands[1].present || inst.size_req == 4,
11615                   _("Thumb does not support the 2-argument "
11616                     "form of this instruction"));
11617       inst.instruction |= inst.operands[0].imm;
11618     }
11619 }
11620
11621 /* THUMB CPY instruction (argument parse).  */
11622
11623 static void
11624 do_t_cpy (void)
11625 {
11626   if (inst.size_req == 4)
11627     {
11628       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11629       inst.instruction |= inst.operands[0].reg << 8;
11630       inst.instruction |= inst.operands[1].reg;
11631     }
11632   else
11633     {
11634       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11635       inst.instruction |= (inst.operands[0].reg & 0x7);
11636       inst.instruction |= inst.operands[1].reg << 3;
11637     }
11638 }
11639
11640 static void
11641 do_t_cbz (void)
11642 {
11643   set_pred_insn_type (OUTSIDE_PRED_INSN);
11644   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11645   inst.instruction |= inst.operands[0].reg;
11646   inst.relocs[0].pc_rel = 1;
11647   inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11648 }
11649
11650 static void
11651 do_t_dbg (void)
11652 {
11653   inst.instruction |= inst.operands[0].imm;
11654 }
11655
11656 static void
11657 do_t_div (void)
11658 {
11659   unsigned Rd, Rn, Rm;
11660
11661   Rd = inst.operands[0].reg;
11662   Rn = (inst.operands[1].present
11663         ? inst.operands[1].reg : Rd);
11664   Rm = inst.operands[2].reg;
11665
11666   reject_bad_reg (Rd);
11667   reject_bad_reg (Rn);
11668   reject_bad_reg (Rm);
11669
11670   inst.instruction |= Rd << 8;
11671   inst.instruction |= Rn << 16;
11672   inst.instruction |= Rm;
11673 }
11674
11675 static void
11676 do_t_hint (void)
11677 {
11678   if (unified_syntax && inst.size_req == 4)
11679     inst.instruction = THUMB_OP32 (inst.instruction);
11680   else
11681     inst.instruction = THUMB_OP16 (inst.instruction);
11682 }
11683
11684 static void
11685 do_t_it (void)
11686 {
11687   unsigned int cond = inst.operands[0].imm;
11688
11689   set_pred_insn_type (IT_INSN);
11690   now_pred.mask = (inst.instruction & 0xf) | 0x10;
11691   now_pred.cc = cond;
11692   now_pred.warn_deprecated = FALSE;
11693   now_pred.type = SCALAR_PRED;
11694
11695   /* If the condition is a negative condition, invert the mask.  */
11696   if ((cond & 0x1) == 0x0)
11697     {
11698       unsigned int mask = inst.instruction & 0x000f;
11699
11700       if ((mask & 0x7) == 0)
11701         {
11702           /* No conversion needed.  */
11703           now_pred.block_length = 1;
11704         }
11705       else if ((mask & 0x3) == 0)
11706         {
11707           mask ^= 0x8;
11708           now_pred.block_length = 2;
11709         }
11710       else if ((mask & 0x1) == 0)
11711         {
11712           mask ^= 0xC;
11713           now_pred.block_length = 3;
11714         }
11715       else
11716         {
11717           mask ^= 0xE;
11718           now_pred.block_length = 4;
11719         }
11720
11721       inst.instruction &= 0xfff0;
11722       inst.instruction |= mask;
11723     }
11724
11725   inst.instruction |= cond << 4;
11726 }
11727
11728 static void
11729 do_mve_vpt (void)
11730 {
11731   /* We are dealing with a vector predicated block.  */
11732   set_pred_insn_type (VPT_INSN);
11733   now_pred.cc = 0;
11734   now_pred.mask = ((inst.instruction & 0x00400000) >> 19)
11735                   | ((inst.instruction & 0xe000) >> 13);
11736   now_pred.warn_deprecated = FALSE;
11737   now_pred.type = VECTOR_PRED;
11738 }
11739
11740 /* Helper function used for both push/pop and ldm/stm.  */
11741 static void
11742 encode_thumb2_multi (bfd_boolean do_io, int base, unsigned mask,
11743                      bfd_boolean writeback)
11744 {
11745   bfd_boolean load, store;
11746
11747   gas_assert (base != -1 || !do_io);
11748   load = do_io && ((inst.instruction & (1 << 20)) != 0);
11749   store = do_io && !load;
11750
11751   if (mask & (1 << 13))
11752     inst.error =  _("SP not allowed in register list");
11753
11754   if (do_io && (mask & (1 << base)) != 0
11755       && writeback)
11756     inst.error = _("having the base register in the register list when "
11757                    "using write back is UNPREDICTABLE");
11758
11759   if (load)
11760     {
11761       if (mask & (1 << 15))
11762         {
11763           if (mask & (1 << 14))
11764             inst.error = _("LR and PC should not both be in register list");
11765           else
11766             set_pred_insn_type_last ();
11767         }
11768     }
11769   else if (store)
11770     {
11771       if (mask & (1 << 15))
11772         inst.error = _("PC not allowed in register list");
11773     }
11774
11775   if (do_io && ((mask & (mask - 1)) == 0))
11776     {
11777       /* Single register transfers implemented as str/ldr.  */
11778       if (writeback)
11779         {
11780           if (inst.instruction & (1 << 23))
11781             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11782           else
11783             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11784         }
11785       else
11786         {
11787           if (inst.instruction & (1 << 23))
11788             inst.instruction = 0x00800000; /* ia -> [base] */
11789           else
11790             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11791         }
11792
11793       inst.instruction |= 0xf8400000;
11794       if (load)
11795         inst.instruction |= 0x00100000;
11796
11797       mask = ffs (mask) - 1;
11798       mask <<= 12;
11799     }
11800   else if (writeback)
11801     inst.instruction |= WRITE_BACK;
11802
11803   inst.instruction |= mask;
11804   if (do_io)
11805     inst.instruction |= base << 16;
11806 }
11807
11808 static void
11809 do_t_ldmstm (void)
11810 {
11811   /* This really doesn't seem worth it.  */
11812   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
11813               _("expression too complex"));
11814   constraint (inst.operands[1].writeback,
11815               _("Thumb load/store multiple does not support {reglist}^"));
11816
11817   if (unified_syntax)
11818     {
11819       bfd_boolean narrow;
11820       unsigned mask;
11821
11822       narrow = FALSE;
11823       /* See if we can use a 16-bit instruction.  */
11824       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11825           && inst.size_req != 4
11826           && !(inst.operands[1].imm & ~0xff))
11827         {
11828           mask = 1 << inst.operands[0].reg;
11829
11830           if (inst.operands[0].reg <= 7)
11831             {
11832               if (inst.instruction == T_MNEM_stmia
11833                   ? inst.operands[0].writeback
11834                   : (inst.operands[0].writeback
11835                      == !(inst.operands[1].imm & mask)))
11836                 {
11837                   if (inst.instruction == T_MNEM_stmia
11838                       && (inst.operands[1].imm & mask)
11839                       && (inst.operands[1].imm & (mask - 1)))
11840                     as_warn (_("value stored for r%d is UNKNOWN"),
11841                              inst.operands[0].reg);
11842
11843                   inst.instruction = THUMB_OP16 (inst.instruction);
11844                   inst.instruction |= inst.operands[0].reg << 8;
11845                   inst.instruction |= inst.operands[1].imm;
11846                   narrow = TRUE;
11847                 }
11848               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11849                 {
11850                   /* This means 1 register in reg list one of 3 situations:
11851                      1. Instruction is stmia, but without writeback.
11852                      2. lmdia without writeback, but with Rn not in
11853                         reglist.
11854                      3. ldmia with writeback, but with Rn in reglist.
11855                      Case 3 is UNPREDICTABLE behaviour, so we handle
11856                      case 1 and 2 which can be converted into a 16-bit
11857                      str or ldr. The SP cases are handled below.  */
11858                   unsigned long opcode;
11859                   /* First, record an error for Case 3.  */
11860                   if (inst.operands[1].imm & mask
11861                       && inst.operands[0].writeback)
11862                     inst.error =
11863                         _("having the base register in the register list when "
11864                           "using write back is UNPREDICTABLE");
11865
11866                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11867                                                              : T_MNEM_ldr);
11868                   inst.instruction = THUMB_OP16 (opcode);
11869                   inst.instruction |= inst.operands[0].reg << 3;
11870                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11871                   narrow = TRUE;
11872                 }
11873             }
11874           else if (inst.operands[0] .reg == REG_SP)
11875             {
11876               if (inst.operands[0].writeback)
11877                 {
11878                   inst.instruction =
11879                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11880                                     ? T_MNEM_push : T_MNEM_pop);
11881                   inst.instruction |= inst.operands[1].imm;
11882                   narrow = TRUE;
11883                 }
11884               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11885                 {
11886                   inst.instruction =
11887                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11888                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11889                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11890                   narrow = TRUE;
11891                 }
11892             }
11893         }
11894
11895       if (!narrow)
11896         {
11897           if (inst.instruction < 0xffff)
11898             inst.instruction = THUMB_OP32 (inst.instruction);
11899
11900           encode_thumb2_multi (TRUE /* do_io */, inst.operands[0].reg,
11901                                inst.operands[1].imm,
11902                                inst.operands[0].writeback);
11903         }
11904     }
11905   else
11906     {
11907       constraint (inst.operands[0].reg > 7
11908                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11909       constraint (inst.instruction != T_MNEM_ldmia
11910                   && inst.instruction != T_MNEM_stmia,
11911                   _("Thumb-2 instruction only valid in unified syntax"));
11912       if (inst.instruction == T_MNEM_stmia)
11913         {
11914           if (!inst.operands[0].writeback)
11915             as_warn (_("this instruction will write back the base register"));
11916           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11917               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11918             as_warn (_("value stored for r%d is UNKNOWN"),
11919                      inst.operands[0].reg);
11920         }
11921       else
11922         {
11923           if (!inst.operands[0].writeback
11924               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11925             as_warn (_("this instruction will write back the base register"));
11926           else if (inst.operands[0].writeback
11927                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11928             as_warn (_("this instruction will not write back the base register"));
11929         }
11930
11931       inst.instruction = THUMB_OP16 (inst.instruction);
11932       inst.instruction |= inst.operands[0].reg << 8;
11933       inst.instruction |= inst.operands[1].imm;
11934     }
11935 }
11936
11937 static void
11938 do_t_ldrex (void)
11939 {
11940   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11941               || inst.operands[1].postind || inst.operands[1].writeback
11942               || inst.operands[1].immisreg || inst.operands[1].shifted
11943               || inst.operands[1].negative,
11944               BAD_ADDR_MODE);
11945
11946   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11947
11948   inst.instruction |= inst.operands[0].reg << 12;
11949   inst.instruction |= inst.operands[1].reg << 16;
11950   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
11951 }
11952
11953 static void
11954 do_t_ldrexd (void)
11955 {
11956   if (!inst.operands[1].present)
11957     {
11958       constraint (inst.operands[0].reg == REG_LR,
11959                   _("r14 not allowed as first register "
11960                     "when second register is omitted"));
11961       inst.operands[1].reg = inst.operands[0].reg + 1;
11962     }
11963   constraint (inst.operands[0].reg == inst.operands[1].reg,
11964               BAD_OVERLAP);
11965
11966   inst.instruction |= inst.operands[0].reg << 12;
11967   inst.instruction |= inst.operands[1].reg << 8;
11968   inst.instruction |= inst.operands[2].reg << 16;
11969 }
11970
11971 static void
11972 do_t_ldst (void)
11973 {
11974   unsigned long opcode;
11975   int Rn;
11976
11977   if (inst.operands[0].isreg
11978       && !inst.operands[0].preind
11979       && inst.operands[0].reg == REG_PC)
11980     set_pred_insn_type_last ();
11981
11982   opcode = inst.instruction;
11983   if (unified_syntax)
11984     {
11985       if (!inst.operands[1].isreg)
11986         {
11987           if (opcode <= 0xffff)
11988             inst.instruction = THUMB_OP32 (opcode);
11989           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11990             return;
11991         }
11992       if (inst.operands[1].isreg
11993           && !inst.operands[1].writeback
11994           && !inst.operands[1].shifted && !inst.operands[1].postind
11995           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11996           && opcode <= 0xffff
11997           && inst.size_req != 4)
11998         {
11999           /* Insn may have a 16-bit form.  */
12000           Rn = inst.operands[1].reg;
12001           if (inst.operands[1].immisreg)
12002             {
12003               inst.instruction = THUMB_OP16 (opcode);
12004               /* [Rn, Rik] */
12005               if (Rn <= 7 && inst.operands[1].imm <= 7)
12006                 goto op16;
12007               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
12008                 reject_bad_reg (inst.operands[1].imm);
12009             }
12010           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
12011                     && opcode != T_MNEM_ldrsb)
12012                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
12013                    || (Rn == REG_SP && opcode == T_MNEM_str))
12014             {
12015               /* [Rn, #const] */
12016               if (Rn > 7)
12017                 {
12018                   if (Rn == REG_PC)
12019                     {
12020                       if (inst.relocs[0].pc_rel)
12021                         opcode = T_MNEM_ldr_pc2;
12022                       else
12023                         opcode = T_MNEM_ldr_pc;
12024                     }
12025                   else
12026                     {
12027                       if (opcode == T_MNEM_ldr)
12028                         opcode = T_MNEM_ldr_sp;
12029                       else
12030                         opcode = T_MNEM_str_sp;
12031                     }
12032                   inst.instruction = inst.operands[0].reg << 8;
12033                 }
12034               else
12035                 {
12036                   inst.instruction = inst.operands[0].reg;
12037                   inst.instruction |= inst.operands[1].reg << 3;
12038                 }
12039               inst.instruction |= THUMB_OP16 (opcode);
12040               if (inst.size_req == 2)
12041                 inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12042               else
12043                 inst.relax = opcode;
12044               return;
12045             }
12046         }
12047       /* Definitely a 32-bit variant.  */
12048
12049       /* Warning for Erratum 752419.  */
12050       if (opcode == T_MNEM_ldr
12051           && inst.operands[0].reg == REG_SP
12052           && inst.operands[1].writeback == 1
12053           && !inst.operands[1].immisreg)
12054         {
12055           if (no_cpu_selected ()
12056               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
12057                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
12058                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
12059             as_warn (_("This instruction may be unpredictable "
12060                        "if executed on M-profile cores "
12061                        "with interrupts enabled."));
12062         }
12063
12064       /* Do some validations regarding addressing modes.  */
12065       if (inst.operands[1].immisreg)
12066         reject_bad_reg (inst.operands[1].imm);
12067
12068       constraint (inst.operands[1].writeback == 1
12069                   && inst.operands[0].reg == inst.operands[1].reg,
12070                   BAD_OVERLAP);
12071
12072       inst.instruction = THUMB_OP32 (opcode);
12073       inst.instruction |= inst.operands[0].reg << 12;
12074       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
12075       check_ldr_r15_aligned ();
12076       return;
12077     }
12078
12079   constraint (inst.operands[0].reg > 7, BAD_HIREG);
12080
12081   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
12082     {
12083       /* Only [Rn,Rm] is acceptable.  */
12084       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
12085       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
12086                   || inst.operands[1].postind || inst.operands[1].shifted
12087                   || inst.operands[1].negative,
12088                   _("Thumb does not support this addressing mode"));
12089       inst.instruction = THUMB_OP16 (inst.instruction);
12090       goto op16;
12091     }
12092
12093   inst.instruction = THUMB_OP16 (inst.instruction);
12094   if (!inst.operands[1].isreg)
12095     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
12096       return;
12097
12098   constraint (!inst.operands[1].preind
12099               || inst.operands[1].shifted
12100               || inst.operands[1].writeback,
12101               _("Thumb does not support this addressing mode"));
12102   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
12103     {
12104       constraint (inst.instruction & 0x0600,
12105                   _("byte or halfword not valid for base register"));
12106       constraint (inst.operands[1].reg == REG_PC
12107                   && !(inst.instruction & THUMB_LOAD_BIT),
12108                   _("r15 based store not allowed"));
12109       constraint (inst.operands[1].immisreg,
12110                   _("invalid base register for register offset"));
12111
12112       if (inst.operands[1].reg == REG_PC)
12113         inst.instruction = T_OPCODE_LDR_PC;
12114       else if (inst.instruction & THUMB_LOAD_BIT)
12115         inst.instruction = T_OPCODE_LDR_SP;
12116       else
12117         inst.instruction = T_OPCODE_STR_SP;
12118
12119       inst.instruction |= inst.operands[0].reg << 8;
12120       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12121       return;
12122     }
12123
12124   constraint (inst.operands[1].reg > 7, BAD_HIREG);
12125   if (!inst.operands[1].immisreg)
12126     {
12127       /* Immediate offset.  */
12128       inst.instruction |= inst.operands[0].reg;
12129       inst.instruction |= inst.operands[1].reg << 3;
12130       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_OFFSET;
12131       return;
12132     }
12133
12134   /* Register offset.  */
12135   constraint (inst.operands[1].imm > 7, BAD_HIREG);
12136   constraint (inst.operands[1].negative,
12137               _("Thumb does not support this addressing mode"));
12138
12139  op16:
12140   switch (inst.instruction)
12141     {
12142     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
12143     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
12144     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
12145     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
12146     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
12147     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
12148     case 0x5600 /* ldrsb */:
12149     case 0x5e00 /* ldrsh */: break;
12150     default: abort ();
12151     }
12152
12153   inst.instruction |= inst.operands[0].reg;
12154   inst.instruction |= inst.operands[1].reg << 3;
12155   inst.instruction |= inst.operands[1].imm << 6;
12156 }
12157
12158 static void
12159 do_t_ldstd (void)
12160 {
12161   if (!inst.operands[1].present)
12162     {
12163       inst.operands[1].reg = inst.operands[0].reg + 1;
12164       constraint (inst.operands[0].reg == REG_LR,
12165                   _("r14 not allowed here"));
12166       constraint (inst.operands[0].reg == REG_R12,
12167                   _("r12 not allowed here"));
12168     }
12169
12170   if (inst.operands[2].writeback
12171       && (inst.operands[0].reg == inst.operands[2].reg
12172       || inst.operands[1].reg == inst.operands[2].reg))
12173     as_warn (_("base register written back, and overlaps "
12174                "one of transfer registers"));
12175
12176   inst.instruction |= inst.operands[0].reg << 12;
12177   inst.instruction |= inst.operands[1].reg << 8;
12178   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
12179 }
12180
12181 static void
12182 do_t_ldstt (void)
12183 {
12184   inst.instruction |= inst.operands[0].reg << 12;
12185   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
12186 }
12187
12188 static void
12189 do_t_mla (void)
12190 {
12191   unsigned Rd, Rn, Rm, Ra;
12192
12193   Rd = inst.operands[0].reg;
12194   Rn = inst.operands[1].reg;
12195   Rm = inst.operands[2].reg;
12196   Ra = inst.operands[3].reg;
12197
12198   reject_bad_reg (Rd);
12199   reject_bad_reg (Rn);
12200   reject_bad_reg (Rm);
12201   reject_bad_reg (Ra);
12202
12203   inst.instruction |= Rd << 8;
12204   inst.instruction |= Rn << 16;
12205   inst.instruction |= Rm;
12206   inst.instruction |= Ra << 12;
12207 }
12208
12209 static void
12210 do_t_mlal (void)
12211 {
12212   unsigned RdLo, RdHi, Rn, Rm;
12213
12214   RdLo = inst.operands[0].reg;
12215   RdHi = inst.operands[1].reg;
12216   Rn = inst.operands[2].reg;
12217   Rm = inst.operands[3].reg;
12218
12219   reject_bad_reg (RdLo);
12220   reject_bad_reg (RdHi);
12221   reject_bad_reg (Rn);
12222   reject_bad_reg (Rm);
12223
12224   inst.instruction |= RdLo << 12;
12225   inst.instruction |= RdHi << 8;
12226   inst.instruction |= Rn << 16;
12227   inst.instruction |= Rm;
12228 }
12229
12230 static void
12231 do_t_mov_cmp (void)
12232 {
12233   unsigned Rn, Rm;
12234
12235   Rn = inst.operands[0].reg;
12236   Rm = inst.operands[1].reg;
12237
12238   if (Rn == REG_PC)
12239     set_pred_insn_type_last ();
12240
12241   if (unified_syntax)
12242     {
12243       int r0off = (inst.instruction == T_MNEM_mov
12244                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
12245       unsigned long opcode;
12246       bfd_boolean narrow;
12247       bfd_boolean low_regs;
12248
12249       low_regs = (Rn <= 7 && Rm <= 7);
12250       opcode = inst.instruction;
12251       if (in_pred_block ())
12252         narrow = opcode != T_MNEM_movs;
12253       else
12254         narrow = opcode != T_MNEM_movs || low_regs;
12255       if (inst.size_req == 4
12256           || inst.operands[1].shifted)
12257         narrow = FALSE;
12258
12259       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
12260       if (opcode == T_MNEM_movs && inst.operands[1].isreg
12261           && !inst.operands[1].shifted
12262           && Rn == REG_PC
12263           && Rm == REG_LR)
12264         {
12265           inst.instruction = T2_SUBS_PC_LR;
12266           return;
12267         }
12268
12269       if (opcode == T_MNEM_cmp)
12270         {
12271           constraint (Rn == REG_PC, BAD_PC);
12272           if (narrow)
12273             {
12274               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
12275                  but valid.  */
12276               warn_deprecated_sp (Rm);
12277               /* R15 was documented as a valid choice for Rm in ARMv6,
12278                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
12279                  tools reject R15, so we do too.  */
12280               constraint (Rm == REG_PC, BAD_PC);
12281             }
12282           else
12283             reject_bad_reg (Rm);
12284         }
12285       else if (opcode == T_MNEM_mov
12286                || opcode == T_MNEM_movs)
12287         {
12288           if (inst.operands[1].isreg)
12289             {
12290               if (opcode == T_MNEM_movs)
12291                 {
12292                   reject_bad_reg (Rn);
12293                   reject_bad_reg (Rm);
12294                 }
12295               else if (narrow)
12296                 {
12297                   /* This is mov.n.  */
12298                   if ((Rn == REG_SP || Rn == REG_PC)
12299                       && (Rm == REG_SP || Rm == REG_PC))
12300                     {
12301                       as_tsktsk (_("Use of r%u as a source register is "
12302                                  "deprecated when r%u is the destination "
12303                                  "register."), Rm, Rn);
12304                     }
12305                 }
12306               else
12307                 {
12308                   /* This is mov.w.  */
12309                   constraint (Rn == REG_PC, BAD_PC);
12310                   constraint (Rm == REG_PC, BAD_PC);
12311                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12312                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12313                 }
12314             }
12315           else
12316             reject_bad_reg (Rn);
12317         }
12318
12319       if (!inst.operands[1].isreg)
12320         {
12321           /* Immediate operand.  */
12322           if (!in_pred_block () && opcode == T_MNEM_mov)
12323             narrow = 0;
12324           if (low_regs && narrow)
12325             {
12326               inst.instruction = THUMB_OP16 (opcode);
12327               inst.instruction |= Rn << 8;
12328               if (inst.relocs[0].type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12329                   || inst.relocs[0].type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12330                 {
12331                   if (inst.size_req == 2)
12332                     inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12333                   else
12334                     inst.relax = opcode;
12335                 }
12336             }
12337           else
12338             {
12339               constraint ((inst.relocs[0].type
12340                            >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC)
12341                           && (inst.relocs[0].type
12342                               <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC) ,
12343                           THUMB1_RELOC_ONLY);
12344
12345               inst.instruction = THUMB_OP32 (inst.instruction);
12346               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12347               inst.instruction |= Rn << r0off;
12348               inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12349             }
12350         }
12351       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12352                && (inst.instruction == T_MNEM_mov
12353                    || inst.instruction == T_MNEM_movs))
12354         {
12355           /* Register shifts are encoded as separate shift instructions.  */
12356           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12357
12358           if (in_pred_block ())
12359             narrow = !flags;
12360           else
12361             narrow = flags;
12362
12363           if (inst.size_req == 4)
12364             narrow = FALSE;
12365
12366           if (!low_regs || inst.operands[1].imm > 7)
12367             narrow = FALSE;
12368
12369           if (Rn != Rm)
12370             narrow = FALSE;
12371
12372           switch (inst.operands[1].shift_kind)
12373             {
12374             case SHIFT_LSL:
12375               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12376               break;
12377             case SHIFT_ASR:
12378               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12379               break;
12380             case SHIFT_LSR:
12381               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12382               break;
12383             case SHIFT_ROR:
12384               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12385               break;
12386             default:
12387               abort ();
12388             }
12389
12390           inst.instruction = opcode;
12391           if (narrow)
12392             {
12393               inst.instruction |= Rn;
12394               inst.instruction |= inst.operands[1].imm << 3;
12395             }
12396           else
12397             {
12398               if (flags)
12399                 inst.instruction |= CONDS_BIT;
12400
12401               inst.instruction |= Rn << 8;
12402               inst.instruction |= Rm << 16;
12403               inst.instruction |= inst.operands[1].imm;
12404             }
12405         }
12406       else if (!narrow)
12407         {
12408           /* Some mov with immediate shift have narrow variants.
12409              Register shifts are handled above.  */
12410           if (low_regs && inst.operands[1].shifted
12411               && (inst.instruction == T_MNEM_mov
12412                   || inst.instruction == T_MNEM_movs))
12413             {
12414               if (in_pred_block ())
12415                 narrow = (inst.instruction == T_MNEM_mov);
12416               else
12417                 narrow = (inst.instruction == T_MNEM_movs);
12418             }
12419
12420           if (narrow)
12421             {
12422               switch (inst.operands[1].shift_kind)
12423                 {
12424                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12425                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12426                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12427                 default: narrow = FALSE; break;
12428                 }
12429             }
12430
12431           if (narrow)
12432             {
12433               inst.instruction |= Rn;
12434               inst.instruction |= Rm << 3;
12435               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
12436             }
12437           else
12438             {
12439               inst.instruction = THUMB_OP32 (inst.instruction);
12440               inst.instruction |= Rn << r0off;
12441               encode_thumb32_shifted_operand (1);
12442             }
12443         }
12444       else
12445         switch (inst.instruction)
12446           {
12447           case T_MNEM_mov:
12448             /* In v4t or v5t a move of two lowregs produces unpredictable
12449                results. Don't allow this.  */
12450             if (low_regs)
12451               {
12452                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12453                             "MOV Rd, Rs with two low registers is not "
12454                             "permitted on this architecture");
12455                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12456                                         arm_ext_v6);
12457               }
12458
12459             inst.instruction = T_OPCODE_MOV_HR;
12460             inst.instruction |= (Rn & 0x8) << 4;
12461             inst.instruction |= (Rn & 0x7);
12462             inst.instruction |= Rm << 3;
12463             break;
12464
12465           case T_MNEM_movs:
12466             /* We know we have low registers at this point.
12467                Generate LSLS Rd, Rs, #0.  */
12468             inst.instruction = T_OPCODE_LSL_I;
12469             inst.instruction |= Rn;
12470             inst.instruction |= Rm << 3;
12471             break;
12472
12473           case T_MNEM_cmp:
12474             if (low_regs)
12475               {
12476                 inst.instruction = T_OPCODE_CMP_LR;
12477                 inst.instruction |= Rn;
12478                 inst.instruction |= Rm << 3;
12479               }
12480             else
12481               {
12482                 inst.instruction = T_OPCODE_CMP_HR;
12483                 inst.instruction |= (Rn & 0x8) << 4;
12484                 inst.instruction |= (Rn & 0x7);
12485                 inst.instruction |= Rm << 3;
12486               }
12487             break;
12488           }
12489       return;
12490     }
12491
12492   inst.instruction = THUMB_OP16 (inst.instruction);
12493
12494   /* PR 10443: Do not silently ignore shifted operands.  */
12495   constraint (inst.operands[1].shifted,
12496               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12497
12498   if (inst.operands[1].isreg)
12499     {
12500       if (Rn < 8 && Rm < 8)
12501         {
12502           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12503              since a MOV instruction produces unpredictable results.  */
12504           if (inst.instruction == T_OPCODE_MOV_I8)
12505             inst.instruction = T_OPCODE_ADD_I3;
12506           else
12507             inst.instruction = T_OPCODE_CMP_LR;
12508
12509           inst.instruction |= Rn;
12510           inst.instruction |= Rm << 3;
12511         }
12512       else
12513         {
12514           if (inst.instruction == T_OPCODE_MOV_I8)
12515             inst.instruction = T_OPCODE_MOV_HR;
12516           else
12517             inst.instruction = T_OPCODE_CMP_HR;
12518           do_t_cpy ();
12519         }
12520     }
12521   else
12522     {
12523       constraint (Rn > 7,
12524                   _("only lo regs allowed with immediate"));
12525       inst.instruction |= Rn << 8;
12526       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_IMM;
12527     }
12528 }
12529
12530 static void
12531 do_t_mov16 (void)
12532 {
12533   unsigned Rd;
12534   bfd_vma imm;
12535   bfd_boolean top;
12536
12537   top = (inst.instruction & 0x00800000) != 0;
12538   if (inst.relocs[0].type == BFD_RELOC_ARM_MOVW)
12539     {
12540       constraint (top, _(":lower16: not allowed in this instruction"));
12541       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVW;
12542     }
12543   else if (inst.relocs[0].type == BFD_RELOC_ARM_MOVT)
12544     {
12545       constraint (!top, _(":upper16: not allowed in this instruction"));
12546       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_MOVT;
12547     }
12548
12549   Rd = inst.operands[0].reg;
12550   reject_bad_reg (Rd);
12551
12552   inst.instruction |= Rd << 8;
12553   if (inst.relocs[0].type == BFD_RELOC_UNUSED)
12554     {
12555       imm = inst.relocs[0].exp.X_add_number;
12556       inst.instruction |= (imm & 0xf000) << 4;
12557       inst.instruction |= (imm & 0x0800) << 15;
12558       inst.instruction |= (imm & 0x0700) << 4;
12559       inst.instruction |= (imm & 0x00ff);
12560     }
12561 }
12562
12563 static void
12564 do_t_mvn_tst (void)
12565 {
12566   unsigned Rn, Rm;
12567
12568   Rn = inst.operands[0].reg;
12569   Rm = inst.operands[1].reg;
12570
12571   if (inst.instruction == T_MNEM_cmp
12572       || inst.instruction == T_MNEM_cmn)
12573     constraint (Rn == REG_PC, BAD_PC);
12574   else
12575     reject_bad_reg (Rn);
12576   reject_bad_reg (Rm);
12577
12578   if (unified_syntax)
12579     {
12580       int r0off = (inst.instruction == T_MNEM_mvn
12581                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12582       bfd_boolean narrow;
12583
12584       if (inst.size_req == 4
12585           || inst.instruction > 0xffff
12586           || inst.operands[1].shifted
12587           || Rn > 7 || Rm > 7)
12588         narrow = FALSE;
12589       else if (inst.instruction == T_MNEM_cmn
12590                || inst.instruction == T_MNEM_tst)
12591         narrow = TRUE;
12592       else if (THUMB_SETS_FLAGS (inst.instruction))
12593         narrow = !in_pred_block ();
12594       else
12595         narrow = in_pred_block ();
12596
12597       if (!inst.operands[1].isreg)
12598         {
12599           /* For an immediate, we always generate a 32-bit opcode;
12600              section relaxation will shrink it later if possible.  */
12601           if (inst.instruction < 0xffff)
12602             inst.instruction = THUMB_OP32 (inst.instruction);
12603           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12604           inst.instruction |= Rn << r0off;
12605           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12606         }
12607       else
12608         {
12609           /* See if we can do this with a 16-bit instruction.  */
12610           if (narrow)
12611             {
12612               inst.instruction = THUMB_OP16 (inst.instruction);
12613               inst.instruction |= Rn;
12614               inst.instruction |= Rm << 3;
12615             }
12616           else
12617             {
12618               constraint (inst.operands[1].shifted
12619                           && inst.operands[1].immisreg,
12620                           _("shift must be constant"));
12621               if (inst.instruction < 0xffff)
12622                 inst.instruction = THUMB_OP32 (inst.instruction);
12623               inst.instruction |= Rn << r0off;
12624               encode_thumb32_shifted_operand (1);
12625             }
12626         }
12627     }
12628   else
12629     {
12630       constraint (inst.instruction > 0xffff
12631                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12632       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12633                   _("unshifted register required"));
12634       constraint (Rn > 7 || Rm > 7,
12635                   BAD_HIREG);
12636
12637       inst.instruction = THUMB_OP16 (inst.instruction);
12638       inst.instruction |= Rn;
12639       inst.instruction |= Rm << 3;
12640     }
12641 }
12642
12643 static void
12644 do_t_mrs (void)
12645 {
12646   unsigned Rd;
12647
12648   if (do_vfp_nsyn_mrs () == SUCCESS)
12649     return;
12650
12651   Rd = inst.operands[0].reg;
12652   reject_bad_reg (Rd);
12653   inst.instruction |= Rd << 8;
12654
12655   if (inst.operands[1].isreg)
12656     {
12657       unsigned br = inst.operands[1].reg;
12658       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12659         as_bad (_("bad register for mrs"));
12660
12661       inst.instruction |= br & (0xf << 16);
12662       inst.instruction |= (br & 0x300) >> 4;
12663       inst.instruction |= (br & SPSR_BIT) >> 2;
12664     }
12665   else
12666     {
12667       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12668
12669       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12670         {
12671           /* PR gas/12698:  The constraint is only applied for m_profile.
12672              If the user has specified -march=all, we want to ignore it as
12673              we are building for any CPU type, including non-m variants.  */
12674           bfd_boolean m_profile =
12675             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12676           constraint ((flags != 0) && m_profile, _("selected processor does "
12677                                                    "not support requested special purpose register"));
12678         }
12679       else
12680         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12681            devices).  */
12682         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12683                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12684
12685       inst.instruction |= (flags & SPSR_BIT) >> 2;
12686       inst.instruction |= inst.operands[1].imm & 0xff;
12687       inst.instruction |= 0xf0000;
12688     }
12689 }
12690
12691 static void
12692 do_t_msr (void)
12693 {
12694   int flags;
12695   unsigned Rn;
12696
12697   if (do_vfp_nsyn_msr () == SUCCESS)
12698     return;
12699
12700   constraint (!inst.operands[1].isreg,
12701               _("Thumb encoding does not support an immediate here"));
12702
12703   if (inst.operands[0].isreg)
12704     flags = (int)(inst.operands[0].reg);
12705   else
12706     flags = inst.operands[0].imm;
12707
12708   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12709     {
12710       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12711
12712       /* PR gas/12698:  The constraint is only applied for m_profile.
12713          If the user has specified -march=all, we want to ignore it as
12714          we are building for any CPU type, including non-m variants.  */
12715       bfd_boolean m_profile =
12716         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12717       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12718            && (bits & ~(PSR_s | PSR_f)) != 0)
12719           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12720               && bits != PSR_f)) && m_profile,
12721           _("selected processor does not support requested special "
12722             "purpose register"));
12723     }
12724   else
12725      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12726                  "requested special purpose register"));
12727
12728   Rn = inst.operands[1].reg;
12729   reject_bad_reg (Rn);
12730
12731   inst.instruction |= (flags & SPSR_BIT) >> 2;
12732   inst.instruction |= (flags & 0xf0000) >> 8;
12733   inst.instruction |= (flags & 0x300) >> 4;
12734   inst.instruction |= (flags & 0xff);
12735   inst.instruction |= Rn << 16;
12736 }
12737
12738 static void
12739 do_t_mul (void)
12740 {
12741   bfd_boolean narrow;
12742   unsigned Rd, Rn, Rm;
12743
12744   if (!inst.operands[2].present)
12745     inst.operands[2].reg = inst.operands[0].reg;
12746
12747   Rd = inst.operands[0].reg;
12748   Rn = inst.operands[1].reg;
12749   Rm = inst.operands[2].reg;
12750
12751   if (unified_syntax)
12752     {
12753       if (inst.size_req == 4
12754           || (Rd != Rn
12755               && Rd != Rm)
12756           || Rn > 7
12757           || Rm > 7)
12758         narrow = FALSE;
12759       else if (inst.instruction == T_MNEM_muls)
12760         narrow = !in_pred_block ();
12761       else
12762         narrow = in_pred_block ();
12763     }
12764   else
12765     {
12766       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12767       constraint (Rn > 7 || Rm > 7,
12768                   BAD_HIREG);
12769       narrow = TRUE;
12770     }
12771
12772   if (narrow)
12773     {
12774       /* 16-bit MULS/Conditional MUL.  */
12775       inst.instruction = THUMB_OP16 (inst.instruction);
12776       inst.instruction |= Rd;
12777
12778       if (Rd == Rn)
12779         inst.instruction |= Rm << 3;
12780       else if (Rd == Rm)
12781         inst.instruction |= Rn << 3;
12782       else
12783         constraint (1, _("dest must overlap one source register"));
12784     }
12785   else
12786     {
12787       constraint (inst.instruction != T_MNEM_mul,
12788                   _("Thumb-2 MUL must not set flags"));
12789       /* 32-bit MUL.  */
12790       inst.instruction = THUMB_OP32 (inst.instruction);
12791       inst.instruction |= Rd << 8;
12792       inst.instruction |= Rn << 16;
12793       inst.instruction |= Rm << 0;
12794
12795       reject_bad_reg (Rd);
12796       reject_bad_reg (Rn);
12797       reject_bad_reg (Rm);
12798     }
12799 }
12800
12801 static void
12802 do_t_mull (void)
12803 {
12804   unsigned RdLo, RdHi, Rn, Rm;
12805
12806   RdLo = inst.operands[0].reg;
12807   RdHi = inst.operands[1].reg;
12808   Rn = inst.operands[2].reg;
12809   Rm = inst.operands[3].reg;
12810
12811   reject_bad_reg (RdLo);
12812   reject_bad_reg (RdHi);
12813   reject_bad_reg (Rn);
12814   reject_bad_reg (Rm);
12815
12816   inst.instruction |= RdLo << 12;
12817   inst.instruction |= RdHi << 8;
12818   inst.instruction |= Rn << 16;
12819   inst.instruction |= Rm;
12820
12821  if (RdLo == RdHi)
12822     as_tsktsk (_("rdhi and rdlo must be different"));
12823 }
12824
12825 static void
12826 do_t_nop (void)
12827 {
12828   set_pred_insn_type (NEUTRAL_IT_INSN);
12829
12830   if (unified_syntax)
12831     {
12832       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12833         {
12834           inst.instruction = THUMB_OP32 (inst.instruction);
12835           inst.instruction |= inst.operands[0].imm;
12836         }
12837       else
12838         {
12839           /* PR9722: Check for Thumb2 availability before
12840              generating a thumb2 nop instruction.  */
12841           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12842             {
12843               inst.instruction = THUMB_OP16 (inst.instruction);
12844               inst.instruction |= inst.operands[0].imm << 4;
12845             }
12846           else
12847             inst.instruction = 0x46c0;
12848         }
12849     }
12850   else
12851     {
12852       constraint (inst.operands[0].present,
12853                   _("Thumb does not support NOP with hints"));
12854       inst.instruction = 0x46c0;
12855     }
12856 }
12857
12858 static void
12859 do_t_neg (void)
12860 {
12861   if (unified_syntax)
12862     {
12863       bfd_boolean narrow;
12864
12865       if (THUMB_SETS_FLAGS (inst.instruction))
12866         narrow = !in_pred_block ();
12867       else
12868         narrow = in_pred_block ();
12869       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12870         narrow = FALSE;
12871       if (inst.size_req == 4)
12872         narrow = FALSE;
12873
12874       if (!narrow)
12875         {
12876           inst.instruction = THUMB_OP32 (inst.instruction);
12877           inst.instruction |= inst.operands[0].reg << 8;
12878           inst.instruction |= inst.operands[1].reg << 16;
12879         }
12880       else
12881         {
12882           inst.instruction = THUMB_OP16 (inst.instruction);
12883           inst.instruction |= inst.operands[0].reg;
12884           inst.instruction |= inst.operands[1].reg << 3;
12885         }
12886     }
12887   else
12888     {
12889       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12890                   BAD_HIREG);
12891       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12892
12893       inst.instruction = THUMB_OP16 (inst.instruction);
12894       inst.instruction |= inst.operands[0].reg;
12895       inst.instruction |= inst.operands[1].reg << 3;
12896     }
12897 }
12898
12899 static void
12900 do_t_orn (void)
12901 {
12902   unsigned Rd, Rn;
12903
12904   Rd = inst.operands[0].reg;
12905   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12906
12907   reject_bad_reg (Rd);
12908   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12909   reject_bad_reg (Rn);
12910
12911   inst.instruction |= Rd << 8;
12912   inst.instruction |= Rn << 16;
12913
12914   if (!inst.operands[2].isreg)
12915     {
12916       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12917       inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
12918     }
12919   else
12920     {
12921       unsigned Rm;
12922
12923       Rm = inst.operands[2].reg;
12924       reject_bad_reg (Rm);
12925
12926       constraint (inst.operands[2].shifted
12927                   && inst.operands[2].immisreg,
12928                   _("shift must be constant"));
12929       encode_thumb32_shifted_operand (2);
12930     }
12931 }
12932
12933 static void
12934 do_t_pkhbt (void)
12935 {
12936   unsigned Rd, Rn, Rm;
12937
12938   Rd = inst.operands[0].reg;
12939   Rn = inst.operands[1].reg;
12940   Rm = inst.operands[2].reg;
12941
12942   reject_bad_reg (Rd);
12943   reject_bad_reg (Rn);
12944   reject_bad_reg (Rm);
12945
12946   inst.instruction |= Rd << 8;
12947   inst.instruction |= Rn << 16;
12948   inst.instruction |= Rm;
12949   if (inst.operands[3].present)
12950     {
12951       unsigned int val = inst.relocs[0].exp.X_add_number;
12952       constraint (inst.relocs[0].exp.X_op != O_constant,
12953                   _("expression too complex"));
12954       inst.instruction |= (val & 0x1c) << 10;
12955       inst.instruction |= (val & 0x03) << 6;
12956     }
12957 }
12958
12959 static void
12960 do_t_pkhtb (void)
12961 {
12962   if (!inst.operands[3].present)
12963     {
12964       unsigned Rtmp;
12965
12966       inst.instruction &= ~0x00000020;
12967
12968       /* PR 10168.  Swap the Rm and Rn registers.  */
12969       Rtmp = inst.operands[1].reg;
12970       inst.operands[1].reg = inst.operands[2].reg;
12971       inst.operands[2].reg = Rtmp;
12972     }
12973   do_t_pkhbt ();
12974 }
12975
12976 static void
12977 do_t_pld (void)
12978 {
12979   if (inst.operands[0].immisreg)
12980     reject_bad_reg (inst.operands[0].imm);
12981
12982   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12983 }
12984
12985 static void
12986 do_t_push_pop (void)
12987 {
12988   unsigned mask;
12989
12990   constraint (inst.operands[0].writeback,
12991               _("push/pop do not support {reglist}^"));
12992   constraint (inst.relocs[0].type != BFD_RELOC_UNUSED,
12993               _("expression too complex"));
12994
12995   mask = inst.operands[0].imm;
12996   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12997     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12998   else if (inst.size_req != 4
12999            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
13000                                        ? REG_LR : REG_PC)))
13001     {
13002       inst.instruction = THUMB_OP16 (inst.instruction);
13003       inst.instruction |= THUMB_PP_PC_LR;
13004       inst.instruction |= mask & 0xff;
13005     }
13006   else if (unified_syntax)
13007     {
13008       inst.instruction = THUMB_OP32 (inst.instruction);
13009       encode_thumb2_multi (TRUE /* do_io */, 13, mask, TRUE);
13010     }
13011   else
13012     {
13013       inst.error = _("invalid register list to push/pop instruction");
13014       return;
13015     }
13016 }
13017
13018 static void
13019 do_t_clrm (void)
13020 {
13021   if (unified_syntax)
13022     encode_thumb2_multi (FALSE /* do_io */, -1, inst.operands[0].imm, FALSE);
13023   else
13024     {
13025       inst.error = _("invalid register list to push/pop instruction");
13026       return;
13027     }
13028 }
13029
13030 static void
13031 do_t_vscclrm (void)
13032 {
13033   if (inst.operands[0].issingle)
13034     {
13035       inst.instruction |= (inst.operands[0].reg & 0x1) << 22;
13036       inst.instruction |= (inst.operands[0].reg & 0x1e) << 11;
13037       inst.instruction |= inst.operands[0].imm;
13038     }
13039   else
13040     {
13041       inst.instruction |= (inst.operands[0].reg & 0x10) << 18;
13042       inst.instruction |= (inst.operands[0].reg & 0xf) << 12;
13043       inst.instruction |= 1 << 8;
13044       inst.instruction |= inst.operands[0].imm << 1;
13045     }
13046 }
13047
13048 static void
13049 do_t_rbit (void)
13050 {
13051   unsigned Rd, Rm;
13052
13053   Rd = inst.operands[0].reg;
13054   Rm = inst.operands[1].reg;
13055
13056   reject_bad_reg (Rd);
13057   reject_bad_reg (Rm);
13058
13059   inst.instruction |= Rd << 8;
13060   inst.instruction |= Rm << 16;
13061   inst.instruction |= Rm;
13062 }
13063
13064 static void
13065 do_t_rev (void)
13066 {
13067   unsigned Rd, Rm;
13068
13069   Rd = inst.operands[0].reg;
13070   Rm = inst.operands[1].reg;
13071
13072   reject_bad_reg (Rd);
13073   reject_bad_reg (Rm);
13074
13075   if (Rd <= 7 && Rm <= 7
13076       && inst.size_req != 4)
13077     {
13078       inst.instruction = THUMB_OP16 (inst.instruction);
13079       inst.instruction |= Rd;
13080       inst.instruction |= Rm << 3;
13081     }
13082   else if (unified_syntax)
13083     {
13084       inst.instruction = THUMB_OP32 (inst.instruction);
13085       inst.instruction |= Rd << 8;
13086       inst.instruction |= Rm << 16;
13087       inst.instruction |= Rm;
13088     }
13089   else
13090     inst.error = BAD_HIREG;
13091 }
13092
13093 static void
13094 do_t_rrx (void)
13095 {
13096   unsigned Rd, Rm;
13097
13098   Rd = inst.operands[0].reg;
13099   Rm = inst.operands[1].reg;
13100
13101   reject_bad_reg (Rd);
13102   reject_bad_reg (Rm);
13103
13104   inst.instruction |= Rd << 8;
13105   inst.instruction |= Rm;
13106 }
13107
13108 static void
13109 do_t_rsb (void)
13110 {
13111   unsigned Rd, Rs;
13112
13113   Rd = inst.operands[0].reg;
13114   Rs = (inst.operands[1].present
13115         ? inst.operands[1].reg    /* Rd, Rs, foo */
13116         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
13117
13118   reject_bad_reg (Rd);
13119   reject_bad_reg (Rs);
13120   if (inst.operands[2].isreg)
13121     reject_bad_reg (inst.operands[2].reg);
13122
13123   inst.instruction |= Rd << 8;
13124   inst.instruction |= Rs << 16;
13125   if (!inst.operands[2].isreg)
13126     {
13127       bfd_boolean narrow;
13128
13129       if ((inst.instruction & 0x00100000) != 0)
13130         narrow = !in_pred_block ();
13131       else
13132         narrow = in_pred_block ();
13133
13134       if (Rd > 7 || Rs > 7)
13135         narrow = FALSE;
13136
13137       if (inst.size_req == 4 || !unified_syntax)
13138         narrow = FALSE;
13139
13140       if (inst.relocs[0].exp.X_op != O_constant
13141           || inst.relocs[0].exp.X_add_number != 0)
13142         narrow = FALSE;
13143
13144       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
13145          relaxation, but it doesn't seem worth the hassle.  */
13146       if (narrow)
13147         {
13148           inst.relocs[0].type = BFD_RELOC_UNUSED;
13149           inst.instruction = THUMB_OP16 (T_MNEM_negs);
13150           inst.instruction |= Rs << 3;
13151           inst.instruction |= Rd;
13152         }
13153       else
13154         {
13155           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
13156           inst.relocs[0].type = BFD_RELOC_ARM_T32_IMMEDIATE;
13157         }
13158     }
13159   else
13160     encode_thumb32_shifted_operand (2);
13161 }
13162
13163 static void
13164 do_t_setend (void)
13165 {
13166   if (warn_on_deprecated
13167       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13168       as_tsktsk (_("setend use is deprecated for ARMv8"));
13169
13170   set_pred_insn_type (OUTSIDE_PRED_INSN);
13171   if (inst.operands[0].imm)
13172     inst.instruction |= 0x8;
13173 }
13174
13175 static void
13176 do_t_shift (void)
13177 {
13178   if (!inst.operands[1].present)
13179     inst.operands[1].reg = inst.operands[0].reg;
13180
13181   if (unified_syntax)
13182     {
13183       bfd_boolean narrow;
13184       int shift_kind;
13185
13186       switch (inst.instruction)
13187         {
13188         case T_MNEM_asr:
13189         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
13190         case T_MNEM_lsl:
13191         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
13192         case T_MNEM_lsr:
13193         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
13194         case T_MNEM_ror:
13195         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
13196         default: abort ();
13197         }
13198
13199       if (THUMB_SETS_FLAGS (inst.instruction))
13200         narrow = !in_pred_block ();
13201       else
13202         narrow = in_pred_block ();
13203       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
13204         narrow = FALSE;
13205       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
13206         narrow = FALSE;
13207       if (inst.operands[2].isreg
13208           && (inst.operands[1].reg != inst.operands[0].reg
13209               || inst.operands[2].reg > 7))
13210         narrow = FALSE;
13211       if (inst.size_req == 4)
13212         narrow = FALSE;
13213
13214       reject_bad_reg (inst.operands[0].reg);
13215       reject_bad_reg (inst.operands[1].reg);
13216
13217       if (!narrow)
13218         {
13219           if (inst.operands[2].isreg)
13220             {
13221               reject_bad_reg (inst.operands[2].reg);
13222               inst.instruction = THUMB_OP32 (inst.instruction);
13223               inst.instruction |= inst.operands[0].reg << 8;
13224               inst.instruction |= inst.operands[1].reg << 16;
13225               inst.instruction |= inst.operands[2].reg;
13226
13227               /* PR 12854: Error on extraneous shifts.  */
13228               constraint (inst.operands[2].shifted,
13229                           _("extraneous shift as part of operand to shift insn"));
13230             }
13231           else
13232             {
13233               inst.operands[1].shifted = 1;
13234               inst.operands[1].shift_kind = shift_kind;
13235               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
13236                                              ? T_MNEM_movs : T_MNEM_mov);
13237               inst.instruction |= inst.operands[0].reg << 8;
13238               encode_thumb32_shifted_operand (1);
13239               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
13240               inst.relocs[0].type = BFD_RELOC_UNUSED;
13241             }
13242         }
13243       else
13244         {
13245           if (inst.operands[2].isreg)
13246             {
13247               switch (shift_kind)
13248                 {
13249                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
13250                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
13251                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
13252                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
13253                 default: abort ();
13254                 }
13255
13256               inst.instruction |= inst.operands[0].reg;
13257               inst.instruction |= inst.operands[2].reg << 3;
13258
13259               /* PR 12854: Error on extraneous shifts.  */
13260               constraint (inst.operands[2].shifted,
13261                           _("extraneous shift as part of operand to shift insn"));
13262             }
13263           else
13264             {
13265               switch (shift_kind)
13266                 {
13267                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
13268                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
13269                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
13270                 default: abort ();
13271                 }
13272               inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13273               inst.instruction |= inst.operands[0].reg;
13274               inst.instruction |= inst.operands[1].reg << 3;
13275             }
13276         }
13277     }
13278   else
13279     {
13280       constraint (inst.operands[0].reg > 7
13281                   || inst.operands[1].reg > 7, BAD_HIREG);
13282       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
13283
13284       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
13285         {
13286           constraint (inst.operands[2].reg > 7, BAD_HIREG);
13287           constraint (inst.operands[0].reg != inst.operands[1].reg,
13288                       _("source1 and dest must be same register"));
13289
13290           switch (inst.instruction)
13291             {
13292             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
13293             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
13294             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
13295             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
13296             default: abort ();
13297             }
13298
13299           inst.instruction |= inst.operands[0].reg;
13300           inst.instruction |= inst.operands[2].reg << 3;
13301
13302           /* PR 12854: Error on extraneous shifts.  */
13303           constraint (inst.operands[2].shifted,
13304                       _("extraneous shift as part of operand to shift insn"));
13305         }
13306       else
13307         {
13308           switch (inst.instruction)
13309             {
13310             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
13311             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
13312             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
13313             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
13314             default: abort ();
13315             }
13316           inst.relocs[0].type = BFD_RELOC_ARM_THUMB_SHIFT;
13317           inst.instruction |= inst.operands[0].reg;
13318           inst.instruction |= inst.operands[1].reg << 3;
13319         }
13320     }
13321 }
13322
13323 static void
13324 do_t_simd (void)
13325 {
13326   unsigned Rd, Rn, Rm;
13327
13328   Rd = inst.operands[0].reg;
13329   Rn = inst.operands[1].reg;
13330   Rm = inst.operands[2].reg;
13331
13332   reject_bad_reg (Rd);
13333   reject_bad_reg (Rn);
13334   reject_bad_reg (Rm);
13335
13336   inst.instruction |= Rd << 8;
13337   inst.instruction |= Rn << 16;
13338   inst.instruction |= Rm;
13339 }
13340
13341 static void
13342 do_t_simd2 (void)
13343 {
13344   unsigned Rd, Rn, Rm;
13345
13346   Rd = inst.operands[0].reg;
13347   Rm = inst.operands[1].reg;
13348   Rn = inst.operands[2].reg;
13349
13350   reject_bad_reg (Rd);
13351   reject_bad_reg (Rn);
13352   reject_bad_reg (Rm);
13353
13354   inst.instruction |= Rd << 8;
13355   inst.instruction |= Rn << 16;
13356   inst.instruction |= Rm;
13357 }
13358
13359 static void
13360 do_t_smc (void)
13361 {
13362   unsigned int value = inst.relocs[0].exp.X_add_number;
13363   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13364               _("SMC is not permitted on this architecture"));
13365   constraint (inst.relocs[0].exp.X_op != O_constant,
13366               _("expression too complex"));
13367   inst.relocs[0].type = BFD_RELOC_UNUSED;
13368   inst.instruction |= (value & 0xf000) >> 12;
13369   inst.instruction |= (value & 0x0ff0);
13370   inst.instruction |= (value & 0x000f) << 16;
13371   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13372   set_pred_insn_type_last ();
13373 }
13374
13375 static void
13376 do_t_hvc (void)
13377 {
13378   unsigned int value = inst.relocs[0].exp.X_add_number;
13379
13380   inst.relocs[0].type = BFD_RELOC_UNUSED;
13381   inst.instruction |= (value & 0x0fff);
13382   inst.instruction |= (value & 0xf000) << 4;
13383 }
13384
13385 static void
13386 do_t_ssat_usat (int bias)
13387 {
13388   unsigned Rd, Rn;
13389
13390   Rd = inst.operands[0].reg;
13391   Rn = inst.operands[2].reg;
13392
13393   reject_bad_reg (Rd);
13394   reject_bad_reg (Rn);
13395
13396   inst.instruction |= Rd << 8;
13397   inst.instruction |= inst.operands[1].imm - bias;
13398   inst.instruction |= Rn << 16;
13399
13400   if (inst.operands[3].present)
13401     {
13402       offsetT shift_amount = inst.relocs[0].exp.X_add_number;
13403
13404       inst.relocs[0].type = BFD_RELOC_UNUSED;
13405
13406       constraint (inst.relocs[0].exp.X_op != O_constant,
13407                   _("expression too complex"));
13408
13409       if (shift_amount != 0)
13410         {
13411           constraint (shift_amount > 31,
13412                       _("shift expression is too large"));
13413
13414           if (inst.operands[3].shift_kind == SHIFT_ASR)
13415             inst.instruction |= 0x00200000;  /* sh bit.  */
13416
13417           inst.instruction |= (shift_amount & 0x1c) << 10;
13418           inst.instruction |= (shift_amount & 0x03) << 6;
13419         }
13420     }
13421 }
13422
13423 static void
13424 do_t_ssat (void)
13425 {
13426   do_t_ssat_usat (1);
13427 }
13428
13429 static void
13430 do_t_ssat16 (void)
13431 {
13432   unsigned Rd, Rn;
13433
13434   Rd = inst.operands[0].reg;
13435   Rn = inst.operands[2].reg;
13436
13437   reject_bad_reg (Rd);
13438   reject_bad_reg (Rn);
13439
13440   inst.instruction |= Rd << 8;
13441   inst.instruction |= inst.operands[1].imm - 1;
13442   inst.instruction |= Rn << 16;
13443 }
13444
13445 static void
13446 do_t_strex (void)
13447 {
13448   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
13449               || inst.operands[2].postind || inst.operands[2].writeback
13450               || inst.operands[2].immisreg || inst.operands[2].shifted
13451               || inst.operands[2].negative,
13452               BAD_ADDR_MODE);
13453
13454   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
13455
13456   inst.instruction |= inst.operands[0].reg << 8;
13457   inst.instruction |= inst.operands[1].reg << 12;
13458   inst.instruction |= inst.operands[2].reg << 16;
13459   inst.relocs[0].type = BFD_RELOC_ARM_T32_OFFSET_U8;
13460 }
13461
13462 static void
13463 do_t_strexd (void)
13464 {
13465   if (!inst.operands[2].present)
13466     inst.operands[2].reg = inst.operands[1].reg + 1;
13467
13468   constraint (inst.operands[0].reg == inst.operands[1].reg
13469               || inst.operands[0].reg == inst.operands[2].reg
13470               || inst.operands[0].reg == inst.operands[3].reg,
13471               BAD_OVERLAP);
13472
13473   inst.instruction |= inst.operands[0].reg;
13474   inst.instruction |= inst.operands[1].reg << 12;
13475   inst.instruction |= inst.operands[2].reg << 8;
13476   inst.instruction |= inst.operands[3].reg << 16;
13477 }
13478
13479 static void
13480 do_t_sxtah (void)
13481 {
13482   unsigned Rd, Rn, Rm;
13483
13484   Rd = inst.operands[0].reg;
13485   Rn = inst.operands[1].reg;
13486   Rm = inst.operands[2].reg;
13487
13488   reject_bad_reg (Rd);
13489   reject_bad_reg (Rn);
13490   reject_bad_reg (Rm);
13491
13492   inst.instruction |= Rd << 8;
13493   inst.instruction |= Rn << 16;
13494   inst.instruction |= Rm;
13495   inst.instruction |= inst.operands[3].imm << 4;
13496 }
13497
13498 static void
13499 do_t_sxth (void)
13500 {
13501   unsigned Rd, Rm;
13502
13503   Rd = inst.operands[0].reg;
13504   Rm = inst.operands[1].reg;
13505
13506   reject_bad_reg (Rd);
13507   reject_bad_reg (Rm);
13508
13509   if (inst.instruction <= 0xffff
13510       && inst.size_req != 4
13511       && Rd <= 7 && Rm <= 7
13512       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13513     {
13514       inst.instruction = THUMB_OP16 (inst.instruction);
13515       inst.instruction |= Rd;
13516       inst.instruction |= Rm << 3;
13517     }
13518   else if (unified_syntax)
13519     {
13520       if (inst.instruction <= 0xffff)
13521         inst.instruction = THUMB_OP32 (inst.instruction);
13522       inst.instruction |= Rd << 8;
13523       inst.instruction |= Rm;
13524       inst.instruction |= inst.operands[2].imm << 4;
13525     }
13526   else
13527     {
13528       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13529                   _("Thumb encoding does not support rotation"));
13530       constraint (1, BAD_HIREG);
13531     }
13532 }
13533
13534 static void
13535 do_t_swi (void)
13536 {
13537   inst.relocs[0].type = BFD_RELOC_ARM_SWI;
13538 }
13539
13540 static void
13541 do_t_tb (void)
13542 {
13543   unsigned Rn, Rm;
13544   int half;
13545
13546   half = (inst.instruction & 0x10) != 0;
13547   set_pred_insn_type_last ();
13548   constraint (inst.operands[0].immisreg,
13549               _("instruction requires register index"));
13550
13551   Rn = inst.operands[0].reg;
13552   Rm = inst.operands[0].imm;
13553
13554   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13555     constraint (Rn == REG_SP, BAD_SP);
13556   reject_bad_reg (Rm);
13557
13558   constraint (!half && inst.operands[0].shifted,
13559               _("instruction does not allow shifted index"));
13560   inst.instruction |= (Rn << 16) | Rm;
13561 }
13562
13563 static void
13564 do_t_udf (void)
13565 {
13566   if (!inst.operands[0].present)
13567     inst.operands[0].imm = 0;
13568
13569   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13570     {
13571       constraint (inst.size_req == 2,
13572                   _("immediate value out of range"));
13573       inst.instruction = THUMB_OP32 (inst.instruction);
13574       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13575       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13576     }
13577   else
13578     {
13579       inst.instruction = THUMB_OP16 (inst.instruction);
13580       inst.instruction |= inst.operands[0].imm;
13581     }
13582
13583   set_pred_insn_type (NEUTRAL_IT_INSN);
13584 }
13585
13586
13587 static void
13588 do_t_usat (void)
13589 {
13590   do_t_ssat_usat (0);
13591 }
13592
13593 static void
13594 do_t_usat16 (void)
13595 {
13596   unsigned Rd, Rn;
13597
13598   Rd = inst.operands[0].reg;
13599   Rn = inst.operands[2].reg;
13600
13601   reject_bad_reg (Rd);
13602   reject_bad_reg (Rn);
13603
13604   inst.instruction |= Rd << 8;
13605   inst.instruction |= inst.operands[1].imm;
13606   inst.instruction |= Rn << 16;
13607 }
13608
13609 /* Checking the range of the branch offset (VAL) with NBITS bits
13610    and IS_SIGNED signedness.  Also checks the LSB to be 0.  */
13611 static int
13612 v8_1_branch_value_check (int val, int nbits, int is_signed)
13613 {
13614   gas_assert (nbits > 0 && nbits <= 32);
13615   if (is_signed)
13616     {
13617       int cmp = (1 << (nbits - 1));
13618       if ((val < -cmp) || (val >= cmp) || (val & 0x01))
13619         return FAIL;
13620     }
13621   else
13622     {
13623       if ((val <= 0) || (val >= (1 << nbits)) || (val & 0x1))
13624         return FAIL;
13625     }
13626     return SUCCESS;
13627 }
13628
13629 /* For branches in Armv8.1-M Mainline.  */
13630 static void
13631 do_t_branch_future (void)
13632 {
13633   unsigned long insn = inst.instruction;
13634
13635   inst.instruction = THUMB_OP32 (inst.instruction);
13636   if (inst.operands[0].hasreloc == 0)
13637     {
13638       if (v8_1_branch_value_check (inst.operands[0].imm, 5, FALSE) == FAIL)
13639         as_bad (BAD_BRANCH_OFF);
13640
13641       inst.instruction |= ((inst.operands[0].imm & 0x1f) >> 1) << 23;
13642     }
13643   else
13644     {
13645       inst.relocs[0].type = BFD_RELOC_THUMB_PCREL_BRANCH5;
13646       inst.relocs[0].pc_rel = 1;
13647     }
13648
13649   switch (insn)
13650     {
13651       case T_MNEM_bf:
13652         if (inst.operands[1].hasreloc == 0)
13653           {
13654             int val = inst.operands[1].imm;
13655             if (v8_1_branch_value_check (inst.operands[1].imm, 17, TRUE) == FAIL)
13656               as_bad (BAD_BRANCH_OFF);
13657
13658             int immA = (val & 0x0001f000) >> 12;
13659             int immB = (val & 0x00000ffc) >> 2;
13660             int immC = (val & 0x00000002) >> 1;
13661             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
13662           }
13663         else
13664           {
13665             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF17;
13666             inst.relocs[1].pc_rel = 1;
13667           }
13668         break;
13669
13670       case T_MNEM_bfl:
13671         if (inst.operands[1].hasreloc == 0)
13672           {
13673             int val = inst.operands[1].imm;
13674             if (v8_1_branch_value_check (inst.operands[1].imm, 19, TRUE) == FAIL)
13675               as_bad (BAD_BRANCH_OFF);
13676
13677             int immA = (val & 0x0007f000) >> 12;
13678             int immB = (val & 0x00000ffc) >> 2;
13679             int immC = (val & 0x00000002) >> 1;
13680             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
13681           }
13682           else
13683           {
13684             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF19;
13685             inst.relocs[1].pc_rel = 1;
13686           }
13687         break;
13688
13689       case T_MNEM_bfcsel:
13690         /* Operand 1.  */
13691         if (inst.operands[1].hasreloc == 0)
13692           {
13693             int val = inst.operands[1].imm;
13694             int immA = (val & 0x00001000) >> 12;
13695             int immB = (val & 0x00000ffc) >> 2;
13696             int immC = (val & 0x00000002) >> 1;
13697             inst.instruction |= (immA << 16) | (immB << 1) | (immC << 11);
13698           }
13699           else
13700           {
13701             inst.relocs[1].type = BFD_RELOC_ARM_THUMB_BF13;
13702             inst.relocs[1].pc_rel = 1;
13703           }
13704
13705         /* Operand 2.  */
13706         if (inst.operands[2].hasreloc == 0)
13707           {
13708               constraint ((inst.operands[0].hasreloc != 0), BAD_ARGS);
13709               int val2 = inst.operands[2].imm;
13710               int val0 = inst.operands[0].imm & 0x1f;
13711               int diff = val2 - val0;
13712               if (diff == 4)
13713                 inst.instruction |= 1 << 17; /* T bit.  */
13714               else if (diff != 2)
13715                 as_bad (_("out of range label-relative fixup value"));
13716           }
13717         else
13718           {
13719               constraint ((inst.operands[0].hasreloc == 0), BAD_ARGS);
13720               inst.relocs[2].type = BFD_RELOC_THUMB_PCREL_BFCSEL;
13721               inst.relocs[2].pc_rel = 1;
13722           }
13723
13724         /* Operand 3.  */
13725         constraint (inst.cond != COND_ALWAYS, BAD_COND);
13726         inst.instruction |= (inst.operands[3].imm & 0xf) << 18;
13727         break;
13728
13729       case T_MNEM_bfx:
13730       case T_MNEM_bflx:
13731         inst.instruction |= inst.operands[1].reg << 16;
13732         break;
13733
13734       default: abort ();
13735     }
13736 }
13737
13738 /* Helper function for do_t_loloop to handle relocations.  */
13739 static void
13740 v8_1_loop_reloc (int is_le)
13741 {
13742   if (inst.relocs[0].exp.X_op == O_constant)
13743     {
13744       int value = inst.relocs[0].exp.X_add_number;
13745       value = (is_le) ? -value : value;
13746
13747       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
13748         as_bad (BAD_BRANCH_OFF);
13749
13750       int imml, immh;
13751
13752       immh = (value & 0x00000ffc) >> 2;
13753       imml = (value & 0x00000002) >> 1;
13754
13755       inst.instruction |= (imml << 11) | (immh << 1);
13756     }
13757   else
13758     {
13759       inst.relocs[0].type = BFD_RELOC_ARM_THUMB_LOOP12;
13760       inst.relocs[0].pc_rel = 1;
13761     }
13762 }
13763
13764 /* To handle the Scalar Low Overhead Loop instructions
13765    in Armv8.1-M Mainline.  */
13766 static void
13767 do_t_loloop (void)
13768 {
13769   unsigned long insn = inst.instruction;
13770
13771   set_pred_insn_type (OUTSIDE_PRED_INSN);
13772   inst.instruction = THUMB_OP32 (inst.instruction);
13773
13774   switch (insn)
13775     {
13776     case T_MNEM_le:
13777       /* le <label>.  */
13778       if (!inst.operands[0].present)
13779         inst.instruction |= 1 << 21;
13780
13781       v8_1_loop_reloc (TRUE);
13782       break;
13783
13784     case T_MNEM_wls:
13785       v8_1_loop_reloc (FALSE);
13786       /* Fall through.  */
13787     case T_MNEM_dls:
13788       constraint (inst.operands[1].isreg != 1, BAD_ARGS);
13789       inst.instruction |= (inst.operands[1].reg << 16);
13790       break;
13791
13792     default: abort();
13793     }
13794 }
13795
13796 /* Neon instruction encoder helpers.  */
13797
13798 /* Encodings for the different types for various Neon opcodes.  */
13799
13800 /* An "invalid" code for the following tables.  */
13801 #define N_INV -1u
13802
13803 struct neon_tab_entry
13804 {
13805   unsigned integer;
13806   unsigned float_or_poly;
13807   unsigned scalar_or_imm;
13808 };
13809
13810 /* Map overloaded Neon opcodes to their respective encodings.  */
13811 #define NEON_ENC_TAB                                    \
13812   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13813   X(vabdl,      0x0800700, N_INV,     N_INV),           \
13814   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13815   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13816   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13817   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13818   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13819   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13820   X(vaddl,      0x0800000, N_INV,     N_INV),           \
13821   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13822   X(vsubl,      0x0800200, N_INV,     N_INV),           \
13823   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13824   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13825   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13826   /* Register variants of the following two instructions are encoded as
13827      vcge / vcgt with the operands reversed.  */        \
13828   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13829   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13830   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13831   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13832   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13833   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13834   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13835   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13836   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13837   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13838   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13839   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13840   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13841   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13842   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13843   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13844   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13845   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13846   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13847   X(vand,       0x0000110, N_INV,     0x0800030),       \
13848   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13849   X(veor,       0x1000110, N_INV,     N_INV),           \
13850   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13851   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13852   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13853   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13854   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13855   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13856   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13857   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13858   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13859   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13860   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13861   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13862   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13863   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13864   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13865   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13866   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13867   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13868   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13869   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13870   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13871   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13872   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13873   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13874   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13875   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13876   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13877   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13878   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13879   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13880   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13881   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13882   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13883   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13884   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13885   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13886   X(aes,        0x3b00300, N_INV,     N_INV),           \
13887   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13888   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13889   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13890
13891 enum neon_opc
13892 {
13893 #define X(OPC,I,F,S) N_MNEM_##OPC
13894 NEON_ENC_TAB
13895 #undef X
13896 };
13897
13898 static const struct neon_tab_entry neon_enc_tab[] =
13899 {
13900 #define X(OPC,I,F,S) { (I), (F), (S) }
13901 NEON_ENC_TAB
13902 #undef X
13903 };
13904
13905 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13906 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13907 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13908 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13909 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13910 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13911 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13912 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13913 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13914 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13915 #define NEON_ENC_SINGLE_(X) \
13916   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13917 #define NEON_ENC_DOUBLE_(X) \
13918   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13919 #define NEON_ENC_FPV8_(X) \
13920   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13921
13922 #define NEON_ENCODE(type, inst)                                 \
13923   do                                                            \
13924     {                                                           \
13925       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13926       inst.is_neon = 1;                                         \
13927     }                                                           \
13928   while (0)
13929
13930 #define check_neon_suffixes                                             \
13931   do                                                                    \
13932     {                                                                   \
13933       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13934         {                                                               \
13935           as_bad (_("invalid neon suffix for non neon instruction"));   \
13936           return;                                                       \
13937         }                                                               \
13938     }                                                                   \
13939   while (0)
13940
13941 /* Define shapes for instruction operands. The following mnemonic characters
13942    are used in this table:
13943
13944      F - VFP S<n> register
13945      D - Neon D<n> register
13946      Q - Neon Q<n> register
13947      I - Immediate
13948      S - Scalar
13949      R - ARM register
13950      L - D<n> register list
13951
13952    This table is used to generate various data:
13953      - enumerations of the form NS_DDR to be used as arguments to
13954        neon_select_shape.
13955      - a table classifying shapes into single, double, quad, mixed.
13956      - a table used to drive neon_select_shape.  */
13957
13958 #define NEON_SHAPE_DEF                  \
13959   X(3, (D, D, D), DOUBLE),              \
13960   X(3, (Q, Q, Q), QUAD),                \
13961   X(3, (D, D, I), DOUBLE),              \
13962   X(3, (Q, Q, I), QUAD),                \
13963   X(3, (D, D, S), DOUBLE),              \
13964   X(3, (Q, Q, S), QUAD),                \
13965   X(3, (Q, Q, R), QUAD),                \
13966   X(2, (D, D), DOUBLE),                 \
13967   X(2, (Q, Q), QUAD),                   \
13968   X(2, (D, S), DOUBLE),                 \
13969   X(2, (Q, S), QUAD),                   \
13970   X(2, (D, R), DOUBLE),                 \
13971   X(2, (Q, R), QUAD),                   \
13972   X(2, (D, I), DOUBLE),                 \
13973   X(2, (Q, I), QUAD),                   \
13974   X(3, (D, L, D), DOUBLE),              \
13975   X(2, (D, Q), MIXED),                  \
13976   X(2, (Q, D), MIXED),                  \
13977   X(3, (D, Q, I), MIXED),               \
13978   X(3, (Q, D, I), MIXED),               \
13979   X(3, (Q, D, D), MIXED),               \
13980   X(3, (D, Q, Q), MIXED),               \
13981   X(3, (Q, Q, D), MIXED),               \
13982   X(3, (Q, D, S), MIXED),               \
13983   X(3, (D, Q, S), MIXED),               \
13984   X(4, (D, D, D, I), DOUBLE),           \
13985   X(4, (Q, Q, Q, I), QUAD),             \
13986   X(4, (D, D, S, I), DOUBLE),           \
13987   X(4, (Q, Q, S, I), QUAD),             \
13988   X(2, (F, F), SINGLE),                 \
13989   X(3, (F, F, F), SINGLE),              \
13990   X(2, (F, I), SINGLE),                 \
13991   X(2, (F, D), MIXED),                  \
13992   X(2, (D, F), MIXED),                  \
13993   X(3, (F, F, I), MIXED),               \
13994   X(4, (R, R, F, F), SINGLE),           \
13995   X(4, (F, F, R, R), SINGLE),           \
13996   X(3, (D, R, R), DOUBLE),              \
13997   X(3, (R, R, D), DOUBLE),              \
13998   X(2, (S, R), SINGLE),                 \
13999   X(2, (R, S), SINGLE),                 \
14000   X(2, (F, R), SINGLE),                 \
14001   X(2, (R, F), SINGLE),                 \
14002 /* Half float shape supported so far.  */\
14003   X (2, (H, D), MIXED),                 \
14004   X (2, (D, H), MIXED),                 \
14005   X (2, (H, F), MIXED),                 \
14006   X (2, (F, H), MIXED),                 \
14007   X (2, (H, H), HALF),                  \
14008   X (2, (H, R), HALF),                  \
14009   X (2, (R, H), HALF),                  \
14010   X (2, (H, I), HALF),                  \
14011   X (3, (H, H, H), HALF),               \
14012   X (3, (H, F, I), MIXED),              \
14013   X (3, (F, H, I), MIXED),              \
14014   X (3, (D, H, H), MIXED),              \
14015   X (3, (D, H, S), MIXED)
14016
14017 #define S2(A,B)         NS_##A##B
14018 #define S3(A,B,C)       NS_##A##B##C
14019 #define S4(A,B,C,D)     NS_##A##B##C##D
14020
14021 #define X(N, L, C) S##N L
14022
14023 enum neon_shape
14024 {
14025   NEON_SHAPE_DEF,
14026   NS_NULL
14027 };
14028
14029 #undef X
14030 #undef S2
14031 #undef S3
14032 #undef S4
14033
14034 enum neon_shape_class
14035 {
14036   SC_HALF,
14037   SC_SINGLE,
14038   SC_DOUBLE,
14039   SC_QUAD,
14040   SC_MIXED
14041 };
14042
14043 #define X(N, L, C) SC_##C
14044
14045 static enum neon_shape_class neon_shape_class[] =
14046 {
14047   NEON_SHAPE_DEF
14048 };
14049
14050 #undef X
14051
14052 enum neon_shape_el
14053 {
14054   SE_H,
14055   SE_F,
14056   SE_D,
14057   SE_Q,
14058   SE_I,
14059   SE_S,
14060   SE_R,
14061   SE_L
14062 };
14063
14064 /* Register widths of above.  */
14065 static unsigned neon_shape_el_size[] =
14066 {
14067   16,
14068   32,
14069   64,
14070   128,
14071   0,
14072   32,
14073   32,
14074   0
14075 };
14076
14077 struct neon_shape_info
14078 {
14079   unsigned els;
14080   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
14081 };
14082
14083 #define S2(A,B)         { SE_##A, SE_##B }
14084 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
14085 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
14086
14087 #define X(N, L, C) { N, S##N L }
14088
14089 static struct neon_shape_info neon_shape_tab[] =
14090 {
14091   NEON_SHAPE_DEF
14092 };
14093
14094 #undef X
14095 #undef S2
14096 #undef S3
14097 #undef S4
14098
14099 /* Bit masks used in type checking given instructions.
14100   'N_EQK' means the type must be the same as (or based on in some way) the key
14101    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
14102    set, various other bits can be set as well in order to modify the meaning of
14103    the type constraint.  */
14104
14105 enum neon_type_mask
14106 {
14107   N_S8   = 0x0000001,
14108   N_S16  = 0x0000002,
14109   N_S32  = 0x0000004,
14110   N_S64  = 0x0000008,
14111   N_U8   = 0x0000010,
14112   N_U16  = 0x0000020,
14113   N_U32  = 0x0000040,
14114   N_U64  = 0x0000080,
14115   N_I8   = 0x0000100,
14116   N_I16  = 0x0000200,
14117   N_I32  = 0x0000400,
14118   N_I64  = 0x0000800,
14119   N_8    = 0x0001000,
14120   N_16   = 0x0002000,
14121   N_32   = 0x0004000,
14122   N_64   = 0x0008000,
14123   N_P8   = 0x0010000,
14124   N_P16  = 0x0020000,
14125   N_F16  = 0x0040000,
14126   N_F32  = 0x0080000,
14127   N_F64  = 0x0100000,
14128   N_P64  = 0x0200000,
14129   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
14130   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
14131   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
14132   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
14133   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
14134   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
14135   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
14136   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
14137   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
14138   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
14139   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
14140   N_UTYP = 0,
14141   N_MAX_NONSPECIAL = N_P64
14142 };
14143
14144 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
14145
14146 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
14147 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14148 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
14149 #define N_S_32     (N_S8 | N_S16 | N_S32)
14150 #define N_F_16_32  (N_F16 | N_F32)
14151 #define N_SUF_32   (N_SU_32 | N_F_16_32)
14152 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
14153 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
14154 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
14155 #define N_I_MVE    (N_I8 | N_I16 | N_I32)
14156 #define N_F_MVE    (N_F16 | N_F32)
14157 #define N_SU_MVE   (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
14158
14159 /* Pass this as the first type argument to neon_check_type to ignore types
14160    altogether.  */
14161 #define N_IGNORE_TYPE (N_KEY | N_EQK)
14162
14163 /* Select a "shape" for the current instruction (describing register types or
14164    sizes) from a list of alternatives. Return NS_NULL if the current instruction
14165    doesn't fit. For non-polymorphic shapes, checking is usually done as a
14166    function of operand parsing, so this function doesn't need to be called.
14167    Shapes should be listed in order of decreasing length.  */
14168
14169 static enum neon_shape
14170 neon_select_shape (enum neon_shape shape, ...)
14171 {
14172   va_list ap;
14173   enum neon_shape first_shape = shape;
14174
14175   /* Fix missing optional operands. FIXME: we don't know at this point how
14176      many arguments we should have, so this makes the assumption that we have
14177      > 1. This is true of all current Neon opcodes, I think, but may not be
14178      true in the future.  */
14179   if (!inst.operands[1].present)
14180     inst.operands[1] = inst.operands[0];
14181
14182   va_start (ap, shape);
14183
14184   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
14185     {
14186       unsigned j;
14187       int matches = 1;
14188
14189       for (j = 0; j < neon_shape_tab[shape].els; j++)
14190         {
14191           if (!inst.operands[j].present)
14192             {
14193               matches = 0;
14194               break;
14195             }
14196
14197           switch (neon_shape_tab[shape].el[j])
14198             {
14199               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
14200                  a VFP single precision register operand, it's essentially
14201                  means only half of the register is used.
14202
14203                  If the type specifier is given after the mnemonics, the
14204                  information is stored in inst.vectype.  If the type specifier
14205                  is given after register operand, the information is stored
14206                  in inst.operands[].vectype.
14207
14208                  When there is only one type specifier, and all the register
14209                  operands are the same type of hardware register, the type
14210                  specifier applies to all register operands.
14211
14212                  If no type specifier is given, the shape is inferred from
14213                  operand information.
14214
14215                  for example:
14216                  vadd.f16 s0, s1, s2:           NS_HHH
14217                  vabs.f16 s0, s1:               NS_HH
14218                  vmov.f16 s0, r1:               NS_HR
14219                  vmov.f16 r0, s1:               NS_RH
14220                  vcvt.f16 r0, s1:               NS_RH
14221                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
14222                  vcvt.f16.s32   s2, s2:         NS_HF
14223               */
14224             case SE_H:
14225               if (!(inst.operands[j].isreg
14226                     && inst.operands[j].isvec
14227                     && inst.operands[j].issingle
14228                     && !inst.operands[j].isquad
14229                     && ((inst.vectype.elems == 1
14230                          && inst.vectype.el[0].size == 16)
14231                         || (inst.vectype.elems > 1
14232                             && inst.vectype.el[j].size == 16)
14233                         || (inst.vectype.elems == 0
14234                             && inst.operands[j].vectype.type != NT_invtype
14235                             && inst.operands[j].vectype.size == 16))))
14236                 matches = 0;
14237               break;
14238
14239             case SE_F:
14240               if (!(inst.operands[j].isreg
14241                     && inst.operands[j].isvec
14242                     && inst.operands[j].issingle
14243                     && !inst.operands[j].isquad
14244                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
14245                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
14246                         || (inst.vectype.elems == 0
14247                             && (inst.operands[j].vectype.size == 32
14248                                 || inst.operands[j].vectype.type == NT_invtype)))))
14249                 matches = 0;
14250               break;
14251
14252             case SE_D:
14253               if (!(inst.operands[j].isreg
14254                     && inst.operands[j].isvec
14255                     && !inst.operands[j].isquad
14256                     && !inst.operands[j].issingle))
14257                 matches = 0;
14258               break;
14259
14260             case SE_R:
14261               if (!(inst.operands[j].isreg
14262                     && !inst.operands[j].isvec))
14263                 matches = 0;
14264               break;
14265
14266             case SE_Q:
14267               if (!(inst.operands[j].isreg
14268                     && inst.operands[j].isvec
14269                     && inst.operands[j].isquad
14270                     && !inst.operands[j].issingle))
14271                 matches = 0;
14272               break;
14273
14274             case SE_I:
14275               if (!(!inst.operands[j].isreg
14276                     && !inst.operands[j].isscalar))
14277                 matches = 0;
14278               break;
14279
14280             case SE_S:
14281               if (!(!inst.operands[j].isreg
14282                     && inst.operands[j].isscalar))
14283                 matches = 0;
14284               break;
14285
14286             case SE_L:
14287               break;
14288             }
14289           if (!matches)
14290             break;
14291         }
14292       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
14293         /* We've matched all the entries in the shape table, and we don't
14294            have any left over operands which have not been matched.  */
14295         break;
14296     }
14297
14298   va_end (ap);
14299
14300   if (shape == NS_NULL && first_shape != NS_NULL)
14301     first_error (_("invalid instruction shape"));
14302
14303   return shape;
14304 }
14305
14306 /* True if SHAPE is predominantly a quadword operation (most of the time, this
14307    means the Q bit should be set).  */
14308
14309 static int
14310 neon_quad (enum neon_shape shape)
14311 {
14312   return neon_shape_class[shape] == SC_QUAD;
14313 }
14314
14315 static void
14316 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
14317                        unsigned *g_size)
14318 {
14319   /* Allow modification to be made to types which are constrained to be
14320      based on the key element, based on bits set alongside N_EQK.  */
14321   if ((typebits & N_EQK) != 0)
14322     {
14323       if ((typebits & N_HLF) != 0)
14324         *g_size /= 2;
14325       else if ((typebits & N_DBL) != 0)
14326         *g_size *= 2;
14327       if ((typebits & N_SGN) != 0)
14328         *g_type = NT_signed;
14329       else if ((typebits & N_UNS) != 0)
14330         *g_type = NT_unsigned;
14331       else if ((typebits & N_INT) != 0)
14332         *g_type = NT_integer;
14333       else if ((typebits & N_FLT) != 0)
14334         *g_type = NT_float;
14335       else if ((typebits & N_SIZ) != 0)
14336         *g_type = NT_untyped;
14337     }
14338 }
14339
14340 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
14341    operand type, i.e. the single type specified in a Neon instruction when it
14342    is the only one given.  */
14343
14344 static struct neon_type_el
14345 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
14346 {
14347   struct neon_type_el dest = *key;
14348
14349   gas_assert ((thisarg & N_EQK) != 0);
14350
14351   neon_modify_type_size (thisarg, &dest.type, &dest.size);
14352
14353   return dest;
14354 }
14355
14356 /* Convert Neon type and size into compact bitmask representation.  */
14357
14358 static enum neon_type_mask
14359 type_chk_of_el_type (enum neon_el_type type, unsigned size)
14360 {
14361   switch (type)
14362     {
14363     case NT_untyped:
14364       switch (size)
14365         {
14366         case 8:  return N_8;
14367         case 16: return N_16;
14368         case 32: return N_32;
14369         case 64: return N_64;
14370         default: ;
14371         }
14372       break;
14373
14374     case NT_integer:
14375       switch (size)
14376         {
14377         case 8:  return N_I8;
14378         case 16: return N_I16;
14379         case 32: return N_I32;
14380         case 64: return N_I64;
14381         default: ;
14382         }
14383       break;
14384
14385     case NT_float:
14386       switch (size)
14387         {
14388         case 16: return N_F16;
14389         case 32: return N_F32;
14390         case 64: return N_F64;
14391         default: ;
14392         }
14393       break;
14394
14395     case NT_poly:
14396       switch (size)
14397         {
14398         case 8:  return N_P8;
14399         case 16: return N_P16;
14400         case 64: return N_P64;
14401         default: ;
14402         }
14403       break;
14404
14405     case NT_signed:
14406       switch (size)
14407         {
14408         case 8:  return N_S8;
14409         case 16: return N_S16;
14410         case 32: return N_S32;
14411         case 64: return N_S64;
14412         default: ;
14413         }
14414       break;
14415
14416     case NT_unsigned:
14417       switch (size)
14418         {
14419         case 8:  return N_U8;
14420         case 16: return N_U16;
14421         case 32: return N_U32;
14422         case 64: return N_U64;
14423         default: ;
14424         }
14425       break;
14426
14427     default: ;
14428     }
14429
14430   return N_UTYP;
14431 }
14432
14433 /* Convert compact Neon bitmask type representation to a type and size. Only
14434    handles the case where a single bit is set in the mask.  */
14435
14436 static int
14437 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
14438                      enum neon_type_mask mask)
14439 {
14440   if ((mask & N_EQK) != 0)
14441     return FAIL;
14442
14443   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
14444     *size = 8;
14445   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
14446     *size = 16;
14447   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
14448     *size = 32;
14449   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
14450     *size = 64;
14451   else
14452     return FAIL;
14453
14454   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
14455     *type = NT_signed;
14456   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
14457     *type = NT_unsigned;
14458   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
14459     *type = NT_integer;
14460   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
14461     *type = NT_untyped;
14462   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
14463     *type = NT_poly;
14464   else if ((mask & (N_F_ALL)) != 0)
14465     *type = NT_float;
14466   else
14467     return FAIL;
14468
14469   return SUCCESS;
14470 }
14471
14472 /* Modify a bitmask of allowed types. This is only needed for type
14473    relaxation.  */
14474
14475 static unsigned
14476 modify_types_allowed (unsigned allowed, unsigned mods)
14477 {
14478   unsigned size;
14479   enum neon_el_type type;
14480   unsigned destmask;
14481   int i;
14482
14483   destmask = 0;
14484
14485   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
14486     {
14487       if (el_type_of_type_chk (&type, &size,
14488                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
14489         {
14490           neon_modify_type_size (mods, &type, &size);
14491           destmask |= type_chk_of_el_type (type, size);
14492         }
14493     }
14494
14495   return destmask;
14496 }
14497
14498 /* Check type and return type classification.
14499    The manual states (paraphrase): If one datatype is given, it indicates the
14500    type given in:
14501     - the second operand, if there is one
14502     - the operand, if there is no second operand
14503     - the result, if there are no operands.
14504    This isn't quite good enough though, so we use a concept of a "key" datatype
14505    which is set on a per-instruction basis, which is the one which matters when
14506    only one data type is written.
14507    Note: this function has side-effects (e.g. filling in missing operands). All
14508    Neon instructions should call it before performing bit encoding.  */
14509
14510 static struct neon_type_el
14511 neon_check_type (unsigned els, enum neon_shape ns, ...)
14512 {
14513   va_list ap;
14514   unsigned i, pass, key_el = 0;
14515   unsigned types[NEON_MAX_TYPE_ELS];
14516   enum neon_el_type k_type = NT_invtype;
14517   unsigned k_size = -1u;
14518   struct neon_type_el badtype = {NT_invtype, -1};
14519   unsigned key_allowed = 0;
14520
14521   /* Optional registers in Neon instructions are always (not) in operand 1.
14522      Fill in the missing operand here, if it was omitted.  */
14523   if (els > 1 && !inst.operands[1].present)
14524     inst.operands[1] = inst.operands[0];
14525
14526   /* Suck up all the varargs.  */
14527   va_start (ap, ns);
14528   for (i = 0; i < els; i++)
14529     {
14530       unsigned thisarg = va_arg (ap, unsigned);
14531       if (thisarg == N_IGNORE_TYPE)
14532         {
14533           va_end (ap);
14534           return badtype;
14535         }
14536       types[i] = thisarg;
14537       if ((thisarg & N_KEY) != 0)
14538         key_el = i;
14539     }
14540   va_end (ap);
14541
14542   if (inst.vectype.elems > 0)
14543     for (i = 0; i < els; i++)
14544       if (inst.operands[i].vectype.type != NT_invtype)
14545         {
14546           first_error (_("types specified in both the mnemonic and operands"));
14547           return badtype;
14548         }
14549
14550   /* Duplicate inst.vectype elements here as necessary.
14551      FIXME: No idea if this is exactly the same as the ARM assembler,
14552      particularly when an insn takes one register and one non-register
14553      operand. */
14554   if (inst.vectype.elems == 1 && els > 1)
14555     {
14556       unsigned j;
14557       inst.vectype.elems = els;
14558       inst.vectype.el[key_el] = inst.vectype.el[0];
14559       for (j = 0; j < els; j++)
14560         if (j != key_el)
14561           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14562                                                   types[j]);
14563     }
14564   else if (inst.vectype.elems == 0 && els > 0)
14565     {
14566       unsigned j;
14567       /* No types were given after the mnemonic, so look for types specified
14568          after each operand. We allow some flexibility here; as long as the
14569          "key" operand has a type, we can infer the others.  */
14570       for (j = 0; j < els; j++)
14571         if (inst.operands[j].vectype.type != NT_invtype)
14572           inst.vectype.el[j] = inst.operands[j].vectype;
14573
14574       if (inst.operands[key_el].vectype.type != NT_invtype)
14575         {
14576           for (j = 0; j < els; j++)
14577             if (inst.operands[j].vectype.type == NT_invtype)
14578               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14579                                                       types[j]);
14580         }
14581       else
14582         {
14583           first_error (_("operand types can't be inferred"));
14584           return badtype;
14585         }
14586     }
14587   else if (inst.vectype.elems != els)
14588     {
14589       first_error (_("type specifier has the wrong number of parts"));
14590       return badtype;
14591     }
14592
14593   for (pass = 0; pass < 2; pass++)
14594     {
14595       for (i = 0; i < els; i++)
14596         {
14597           unsigned thisarg = types[i];
14598           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
14599             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
14600           enum neon_el_type g_type = inst.vectype.el[i].type;
14601           unsigned g_size = inst.vectype.el[i].size;
14602
14603           /* Decay more-specific signed & unsigned types to sign-insensitive
14604              integer types if sign-specific variants are unavailable.  */
14605           if ((g_type == NT_signed || g_type == NT_unsigned)
14606               && (types_allowed & N_SU_ALL) == 0)
14607             g_type = NT_integer;
14608
14609           /* If only untyped args are allowed, decay any more specific types to
14610              them. Some instructions only care about signs for some element
14611              sizes, so handle that properly.  */
14612           if (((types_allowed & N_UNT) == 0)
14613               && ((g_size == 8 && (types_allowed & N_8) != 0)
14614                   || (g_size == 16 && (types_allowed & N_16) != 0)
14615                   || (g_size == 32 && (types_allowed & N_32) != 0)
14616                   || (g_size == 64 && (types_allowed & N_64) != 0)))
14617             g_type = NT_untyped;
14618
14619           if (pass == 0)
14620             {
14621               if ((thisarg & N_KEY) != 0)
14622                 {
14623                   k_type = g_type;
14624                   k_size = g_size;
14625                   key_allowed = thisarg & ~N_KEY;
14626
14627                   /* Check architecture constraint on FP16 extension.  */
14628                   if (k_size == 16
14629                       && k_type == NT_float
14630                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14631                     {
14632                       inst.error = _(BAD_FP16);
14633                       return badtype;
14634                     }
14635                 }
14636             }
14637           else
14638             {
14639               if ((thisarg & N_VFP) != 0)
14640                 {
14641                   enum neon_shape_el regshape;
14642                   unsigned regwidth, match;
14643
14644                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
14645                   if (ns == NS_NULL)
14646                     {
14647                       first_error (_("invalid instruction shape"));
14648                       return badtype;
14649                     }
14650                   regshape = neon_shape_tab[ns].el[i];
14651                   regwidth = neon_shape_el_size[regshape];
14652
14653                   /* In VFP mode, operands must match register widths. If we
14654                      have a key operand, use its width, else use the width of
14655                      the current operand.  */
14656                   if (k_size != -1u)
14657                     match = k_size;
14658                   else
14659                     match = g_size;
14660
14661                   /* FP16 will use a single precision register.  */
14662                   if (regwidth == 32 && match == 16)
14663                     {
14664                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14665                         match = regwidth;
14666                       else
14667                         {
14668                           inst.error = _(BAD_FP16);
14669                           return badtype;
14670                         }
14671                     }
14672
14673                   if (regwidth != match)
14674                     {
14675                       first_error (_("operand size must match register width"));
14676                       return badtype;
14677                     }
14678                 }
14679
14680               if ((thisarg & N_EQK) == 0)
14681                 {
14682                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
14683
14684                   if ((given_type & types_allowed) == 0)
14685                     {
14686                       first_error (_("bad type in SIMD instruction"));
14687                       return badtype;
14688                     }
14689                 }
14690               else
14691                 {
14692                   enum neon_el_type mod_k_type = k_type;
14693                   unsigned mod_k_size = k_size;
14694                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
14695                   if (g_type != mod_k_type || g_size != mod_k_size)
14696                     {
14697                       first_error (_("inconsistent types in Neon instruction"));
14698                       return badtype;
14699                     }
14700                 }
14701             }
14702         }
14703     }
14704
14705   return inst.vectype.el[key_el];
14706 }
14707
14708 /* Neon-style VFP instruction forwarding.  */
14709
14710 /* Thumb VFP instructions have 0xE in the condition field.  */
14711
14712 static void
14713 do_vfp_cond_or_thumb (void)
14714 {
14715   inst.is_neon = 1;
14716
14717   if (thumb_mode)
14718     inst.instruction |= 0xe0000000;
14719   else
14720     inst.instruction |= inst.cond << 28;
14721 }
14722
14723 /* Look up and encode a simple mnemonic, for use as a helper function for the
14724    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
14725    etc.  It is assumed that operand parsing has already been done, and that the
14726    operands are in the form expected by the given opcode (this isn't necessarily
14727    the same as the form in which they were parsed, hence some massaging must
14728    take place before this function is called).
14729    Checks current arch version against that in the looked-up opcode.  */
14730
14731 static void
14732 do_vfp_nsyn_opcode (const char *opname)
14733 {
14734   const struct asm_opcode *opcode;
14735
14736   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
14737
14738   if (!opcode)
14739     abort ();
14740
14741   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
14742                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
14743               _(BAD_FPU));
14744
14745   inst.is_neon = 1;
14746
14747   if (thumb_mode)
14748     {
14749       inst.instruction = opcode->tvalue;
14750       opcode->tencode ();
14751     }
14752   else
14753     {
14754       inst.instruction = (inst.cond << 28) | opcode->avalue;
14755       opcode->aencode ();
14756     }
14757 }
14758
14759 static void
14760 do_vfp_nsyn_add_sub (enum neon_shape rs)
14761 {
14762   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
14763
14764   if (rs == NS_FFF || rs == NS_HHH)
14765     {
14766       if (is_add)
14767         do_vfp_nsyn_opcode ("fadds");
14768       else
14769         do_vfp_nsyn_opcode ("fsubs");
14770
14771       /* ARMv8.2 fp16 instruction.  */
14772       if (rs == NS_HHH)
14773         do_scalar_fp16_v82_encode ();
14774     }
14775   else
14776     {
14777       if (is_add)
14778         do_vfp_nsyn_opcode ("faddd");
14779       else
14780         do_vfp_nsyn_opcode ("fsubd");
14781     }
14782 }
14783
14784 /* Check operand types to see if this is a VFP instruction, and if so call
14785    PFN ().  */
14786
14787 static int
14788 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
14789 {
14790   enum neon_shape rs;
14791   struct neon_type_el et;
14792
14793   switch (args)
14794     {
14795     case 2:
14796       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14797       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14798       break;
14799
14800     case 3:
14801       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14802       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14803                             N_F_ALL | N_KEY | N_VFP);
14804       break;
14805
14806     default:
14807       abort ();
14808     }
14809
14810   if (et.type != NT_invtype)
14811     {
14812       pfn (rs);
14813       return SUCCESS;
14814     }
14815
14816   inst.error = NULL;
14817   return FAIL;
14818 }
14819
14820 static void
14821 do_vfp_nsyn_mla_mls (enum neon_shape rs)
14822 {
14823   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
14824
14825   if (rs == NS_FFF || rs == NS_HHH)
14826     {
14827       if (is_mla)
14828         do_vfp_nsyn_opcode ("fmacs");
14829       else
14830         do_vfp_nsyn_opcode ("fnmacs");
14831
14832       /* ARMv8.2 fp16 instruction.  */
14833       if (rs == NS_HHH)
14834         do_scalar_fp16_v82_encode ();
14835     }
14836   else
14837     {
14838       if (is_mla)
14839         do_vfp_nsyn_opcode ("fmacd");
14840       else
14841         do_vfp_nsyn_opcode ("fnmacd");
14842     }
14843 }
14844
14845 static void
14846 do_vfp_nsyn_fma_fms (enum neon_shape rs)
14847 {
14848   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
14849
14850   if (rs == NS_FFF || rs == NS_HHH)
14851     {
14852       if (is_fma)
14853         do_vfp_nsyn_opcode ("ffmas");
14854       else
14855         do_vfp_nsyn_opcode ("ffnmas");
14856
14857       /* ARMv8.2 fp16 instruction.  */
14858       if (rs == NS_HHH)
14859         do_scalar_fp16_v82_encode ();
14860     }
14861   else
14862     {
14863       if (is_fma)
14864         do_vfp_nsyn_opcode ("ffmad");
14865       else
14866         do_vfp_nsyn_opcode ("ffnmad");
14867     }
14868 }
14869
14870 static void
14871 do_vfp_nsyn_mul (enum neon_shape rs)
14872 {
14873   if (rs == NS_FFF || rs == NS_HHH)
14874     {
14875       do_vfp_nsyn_opcode ("fmuls");
14876
14877       /* ARMv8.2 fp16 instruction.  */
14878       if (rs == NS_HHH)
14879         do_scalar_fp16_v82_encode ();
14880     }
14881   else
14882     do_vfp_nsyn_opcode ("fmuld");
14883 }
14884
14885 static void
14886 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14887 {
14888   int is_neg = (inst.instruction & 0x80) != 0;
14889   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
14890
14891   if (rs == NS_FF || rs == NS_HH)
14892     {
14893       if (is_neg)
14894         do_vfp_nsyn_opcode ("fnegs");
14895       else
14896         do_vfp_nsyn_opcode ("fabss");
14897
14898       /* ARMv8.2 fp16 instruction.  */
14899       if (rs == NS_HH)
14900         do_scalar_fp16_v82_encode ();
14901     }
14902   else
14903     {
14904       if (is_neg)
14905         do_vfp_nsyn_opcode ("fnegd");
14906       else
14907         do_vfp_nsyn_opcode ("fabsd");
14908     }
14909 }
14910
14911 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14912    insns belong to Neon, and are handled elsewhere.  */
14913
14914 static void
14915 do_vfp_nsyn_ldm_stm (int is_dbmode)
14916 {
14917   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14918   if (is_ldm)
14919     {
14920       if (is_dbmode)
14921         do_vfp_nsyn_opcode ("fldmdbs");
14922       else
14923         do_vfp_nsyn_opcode ("fldmias");
14924     }
14925   else
14926     {
14927       if (is_dbmode)
14928         do_vfp_nsyn_opcode ("fstmdbs");
14929       else
14930         do_vfp_nsyn_opcode ("fstmias");
14931     }
14932 }
14933
14934 static void
14935 do_vfp_nsyn_sqrt (void)
14936 {
14937   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14938   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14939
14940   if (rs == NS_FF || rs == NS_HH)
14941     {
14942       do_vfp_nsyn_opcode ("fsqrts");
14943
14944       /* ARMv8.2 fp16 instruction.  */
14945       if (rs == NS_HH)
14946         do_scalar_fp16_v82_encode ();
14947     }
14948   else
14949     do_vfp_nsyn_opcode ("fsqrtd");
14950 }
14951
14952 static void
14953 do_vfp_nsyn_div (void)
14954 {
14955   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14956   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14957                    N_F_ALL | N_KEY | N_VFP);
14958
14959   if (rs == NS_FFF || rs == NS_HHH)
14960     {
14961       do_vfp_nsyn_opcode ("fdivs");
14962
14963       /* ARMv8.2 fp16 instruction.  */
14964       if (rs == NS_HHH)
14965         do_scalar_fp16_v82_encode ();
14966     }
14967   else
14968     do_vfp_nsyn_opcode ("fdivd");
14969 }
14970
14971 static void
14972 do_vfp_nsyn_nmul (void)
14973 {
14974   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14975   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14976                    N_F_ALL | N_KEY | N_VFP);
14977
14978   if (rs == NS_FFF || rs == NS_HHH)
14979     {
14980       NEON_ENCODE (SINGLE, inst);
14981       do_vfp_sp_dyadic ();
14982
14983       /* ARMv8.2 fp16 instruction.  */
14984       if (rs == NS_HHH)
14985         do_scalar_fp16_v82_encode ();
14986     }
14987   else
14988     {
14989       NEON_ENCODE (DOUBLE, inst);
14990       do_vfp_dp_rd_rn_rm ();
14991     }
14992   do_vfp_cond_or_thumb ();
14993
14994 }
14995
14996 static void
14997 do_vfp_nsyn_cmp (void)
14998 {
14999   enum neon_shape rs;
15000   if (inst.operands[1].isreg)
15001     {
15002       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
15003       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
15004
15005       if (rs == NS_FF || rs == NS_HH)
15006         {
15007           NEON_ENCODE (SINGLE, inst);
15008           do_vfp_sp_monadic ();
15009         }
15010       else
15011         {
15012           NEON_ENCODE (DOUBLE, inst);
15013           do_vfp_dp_rd_rm ();
15014         }
15015     }
15016   else
15017     {
15018       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
15019       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
15020
15021       switch (inst.instruction & 0x0fffffff)
15022         {
15023         case N_MNEM_vcmp:
15024           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
15025           break;
15026         case N_MNEM_vcmpe:
15027           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
15028           break;
15029         default:
15030           abort ();
15031         }
15032
15033       if (rs == NS_FI || rs == NS_HI)
15034         {
15035           NEON_ENCODE (SINGLE, inst);
15036           do_vfp_sp_compare_z ();
15037         }
15038       else
15039         {
15040           NEON_ENCODE (DOUBLE, inst);
15041           do_vfp_dp_rd ();
15042         }
15043     }
15044   do_vfp_cond_or_thumb ();
15045
15046   /* ARMv8.2 fp16 instruction.  */
15047   if (rs == NS_HI || rs == NS_HH)
15048     do_scalar_fp16_v82_encode ();
15049 }
15050
15051 static void
15052 nsyn_insert_sp (void)
15053 {
15054   inst.operands[1] = inst.operands[0];
15055   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
15056   inst.operands[0].reg = REG_SP;
15057   inst.operands[0].isreg = 1;
15058   inst.operands[0].writeback = 1;
15059   inst.operands[0].present = 1;
15060 }
15061
15062 static void
15063 do_vfp_nsyn_push (void)
15064 {
15065   nsyn_insert_sp ();
15066
15067   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15068               _("register list must contain at least 1 and at most 16 "
15069                 "registers"));
15070
15071   if (inst.operands[1].issingle)
15072     do_vfp_nsyn_opcode ("fstmdbs");
15073   else
15074     do_vfp_nsyn_opcode ("fstmdbd");
15075 }
15076
15077 static void
15078 do_vfp_nsyn_pop (void)
15079 {
15080   nsyn_insert_sp ();
15081
15082   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
15083               _("register list must contain at least 1 and at most 16 "
15084                 "registers"));
15085
15086   if (inst.operands[1].issingle)
15087     do_vfp_nsyn_opcode ("fldmias");
15088   else
15089     do_vfp_nsyn_opcode ("fldmiad");
15090 }
15091
15092 /* Fix up Neon data-processing instructions, ORing in the correct bits for
15093    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
15094
15095 static void
15096 neon_dp_fixup (struct arm_it* insn)
15097 {
15098   unsigned int i = insn->instruction;
15099   insn->is_neon = 1;
15100
15101   if (thumb_mode)
15102     {
15103       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
15104       if (i & (1 << 24))
15105         i |= 1 << 28;
15106
15107       i &= ~(1 << 24);
15108
15109       i |= 0xef000000;
15110     }
15111   else
15112     i |= 0xf2000000;
15113
15114   insn->instruction = i;
15115 }
15116
15117 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
15118    (0, 1, 2, 3).  */
15119
15120 static unsigned
15121 neon_logbits (unsigned x)
15122 {
15123   return ffs (x) - 4;
15124 }
15125
15126 #define LOW4(R) ((R) & 0xf)
15127 #define HI1(R) (((R) >> 4) & 1)
15128
15129 static void
15130 mve_encode_qqr (int size, int fp)
15131 {
15132   if (inst.operands[2].reg == REG_SP)
15133     as_tsktsk (MVE_BAD_SP);
15134   else if (inst.operands[2].reg == REG_PC)
15135     as_tsktsk (MVE_BAD_PC);
15136
15137   if (fp)
15138     {
15139       /* vadd.  */
15140       if (((unsigned)inst.instruction) == 0xd00)
15141         inst.instruction = 0xee300f40;
15142       /* vsub.  */
15143       else if (((unsigned)inst.instruction) == 0x200d00)
15144         inst.instruction = 0xee301f40;
15145
15146       /* Setting size which is 1 for F16 and 0 for F32.  */
15147       inst.instruction |= (size == 16) << 28;
15148     }
15149   else
15150     {
15151       /* vadd.  */
15152       if (((unsigned)inst.instruction) == 0x800)
15153         inst.instruction = 0xee010f40;
15154       /* vsub.  */
15155       else if (((unsigned)inst.instruction) == 0x1000800)
15156         inst.instruction = 0xee011f40;
15157       /* Setting bits for size.  */
15158       inst.instruction |= neon_logbits (size) << 20;
15159     }
15160   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15161   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15162   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15163   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15164   inst.instruction |= inst.operands[2].reg;
15165   inst.is_neon = 1;
15166 }
15167
15168 /* Encode insns with bit pattern:
15169
15170   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15171   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
15172
15173   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
15174   different meaning for some instruction.  */
15175
15176 static void
15177 neon_three_same (int isquad, int ubit, int size)
15178 {
15179   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15180   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15181   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15182   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15183   inst.instruction |= LOW4 (inst.operands[2].reg);
15184   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15185   inst.instruction |= (isquad != 0) << 6;
15186   inst.instruction |= (ubit != 0) << 24;
15187   if (size != -1)
15188     inst.instruction |= neon_logbits (size) << 20;
15189
15190   neon_dp_fixup (&inst);
15191 }
15192
15193 /* Encode instructions of the form:
15194
15195   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
15196   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
15197
15198   Don't write size if SIZE == -1.  */
15199
15200 static void
15201 neon_two_same (int qbit, int ubit, int size)
15202 {
15203   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15204   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15205   inst.instruction |= LOW4 (inst.operands[1].reg);
15206   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15207   inst.instruction |= (qbit != 0) << 6;
15208   inst.instruction |= (ubit != 0) << 24;
15209
15210   if (size != -1)
15211     inst.instruction |= neon_logbits (size) << 18;
15212
15213   neon_dp_fixup (&inst);
15214 }
15215
15216 /* Neon instruction encoders, in approximate order of appearance.  */
15217
15218 static void
15219 do_neon_dyadic_i_su (void)
15220 {
15221   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15222   struct neon_type_el et = neon_check_type (3, rs,
15223     N_EQK, N_EQK, N_SU_32 | N_KEY);
15224   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15225 }
15226
15227 static void
15228 do_neon_dyadic_i64_su (void)
15229 {
15230   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15231   struct neon_type_el et = neon_check_type (3, rs,
15232     N_EQK, N_EQK, N_SU_ALL | N_KEY);
15233   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15234 }
15235
15236 static void
15237 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
15238                 unsigned immbits)
15239 {
15240   unsigned size = et.size >> 3;
15241   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15242   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15243   inst.instruction |= LOW4 (inst.operands[1].reg);
15244   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15245   inst.instruction |= (isquad != 0) << 6;
15246   inst.instruction |= immbits << 16;
15247   inst.instruction |= (size >> 3) << 7;
15248   inst.instruction |= (size & 0x7) << 19;
15249   if (write_ubit)
15250     inst.instruction |= (uval != 0) << 24;
15251
15252   neon_dp_fixup (&inst);
15253 }
15254
15255 static void
15256 do_neon_shl_imm (void)
15257 {
15258   if (!inst.operands[2].isreg)
15259     {
15260       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15261       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
15262       int imm = inst.operands[2].imm;
15263
15264       constraint (imm < 0 || (unsigned)imm >= et.size,
15265                   _("immediate out of range for shift"));
15266       NEON_ENCODE (IMMED, inst);
15267       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15268     }
15269   else
15270     {
15271       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15272       struct neon_type_el et = neon_check_type (3, rs,
15273         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
15274       unsigned int tmp;
15275
15276       /* VSHL/VQSHL 3-register variants have syntax such as:
15277            vshl.xx Dd, Dm, Dn
15278          whereas other 3-register operations encoded by neon_three_same have
15279          syntax like:
15280            vadd.xx Dd, Dn, Dm
15281          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
15282          here.  */
15283       tmp = inst.operands[2].reg;
15284       inst.operands[2].reg = inst.operands[1].reg;
15285       inst.operands[1].reg = tmp;
15286       NEON_ENCODE (INTEGER, inst);
15287       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15288     }
15289 }
15290
15291 static void
15292 do_neon_qshl_imm (void)
15293 {
15294   if (!inst.operands[2].isreg)
15295     {
15296       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15297       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
15298       int imm = inst.operands[2].imm;
15299
15300       constraint (imm < 0 || (unsigned)imm >= et.size,
15301                   _("immediate out of range for shift"));
15302       NEON_ENCODE (IMMED, inst);
15303       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
15304     }
15305   else
15306     {
15307       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15308       struct neon_type_el et = neon_check_type (3, rs,
15309         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
15310       unsigned int tmp;
15311
15312       /* See note in do_neon_shl_imm.  */
15313       tmp = inst.operands[2].reg;
15314       inst.operands[2].reg = inst.operands[1].reg;
15315       inst.operands[1].reg = tmp;
15316       NEON_ENCODE (INTEGER, inst);
15317       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15318     }
15319 }
15320
15321 static void
15322 do_neon_rshl (void)
15323 {
15324   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15325   struct neon_type_el et = neon_check_type (3, rs,
15326     N_EQK, N_EQK, N_SU_ALL | N_KEY);
15327   unsigned int tmp;
15328
15329   tmp = inst.operands[2].reg;
15330   inst.operands[2].reg = inst.operands[1].reg;
15331   inst.operands[1].reg = tmp;
15332   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
15333 }
15334
15335 static int
15336 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
15337 {
15338   /* Handle .I8 pseudo-instructions.  */
15339   if (size == 8)
15340     {
15341       /* Unfortunately, this will make everything apart from zero out-of-range.
15342          FIXME is this the intended semantics? There doesn't seem much point in
15343          accepting .I8 if so.  */
15344       immediate |= immediate << 8;
15345       size = 16;
15346     }
15347
15348   if (size >= 32)
15349     {
15350       if (immediate == (immediate & 0x000000ff))
15351         {
15352           *immbits = immediate;
15353           return 0x1;
15354         }
15355       else if (immediate == (immediate & 0x0000ff00))
15356         {
15357           *immbits = immediate >> 8;
15358           return 0x3;
15359         }
15360       else if (immediate == (immediate & 0x00ff0000))
15361         {
15362           *immbits = immediate >> 16;
15363           return 0x5;
15364         }
15365       else if (immediate == (immediate & 0xff000000))
15366         {
15367           *immbits = immediate >> 24;
15368           return 0x7;
15369         }
15370       if ((immediate & 0xffff) != (immediate >> 16))
15371         goto bad_immediate;
15372       immediate &= 0xffff;
15373     }
15374
15375   if (immediate == (immediate & 0x000000ff))
15376     {
15377       *immbits = immediate;
15378       return 0x9;
15379     }
15380   else if (immediate == (immediate & 0x0000ff00))
15381     {
15382       *immbits = immediate >> 8;
15383       return 0xb;
15384     }
15385
15386   bad_immediate:
15387   first_error (_("immediate value out of range"));
15388   return FAIL;
15389 }
15390
15391 static void
15392 do_neon_logic (void)
15393 {
15394   if (inst.operands[2].present && inst.operands[2].isreg)
15395     {
15396       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15397       neon_check_type (3, rs, N_IGNORE_TYPE);
15398       /* U bit and size field were set as part of the bitmask.  */
15399       NEON_ENCODE (INTEGER, inst);
15400       neon_three_same (neon_quad (rs), 0, -1);
15401     }
15402   else
15403     {
15404       const int three_ops_form = (inst.operands[2].present
15405                                   && !inst.operands[2].isreg);
15406       const int immoperand = (three_ops_form ? 2 : 1);
15407       enum neon_shape rs = (three_ops_form
15408                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
15409                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
15410       struct neon_type_el et = neon_check_type (2, rs,
15411         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
15412       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
15413       unsigned immbits;
15414       int cmode;
15415
15416       if (et.type == NT_invtype)
15417         return;
15418
15419       if (three_ops_form)
15420         constraint (inst.operands[0].reg != inst.operands[1].reg,
15421                     _("first and second operands shall be the same register"));
15422
15423       NEON_ENCODE (IMMED, inst);
15424
15425       immbits = inst.operands[immoperand].imm;
15426       if (et.size == 64)
15427         {
15428           /* .i64 is a pseudo-op, so the immediate must be a repeating
15429              pattern.  */
15430           if (immbits != (inst.operands[immoperand].regisimm ?
15431                           inst.operands[immoperand].reg : 0))
15432             {
15433               /* Set immbits to an invalid constant.  */
15434               immbits = 0xdeadbeef;
15435             }
15436         }
15437
15438       switch (opcode)
15439         {
15440         case N_MNEM_vbic:
15441           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
15442           break;
15443
15444         case N_MNEM_vorr:
15445           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
15446           break;
15447
15448         case N_MNEM_vand:
15449           /* Pseudo-instruction for VBIC.  */
15450           neon_invert_size (&immbits, 0, et.size);
15451           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
15452           break;
15453
15454         case N_MNEM_vorn:
15455           /* Pseudo-instruction for VORR.  */
15456           neon_invert_size (&immbits, 0, et.size);
15457           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
15458           break;
15459
15460         default:
15461           abort ();
15462         }
15463
15464       if (cmode == FAIL)
15465         return;
15466
15467       inst.instruction |= neon_quad (rs) << 6;
15468       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15469       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15470       inst.instruction |= cmode << 8;
15471       neon_write_immbits (immbits);
15472
15473       neon_dp_fixup (&inst);
15474     }
15475 }
15476
15477 static void
15478 do_neon_bitfield (void)
15479 {
15480   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15481   neon_check_type (3, rs, N_IGNORE_TYPE);
15482   neon_three_same (neon_quad (rs), 0, -1);
15483 }
15484
15485 static void
15486 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
15487                   unsigned destbits)
15488 {
15489   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
15490   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
15491                                             types | N_KEY);
15492   if (et.type == NT_float)
15493     {
15494       NEON_ENCODE (FLOAT, inst);
15495       if (rs == NS_QQR)
15496         mve_encode_qqr (et.size, 1);
15497       else
15498         neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15499     }
15500   else
15501     {
15502       NEON_ENCODE (INTEGER, inst);
15503       if (rs == NS_QQR)
15504         mve_encode_qqr (et.size, 0);
15505       else
15506         neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
15507     }
15508 }
15509
15510
15511 static void
15512 do_neon_dyadic_if_su_d (void)
15513 {
15514   /* This version only allow D registers, but that constraint is enforced during
15515      operand parsing so we don't need to do anything extra here.  */
15516   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
15517 }
15518
15519 static void
15520 do_neon_dyadic_if_i_d (void)
15521 {
15522   /* The "untyped" case can't happen. Do this to stop the "U" bit being
15523      affected if we specify unsigned args.  */
15524   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15525 }
15526
15527 enum vfp_or_neon_is_neon_bits
15528 {
15529   NEON_CHECK_CC = 1,
15530   NEON_CHECK_ARCH = 2,
15531   NEON_CHECK_ARCH8 = 4
15532 };
15533
15534 /* Call this function if an instruction which may have belonged to the VFP or
15535    Neon instruction sets, but turned out to be a Neon instruction (due to the
15536    operand types involved, etc.). We have to check and/or fix-up a couple of
15537    things:
15538
15539      - Make sure the user hasn't attempted to make a Neon instruction
15540        conditional.
15541      - Alter the value in the condition code field if necessary.
15542      - Make sure that the arch supports Neon instructions.
15543
15544    Which of these operations take place depends on bits from enum
15545    vfp_or_neon_is_neon_bits.
15546
15547    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
15548    current instruction's condition is COND_ALWAYS, the condition field is
15549    changed to inst.uncond_value. This is necessary because instructions shared
15550    between VFP and Neon may be conditional for the VFP variants only, and the
15551    unconditional Neon version must have, e.g., 0xF in the condition field.  */
15552
15553 static int
15554 vfp_or_neon_is_neon (unsigned check)
15555 {
15556   /* Conditions are always legal in Thumb mode (IT blocks).  */
15557   if (!thumb_mode && (check & NEON_CHECK_CC))
15558     {
15559       if (inst.cond != COND_ALWAYS)
15560         {
15561           first_error (_(BAD_COND));
15562           return FAIL;
15563         }
15564       if (inst.uncond_value != -1)
15565         inst.instruction |= inst.uncond_value << 28;
15566     }
15567
15568
15569     if (((check & NEON_CHECK_ARCH) && !mark_feature_used (&fpu_neon_ext_v1))
15570         || ((check & NEON_CHECK_ARCH8)
15571             && !mark_feature_used (&fpu_neon_ext_armv8)))
15572       {
15573         first_error (_(BAD_FPU));
15574         return FAIL;
15575       }
15576
15577   return SUCCESS;
15578 }
15579
15580 static int
15581 check_simd_pred_availability (int fp, unsigned check)
15582 {
15583   if (inst.cond > COND_ALWAYS)
15584     {
15585       if (!ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
15586         {
15587           inst.error = BAD_FPU;
15588           return 1;
15589         }
15590       inst.pred_insn_type = INSIDE_VPT_INSN;
15591     }
15592   else if (inst.cond < COND_ALWAYS)
15593     {
15594       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
15595         inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15596       else if (vfp_or_neon_is_neon (check) == FAIL)
15597         return 2;
15598     }
15599   else
15600     {
15601       if (!ARM_CPU_HAS_FEATURE (cpu_variant, fp ? mve_fp_ext : mve_ext)
15602           && vfp_or_neon_is_neon (check) == FAIL)
15603         return 3;
15604
15605       if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
15606         inst.pred_insn_type = MVE_OUTSIDE_PRED_INSN;
15607     }
15608   return 0;
15609 }
15610
15611 static void
15612 do_neon_dyadic_if_su (void)
15613 {
15614   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
15615   struct neon_type_el et = neon_check_type (3, rs, N_EQK , N_EQK,
15616                                             N_SUF_32 | N_KEY);
15617
15618   if (check_simd_pred_availability (et.type == NT_float,
15619                                     NEON_CHECK_ARCH | NEON_CHECK_CC))
15620     return;
15621
15622   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
15623 }
15624
15625 static void
15626 do_neon_addsub_if_i (void)
15627 {
15628   if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1xd)
15629       && try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
15630     return;
15631
15632   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_QQR, NS_NULL);
15633   struct neon_type_el et = neon_check_type (3, rs, N_EQK,
15634                                             N_EQK, N_IF_32 | N_I64 | N_KEY);
15635
15636   constraint (rs == NS_QQR && et.size == 64, BAD_FPU);
15637   /* If we are parsing Q registers and the element types match MVE, which NEON
15638      also supports, then we must check whether this is an instruction that can
15639      be used by both MVE/NEON.  This distinction can be made based on whether
15640      they are predicated or not.  */
15641   if ((rs == NS_QQQ || rs == NS_QQR) && et.size != 64)
15642     {
15643       if (check_simd_pred_availability (et.type == NT_float,
15644                                         NEON_CHECK_ARCH | NEON_CHECK_CC))
15645         return;
15646     }
15647   else
15648     {
15649       /* If they are either in a D register or are using an unsupported.  */
15650       if (rs != NS_QQR
15651           && vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15652         return;
15653     }
15654
15655   /* The "untyped" case can't happen. Do this to stop the "U" bit being
15656      affected if we specify unsigned args.  */
15657   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
15658 }
15659
15660 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
15661    result to be:
15662      V<op> A,B     (A is operand 0, B is operand 2)
15663    to mean:
15664      V<op> A,B,A
15665    not:
15666      V<op> A,B,B
15667    so handle that case specially.  */
15668
15669 static void
15670 neon_exchange_operands (void)
15671 {
15672   if (inst.operands[1].present)
15673     {
15674       void *scratch = xmalloc (sizeof (inst.operands[0]));
15675
15676       /* Swap operands[1] and operands[2].  */
15677       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
15678       inst.operands[1] = inst.operands[2];
15679       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
15680       free (scratch);
15681     }
15682   else
15683     {
15684       inst.operands[1] = inst.operands[2];
15685       inst.operands[2] = inst.operands[0];
15686     }
15687 }
15688
15689 static void
15690 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
15691 {
15692   if (inst.operands[2].isreg)
15693     {
15694       if (invert)
15695         neon_exchange_operands ();
15696       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
15697     }
15698   else
15699     {
15700       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15701       struct neon_type_el et = neon_check_type (2, rs,
15702         N_EQK | N_SIZ, immtypes | N_KEY);
15703
15704       NEON_ENCODE (IMMED, inst);
15705       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15706       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15707       inst.instruction |= LOW4 (inst.operands[1].reg);
15708       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15709       inst.instruction |= neon_quad (rs) << 6;
15710       inst.instruction |= (et.type == NT_float) << 10;
15711       inst.instruction |= neon_logbits (et.size) << 18;
15712
15713       neon_dp_fixup (&inst);
15714     }
15715 }
15716
15717 static void
15718 do_neon_cmp (void)
15719 {
15720   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
15721 }
15722
15723 static void
15724 do_neon_cmp_inv (void)
15725 {
15726   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
15727 }
15728
15729 static void
15730 do_neon_ceq (void)
15731 {
15732   neon_compare (N_IF_32, N_IF_32, FALSE);
15733 }
15734
15735 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
15736    scalars, which are encoded in 5 bits, M : Rm.
15737    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
15738    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
15739    index in M.
15740
15741    Dot Product instructions are similar to multiply instructions except elsize
15742    should always be 32.
15743
15744    This function translates SCALAR, which is GAS's internal encoding of indexed
15745    scalar register, to raw encoding.  There is also register and index range
15746    check based on ELSIZE.  */
15747
15748 static unsigned
15749 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
15750 {
15751   unsigned regno = NEON_SCALAR_REG (scalar);
15752   unsigned elno = NEON_SCALAR_INDEX (scalar);
15753
15754   switch (elsize)
15755     {
15756     case 16:
15757       if (regno > 7 || elno > 3)
15758         goto bad_scalar;
15759       return regno | (elno << 3);
15760
15761     case 32:
15762       if (regno > 15 || elno > 1)
15763         goto bad_scalar;
15764       return regno | (elno << 4);
15765
15766     default:
15767     bad_scalar:
15768       first_error (_("scalar out of range for multiply instruction"));
15769     }
15770
15771   return 0;
15772 }
15773
15774 /* Encode multiply / multiply-accumulate scalar instructions.  */
15775
15776 static void
15777 neon_mul_mac (struct neon_type_el et, int ubit)
15778 {
15779   unsigned scalar;
15780
15781   /* Give a more helpful error message if we have an invalid type.  */
15782   if (et.type == NT_invtype)
15783     return;
15784
15785   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
15786   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15787   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15788   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15789   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15790   inst.instruction |= LOW4 (scalar);
15791   inst.instruction |= HI1 (scalar) << 5;
15792   inst.instruction |= (et.type == NT_float) << 8;
15793   inst.instruction |= neon_logbits (et.size) << 20;
15794   inst.instruction |= (ubit != 0) << 24;
15795
15796   neon_dp_fixup (&inst);
15797 }
15798
15799 static void
15800 do_neon_mac_maybe_scalar (void)
15801 {
15802   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
15803     return;
15804
15805   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15806     return;
15807
15808   if (inst.operands[2].isscalar)
15809     {
15810       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15811       struct neon_type_el et = neon_check_type (3, rs,
15812         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
15813       NEON_ENCODE (SCALAR, inst);
15814       neon_mul_mac (et, neon_quad (rs));
15815     }
15816   else
15817     {
15818       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
15819          affected if we specify unsigned args.  */
15820       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15821     }
15822 }
15823
15824 static void
15825 do_neon_fmac (void)
15826 {
15827   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
15828     return;
15829
15830   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15831     return;
15832
15833   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15834 }
15835
15836 static void
15837 do_neon_tst (void)
15838 {
15839   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15840   struct neon_type_el et = neon_check_type (3, rs,
15841     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
15842   neon_three_same (neon_quad (rs), 0, et.size);
15843 }
15844
15845 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
15846    same types as the MAC equivalents. The polynomial type for this instruction
15847    is encoded the same as the integer type.  */
15848
15849 static void
15850 do_neon_mul (void)
15851 {
15852   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
15853     return;
15854
15855   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15856     return;
15857
15858   if (inst.operands[2].isscalar)
15859     do_neon_mac_maybe_scalar ();
15860   else
15861     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
15862 }
15863
15864 static void
15865 do_neon_qdmulh (void)
15866 {
15867   if (inst.operands[2].isscalar)
15868     {
15869       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15870       struct neon_type_el et = neon_check_type (3, rs,
15871         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15872       NEON_ENCODE (SCALAR, inst);
15873       neon_mul_mac (et, neon_quad (rs));
15874     }
15875   else
15876     {
15877       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15878       struct neon_type_el et = neon_check_type (3, rs,
15879         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15880       NEON_ENCODE (INTEGER, inst);
15881       /* The U bit (rounding) comes from bit mask.  */
15882       neon_three_same (neon_quad (rs), 0, et.size);
15883     }
15884 }
15885
15886 static void
15887 do_neon_qrdmlah (void)
15888 {
15889   /* Check we're on the correct architecture.  */
15890   if (!mark_feature_used (&fpu_neon_ext_armv8))
15891     inst.error =
15892       _("instruction form not available on this architecture.");
15893   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
15894     {
15895       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
15896       record_feature_use (&fpu_neon_ext_v8_1);
15897     }
15898
15899   if (inst.operands[2].isscalar)
15900     {
15901       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15902       struct neon_type_el et = neon_check_type (3, rs,
15903         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15904       NEON_ENCODE (SCALAR, inst);
15905       neon_mul_mac (et, neon_quad (rs));
15906     }
15907   else
15908     {
15909       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15910       struct neon_type_el et = neon_check_type (3, rs,
15911         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15912       NEON_ENCODE (INTEGER, inst);
15913       /* The U bit (rounding) comes from bit mask.  */
15914       neon_three_same (neon_quad (rs), 0, et.size);
15915     }
15916 }
15917
15918 static void
15919 do_neon_fcmp_absolute (void)
15920 {
15921   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15922   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15923                                             N_F_16_32 | N_KEY);
15924   /* Size field comes from bit mask.  */
15925   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
15926 }
15927
15928 static void
15929 do_neon_fcmp_absolute_inv (void)
15930 {
15931   neon_exchange_operands ();
15932   do_neon_fcmp_absolute ();
15933 }
15934
15935 static void
15936 do_neon_step (void)
15937 {
15938   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15939   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15940                                             N_F_16_32 | N_KEY);
15941   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15942 }
15943
15944 static void
15945 do_neon_abs_neg (void)
15946 {
15947   enum neon_shape rs;
15948   struct neon_type_el et;
15949
15950   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
15951     return;
15952
15953   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15954   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
15955
15956   if (check_simd_pred_availability (et.type == NT_float,
15957                                     NEON_CHECK_ARCH | NEON_CHECK_CC))
15958     return;
15959
15960   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15961   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15962   inst.instruction |= LOW4 (inst.operands[1].reg);
15963   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15964   inst.instruction |= neon_quad (rs) << 6;
15965   inst.instruction |= (et.type == NT_float) << 10;
15966   inst.instruction |= neon_logbits (et.size) << 18;
15967
15968   neon_dp_fixup (&inst);
15969 }
15970
15971 static void
15972 do_neon_sli (void)
15973 {
15974   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15975   struct neon_type_el et = neon_check_type (2, rs,
15976     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15977   int imm = inst.operands[2].imm;
15978   constraint (imm < 0 || (unsigned)imm >= et.size,
15979               _("immediate out of range for insert"));
15980   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15981 }
15982
15983 static void
15984 do_neon_sri (void)
15985 {
15986   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15987   struct neon_type_el et = neon_check_type (2, rs,
15988     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15989   int imm = inst.operands[2].imm;
15990   constraint (imm < 1 || (unsigned)imm > et.size,
15991               _("immediate out of range for insert"));
15992   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
15993 }
15994
15995 static void
15996 do_neon_qshlu_imm (void)
15997 {
15998   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15999   struct neon_type_el et = neon_check_type (2, rs,
16000     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
16001   int imm = inst.operands[2].imm;
16002   constraint (imm < 0 || (unsigned)imm >= et.size,
16003               _("immediate out of range for shift"));
16004   /* Only encodes the 'U present' variant of the instruction.
16005      In this case, signed types have OP (bit 8) set to 0.
16006      Unsigned types have OP set to 1.  */
16007   inst.instruction |= (et.type == NT_unsigned) << 8;
16008   /* The rest of the bits are the same as other immediate shifts.  */
16009   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
16010 }
16011
16012 static void
16013 do_neon_qmovn (void)
16014 {
16015   struct neon_type_el et = neon_check_type (2, NS_DQ,
16016     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
16017   /* Saturating move where operands can be signed or unsigned, and the
16018      destination has the same signedness.  */
16019   NEON_ENCODE (INTEGER, inst);
16020   if (et.type == NT_unsigned)
16021     inst.instruction |= 0xc0;
16022   else
16023     inst.instruction |= 0x80;
16024   neon_two_same (0, 1, et.size / 2);
16025 }
16026
16027 static void
16028 do_neon_qmovun (void)
16029 {
16030   struct neon_type_el et = neon_check_type (2, NS_DQ,
16031     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
16032   /* Saturating move with unsigned results. Operands must be signed.  */
16033   NEON_ENCODE (INTEGER, inst);
16034   neon_two_same (0, 1, et.size / 2);
16035 }
16036
16037 static void
16038 do_neon_rshift_sat_narrow (void)
16039 {
16040   /* FIXME: Types for narrowing. If operands are signed, results can be signed
16041      or unsigned. If operands are unsigned, results must also be unsigned.  */
16042   struct neon_type_el et = neon_check_type (2, NS_DQI,
16043     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
16044   int imm = inst.operands[2].imm;
16045   /* This gets the bounds check, size encoding and immediate bits calculation
16046      right.  */
16047   et.size /= 2;
16048
16049   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
16050      VQMOVN.I<size> <Dd>, <Qm>.  */
16051   if (imm == 0)
16052     {
16053       inst.operands[2].present = 0;
16054       inst.instruction = N_MNEM_vqmovn;
16055       do_neon_qmovn ();
16056       return;
16057     }
16058
16059   constraint (imm < 1 || (unsigned)imm > et.size,
16060               _("immediate out of range"));
16061   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
16062 }
16063
16064 static void
16065 do_neon_rshift_sat_narrow_u (void)
16066 {
16067   /* FIXME: Types for narrowing. If operands are signed, results can be signed
16068      or unsigned. If operands are unsigned, results must also be unsigned.  */
16069   struct neon_type_el et = neon_check_type (2, NS_DQI,
16070     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
16071   int imm = inst.operands[2].imm;
16072   /* This gets the bounds check, size encoding and immediate bits calculation
16073      right.  */
16074   et.size /= 2;
16075
16076   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
16077      VQMOVUN.I<size> <Dd>, <Qm>.  */
16078   if (imm == 0)
16079     {
16080       inst.operands[2].present = 0;
16081       inst.instruction = N_MNEM_vqmovun;
16082       do_neon_qmovun ();
16083       return;
16084     }
16085
16086   constraint (imm < 1 || (unsigned)imm > et.size,
16087               _("immediate out of range"));
16088   /* FIXME: The manual is kind of unclear about what value U should have in
16089      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
16090      must be 1.  */
16091   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
16092 }
16093
16094 static void
16095 do_neon_movn (void)
16096 {
16097   struct neon_type_el et = neon_check_type (2, NS_DQ,
16098     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
16099   NEON_ENCODE (INTEGER, inst);
16100   neon_two_same (0, 1, et.size / 2);
16101 }
16102
16103 static void
16104 do_neon_rshift_narrow (void)
16105 {
16106   struct neon_type_el et = neon_check_type (2, NS_DQI,
16107     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
16108   int imm = inst.operands[2].imm;
16109   /* This gets the bounds check, size encoding and immediate bits calculation
16110      right.  */
16111   et.size /= 2;
16112
16113   /* If immediate is zero then we are a pseudo-instruction for
16114      VMOVN.I<size> <Dd>, <Qm>  */
16115   if (imm == 0)
16116     {
16117       inst.operands[2].present = 0;
16118       inst.instruction = N_MNEM_vmovn;
16119       do_neon_movn ();
16120       return;
16121     }
16122
16123   constraint (imm < 1 || (unsigned)imm > et.size,
16124               _("immediate out of range for narrowing operation"));
16125   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
16126 }
16127
16128 static void
16129 do_neon_shll (void)
16130 {
16131   /* FIXME: Type checking when lengthening.  */
16132   struct neon_type_el et = neon_check_type (2, NS_QDI,
16133     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
16134   unsigned imm = inst.operands[2].imm;
16135
16136   if (imm == et.size)
16137     {
16138       /* Maximum shift variant.  */
16139       NEON_ENCODE (INTEGER, inst);
16140       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16141       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16142       inst.instruction |= LOW4 (inst.operands[1].reg);
16143       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16144       inst.instruction |= neon_logbits (et.size) << 18;
16145
16146       neon_dp_fixup (&inst);
16147     }
16148   else
16149     {
16150       /* A more-specific type check for non-max versions.  */
16151       et = neon_check_type (2, NS_QDI,
16152         N_EQK | N_DBL, N_SU_32 | N_KEY);
16153       NEON_ENCODE (IMMED, inst);
16154       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
16155     }
16156 }
16157
16158 /* Check the various types for the VCVT instruction, and return which version
16159    the current instruction is.  */
16160
16161 #define CVT_FLAVOUR_VAR                                                       \
16162   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
16163   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
16164   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
16165   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
16166   /* Half-precision conversions.  */                                          \
16167   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
16168   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
16169   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
16170   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
16171   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
16172   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
16173   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
16174      Compared with single/double precision variants, only the co-processor    \
16175      field is different, so the encoding flow is reused here.  */             \
16176   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
16177   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
16178   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
16179   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
16180   /* VFP instructions.  */                                                    \
16181   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
16182   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
16183   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
16184   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
16185   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
16186   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
16187   /* VFP instructions with bitshift.  */                                      \
16188   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
16189   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
16190   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
16191   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
16192   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
16193   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
16194   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
16195   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
16196
16197 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
16198   neon_cvt_flavour_##C,
16199
16200 /* The different types of conversions we can do.  */
16201 enum neon_cvt_flavour
16202 {
16203   CVT_FLAVOUR_VAR
16204   neon_cvt_flavour_invalid,
16205   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
16206 };
16207
16208 #undef CVT_VAR
16209
16210 static enum neon_cvt_flavour
16211 get_neon_cvt_flavour (enum neon_shape rs)
16212 {
16213 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
16214   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
16215   if (et.type != NT_invtype)                            \
16216     {                                                   \
16217       inst.error = NULL;                                \
16218       return (neon_cvt_flavour_##C);                    \
16219     }
16220
16221   struct neon_type_el et;
16222   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
16223                         || rs == NS_FF) ? N_VFP : 0;
16224   /* The instruction versions which take an immediate take one register
16225      argument, which is extended to the width of the full register. Thus the
16226      "source" and "destination" registers must have the same width.  Hack that
16227      here by making the size equal to the key (wider, in this case) operand.  */
16228   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
16229
16230   CVT_FLAVOUR_VAR;
16231
16232   return neon_cvt_flavour_invalid;
16233 #undef CVT_VAR
16234 }
16235
16236 enum neon_cvt_mode
16237 {
16238   neon_cvt_mode_a,
16239   neon_cvt_mode_n,
16240   neon_cvt_mode_p,
16241   neon_cvt_mode_m,
16242   neon_cvt_mode_z,
16243   neon_cvt_mode_x,
16244   neon_cvt_mode_r
16245 };
16246
16247 /* Neon-syntax VFP conversions.  */
16248
16249 static void
16250 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
16251 {
16252   const char *opname = 0;
16253
16254   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
16255       || rs == NS_FHI || rs == NS_HFI)
16256     {
16257       /* Conversions with immediate bitshift.  */
16258       const char *enc[] =
16259         {
16260 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
16261           CVT_FLAVOUR_VAR
16262           NULL
16263 #undef CVT_VAR
16264         };
16265
16266       if (flavour < (int) ARRAY_SIZE (enc))
16267         {
16268           opname = enc[flavour];
16269           constraint (inst.operands[0].reg != inst.operands[1].reg,
16270                       _("operands 0 and 1 must be the same register"));
16271           inst.operands[1] = inst.operands[2];
16272           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
16273         }
16274     }
16275   else
16276     {
16277       /* Conversions without bitshift.  */
16278       const char *enc[] =
16279         {
16280 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
16281           CVT_FLAVOUR_VAR
16282           NULL
16283 #undef CVT_VAR
16284         };
16285
16286       if (flavour < (int) ARRAY_SIZE (enc))
16287         opname = enc[flavour];
16288     }
16289
16290   if (opname)
16291     do_vfp_nsyn_opcode (opname);
16292
16293   /* ARMv8.2 fp16 VCVT instruction.  */
16294   if (flavour == neon_cvt_flavour_s32_f16
16295       || flavour == neon_cvt_flavour_u32_f16
16296       || flavour == neon_cvt_flavour_f16_u32
16297       || flavour == neon_cvt_flavour_f16_s32)
16298     do_scalar_fp16_v82_encode ();
16299 }
16300
16301 static void
16302 do_vfp_nsyn_cvtz (void)
16303 {
16304   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
16305   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
16306   const char *enc[] =
16307     {
16308 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
16309       CVT_FLAVOUR_VAR
16310       NULL
16311 #undef CVT_VAR
16312     };
16313
16314   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
16315     do_vfp_nsyn_opcode (enc[flavour]);
16316 }
16317
16318 static void
16319 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
16320                       enum neon_cvt_mode mode)
16321 {
16322   int sz, op;
16323   int rm;
16324
16325   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
16326      D register operands.  */
16327   if (flavour == neon_cvt_flavour_s32_f64
16328       || flavour == neon_cvt_flavour_u32_f64)
16329     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16330                 _(BAD_FPU));
16331
16332   if (flavour == neon_cvt_flavour_s32_f16
16333       || flavour == neon_cvt_flavour_u32_f16)
16334     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
16335                 _(BAD_FP16));
16336
16337   set_pred_insn_type (OUTSIDE_PRED_INSN);
16338
16339   switch (flavour)
16340     {
16341     case neon_cvt_flavour_s32_f64:
16342       sz = 1;
16343       op = 1;
16344       break;
16345     case neon_cvt_flavour_s32_f32:
16346       sz = 0;
16347       op = 1;
16348       break;
16349     case neon_cvt_flavour_s32_f16:
16350       sz = 0;
16351       op = 1;
16352       break;
16353     case neon_cvt_flavour_u32_f64:
16354       sz = 1;
16355       op = 0;
16356       break;
16357     case neon_cvt_flavour_u32_f32:
16358       sz = 0;
16359       op = 0;
16360       break;
16361     case neon_cvt_flavour_u32_f16:
16362       sz = 0;
16363       op = 0;
16364       break;
16365     default:
16366       first_error (_("invalid instruction shape"));
16367       return;
16368     }
16369
16370   switch (mode)
16371     {
16372     case neon_cvt_mode_a: rm = 0; break;
16373     case neon_cvt_mode_n: rm = 1; break;
16374     case neon_cvt_mode_p: rm = 2; break;
16375     case neon_cvt_mode_m: rm = 3; break;
16376     default: first_error (_("invalid rounding mode")); return;
16377     }
16378
16379   NEON_ENCODE (FPV8, inst);
16380   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
16381   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
16382   inst.instruction |= sz << 8;
16383
16384   /* ARMv8.2 fp16 VCVT instruction.  */
16385   if (flavour == neon_cvt_flavour_s32_f16
16386       ||flavour == neon_cvt_flavour_u32_f16)
16387     do_scalar_fp16_v82_encode ();
16388   inst.instruction |= op << 7;
16389   inst.instruction |= rm << 16;
16390   inst.instruction |= 0xf0000000;
16391   inst.is_neon = TRUE;
16392 }
16393
16394 static void
16395 do_neon_cvt_1 (enum neon_cvt_mode mode)
16396 {
16397   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
16398                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
16399                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
16400                                           NS_NULL);
16401   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
16402
16403   if (flavour == neon_cvt_flavour_invalid)
16404     return;
16405
16406   /* PR11109: Handle round-to-zero for VCVT conversions.  */
16407   if (mode == neon_cvt_mode_z
16408       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
16409       && (flavour == neon_cvt_flavour_s16_f16
16410           || flavour == neon_cvt_flavour_u16_f16
16411           || flavour == neon_cvt_flavour_s32_f32
16412           || flavour == neon_cvt_flavour_u32_f32
16413           || flavour == neon_cvt_flavour_s32_f64
16414           || flavour == neon_cvt_flavour_u32_f64)
16415       && (rs == NS_FD || rs == NS_FF))
16416     {
16417       do_vfp_nsyn_cvtz ();
16418       return;
16419     }
16420
16421   /* ARMv8.2 fp16 VCVT conversions.  */
16422   if (mode == neon_cvt_mode_z
16423       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
16424       && (flavour == neon_cvt_flavour_s32_f16
16425           || flavour == neon_cvt_flavour_u32_f16)
16426       && (rs == NS_FH))
16427     {
16428       do_vfp_nsyn_cvtz ();
16429       do_scalar_fp16_v82_encode ();
16430       return;
16431     }
16432
16433   /* VFP rather than Neon conversions.  */
16434   if (flavour >= neon_cvt_flavour_first_fp)
16435     {
16436       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
16437         do_vfp_nsyn_cvt (rs, flavour);
16438       else
16439         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
16440
16441       return;
16442     }
16443
16444   switch (rs)
16445     {
16446     case NS_DDI:
16447     case NS_QQI:
16448       {
16449         unsigned immbits;
16450         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
16451                              0x0000100, 0x1000100, 0x0, 0x1000000};
16452
16453         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16454           return;
16455
16456         /* Fixed-point conversion with #0 immediate is encoded as an
16457            integer conversion.  */
16458         if (inst.operands[2].present && inst.operands[2].imm == 0)
16459           goto int_encode;
16460         NEON_ENCODE (IMMED, inst);
16461         if (flavour != neon_cvt_flavour_invalid)
16462           inst.instruction |= enctab[flavour];
16463         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16464         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16465         inst.instruction |= LOW4 (inst.operands[1].reg);
16466         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16467         inst.instruction |= neon_quad (rs) << 6;
16468         inst.instruction |= 1 << 21;
16469         if (flavour < neon_cvt_flavour_s16_f16)
16470           {
16471             inst.instruction |= 1 << 21;
16472             immbits = 32 - inst.operands[2].imm;
16473             inst.instruction |= immbits << 16;
16474           }
16475         else
16476           {
16477             inst.instruction |= 3 << 20;
16478             immbits = 16 - inst.operands[2].imm;
16479             inst.instruction |= immbits << 16;
16480             inst.instruction &= ~(1 << 9);
16481           }
16482
16483         neon_dp_fixup (&inst);
16484       }
16485       break;
16486
16487     case NS_DD:
16488     case NS_QQ:
16489       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
16490         {
16491           NEON_ENCODE (FLOAT, inst);
16492           set_pred_insn_type (OUTSIDE_PRED_INSN);
16493
16494           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16495             return;
16496
16497           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16498           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16499           inst.instruction |= LOW4 (inst.operands[1].reg);
16500           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16501           inst.instruction |= neon_quad (rs) << 6;
16502           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
16503                                || flavour == neon_cvt_flavour_u32_f32) << 7;
16504           inst.instruction |= mode << 8;
16505           if (flavour == neon_cvt_flavour_u16_f16
16506               || flavour == neon_cvt_flavour_s16_f16)
16507             /* Mask off the original size bits and reencode them.  */
16508             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
16509
16510           if (thumb_mode)
16511             inst.instruction |= 0xfc000000;
16512           else
16513             inst.instruction |= 0xf0000000;
16514         }
16515       else
16516         {
16517     int_encode:
16518           {
16519             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
16520                                   0x100, 0x180, 0x0, 0x080};
16521
16522             NEON_ENCODE (INTEGER, inst);
16523
16524             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16525               return;
16526
16527             if (flavour != neon_cvt_flavour_invalid)
16528               inst.instruction |= enctab[flavour];
16529
16530             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16531             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16532             inst.instruction |= LOW4 (inst.operands[1].reg);
16533             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16534             inst.instruction |= neon_quad (rs) << 6;
16535             if (flavour >= neon_cvt_flavour_s16_f16
16536                 && flavour <= neon_cvt_flavour_f16_u16)
16537               /* Half precision.  */
16538               inst.instruction |= 1 << 18;
16539             else
16540               inst.instruction |= 2 << 18;
16541
16542             neon_dp_fixup (&inst);
16543           }
16544         }
16545       break;
16546
16547     /* Half-precision conversions for Advanced SIMD -- neon.  */
16548     case NS_QD:
16549     case NS_DQ:
16550       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16551         return;
16552
16553       if ((rs == NS_DQ)
16554           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
16555           {
16556             as_bad (_("operand size must match register width"));
16557             break;
16558           }
16559
16560       if ((rs == NS_QD)
16561           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
16562           {
16563             as_bad (_("operand size must match register width"));
16564             break;
16565           }
16566
16567       if (rs == NS_DQ)
16568         inst.instruction = 0x3b60600;
16569       else
16570         inst.instruction = 0x3b60700;
16571
16572       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16573       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16574       inst.instruction |= LOW4 (inst.operands[1].reg);
16575       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16576       neon_dp_fixup (&inst);
16577       break;
16578
16579     default:
16580       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
16581       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
16582         do_vfp_nsyn_cvt (rs, flavour);
16583       else
16584         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
16585     }
16586 }
16587
16588 static void
16589 do_neon_cvtr (void)
16590 {
16591   do_neon_cvt_1 (neon_cvt_mode_x);
16592 }
16593
16594 static void
16595 do_neon_cvt (void)
16596 {
16597   do_neon_cvt_1 (neon_cvt_mode_z);
16598 }
16599
16600 static void
16601 do_neon_cvta (void)
16602 {
16603   do_neon_cvt_1 (neon_cvt_mode_a);
16604 }
16605
16606 static void
16607 do_neon_cvtn (void)
16608 {
16609   do_neon_cvt_1 (neon_cvt_mode_n);
16610 }
16611
16612 static void
16613 do_neon_cvtp (void)
16614 {
16615   do_neon_cvt_1 (neon_cvt_mode_p);
16616 }
16617
16618 static void
16619 do_neon_cvtm (void)
16620 {
16621   do_neon_cvt_1 (neon_cvt_mode_m);
16622 }
16623
16624 static void
16625 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
16626 {
16627   if (is_double)
16628     mark_feature_used (&fpu_vfp_ext_armv8);
16629
16630   encode_arm_vfp_reg (inst.operands[0].reg,
16631                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
16632   encode_arm_vfp_reg (inst.operands[1].reg,
16633                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
16634   inst.instruction |= to ? 0x10000 : 0;
16635   inst.instruction |= t ? 0x80 : 0;
16636   inst.instruction |= is_double ? 0x100 : 0;
16637   do_vfp_cond_or_thumb ();
16638 }
16639
16640 static void
16641 do_neon_cvttb_1 (bfd_boolean t)
16642 {
16643   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
16644                                           NS_DF, NS_DH, NS_NULL);
16645
16646   if (rs == NS_NULL)
16647     return;
16648   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
16649     {
16650       inst.error = NULL;
16651       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
16652     }
16653   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
16654     {
16655       inst.error = NULL;
16656       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
16657     }
16658   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
16659     {
16660       /* The VCVTB and VCVTT instructions with D-register operands
16661          don't work for SP only targets.  */
16662       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16663                   _(BAD_FPU));
16664
16665       inst.error = NULL;
16666       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
16667     }
16668   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
16669     {
16670       /* The VCVTB and VCVTT instructions with D-register operands
16671          don't work for SP only targets.  */
16672       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16673                   _(BAD_FPU));
16674
16675       inst.error = NULL;
16676       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
16677     }
16678   else
16679     return;
16680 }
16681
16682 static void
16683 do_neon_cvtb (void)
16684 {
16685   do_neon_cvttb_1 (FALSE);
16686 }
16687
16688
16689 static void
16690 do_neon_cvtt (void)
16691 {
16692   do_neon_cvttb_1 (TRUE);
16693 }
16694
16695 static void
16696 neon_move_immediate (void)
16697 {
16698   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
16699   struct neon_type_el et = neon_check_type (2, rs,
16700     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
16701   unsigned immlo, immhi = 0, immbits;
16702   int op, cmode, float_p;
16703
16704   constraint (et.type == NT_invtype,
16705               _("operand size must be specified for immediate VMOV"));
16706
16707   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
16708   op = (inst.instruction & (1 << 5)) != 0;
16709
16710   immlo = inst.operands[1].imm;
16711   if (inst.operands[1].regisimm)
16712     immhi = inst.operands[1].reg;
16713
16714   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
16715               _("immediate has bits set outside the operand size"));
16716
16717   float_p = inst.operands[1].immisfloat;
16718
16719   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
16720                                         et.size, et.type)) == FAIL)
16721     {
16722       /* Invert relevant bits only.  */
16723       neon_invert_size (&immlo, &immhi, et.size);
16724       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
16725          with one or the other; those cases are caught by
16726          neon_cmode_for_move_imm.  */
16727       op = !op;
16728       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
16729                                             &op, et.size, et.type)) == FAIL)
16730         {
16731           first_error (_("immediate out of range"));
16732           return;
16733         }
16734     }
16735
16736   inst.instruction &= ~(1 << 5);
16737   inst.instruction |= op << 5;
16738
16739   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16740   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16741   inst.instruction |= neon_quad (rs) << 6;
16742   inst.instruction |= cmode << 8;
16743
16744   neon_write_immbits (immbits);
16745 }
16746
16747 static void
16748 do_neon_mvn (void)
16749 {
16750   if (inst.operands[1].isreg)
16751     {
16752       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16753
16754       NEON_ENCODE (INTEGER, inst);
16755       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16756       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16757       inst.instruction |= LOW4 (inst.operands[1].reg);
16758       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16759       inst.instruction |= neon_quad (rs) << 6;
16760     }
16761   else
16762     {
16763       NEON_ENCODE (IMMED, inst);
16764       neon_move_immediate ();
16765     }
16766
16767   neon_dp_fixup (&inst);
16768 }
16769
16770 /* Encode instructions of form:
16771
16772   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16773   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
16774
16775 static void
16776 neon_mixed_length (struct neon_type_el et, unsigned size)
16777 {
16778   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16779   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16780   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16781   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16782   inst.instruction |= LOW4 (inst.operands[2].reg);
16783   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16784   inst.instruction |= (et.type == NT_unsigned) << 24;
16785   inst.instruction |= neon_logbits (size) << 20;
16786
16787   neon_dp_fixup (&inst);
16788 }
16789
16790 static void
16791 do_neon_dyadic_long (void)
16792 {
16793   enum neon_shape rs = neon_select_shape (NS_QDD, NS_QQQ, NS_QQR, NS_NULL);
16794   if (rs == NS_QDD)
16795     {
16796       if (vfp_or_neon_is_neon (NEON_CHECK_ARCH | NEON_CHECK_CC) == FAIL)
16797         return;
16798
16799       NEON_ENCODE (INTEGER, inst);
16800       /* FIXME: Type checking for lengthening op.  */
16801       struct neon_type_el et = neon_check_type (3, NS_QDD,
16802         N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
16803       neon_mixed_length (et, et.size);
16804     }
16805   else if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext)
16806            && (inst.cond == 0xf || inst.cond == 0x10))
16807     {
16808       /* If parsing for MVE, vaddl/vsubl/vabdl{e,t} can only be vadd/vsub/vabd
16809          in an IT block with le/lt conditions.  */
16810
16811       if (inst.cond == 0xf)
16812         inst.cond = 0xb;
16813       else if (inst.cond == 0x10)
16814         inst.cond = 0xd;
16815
16816       inst.pred_insn_type = INSIDE_IT_INSN;
16817
16818       if (inst.instruction == N_MNEM_vaddl)
16819         {
16820           inst.instruction = N_MNEM_vadd;
16821           do_neon_addsub_if_i ();
16822         }
16823       else if (inst.instruction == N_MNEM_vsubl)
16824         {
16825           inst.instruction = N_MNEM_vsub;
16826           do_neon_addsub_if_i ();
16827         }
16828       else if (inst.instruction == N_MNEM_vabdl)
16829         {
16830           inst.instruction = N_MNEM_vabd;
16831           do_neon_dyadic_if_su ();
16832         }
16833     }
16834   else
16835     first_error (BAD_FPU);
16836 }
16837
16838 static void
16839 do_neon_abal (void)
16840 {
16841   struct neon_type_el et = neon_check_type (3, NS_QDD,
16842     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
16843   neon_mixed_length (et, et.size);
16844 }
16845
16846 static void
16847 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
16848 {
16849   if (inst.operands[2].isscalar)
16850     {
16851       struct neon_type_el et = neon_check_type (3, NS_QDS,
16852         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
16853       NEON_ENCODE (SCALAR, inst);
16854       neon_mul_mac (et, et.type == NT_unsigned);
16855     }
16856   else
16857     {
16858       struct neon_type_el et = neon_check_type (3, NS_QDD,
16859         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
16860       NEON_ENCODE (INTEGER, inst);
16861       neon_mixed_length (et, et.size);
16862     }
16863 }
16864
16865 static void
16866 do_neon_mac_maybe_scalar_long (void)
16867 {
16868   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
16869 }
16870
16871 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
16872    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
16873
16874 static unsigned
16875 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
16876 {
16877   unsigned regno = NEON_SCALAR_REG (scalar);
16878   unsigned elno = NEON_SCALAR_INDEX (scalar);
16879
16880   if (quad_p)
16881     {
16882       if (regno > 7 || elno > 3)
16883         goto bad_scalar;
16884
16885       return ((regno & 0x7)
16886               | ((elno & 0x1) << 3)
16887               | (((elno >> 1) & 0x1) << 5));
16888     }
16889   else
16890     {
16891       if (regno > 15 || elno > 1)
16892         goto bad_scalar;
16893
16894       return (((regno & 0x1) << 5)
16895               | ((regno >> 1) & 0x7)
16896               | ((elno & 0x1) << 3));
16897     }
16898
16899 bad_scalar:
16900   first_error (_("scalar out of range for multiply instruction"));
16901   return 0;
16902 }
16903
16904 static void
16905 do_neon_fmac_maybe_scalar_long (int subtype)
16906 {
16907   enum neon_shape rs;
16908   int high8;
16909   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
16910      field (bits[21:20]) has different meaning.  For scalar index variant, it's
16911      used to differentiate add and subtract, otherwise it's with fixed value
16912      0x2.  */
16913   int size = -1;
16914
16915   if (inst.cond != COND_ALWAYS)
16916     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
16917                "behaviour is UNPREDICTABLE"));
16918
16919   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
16920               _(BAD_FP16));
16921
16922   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
16923               _(BAD_FPU));
16924
16925   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
16926      be a scalar index register.  */
16927   if (inst.operands[2].isscalar)
16928     {
16929       high8 = 0xfe000000;
16930       if (subtype)
16931         size = 16;
16932       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
16933     }
16934   else
16935     {
16936       high8 = 0xfc000000;
16937       size = 32;
16938       if (subtype)
16939         inst.instruction |= (0x1 << 23);
16940       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
16941     }
16942
16943   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
16944
16945   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
16946      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
16947      so we simply pass -1 as size.  */
16948   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
16949   neon_three_same (quad_p, 0, size);
16950
16951   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
16952   inst.instruction &= 0x00ffffff;
16953   inst.instruction |= high8;
16954
16955 #define LOW1(R) ((R) & 0x1)
16956 #define HI4(R) (((R) >> 1) & 0xf)
16957   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
16958      whether the instruction is in Q form and whether Vm is a scalar indexed
16959      operand.  */
16960   if (inst.operands[2].isscalar)
16961     {
16962       unsigned rm
16963         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
16964       inst.instruction &= 0xffffffd0;
16965       inst.instruction |= rm;
16966
16967       if (!quad_p)
16968         {
16969           /* Redo Rn as well.  */
16970           inst.instruction &= 0xfff0ff7f;
16971           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16972           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16973         }
16974     }
16975   else if (!quad_p)
16976     {
16977       /* Redo Rn and Rm.  */
16978       inst.instruction &= 0xfff0ff50;
16979       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16980       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16981       inst.instruction |= HI4 (inst.operands[2].reg);
16982       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
16983     }
16984 }
16985
16986 static void
16987 do_neon_vfmal (void)
16988 {
16989   return do_neon_fmac_maybe_scalar_long (0);
16990 }
16991
16992 static void
16993 do_neon_vfmsl (void)
16994 {
16995   return do_neon_fmac_maybe_scalar_long (1);
16996 }
16997
16998 static void
16999 do_neon_dyadic_wide (void)
17000 {
17001   struct neon_type_el et = neon_check_type (3, NS_QQD,
17002     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
17003   neon_mixed_length (et, et.size);
17004 }
17005
17006 static void
17007 do_neon_dyadic_narrow (void)
17008 {
17009   struct neon_type_el et = neon_check_type (3, NS_QDD,
17010     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
17011   /* Operand sign is unimportant, and the U bit is part of the opcode,
17012      so force the operand type to integer.  */
17013   et.type = NT_integer;
17014   neon_mixed_length (et, et.size / 2);
17015 }
17016
17017 static void
17018 do_neon_mul_sat_scalar_long (void)
17019 {
17020   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
17021 }
17022
17023 static void
17024 do_neon_vmull (void)
17025 {
17026   if (inst.operands[2].isscalar)
17027     do_neon_mac_maybe_scalar_long ();
17028   else
17029     {
17030       struct neon_type_el et = neon_check_type (3, NS_QDD,
17031         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
17032
17033       if (et.type == NT_poly)
17034         NEON_ENCODE (POLY, inst);
17035       else
17036         NEON_ENCODE (INTEGER, inst);
17037
17038       /* For polynomial encoding the U bit must be zero, and the size must
17039          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
17040          obviously, as 0b10).  */
17041       if (et.size == 64)
17042         {
17043           /* Check we're on the correct architecture.  */
17044           if (!mark_feature_used (&fpu_crypto_ext_armv8))
17045             inst.error =
17046               _("Instruction form not available on this architecture.");
17047
17048           et.size = 32;
17049         }
17050
17051       neon_mixed_length (et, et.size);
17052     }
17053 }
17054
17055 static void
17056 do_neon_ext (void)
17057 {
17058   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17059   struct neon_type_el et = neon_check_type (3, rs,
17060     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
17061   unsigned imm = (inst.operands[3].imm * et.size) / 8;
17062
17063   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
17064               _("shift out of range"));
17065   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17066   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17067   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17068   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17069   inst.instruction |= LOW4 (inst.operands[2].reg);
17070   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
17071   inst.instruction |= neon_quad (rs) << 6;
17072   inst.instruction |= imm << 8;
17073
17074   neon_dp_fixup (&inst);
17075 }
17076
17077 static void
17078 do_neon_rev (void)
17079 {
17080   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17081   struct neon_type_el et = neon_check_type (2, rs,
17082     N_EQK, N_8 | N_16 | N_32 | N_KEY);
17083   unsigned op = (inst.instruction >> 7) & 3;
17084   /* N (width of reversed regions) is encoded as part of the bitmask. We
17085      extract it here to check the elements to be reversed are smaller.
17086      Otherwise we'd get a reserved instruction.  */
17087   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
17088   gas_assert (elsize != 0);
17089   constraint (et.size >= elsize,
17090               _("elements must be smaller than reversal region"));
17091   neon_two_same (neon_quad (rs), 1, et.size);
17092 }
17093
17094 static void
17095 do_neon_dup (void)
17096 {
17097   if (inst.operands[1].isscalar)
17098     {
17099       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
17100       struct neon_type_el et = neon_check_type (2, rs,
17101         N_EQK, N_8 | N_16 | N_32 | N_KEY);
17102       unsigned sizebits = et.size >> 3;
17103       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
17104       int logsize = neon_logbits (et.size);
17105       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
17106
17107       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
17108         return;
17109
17110       NEON_ENCODE (SCALAR, inst);
17111       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17112       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17113       inst.instruction |= LOW4 (dm);
17114       inst.instruction |= HI1 (dm) << 5;
17115       inst.instruction |= neon_quad (rs) << 6;
17116       inst.instruction |= x << 17;
17117       inst.instruction |= sizebits << 16;
17118
17119       neon_dp_fixup (&inst);
17120     }
17121   else
17122     {
17123       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
17124       struct neon_type_el et = neon_check_type (2, rs,
17125         N_8 | N_16 | N_32 | N_KEY, N_EQK);
17126       /* Duplicate ARM register to lanes of vector.  */
17127       NEON_ENCODE (ARMREG, inst);
17128       switch (et.size)
17129         {
17130         case 8:  inst.instruction |= 0x400000; break;
17131         case 16: inst.instruction |= 0x000020; break;
17132         case 32: inst.instruction |= 0x000000; break;
17133         default: break;
17134         }
17135       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
17136       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
17137       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
17138       inst.instruction |= neon_quad (rs) << 21;
17139       /* The encoding for this instruction is identical for the ARM and Thumb
17140          variants, except for the condition field.  */
17141       do_vfp_cond_or_thumb ();
17142     }
17143 }
17144
17145 /* VMOV has particularly many variations. It can be one of:
17146      0. VMOV<c><q> <Qd>, <Qm>
17147      1. VMOV<c><q> <Dd>, <Dm>
17148    (Register operations, which are VORR with Rm = Rn.)
17149      2. VMOV<c><q>.<dt> <Qd>, #<imm>
17150      3. VMOV<c><q>.<dt> <Dd>, #<imm>
17151    (Immediate loads.)
17152      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
17153    (ARM register to scalar.)
17154      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
17155    (Two ARM registers to vector.)
17156      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
17157    (Scalar to ARM register.)
17158      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
17159    (Vector to two ARM registers.)
17160      8. VMOV.F32 <Sd>, <Sm>
17161      9. VMOV.F64 <Dd>, <Dm>
17162    (VFP register moves.)
17163     10. VMOV.F32 <Sd>, #imm
17164     11. VMOV.F64 <Dd>, #imm
17165    (VFP float immediate load.)
17166     12. VMOV <Rd>, <Sm>
17167    (VFP single to ARM reg.)
17168     13. VMOV <Sd>, <Rm>
17169    (ARM reg to VFP single.)
17170     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
17171    (Two ARM regs to two VFP singles.)
17172     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
17173    (Two VFP singles to two ARM regs.)
17174
17175    These cases can be disambiguated using neon_select_shape, except cases 1/9
17176    and 3/11 which depend on the operand type too.
17177
17178    All the encoded bits are hardcoded by this function.
17179
17180    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
17181    Cases 5, 7 may be used with VFPv2 and above.
17182
17183    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
17184    can specify a type where it doesn't make sense to, and is ignored).  */
17185
17186 static void
17187 do_neon_mov (void)
17188 {
17189   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
17190                                           NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR,
17191                                           NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
17192                                           NS_HR, NS_RH, NS_HI, NS_NULL);
17193   struct neon_type_el et;
17194   const char *ldconst = 0;
17195
17196   switch (rs)
17197     {
17198     case NS_DD:  /* case 1/9.  */
17199       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
17200       /* It is not an error here if no type is given.  */
17201       inst.error = NULL;
17202       if (et.type == NT_float && et.size == 64)
17203         {
17204           do_vfp_nsyn_opcode ("fcpyd");
17205           break;
17206         }
17207       /* fall through.  */
17208
17209     case NS_QQ:  /* case 0/1.  */
17210       {
17211         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17212           return;
17213         /* The architecture manual I have doesn't explicitly state which
17214            value the U bit should have for register->register moves, but
17215            the equivalent VORR instruction has U = 0, so do that.  */
17216         inst.instruction = 0x0200110;
17217         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17218         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17219         inst.instruction |= LOW4 (inst.operands[1].reg);
17220         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17221         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17222         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17223         inst.instruction |= neon_quad (rs) << 6;
17224
17225         neon_dp_fixup (&inst);
17226       }
17227       break;
17228
17229     case NS_DI:  /* case 3/11.  */
17230       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
17231       inst.error = NULL;
17232       if (et.type == NT_float && et.size == 64)
17233         {
17234           /* case 11 (fconstd).  */
17235           ldconst = "fconstd";
17236           goto encode_fconstd;
17237         }
17238       /* fall through.  */
17239
17240     case NS_QI:  /* case 2/3.  */
17241       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
17242         return;
17243       inst.instruction = 0x0800010;
17244       neon_move_immediate ();
17245       neon_dp_fixup (&inst);
17246       break;
17247
17248     case NS_SR:  /* case 4.  */
17249       {
17250         unsigned bcdebits = 0;
17251         int logsize;
17252         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
17253         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
17254
17255         /* .<size> is optional here, defaulting to .32. */
17256         if (inst.vectype.elems == 0
17257             && inst.operands[0].vectype.type == NT_invtype
17258             && inst.operands[1].vectype.type == NT_invtype)
17259           {
17260             inst.vectype.el[0].type = NT_untyped;
17261             inst.vectype.el[0].size = 32;
17262             inst.vectype.elems = 1;
17263           }
17264
17265         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
17266         logsize = neon_logbits (et.size);
17267
17268         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
17269                     _(BAD_FPU));
17270         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
17271                     && et.size != 32, _(BAD_FPU));
17272         constraint (et.type == NT_invtype, _("bad type for scalar"));
17273         constraint (x >= 64 / et.size, _("scalar index out of range"));
17274
17275         switch (et.size)
17276           {
17277           case 8:  bcdebits = 0x8; break;
17278           case 16: bcdebits = 0x1; break;
17279           case 32: bcdebits = 0x0; break;
17280           default: ;
17281           }
17282
17283         bcdebits |= x << logsize;
17284
17285         inst.instruction = 0xe000b10;
17286         do_vfp_cond_or_thumb ();
17287         inst.instruction |= LOW4 (dn) << 16;
17288         inst.instruction |= HI1 (dn) << 7;
17289         inst.instruction |= inst.operands[1].reg << 12;
17290         inst.instruction |= (bcdebits & 3) << 5;
17291         inst.instruction |= (bcdebits >> 2) << 21;
17292       }
17293       break;
17294
17295     case NS_DRR:  /* case 5 (fmdrr).  */
17296       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
17297                   _(BAD_FPU));
17298
17299       inst.instruction = 0xc400b10;
17300       do_vfp_cond_or_thumb ();
17301       inst.instruction |= LOW4 (inst.operands[0].reg);
17302       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
17303       inst.instruction |= inst.operands[1].reg << 12;
17304       inst.instruction |= inst.operands[2].reg << 16;
17305       break;
17306
17307     case NS_RS:  /* case 6.  */
17308       {
17309         unsigned logsize;
17310         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
17311         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
17312         unsigned abcdebits = 0;
17313
17314         /* .<dt> is optional here, defaulting to .32. */
17315         if (inst.vectype.elems == 0
17316             && inst.operands[0].vectype.type == NT_invtype
17317             && inst.operands[1].vectype.type == NT_invtype)
17318           {
17319             inst.vectype.el[0].type = NT_untyped;
17320             inst.vectype.el[0].size = 32;
17321             inst.vectype.elems = 1;
17322           }
17323
17324         et = neon_check_type (2, NS_NULL,
17325                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
17326         logsize = neon_logbits (et.size);
17327
17328         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
17329                     _(BAD_FPU));
17330         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
17331                     && et.size != 32, _(BAD_FPU));
17332         constraint (et.type == NT_invtype, _("bad type for scalar"));
17333         constraint (x >= 64 / et.size, _("scalar index out of range"));
17334
17335         switch (et.size)
17336           {
17337           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
17338           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
17339           case 32: abcdebits = 0x00; break;
17340           default: ;
17341           }
17342
17343         abcdebits |= x << logsize;
17344         inst.instruction = 0xe100b10;
17345         do_vfp_cond_or_thumb ();
17346         inst.instruction |= LOW4 (dn) << 16;
17347         inst.instruction |= HI1 (dn) << 7;
17348         inst.instruction |= inst.operands[0].reg << 12;
17349         inst.instruction |= (abcdebits & 3) << 5;
17350         inst.instruction |= (abcdebits >> 2) << 21;
17351       }
17352       break;
17353
17354     case NS_RRD:  /* case 7 (fmrrd).  */
17355       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
17356                   _(BAD_FPU));
17357
17358       inst.instruction = 0xc500b10;
17359       do_vfp_cond_or_thumb ();
17360       inst.instruction |= inst.operands[0].reg << 12;
17361       inst.instruction |= inst.operands[1].reg << 16;
17362       inst.instruction |= LOW4 (inst.operands[2].reg);
17363       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
17364       break;
17365
17366     case NS_FF:  /* case 8 (fcpys).  */
17367       do_vfp_nsyn_opcode ("fcpys");
17368       break;
17369
17370     case NS_HI:
17371     case NS_FI:  /* case 10 (fconsts).  */
17372       ldconst = "fconsts";
17373     encode_fconstd:
17374       if (!inst.operands[1].immisfloat)
17375         {
17376           unsigned new_imm;
17377           /* Immediate has to fit in 8 bits so float is enough.  */
17378           float imm = (float) inst.operands[1].imm;
17379           memcpy (&new_imm, &imm, sizeof (float));
17380           /* But the assembly may have been written to provide an integer
17381              bit pattern that equates to a float, so check that the
17382              conversion has worked.  */
17383           if (is_quarter_float (new_imm))
17384             {
17385               if (is_quarter_float (inst.operands[1].imm))
17386                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
17387
17388               inst.operands[1].imm = new_imm;
17389               inst.operands[1].immisfloat = 1;
17390             }
17391         }
17392
17393       if (is_quarter_float (inst.operands[1].imm))
17394         {
17395           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
17396           do_vfp_nsyn_opcode (ldconst);
17397
17398           /* ARMv8.2 fp16 vmov.f16 instruction.  */
17399           if (rs == NS_HI)
17400             do_scalar_fp16_v82_encode ();
17401         }
17402       else
17403         first_error (_("immediate out of range"));
17404       break;
17405
17406     case NS_RH:
17407     case NS_RF:  /* case 12 (fmrs).  */
17408       do_vfp_nsyn_opcode ("fmrs");
17409       /* ARMv8.2 fp16 vmov.f16 instruction.  */
17410       if (rs == NS_RH)
17411         do_scalar_fp16_v82_encode ();
17412       break;
17413
17414     case NS_HR:
17415     case NS_FR:  /* case 13 (fmsr).  */
17416       do_vfp_nsyn_opcode ("fmsr");
17417       /* ARMv8.2 fp16 vmov.f16 instruction.  */
17418       if (rs == NS_HR)
17419         do_scalar_fp16_v82_encode ();
17420       break;
17421
17422     /* The encoders for the fmrrs and fmsrr instructions expect three operands
17423        (one of which is a list), but we have parsed four.  Do some fiddling to
17424        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
17425        expect.  */
17426     case NS_RRFF:  /* case 14 (fmrrs).  */
17427       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
17428                   _("VFP registers must be adjacent"));
17429       inst.operands[2].imm = 2;
17430       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
17431       do_vfp_nsyn_opcode ("fmrrs");
17432       break;
17433
17434     case NS_FFRR:  /* case 15 (fmsrr).  */
17435       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
17436                   _("VFP registers must be adjacent"));
17437       inst.operands[1] = inst.operands[2];
17438       inst.operands[2] = inst.operands[3];
17439       inst.operands[0].imm = 2;
17440       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
17441       do_vfp_nsyn_opcode ("fmsrr");
17442       break;
17443
17444     case NS_NULL:
17445       /* neon_select_shape has determined that the instruction
17446          shape is wrong and has already set the error message.  */
17447       break;
17448
17449     default:
17450       abort ();
17451     }
17452 }
17453
17454 static void
17455 do_neon_rshift_round_imm (void)
17456 {
17457   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
17458   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
17459   int imm = inst.operands[2].imm;
17460
17461   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
17462   if (imm == 0)
17463     {
17464       inst.operands[2].present = 0;
17465       do_neon_mov ();
17466       return;
17467     }
17468
17469   constraint (imm < 1 || (unsigned)imm > et.size,
17470               _("immediate out of range for shift"));
17471   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
17472                   et.size - imm);
17473 }
17474
17475 static void
17476 do_neon_movhf (void)
17477 {
17478   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
17479   constraint (rs != NS_HH, _("invalid suffix"));
17480
17481   if (inst.cond != COND_ALWAYS)
17482     {
17483       if (thumb_mode)
17484         {
17485           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
17486                      " the behaviour is UNPREDICTABLE"));
17487         }
17488       else
17489         {
17490           inst.error = BAD_COND;
17491           return;
17492         }
17493     }
17494
17495   do_vfp_sp_monadic ();
17496
17497   inst.is_neon = 1;
17498   inst.instruction |= 0xf0000000;
17499 }
17500
17501 static void
17502 do_neon_movl (void)
17503 {
17504   struct neon_type_el et = neon_check_type (2, NS_QD,
17505     N_EQK | N_DBL, N_SU_32 | N_KEY);
17506   unsigned sizebits = et.size >> 3;
17507   inst.instruction |= sizebits << 19;
17508   neon_two_same (0, et.type == NT_unsigned, -1);
17509 }
17510
17511 static void
17512 do_neon_trn (void)
17513 {
17514   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17515   struct neon_type_el et = neon_check_type (2, rs,
17516     N_EQK, N_8 | N_16 | N_32 | N_KEY);
17517   NEON_ENCODE (INTEGER, inst);
17518   neon_two_same (neon_quad (rs), 1, et.size);
17519 }
17520
17521 static void
17522 do_neon_zip_uzp (void)
17523 {
17524   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17525   struct neon_type_el et = neon_check_type (2, rs,
17526     N_EQK, N_8 | N_16 | N_32 | N_KEY);
17527   if (rs == NS_DD && et.size == 32)
17528     {
17529       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
17530       inst.instruction = N_MNEM_vtrn;
17531       do_neon_trn ();
17532       return;
17533     }
17534   neon_two_same (neon_quad (rs), 1, et.size);
17535 }
17536
17537 static void
17538 do_neon_sat_abs_neg (void)
17539 {
17540   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17541   struct neon_type_el et = neon_check_type (2, rs,
17542     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17543   neon_two_same (neon_quad (rs), 1, et.size);
17544 }
17545
17546 static void
17547 do_neon_pair_long (void)
17548 {
17549   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17550   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
17551   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
17552   inst.instruction |= (et.type == NT_unsigned) << 7;
17553   neon_two_same (neon_quad (rs), 1, et.size);
17554 }
17555
17556 static void
17557 do_neon_recip_est (void)
17558 {
17559   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17560   struct neon_type_el et = neon_check_type (2, rs,
17561     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
17562   inst.instruction |= (et.type == NT_float) << 8;
17563   neon_two_same (neon_quad (rs), 1, et.size);
17564 }
17565
17566 static void
17567 do_neon_cls (void)
17568 {
17569   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17570   struct neon_type_el et = neon_check_type (2, rs,
17571     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
17572   neon_two_same (neon_quad (rs), 1, et.size);
17573 }
17574
17575 static void
17576 do_neon_clz (void)
17577 {
17578   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17579   struct neon_type_el et = neon_check_type (2, rs,
17580     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
17581   neon_two_same (neon_quad (rs), 1, et.size);
17582 }
17583
17584 static void
17585 do_neon_cnt (void)
17586 {
17587   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17588   struct neon_type_el et = neon_check_type (2, rs,
17589     N_EQK | N_INT, N_8 | N_KEY);
17590   neon_two_same (neon_quad (rs), 1, et.size);
17591 }
17592
17593 static void
17594 do_neon_swp (void)
17595 {
17596   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
17597   neon_two_same (neon_quad (rs), 1, -1);
17598 }
17599
17600 static void
17601 do_neon_tbl_tbx (void)
17602 {
17603   unsigned listlenbits;
17604   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
17605
17606   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
17607     {
17608       first_error (_("bad list length for table lookup"));
17609       return;
17610     }
17611
17612   listlenbits = inst.operands[1].imm - 1;
17613   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17614   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17615   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17616   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17617   inst.instruction |= LOW4 (inst.operands[2].reg);
17618   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
17619   inst.instruction |= listlenbits << 8;
17620
17621   neon_dp_fixup (&inst);
17622 }
17623
17624 static void
17625 do_neon_ldm_stm (void)
17626 {
17627   /* P, U and L bits are part of bitmask.  */
17628   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
17629   unsigned offsetbits = inst.operands[1].imm * 2;
17630
17631   if (inst.operands[1].issingle)
17632     {
17633       do_vfp_nsyn_ldm_stm (is_dbmode);
17634       return;
17635     }
17636
17637   constraint (is_dbmode && !inst.operands[0].writeback,
17638               _("writeback (!) must be used for VLDMDB and VSTMDB"));
17639
17640   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
17641               _("register list must contain at least 1 and at most 16 "
17642                 "registers"));
17643
17644   inst.instruction |= inst.operands[0].reg << 16;
17645   inst.instruction |= inst.operands[0].writeback << 21;
17646   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
17647   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
17648
17649   inst.instruction |= offsetbits;
17650
17651   do_vfp_cond_or_thumb ();
17652 }
17653
17654 static void
17655 do_neon_ldr_str (void)
17656 {
17657   int is_ldr = (inst.instruction & (1 << 20)) != 0;
17658
17659   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
17660      And is UNPREDICTABLE in thumb mode.  */
17661   if (!is_ldr
17662       && inst.operands[1].reg == REG_PC
17663       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
17664     {
17665       if (thumb_mode)
17666         inst.error = _("Use of PC here is UNPREDICTABLE");
17667       else if (warn_on_deprecated)
17668         as_tsktsk (_("Use of PC here is deprecated"));
17669     }
17670
17671   if (inst.operands[0].issingle)
17672     {
17673       if (is_ldr)
17674         do_vfp_nsyn_opcode ("flds");
17675       else
17676         do_vfp_nsyn_opcode ("fsts");
17677
17678       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
17679       if (inst.vectype.el[0].size == 16)
17680         do_scalar_fp16_v82_encode ();
17681     }
17682   else
17683     {
17684       if (is_ldr)
17685         do_vfp_nsyn_opcode ("fldd");
17686       else
17687         do_vfp_nsyn_opcode ("fstd");
17688     }
17689 }
17690
17691 static void
17692 do_t_vldr_vstr_sysreg (void)
17693 {
17694   int fp_vldr_bitno = 20, sysreg_vldr_bitno = 20;
17695   bfd_boolean is_vldr = ((inst.instruction & (1 << fp_vldr_bitno)) != 0);
17696
17697   /* Use of PC is UNPREDICTABLE.  */
17698   if (inst.operands[1].reg == REG_PC)
17699     inst.error = _("Use of PC here is UNPREDICTABLE");
17700
17701   if (inst.operands[1].immisreg)
17702     inst.error = _("instruction does not accept register index");
17703
17704   if (!inst.operands[1].isreg)
17705     inst.error = _("instruction does not accept PC-relative addressing");
17706
17707   if (abs (inst.operands[1].imm) >= (1 << 7))
17708     inst.error = _("immediate value out of range");
17709
17710   inst.instruction = 0xec000f80;
17711   if (is_vldr)
17712     inst.instruction |= 1 << sysreg_vldr_bitno;
17713   encode_arm_cp_address (1, TRUE, FALSE, BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM);
17714   inst.instruction |= (inst.operands[0].imm & 0x7) << 13;
17715   inst.instruction |= (inst.operands[0].imm & 0x8) << 19;
17716 }
17717
17718 static void
17719 do_vldr_vstr (void)
17720 {
17721   bfd_boolean sysreg_op = !inst.operands[0].isreg;
17722
17723   /* VLDR/VSTR (System Register).  */
17724   if (sysreg_op)
17725     {
17726       if (!mark_feature_used (&arm_ext_v8_1m_main))
17727         as_bad (_("Instruction not permitted on this architecture"));
17728
17729       do_t_vldr_vstr_sysreg ();
17730     }
17731   /* VLDR/VSTR.  */
17732   else
17733     {
17734       if (!mark_feature_used (&fpu_vfp_ext_v1xd))
17735         as_bad (_("Instruction not permitted on this architecture"));
17736       do_neon_ldr_str ();
17737     }
17738 }
17739
17740 /* "interleave" version also handles non-interleaving register VLD1/VST1
17741    instructions.  */
17742
17743 static void
17744 do_neon_ld_st_interleave (void)
17745 {
17746   struct neon_type_el et = neon_check_type (1, NS_NULL,
17747                                             N_8 | N_16 | N_32 | N_64);
17748   unsigned alignbits = 0;
17749   unsigned idx;
17750   /* The bits in this table go:
17751      0: register stride of one (0) or two (1)
17752      1,2: register list length, minus one (1, 2, 3, 4).
17753      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
17754      We use -1 for invalid entries.  */
17755   const int typetable[] =
17756     {
17757       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
17758        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
17759        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
17760        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
17761     };
17762   int typebits;
17763
17764   if (et.type == NT_invtype)
17765     return;
17766
17767   if (inst.operands[1].immisalign)
17768     switch (inst.operands[1].imm >> 8)
17769       {
17770       case 64: alignbits = 1; break;
17771       case 128:
17772         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
17773             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17774           goto bad_alignment;
17775         alignbits = 2;
17776         break;
17777       case 256:
17778         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17779           goto bad_alignment;
17780         alignbits = 3;
17781         break;
17782       default:
17783       bad_alignment:
17784         first_error (_("bad alignment"));
17785         return;
17786       }
17787
17788   inst.instruction |= alignbits << 4;
17789   inst.instruction |= neon_logbits (et.size) << 6;
17790
17791   /* Bits [4:6] of the immediate in a list specifier encode register stride
17792      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
17793      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
17794      up the right value for "type" in a table based on this value and the given
17795      list style, then stick it back.  */
17796   idx = ((inst.operands[0].imm >> 4) & 7)
17797         | (((inst.instruction >> 8) & 3) << 3);
17798
17799   typebits = typetable[idx];
17800
17801   constraint (typebits == -1, _("bad list type for instruction"));
17802   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
17803               _("bad element type for instruction"));
17804
17805   inst.instruction &= ~0xf00;
17806   inst.instruction |= typebits << 8;
17807 }
17808
17809 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
17810    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
17811    otherwise. The variable arguments are a list of pairs of legal (size, align)
17812    values, terminated with -1.  */
17813
17814 static int
17815 neon_alignment_bit (int size, int align, int *do_alignment, ...)
17816 {
17817   va_list ap;
17818   int result = FAIL, thissize, thisalign;
17819
17820   if (!inst.operands[1].immisalign)
17821     {
17822       *do_alignment = 0;
17823       return SUCCESS;
17824     }
17825
17826   va_start (ap, do_alignment);
17827
17828   do
17829     {
17830       thissize = va_arg (ap, int);
17831       if (thissize == -1)
17832         break;
17833       thisalign = va_arg (ap, int);
17834
17835       if (size == thissize && align == thisalign)
17836         result = SUCCESS;
17837     }
17838   while (result != SUCCESS);
17839
17840   va_end (ap);
17841
17842   if (result == SUCCESS)
17843     *do_alignment = 1;
17844   else
17845     first_error (_("unsupported alignment for instruction"));
17846
17847   return result;
17848 }
17849
17850 static void
17851 do_neon_ld_st_lane (void)
17852 {
17853   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17854   int align_good, do_alignment = 0;
17855   int logsize = neon_logbits (et.size);
17856   int align = inst.operands[1].imm >> 8;
17857   int n = (inst.instruction >> 8) & 3;
17858   int max_el = 64 / et.size;
17859
17860   if (et.type == NT_invtype)
17861     return;
17862
17863   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
17864               _("bad list length"));
17865   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
17866               _("scalar index out of range"));
17867   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
17868               && et.size == 8,
17869               _("stride of 2 unavailable when element size is 8"));
17870
17871   switch (n)
17872     {
17873     case 0:  /* VLD1 / VST1.  */
17874       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
17875                                        32, 32, -1);
17876       if (align_good == FAIL)
17877         return;
17878       if (do_alignment)
17879         {
17880           unsigned alignbits = 0;
17881           switch (et.size)
17882             {
17883             case 16: alignbits = 0x1; break;
17884             case 32: alignbits = 0x3; break;
17885             default: ;
17886             }
17887           inst.instruction |= alignbits << 4;
17888         }
17889       break;
17890
17891     case 1:  /* VLD2 / VST2.  */
17892       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
17893                       16, 32, 32, 64, -1);
17894       if (align_good == FAIL)
17895         return;
17896       if (do_alignment)
17897         inst.instruction |= 1 << 4;
17898       break;
17899
17900     case 2:  /* VLD3 / VST3.  */
17901       constraint (inst.operands[1].immisalign,
17902                   _("can't use alignment with this instruction"));
17903       break;
17904
17905     case 3:  /* VLD4 / VST4.  */
17906       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17907                                        16, 64, 32, 64, 32, 128, -1);
17908       if (align_good == FAIL)
17909         return;
17910       if (do_alignment)
17911         {
17912           unsigned alignbits = 0;
17913           switch (et.size)
17914             {
17915             case 8:  alignbits = 0x1; break;
17916             case 16: alignbits = 0x1; break;
17917             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
17918             default: ;
17919             }
17920           inst.instruction |= alignbits << 4;
17921         }
17922       break;
17923
17924     default: ;
17925     }
17926
17927   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
17928   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17929     inst.instruction |= 1 << (4 + logsize);
17930
17931   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
17932   inst.instruction |= logsize << 10;
17933 }
17934
17935 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
17936
17937 static void
17938 do_neon_ld_dup (void)
17939 {
17940   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17941   int align_good, do_alignment = 0;
17942
17943   if (et.type == NT_invtype)
17944     return;
17945
17946   switch ((inst.instruction >> 8) & 3)
17947     {
17948     case 0:  /* VLD1.  */
17949       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
17950       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17951                                        &do_alignment, 16, 16, 32, 32, -1);
17952       if (align_good == FAIL)
17953         return;
17954       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
17955         {
17956         case 1: break;
17957         case 2: inst.instruction |= 1 << 5; break;
17958         default: first_error (_("bad list length")); return;
17959         }
17960       inst.instruction |= neon_logbits (et.size) << 6;
17961       break;
17962
17963     case 1:  /* VLD2.  */
17964       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17965                                        &do_alignment, 8, 16, 16, 32, 32, 64,
17966                                        -1);
17967       if (align_good == FAIL)
17968         return;
17969       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
17970                   _("bad list length"));
17971       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17972         inst.instruction |= 1 << 5;
17973       inst.instruction |= neon_logbits (et.size) << 6;
17974       break;
17975
17976     case 2:  /* VLD3.  */
17977       constraint (inst.operands[1].immisalign,
17978                   _("can't use alignment with this instruction"));
17979       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
17980                   _("bad list length"));
17981       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17982         inst.instruction |= 1 << 5;
17983       inst.instruction |= neon_logbits (et.size) << 6;
17984       break;
17985
17986     case 3:  /* VLD4.  */
17987       {
17988         int align = inst.operands[1].imm >> 8;
17989         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17990                                          16, 64, 32, 64, 32, 128, -1);
17991         if (align_good == FAIL)
17992           return;
17993         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
17994                     _("bad list length"));
17995         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17996           inst.instruction |= 1 << 5;
17997         if (et.size == 32 && align == 128)
17998           inst.instruction |= 0x3 << 6;
17999         else
18000           inst.instruction |= neon_logbits (et.size) << 6;
18001       }
18002       break;
18003
18004     default: ;
18005     }
18006
18007   inst.instruction |= do_alignment << 4;
18008 }
18009
18010 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
18011    apart from bits [11:4].  */
18012
18013 static void
18014 do_neon_ldx_stx (void)
18015 {
18016   if (inst.operands[1].isreg)
18017     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
18018
18019   switch (NEON_LANE (inst.operands[0].imm))
18020     {
18021     case NEON_INTERLEAVE_LANES:
18022       NEON_ENCODE (INTERLV, inst);
18023       do_neon_ld_st_interleave ();
18024       break;
18025
18026     case NEON_ALL_LANES:
18027       NEON_ENCODE (DUP, inst);
18028       if (inst.instruction == N_INV)
18029         {
18030           first_error ("only loads support such operands");
18031           break;
18032         }
18033       do_neon_ld_dup ();
18034       break;
18035
18036     default:
18037       NEON_ENCODE (LANE, inst);
18038       do_neon_ld_st_lane ();
18039     }
18040
18041   /* L bit comes from bit mask.  */
18042   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18043   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18044   inst.instruction |= inst.operands[1].reg << 16;
18045
18046   if (inst.operands[1].postind)
18047     {
18048       int postreg = inst.operands[1].imm & 0xf;
18049       constraint (!inst.operands[1].immisreg,
18050                   _("post-index must be a register"));
18051       constraint (postreg == 0xd || postreg == 0xf,
18052                   _("bad register for post-index"));
18053       inst.instruction |= postreg;
18054     }
18055   else
18056     {
18057       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
18058       constraint (inst.relocs[0].exp.X_op != O_constant
18059                   || inst.relocs[0].exp.X_add_number != 0,
18060                   BAD_ADDR_MODE);
18061
18062       if (inst.operands[1].writeback)
18063         {
18064           inst.instruction |= 0xd;
18065         }
18066       else
18067         inst.instruction |= 0xf;
18068     }
18069
18070   if (thumb_mode)
18071     inst.instruction |= 0xf9000000;
18072   else
18073     inst.instruction |= 0xf4000000;
18074 }
18075
18076 /* FP v8.  */
18077 static void
18078 do_vfp_nsyn_fpv8 (enum neon_shape rs)
18079 {
18080   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
18081      D register operands.  */
18082   if (neon_shape_class[rs] == SC_DOUBLE)
18083     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18084                 _(BAD_FPU));
18085
18086   NEON_ENCODE (FPV8, inst);
18087
18088   if (rs == NS_FFF || rs == NS_HHH)
18089     {
18090       do_vfp_sp_dyadic ();
18091
18092       /* ARMv8.2 fp16 instruction.  */
18093       if (rs == NS_HHH)
18094         do_scalar_fp16_v82_encode ();
18095     }
18096   else
18097     do_vfp_dp_rd_rn_rm ();
18098
18099   if (rs == NS_DDD)
18100     inst.instruction |= 0x100;
18101
18102   inst.instruction |= 0xf0000000;
18103 }
18104
18105 static void
18106 do_vsel (void)
18107 {
18108   set_pred_insn_type (OUTSIDE_PRED_INSN);
18109
18110   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
18111     first_error (_("invalid instruction shape"));
18112 }
18113
18114 static void
18115 do_vmaxnm (void)
18116 {
18117   set_pred_insn_type (OUTSIDE_PRED_INSN);
18118
18119   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
18120     return;
18121
18122   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
18123     return;
18124
18125   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
18126 }
18127
18128 static void
18129 do_vrint_1 (enum neon_cvt_mode mode)
18130 {
18131   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
18132   struct neon_type_el et;
18133
18134   if (rs == NS_NULL)
18135     return;
18136
18137   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
18138      D register operands.  */
18139   if (neon_shape_class[rs] == SC_DOUBLE)
18140     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18141                 _(BAD_FPU));
18142
18143   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
18144                         | N_VFP);
18145   if (et.type != NT_invtype)
18146     {
18147       /* VFP encodings.  */
18148       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
18149           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
18150         set_pred_insn_type (OUTSIDE_PRED_INSN);
18151
18152       NEON_ENCODE (FPV8, inst);
18153       if (rs == NS_FF || rs == NS_HH)
18154         do_vfp_sp_monadic ();
18155       else
18156         do_vfp_dp_rd_rm ();
18157
18158       switch (mode)
18159         {
18160         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
18161         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
18162         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
18163         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
18164         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
18165         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
18166         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
18167         default: abort ();
18168         }
18169
18170       inst.instruction |= (rs == NS_DD) << 8;
18171       do_vfp_cond_or_thumb ();
18172
18173       /* ARMv8.2 fp16 vrint instruction.  */
18174       if (rs == NS_HH)
18175       do_scalar_fp16_v82_encode ();
18176     }
18177   else
18178     {
18179       /* Neon encodings (or something broken...).  */
18180       inst.error = NULL;
18181       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
18182
18183       if (et.type == NT_invtype)
18184         return;
18185
18186       set_pred_insn_type (OUTSIDE_PRED_INSN);
18187       NEON_ENCODE (FLOAT, inst);
18188
18189       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
18190         return;
18191
18192       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18193       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18194       inst.instruction |= LOW4 (inst.operands[1].reg);
18195       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18196       inst.instruction |= neon_quad (rs) << 6;
18197       /* Mask off the original size bits and reencode them.  */
18198       inst.instruction = ((inst.instruction & 0xfff3ffff)
18199                           | neon_logbits (et.size) << 18);
18200
18201       switch (mode)
18202         {
18203         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
18204         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
18205         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
18206         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
18207         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
18208         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
18209         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
18210         default: abort ();
18211         }
18212
18213       if (thumb_mode)
18214         inst.instruction |= 0xfc000000;
18215       else
18216         inst.instruction |= 0xf0000000;
18217     }
18218 }
18219
18220 static void
18221 do_vrintx (void)
18222 {
18223   do_vrint_1 (neon_cvt_mode_x);
18224 }
18225
18226 static void
18227 do_vrintz (void)
18228 {
18229   do_vrint_1 (neon_cvt_mode_z);
18230 }
18231
18232 static void
18233 do_vrintr (void)
18234 {
18235   do_vrint_1 (neon_cvt_mode_r);
18236 }
18237
18238 static void
18239 do_vrinta (void)
18240 {
18241   do_vrint_1 (neon_cvt_mode_a);
18242 }
18243
18244 static void
18245 do_vrintn (void)
18246 {
18247   do_vrint_1 (neon_cvt_mode_n);
18248 }
18249
18250 static void
18251 do_vrintp (void)
18252 {
18253   do_vrint_1 (neon_cvt_mode_p);
18254 }
18255
18256 static void
18257 do_vrintm (void)
18258 {
18259   do_vrint_1 (neon_cvt_mode_m);
18260 }
18261
18262 static unsigned
18263 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
18264 {
18265   unsigned regno = NEON_SCALAR_REG (opnd);
18266   unsigned elno = NEON_SCALAR_INDEX (opnd);
18267
18268   if (elsize == 16 && elno < 2 && regno < 16)
18269     return regno | (elno << 4);
18270   else if (elsize == 32 && elno == 0)
18271     return regno;
18272
18273   first_error (_("scalar out of range"));
18274   return 0;
18275 }
18276
18277 static void
18278 do_vcmla (void)
18279 {
18280   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
18281               _(BAD_FPU));
18282   constraint (inst.relocs[0].exp.X_op != O_constant,
18283               _("expression too complex"));
18284   unsigned rot = inst.relocs[0].exp.X_add_number;
18285   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
18286               _("immediate out of range"));
18287   rot /= 90;
18288   if (inst.operands[2].isscalar)
18289     {
18290       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
18291       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
18292                                        N_KEY | N_F16 | N_F32).size;
18293       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
18294       inst.is_neon = 1;
18295       inst.instruction = 0xfe000800;
18296       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18297       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18298       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
18299       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
18300       inst.instruction |= LOW4 (m);
18301       inst.instruction |= HI1 (m) << 5;
18302       inst.instruction |= neon_quad (rs) << 6;
18303       inst.instruction |= rot << 20;
18304       inst.instruction |= (size == 32) << 23;
18305     }
18306   else
18307     {
18308       enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
18309       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
18310                                        N_KEY | N_F16 | N_F32).size;
18311       neon_three_same (neon_quad (rs), 0, -1);
18312       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
18313       inst.instruction |= 0xfc200800;
18314       inst.instruction |= rot << 23;
18315       inst.instruction |= (size == 32) << 20;
18316     }
18317 }
18318
18319 static void
18320 do_vcadd (void)
18321 {
18322   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
18323               _(BAD_FPU));
18324   constraint (inst.relocs[0].exp.X_op != O_constant,
18325               _("expression too complex"));
18326   unsigned rot = inst.relocs[0].exp.X_add_number;
18327   constraint (rot != 90 && rot != 270, _("immediate out of range"));
18328   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
18329   unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
18330                                    N_KEY | N_F16 | N_F32).size;
18331   neon_three_same (neon_quad (rs), 0, -1);
18332   inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
18333   inst.instruction |= 0xfc800800;
18334   inst.instruction |= (rot == 270) << 24;
18335   inst.instruction |= (size == 32) << 20;
18336 }
18337
18338 /* Dot Product instructions encoding support.  */
18339
18340 static void
18341 do_neon_dotproduct (int unsigned_p)
18342 {
18343   enum neon_shape rs;
18344   unsigned scalar_oprd2 = 0;
18345   int high8;
18346
18347   if (inst.cond != COND_ALWAYS)
18348     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
18349                "is UNPREDICTABLE"));
18350
18351   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
18352               _(BAD_FPU));
18353
18354   /* Dot Product instructions are in three-same D/Q register format or the third
18355      operand can be a scalar index register.  */
18356   if (inst.operands[2].isscalar)
18357     {
18358       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
18359       high8 = 0xfe000000;
18360       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
18361     }
18362   else
18363     {
18364       high8 = 0xfc000000;
18365       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
18366     }
18367
18368   if (unsigned_p)
18369     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
18370   else
18371     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
18372
18373   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
18374      Product instruction, so we pass 0 as the "ubit" parameter.  And the
18375      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
18376   neon_three_same (neon_quad (rs), 0, 32);
18377
18378   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
18379      different NEON three-same encoding.  */
18380   inst.instruction &= 0x00ffffff;
18381   inst.instruction |= high8;
18382   /* Encode 'U' bit which indicates signedness.  */
18383   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
18384   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
18385      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
18386      the instruction encoding.  */
18387   if (inst.operands[2].isscalar)
18388     {
18389       inst.instruction &= 0xffffffd0;
18390       inst.instruction |= LOW4 (scalar_oprd2);
18391       inst.instruction |= HI1 (scalar_oprd2) << 5;
18392     }
18393 }
18394
18395 /* Dot Product instructions for signed integer.  */
18396
18397 static void
18398 do_neon_dotproduct_s (void)
18399 {
18400   return do_neon_dotproduct (0);
18401 }
18402
18403 /* Dot Product instructions for unsigned integer.  */
18404
18405 static void
18406 do_neon_dotproduct_u (void)
18407 {
18408   return do_neon_dotproduct (1);
18409 }
18410
18411 /* Crypto v1 instructions.  */
18412 static void
18413 do_crypto_2op_1 (unsigned elttype, int op)
18414 {
18415   set_pred_insn_type (OUTSIDE_PRED_INSN);
18416
18417   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
18418       == NT_invtype)
18419     return;
18420
18421   inst.error = NULL;
18422
18423   NEON_ENCODE (INTEGER, inst);
18424   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
18425   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
18426   inst.instruction |= LOW4 (inst.operands[1].reg);
18427   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
18428   if (op != -1)
18429     inst.instruction |= op << 6;
18430
18431   if (thumb_mode)
18432     inst.instruction |= 0xfc000000;
18433   else
18434     inst.instruction |= 0xf0000000;
18435 }
18436
18437 static void
18438 do_crypto_3op_1 (int u, int op)
18439 {
18440   set_pred_insn_type (OUTSIDE_PRED_INSN);
18441
18442   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
18443                        N_32 | N_UNT | N_KEY).type == NT_invtype)
18444     return;
18445
18446   inst.error = NULL;
18447
18448   NEON_ENCODE (INTEGER, inst);
18449   neon_three_same (1, u, 8 << op);
18450 }
18451
18452 static void
18453 do_aese (void)
18454 {
18455   do_crypto_2op_1 (N_8, 0);
18456 }
18457
18458 static void
18459 do_aesd (void)
18460 {
18461   do_crypto_2op_1 (N_8, 1);
18462 }
18463
18464 static void
18465 do_aesmc (void)
18466 {
18467   do_crypto_2op_1 (N_8, 2);
18468 }
18469
18470 static void
18471 do_aesimc (void)
18472 {
18473   do_crypto_2op_1 (N_8, 3);
18474 }
18475
18476 static void
18477 do_sha1c (void)
18478 {
18479   do_crypto_3op_1 (0, 0);
18480 }
18481
18482 static void
18483 do_sha1p (void)
18484 {
18485   do_crypto_3op_1 (0, 1);
18486 }
18487
18488 static void
18489 do_sha1m (void)
18490 {
18491   do_crypto_3op_1 (0, 2);
18492 }
18493
18494 static void
18495 do_sha1su0 (void)
18496 {
18497   do_crypto_3op_1 (0, 3);
18498 }
18499
18500 static void
18501 do_sha256h (void)
18502 {
18503   do_crypto_3op_1 (1, 0);
18504 }
18505
18506 static void
18507 do_sha256h2 (void)
18508 {
18509   do_crypto_3op_1 (1, 1);
18510 }
18511
18512 static void
18513 do_sha256su1 (void)
18514 {
18515   do_crypto_3op_1 (1, 2);
18516 }
18517
18518 static void
18519 do_sha1h (void)
18520 {
18521   do_crypto_2op_1 (N_32, -1);
18522 }
18523
18524 static void
18525 do_sha1su1 (void)
18526 {
18527   do_crypto_2op_1 (N_32, 0);
18528 }
18529
18530 static void
18531 do_sha256su0 (void)
18532 {
18533   do_crypto_2op_1 (N_32, 1);
18534 }
18535
18536 static void
18537 do_crc32_1 (unsigned int poly, unsigned int sz)
18538 {
18539   unsigned int Rd = inst.operands[0].reg;
18540   unsigned int Rn = inst.operands[1].reg;
18541   unsigned int Rm = inst.operands[2].reg;
18542
18543   set_pred_insn_type (OUTSIDE_PRED_INSN);
18544   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
18545   inst.instruction |= LOW4 (Rn) << 16;
18546   inst.instruction |= LOW4 (Rm);
18547   inst.instruction |= sz << (thumb_mode ? 4 : 21);
18548   inst.instruction |= poly << (thumb_mode ? 20 : 9);
18549
18550   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
18551     as_warn (UNPRED_REG ("r15"));
18552 }
18553
18554 static void
18555 do_crc32b (void)
18556 {
18557   do_crc32_1 (0, 0);
18558 }
18559
18560 static void
18561 do_crc32h (void)
18562 {
18563   do_crc32_1 (0, 1);
18564 }
18565
18566 static void
18567 do_crc32w (void)
18568 {
18569   do_crc32_1 (0, 2);
18570 }
18571
18572 static void
18573 do_crc32cb (void)
18574 {
18575   do_crc32_1 (1, 0);
18576 }
18577
18578 static void
18579 do_crc32ch (void)
18580 {
18581   do_crc32_1 (1, 1);
18582 }
18583
18584 static void
18585 do_crc32cw (void)
18586 {
18587   do_crc32_1 (1, 2);
18588 }
18589
18590 static void
18591 do_vjcvt (void)
18592 {
18593   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
18594               _(BAD_FPU));
18595   neon_check_type (2, NS_FD, N_S32, N_F64);
18596   do_vfp_sp_dp_cvt ();
18597   do_vfp_cond_or_thumb ();
18598 }
18599
18600 \f
18601 /* Overall per-instruction processing.  */
18602
18603 /* We need to be able to fix up arbitrary expressions in some statements.
18604    This is so that we can handle symbols that are an arbitrary distance from
18605    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
18606    which returns part of an address in a form which will be valid for
18607    a data instruction.  We do this by pushing the expression into a symbol
18608    in the expr_section, and creating a fix for that.  */
18609
18610 static void
18611 fix_new_arm (fragS *       frag,
18612              int           where,
18613              short int     size,
18614              expressionS * exp,
18615              int           pc_rel,
18616              int           reloc)
18617 {
18618   fixS *           new_fix;
18619
18620   switch (exp->X_op)
18621     {
18622     case O_constant:
18623       if (pc_rel)
18624         {
18625           /* Create an absolute valued symbol, so we have something to
18626              refer to in the object file.  Unfortunately for us, gas's
18627              generic expression parsing will already have folded out
18628              any use of .set foo/.type foo %function that may have
18629              been used to set type information of the target location,
18630              that's being specified symbolically.  We have to presume
18631              the user knows what they are doing.  */
18632           char name[16 + 8];
18633           symbolS *symbol;
18634
18635           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
18636
18637           symbol = symbol_find_or_make (name);
18638           S_SET_SEGMENT (symbol, absolute_section);
18639           symbol_set_frag (symbol, &zero_address_frag);
18640           S_SET_VALUE (symbol, exp->X_add_number);
18641           exp->X_op = O_symbol;
18642           exp->X_add_symbol = symbol;
18643           exp->X_add_number = 0;
18644         }
18645       /* FALLTHROUGH */
18646     case O_symbol:
18647     case O_add:
18648     case O_subtract:
18649       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
18650                              (enum bfd_reloc_code_real) reloc);
18651       break;
18652
18653     default:
18654       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
18655                                   pc_rel, (enum bfd_reloc_code_real) reloc);
18656       break;
18657     }
18658
18659   /* Mark whether the fix is to a THUMB instruction, or an ARM
18660      instruction.  */
18661   new_fix->tc_fix_data = thumb_mode;
18662 }
18663
18664 /* Create a frg for an instruction requiring relaxation.  */
18665 static void
18666 output_relax_insn (void)
18667 {
18668   char * to;
18669   symbolS *sym;
18670   int offset;
18671
18672   /* The size of the instruction is unknown, so tie the debug info to the
18673      start of the instruction.  */
18674   dwarf2_emit_insn (0);
18675
18676   switch (inst.relocs[0].exp.X_op)
18677     {
18678     case O_symbol:
18679       sym = inst.relocs[0].exp.X_add_symbol;
18680       offset = inst.relocs[0].exp.X_add_number;
18681       break;
18682     case O_constant:
18683       sym = NULL;
18684       offset = inst.relocs[0].exp.X_add_number;
18685       break;
18686     default:
18687       sym = make_expr_symbol (&inst.relocs[0].exp);
18688       offset = 0;
18689       break;
18690   }
18691   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
18692                  inst.relax, sym, offset, NULL/*offset, opcode*/);
18693   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
18694 }
18695
18696 /* Write a 32-bit thumb instruction to buf.  */
18697 static void
18698 put_thumb32_insn (char * buf, unsigned long insn)
18699 {
18700   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
18701   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
18702 }
18703
18704 static void
18705 output_inst (const char * str)
18706 {
18707   char * to = NULL;
18708
18709   if (inst.error)
18710     {
18711       as_bad ("%s -- `%s'", inst.error, str);
18712       return;
18713     }
18714   if (inst.relax)
18715     {
18716       output_relax_insn ();
18717       return;
18718     }
18719   if (inst.size == 0)
18720     return;
18721
18722   to = frag_more (inst.size);
18723   /* PR 9814: Record the thumb mode into the current frag so that we know
18724      what type of NOP padding to use, if necessary.  We override any previous
18725      setting so that if the mode has changed then the NOPS that we use will
18726      match the encoding of the last instruction in the frag.  */
18727   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
18728
18729   if (thumb_mode && (inst.size > THUMB_SIZE))
18730     {
18731       gas_assert (inst.size == (2 * THUMB_SIZE));
18732       put_thumb32_insn (to, inst.instruction);
18733     }
18734   else if (inst.size > INSN_SIZE)
18735     {
18736       gas_assert (inst.size == (2 * INSN_SIZE));
18737       md_number_to_chars (to, inst.instruction, INSN_SIZE);
18738       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
18739     }
18740   else
18741     md_number_to_chars (to, inst.instruction, inst.size);
18742
18743   int r;
18744   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
18745     {
18746       if (inst.relocs[r].type != BFD_RELOC_UNUSED)
18747         fix_new_arm (frag_now, to - frag_now->fr_literal,
18748                      inst.size, & inst.relocs[r].exp, inst.relocs[r].pc_rel,
18749                      inst.relocs[r].type);
18750     }
18751
18752   dwarf2_emit_insn (inst.size);
18753 }
18754
18755 static char *
18756 output_it_inst (int cond, int mask, char * to)
18757 {
18758   unsigned long instruction = 0xbf00;
18759
18760   mask &= 0xf;
18761   instruction |= mask;
18762   instruction |= cond << 4;
18763
18764   if (to == NULL)
18765     {
18766       to = frag_more (2);
18767 #ifdef OBJ_ELF
18768       dwarf2_emit_insn (2);
18769 #endif
18770     }
18771
18772   md_number_to_chars (to, instruction, 2);
18773
18774   return to;
18775 }
18776
18777 /* Tag values used in struct asm_opcode's tag field.  */
18778 enum opcode_tag
18779 {
18780   OT_unconditional,     /* Instruction cannot be conditionalized.
18781                            The ARM condition field is still 0xE.  */
18782   OT_unconditionalF,    /* Instruction cannot be conditionalized
18783                            and carries 0xF in its ARM condition field.  */
18784   OT_csuffix,           /* Instruction takes a conditional suffix.  */
18785   OT_csuffixF,          /* Some forms of the instruction take a scalar
18786                            conditional suffix, others place 0xF where the
18787                            condition field would be, others take a vector
18788                            conditional suffix.  */
18789   OT_cinfix3,           /* Instruction takes a conditional infix,
18790                            beginning at character index 3.  (In
18791                            unified mode, it becomes a suffix.)  */
18792   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
18793                             tsts, cmps, cmns, and teqs. */
18794   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
18795                            character index 3, even in unified mode.  Used for
18796                            legacy instructions where suffix and infix forms
18797                            may be ambiguous.  */
18798   OT_csuf_or_in3,       /* Instruction takes either a conditional
18799                            suffix or an infix at character index 3.  */
18800   OT_odd_infix_unc,     /* This is the unconditional variant of an
18801                            instruction that takes a conditional infix
18802                            at an unusual position.  In unified mode,
18803                            this variant will accept a suffix.  */
18804   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
18805                            are the conditional variants of instructions that
18806                            take conditional infixes in unusual positions.
18807                            The infix appears at character index
18808                            (tag - OT_odd_infix_0).  These are not accepted
18809                            in unified mode.  */
18810 };
18811
18812 /* Subroutine of md_assemble, responsible for looking up the primary
18813    opcode from the mnemonic the user wrote.  STR points to the
18814    beginning of the mnemonic.
18815
18816    This is not simply a hash table lookup, because of conditional
18817    variants.  Most instructions have conditional variants, which are
18818    expressed with a _conditional affix_ to the mnemonic.  If we were
18819    to encode each conditional variant as a literal string in the opcode
18820    table, it would have approximately 20,000 entries.
18821
18822    Most mnemonics take this affix as a suffix, and in unified syntax,
18823    'most' is upgraded to 'all'.  However, in the divided syntax, some
18824    instructions take the affix as an infix, notably the s-variants of
18825    the arithmetic instructions.  Of those instructions, all but six
18826    have the infix appear after the third character of the mnemonic.
18827
18828    Accordingly, the algorithm for looking up primary opcodes given
18829    an identifier is:
18830
18831    1. Look up the identifier in the opcode table.
18832       If we find a match, go to step U.
18833
18834    2. Look up the last two characters of the identifier in the
18835       conditions table.  If we find a match, look up the first N-2
18836       characters of the identifier in the opcode table.  If we
18837       find a match, go to step CE.
18838
18839    3. Look up the fourth and fifth characters of the identifier in
18840       the conditions table.  If we find a match, extract those
18841       characters from the identifier, and look up the remaining
18842       characters in the opcode table.  If we find a match, go
18843       to step CM.
18844
18845    4. Fail.
18846
18847    U. Examine the tag field of the opcode structure, in case this is
18848       one of the six instructions with its conditional infix in an
18849       unusual place.  If it is, the tag tells us where to find the
18850       infix; look it up in the conditions table and set inst.cond
18851       accordingly.  Otherwise, this is an unconditional instruction.
18852       Again set inst.cond accordingly.  Return the opcode structure.
18853
18854   CE. Examine the tag field to make sure this is an instruction that
18855       should receive a conditional suffix.  If it is not, fail.
18856       Otherwise, set inst.cond from the suffix we already looked up,
18857       and return the opcode structure.
18858
18859   CM. Examine the tag field to make sure this is an instruction that
18860       should receive a conditional infix after the third character.
18861       If it is not, fail.  Otherwise, undo the edits to the current
18862       line of input and proceed as for case CE.  */
18863
18864 static const struct asm_opcode *
18865 opcode_lookup (char **str)
18866 {
18867   char *end, *base;
18868   char *affix;
18869   const struct asm_opcode *opcode;
18870   const struct asm_cond *cond;
18871   char save[2];
18872
18873   /* Scan up to the end of the mnemonic, which must end in white space,
18874      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
18875   for (base = end = *str; *end != '\0'; end++)
18876     if (*end == ' ' || *end == '.')
18877       break;
18878
18879   if (end == base)
18880     return NULL;
18881
18882   /* Handle a possible width suffix and/or Neon type suffix.  */
18883   if (end[0] == '.')
18884     {
18885       int offset = 2;
18886
18887       /* The .w and .n suffixes are only valid if the unified syntax is in
18888          use.  */
18889       if (unified_syntax && end[1] == 'w')
18890         inst.size_req = 4;
18891       else if (unified_syntax && end[1] == 'n')
18892         inst.size_req = 2;
18893       else
18894         offset = 0;
18895
18896       inst.vectype.elems = 0;
18897
18898       *str = end + offset;
18899
18900       if (end[offset] == '.')
18901         {
18902           /* See if we have a Neon type suffix (possible in either unified or
18903              non-unified ARM syntax mode).  */
18904           if (parse_neon_type (&inst.vectype, str) == FAIL)
18905             return NULL;
18906         }
18907       else if (end[offset] != '\0' && end[offset] != ' ')
18908         return NULL;
18909     }
18910   else
18911     *str = end;
18912
18913   /* Look for unaffixed or special-case affixed mnemonic.  */
18914   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18915                                                     end - base);
18916   if (opcode)
18917     {
18918       /* step U */
18919       if (opcode->tag < OT_odd_infix_0)
18920         {
18921           inst.cond = COND_ALWAYS;
18922           return opcode;
18923         }
18924
18925       if (warn_on_deprecated && unified_syntax)
18926         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
18927       affix = base + (opcode->tag - OT_odd_infix_0);
18928       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18929       gas_assert (cond);
18930
18931       inst.cond = cond->value;
18932       return opcode;
18933     }
18934  if (ARM_CPU_HAS_FEATURE (cpu_variant, mve_ext))
18935    {
18936     /* Cannot have a conditional suffix on a mnemonic of less than a character.
18937      */
18938     if (end - base < 2)
18939       return NULL;
18940      affix = end - 1;
18941      cond = (const struct asm_cond *) hash_find_n (arm_vcond_hsh, affix, 1);
18942      opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18943                                                       affix - base);
18944      /* If this opcode can not be vector predicated then don't accept it with a
18945         vector predication code.  */
18946      if (opcode && !opcode->mayBeVecPred)
18947        opcode = NULL;
18948    }
18949   if (!opcode || !cond)
18950     {
18951       /* Cannot have a conditional suffix on a mnemonic of less than two
18952          characters.  */
18953       if (end - base < 3)
18954         return NULL;
18955
18956       /* Look for suffixed mnemonic.  */
18957       affix = end - 2;
18958       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18959       opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18960                                                         affix - base);
18961     }
18962
18963   if (opcode && cond)
18964     {
18965       /* step CE */
18966       switch (opcode->tag)
18967         {
18968         case OT_cinfix3_legacy:
18969           /* Ignore conditional suffixes matched on infix only mnemonics.  */
18970           break;
18971
18972         case OT_cinfix3:
18973         case OT_cinfix3_deprecated:
18974         case OT_odd_infix_unc:
18975           if (!unified_syntax)
18976             return NULL;
18977           /* Fall through.  */
18978
18979         case OT_csuffix:
18980         case OT_csuffixF:
18981         case OT_csuf_or_in3:
18982           inst.cond = cond->value;
18983           return opcode;
18984
18985         case OT_unconditional:
18986         case OT_unconditionalF:
18987           if (thumb_mode)
18988             inst.cond = cond->value;
18989           else
18990             {
18991               /* Delayed diagnostic.  */
18992               inst.error = BAD_COND;
18993               inst.cond = COND_ALWAYS;
18994             }
18995           return opcode;
18996
18997         default:
18998           return NULL;
18999         }
19000     }
19001
19002   /* Cannot have a usual-position infix on a mnemonic of less than
19003      six characters (five would be a suffix).  */
19004   if (end - base < 6)
19005     return NULL;
19006
19007   /* Look for infixed mnemonic in the usual position.  */
19008   affix = base + 3;
19009   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
19010   if (!cond)
19011     return NULL;
19012
19013   memcpy (save, affix, 2);
19014   memmove (affix, affix + 2, (end - affix) - 2);
19015   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
19016                                                     (end - base) - 2);
19017   memmove (affix + 2, affix, (end - affix) - 2);
19018   memcpy (affix, save, 2);
19019
19020   if (opcode
19021       && (opcode->tag == OT_cinfix3
19022           || opcode->tag == OT_cinfix3_deprecated
19023           || opcode->tag == OT_csuf_or_in3
19024           || opcode->tag == OT_cinfix3_legacy))
19025     {
19026       /* Step CM.  */
19027       if (warn_on_deprecated && unified_syntax
19028           && (opcode->tag == OT_cinfix3
19029               || opcode->tag == OT_cinfix3_deprecated))
19030         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
19031
19032       inst.cond = cond->value;
19033       return opcode;
19034     }
19035
19036   return NULL;
19037 }
19038
19039 /* This function generates an initial IT instruction, leaving its block
19040    virtually open for the new instructions. Eventually,
19041    the mask will be updated by now_pred_add_mask () each time
19042    a new instruction needs to be included in the IT block.
19043    Finally, the block is closed with close_automatic_it_block ().
19044    The block closure can be requested either from md_assemble (),
19045    a tencode (), or due to a label hook.  */
19046
19047 static void
19048 new_automatic_it_block (int cond)
19049 {
19050   now_pred.state = AUTOMATIC_PRED_BLOCK;
19051   now_pred.mask = 0x18;
19052   now_pred.cc = cond;
19053   now_pred.block_length = 1;
19054   mapping_state (MAP_THUMB);
19055   now_pred.insn = output_it_inst (cond, now_pred.mask, NULL);
19056   now_pred.warn_deprecated = FALSE;
19057   now_pred.insn_cond = TRUE;
19058 }
19059
19060 /* Close an automatic IT block.
19061    See comments in new_automatic_it_block ().  */
19062
19063 static void
19064 close_automatic_it_block (void)
19065 {
19066   now_pred.mask = 0x10;
19067   now_pred.block_length = 0;
19068 }
19069
19070 /* Update the mask of the current automatically-generated IT
19071    instruction. See comments in new_automatic_it_block ().  */
19072
19073 static void
19074 now_pred_add_mask (int cond)
19075 {
19076 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
19077 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
19078                                               | ((bitvalue) << (nbit)))
19079   const int resulting_bit = (cond & 1);
19080
19081   now_pred.mask &= 0xf;
19082   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
19083                                    resulting_bit,
19084                                   (5 - now_pred.block_length));
19085   now_pred.mask = SET_BIT_VALUE (now_pred.mask,
19086                                    1,
19087                                    ((5 - now_pred.block_length) - 1));
19088   output_it_inst (now_pred.cc, now_pred.mask, now_pred.insn);
19089
19090 #undef CLEAR_BIT
19091 #undef SET_BIT_VALUE
19092 }
19093
19094 /* The IT blocks handling machinery is accessed through the these functions:
19095      it_fsm_pre_encode ()               from md_assemble ()
19096      set_pred_insn_type ()              optional, from the tencode functions
19097      set_pred_insn_type_last ()         ditto
19098      in_pred_block ()                   ditto
19099      it_fsm_post_encode ()              from md_assemble ()
19100      force_automatic_it_block_close ()  from label handling functions
19101
19102    Rationale:
19103      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
19104         initializing the IT insn type with a generic initial value depending
19105         on the inst.condition.
19106      2) During the tencode function, two things may happen:
19107         a) The tencode function overrides the IT insn type by
19108            calling either set_pred_insn_type (type) or
19109            set_pred_insn_type_last ().
19110         b) The tencode function queries the IT block state by
19111            calling in_pred_block () (i.e. to determine narrow/not narrow mode).
19112
19113         Both set_pred_insn_type and in_pred_block run the internal FSM state
19114         handling function (handle_pred_state), because: a) setting the IT insn
19115         type may incur in an invalid state (exiting the function),
19116         and b) querying the state requires the FSM to be updated.
19117         Specifically we want to avoid creating an IT block for conditional
19118         branches, so it_fsm_pre_encode is actually a guess and we can't
19119         determine whether an IT block is required until the tencode () routine
19120         has decided what type of instruction this actually it.
19121         Because of this, if set_pred_insn_type and in_pred_block have to be
19122         used, set_pred_insn_type has to be called first.
19123
19124         set_pred_insn_type_last () is a wrapper of set_pred_insn_type (type),
19125         that determines the insn IT type depending on the inst.cond code.
19126         When a tencode () routine encodes an instruction that can be
19127         either outside an IT block, or, in the case of being inside, has to be
19128         the last one, set_pred_insn_type_last () will determine the proper
19129         IT instruction type based on the inst.cond code. Otherwise,
19130         set_pred_insn_type can be called for overriding that logic or
19131         for covering other cases.
19132
19133         Calling handle_pred_state () may not transition the IT block state to
19134         OUTSIDE_PRED_BLOCK immediately, since the (current) state could be
19135         still queried. Instead, if the FSM determines that the state should
19136         be transitioned to OUTSIDE_PRED_BLOCK, a flag is marked to be closed
19137         after the tencode () function: that's what it_fsm_post_encode () does.
19138
19139         Since in_pred_block () calls the state handling function to get an
19140         updated state, an error may occur (due to invalid insns combination).
19141         In that case, inst.error is set.
19142         Therefore, inst.error has to be checked after the execution of
19143         the tencode () routine.
19144
19145      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
19146         any pending state change (if any) that didn't take place in
19147         handle_pred_state () as explained above.  */
19148
19149 static void
19150 it_fsm_pre_encode (void)
19151 {
19152   if (inst.cond != COND_ALWAYS)
19153     inst.pred_insn_type =  INSIDE_IT_INSN;
19154   else
19155     inst.pred_insn_type = OUTSIDE_PRED_INSN;
19156
19157   now_pred.state_handled = 0;
19158 }
19159
19160 /* IT state FSM handling function.  */
19161 /* MVE instructions and non-MVE instructions are handled differently because of
19162    the introduction of VPT blocks.
19163    Specifications say that any non-MVE instruction inside a VPT block is
19164    UNPREDICTABLE, with the exception of the BKPT instruction.  Whereas most MVE
19165    instructions are deemed to be UNPREDICTABLE if inside an IT block.  For the
19166    few exceptions this will be handled at their respective handler functions.
19167    The error messages provided depending on the different combinations possible
19168    are described in the cases below:
19169    For 'most' MVE instructions:
19170    1) In an IT block, with an IT code: syntax error
19171    2) In an IT block, with a VPT code: error: must be in a VPT block
19172    3) In an IT block, with no code: warning: UNPREDICTABLE
19173    4) In a VPT block, with an IT code: syntax error
19174    5) In a VPT block, with a VPT code: OK!
19175    6) In a VPT block, with no code: error: missing code
19176    7) Outside a pred block, with an IT code: error: syntax error
19177    8) Outside a pred block, with a VPT code: error: should be in a VPT block
19178    9) Outside a pred block, with no code: OK!
19179    For non-MVE instructions:
19180    10) In an IT block, with an IT code: OK!
19181    11) In an IT block, with a VPT code: syntax error
19182    12) In an IT block, with no code: error: missing code
19183    13) In a VPT block, with an IT code: error: should be in an IT block
19184    14) In a VPT block, with a VPT code: syntax error
19185    15) In a VPT block, with no code: UNPREDICTABLE
19186    16) Outside a pred block, with an IT code: error: should be in an IT block
19187    17) Outside a pred block, with a VPT code: syntax error
19188    18) Outside a pred block, with no code: OK!
19189  */
19190
19191
19192 static int
19193 handle_pred_state (void)
19194 {
19195   now_pred.state_handled = 1;
19196   now_pred.insn_cond = FALSE;
19197
19198   switch (now_pred.state)
19199     {
19200     case OUTSIDE_PRED_BLOCK:
19201       switch (inst.pred_insn_type)
19202         {
19203         case MVE_OUTSIDE_PRED_INSN:
19204           if (inst.cond < COND_ALWAYS)
19205             {
19206               /* Case 7: Outside a pred block, with an IT code: error: syntax
19207                  error.  */
19208               inst.error = BAD_SYNTAX;
19209               return FAIL;
19210             }
19211           /* Case 9:  Outside a pred block, with no code: OK!  */
19212           break;
19213         case OUTSIDE_PRED_INSN:
19214           if (inst.cond > COND_ALWAYS)
19215             {
19216               /* Case 17:  Outside a pred block, with a VPT code: syntax error.
19217                */
19218               inst.error = BAD_SYNTAX;
19219               return FAIL;
19220             }
19221           /* Case 18: Outside a pred block, with no code: OK!  */
19222           break;
19223
19224         case INSIDE_VPT_INSN:
19225           /* Case 8: Outside a pred block, with a VPT code: error: should be in
19226              a VPT block.  */
19227           inst.error = BAD_OUT_VPT;
19228           return FAIL;
19229
19230         case INSIDE_IT_INSN:
19231         case INSIDE_IT_LAST_INSN:
19232           if (inst.cond < COND_ALWAYS)
19233             {
19234               /* Case 16: Outside a pred block, with an IT code: error: should
19235                  be in an IT block.  */
19236               if (thumb_mode == 0)
19237                 {
19238                   if (unified_syntax
19239                       && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
19240                     as_tsktsk (_("Warning: conditional outside an IT block"\
19241                                  " for Thumb."));
19242                 }
19243               else
19244                 {
19245                   if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
19246                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
19247                     {
19248                       /* Automatically generate the IT instruction.  */
19249                       new_automatic_it_block (inst.cond);
19250                       if (inst.pred_insn_type == INSIDE_IT_LAST_INSN)
19251                         close_automatic_it_block ();
19252                     }
19253                   else
19254                     {
19255                       inst.error = BAD_OUT_IT;
19256                       return FAIL;
19257                     }
19258                 }
19259               break;
19260             }
19261           else if (inst.cond > COND_ALWAYS)
19262             {
19263               /* Case 17: Outside a pred block, with a VPT code: syntax error.
19264                */
19265               inst.error = BAD_SYNTAX;
19266               return FAIL;
19267             }
19268           else
19269             gas_assert (0);
19270         case IF_INSIDE_IT_LAST_INSN:
19271         case NEUTRAL_IT_INSN:
19272           break;
19273
19274         case VPT_INSN:
19275           if (inst.cond != COND_ALWAYS)
19276             first_error (BAD_SYNTAX);
19277           now_pred.state = MANUAL_PRED_BLOCK;
19278           now_pred.block_length = 0;
19279           now_pred.type = VECTOR_PRED;
19280           now_pred.cc = 0;
19281           break;
19282         case IT_INSN:
19283           now_pred.state = MANUAL_PRED_BLOCK;
19284           now_pred.block_length = 0;
19285           now_pred.type = SCALAR_PRED;
19286           break;
19287         }
19288       break;
19289
19290     case AUTOMATIC_PRED_BLOCK:
19291       /* Three things may happen now:
19292          a) We should increment current it block size;
19293          b) We should close current it block (closing insn or 4 insns);
19294          c) We should close current it block and start a new one (due
19295          to incompatible conditions or
19296          4 insns-length block reached).  */
19297
19298       switch (inst.pred_insn_type)
19299         {
19300         case INSIDE_VPT_INSN:
19301         case VPT_INSN:
19302         case MVE_OUTSIDE_PRED_INSN:
19303           gas_assert (0);
19304         case OUTSIDE_PRED_INSN:
19305           /* The closure of the block shall happen immediately,
19306              so any in_pred_block () call reports the block as closed.  */
19307           force_automatic_it_block_close ();
19308           break;
19309
19310         case INSIDE_IT_INSN:
19311         case INSIDE_IT_LAST_INSN:
19312         case IF_INSIDE_IT_LAST_INSN:
19313           now_pred.block_length++;
19314
19315           if (now_pred.block_length > 4
19316               || !now_pred_compatible (inst.cond))
19317             {
19318               force_automatic_it_block_close ();
19319               if (inst.pred_insn_type != IF_INSIDE_IT_LAST_INSN)
19320                 new_automatic_it_block (inst.cond);
19321             }
19322           else
19323             {
19324               now_pred.insn_cond = TRUE;
19325               now_pred_add_mask (inst.cond);
19326             }
19327
19328           if (now_pred.state == AUTOMATIC_PRED_BLOCK
19329               && (inst.pred_insn_type == INSIDE_IT_LAST_INSN
19330                   || inst.pred_insn_type == IF_INSIDE_IT_LAST_INSN))
19331             close_automatic_it_block ();
19332           break;
19333
19334         case NEUTRAL_IT_INSN:
19335           now_pred.block_length++;
19336           now_pred.insn_cond = TRUE;
19337
19338           if (now_pred.block_length > 4)
19339             force_automatic_it_block_close ();
19340           else
19341             now_pred_add_mask (now_pred.cc & 1);
19342           break;
19343
19344         case IT_INSN:
19345           close_automatic_it_block ();
19346           now_pred.state = MANUAL_PRED_BLOCK;
19347           break;
19348         }
19349       break;
19350
19351     case MANUAL_PRED_BLOCK:
19352       {
19353         int cond, is_last;
19354         if (now_pred.type == SCALAR_PRED)
19355           {
19356             /* Check conditional suffixes.  */
19357             cond = now_pred.cc ^ ((now_pred.mask >> 4) & 1) ^ 1;
19358             now_pred.mask <<= 1;
19359             now_pred.mask &= 0x1f;
19360             is_last = (now_pred.mask == 0x10);
19361           }
19362         else
19363           {
19364             now_pred.cc ^= (now_pred.mask >> 4);
19365             cond = now_pred.cc + 0xf;
19366             now_pred.mask <<= 1;
19367             now_pred.mask &= 0x1f;
19368             is_last = now_pred.mask == 0x10;
19369           }
19370         now_pred.insn_cond = TRUE;
19371
19372         switch (inst.pred_insn_type)
19373           {
19374           case OUTSIDE_PRED_INSN:
19375             if (now_pred.type == SCALAR_PRED)
19376               {
19377                 if (inst.cond == COND_ALWAYS)
19378                   {
19379                     /* Case 12: In an IT block, with no code: error: missing
19380                        code.  */
19381                     inst.error = BAD_NOT_IT;
19382                     return FAIL;
19383                   }
19384                 else if (inst.cond > COND_ALWAYS)
19385                   {
19386                     /* Case 11: In an IT block, with a VPT code: syntax error.
19387                      */
19388                     inst.error = BAD_SYNTAX;
19389                     return FAIL;
19390                   }
19391                 else if (thumb_mode)
19392                   {
19393                     /* This is for some special cases where a non-MVE
19394                        instruction is not allowed in an IT block, such as cbz,
19395                        but are put into one with a condition code.
19396                        You could argue this should be a syntax error, but we
19397                        gave the 'not allowed in IT block' diagnostic in the
19398                        past so we will keep doing so.  */
19399                     inst.error = BAD_NOT_IT;
19400                     return FAIL;
19401                   }
19402                 break;
19403               }
19404             else
19405               {
19406                 /* Case 15: In a VPT block, with no code: UNPREDICTABLE.  */
19407                 as_tsktsk (MVE_NOT_VPT);
19408                 return SUCCESS;
19409               }
19410           case MVE_OUTSIDE_PRED_INSN:
19411             if (now_pred.type == SCALAR_PRED)
19412               {
19413                 if (inst.cond == COND_ALWAYS)
19414                   {
19415                     /* Case 3: In an IT block, with no code: warning:
19416                        UNPREDICTABLE.  */
19417                     as_tsktsk (MVE_NOT_IT);
19418                     return SUCCESS;
19419                   }
19420                 else if (inst.cond < COND_ALWAYS)
19421                   {
19422                     /* Case 1: In an IT block, with an IT code: syntax error.
19423                      */
19424                     inst.error = BAD_SYNTAX;
19425                     return FAIL;
19426                   }
19427                 else
19428                   gas_assert (0);
19429               }
19430             else
19431               {
19432                 if (inst.cond < COND_ALWAYS)
19433                   {
19434                     /* Case 4: In a VPT block, with an IT code: syntax error.
19435                      */
19436                     inst.error = BAD_SYNTAX;
19437                     return FAIL;
19438                   }
19439                 else if (inst.cond == COND_ALWAYS)
19440                   {
19441                     /* Case 6: In a VPT block, with no code: error: missing
19442                        code.  */
19443                     inst.error = BAD_NOT_VPT;
19444                     return FAIL;
19445                   }
19446                 else
19447                   {
19448                     gas_assert (0);
19449                   }
19450               }
19451           case INSIDE_IT_INSN:
19452             if (inst.cond > COND_ALWAYS)
19453               {
19454                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
19455                 /* Case 14: In a VPT block, with a VPT code: syntax error.  */
19456                 inst.error = BAD_SYNTAX;
19457                 return FAIL;
19458               }
19459             else if (now_pred.type == SCALAR_PRED)
19460               {
19461                 /* Case 10: In an IT block, with an IT code: OK!  */
19462                 if (cond != inst.cond)
19463                   {
19464                     inst.error = now_pred.type == SCALAR_PRED ? BAD_IT_COND :
19465                       BAD_VPT_COND;
19466                     return FAIL;
19467                   }
19468               }
19469             else
19470               {
19471                 /* Case 13: In a VPT block, with an IT code: error: should be
19472                    in an IT block.  */
19473                 inst.error = BAD_OUT_IT;
19474                 return FAIL;
19475               }
19476             break;
19477
19478           case INSIDE_VPT_INSN:
19479             if (now_pred.type == SCALAR_PRED)
19480               {
19481                 /* Case 2: In an IT block, with a VPT code: error: must be in a
19482                    VPT block.  */
19483                 inst.error = BAD_OUT_VPT;
19484                 return FAIL;
19485               }
19486             /* Case 5:  In a VPT block, with a VPT code: OK!  */
19487             else if (cond != inst.cond)
19488               {
19489                 inst.error = BAD_VPT_COND;
19490                 return FAIL;
19491               }
19492             break;
19493           case INSIDE_IT_LAST_INSN:
19494           case IF_INSIDE_IT_LAST_INSN:
19495             if (now_pred.type == VECTOR_PRED || inst.cond > COND_ALWAYS)
19496               {
19497                 /* Case 4: In a VPT block, with an IT code: syntax error.  */
19498                 /* Case 11: In an IT block, with a VPT code: syntax error.  */
19499                 inst.error = BAD_SYNTAX;
19500                 return FAIL;
19501               }
19502             else if (cond != inst.cond)
19503               {
19504                 inst.error = BAD_IT_COND;
19505                 return FAIL;
19506               }
19507             if (!is_last)
19508               {
19509                 inst.error = BAD_BRANCH;
19510                 return FAIL;
19511               }
19512             break;
19513
19514           case NEUTRAL_IT_INSN:
19515             /* The BKPT instruction is unconditional even in a IT or VPT
19516                block.  */
19517             break;
19518
19519           case IT_INSN:
19520             if (now_pred.type == SCALAR_PRED)
19521               {
19522                 inst.error = BAD_IT_IT;
19523                 return FAIL;
19524               }
19525             /* fall through.  */
19526           case VPT_INSN:
19527             if (inst.cond == COND_ALWAYS)
19528               {
19529                 /* Executing a VPT/VPST instruction inside an IT block or a
19530                    VPT/VPST/IT instruction inside a VPT block is UNPREDICTABLE.
19531                  */
19532                 if (now_pred.type == SCALAR_PRED)
19533                   as_tsktsk (MVE_NOT_IT);
19534                 else
19535                   as_tsktsk (MVE_NOT_VPT);
19536                 return SUCCESS;
19537               }
19538             else
19539               {
19540                 /* VPT/VPST do not accept condition codes.  */
19541                 inst.error = BAD_SYNTAX;
19542                 return FAIL;
19543               }
19544           }
19545         }
19546       break;
19547     }
19548
19549   return SUCCESS;
19550 }
19551
19552 struct depr_insn_mask
19553 {
19554   unsigned long pattern;
19555   unsigned long mask;
19556   const char* description;
19557 };
19558
19559 /* List of 16-bit instruction patterns deprecated in an IT block in
19560    ARMv8.  */
19561 static const struct depr_insn_mask depr_it_insns[] = {
19562   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
19563   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
19564   { 0xa000, 0xb800, N_("ADR") },
19565   { 0x4800, 0xf800, N_("Literal loads") },
19566   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
19567   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
19568   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
19569      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
19570   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
19571   { 0, 0, NULL }
19572 };
19573
19574 static void
19575 it_fsm_post_encode (void)
19576 {
19577   int is_last;
19578
19579   if (!now_pred.state_handled)
19580     handle_pred_state ();
19581
19582   if (now_pred.insn_cond
19583       && !now_pred.warn_deprecated
19584       && warn_on_deprecated
19585       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
19586       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
19587     {
19588       if (inst.instruction >= 0x10000)
19589         {
19590           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
19591                      "performance deprecated in ARMv8-A and ARMv8-R"));
19592           now_pred.warn_deprecated = TRUE;
19593         }
19594       else
19595         {
19596           const struct depr_insn_mask *p = depr_it_insns;
19597
19598           while (p->mask != 0)
19599             {
19600               if ((inst.instruction & p->mask) == p->pattern)
19601                 {
19602                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
19603                                "instructions of the following class are "
19604                                "performance deprecated in ARMv8-A and "
19605                                "ARMv8-R: %s"), p->description);
19606                   now_pred.warn_deprecated = TRUE;
19607                   break;
19608                 }
19609
19610               ++p;
19611             }
19612         }
19613
19614       if (now_pred.block_length > 1)
19615         {
19616           as_tsktsk (_("IT blocks containing more than one conditional "
19617                      "instruction are performance deprecated in ARMv8-A and "
19618                      "ARMv8-R"));
19619           now_pred.warn_deprecated = TRUE;
19620         }
19621     }
19622
19623     is_last = (now_pred.mask == 0x10);
19624     if (is_last)
19625       {
19626         now_pred.state = OUTSIDE_PRED_BLOCK;
19627         now_pred.mask = 0;
19628       }
19629 }
19630
19631 static void
19632 force_automatic_it_block_close (void)
19633 {
19634   if (now_pred.state == AUTOMATIC_PRED_BLOCK)
19635     {
19636       close_automatic_it_block ();
19637       now_pred.state = OUTSIDE_PRED_BLOCK;
19638       now_pred.mask = 0;
19639     }
19640 }
19641
19642 static int
19643 in_pred_block (void)
19644 {
19645   if (!now_pred.state_handled)
19646     handle_pred_state ();
19647
19648   return now_pred.state != OUTSIDE_PRED_BLOCK;
19649 }
19650
19651 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
19652    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
19653    here, hence the "known" in the function name.  */
19654
19655 static bfd_boolean
19656 known_t32_only_insn (const struct asm_opcode *opcode)
19657 {
19658   /* Original Thumb-1 wide instruction.  */
19659   if (opcode->tencode == do_t_blx
19660       || opcode->tencode == do_t_branch23
19661       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
19662       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
19663     return TRUE;
19664
19665   /* Wide-only instruction added to ARMv8-M Baseline.  */
19666   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
19667       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
19668       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
19669       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
19670     return TRUE;
19671
19672   return FALSE;
19673 }
19674
19675 /* Whether wide instruction variant can be used if available for a valid OPCODE
19676    in ARCH.  */
19677
19678 static bfd_boolean
19679 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
19680 {
19681   if (known_t32_only_insn (opcode))
19682     return TRUE;
19683
19684   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
19685      of variant T3 of B.W is checked in do_t_branch.  */
19686   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
19687       && opcode->tencode == do_t_branch)
19688     return TRUE;
19689
19690   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
19691   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
19692       && opcode->tencode == do_t_mov_cmp
19693       /* Make sure CMP instruction is not affected.  */
19694       && opcode->aencode == do_mov)
19695     return TRUE;
19696
19697   /* Wide instruction variants of all instructions with narrow *and* wide
19698      variants become available with ARMv6t2.  Other opcodes are either
19699      narrow-only or wide-only and are thus available if OPCODE is valid.  */
19700   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
19701     return TRUE;
19702
19703   /* OPCODE with narrow only instruction variant or wide variant not
19704      available.  */
19705   return FALSE;
19706 }
19707
19708 void
19709 md_assemble (char *str)
19710 {
19711   char *p = str;
19712   const struct asm_opcode * opcode;
19713
19714   /* Align the previous label if needed.  */
19715   if (last_label_seen != NULL)
19716     {
19717       symbol_set_frag (last_label_seen, frag_now);
19718       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
19719       S_SET_SEGMENT (last_label_seen, now_seg);
19720     }
19721
19722   memset (&inst, '\0', sizeof (inst));
19723   int r;
19724   for (r = 0; r < ARM_IT_MAX_RELOCS; r++)
19725     inst.relocs[r].type = BFD_RELOC_UNUSED;
19726
19727   opcode = opcode_lookup (&p);
19728   if (!opcode)
19729     {
19730       /* It wasn't an instruction, but it might be a register alias of
19731          the form alias .req reg, or a Neon .dn/.qn directive.  */
19732       if (! create_register_alias (str, p)
19733           && ! create_neon_reg_alias (str, p))
19734         as_bad (_("bad instruction `%s'"), str);
19735
19736       return;
19737     }
19738
19739   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
19740     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
19741
19742   /* The value which unconditional instructions should have in place of the
19743      condition field.  */
19744   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
19745
19746   if (thumb_mode)
19747     {
19748       arm_feature_set variant;
19749
19750       variant = cpu_variant;
19751       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
19752       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
19753         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
19754       /* Check that this instruction is supported for this CPU.  */
19755       if (!opcode->tvariant
19756           || (thumb_mode == 1
19757               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
19758         {
19759           if (opcode->tencode == do_t_swi)
19760             as_bad (_("SVC is not permitted on this architecture"));
19761           else
19762             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
19763           return;
19764         }
19765       if (inst.cond != COND_ALWAYS && !unified_syntax
19766           && opcode->tencode != do_t_branch)
19767         {
19768           as_bad (_("Thumb does not support conditional execution"));
19769           return;
19770         }
19771
19772       /* Two things are addressed here:
19773          1) Implicit require narrow instructions on Thumb-1.
19774             This avoids relaxation accidentally introducing Thumb-2
19775             instructions.
19776          2) Reject wide instructions in non Thumb-2 cores.
19777
19778          Only instructions with narrow and wide variants need to be handled
19779          but selecting all non wide-only instructions is easier.  */
19780       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
19781           && !t32_insn_ok (variant, opcode))
19782         {
19783           if (inst.size_req == 0)
19784             inst.size_req = 2;
19785           else if (inst.size_req == 4)
19786             {
19787               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
19788                 as_bad (_("selected processor does not support 32bit wide "
19789                           "variant of instruction `%s'"), str);
19790               else
19791                 as_bad (_("selected processor does not support `%s' in "
19792                           "Thumb-2 mode"), str);
19793               return;
19794             }
19795         }
19796
19797       inst.instruction = opcode->tvalue;
19798
19799       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
19800         {
19801           /* Prepare the pred_insn_type for those encodings that don't set
19802              it.  */
19803           it_fsm_pre_encode ();
19804
19805           opcode->tencode ();
19806
19807           it_fsm_post_encode ();
19808         }
19809
19810       if (!(inst.error || inst.relax))
19811         {
19812           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
19813           inst.size = (inst.instruction > 0xffff ? 4 : 2);
19814           if (inst.size_req && inst.size_req != inst.size)
19815             {
19816               as_bad (_("cannot honor width suffix -- `%s'"), str);
19817               return;
19818             }
19819         }
19820
19821       /* Something has gone badly wrong if we try to relax a fixed size
19822          instruction.  */
19823       gas_assert (inst.size_req == 0 || !inst.relax);
19824
19825       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
19826                               *opcode->tvariant);
19827       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
19828          set those bits when Thumb-2 32-bit instructions are seen.  The impact
19829          of relaxable instructions will be considered later after we finish all
19830          relaxation.  */
19831       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
19832         variant = arm_arch_none;
19833       else
19834         variant = cpu_variant;
19835       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
19836         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
19837                                 arm_ext_v6t2);
19838
19839       check_neon_suffixes;
19840
19841       if (!inst.error)
19842         {
19843           mapping_state (MAP_THUMB);
19844         }
19845     }
19846   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
19847     {
19848       bfd_boolean is_bx;
19849
19850       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
19851       is_bx = (opcode->aencode == do_bx);
19852
19853       /* Check that this instruction is supported for this CPU.  */
19854       if (!(is_bx && fix_v4bx)
19855           && !(opcode->avariant &&
19856                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
19857         {
19858           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
19859           return;
19860         }
19861       if (inst.size_req)
19862         {
19863           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
19864           return;
19865         }
19866
19867       inst.instruction = opcode->avalue;
19868       if (opcode->tag == OT_unconditionalF)
19869         inst.instruction |= 0xFU << 28;
19870       else
19871         inst.instruction |= inst.cond << 28;
19872       inst.size = INSN_SIZE;
19873       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
19874         {
19875           it_fsm_pre_encode ();
19876           opcode->aencode ();
19877           it_fsm_post_encode ();
19878         }
19879       /* Arm mode bx is marked as both v4T and v5 because it's still required
19880          on a hypothetical non-thumb v5 core.  */
19881       if (is_bx)
19882         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
19883       else
19884         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
19885                                 *opcode->avariant);
19886
19887       check_neon_suffixes;
19888
19889       if (!inst.error)
19890         {
19891           mapping_state (MAP_ARM);
19892         }
19893     }
19894   else
19895     {
19896       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
19897                 "-- `%s'"), str);
19898       return;
19899     }
19900   output_inst (str);
19901 }
19902
19903 static void
19904 check_pred_blocks_finished (void)
19905 {
19906 #ifdef OBJ_ELF
19907   asection *sect;
19908
19909   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
19910     if (seg_info (sect)->tc_segment_info_data.current_pred.state
19911         == MANUAL_PRED_BLOCK)
19912       {
19913         if (now_pred.type == SCALAR_PRED)
19914           as_warn (_("section '%s' finished with an open IT block."),
19915                    sect->name);
19916         else
19917           as_warn (_("section '%s' finished with an open VPT/VPST block."),
19918                    sect->name);
19919       }
19920 #else
19921   if (now_pred.state == MANUAL_PRED_BLOCK)
19922     {
19923       if (now_pred.type == SCALAR_PRED)
19924        as_warn (_("file finished with an open IT block."));
19925       else
19926         as_warn (_("file finished with an open VPT/VPST block."));
19927     }
19928 #endif
19929 }
19930
19931 /* Various frobbings of labels and their addresses.  */
19932
19933 void
19934 arm_start_line_hook (void)
19935 {
19936   last_label_seen = NULL;
19937 }
19938
19939 void
19940 arm_frob_label (symbolS * sym)
19941 {
19942   last_label_seen = sym;
19943
19944   ARM_SET_THUMB (sym, thumb_mode);
19945
19946 #if defined OBJ_COFF || defined OBJ_ELF
19947   ARM_SET_INTERWORK (sym, support_interwork);
19948 #endif
19949
19950   force_automatic_it_block_close ();
19951
19952   /* Note - do not allow local symbols (.Lxxx) to be labelled
19953      as Thumb functions.  This is because these labels, whilst
19954      they exist inside Thumb code, are not the entry points for
19955      possible ARM->Thumb calls.  Also, these labels can be used
19956      as part of a computed goto or switch statement.  eg gcc
19957      can generate code that looks like this:
19958
19959                 ldr  r2, [pc, .Laaa]
19960                 lsl  r3, r3, #2
19961                 ldr  r2, [r3, r2]
19962                 mov  pc, r2
19963
19964        .Lbbb:  .word .Lxxx
19965        .Lccc:  .word .Lyyy
19966        ..etc...
19967        .Laaa:   .word Lbbb
19968
19969      The first instruction loads the address of the jump table.
19970      The second instruction converts a table index into a byte offset.
19971      The third instruction gets the jump address out of the table.
19972      The fourth instruction performs the jump.
19973
19974      If the address stored at .Laaa is that of a symbol which has the
19975      Thumb_Func bit set, then the linker will arrange for this address
19976      to have the bottom bit set, which in turn would mean that the
19977      address computation performed by the third instruction would end
19978      up with the bottom bit set.  Since the ARM is capable of unaligned
19979      word loads, the instruction would then load the incorrect address
19980      out of the jump table, and chaos would ensue.  */
19981   if (label_is_thumb_function_name
19982       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
19983       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
19984     {
19985       /* When the address of a Thumb function is taken the bottom
19986          bit of that address should be set.  This will allow
19987          interworking between Arm and Thumb functions to work
19988          correctly.  */
19989
19990       THUMB_SET_FUNC (sym, 1);
19991
19992       label_is_thumb_function_name = FALSE;
19993     }
19994
19995   dwarf2_emit_label (sym);
19996 }
19997
19998 bfd_boolean
19999 arm_data_in_code (void)
20000 {
20001   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
20002     {
20003       *input_line_pointer = '/';
20004       input_line_pointer += 5;
20005       *input_line_pointer = 0;
20006       return TRUE;
20007     }
20008
20009   return FALSE;
20010 }
20011
20012 char *
20013 arm_canonicalize_symbol_name (char * name)
20014 {
20015   int len;
20016
20017   if (thumb_mode && (len = strlen (name)) > 5
20018       && streq (name + len - 5, "/data"))
20019     *(name + len - 5) = 0;
20020
20021   return name;
20022 }
20023 \f
20024 /* Table of all register names defined by default.  The user can
20025    define additional names with .req.  Note that all register names
20026    should appear in both upper and lowercase variants.  Some registers
20027    also have mixed-case names.  */
20028
20029 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
20030 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
20031 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
20032 #define REGSET(p,t) \
20033   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
20034   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
20035   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
20036   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
20037 #define REGSETH(p,t) \
20038   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
20039   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
20040   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
20041   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
20042 #define REGSET2(p,t) \
20043   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
20044   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
20045   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
20046   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
20047 #define SPLRBANK(base,bank,t) \
20048   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
20049   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
20050   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
20051   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
20052   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
20053   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
20054
20055 static const struct reg_entry reg_names[] =
20056 {
20057   /* ARM integer registers.  */
20058   REGSET(r, RN), REGSET(R, RN),
20059
20060   /* ATPCS synonyms.  */
20061   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
20062   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
20063   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
20064
20065   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
20066   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
20067   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
20068
20069   /* Well-known aliases.  */
20070   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
20071   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
20072
20073   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
20074   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
20075
20076   /* Coprocessor numbers.  */
20077   REGSET(p, CP), REGSET(P, CP),
20078
20079   /* Coprocessor register numbers.  The "cr" variants are for backward
20080      compatibility.  */
20081   REGSET(c,  CN), REGSET(C, CN),
20082   REGSET(cr, CN), REGSET(CR, CN),
20083
20084   /* ARM banked registers.  */
20085   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
20086   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
20087   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
20088   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
20089   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
20090   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
20091   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
20092
20093   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
20094   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
20095   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
20096   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
20097   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
20098   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
20099   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
20100   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
20101
20102   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
20103   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
20104   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
20105   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
20106   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
20107   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
20108   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
20109   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
20110   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
20111
20112   /* FPA registers.  */
20113   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
20114   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
20115
20116   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
20117   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
20118
20119   /* VFP SP registers.  */
20120   REGSET(s,VFS),  REGSET(S,VFS),
20121   REGSETH(s,VFS), REGSETH(S,VFS),
20122
20123   /* VFP DP Registers.  */
20124   REGSET(d,VFD),  REGSET(D,VFD),
20125   /* Extra Neon DP registers.  */
20126   REGSETH(d,VFD), REGSETH(D,VFD),
20127
20128   /* Neon QP registers.  */
20129   REGSET2(q,NQ),  REGSET2(Q,NQ),
20130
20131   /* VFP control registers.  */
20132   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
20133   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
20134   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
20135   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
20136   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
20137   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
20138   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
20139
20140   /* Maverick DSP coprocessor registers.  */
20141   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
20142   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
20143
20144   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
20145   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
20146   REGDEF(dspsc,0,DSPSC),
20147
20148   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
20149   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
20150   REGDEF(DSPSC,0,DSPSC),
20151
20152   /* iWMMXt data registers - p0, c0-15.  */
20153   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
20154
20155   /* iWMMXt control registers - p1, c0-3.  */
20156   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
20157   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
20158   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
20159   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
20160
20161   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
20162   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
20163   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
20164   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
20165   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
20166
20167   /* XScale accumulator registers.  */
20168   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
20169 };
20170 #undef REGDEF
20171 #undef REGNUM
20172 #undef REGSET
20173
20174 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
20175    within psr_required_here.  */
20176 static const struct asm_psr psrs[] =
20177 {
20178   /* Backward compatibility notation.  Note that "all" is no longer
20179      truly all possible PSR bits.  */
20180   {"all",  PSR_c | PSR_f},
20181   {"flg",  PSR_f},
20182   {"ctl",  PSR_c},
20183
20184   /* Individual flags.  */
20185   {"f",    PSR_f},
20186   {"c",    PSR_c},
20187   {"x",    PSR_x},
20188   {"s",    PSR_s},
20189
20190   /* Combinations of flags.  */
20191   {"fs",   PSR_f | PSR_s},
20192   {"fx",   PSR_f | PSR_x},
20193   {"fc",   PSR_f | PSR_c},
20194   {"sf",   PSR_s | PSR_f},
20195   {"sx",   PSR_s | PSR_x},
20196   {"sc",   PSR_s | PSR_c},
20197   {"xf",   PSR_x | PSR_f},
20198   {"xs",   PSR_x | PSR_s},
20199   {"xc",   PSR_x | PSR_c},
20200   {"cf",   PSR_c | PSR_f},
20201   {"cs",   PSR_c | PSR_s},
20202   {"cx",   PSR_c | PSR_x},
20203   {"fsx",  PSR_f | PSR_s | PSR_x},
20204   {"fsc",  PSR_f | PSR_s | PSR_c},
20205   {"fxs",  PSR_f | PSR_x | PSR_s},
20206   {"fxc",  PSR_f | PSR_x | PSR_c},
20207   {"fcs",  PSR_f | PSR_c | PSR_s},
20208   {"fcx",  PSR_f | PSR_c | PSR_x},
20209   {"sfx",  PSR_s | PSR_f | PSR_x},
20210   {"sfc",  PSR_s | PSR_f | PSR_c},
20211   {"sxf",  PSR_s | PSR_x | PSR_f},
20212   {"sxc",  PSR_s | PSR_x | PSR_c},
20213   {"scf",  PSR_s | PSR_c | PSR_f},
20214   {"scx",  PSR_s | PSR_c | PSR_x},
20215   {"xfs",  PSR_x | PSR_f | PSR_s},
20216   {"xfc",  PSR_x | PSR_f | PSR_c},
20217   {"xsf",  PSR_x | PSR_s | PSR_f},
20218   {"xsc",  PSR_x | PSR_s | PSR_c},
20219   {"xcf",  PSR_x | PSR_c | PSR_f},
20220   {"xcs",  PSR_x | PSR_c | PSR_s},
20221   {"cfs",  PSR_c | PSR_f | PSR_s},
20222   {"cfx",  PSR_c | PSR_f | PSR_x},
20223   {"csf",  PSR_c | PSR_s | PSR_f},
20224   {"csx",  PSR_c | PSR_s | PSR_x},
20225   {"cxf",  PSR_c | PSR_x | PSR_f},
20226   {"cxs",  PSR_c | PSR_x | PSR_s},
20227   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
20228   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
20229   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
20230   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
20231   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
20232   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
20233   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
20234   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
20235   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
20236   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
20237   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
20238   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
20239   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
20240   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
20241   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
20242   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
20243   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
20244   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
20245   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
20246   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
20247   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
20248   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
20249   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
20250   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
20251 };
20252
20253 /* Table of V7M psr names.  */
20254 static const struct asm_psr v7m_psrs[] =
20255 {
20256   {"apsr",         0x0 }, {"APSR",         0x0 },
20257   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
20258   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
20259   {"psr",          0x3 }, {"PSR",          0x3 },
20260   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
20261   {"ipsr",         0x5 }, {"IPSR",         0x5 },
20262   {"epsr",         0x6 }, {"EPSR",         0x6 },
20263   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
20264   {"msp",          0x8 }, {"MSP",          0x8 },
20265   {"psp",          0x9 }, {"PSP",          0x9 },
20266   {"msplim",       0xa }, {"MSPLIM",       0xa },
20267   {"psplim",       0xb }, {"PSPLIM",       0xb },
20268   {"primask",      0x10}, {"PRIMASK",      0x10},
20269   {"basepri",      0x11}, {"BASEPRI",      0x11},
20270   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
20271   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
20272   {"control",      0x14}, {"CONTROL",      0x14},
20273   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
20274   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
20275   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
20276   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
20277   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
20278   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
20279   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
20280   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
20281   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
20282 };
20283
20284 /* Table of all shift-in-operand names.  */
20285 static const struct asm_shift_name shift_names [] =
20286 {
20287   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
20288   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
20289   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
20290   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
20291   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
20292   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
20293 };
20294
20295 /* Table of all explicit relocation names.  */
20296 #ifdef OBJ_ELF
20297 static struct reloc_entry reloc_names[] =
20298 {
20299   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
20300   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
20301   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
20302   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
20303   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
20304   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
20305   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
20306   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
20307   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
20308   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
20309   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
20310   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
20311   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
20312         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
20313   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
20314         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
20315   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
20316         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
20317   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
20318         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
20319   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
20320         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
20321   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
20322         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
20323    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
20324    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
20325    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
20326 };
20327 #endif
20328
20329 /* Table of all conditional affixes.  */
20330 static const struct asm_cond conds[] =
20331 {
20332   {"eq", 0x0},
20333   {"ne", 0x1},
20334   {"cs", 0x2}, {"hs", 0x2},
20335   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
20336   {"mi", 0x4},
20337   {"pl", 0x5},
20338   {"vs", 0x6},
20339   {"vc", 0x7},
20340   {"hi", 0x8},
20341   {"ls", 0x9},
20342   {"ge", 0xa},
20343   {"lt", 0xb},
20344   {"gt", 0xc},
20345   {"le", 0xd},
20346   {"al", 0xe}
20347 };
20348 static const struct asm_cond vconds[] =
20349 {
20350     {"t", 0xf},
20351     {"e", 0x10}
20352 };
20353
20354 #define UL_BARRIER(L,U,CODE,FEAT) \
20355   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
20356   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
20357
20358 static struct asm_barrier_opt barrier_opt_names[] =
20359 {
20360   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
20361   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
20362   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
20363   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
20364   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
20365   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
20366   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
20367   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
20368   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
20369   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
20370   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
20371   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
20372   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
20373   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
20374   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
20375   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
20376 };
20377
20378 #undef UL_BARRIER
20379
20380 /* Table of ARM-format instructions.    */
20381
20382 /* Macros for gluing together operand strings.  N.B. In all cases
20383    other than OPS0, the trailing OP_stop comes from default
20384    zero-initialization of the unspecified elements of the array.  */
20385 #define OPS0()            { OP_stop, }
20386 #define OPS1(a)           { OP_##a, }
20387 #define OPS2(a,b)         { OP_##a,OP_##b, }
20388 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
20389 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
20390 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
20391 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
20392
20393 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
20394    This is useful when mixing operands for ARM and THUMB, i.e. using the
20395    MIX_ARM_THUMB_OPERANDS macro.
20396    In order to use these macros, prefix the number of operands with _
20397    e.g. _3.  */
20398 #define OPS_1(a)           { a, }
20399 #define OPS_2(a,b)         { a,b, }
20400 #define OPS_3(a,b,c)       { a,b,c, }
20401 #define OPS_4(a,b,c,d)     { a,b,c,d, }
20402 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
20403 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
20404
20405 /* These macros abstract out the exact format of the mnemonic table and
20406    save some repeated characters.  */
20407
20408 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
20409 #define TxCE(mnem, op, top, nops, ops, ae, te) \
20410   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
20411     THUMB_VARIANT, do_##ae, do_##te, 0 }
20412
20413 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
20414    a T_MNEM_xyz enumerator.  */
20415 #define TCE(mnem, aop, top, nops, ops, ae, te) \
20416       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
20417 #define tCE(mnem, aop, top, nops, ops, ae, te) \
20418       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
20419
20420 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
20421    infix after the third character.  */
20422 #define TxC3(mnem, op, top, nops, ops, ae, te) \
20423   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
20424     THUMB_VARIANT, do_##ae, do_##te, 0 }
20425 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
20426   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
20427     THUMB_VARIANT, do_##ae, do_##te, 0 }
20428 #define TC3(mnem, aop, top, nops, ops, ae, te) \
20429       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
20430 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
20431       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
20432 #define tC3(mnem, aop, top, nops, ops, ae, te) \
20433       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
20434 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
20435       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
20436
20437 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
20438    field is still 0xE.  Many of the Thumb variants can be executed
20439    conditionally, so this is checked separately.  */
20440 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
20441   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
20442     THUMB_VARIANT, do_##ae, do_##te, 0 }
20443
20444 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
20445    Used by mnemonics that have very minimal differences in the encoding for
20446    ARM and Thumb variants and can be handled in a common function.  */
20447 #define TUEc(mnem, op, top, nops, ops, en) \
20448   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
20449     THUMB_VARIANT, do_##en, do_##en, 0 }
20450
20451 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
20452    condition code field.  */
20453 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
20454   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
20455     THUMB_VARIANT, do_##ae, do_##te, 0 }
20456
20457 /* ARM-only variants of all the above.  */
20458 #define CE(mnem,  op, nops, ops, ae)    \
20459   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20460
20461 #define C3(mnem, op, nops, ops, ae)     \
20462   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20463
20464 /* Thumb-only variants of TCE and TUE.  */
20465 #define ToC(mnem, top, nops, ops, te) \
20466   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
20467     do_##te, 0 }
20468
20469 #define ToU(mnem, top, nops, ops, te) \
20470   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
20471     NULL, do_##te, 0 }
20472
20473 /* T_MNEM_xyz enumerator variants of ToC.  */
20474 #define toC(mnem, top, nops, ops, te) \
20475   { mnem, OPS##nops ops, OT_csuffix, 0x0, T_MNEM##top, 0, THUMB_VARIANT, NULL, \
20476     do_##te, 0 }
20477
20478 /* T_MNEM_xyz enumerator variants of ToU.  */
20479 #define toU(mnem, top, nops, ops, te) \
20480   { mnem, OPS##nops ops, OT_unconditional, 0x0, T_MNEM##top, 0, THUMB_VARIANT, \
20481     NULL, do_##te, 0 }
20482
20483 /* Legacy mnemonics that always have conditional infix after the third
20484    character.  */
20485 #define CL(mnem, op, nops, ops, ae)     \
20486   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
20487     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20488
20489 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
20490 #define cCE(mnem,  op, nops, ops, ae)   \
20491   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
20492
20493 /* Legacy coprocessor instructions where conditional infix and conditional
20494    suffix are ambiguous.  For consistency this includes all FPA instructions,
20495    not just the potentially ambiguous ones.  */
20496 #define cCL(mnem, op, nops, ops, ae)    \
20497   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
20498     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
20499
20500 /* Coprocessor, takes either a suffix or a position-3 infix
20501    (for an FPA corner case). */
20502 #define C3E(mnem, op, nops, ops, ae) \
20503   { mnem, OPS##nops ops, OT_csuf_or_in3, \
20504     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae, 0 }
20505
20506 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
20507   { m1 #m2 m3, OPS##nops ops, \
20508     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
20509     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20510
20511 #define CM(m1, m2, op, nops, ops, ae)   \
20512   xCM_ (m1,   , m2, op, nops, ops, ae), \
20513   xCM_ (m1, eq, m2, op, nops, ops, ae), \
20514   xCM_ (m1, ne, m2, op, nops, ops, ae), \
20515   xCM_ (m1, cs, m2, op, nops, ops, ae), \
20516   xCM_ (m1, hs, m2, op, nops, ops, ae), \
20517   xCM_ (m1, cc, m2, op, nops, ops, ae), \
20518   xCM_ (m1, ul, m2, op, nops, ops, ae), \
20519   xCM_ (m1, lo, m2, op, nops, ops, ae), \
20520   xCM_ (m1, mi, m2, op, nops, ops, ae), \
20521   xCM_ (m1, pl, m2, op, nops, ops, ae), \
20522   xCM_ (m1, vs, m2, op, nops, ops, ae), \
20523   xCM_ (m1, vc, m2, op, nops, ops, ae), \
20524   xCM_ (m1, hi, m2, op, nops, ops, ae), \
20525   xCM_ (m1, ls, m2, op, nops, ops, ae), \
20526   xCM_ (m1, ge, m2, op, nops, ops, ae), \
20527   xCM_ (m1, lt, m2, op, nops, ops, ae), \
20528   xCM_ (m1, gt, m2, op, nops, ops, ae), \
20529   xCM_ (m1, le, m2, op, nops, ops, ae), \
20530   xCM_ (m1, al, m2, op, nops, ops, ae)
20531
20532 #define UE(mnem, op, nops, ops, ae)     \
20533   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20534
20535 #define UF(mnem, op, nops, ops, ae)     \
20536   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL, 0 }
20537
20538 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
20539    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
20540    use the same encoding function for each.  */
20541 #define NUF(mnem, op, nops, ops, enc)                                   \
20542   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
20543     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
20544
20545 /* Neon data processing, version which indirects through neon_enc_tab for
20546    the various overloaded versions of opcodes.  */
20547 #define nUF(mnem, op, nops, ops, enc)                                   \
20548   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
20549     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 0 }
20550
20551 /* Neon insn with conditional suffix for the ARM version, non-overloaded
20552    version.  */
20553 #define NCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
20554   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
20555     THUMB_VARIANT, do_##enc, do_##enc, mve_p }
20556
20557 #define NCE(mnem, op, nops, ops, enc)                                   \
20558    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
20559
20560 #define NCEF(mnem, op, nops, ops, enc)                                  \
20561     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
20562
20563 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
20564 #define nCE_tag(mnem, op, nops, ops, enc, tag, mve_p)                           \
20565   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
20566     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, mve_p }
20567
20568 #define nCE(mnem, op, nops, ops, enc)                                   \
20569    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 0)
20570
20571 #define nCEF(mnem, op, nops, ops, enc)                                  \
20572     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 0)
20573
20574 /*   */
20575 #define mCEF(mnem, op, nops, ops, enc)                          \
20576   { #mnem, OPS##nops ops, OT_csuffixF, 0, M_MNEM##op,           \
20577     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
20578
20579
20580 /* nCEF but for MVE predicated instructions.  */
20581 #define mnCEF(mnem, op, nops, ops, enc)                                 \
20582     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
20583
20584 /* nCE but for MVE predicated instructions.  */
20585 #define mnCE(mnem, op, nops, ops, enc)                                  \
20586    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
20587
20588 /* NUF but for potentially MVE predicated instructions.  */
20589 #define MNUF(mnem, op, nops, ops, enc)                                  \
20590   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
20591     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
20592
20593 /* nUF but for potentially MVE predicated instructions.  */
20594 #define mnUF(mnem, op, nops, ops, enc)                                  \
20595   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
20596     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc, 1 }
20597
20598 /* ToC but for potentially MVE predicated instructions.  */
20599 #define mToC(mnem, top, nops, ops, te) \
20600   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
20601     do_##te, 1 }
20602
20603 /* NCE but for MVE predicated instructions.  */
20604 #define MNCE(mnem, op, nops, ops, enc)                                  \
20605    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix, 1)
20606
20607 /* NCEF but for MVE predicated instructions.  */
20608 #define MNCEF(mnem, op, nops, ops, enc)                                 \
20609     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF, 1)
20610 #define do_0 0
20611
20612 static const struct asm_opcode insns[] =
20613 {
20614 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
20615 #define THUMB_VARIANT  & arm_ext_v4t
20616  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
20617  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
20618  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
20619  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
20620  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
20621  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
20622  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
20623  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
20624  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
20625  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
20626  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
20627  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
20628  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
20629  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
20630  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
20631  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
20632
20633  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
20634     for setting PSR flag bits.  They are obsolete in V6 and do not
20635     have Thumb equivalents. */
20636  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
20637  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
20638   CL("tstp",    110f000,           2, (RR, SH),      cmp),
20639  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
20640  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
20641   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
20642  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
20643  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
20644   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
20645
20646  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
20647  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
20648  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
20649  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
20650
20651  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
20652  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
20653  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
20654                                                                 OP_RRnpc),
20655                                         OP_ADDRGLDR),ldst, t_ldst),
20656  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
20657
20658  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20659  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20660  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20661  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20662  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20663  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
20664
20665  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
20666  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
20667
20668   /* Pseudo ops.  */
20669  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
20670   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
20671  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
20672  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
20673
20674   /* Thumb-compatibility pseudo ops.  */
20675  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
20676  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
20677  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
20678  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
20679  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
20680  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
20681  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
20682  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
20683  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
20684  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
20685  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
20686  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
20687
20688  /* These may simplify to neg.  */
20689  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
20690  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
20691
20692 #undef THUMB_VARIANT
20693 #define THUMB_VARIANT  & arm_ext_os
20694
20695  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
20696  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
20697
20698 #undef  THUMB_VARIANT
20699 #define THUMB_VARIANT  & arm_ext_v6
20700
20701  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
20702
20703  /* V1 instructions with no Thumb analogue prior to V6T2.  */
20704 #undef  THUMB_VARIANT
20705 #define THUMB_VARIANT  & arm_ext_v6t2
20706
20707  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
20708  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
20709   CL("teqp",    130f000,           2, (RR, SH),      cmp),
20710
20711  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
20712  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
20713  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
20714  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
20715
20716  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
20717  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
20718
20719  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
20720  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
20721
20722  /* V1 instructions with no Thumb analogue at all.  */
20723   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
20724   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
20725
20726   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
20727   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
20728   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
20729   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
20730   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
20731   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
20732   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
20733   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
20734
20735 #undef  ARM_VARIANT
20736 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
20737 #undef  THUMB_VARIANT
20738 #define THUMB_VARIANT  & arm_ext_v4t
20739
20740  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
20741  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
20742
20743 #undef  THUMB_VARIANT
20744 #define THUMB_VARIANT  & arm_ext_v6t2
20745
20746  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
20747   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
20748
20749   /* Generic coprocessor instructions.  */
20750  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
20751  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20752  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20753  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20754  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20755  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
20756  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
20757
20758 #undef  ARM_VARIANT
20759 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
20760
20761   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
20762   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
20763
20764 #undef  ARM_VARIANT
20765 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
20766 #undef  THUMB_VARIANT
20767 #define THUMB_VARIANT  & arm_ext_msr
20768
20769  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
20770  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
20771
20772 #undef  ARM_VARIANT
20773 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
20774 #undef  THUMB_VARIANT
20775 #define THUMB_VARIANT  & arm_ext_v6t2
20776
20777  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
20778   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
20779  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
20780   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
20781  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
20782   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
20783  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
20784   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
20785
20786 #undef  ARM_VARIANT
20787 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
20788 #undef  THUMB_VARIANT
20789 #define THUMB_VARIANT  & arm_ext_v4t
20790
20791  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20792  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20793  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20794  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20795  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20796  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
20797
20798 #undef  ARM_VARIANT
20799 #define ARM_VARIANT  & arm_ext_v4t_5
20800
20801   /* ARM Architecture 4T.  */
20802   /* Note: bx (and blx) are required on V5, even if the processor does
20803      not support Thumb.  */
20804  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
20805
20806 #undef  ARM_VARIANT
20807 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
20808 #undef  THUMB_VARIANT
20809 #define THUMB_VARIANT  & arm_ext_v5t
20810
20811   /* Note: blx has 2 variants; the .value coded here is for
20812      BLX(2).  Only this variant has conditional execution.  */
20813  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
20814  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
20815
20816 #undef  THUMB_VARIANT
20817 #define THUMB_VARIANT  & arm_ext_v6t2
20818
20819  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
20820  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20821  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
20822  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
20823  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
20824  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
20825  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
20826  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
20827
20828 #undef  ARM_VARIANT
20829 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
20830 #undef  THUMB_VARIANT
20831 #define THUMB_VARIANT  & arm_ext_v5exp
20832
20833  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20834  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20835  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20836  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20837
20838  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20839  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
20840
20841  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
20842  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
20843  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
20844  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
20845
20846  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20847  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20848  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20849  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20850
20851  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20852  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
20853
20854  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
20855  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
20856  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
20857  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
20858
20859 #undef  ARM_VARIANT
20860 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
20861 #undef  THUMB_VARIANT
20862 #define THUMB_VARIANT  & arm_ext_v6t2
20863
20864  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
20865  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
20866      ldrd, t_ldstd),
20867  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
20868                                        ADDRGLDRS), ldrd, t_ldstd),
20869
20870  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
20871  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
20872
20873 #undef  ARM_VARIANT
20874 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
20875
20876  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
20877
20878 #undef  ARM_VARIANT
20879 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
20880 #undef  THUMB_VARIANT
20881 #define THUMB_VARIANT  & arm_ext_v6
20882
20883  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
20884  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
20885  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
20886  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
20887  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
20888  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
20889  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
20890  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
20891  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
20892  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
20893
20894 #undef  THUMB_VARIANT
20895 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
20896
20897  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
20898  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
20899                                       strex,  t_strex),
20900 #undef  THUMB_VARIANT
20901 #define THUMB_VARIANT  & arm_ext_v6t2
20902
20903  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
20904  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
20905
20906  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
20907  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
20908
20909 /*  ARM V6 not included in V7M.  */
20910 #undef  THUMB_VARIANT
20911 #define THUMB_VARIANT  & arm_ext_v6_notm
20912  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
20913  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
20914   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
20915   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
20916  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
20917  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
20918   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
20919  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
20920   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
20921  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
20922  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
20923  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
20924   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
20925   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
20926   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
20927   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
20928  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
20929  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
20930  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
20931
20932 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
20933 #undef  THUMB_VARIANT
20934 #define THUMB_VARIANT  & arm_ext_v6_dsp
20935  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
20936  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
20937  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20938  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20939  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20940  /* Old name for QASX.  */
20941  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20942  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20943  /* Old name for QSAX.  */
20944  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20945  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20946  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20947  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20948  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20949  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20950  /* Old name for SASX.  */
20951  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20952  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20953  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20954  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20955  /* Old name for SHASX.  */
20956  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20957  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20958  /* Old name for SHSAX.  */
20959  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20960  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20961  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20962  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20963  /* Old name for SSAX.  */
20964  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20965  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20966  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20967  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20968  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20969  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20970  /* Old name for UASX.  */
20971  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20972  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20973  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20974  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20975  /* Old name for UHASX.  */
20976  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20977  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20978  /* Old name for UHSAX.  */
20979  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20980  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20981  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20982  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20983  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20984  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20985  /* Old name for UQASX.  */
20986  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20987  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20988  /* Old name for UQSAX.  */
20989  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
20990  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20991  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20992  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20993  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20994  /* Old name for USAX.  */
20995  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20996  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
20997  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
20998  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
20999  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
21000  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
21001  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
21002  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
21003  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
21004  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
21005  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
21006  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21007  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21008  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
21009  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
21010  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21011  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21012  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
21013  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
21014  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21015  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21016  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21017  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
21018  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21019  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21020  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21021  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21022  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21023  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
21024  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
21025  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
21026  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
21027  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
21028  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
21029
21030 #undef  ARM_VARIANT
21031 #define ARM_VARIANT   & arm_ext_v6k_v6t2
21032 #undef  THUMB_VARIANT
21033 #define THUMB_VARIANT & arm_ext_v6k_v6t2
21034
21035  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
21036  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
21037  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
21038  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
21039
21040 #undef  THUMB_VARIANT
21041 #define THUMB_VARIANT  & arm_ext_v6_notm
21042  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
21043                                       ldrexd, t_ldrexd),
21044  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
21045                                        RRnpcb), strexd, t_strexd),
21046
21047 #undef  THUMB_VARIANT
21048 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
21049  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
21050      rd_rn,  rd_rn),
21051  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
21052      rd_rn,  rd_rn),
21053  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
21054      strex, t_strexbh),
21055  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
21056      strex, t_strexbh),
21057  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
21058
21059 #undef  ARM_VARIANT
21060 #define ARM_VARIANT    & arm_ext_sec
21061 #undef  THUMB_VARIANT
21062 #define THUMB_VARIANT  & arm_ext_sec
21063
21064  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
21065
21066 #undef  ARM_VARIANT
21067 #define ARM_VARIANT    & arm_ext_virt
21068 #undef  THUMB_VARIANT
21069 #define THUMB_VARIANT    & arm_ext_virt
21070
21071  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
21072  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
21073
21074 #undef  ARM_VARIANT
21075 #define ARM_VARIANT    & arm_ext_pan
21076 #undef  THUMB_VARIANT
21077 #define THUMB_VARIANT  & arm_ext_pan
21078
21079  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
21080
21081 #undef  ARM_VARIANT
21082 #define ARM_VARIANT    & arm_ext_v6t2
21083 #undef  THUMB_VARIANT
21084 #define THUMB_VARIANT  & arm_ext_v6t2
21085
21086  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
21087  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
21088  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
21089  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
21090
21091  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
21092  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
21093
21094  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
21095  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
21096  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
21097  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
21098
21099 #undef  ARM_VARIANT
21100 #define ARM_VARIANT    & arm_ext_v3
21101 #undef  THUMB_VARIANT
21102 #define THUMB_VARIANT  & arm_ext_v6t2
21103
21104  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
21105  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
21106  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
21107
21108 #undef  ARM_VARIANT
21109 #define ARM_VARIANT    & arm_ext_v6t2
21110 #undef  THUMB_VARIANT
21111 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
21112  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
21113  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
21114
21115  /* Thumb-only instructions.  */
21116 #undef  ARM_VARIANT
21117 #define ARM_VARIANT NULL
21118   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
21119   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
21120
21121  /* ARM does not really have an IT instruction, so always allow it.
21122     The opcode is copied from Thumb in order to allow warnings in
21123     -mimplicit-it=[never | arm] modes.  */
21124 #undef  ARM_VARIANT
21125 #define ARM_VARIANT  & arm_ext_v1
21126 #undef  THUMB_VARIANT
21127 #define THUMB_VARIANT  & arm_ext_v6t2
21128
21129  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
21130  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
21131  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
21132  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
21133  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
21134  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
21135  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
21136  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
21137  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
21138  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
21139  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
21140  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
21141  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
21142  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
21143  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
21144  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
21145  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
21146  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
21147
21148  /* Thumb2 only instructions.  */
21149 #undef  ARM_VARIANT
21150 #define ARM_VARIANT  NULL
21151
21152  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
21153  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
21154  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
21155  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
21156  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
21157  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
21158
21159  /* Hardware division instructions.  */
21160 #undef  ARM_VARIANT
21161 #define ARM_VARIANT    & arm_ext_adiv
21162 #undef  THUMB_VARIANT
21163 #define THUMB_VARIANT  & arm_ext_div
21164
21165  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
21166  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
21167
21168  /* ARM V6M/V7 instructions.  */
21169 #undef  ARM_VARIANT
21170 #define ARM_VARIANT    & arm_ext_barrier
21171 #undef  THUMB_VARIANT
21172 #define THUMB_VARIANT  & arm_ext_barrier
21173
21174  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
21175  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
21176  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
21177
21178  /* ARM V7 instructions.  */
21179 #undef  ARM_VARIANT
21180 #define ARM_VARIANT    & arm_ext_v7
21181 #undef  THUMB_VARIANT
21182 #define THUMB_VARIANT  & arm_ext_v7
21183
21184  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
21185  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
21186
21187 #undef  ARM_VARIANT
21188 #define ARM_VARIANT    & arm_ext_mp
21189 #undef  THUMB_VARIANT
21190 #define THUMB_VARIANT  & arm_ext_mp
21191
21192  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
21193
21194  /* AArchv8 instructions.  */
21195 #undef  ARM_VARIANT
21196 #define ARM_VARIANT   & arm_ext_v8
21197
21198 /* Instructions shared between armv8-a and armv8-m.  */
21199 #undef  THUMB_VARIANT
21200 #define THUMB_VARIANT & arm_ext_atomics
21201
21202  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
21203  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
21204  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
21205  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
21206  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
21207  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
21208  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
21209  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
21210  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
21211  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
21212                                                         stlex,  t_stlex),
21213  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
21214                                                         stlex, t_stlex),
21215  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
21216                                                         stlex, t_stlex),
21217 #undef  THUMB_VARIANT
21218 #define THUMB_VARIANT & arm_ext_v8
21219
21220  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
21221  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
21222                                                         ldrexd, t_ldrexd),
21223  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
21224                                                         strexd, t_strexd),
21225
21226 /* Defined in V8 but is in undefined encoding space for earlier
21227    architectures.  However earlier architectures are required to treat
21228    this instuction as a semihosting trap as well.  Hence while not explicitly
21229    defined as such, it is in fact correct to define the instruction for all
21230    architectures.  */
21231 #undef  THUMB_VARIANT
21232 #define THUMB_VARIANT  & arm_ext_v1
21233 #undef  ARM_VARIANT
21234 #define ARM_VARIANT  & arm_ext_v1
21235  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
21236
21237  /* ARMv8 T32 only.  */
21238 #undef  ARM_VARIANT
21239 #define ARM_VARIANT  NULL
21240  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
21241  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
21242  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
21243
21244   /* FP for ARMv8.  */
21245 #undef  ARM_VARIANT
21246 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
21247 #undef  THUMB_VARIANT
21248 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
21249
21250   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
21251   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
21252   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
21253   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
21254   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
21255   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
21256   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
21257   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
21258   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
21259   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
21260   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
21261   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
21262   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
21263   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
21264   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
21265   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
21266   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
21267
21268   /* Crypto v1 extensions.  */
21269 #undef  ARM_VARIANT
21270 #define ARM_VARIANT & fpu_crypto_ext_armv8
21271 #undef  THUMB_VARIANT
21272 #define THUMB_VARIANT & fpu_crypto_ext_armv8
21273
21274   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
21275   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
21276   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
21277   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
21278   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
21279   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
21280   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
21281   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
21282   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
21283   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
21284   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
21285   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
21286   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
21287   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
21288
21289 #undef  ARM_VARIANT
21290 #define ARM_VARIANT   & crc_ext_armv8
21291 #undef  THUMB_VARIANT
21292 #define THUMB_VARIANT & crc_ext_armv8
21293   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
21294   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
21295   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
21296   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
21297   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
21298   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
21299
21300  /* ARMv8.2 RAS extension.  */
21301 #undef  ARM_VARIANT
21302 #define ARM_VARIANT   & arm_ext_ras
21303 #undef  THUMB_VARIANT
21304 #define THUMB_VARIANT & arm_ext_ras
21305  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
21306
21307 #undef  ARM_VARIANT
21308 #define ARM_VARIANT   & arm_ext_v8_3
21309 #undef  THUMB_VARIANT
21310 #define THUMB_VARIANT & arm_ext_v8_3
21311  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
21312  NUF (vcmla, 0, 4, (RNDQ, RNDQ, RNDQ_RNSC, EXPi), vcmla),
21313  NUF (vcadd, 0, 4, (RNDQ, RNDQ, RNDQ, EXPi), vcadd),
21314
21315 #undef  ARM_VARIANT
21316 #define ARM_VARIANT   & fpu_neon_ext_dotprod
21317 #undef  THUMB_VARIANT
21318 #define THUMB_VARIANT & fpu_neon_ext_dotprod
21319  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
21320  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
21321
21322 #undef  ARM_VARIANT
21323 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
21324 #undef  THUMB_VARIANT
21325 #define THUMB_VARIANT NULL
21326
21327  cCE("wfs",     e200110, 1, (RR),            rd),
21328  cCE("rfs",     e300110, 1, (RR),            rd),
21329  cCE("wfc",     e400110, 1, (RR),            rd),
21330  cCE("rfc",     e500110, 1, (RR),            rd),
21331
21332  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21333  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21334  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21335  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21336
21337  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21338  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21339  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21340  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
21341
21342  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
21343  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
21344  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
21345  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
21346  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
21347  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
21348  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
21349  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
21350  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
21351  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
21352  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
21353  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
21354
21355  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
21356  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
21357  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
21358  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
21359  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
21360  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
21361  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
21362  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
21363  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
21364  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
21365  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
21366  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
21367
21368  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
21369  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
21370  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
21371  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
21372  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
21373  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
21374  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
21375  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
21376  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
21377  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
21378  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
21379  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
21380
21381  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
21382  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
21383  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
21384  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
21385  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
21386  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
21387  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
21388  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
21389  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
21390  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
21391  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
21392  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
21393
21394  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
21395  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
21396  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
21397  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
21398  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
21399  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
21400  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
21401  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
21402  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
21403  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
21404  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
21405  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
21406
21407  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
21408  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
21409  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
21410  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
21411  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
21412  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
21413  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
21414  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
21415  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
21416  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
21417  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
21418  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
21419
21420  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
21421  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
21422  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
21423  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
21424  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
21425  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
21426  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
21427  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
21428  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
21429  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
21430  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
21431  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
21432
21433  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
21434  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
21435  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
21436  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
21437  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
21438  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
21439  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
21440  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
21441  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
21442  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
21443  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
21444  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
21445
21446  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
21447  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
21448  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
21449  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
21450  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
21451  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
21452  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
21453  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
21454  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
21455  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
21456  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
21457  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
21458
21459  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
21460  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
21461  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
21462  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
21463  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
21464  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
21465  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
21466  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
21467  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
21468  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
21469  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
21470  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
21471
21472  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
21473  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
21474  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
21475  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
21476  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
21477  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
21478  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
21479  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
21480  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
21481  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
21482  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
21483  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
21484
21485  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
21486  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
21487  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
21488  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
21489  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
21490  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
21491  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
21492  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
21493  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
21494  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
21495  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
21496  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
21497
21498  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
21499  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
21500  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
21501  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
21502  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
21503  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
21504  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
21505  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
21506  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
21507  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
21508  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
21509  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
21510
21511  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
21512  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
21513  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
21514  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
21515  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
21516  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
21517  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
21518  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
21519  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
21520  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
21521  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
21522  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
21523
21524  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
21525  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
21526  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
21527  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
21528  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
21529  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
21530  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
21531  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
21532  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
21533  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
21534  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
21535  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
21536
21537  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
21538  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
21539  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
21540  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
21541  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
21542  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
21543  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
21544  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
21545  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
21546  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
21547  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
21548  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
21549
21550  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
21551  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
21552  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
21553  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
21554  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
21555  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21556  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21557  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21558  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
21559  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
21560  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
21561  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
21562
21563  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
21564  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
21565  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
21566  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
21567  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
21568  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21569  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21570  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21571  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
21572  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
21573  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
21574  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
21575
21576  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
21577  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
21578  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
21579  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
21580  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
21581  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21582  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21583  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21584  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
21585  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
21586  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
21587  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
21588
21589  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
21590  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
21591  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
21592  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
21593  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
21594  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21595  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21596  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21597  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
21598  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
21599  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
21600  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
21601
21602  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
21603  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
21604  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
21605  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
21606  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
21607  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21608  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21609  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21610  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
21611  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
21612  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
21613  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
21614
21615  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
21616  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
21617  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
21618  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
21619  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
21620  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21621  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21622  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21623  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
21624  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
21625  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
21626  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
21627
21628  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
21629  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
21630  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
21631  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
21632  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
21633  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21634  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21635  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21636  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
21637  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
21638  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
21639  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
21640
21641  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
21642  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
21643  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
21644  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
21645  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
21646  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21647  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21648  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21649  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
21650  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
21651  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
21652  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
21653
21654  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
21655  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
21656  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
21657  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
21658  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
21659  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21660  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21661  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21662  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
21663  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
21664  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
21665  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
21666
21667  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
21668  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
21669  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
21670  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
21671  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
21672  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21673  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21674  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21675  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
21676  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
21677  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
21678  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
21679
21680  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
21681  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
21682  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
21683  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
21684  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
21685  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21686  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21687  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21688  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
21689  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
21690  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
21691  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
21692
21693  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
21694  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
21695  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
21696  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
21697  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
21698  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21699  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21700  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21701  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
21702  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
21703  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
21704  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
21705
21706  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
21707  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
21708  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
21709  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
21710  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
21711  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
21712  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
21713  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
21714  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
21715  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
21716  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
21717  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
21718
21719  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
21720  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
21721  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
21722  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
21723
21724  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
21725  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
21726  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
21727  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
21728  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
21729  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
21730  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
21731  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
21732  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
21733  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
21734  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
21735  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
21736
21737   /* The implementation of the FIX instruction is broken on some
21738      assemblers, in that it accepts a precision specifier as well as a
21739      rounding specifier, despite the fact that this is meaningless.
21740      To be more compatible, we accept it as well, though of course it
21741      does not set any bits.  */
21742  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
21743  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
21744  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
21745  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
21746  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
21747  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
21748  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
21749  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
21750  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
21751  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
21752  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
21753  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
21754  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
21755
21756   /* Instructions that were new with the real FPA, call them V2.  */
21757 #undef  ARM_VARIANT
21758 #define ARM_VARIANT  & fpu_fpa_ext_v2
21759
21760  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21761  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21762  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21763  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21764  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21765  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
21766
21767 #undef  ARM_VARIANT
21768 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
21769
21770   /* Moves and type conversions.  */
21771  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
21772  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
21773  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
21774  cCE("fmstat",  ef1fa10, 0, (),               noargs),
21775  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
21776  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
21777  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21778  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
21779  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
21780  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21781  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
21782  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21783  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
21784  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
21785
21786   /* Memory operations.  */
21787  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
21788  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
21789  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
21790  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
21791  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
21792  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
21793  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
21794  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
21795  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
21796  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
21797  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
21798  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
21799  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
21800  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
21801  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
21802  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
21803  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
21804  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
21805
21806   /* Monadic operations.  */
21807  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21808  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
21809  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21810
21811   /* Dyadic operations.  */
21812  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21813  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21814  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21815  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21816  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21817  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21818  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21819  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21820  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21821
21822   /* Comparisons.  */
21823  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
21824  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
21825  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
21826  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
21827
21828  /* Double precision load/store are still present on single precision
21829     implementations.  */
21830  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
21831  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
21832  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
21833  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
21834  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
21835  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
21836  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
21837  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
21838  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
21839  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
21840
21841 #undef  ARM_VARIANT
21842 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
21843
21844   /* Moves and type conversions.  */
21845  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
21846  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
21847  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
21848  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
21849  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
21850  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
21851  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
21852  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
21853  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
21854  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
21855  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
21856  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
21857  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
21858
21859   /* Monadic operations.  */
21860  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
21861  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
21862  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
21863
21864   /* Dyadic operations.  */
21865  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21866  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21867  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21868  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21869  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21870  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21871  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21872  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21873  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21874
21875   /* Comparisons.  */
21876  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
21877  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
21878  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
21879  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
21880
21881 #undef  ARM_VARIANT
21882 #define ARM_VARIANT  & fpu_vfp_ext_v2
21883
21884  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
21885  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
21886  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
21887  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
21888
21889 /* Instructions which may belong to either the Neon or VFP instruction sets.
21890    Individual encoder functions perform additional architecture checks.  */
21891 #undef  ARM_VARIANT
21892 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
21893 #undef  THUMB_VARIANT
21894 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
21895
21896   /* These mnemonics are unique to VFP.  */
21897  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
21898  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
21899  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21900  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21901  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21902  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
21903  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
21904  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
21905  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
21906  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
21907
21908   /* Mnemonics shared by Neon and VFP.  */
21909  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
21910  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
21911  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
21912
21913  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21914  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21915  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21916  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21917  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21918  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
21919
21920  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
21921  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
21922  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
21923  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
21924
21925
21926   /* NOTE: All VMOV encoding is special-cased!  */
21927  NCE(vmov,      0,       1, (VMOV), neon_mov),
21928  NCE(vmovq,     0,       1, (VMOV), neon_mov),
21929
21930 #undef  THUMB_VARIANT
21931 /* Could be either VLDR/VSTR or VLDR/VSTR (system register) which are guarded
21932    by different feature bits.  Since we are setting the Thumb guard, we can
21933    require Thumb-1 which makes it a nop guard and set the right feature bit in
21934    do_vldr_vstr ().  */
21935 #define THUMB_VARIANT  & arm_ext_v4t
21936  NCE(vldr,      d100b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
21937  NCE(vstr,      d000b00, 2, (VLDR, ADDRGLDC), vldr_vstr),
21938
21939 #undef  ARM_VARIANT
21940 #define ARM_VARIANT    & arm_ext_fp16
21941 #undef  THUMB_VARIANT
21942 #define THUMB_VARIANT  & arm_ext_fp16
21943  /* New instructions added from v8.2, allowing the extraction and insertion of
21944     the upper 16 bits of a 32-bit vector register.  */
21945  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
21946  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
21947
21948  /* New backported fma/fms instructions optional in v8.2.  */
21949  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
21950  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
21951
21952 #undef  THUMB_VARIANT
21953 #define THUMB_VARIANT  & fpu_neon_ext_v1
21954 #undef  ARM_VARIANT
21955 #define ARM_VARIANT    & fpu_neon_ext_v1
21956
21957   /* Data processing with three registers of the same length.  */
21958   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
21959  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
21960  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
21961  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
21962  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
21963  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
21964  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
21965  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
21966  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
21967   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
21968  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
21969  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
21970  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
21971  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
21972  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
21973  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
21974  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
21975  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
21976   /* If not immediate, fall back to neon_dyadic_i64_su.
21977      shl_imm should accept I8 I16 I32 I64,
21978      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
21979  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
21980  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
21981  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
21982  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
21983   /* Logic ops, types optional & ignored.  */
21984  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
21985  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
21986  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
21987  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
21988  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
21989  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
21990  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
21991  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
21992  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
21993  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
21994   /* Bitfield ops, untyped.  */
21995  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
21996  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
21997  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
21998  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
21999  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
22000  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
22001   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
22002  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
22003  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
22004  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
22005  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
22006  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
22007   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
22008      back to neon_dyadic_if_su.  */
22009  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
22010  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
22011  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
22012  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
22013  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
22014  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
22015  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
22016  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
22017   /* Comparison. Type I8 I16 I32 F32.  */
22018  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
22019  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
22020   /* As above, D registers only.  */
22021  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
22022  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
22023   /* Int and float variants, signedness unimportant.  */
22024  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
22025  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
22026  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
22027   /* Add/sub take types I8 I16 I32 I64 F32.  */
22028  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
22029  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
22030   /* vtst takes sizes 8, 16, 32.  */
22031  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
22032  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
22033   /* VMUL takes I8 I16 I32 F32 P8.  */
22034  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
22035   /* VQD{R}MULH takes S16 S32.  */
22036  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
22037  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
22038  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
22039  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
22040  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
22041  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
22042  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
22043  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
22044  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
22045  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
22046  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
22047  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
22048  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
22049  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
22050  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
22051  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
22052  /* ARM v8.1 extension.  */
22053  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
22054  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
22055  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
22056  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
22057
22058   /* Two address, int/float. Types S8 S16 S32 F32.  */
22059  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
22060  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
22061
22062   /* Data processing with two registers and a shift amount.  */
22063   /* Right shifts, and variants with rounding.
22064      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
22065  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
22066  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
22067  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
22068  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
22069  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
22070  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
22071  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
22072  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
22073   /* Shift and insert. Sizes accepted 8 16 32 64.  */
22074  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
22075  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
22076  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
22077  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
22078   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
22079  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
22080  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
22081   /* Right shift immediate, saturating & narrowing, with rounding variants.
22082      Types accepted S16 S32 S64 U16 U32 U64.  */
22083  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
22084  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
22085   /* As above, unsigned. Types accepted S16 S32 S64.  */
22086  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
22087  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
22088   /* Right shift narrowing. Types accepted I16 I32 I64.  */
22089  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
22090  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
22091   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
22092  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
22093   /* CVT with optional immediate for fixed-point variant.  */
22094  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
22095
22096  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
22097  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
22098
22099   /* Data processing, three registers of different lengths.  */
22100   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
22101  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
22102   /* If not scalar, fall back to neon_dyadic_long.
22103      Vector types as above, scalar types S16 S32 U16 U32.  */
22104  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
22105  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
22106   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
22107  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
22108  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
22109   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
22110  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
22111  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
22112  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
22113  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
22114   /* Saturating doubling multiplies. Types S16 S32.  */
22115  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
22116  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
22117  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
22118   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
22119      S16 S32 U16 U32.  */
22120  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
22121
22122   /* Extract. Size 8.  */
22123  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
22124  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
22125
22126   /* Two registers, miscellaneous.  */
22127   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
22128  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
22129  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
22130  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
22131  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
22132  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
22133  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
22134   /* Vector replicate. Sizes 8 16 32.  */
22135  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
22136  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
22137   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
22138  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
22139   /* VMOVN. Types I16 I32 I64.  */
22140  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
22141   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
22142  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
22143   /* VQMOVUN. Types S16 S32 S64.  */
22144  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
22145   /* VZIP / VUZP. Sizes 8 16 32.  */
22146  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
22147  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
22148  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
22149  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
22150   /* VQABS / VQNEG. Types S8 S16 S32.  */
22151  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
22152  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
22153  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
22154  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
22155   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
22156  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
22157  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
22158  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
22159  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
22160   /* Reciprocal estimates.  Types U32 F16 F32.  */
22161  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
22162  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
22163  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
22164  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
22165   /* VCLS. Types S8 S16 S32.  */
22166  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
22167  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
22168   /* VCLZ. Types I8 I16 I32.  */
22169  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
22170  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
22171   /* VCNT. Size 8.  */
22172  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
22173  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
22174   /* Two address, untyped.  */
22175  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
22176  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
22177   /* VTRN. Sizes 8 16 32.  */
22178  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
22179  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
22180
22181   /* Table lookup. Size 8.  */
22182  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
22183  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
22184
22185 #undef  THUMB_VARIANT
22186 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
22187 #undef  ARM_VARIANT
22188 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
22189
22190   /* Neon element/structure load/store.  */
22191  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22192  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22193  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22194  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22195  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22196  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22197  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22198  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
22199
22200 #undef  THUMB_VARIANT
22201 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
22202 #undef  ARM_VARIANT
22203 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
22204  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
22205  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
22206  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
22207  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
22208  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
22209  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
22210  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
22211  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
22212  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
22213
22214 #undef  THUMB_VARIANT
22215 #define THUMB_VARIANT  & fpu_vfp_ext_v3
22216 #undef  ARM_VARIANT
22217 #define ARM_VARIANT    & fpu_vfp_ext_v3
22218
22219  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
22220  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
22221  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
22222  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
22223  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
22224  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
22225  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
22226  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
22227  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
22228
22229 #undef  ARM_VARIANT
22230 #define ARM_VARIANT    & fpu_vfp_ext_fma
22231 #undef  THUMB_VARIANT
22232 #define THUMB_VARIANT  & fpu_vfp_ext_fma
22233  /* Mnemonics shared by Neon and VFP.  These are included in the
22234     VFP FMA variant; NEON and VFP FMA always includes the NEON
22235     FMA instructions.  */
22236  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
22237  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
22238  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
22239     the v form should always be used.  */
22240  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
22241  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
22242  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
22243  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
22244  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
22245  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
22246
22247 #undef THUMB_VARIANT
22248 #undef  ARM_VARIANT
22249 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
22250
22251  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22252  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22253  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22254  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22255  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22256  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
22257  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
22258  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
22259
22260 #undef  ARM_VARIANT
22261 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
22262
22263  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
22264  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
22265  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
22266  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
22267  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
22268  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
22269  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
22270  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
22271  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
22272  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22273  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22274  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22275  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22276  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22277  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
22278  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
22279  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
22280  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
22281  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
22282  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
22283  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22284  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22285  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22286  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22287  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22288  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
22289  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
22290  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
22291  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
22292  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
22293  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
22294  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
22295  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
22296  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
22297  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
22298  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
22299  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
22300  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22301  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22302  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22303  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22304  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22305  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22306  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22307  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22308  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22309  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
22310  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22311  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22312  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22313  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22314  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22315  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22316  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22317  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22318  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22319  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22320  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22321  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22322  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22323  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22324  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22325  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22326  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22327  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22328  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22329  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
22330  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
22331  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
22332  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
22333  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22334  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22335  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22336  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22337  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22338  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22339  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22340  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22341  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22342  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22343  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22344  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22345  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22346  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22347  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22348  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22349  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22350  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22351  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
22352  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22353  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22354  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22355  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22356  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22357  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22358  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22359  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22360  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22361  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22362  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22363  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22364  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22365  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22366  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22367  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22368  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22369  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22370  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22371  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22372  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22373  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
22374  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22375  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22376  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22377  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22378  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22379  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22380  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22381  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22382  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22383  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22384  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22385  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22386  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22387  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22388  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22389  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22390  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
22391  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
22392  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
22393  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
22394  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
22395  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
22396  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22397  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22398  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22399  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22400  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22401  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22402  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22403  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22404  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22405  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
22406  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
22407  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
22408  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
22409  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
22410  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
22411  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22412  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22413  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22414  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
22415  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
22416  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
22417  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
22418  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
22419  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
22420  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22421  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22422  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
22423  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22424  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
22425
22426 #undef  ARM_VARIANT
22427 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
22428
22429  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
22430  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
22431  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
22432  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
22433  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
22434  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
22435  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22436  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22437  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22438  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22439  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22440  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22441  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22442  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22443  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22444  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22445  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22446  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22447  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22448  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22449  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
22450  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22451  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22452  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22453  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22454  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22455  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22456  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22457  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22458  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22459  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22460  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22461  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22462  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22463  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22464  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22465  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22466  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22467  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22468  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22469  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22470  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22471  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22472  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22473  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22474  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22475  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22476  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22477  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22478  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22479  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22480  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22481  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22482  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22483  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22484  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22485  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
22486
22487 #undef  ARM_VARIANT
22488 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
22489
22490  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
22491  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
22492  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
22493  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
22494  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
22495  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
22496  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
22497  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
22498  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
22499  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
22500  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
22501  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
22502  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
22503  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
22504  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
22505  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
22506  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
22507  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
22508  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
22509  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
22510  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
22511  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
22512  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
22513  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
22514  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
22515  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
22516  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
22517  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
22518  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
22519  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
22520  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
22521  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
22522  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
22523  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
22524  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
22525  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
22526  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
22527  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
22528  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
22529  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
22530  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
22531  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
22532  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
22533  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
22534  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
22535  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
22536  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
22537  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
22538  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
22539  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
22540  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
22541  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
22542  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
22543  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
22544  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
22545  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
22546  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
22547  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
22548  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
22549  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
22550  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
22551  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
22552  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
22553  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
22554  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
22555  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
22556  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
22557  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
22558  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
22559  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
22560  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
22561  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
22562  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
22563  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
22564  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
22565  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
22566
22567  /* ARMv8.5-A instructions.  */
22568 #undef  ARM_VARIANT
22569 #define ARM_VARIANT   & arm_ext_sb
22570 #undef  THUMB_VARIANT
22571 #define THUMB_VARIANT & arm_ext_sb
22572  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
22573
22574 #undef  ARM_VARIANT
22575 #define ARM_VARIANT   & arm_ext_predres
22576 #undef  THUMB_VARIANT
22577 #define THUMB_VARIANT & arm_ext_predres
22578  CE("cfprctx", e070f93, 1, (RRnpc), rd),
22579  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
22580  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
22581
22582  /* ARMv8-M instructions.  */
22583 #undef  ARM_VARIANT
22584 #define ARM_VARIANT NULL
22585 #undef  THUMB_VARIANT
22586 #define THUMB_VARIANT & arm_ext_v8m
22587  ToU("sg",    e97fe97f, 0, (),             noargs),
22588  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
22589  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
22590  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
22591  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
22592  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
22593  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
22594
22595  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
22596     instructions behave as nop if no VFP is present.  */
22597 #undef  THUMB_VARIANT
22598 #define THUMB_VARIANT & arm_ext_v8m_main
22599  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
22600  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
22601
22602  /* Armv8.1-M Mainline instructions.  */
22603 #undef  THUMB_VARIANT
22604 #define THUMB_VARIANT & arm_ext_v8_1m_main
22605  toC("bf",     _bf,     2, (EXPs, EXPs),             t_branch_future),
22606  toU("bfcsel", _bfcsel, 4, (EXPs, EXPs, EXPs, COND), t_branch_future),
22607  toC("bfx",    _bfx,    2, (EXPs, RRnpcsp),          t_branch_future),
22608  toC("bfl",    _bfl,    2, (EXPs, EXPs),             t_branch_future),
22609  toC("bflx",   _bflx,   2, (EXPs, RRnpcsp),          t_branch_future),
22610
22611  toU("dls", _dls, 2, (LR, RRnpcsp),      t_loloop),
22612  toU("wls", _wls, 3, (LR, RRnpcsp, EXP), t_loloop),
22613  toU("le",  _le,  2, (oLR, EXP),         t_loloop),
22614
22615  ToC("clrm",    e89f0000, 1, (CLRMLST),  t_clrm),
22616  ToC("vscclrm", ec9f0a00, 1, (VRSDVLST), t_vscclrm),
22617
22618 #undef  THUMB_VARIANT
22619 #define THUMB_VARIANT & mve_ext
22620  ToC("vpst",    fe710f4d, 0, (), mve_vpt),
22621  ToC("vpstt",   fe318f4d, 0, (), mve_vpt),
22622  ToC("vpste",   fe718f4d, 0, (), mve_vpt),
22623  ToC("vpsttt",  fe314f4d, 0, (), mve_vpt),
22624  ToC("vpstte",  fe31cf4d, 0, (), mve_vpt),
22625  ToC("vpstet",  fe71cf4d, 0, (), mve_vpt),
22626  ToC("vpstee",  fe714f4d, 0, (), mve_vpt),
22627  ToC("vpstttt", fe312f4d, 0, (), mve_vpt),
22628  ToC("vpsttte", fe316f4d, 0, (), mve_vpt),
22629  ToC("vpsttet", fe31ef4d, 0, (), mve_vpt),
22630  ToC("vpsttee", fe31af4d, 0, (), mve_vpt),
22631  ToC("vpstett", fe71af4d, 0, (), mve_vpt),
22632  ToC("vpstete", fe71ef4d, 0, (), mve_vpt),
22633  ToC("vpsteet", fe716f4d, 0, (), mve_vpt),
22634  ToC("vpsteee", fe712f4d, 0, (), mve_vpt),
22635
22636 #undef  ARM_VARIANT
22637 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
22638 #undef  THUMB_VARIANT
22639 #define THUMB_VARIANT  & arm_ext_v6t2
22640
22641  mnCEF(vadd,     _vadd,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR), neon_addsub_if_i),
22642  mnCEF(vsub,     _vsub,    3, (RNSDQMQ, oRNSDQMQ, RNSDQMQR), neon_addsub_if_i),
22643
22644  MNCEF(vabs,  1b10300,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
22645  MNCEF(vneg,  1b10380,  2, (RNSDQMQ, RNSDQMQ),  neon_abs_neg),
22646
22647 #undef ARM_VARIANT
22648 #define ARM_VARIANT & fpu_neon_ext_v1
22649  mnUF(vabd,      _vabd,    3, (RNDQMQ, oRNDQMQ, RNDQMQ), neon_dyadic_if_su),
22650  mnUF(vabdl,     _vabdl,          3, (RNQMQ, RNDMQ, RNDMQ),   neon_dyadic_long),
22651  mnUF(vaddl,     _vaddl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
22652  mnUF(vsubl,     _vsubl,          3, (RNQMQ, RNDMQ, RNDMQR),  neon_dyadic_long),
22653 };
22654 #undef ARM_VARIANT
22655 #undef THUMB_VARIANT
22656 #undef TCE
22657 #undef TUE
22658 #undef TUF
22659 #undef TCC
22660 #undef cCE
22661 #undef cCL
22662 #undef C3E
22663 #undef C3
22664 #undef CE
22665 #undef CM
22666 #undef CL
22667 #undef UE
22668 #undef UF
22669 #undef UT
22670 #undef NUF
22671 #undef nUF
22672 #undef NCE
22673 #undef nCE
22674 #undef OPS0
22675 #undef OPS1
22676 #undef OPS2
22677 #undef OPS3
22678 #undef OPS4
22679 #undef OPS5
22680 #undef OPS6
22681 #undef do_0
22682 #undef ToC
22683 #undef toC
22684 #undef ToU
22685 #undef toU
22686 \f
22687 /* MD interface: bits in the object file.  */
22688
22689 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
22690    for use in the a.out file, and stores them in the array pointed to by buf.
22691    This knows about the endian-ness of the target machine and does
22692    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
22693    2 (short) and 4 (long)  Floating numbers are put out as a series of
22694    LITTLENUMS (shorts, here at least).  */
22695
22696 void
22697 md_number_to_chars (char * buf, valueT val, int n)
22698 {
22699   if (target_big_endian)
22700     number_to_chars_bigendian (buf, val, n);
22701   else
22702     number_to_chars_littleendian (buf, val, n);
22703 }
22704
22705 static valueT
22706 md_chars_to_number (char * buf, int n)
22707 {
22708   valueT result = 0;
22709   unsigned char * where = (unsigned char *) buf;
22710
22711   if (target_big_endian)
22712     {
22713       while (n--)
22714         {
22715           result <<= 8;
22716           result |= (*where++ & 255);
22717         }
22718     }
22719   else
22720     {
22721       while (n--)
22722         {
22723           result <<= 8;
22724           result |= (where[n] & 255);
22725         }
22726     }
22727
22728   return result;
22729 }
22730
22731 /* MD interface: Sections.  */
22732
22733 /* Calculate the maximum variable size (i.e., excluding fr_fix)
22734    that an rs_machine_dependent frag may reach.  */
22735
22736 unsigned int
22737 arm_frag_max_var (fragS *fragp)
22738 {
22739   /* We only use rs_machine_dependent for variable-size Thumb instructions,
22740      which are either THUMB_SIZE (2) or INSN_SIZE (4).
22741
22742      Note that we generate relaxable instructions even for cases that don't
22743      really need it, like an immediate that's a trivial constant.  So we're
22744      overestimating the instruction size for some of those cases.  Rather
22745      than putting more intelligence here, it would probably be better to
22746      avoid generating a relaxation frag in the first place when it can be
22747      determined up front that a short instruction will suffice.  */
22748
22749   gas_assert (fragp->fr_type == rs_machine_dependent);
22750   return INSN_SIZE;
22751 }
22752
22753 /* Estimate the size of a frag before relaxing.  Assume everything fits in
22754    2 bytes.  */
22755
22756 int
22757 md_estimate_size_before_relax (fragS * fragp,
22758                                segT    segtype ATTRIBUTE_UNUSED)
22759 {
22760   fragp->fr_var = 2;
22761   return 2;
22762 }
22763
22764 /* Convert a machine dependent frag.  */
22765
22766 void
22767 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
22768 {
22769   unsigned long insn;
22770   unsigned long old_op;
22771   char *buf;
22772   expressionS exp;
22773   fixS *fixp;
22774   int reloc_type;
22775   int pc_rel;
22776   int opcode;
22777
22778   buf = fragp->fr_literal + fragp->fr_fix;
22779
22780   old_op = bfd_get_16(abfd, buf);
22781   if (fragp->fr_symbol)
22782     {
22783       exp.X_op = O_symbol;
22784       exp.X_add_symbol = fragp->fr_symbol;
22785     }
22786   else
22787     {
22788       exp.X_op = O_constant;
22789     }
22790   exp.X_add_number = fragp->fr_offset;
22791   opcode = fragp->fr_subtype;
22792   switch (opcode)
22793     {
22794     case T_MNEM_ldr_pc:
22795     case T_MNEM_ldr_pc2:
22796     case T_MNEM_ldr_sp:
22797     case T_MNEM_str_sp:
22798     case T_MNEM_ldr:
22799     case T_MNEM_ldrb:
22800     case T_MNEM_ldrh:
22801     case T_MNEM_str:
22802     case T_MNEM_strb:
22803     case T_MNEM_strh:
22804       if (fragp->fr_var == 4)
22805         {
22806           insn = THUMB_OP32 (opcode);
22807           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
22808             {
22809               insn |= (old_op & 0x700) << 4;
22810             }
22811           else
22812             {
22813               insn |= (old_op & 7) << 12;
22814               insn |= (old_op & 0x38) << 13;
22815             }
22816           insn |= 0x00000c00;
22817           put_thumb32_insn (buf, insn);
22818           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
22819         }
22820       else
22821         {
22822           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
22823         }
22824       pc_rel = (opcode == T_MNEM_ldr_pc2);
22825       break;
22826     case T_MNEM_adr:
22827       if (fragp->fr_var == 4)
22828         {
22829           insn = THUMB_OP32 (opcode);
22830           insn |= (old_op & 0xf0) << 4;
22831           put_thumb32_insn (buf, insn);
22832           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
22833         }
22834       else
22835         {
22836           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
22837           exp.X_add_number -= 4;
22838         }
22839       pc_rel = 1;
22840       break;
22841     case T_MNEM_mov:
22842     case T_MNEM_movs:
22843     case T_MNEM_cmp:
22844     case T_MNEM_cmn:
22845       if (fragp->fr_var == 4)
22846         {
22847           int r0off = (opcode == T_MNEM_mov
22848                        || opcode == T_MNEM_movs) ? 0 : 8;
22849           insn = THUMB_OP32 (opcode);
22850           insn = (insn & 0xe1ffffff) | 0x10000000;
22851           insn |= (old_op & 0x700) << r0off;
22852           put_thumb32_insn (buf, insn);
22853           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
22854         }
22855       else
22856         {
22857           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
22858         }
22859       pc_rel = 0;
22860       break;
22861     case T_MNEM_b:
22862       if (fragp->fr_var == 4)
22863         {
22864           insn = THUMB_OP32(opcode);
22865           put_thumb32_insn (buf, insn);
22866           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
22867         }
22868       else
22869         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
22870       pc_rel = 1;
22871       break;
22872     case T_MNEM_bcond:
22873       if (fragp->fr_var == 4)
22874         {
22875           insn = THUMB_OP32(opcode);
22876           insn |= (old_op & 0xf00) << 14;
22877           put_thumb32_insn (buf, insn);
22878           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
22879         }
22880       else
22881         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
22882       pc_rel = 1;
22883       break;
22884     case T_MNEM_add_sp:
22885     case T_MNEM_add_pc:
22886     case T_MNEM_inc_sp:
22887     case T_MNEM_dec_sp:
22888       if (fragp->fr_var == 4)
22889         {
22890           /* ??? Choose between add and addw.  */
22891           insn = THUMB_OP32 (opcode);
22892           insn |= (old_op & 0xf0) << 4;
22893           put_thumb32_insn (buf, insn);
22894           if (opcode == T_MNEM_add_pc)
22895             reloc_type = BFD_RELOC_ARM_T32_IMM12;
22896           else
22897             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
22898         }
22899       else
22900         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
22901       pc_rel = 0;
22902       break;
22903
22904     case T_MNEM_addi:
22905     case T_MNEM_addis:
22906     case T_MNEM_subi:
22907     case T_MNEM_subis:
22908       if (fragp->fr_var == 4)
22909         {
22910           insn = THUMB_OP32 (opcode);
22911           insn |= (old_op & 0xf0) << 4;
22912           insn |= (old_op & 0xf) << 16;
22913           put_thumb32_insn (buf, insn);
22914           if (insn & (1 << 20))
22915             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
22916           else
22917             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
22918         }
22919       else
22920         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
22921       pc_rel = 0;
22922       break;
22923     default:
22924       abort ();
22925     }
22926   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
22927                       (enum bfd_reloc_code_real) reloc_type);
22928   fixp->fx_file = fragp->fr_file;
22929   fixp->fx_line = fragp->fr_line;
22930   fragp->fr_fix += fragp->fr_var;
22931
22932   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
22933   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
22934       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
22935     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
22936 }
22937
22938 /* Return the size of a relaxable immediate operand instruction.
22939    SHIFT and SIZE specify the form of the allowable immediate.  */
22940 static int
22941 relax_immediate (fragS *fragp, int size, int shift)
22942 {
22943   offsetT offset;
22944   offsetT mask;
22945   offsetT low;
22946
22947   /* ??? Should be able to do better than this.  */
22948   if (fragp->fr_symbol)
22949     return 4;
22950
22951   low = (1 << shift) - 1;
22952   mask = (1 << (shift + size)) - (1 << shift);
22953   offset = fragp->fr_offset;
22954   /* Force misaligned offsets to 32-bit variant.  */
22955   if (offset & low)
22956     return 4;
22957   if (offset & ~mask)
22958     return 4;
22959   return 2;
22960 }
22961
22962 /* Get the address of a symbol during relaxation.  */
22963 static addressT
22964 relaxed_symbol_addr (fragS *fragp, long stretch)
22965 {
22966   fragS *sym_frag;
22967   addressT addr;
22968   symbolS *sym;
22969
22970   sym = fragp->fr_symbol;
22971   sym_frag = symbol_get_frag (sym);
22972   know (S_GET_SEGMENT (sym) != absolute_section
22973         || sym_frag == &zero_address_frag);
22974   addr = S_GET_VALUE (sym) + fragp->fr_offset;
22975
22976   /* If frag has yet to be reached on this pass, assume it will
22977      move by STRETCH just as we did.  If this is not so, it will
22978      be because some frag between grows, and that will force
22979      another pass.  */
22980
22981   if (stretch != 0
22982       && sym_frag->relax_marker != fragp->relax_marker)
22983     {
22984       fragS *f;
22985
22986       /* Adjust stretch for any alignment frag.  Note that if have
22987          been expanding the earlier code, the symbol may be
22988          defined in what appears to be an earlier frag.  FIXME:
22989          This doesn't handle the fr_subtype field, which specifies
22990          a maximum number of bytes to skip when doing an
22991          alignment.  */
22992       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
22993         {
22994           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
22995             {
22996               if (stretch < 0)
22997                 stretch = - ((- stretch)
22998                              & ~ ((1 << (int) f->fr_offset) - 1));
22999               else
23000                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
23001               if (stretch == 0)
23002                 break;
23003             }
23004         }
23005       if (f != NULL)
23006         addr += stretch;
23007     }
23008
23009   return addr;
23010 }
23011
23012 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
23013    load.  */
23014 static int
23015 relax_adr (fragS *fragp, asection *sec, long stretch)
23016 {
23017   addressT addr;
23018   offsetT val;
23019
23020   /* Assume worst case for symbols not known to be in the same section.  */
23021   if (fragp->fr_symbol == NULL
23022       || !S_IS_DEFINED (fragp->fr_symbol)
23023       || sec != S_GET_SEGMENT (fragp->fr_symbol)
23024       || S_IS_WEAK (fragp->fr_symbol))
23025     return 4;
23026
23027   val = relaxed_symbol_addr (fragp, stretch);
23028   addr = fragp->fr_address + fragp->fr_fix;
23029   addr = (addr + 4) & ~3;
23030   /* Force misaligned targets to 32-bit variant.  */
23031   if (val & 3)
23032     return 4;
23033   val -= addr;
23034   if (val < 0 || val > 1020)
23035     return 4;
23036   return 2;
23037 }
23038
23039 /* Return the size of a relaxable add/sub immediate instruction.  */
23040 static int
23041 relax_addsub (fragS *fragp, asection *sec)
23042 {
23043   char *buf;
23044   int op;
23045
23046   buf = fragp->fr_literal + fragp->fr_fix;
23047   op = bfd_get_16(sec->owner, buf);
23048   if ((op & 0xf) == ((op >> 4) & 0xf))
23049     return relax_immediate (fragp, 8, 0);
23050   else
23051     return relax_immediate (fragp, 3, 0);
23052 }
23053
23054 /* Return TRUE iff the definition of symbol S could be pre-empted
23055    (overridden) at link or load time.  */
23056 static bfd_boolean
23057 symbol_preemptible (symbolS *s)
23058 {
23059   /* Weak symbols can always be pre-empted.  */
23060   if (S_IS_WEAK (s))
23061     return TRUE;
23062
23063   /* Non-global symbols cannot be pre-empted. */
23064   if (! S_IS_EXTERNAL (s))
23065     return FALSE;
23066
23067 #ifdef OBJ_ELF
23068   /* In ELF, a global symbol can be marked protected, or private.  In that
23069      case it can't be pre-empted (other definitions in the same link unit
23070      would violate the ODR).  */
23071   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
23072     return FALSE;
23073 #endif
23074
23075   /* Other global symbols might be pre-empted.  */
23076   return TRUE;
23077 }
23078
23079 /* Return the size of a relaxable branch instruction.  BITS is the
23080    size of the offset field in the narrow instruction.  */
23081
23082 static int
23083 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
23084 {
23085   addressT addr;
23086   offsetT val;
23087   offsetT limit;
23088
23089   /* Assume worst case for symbols not known to be in the same section.  */
23090   if (!S_IS_DEFINED (fragp->fr_symbol)
23091       || sec != S_GET_SEGMENT (fragp->fr_symbol)
23092       || S_IS_WEAK (fragp->fr_symbol))
23093     return 4;
23094
23095 #ifdef OBJ_ELF
23096   /* A branch to a function in ARM state will require interworking.  */
23097   if (S_IS_DEFINED (fragp->fr_symbol)
23098       && ARM_IS_FUNC (fragp->fr_symbol))
23099       return 4;
23100 #endif
23101
23102   if (symbol_preemptible (fragp->fr_symbol))
23103     return 4;
23104
23105   val = relaxed_symbol_addr (fragp, stretch);
23106   addr = fragp->fr_address + fragp->fr_fix + 4;
23107   val -= addr;
23108
23109   /* Offset is a signed value *2 */
23110   limit = 1 << bits;
23111   if (val >= limit || val < -limit)
23112     return 4;
23113   return 2;
23114 }
23115
23116
23117 /* Relax a machine dependent frag.  This returns the amount by which
23118    the current size of the frag should change.  */
23119
23120 int
23121 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
23122 {
23123   int oldsize;
23124   int newsize;
23125
23126   oldsize = fragp->fr_var;
23127   switch (fragp->fr_subtype)
23128     {
23129     case T_MNEM_ldr_pc2:
23130       newsize = relax_adr (fragp, sec, stretch);
23131       break;
23132     case T_MNEM_ldr_pc:
23133     case T_MNEM_ldr_sp:
23134     case T_MNEM_str_sp:
23135       newsize = relax_immediate (fragp, 8, 2);
23136       break;
23137     case T_MNEM_ldr:
23138     case T_MNEM_str:
23139       newsize = relax_immediate (fragp, 5, 2);
23140       break;
23141     case T_MNEM_ldrh:
23142     case T_MNEM_strh:
23143       newsize = relax_immediate (fragp, 5, 1);
23144       break;
23145     case T_MNEM_ldrb:
23146     case T_MNEM_strb:
23147       newsize = relax_immediate (fragp, 5, 0);
23148       break;
23149     case T_MNEM_adr:
23150       newsize = relax_adr (fragp, sec, stretch);
23151       break;
23152     case T_MNEM_mov:
23153     case T_MNEM_movs:
23154     case T_MNEM_cmp:
23155     case T_MNEM_cmn:
23156       newsize = relax_immediate (fragp, 8, 0);
23157       break;
23158     case T_MNEM_b:
23159       newsize = relax_branch (fragp, sec, 11, stretch);
23160       break;
23161     case T_MNEM_bcond:
23162       newsize = relax_branch (fragp, sec, 8, stretch);
23163       break;
23164     case T_MNEM_add_sp:
23165     case T_MNEM_add_pc:
23166       newsize = relax_immediate (fragp, 8, 2);
23167       break;
23168     case T_MNEM_inc_sp:
23169     case T_MNEM_dec_sp:
23170       newsize = relax_immediate (fragp, 7, 2);
23171       break;
23172     case T_MNEM_addi:
23173     case T_MNEM_addis:
23174     case T_MNEM_subi:
23175     case T_MNEM_subis:
23176       newsize = relax_addsub (fragp, sec);
23177       break;
23178     default:
23179       abort ();
23180     }
23181
23182   fragp->fr_var = newsize;
23183   /* Freeze wide instructions that are at or before the same location as
23184      in the previous pass.  This avoids infinite loops.
23185      Don't freeze them unconditionally because targets may be artificially
23186      misaligned by the expansion of preceding frags.  */
23187   if (stretch <= 0 && newsize > 2)
23188     {
23189       md_convert_frag (sec->owner, sec, fragp);
23190       frag_wane (fragp);
23191     }
23192
23193   return newsize - oldsize;
23194 }
23195
23196 /* Round up a section size to the appropriate boundary.  */
23197
23198 valueT
23199 md_section_align (segT   segment ATTRIBUTE_UNUSED,
23200                   valueT size)
23201 {
23202   return size;
23203 }
23204
23205 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
23206    of an rs_align_code fragment.  */
23207
23208 void
23209 arm_handle_align (fragS * fragP)
23210 {
23211   static unsigned char const arm_noop[2][2][4] =
23212     {
23213       {  /* ARMv1 */
23214         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
23215         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
23216       },
23217       {  /* ARMv6k */
23218         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
23219         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
23220       },
23221     };
23222   static unsigned char const thumb_noop[2][2][2] =
23223     {
23224       {  /* Thumb-1 */
23225         {0xc0, 0x46},  /* LE */
23226         {0x46, 0xc0},  /* BE */
23227       },
23228       {  /* Thumb-2 */
23229         {0x00, 0xbf},  /* LE */
23230         {0xbf, 0x00}   /* BE */
23231       }
23232     };
23233   static unsigned char const wide_thumb_noop[2][4] =
23234     {  /* Wide Thumb-2 */
23235       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
23236       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
23237     };
23238
23239   unsigned bytes, fix, noop_size;
23240   char * p;
23241   const unsigned char * noop;
23242   const unsigned char *narrow_noop = NULL;
23243 #ifdef OBJ_ELF
23244   enum mstate state;
23245 #endif
23246
23247   if (fragP->fr_type != rs_align_code)
23248     return;
23249
23250   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
23251   p = fragP->fr_literal + fragP->fr_fix;
23252   fix = 0;
23253
23254   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
23255     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
23256
23257   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
23258
23259   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
23260     {
23261       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
23262                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
23263         {
23264           narrow_noop = thumb_noop[1][target_big_endian];
23265           noop = wide_thumb_noop[target_big_endian];
23266         }
23267       else
23268         noop = thumb_noop[0][target_big_endian];
23269       noop_size = 2;
23270 #ifdef OBJ_ELF
23271       state = MAP_THUMB;
23272 #endif
23273     }
23274   else
23275     {
23276       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
23277                                            ? selected_cpu : arm_arch_none,
23278                                            arm_ext_v6k) != 0]
23279                      [target_big_endian];
23280       noop_size = 4;
23281 #ifdef OBJ_ELF
23282       state = MAP_ARM;
23283 #endif
23284     }
23285
23286   fragP->fr_var = noop_size;
23287
23288   if (bytes & (noop_size - 1))
23289     {
23290       fix = bytes & (noop_size - 1);
23291 #ifdef OBJ_ELF
23292       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
23293 #endif
23294       memset (p, 0, fix);
23295       p += fix;
23296       bytes -= fix;
23297     }
23298
23299   if (narrow_noop)
23300     {
23301       if (bytes & noop_size)
23302         {
23303           /* Insert a narrow noop.  */
23304           memcpy (p, narrow_noop, noop_size);
23305           p += noop_size;
23306           bytes -= noop_size;
23307           fix += noop_size;
23308         }
23309
23310       /* Use wide noops for the remainder */
23311       noop_size = 4;
23312     }
23313
23314   while (bytes >= noop_size)
23315     {
23316       memcpy (p, noop, noop_size);
23317       p += noop_size;
23318       bytes -= noop_size;
23319       fix += noop_size;
23320     }
23321
23322   fragP->fr_fix += fix;
23323 }
23324
23325 /* Called from md_do_align.  Used to create an alignment
23326    frag in a code section.  */
23327
23328 void
23329 arm_frag_align_code (int n, int max)
23330 {
23331   char * p;
23332
23333   /* We assume that there will never be a requirement
23334      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
23335   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
23336     {
23337       char err_msg[128];
23338
23339       sprintf (err_msg,
23340         _("alignments greater than %d bytes not supported in .text sections."),
23341         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
23342       as_fatal ("%s", err_msg);
23343     }
23344
23345   p = frag_var (rs_align_code,
23346                 MAX_MEM_FOR_RS_ALIGN_CODE,
23347                 1,
23348                 (relax_substateT) max,
23349                 (symbolS *) NULL,
23350                 (offsetT) n,
23351                 (char *) NULL);
23352   *p = 0;
23353 }
23354
23355 /* Perform target specific initialisation of a frag.
23356    Note - despite the name this initialisation is not done when the frag
23357    is created, but only when its type is assigned.  A frag can be created
23358    and used a long time before its type is set, so beware of assuming that
23359    this initialisation is performed first.  */
23360
23361 #ifndef OBJ_ELF
23362 void
23363 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
23364 {
23365   /* Record whether this frag is in an ARM or a THUMB area.  */
23366   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
23367 }
23368
23369 #else /* OBJ_ELF is defined.  */
23370 void
23371 arm_init_frag (fragS * fragP, int max_chars)
23372 {
23373   bfd_boolean frag_thumb_mode;
23374
23375   /* If the current ARM vs THUMB mode has not already
23376      been recorded into this frag then do so now.  */
23377   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
23378     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
23379
23380   /* PR 21809: Do not set a mapping state for debug sections
23381      - it just confuses other tools.  */
23382   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
23383     return;
23384
23385   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
23386
23387   /* Record a mapping symbol for alignment frags.  We will delete this
23388      later if the alignment ends up empty.  */
23389   switch (fragP->fr_type)
23390     {
23391     case rs_align:
23392     case rs_align_test:
23393     case rs_fill:
23394       mapping_state_2 (MAP_DATA, max_chars);
23395       break;
23396     case rs_align_code:
23397       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
23398       break;
23399     default:
23400       break;
23401     }
23402 }
23403
23404 /* When we change sections we need to issue a new mapping symbol.  */
23405
23406 void
23407 arm_elf_change_section (void)
23408 {
23409   /* Link an unlinked unwind index table section to the .text section.  */
23410   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
23411       && elf_linked_to_section (now_seg) == NULL)
23412     elf_linked_to_section (now_seg) = text_section;
23413 }
23414
23415 int
23416 arm_elf_section_type (const char * str, size_t len)
23417 {
23418   if (len == 5 && strncmp (str, "exidx", 5) == 0)
23419     return SHT_ARM_EXIDX;
23420
23421   return -1;
23422 }
23423 \f
23424 /* Code to deal with unwinding tables.  */
23425
23426 static void add_unwind_adjustsp (offsetT);
23427
23428 /* Generate any deferred unwind frame offset.  */
23429
23430 static void
23431 flush_pending_unwind (void)
23432 {
23433   offsetT offset;
23434
23435   offset = unwind.pending_offset;
23436   unwind.pending_offset = 0;
23437   if (offset != 0)
23438     add_unwind_adjustsp (offset);
23439 }
23440
23441 /* Add an opcode to this list for this function.  Two-byte opcodes should
23442    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
23443    order.  */
23444
23445 static void
23446 add_unwind_opcode (valueT op, int length)
23447 {
23448   /* Add any deferred stack adjustment.  */
23449   if (unwind.pending_offset)
23450     flush_pending_unwind ();
23451
23452   unwind.sp_restored = 0;
23453
23454   if (unwind.opcode_count + length > unwind.opcode_alloc)
23455     {
23456       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
23457       if (unwind.opcodes)
23458         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
23459                                      unwind.opcode_alloc);
23460       else
23461         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
23462     }
23463   while (length > 0)
23464     {
23465       length--;
23466       unwind.opcodes[unwind.opcode_count] = op & 0xff;
23467       op >>= 8;
23468       unwind.opcode_count++;
23469     }
23470 }
23471
23472 /* Add unwind opcodes to adjust the stack pointer.  */
23473
23474 static void
23475 add_unwind_adjustsp (offsetT offset)
23476 {
23477   valueT op;
23478
23479   if (offset > 0x200)
23480     {
23481       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
23482       char bytes[5];
23483       int n;
23484       valueT o;
23485
23486       /* Long form: 0xb2, uleb128.  */
23487       /* This might not fit in a word so add the individual bytes,
23488          remembering the list is built in reverse order.  */
23489       o = (valueT) ((offset - 0x204) >> 2);
23490       if (o == 0)
23491         add_unwind_opcode (0, 1);
23492
23493       /* Calculate the uleb128 encoding of the offset.  */
23494       n = 0;
23495       while (o)
23496         {
23497           bytes[n] = o & 0x7f;
23498           o >>= 7;
23499           if (o)
23500             bytes[n] |= 0x80;
23501           n++;
23502         }
23503       /* Add the insn.  */
23504       for (; n; n--)
23505         add_unwind_opcode (bytes[n - 1], 1);
23506       add_unwind_opcode (0xb2, 1);
23507     }
23508   else if (offset > 0x100)
23509     {
23510       /* Two short opcodes.  */
23511       add_unwind_opcode (0x3f, 1);
23512       op = (offset - 0x104) >> 2;
23513       add_unwind_opcode (op, 1);
23514     }
23515   else if (offset > 0)
23516     {
23517       /* Short opcode.  */
23518       op = (offset - 4) >> 2;
23519       add_unwind_opcode (op, 1);
23520     }
23521   else if (offset < 0)
23522     {
23523       offset = -offset;
23524       while (offset > 0x100)
23525         {
23526           add_unwind_opcode (0x7f, 1);
23527           offset -= 0x100;
23528         }
23529       op = ((offset - 4) >> 2) | 0x40;
23530       add_unwind_opcode (op, 1);
23531     }
23532 }
23533
23534 /* Finish the list of unwind opcodes for this function.  */
23535
23536 static void
23537 finish_unwind_opcodes (void)
23538 {
23539   valueT op;
23540
23541   if (unwind.fp_used)
23542     {
23543       /* Adjust sp as necessary.  */
23544       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
23545       flush_pending_unwind ();
23546
23547       /* After restoring sp from the frame pointer.  */
23548       op = 0x90 | unwind.fp_reg;
23549       add_unwind_opcode (op, 1);
23550     }
23551   else
23552     flush_pending_unwind ();
23553 }
23554
23555
23556 /* Start an exception table entry.  If idx is nonzero this is an index table
23557    entry.  */
23558
23559 static void
23560 start_unwind_section (const segT text_seg, int idx)
23561 {
23562   const char * text_name;
23563   const char * prefix;
23564   const char * prefix_once;
23565   const char * group_name;
23566   char * sec_name;
23567   int type;
23568   int flags;
23569   int linkonce;
23570
23571   if (idx)
23572     {
23573       prefix = ELF_STRING_ARM_unwind;
23574       prefix_once = ELF_STRING_ARM_unwind_once;
23575       type = SHT_ARM_EXIDX;
23576     }
23577   else
23578     {
23579       prefix = ELF_STRING_ARM_unwind_info;
23580       prefix_once = ELF_STRING_ARM_unwind_info_once;
23581       type = SHT_PROGBITS;
23582     }
23583
23584   text_name = segment_name (text_seg);
23585   if (streq (text_name, ".text"))
23586     text_name = "";
23587
23588   if (strncmp (text_name, ".gnu.linkonce.t.",
23589                strlen (".gnu.linkonce.t.")) == 0)
23590     {
23591       prefix = prefix_once;
23592       text_name += strlen (".gnu.linkonce.t.");
23593     }
23594
23595   sec_name = concat (prefix, text_name, (char *) NULL);
23596
23597   flags = SHF_ALLOC;
23598   linkonce = 0;
23599   group_name = 0;
23600
23601   /* Handle COMDAT group.  */
23602   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
23603     {
23604       group_name = elf_group_name (text_seg);
23605       if (group_name == NULL)
23606         {
23607           as_bad (_("Group section `%s' has no group signature"),
23608                   segment_name (text_seg));
23609           ignore_rest_of_line ();
23610           return;
23611         }
23612       flags |= SHF_GROUP;
23613       linkonce = 1;
23614     }
23615
23616   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
23617                           linkonce, 0);
23618
23619   /* Set the section link for index tables.  */
23620   if (idx)
23621     elf_linked_to_section (now_seg) = text_seg;
23622 }
23623
23624
23625 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
23626    personality routine data.  Returns zero, or the index table value for
23627    an inline entry.  */
23628
23629 static valueT
23630 create_unwind_entry (int have_data)
23631 {
23632   int size;
23633   addressT where;
23634   char *ptr;
23635   /* The current word of data.  */
23636   valueT data;
23637   /* The number of bytes left in this word.  */
23638   int n;
23639
23640   finish_unwind_opcodes ();
23641
23642   /* Remember the current text section.  */
23643   unwind.saved_seg = now_seg;
23644   unwind.saved_subseg = now_subseg;
23645
23646   start_unwind_section (now_seg, 0);
23647
23648   if (unwind.personality_routine == NULL)
23649     {
23650       if (unwind.personality_index == -2)
23651         {
23652           if (have_data)
23653             as_bad (_("handlerdata in cantunwind frame"));
23654           return 1; /* EXIDX_CANTUNWIND.  */
23655         }
23656
23657       /* Use a default personality routine if none is specified.  */
23658       if (unwind.personality_index == -1)
23659         {
23660           if (unwind.opcode_count > 3)
23661             unwind.personality_index = 1;
23662           else
23663             unwind.personality_index = 0;
23664         }
23665
23666       /* Space for the personality routine entry.  */
23667       if (unwind.personality_index == 0)
23668         {
23669           if (unwind.opcode_count > 3)
23670             as_bad (_("too many unwind opcodes for personality routine 0"));
23671
23672           if (!have_data)
23673             {
23674               /* All the data is inline in the index table.  */
23675               data = 0x80;
23676               n = 3;
23677               while (unwind.opcode_count > 0)
23678                 {
23679                   unwind.opcode_count--;
23680                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
23681                   n--;
23682                 }
23683
23684               /* Pad with "finish" opcodes.  */
23685               while (n--)
23686                 data = (data << 8) | 0xb0;
23687
23688               return data;
23689             }
23690           size = 0;
23691         }
23692       else
23693         /* We get two opcodes "free" in the first word.  */
23694         size = unwind.opcode_count - 2;
23695     }
23696   else
23697     {
23698       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
23699       if (unwind.personality_index != -1)
23700         {
23701           as_bad (_("attempt to recreate an unwind entry"));
23702           return 1;
23703         }
23704
23705       /* An extra byte is required for the opcode count.        */
23706       size = unwind.opcode_count + 1;
23707     }
23708
23709   size = (size + 3) >> 2;
23710   if (size > 0xff)
23711     as_bad (_("too many unwind opcodes"));
23712
23713   frag_align (2, 0, 0);
23714   record_alignment (now_seg, 2);
23715   unwind.table_entry = expr_build_dot ();
23716
23717   /* Allocate the table entry.  */
23718   ptr = frag_more ((size << 2) + 4);
23719   /* PR 13449: Zero the table entries in case some of them are not used.  */
23720   memset (ptr, 0, (size << 2) + 4);
23721   where = frag_now_fix () - ((size << 2) + 4);
23722
23723   switch (unwind.personality_index)
23724     {
23725     case -1:
23726       /* ??? Should this be a PLT generating relocation?  */
23727       /* Custom personality routine.  */
23728       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
23729                BFD_RELOC_ARM_PREL31);
23730
23731       where += 4;
23732       ptr += 4;
23733
23734       /* Set the first byte to the number of additional words.  */
23735       data = size > 0 ? size - 1 : 0;
23736       n = 3;
23737       break;
23738
23739     /* ABI defined personality routines.  */
23740     case 0:
23741       /* Three opcodes bytes are packed into the first word.  */
23742       data = 0x80;
23743       n = 3;
23744       break;
23745
23746     case 1:
23747     case 2:
23748       /* The size and first two opcode bytes go in the first word.  */
23749       data = ((0x80 + unwind.personality_index) << 8) | size;
23750       n = 2;
23751       break;
23752
23753     default:
23754       /* Should never happen.  */
23755       abort ();
23756     }
23757
23758   /* Pack the opcodes into words (MSB first), reversing the list at the same
23759      time.  */
23760   while (unwind.opcode_count > 0)
23761     {
23762       if (n == 0)
23763         {
23764           md_number_to_chars (ptr, data, 4);
23765           ptr += 4;
23766           n = 4;
23767           data = 0;
23768         }
23769       unwind.opcode_count--;
23770       n--;
23771       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
23772     }
23773
23774   /* Finish off the last word.  */
23775   if (n < 4)
23776     {
23777       /* Pad with "finish" opcodes.  */
23778       while (n--)
23779         data = (data << 8) | 0xb0;
23780
23781       md_number_to_chars (ptr, data, 4);
23782     }
23783
23784   if (!have_data)
23785     {
23786       /* Add an empty descriptor if there is no user-specified data.   */
23787       ptr = frag_more (4);
23788       md_number_to_chars (ptr, 0, 4);
23789     }
23790
23791   return 0;
23792 }
23793
23794
23795 /* Initialize the DWARF-2 unwind information for this procedure.  */
23796
23797 void
23798 tc_arm_frame_initial_instructions (void)
23799 {
23800   cfi_add_CFA_def_cfa (REG_SP, 0);
23801 }
23802 #endif /* OBJ_ELF */
23803
23804 /* Convert REGNAME to a DWARF-2 register number.  */
23805
23806 int
23807 tc_arm_regname_to_dw2regnum (char *regname)
23808 {
23809   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
23810   if (reg != FAIL)
23811     return reg;
23812
23813   /* PR 16694: Allow VFP registers as well.  */
23814   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
23815   if (reg != FAIL)
23816     return 64 + reg;
23817
23818   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
23819   if (reg != FAIL)
23820     return reg + 256;
23821
23822   return FAIL;
23823 }
23824
23825 #ifdef TE_PE
23826 void
23827 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
23828 {
23829   expressionS exp;
23830
23831   exp.X_op = O_secrel;
23832   exp.X_add_symbol = symbol;
23833   exp.X_add_number = 0;
23834   emit_expr (&exp, size);
23835 }
23836 #endif
23837
23838 /* MD interface: Symbol and relocation handling.  */
23839
23840 /* Return the address within the segment that a PC-relative fixup is
23841    relative to.  For ARM, PC-relative fixups applied to instructions
23842    are generally relative to the location of the fixup plus 8 bytes.
23843    Thumb branches are offset by 4, and Thumb loads relative to PC
23844    require special handling.  */
23845
23846 long
23847 md_pcrel_from_section (fixS * fixP, segT seg)
23848 {
23849   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
23850
23851   /* If this is pc-relative and we are going to emit a relocation
23852      then we just want to put out any pipeline compensation that the linker
23853      will need.  Otherwise we want to use the calculated base.
23854      For WinCE we skip the bias for externals as well, since this
23855      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
23856   if (fixP->fx_pcrel
23857       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
23858           || (arm_force_relocation (fixP)
23859 #ifdef TE_WINCE
23860               && !S_IS_EXTERNAL (fixP->fx_addsy)
23861 #endif
23862               )))
23863     base = 0;
23864
23865
23866   switch (fixP->fx_r_type)
23867     {
23868       /* PC relative addressing on the Thumb is slightly odd as the
23869          bottom two bits of the PC are forced to zero for the
23870          calculation.  This happens *after* application of the
23871          pipeline offset.  However, Thumb adrl already adjusts for
23872          this, so we need not do it again.  */
23873     case BFD_RELOC_ARM_THUMB_ADD:
23874       return base & ~3;
23875
23876     case BFD_RELOC_ARM_THUMB_OFFSET:
23877     case BFD_RELOC_ARM_T32_OFFSET_IMM:
23878     case BFD_RELOC_ARM_T32_ADD_PC12:
23879     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
23880       return (base + 4) & ~3;
23881
23882       /* Thumb branches are simply offset by +4.  */
23883     case BFD_RELOC_THUMB_PCREL_BRANCH5:
23884     case BFD_RELOC_THUMB_PCREL_BRANCH7:
23885     case BFD_RELOC_THUMB_PCREL_BRANCH9:
23886     case BFD_RELOC_THUMB_PCREL_BRANCH12:
23887     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23888     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23889     case BFD_RELOC_THUMB_PCREL_BFCSEL:
23890     case BFD_RELOC_ARM_THUMB_BF17:
23891     case BFD_RELOC_ARM_THUMB_BF19:
23892     case BFD_RELOC_ARM_THUMB_BF13:
23893     case BFD_RELOC_ARM_THUMB_LOOP12:
23894       return base + 4;
23895
23896     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23897       if (fixP->fx_addsy
23898           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23899           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23900           && ARM_IS_FUNC (fixP->fx_addsy)
23901           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23902         base = fixP->fx_where + fixP->fx_frag->fr_address;
23903        return base + 4;
23904
23905       /* BLX is like branches above, but forces the low two bits of PC to
23906          zero.  */
23907     case BFD_RELOC_THUMB_PCREL_BLX:
23908       if (fixP->fx_addsy
23909           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23910           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23911           && THUMB_IS_FUNC (fixP->fx_addsy)
23912           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23913         base = fixP->fx_where + fixP->fx_frag->fr_address;
23914       return (base + 4) & ~3;
23915
23916       /* ARM mode branches are offset by +8.  However, the Windows CE
23917          loader expects the relocation not to take this into account.  */
23918     case BFD_RELOC_ARM_PCREL_BLX:
23919       if (fixP->fx_addsy
23920           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23921           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23922           && ARM_IS_FUNC (fixP->fx_addsy)
23923           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23924         base = fixP->fx_where + fixP->fx_frag->fr_address;
23925       return base + 8;
23926
23927     case BFD_RELOC_ARM_PCREL_CALL:
23928       if (fixP->fx_addsy
23929           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23930           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23931           && THUMB_IS_FUNC (fixP->fx_addsy)
23932           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23933         base = fixP->fx_where + fixP->fx_frag->fr_address;
23934       return base + 8;
23935
23936     case BFD_RELOC_ARM_PCREL_BRANCH:
23937     case BFD_RELOC_ARM_PCREL_JUMP:
23938     case BFD_RELOC_ARM_PLT32:
23939 #ifdef TE_WINCE
23940       /* When handling fixups immediately, because we have already
23941          discovered the value of a symbol, or the address of the frag involved
23942          we must account for the offset by +8, as the OS loader will never see the reloc.
23943          see fixup_segment() in write.c
23944          The S_IS_EXTERNAL test handles the case of global symbols.
23945          Those need the calculated base, not just the pipe compensation the linker will need.  */
23946       if (fixP->fx_pcrel
23947           && fixP->fx_addsy != NULL
23948           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23949           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
23950         return base + 8;
23951       return base;
23952 #else
23953       return base + 8;
23954 #endif
23955
23956
23957       /* ARM mode loads relative to PC are also offset by +8.  Unlike
23958          branches, the Windows CE loader *does* expect the relocation
23959          to take this into account.  */
23960     case BFD_RELOC_ARM_OFFSET_IMM:
23961     case BFD_RELOC_ARM_OFFSET_IMM8:
23962     case BFD_RELOC_ARM_HWLITERAL:
23963     case BFD_RELOC_ARM_LITERAL:
23964     case BFD_RELOC_ARM_CP_OFF_IMM:
23965       return base + 8;
23966
23967
23968       /* Other PC-relative relocations are un-offset.  */
23969     default:
23970       return base;
23971     }
23972 }
23973
23974 static bfd_boolean flag_warn_syms = TRUE;
23975
23976 bfd_boolean
23977 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
23978 {
23979   /* PR 18347 - Warn if the user attempts to create a symbol with the same
23980      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
23981      does mean that the resulting code might be very confusing to the reader.
23982      Also this warning can be triggered if the user omits an operand before
23983      an immediate address, eg:
23984
23985        LDR =foo
23986
23987      GAS treats this as an assignment of the value of the symbol foo to a
23988      symbol LDR, and so (without this code) it will not issue any kind of
23989      warning or error message.
23990
23991      Note - ARM instructions are case-insensitive but the strings in the hash
23992      table are all stored in lower case, so we must first ensure that name is
23993      lower case too.  */
23994   if (flag_warn_syms && arm_ops_hsh)
23995     {
23996       char * nbuf = strdup (name);
23997       char * p;
23998
23999       for (p = nbuf; *p; p++)
24000         *p = TOLOWER (*p);
24001       if (hash_find (arm_ops_hsh, nbuf) != NULL)
24002         {
24003           static struct hash_control * already_warned = NULL;
24004
24005           if (already_warned == NULL)
24006             already_warned = hash_new ();
24007           /* Only warn about the symbol once.  To keep the code
24008              simple we let hash_insert do the lookup for us.  */
24009           if (hash_insert (already_warned, nbuf, NULL) == NULL)
24010             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
24011         }
24012       else
24013         free (nbuf);
24014     }
24015
24016   return FALSE;
24017 }
24018
24019 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
24020    Otherwise we have no need to default values of symbols.  */
24021
24022 symbolS *
24023 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
24024 {
24025 #ifdef OBJ_ELF
24026   if (name[0] == '_' && name[1] == 'G'
24027       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
24028     {
24029       if (!GOT_symbol)
24030         {
24031           if (symbol_find (name))
24032             as_bad (_("GOT already in the symbol table"));
24033
24034           GOT_symbol = symbol_new (name, undefined_section,
24035                                    (valueT) 0, & zero_address_frag);
24036         }
24037
24038       return GOT_symbol;
24039     }
24040 #endif
24041
24042   return NULL;
24043 }
24044
24045 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
24046    computed as two separate immediate values, added together.  We
24047    already know that this value cannot be computed by just one ARM
24048    instruction.  */
24049
24050 static unsigned int
24051 validate_immediate_twopart (unsigned int   val,
24052                             unsigned int * highpart)
24053 {
24054   unsigned int a;
24055   unsigned int i;
24056
24057   for (i = 0; i < 32; i += 2)
24058     if (((a = rotate_left (val, i)) & 0xff) != 0)
24059       {
24060         if (a & 0xff00)
24061           {
24062             if (a & ~ 0xffff)
24063               continue;
24064             * highpart = (a  >> 8) | ((i + 24) << 7);
24065           }
24066         else if (a & 0xff0000)
24067           {
24068             if (a & 0xff000000)
24069               continue;
24070             * highpart = (a >> 16) | ((i + 16) << 7);
24071           }
24072         else
24073           {
24074             gas_assert (a & 0xff000000);
24075             * highpart = (a >> 24) | ((i + 8) << 7);
24076           }
24077
24078         return (a & 0xff) | (i << 7);
24079       }
24080
24081   return FAIL;
24082 }
24083
24084 static int
24085 validate_offset_imm (unsigned int val, int hwse)
24086 {
24087   if ((hwse && val > 255) || val > 4095)
24088     return FAIL;
24089   return val;
24090 }
24091
24092 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
24093    negative immediate constant by altering the instruction.  A bit of
24094    a hack really.
24095         MOV <-> MVN
24096         AND <-> BIC
24097         ADC <-> SBC
24098         by inverting the second operand, and
24099         ADD <-> SUB
24100         CMP <-> CMN
24101         by negating the second operand.  */
24102
24103 static int
24104 negate_data_op (unsigned long * instruction,
24105                 unsigned long   value)
24106 {
24107   int op, new_inst;
24108   unsigned long negated, inverted;
24109
24110   negated = encode_arm_immediate (-value);
24111   inverted = encode_arm_immediate (~value);
24112
24113   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
24114   switch (op)
24115     {
24116       /* First negates.  */
24117     case OPCODE_SUB:             /* ADD <-> SUB  */
24118       new_inst = OPCODE_ADD;
24119       value = negated;
24120       break;
24121
24122     case OPCODE_ADD:
24123       new_inst = OPCODE_SUB;
24124       value = negated;
24125       break;
24126
24127     case OPCODE_CMP:             /* CMP <-> CMN  */
24128       new_inst = OPCODE_CMN;
24129       value = negated;
24130       break;
24131
24132     case OPCODE_CMN:
24133       new_inst = OPCODE_CMP;
24134       value = negated;
24135       break;
24136
24137       /* Now Inverted ops.  */
24138     case OPCODE_MOV:             /* MOV <-> MVN  */
24139       new_inst = OPCODE_MVN;
24140       value = inverted;
24141       break;
24142
24143     case OPCODE_MVN:
24144       new_inst = OPCODE_MOV;
24145       value = inverted;
24146       break;
24147
24148     case OPCODE_AND:             /* AND <-> BIC  */
24149       new_inst = OPCODE_BIC;
24150       value = inverted;
24151       break;
24152
24153     case OPCODE_BIC:
24154       new_inst = OPCODE_AND;
24155       value = inverted;
24156       break;
24157
24158     case OPCODE_ADC:              /* ADC <-> SBC  */
24159       new_inst = OPCODE_SBC;
24160       value = inverted;
24161       break;
24162
24163     case OPCODE_SBC:
24164       new_inst = OPCODE_ADC;
24165       value = inverted;
24166       break;
24167
24168       /* We cannot do anything.  */
24169     default:
24170       return FAIL;
24171     }
24172
24173   if (value == (unsigned) FAIL)
24174     return FAIL;
24175
24176   *instruction &= OPCODE_MASK;
24177   *instruction |= new_inst << DATA_OP_SHIFT;
24178   return value;
24179 }
24180
24181 /* Like negate_data_op, but for Thumb-2.   */
24182
24183 static unsigned int
24184 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
24185 {
24186   int op, new_inst;
24187   int rd;
24188   unsigned int negated, inverted;
24189
24190   negated = encode_thumb32_immediate (-value);
24191   inverted = encode_thumb32_immediate (~value);
24192
24193   rd = (*instruction >> 8) & 0xf;
24194   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
24195   switch (op)
24196     {
24197       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
24198     case T2_OPCODE_SUB:
24199       new_inst = T2_OPCODE_ADD;
24200       value = negated;
24201       break;
24202
24203     case T2_OPCODE_ADD:
24204       new_inst = T2_OPCODE_SUB;
24205       value = negated;
24206       break;
24207
24208       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
24209     case T2_OPCODE_ORR:
24210       new_inst = T2_OPCODE_ORN;
24211       value = inverted;
24212       break;
24213
24214     case T2_OPCODE_ORN:
24215       new_inst = T2_OPCODE_ORR;
24216       value = inverted;
24217       break;
24218
24219       /* AND <-> BIC.  TST has no inverted equivalent.  */
24220     case T2_OPCODE_AND:
24221       new_inst = T2_OPCODE_BIC;
24222       if (rd == 15)
24223         value = FAIL;
24224       else
24225         value = inverted;
24226       break;
24227
24228     case T2_OPCODE_BIC:
24229       new_inst = T2_OPCODE_AND;
24230       value = inverted;
24231       break;
24232
24233       /* ADC <-> SBC  */
24234     case T2_OPCODE_ADC:
24235       new_inst = T2_OPCODE_SBC;
24236       value = inverted;
24237       break;
24238
24239     case T2_OPCODE_SBC:
24240       new_inst = T2_OPCODE_ADC;
24241       value = inverted;
24242       break;
24243
24244       /* We cannot do anything.  */
24245     default:
24246       return FAIL;
24247     }
24248
24249   if (value == (unsigned int)FAIL)
24250     return FAIL;
24251
24252   *instruction &= T2_OPCODE_MASK;
24253   *instruction |= new_inst << T2_DATA_OP_SHIFT;
24254   return value;
24255 }
24256
24257 /* Read a 32-bit thumb instruction from buf.  */
24258
24259 static unsigned long
24260 get_thumb32_insn (char * buf)
24261 {
24262   unsigned long insn;
24263   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
24264   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
24265
24266   return insn;
24267 }
24268
24269 /* We usually want to set the low bit on the address of thumb function
24270    symbols.  In particular .word foo - . should have the low bit set.
24271    Generic code tries to fold the difference of two symbols to
24272    a constant.  Prevent this and force a relocation when the first symbols
24273    is a thumb function.  */
24274
24275 bfd_boolean
24276 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
24277 {
24278   if (op == O_subtract
24279       && l->X_op == O_symbol
24280       && r->X_op == O_symbol
24281       && THUMB_IS_FUNC (l->X_add_symbol))
24282     {
24283       l->X_op = O_subtract;
24284       l->X_op_symbol = r->X_add_symbol;
24285       l->X_add_number -= r->X_add_number;
24286       return TRUE;
24287     }
24288
24289   /* Process as normal.  */
24290   return FALSE;
24291 }
24292
24293 /* Encode Thumb2 unconditional branches and calls. The encoding
24294    for the 2 are identical for the immediate values.  */
24295
24296 static void
24297 encode_thumb2_b_bl_offset (char * buf, offsetT value)
24298 {
24299 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
24300   offsetT newval;
24301   offsetT newval2;
24302   addressT S, I1, I2, lo, hi;
24303
24304   S = (value >> 24) & 0x01;
24305   I1 = (value >> 23) & 0x01;
24306   I2 = (value >> 22) & 0x01;
24307   hi = (value >> 12) & 0x3ff;
24308   lo = (value >> 1) & 0x7ff;
24309   newval   = md_chars_to_number (buf, THUMB_SIZE);
24310   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
24311   newval  |= (S << 10) | hi;
24312   newval2 &=  ~T2I1I2MASK;
24313   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
24314   md_number_to_chars (buf, newval, THUMB_SIZE);
24315   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
24316 }
24317
24318 void
24319 md_apply_fix (fixS *    fixP,
24320                valueT * valP,
24321                segT     seg)
24322 {
24323   offsetT        value = * valP;
24324   offsetT        newval;
24325   unsigned int   newimm;
24326   unsigned long  temp;
24327   int            sign;
24328   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
24329
24330   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
24331
24332   /* Note whether this will delete the relocation.  */
24333
24334   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
24335     fixP->fx_done = 1;
24336
24337   /* On a 64-bit host, silently truncate 'value' to 32 bits for
24338      consistency with the behaviour on 32-bit hosts.  Remember value
24339      for emit_reloc.  */
24340   value &= 0xffffffff;
24341   value ^= 0x80000000;
24342   value -= 0x80000000;
24343
24344   *valP = value;
24345   fixP->fx_addnumber = value;
24346
24347   /* Same treatment for fixP->fx_offset.  */
24348   fixP->fx_offset &= 0xffffffff;
24349   fixP->fx_offset ^= 0x80000000;
24350   fixP->fx_offset -= 0x80000000;
24351
24352   switch (fixP->fx_r_type)
24353     {
24354     case BFD_RELOC_NONE:
24355       /* This will need to go in the object file.  */
24356       fixP->fx_done = 0;
24357       break;
24358
24359     case BFD_RELOC_ARM_IMMEDIATE:
24360       /* We claim that this fixup has been processed here,
24361          even if in fact we generate an error because we do
24362          not have a reloc for it, so tc_gen_reloc will reject it.  */
24363       fixP->fx_done = 1;
24364
24365       if (fixP->fx_addsy)
24366         {
24367           const char *msg = 0;
24368
24369           if (! S_IS_DEFINED (fixP->fx_addsy))
24370             msg = _("undefined symbol %s used as an immediate value");
24371           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
24372             msg = _("symbol %s is in a different section");
24373           else if (S_IS_WEAK (fixP->fx_addsy))
24374             msg = _("symbol %s is weak and may be overridden later");
24375
24376           if (msg)
24377             {
24378               as_bad_where (fixP->fx_file, fixP->fx_line,
24379                             msg, S_GET_NAME (fixP->fx_addsy));
24380               break;
24381             }
24382         }
24383
24384       temp = md_chars_to_number (buf, INSN_SIZE);
24385
24386       /* If the offset is negative, we should use encoding A2 for ADR.  */
24387       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
24388         newimm = negate_data_op (&temp, value);
24389       else
24390         {
24391           newimm = encode_arm_immediate (value);
24392
24393           /* If the instruction will fail, see if we can fix things up by
24394              changing the opcode.  */
24395           if (newimm == (unsigned int) FAIL)
24396             newimm = negate_data_op (&temp, value);
24397           /* MOV accepts both ARM modified immediate (A1 encoding) and
24398              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
24399              When disassembling, MOV is preferred when there is no encoding
24400              overlap.  */
24401           if (newimm == (unsigned int) FAIL
24402               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
24403               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
24404               && !((temp >> SBIT_SHIFT) & 0x1)
24405               && value >= 0 && value <= 0xffff)
24406             {
24407               /* Clear bits[23:20] to change encoding from A1 to A2.  */
24408               temp &= 0xff0fffff;
24409               /* Encoding high 4bits imm.  Code below will encode the remaining
24410                  low 12bits.  */
24411               temp |= (value & 0x0000f000) << 4;
24412               newimm = value & 0x00000fff;
24413             }
24414         }
24415
24416       if (newimm == (unsigned int) FAIL)
24417         {
24418           as_bad_where (fixP->fx_file, fixP->fx_line,
24419                         _("invalid constant (%lx) after fixup"),
24420                         (unsigned long) value);
24421           break;
24422         }
24423
24424       newimm |= (temp & 0xfffff000);
24425       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
24426       break;
24427
24428     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
24429       {
24430         unsigned int highpart = 0;
24431         unsigned int newinsn  = 0xe1a00000; /* nop.  */
24432
24433         if (fixP->fx_addsy)
24434           {
24435             const char *msg = 0;
24436
24437             if (! S_IS_DEFINED (fixP->fx_addsy))
24438               msg = _("undefined symbol %s used as an immediate value");
24439             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
24440               msg = _("symbol %s is in a different section");
24441             else if (S_IS_WEAK (fixP->fx_addsy))
24442               msg = _("symbol %s is weak and may be overridden later");
24443
24444             if (msg)
24445               {
24446                 as_bad_where (fixP->fx_file, fixP->fx_line,
24447                               msg, S_GET_NAME (fixP->fx_addsy));
24448                 break;
24449               }
24450           }
24451
24452         newimm = encode_arm_immediate (value);
24453         temp = md_chars_to_number (buf, INSN_SIZE);
24454
24455         /* If the instruction will fail, see if we can fix things up by
24456            changing the opcode.  */
24457         if (newimm == (unsigned int) FAIL
24458             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
24459           {
24460             /* No ?  OK - try using two ADD instructions to generate
24461                the value.  */
24462             newimm = validate_immediate_twopart (value, & highpart);
24463
24464             /* Yes - then make sure that the second instruction is
24465                also an add.  */
24466             if (newimm != (unsigned int) FAIL)
24467               newinsn = temp;
24468             /* Still No ?  Try using a negated value.  */
24469             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
24470               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
24471             /* Otherwise - give up.  */
24472             else
24473               {
24474                 as_bad_where (fixP->fx_file, fixP->fx_line,
24475                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
24476                               (long) value);
24477                 break;
24478               }
24479
24480             /* Replace the first operand in the 2nd instruction (which
24481                is the PC) with the destination register.  We have
24482                already added in the PC in the first instruction and we
24483                do not want to do it again.  */
24484             newinsn &= ~ 0xf0000;
24485             newinsn |= ((newinsn & 0x0f000) << 4);
24486           }
24487
24488         newimm |= (temp & 0xfffff000);
24489         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
24490
24491         highpart |= (newinsn & 0xfffff000);
24492         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
24493       }
24494       break;
24495
24496     case BFD_RELOC_ARM_OFFSET_IMM:
24497       if (!fixP->fx_done && seg->use_rela_p)
24498         value = 0;
24499       /* Fall through.  */
24500
24501     case BFD_RELOC_ARM_LITERAL:
24502       sign = value > 0;
24503
24504       if (value < 0)
24505         value = - value;
24506
24507       if (validate_offset_imm (value, 0) == FAIL)
24508         {
24509           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
24510             as_bad_where (fixP->fx_file, fixP->fx_line,
24511                           _("invalid literal constant: pool needs to be closer"));
24512           else
24513             as_bad_where (fixP->fx_file, fixP->fx_line,
24514                           _("bad immediate value for offset (%ld)"),
24515                           (long) value);
24516           break;
24517         }
24518
24519       newval = md_chars_to_number (buf, INSN_SIZE);
24520       if (value == 0)
24521         newval &= 0xfffff000;
24522       else
24523         {
24524           newval &= 0xff7ff000;
24525           newval |= value | (sign ? INDEX_UP : 0);
24526         }
24527       md_number_to_chars (buf, newval, INSN_SIZE);
24528       break;
24529
24530     case BFD_RELOC_ARM_OFFSET_IMM8:
24531     case BFD_RELOC_ARM_HWLITERAL:
24532       sign = value > 0;
24533
24534       if (value < 0)
24535         value = - value;
24536
24537       if (validate_offset_imm (value, 1) == FAIL)
24538         {
24539           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
24540             as_bad_where (fixP->fx_file, fixP->fx_line,
24541                           _("invalid literal constant: pool needs to be closer"));
24542           else
24543             as_bad_where (fixP->fx_file, fixP->fx_line,
24544                           _("bad immediate value for 8-bit offset (%ld)"),
24545                           (long) value);
24546           break;
24547         }
24548
24549       newval = md_chars_to_number (buf, INSN_SIZE);
24550       if (value == 0)
24551         newval &= 0xfffff0f0;
24552       else
24553         {
24554           newval &= 0xff7ff0f0;
24555           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
24556         }
24557       md_number_to_chars (buf, newval, INSN_SIZE);
24558       break;
24559
24560     case BFD_RELOC_ARM_T32_OFFSET_U8:
24561       if (value < 0 || value > 1020 || value % 4 != 0)
24562         as_bad_where (fixP->fx_file, fixP->fx_line,
24563                       _("bad immediate value for offset (%ld)"), (long) value);
24564       value /= 4;
24565
24566       newval = md_chars_to_number (buf+2, THUMB_SIZE);
24567       newval |= value;
24568       md_number_to_chars (buf+2, newval, THUMB_SIZE);
24569       break;
24570
24571     case BFD_RELOC_ARM_T32_OFFSET_IMM:
24572       /* This is a complicated relocation used for all varieties of Thumb32
24573          load/store instruction with immediate offset:
24574
24575          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
24576                                                    *4, optional writeback(W)
24577                                                    (doubleword load/store)
24578
24579          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
24580          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
24581          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
24582          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
24583          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
24584
24585          Uppercase letters indicate bits that are already encoded at
24586          this point.  Lowercase letters are our problem.  For the
24587          second block of instructions, the secondary opcode nybble
24588          (bits 8..11) is present, and bit 23 is zero, even if this is
24589          a PC-relative operation.  */
24590       newval = md_chars_to_number (buf, THUMB_SIZE);
24591       newval <<= 16;
24592       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
24593
24594       if ((newval & 0xf0000000) == 0xe0000000)
24595         {
24596           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
24597           if (value >= 0)
24598             newval |= (1 << 23);
24599           else
24600             value = -value;
24601           if (value % 4 != 0)
24602             {
24603               as_bad_where (fixP->fx_file, fixP->fx_line,
24604                             _("offset not a multiple of 4"));
24605               break;
24606             }
24607           value /= 4;
24608           if (value > 0xff)
24609             {
24610               as_bad_where (fixP->fx_file, fixP->fx_line,
24611                             _("offset out of range"));
24612               break;
24613             }
24614           newval &= ~0xff;
24615         }
24616       else if ((newval & 0x000f0000) == 0x000f0000)
24617         {
24618           /* PC-relative, 12-bit offset.  */
24619           if (value >= 0)
24620             newval |= (1 << 23);
24621           else
24622             value = -value;
24623           if (value > 0xfff)
24624             {
24625               as_bad_where (fixP->fx_file, fixP->fx_line,
24626                             _("offset out of range"));
24627               break;
24628             }
24629           newval &= ~0xfff;
24630         }
24631       else if ((newval & 0x00000100) == 0x00000100)
24632         {
24633           /* Writeback: 8-bit, +/- offset.  */
24634           if (value >= 0)
24635             newval |= (1 << 9);
24636           else
24637             value = -value;
24638           if (value > 0xff)
24639             {
24640               as_bad_where (fixP->fx_file, fixP->fx_line,
24641                             _("offset out of range"));
24642               break;
24643             }
24644           newval &= ~0xff;
24645         }
24646       else if ((newval & 0x00000f00) == 0x00000e00)
24647         {
24648           /* T-instruction: positive 8-bit offset.  */
24649           if (value < 0 || value > 0xff)
24650             {
24651               as_bad_where (fixP->fx_file, fixP->fx_line,
24652                             _("offset out of range"));
24653               break;
24654             }
24655           newval &= ~0xff;
24656           newval |= value;
24657         }
24658       else
24659         {
24660           /* Positive 12-bit or negative 8-bit offset.  */
24661           int limit;
24662           if (value >= 0)
24663             {
24664               newval |= (1 << 23);
24665               limit = 0xfff;
24666             }
24667           else
24668             {
24669               value = -value;
24670               limit = 0xff;
24671             }
24672           if (value > limit)
24673             {
24674               as_bad_where (fixP->fx_file, fixP->fx_line,
24675                             _("offset out of range"));
24676               break;
24677             }
24678           newval &= ~limit;
24679         }
24680
24681       newval |= value;
24682       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
24683       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
24684       break;
24685
24686     case BFD_RELOC_ARM_SHIFT_IMM:
24687       newval = md_chars_to_number (buf, INSN_SIZE);
24688       if (((unsigned long) value) > 32
24689           || (value == 32
24690               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
24691         {
24692           as_bad_where (fixP->fx_file, fixP->fx_line,
24693                         _("shift expression is too large"));
24694           break;
24695         }
24696
24697       if (value == 0)
24698         /* Shifts of zero must be done as lsl.  */
24699         newval &= ~0x60;
24700       else if (value == 32)
24701         value = 0;
24702       newval &= 0xfffff07f;
24703       newval |= (value & 0x1f) << 7;
24704       md_number_to_chars (buf, newval, INSN_SIZE);
24705       break;
24706
24707     case BFD_RELOC_ARM_T32_IMMEDIATE:
24708     case BFD_RELOC_ARM_T32_ADD_IMM:
24709     case BFD_RELOC_ARM_T32_IMM12:
24710     case BFD_RELOC_ARM_T32_ADD_PC12:
24711       /* We claim that this fixup has been processed here,
24712          even if in fact we generate an error because we do
24713          not have a reloc for it, so tc_gen_reloc will reject it.  */
24714       fixP->fx_done = 1;
24715
24716       if (fixP->fx_addsy
24717           && ! S_IS_DEFINED (fixP->fx_addsy))
24718         {
24719           as_bad_where (fixP->fx_file, fixP->fx_line,
24720                         _("undefined symbol %s used as an immediate value"),
24721                         S_GET_NAME (fixP->fx_addsy));
24722           break;
24723         }
24724
24725       newval = md_chars_to_number (buf, THUMB_SIZE);
24726       newval <<= 16;
24727       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
24728
24729       newimm = FAIL;
24730       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
24731            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
24732               Thumb2 modified immediate encoding (T2).  */
24733            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
24734           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
24735         {
24736           newimm = encode_thumb32_immediate (value);
24737           if (newimm == (unsigned int) FAIL)
24738             newimm = thumb32_negate_data_op (&newval, value);
24739         }
24740       if (newimm == (unsigned int) FAIL)
24741         {
24742           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
24743             {
24744               /* Turn add/sum into addw/subw.  */
24745               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
24746                 newval = (newval & 0xfeffffff) | 0x02000000;
24747               /* No flat 12-bit imm encoding for addsw/subsw.  */
24748               if ((newval & 0x00100000) == 0)
24749                 {
24750                   /* 12 bit immediate for addw/subw.  */
24751                   if (value < 0)
24752                     {
24753                       value = -value;
24754                       newval ^= 0x00a00000;
24755                     }
24756                   if (value > 0xfff)
24757                     newimm = (unsigned int) FAIL;
24758                   else
24759                     newimm = value;
24760                 }
24761             }
24762           else
24763             {
24764               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
24765                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
24766                  disassembling, MOV is preferred when there is no encoding
24767                  overlap.  */
24768               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
24769                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
24770                      but with the Rn field [19:16] set to 1111.  */
24771                   && (((newval >> 16) & 0xf) == 0xf)
24772                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
24773                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
24774                   && value >= 0 && value <= 0xffff)
24775                 {
24776                   /* Toggle bit[25] to change encoding from T2 to T3.  */
24777                   newval ^= 1 << 25;
24778                   /* Clear bits[19:16].  */
24779                   newval &= 0xfff0ffff;
24780                   /* Encoding high 4bits imm.  Code below will encode the
24781                      remaining low 12bits.  */
24782                   newval |= (value & 0x0000f000) << 4;
24783                   newimm = value & 0x00000fff;
24784                 }
24785             }
24786         }
24787
24788       if (newimm == (unsigned int)FAIL)
24789         {
24790           as_bad_where (fixP->fx_file, fixP->fx_line,
24791                         _("invalid constant (%lx) after fixup"),
24792                         (unsigned long) value);
24793           break;
24794         }
24795
24796       newval |= (newimm & 0x800) << 15;
24797       newval |= (newimm & 0x700) << 4;
24798       newval |= (newimm & 0x0ff);
24799
24800       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
24801       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
24802       break;
24803
24804     case BFD_RELOC_ARM_SMC:
24805       if (((unsigned long) value) > 0xffff)
24806         as_bad_where (fixP->fx_file, fixP->fx_line,
24807                       _("invalid smc expression"));
24808       newval = md_chars_to_number (buf, INSN_SIZE);
24809       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
24810       md_number_to_chars (buf, newval, INSN_SIZE);
24811       break;
24812
24813     case BFD_RELOC_ARM_HVC:
24814       if (((unsigned long) value) > 0xffff)
24815         as_bad_where (fixP->fx_file, fixP->fx_line,
24816                       _("invalid hvc expression"));
24817       newval = md_chars_to_number (buf, INSN_SIZE);
24818       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
24819       md_number_to_chars (buf, newval, INSN_SIZE);
24820       break;
24821
24822     case BFD_RELOC_ARM_SWI:
24823       if (fixP->tc_fix_data != 0)
24824         {
24825           if (((unsigned long) value) > 0xff)
24826             as_bad_where (fixP->fx_file, fixP->fx_line,
24827                           _("invalid swi expression"));
24828           newval = md_chars_to_number (buf, THUMB_SIZE);
24829           newval |= value;
24830           md_number_to_chars (buf, newval, THUMB_SIZE);
24831         }
24832       else
24833         {
24834           if (((unsigned long) value) > 0x00ffffff)
24835             as_bad_where (fixP->fx_file, fixP->fx_line,
24836                           _("invalid swi expression"));
24837           newval = md_chars_to_number (buf, INSN_SIZE);
24838           newval |= value;
24839           md_number_to_chars (buf, newval, INSN_SIZE);
24840         }
24841       break;
24842
24843     case BFD_RELOC_ARM_MULTI:
24844       if (((unsigned long) value) > 0xffff)
24845         as_bad_where (fixP->fx_file, fixP->fx_line,
24846                       _("invalid expression in load/store multiple"));
24847       newval = value | md_chars_to_number (buf, INSN_SIZE);
24848       md_number_to_chars (buf, newval, INSN_SIZE);
24849       break;
24850
24851 #ifdef OBJ_ELF
24852     case BFD_RELOC_ARM_PCREL_CALL:
24853
24854       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
24855           && fixP->fx_addsy
24856           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
24857           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
24858           && THUMB_IS_FUNC (fixP->fx_addsy))
24859         /* Flip the bl to blx. This is a simple flip
24860            bit here because we generate PCREL_CALL for
24861            unconditional bls.  */
24862         {
24863           newval = md_chars_to_number (buf, INSN_SIZE);
24864           newval = newval | 0x10000000;
24865           md_number_to_chars (buf, newval, INSN_SIZE);
24866           temp = 1;
24867           fixP->fx_done = 1;
24868         }
24869       else
24870         temp = 3;
24871       goto arm_branch_common;
24872
24873     case BFD_RELOC_ARM_PCREL_JUMP:
24874       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
24875           && fixP->fx_addsy
24876           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
24877           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
24878           && THUMB_IS_FUNC (fixP->fx_addsy))
24879         {
24880           /* This would map to a bl<cond>, b<cond>,
24881              b<always> to a Thumb function. We
24882              need to force a relocation for this particular
24883              case.  */
24884           newval = md_chars_to_number (buf, INSN_SIZE);
24885           fixP->fx_done = 0;
24886         }
24887       /* Fall through.  */
24888
24889     case BFD_RELOC_ARM_PLT32:
24890 #endif
24891     case BFD_RELOC_ARM_PCREL_BRANCH:
24892       temp = 3;
24893       goto arm_branch_common;
24894
24895     case BFD_RELOC_ARM_PCREL_BLX:
24896
24897       temp = 1;
24898       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
24899           && fixP->fx_addsy
24900           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
24901           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
24902           && ARM_IS_FUNC (fixP->fx_addsy))
24903         {
24904           /* Flip the blx to a bl and warn.  */
24905           const char *name = S_GET_NAME (fixP->fx_addsy);
24906           newval = 0xeb000000;
24907           as_warn_where (fixP->fx_file, fixP->fx_line,
24908                          _("blx to '%s' an ARM ISA state function changed to bl"),
24909                           name);
24910           md_number_to_chars (buf, newval, INSN_SIZE);
24911           temp = 3;
24912           fixP->fx_done = 1;
24913         }
24914
24915 #ifdef OBJ_ELF
24916        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
24917          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
24918 #endif
24919
24920     arm_branch_common:
24921       /* We are going to store value (shifted right by two) in the
24922          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
24923          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
24924          also be clear.  */
24925       if (value & temp)
24926         as_bad_where (fixP->fx_file, fixP->fx_line,
24927                       _("misaligned branch destination"));
24928       if ((value & (offsetT)0xfe000000) != (offsetT)0
24929           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
24930         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
24931
24932       if (fixP->fx_done || !seg->use_rela_p)
24933         {
24934           newval = md_chars_to_number (buf, INSN_SIZE);
24935           newval |= (value >> 2) & 0x00ffffff;
24936           /* Set the H bit on BLX instructions.  */
24937           if (temp == 1)
24938             {
24939               if (value & 2)
24940                 newval |= 0x01000000;
24941               else
24942                 newval &= ~0x01000000;
24943             }
24944           md_number_to_chars (buf, newval, INSN_SIZE);
24945         }
24946       break;
24947
24948     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
24949       /* CBZ can only branch forward.  */
24950
24951       /* Attempts to use CBZ to branch to the next instruction
24952          (which, strictly speaking, are prohibited) will be turned into
24953          no-ops.
24954
24955          FIXME: It may be better to remove the instruction completely and
24956          perform relaxation.  */
24957       if (value == -2)
24958         {
24959           newval = md_chars_to_number (buf, THUMB_SIZE);
24960           newval = 0xbf00; /* NOP encoding T1 */
24961           md_number_to_chars (buf, newval, THUMB_SIZE);
24962         }
24963       else
24964         {
24965           if (value & ~0x7e)
24966             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
24967
24968           if (fixP->fx_done || !seg->use_rela_p)
24969             {
24970               newval = md_chars_to_number (buf, THUMB_SIZE);
24971               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
24972               md_number_to_chars (buf, newval, THUMB_SIZE);
24973             }
24974         }
24975       break;
24976
24977     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
24978       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
24979         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
24980
24981       if (fixP->fx_done || !seg->use_rela_p)
24982         {
24983           newval = md_chars_to_number (buf, THUMB_SIZE);
24984           newval |= (value & 0x1ff) >> 1;
24985           md_number_to_chars (buf, newval, THUMB_SIZE);
24986         }
24987       break;
24988
24989     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
24990       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
24991         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
24992
24993       if (fixP->fx_done || !seg->use_rela_p)
24994         {
24995           newval = md_chars_to_number (buf, THUMB_SIZE);
24996           newval |= (value & 0xfff) >> 1;
24997           md_number_to_chars (buf, newval, THUMB_SIZE);
24998         }
24999       break;
25000
25001     case BFD_RELOC_THUMB_PCREL_BRANCH20:
25002       if (fixP->fx_addsy
25003           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25004           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25005           && ARM_IS_FUNC (fixP->fx_addsy)
25006           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25007         {
25008           /* Force a relocation for a branch 20 bits wide.  */
25009           fixP->fx_done = 0;
25010         }
25011       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
25012         as_bad_where (fixP->fx_file, fixP->fx_line,
25013                       _("conditional branch out of range"));
25014
25015       if (fixP->fx_done || !seg->use_rela_p)
25016         {
25017           offsetT newval2;
25018           addressT S, J1, J2, lo, hi;
25019
25020           S  = (value & 0x00100000) >> 20;
25021           J2 = (value & 0x00080000) >> 19;
25022           J1 = (value & 0x00040000) >> 18;
25023           hi = (value & 0x0003f000) >> 12;
25024           lo = (value & 0x00000ffe) >> 1;
25025
25026           newval   = md_chars_to_number (buf, THUMB_SIZE);
25027           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25028           newval  |= (S << 10) | hi;
25029           newval2 |= (J1 << 13) | (J2 << 11) | lo;
25030           md_number_to_chars (buf, newval, THUMB_SIZE);
25031           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
25032         }
25033       break;
25034
25035     case BFD_RELOC_THUMB_PCREL_BLX:
25036       /* If there is a blx from a thumb state function to
25037          another thumb function flip this to a bl and warn
25038          about it.  */
25039
25040       if (fixP->fx_addsy
25041           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25042           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25043           && THUMB_IS_FUNC (fixP->fx_addsy))
25044         {
25045           const char *name = S_GET_NAME (fixP->fx_addsy);
25046           as_warn_where (fixP->fx_file, fixP->fx_line,
25047                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
25048                          name);
25049           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25050           newval = newval | 0x1000;
25051           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
25052           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
25053           fixP->fx_done = 1;
25054         }
25055
25056
25057       goto thumb_bl_common;
25058
25059     case BFD_RELOC_THUMB_PCREL_BRANCH23:
25060       /* A bl from Thumb state ISA to an internal ARM state function
25061          is converted to a blx.  */
25062       if (fixP->fx_addsy
25063           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25064           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25065           && ARM_IS_FUNC (fixP->fx_addsy)
25066           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
25067         {
25068           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25069           newval = newval & ~0x1000;
25070           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
25071           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
25072           fixP->fx_done = 1;
25073         }
25074
25075     thumb_bl_common:
25076
25077       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
25078         /* For a BLX instruction, make sure that the relocation is rounded up
25079            to a word boundary.  This follows the semantics of the instruction
25080            which specifies that bit 1 of the target address will come from bit
25081            1 of the base address.  */
25082         value = (value + 3) & ~ 3;
25083
25084 #ifdef OBJ_ELF
25085        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
25086            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
25087          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
25088 #endif
25089
25090       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
25091         {
25092           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
25093             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
25094           else if ((value & ~0x1ffffff)
25095                    && ((value & ~0x1ffffff) != ~0x1ffffff))
25096             as_bad_where (fixP->fx_file, fixP->fx_line,
25097                           _("Thumb2 branch out of range"));
25098         }
25099
25100       if (fixP->fx_done || !seg->use_rela_p)
25101         encode_thumb2_b_bl_offset (buf, value);
25102
25103       break;
25104
25105     case BFD_RELOC_THUMB_PCREL_BRANCH25:
25106       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
25107         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
25108
25109       if (fixP->fx_done || !seg->use_rela_p)
25110           encode_thumb2_b_bl_offset (buf, value);
25111
25112       break;
25113
25114     case BFD_RELOC_8:
25115       if (fixP->fx_done || !seg->use_rela_p)
25116         *buf = value;
25117       break;
25118
25119     case BFD_RELOC_16:
25120       if (fixP->fx_done || !seg->use_rela_p)
25121         md_number_to_chars (buf, value, 2);
25122       break;
25123
25124 #ifdef OBJ_ELF
25125     case BFD_RELOC_ARM_TLS_CALL:
25126     case BFD_RELOC_ARM_THM_TLS_CALL:
25127     case BFD_RELOC_ARM_TLS_DESCSEQ:
25128     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
25129     case BFD_RELOC_ARM_TLS_GOTDESC:
25130     case BFD_RELOC_ARM_TLS_GD32:
25131     case BFD_RELOC_ARM_TLS_LE32:
25132     case BFD_RELOC_ARM_TLS_IE32:
25133     case BFD_RELOC_ARM_TLS_LDM32:
25134     case BFD_RELOC_ARM_TLS_LDO32:
25135       S_SET_THREAD_LOCAL (fixP->fx_addsy);
25136       break;
25137
25138       /* Same handling as above, but with the arm_fdpic guard.  */
25139     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
25140     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
25141     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
25142       if (arm_fdpic)
25143         {
25144           S_SET_THREAD_LOCAL (fixP->fx_addsy);
25145         }
25146       else
25147         {
25148           as_bad_where (fixP->fx_file, fixP->fx_line,
25149                         _("Relocation supported only in FDPIC mode"));
25150         }
25151       break;
25152
25153     case BFD_RELOC_ARM_GOT32:
25154     case BFD_RELOC_ARM_GOTOFF:
25155       break;
25156
25157     case BFD_RELOC_ARM_GOT_PREL:
25158       if (fixP->fx_done || !seg->use_rela_p)
25159         md_number_to_chars (buf, value, 4);
25160       break;
25161
25162     case BFD_RELOC_ARM_TARGET2:
25163       /* TARGET2 is not partial-inplace, so we need to write the
25164          addend here for REL targets, because it won't be written out
25165          during reloc processing later.  */
25166       if (fixP->fx_done || !seg->use_rela_p)
25167         md_number_to_chars (buf, fixP->fx_offset, 4);
25168       break;
25169
25170       /* Relocations for FDPIC.  */
25171     case BFD_RELOC_ARM_GOTFUNCDESC:
25172     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
25173     case BFD_RELOC_ARM_FUNCDESC:
25174       if (arm_fdpic)
25175         {
25176           if (fixP->fx_done || !seg->use_rela_p)
25177             md_number_to_chars (buf, 0, 4);
25178         }
25179       else
25180         {
25181           as_bad_where (fixP->fx_file, fixP->fx_line,
25182                         _("Relocation supported only in FDPIC mode"));
25183       }
25184       break;
25185 #endif
25186
25187     case BFD_RELOC_RVA:
25188     case BFD_RELOC_32:
25189     case BFD_RELOC_ARM_TARGET1:
25190     case BFD_RELOC_ARM_ROSEGREL32:
25191     case BFD_RELOC_ARM_SBREL32:
25192     case BFD_RELOC_32_PCREL:
25193 #ifdef TE_PE
25194     case BFD_RELOC_32_SECREL:
25195 #endif
25196       if (fixP->fx_done || !seg->use_rela_p)
25197 #ifdef TE_WINCE
25198         /* For WinCE we only do this for pcrel fixups.  */
25199         if (fixP->fx_done || fixP->fx_pcrel)
25200 #endif
25201           md_number_to_chars (buf, value, 4);
25202       break;
25203
25204 #ifdef OBJ_ELF
25205     case BFD_RELOC_ARM_PREL31:
25206       if (fixP->fx_done || !seg->use_rela_p)
25207         {
25208           newval = md_chars_to_number (buf, 4) & 0x80000000;
25209           if ((value ^ (value >> 1)) & 0x40000000)
25210             {
25211               as_bad_where (fixP->fx_file, fixP->fx_line,
25212                             _("rel31 relocation overflow"));
25213             }
25214           newval |= value & 0x7fffffff;
25215           md_number_to_chars (buf, newval, 4);
25216         }
25217       break;
25218 #endif
25219
25220     case BFD_RELOC_ARM_CP_OFF_IMM:
25221     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
25222     case BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM:
25223       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
25224         newval = md_chars_to_number (buf, INSN_SIZE);
25225       else
25226         newval = get_thumb32_insn (buf);
25227       if ((newval & 0x0f200f00) == 0x0d000900)
25228         {
25229           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
25230              has permitted values that are multiples of 2, in the range 0
25231              to 510.  */
25232           if (value < -510 || value > 510 || (value & 1))
25233             as_bad_where (fixP->fx_file, fixP->fx_line,
25234                           _("co-processor offset out of range"));
25235         }
25236       else if ((newval & 0xfe001f80) == 0xec000f80)
25237         {
25238           if (value < -511 || value > 512 || (value & 3))
25239             as_bad_where (fixP->fx_file, fixP->fx_line,
25240                           _("co-processor offset out of range"));
25241         }
25242       else if (value < -1023 || value > 1023 || (value & 3))
25243         as_bad_where (fixP->fx_file, fixP->fx_line,
25244                       _("co-processor offset out of range"));
25245     cp_off_common:
25246       sign = value > 0;
25247       if (value < 0)
25248         value = -value;
25249       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
25250           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
25251         newval = md_chars_to_number (buf, INSN_SIZE);
25252       else
25253         newval = get_thumb32_insn (buf);
25254       if (value == 0)
25255         {
25256           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
25257             newval &= 0xffffff80;
25258           else
25259             newval &= 0xffffff00;
25260         }
25261       else
25262         {
25263           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_VLDR_VSTR_OFF_IMM)
25264             newval &= 0xff7fff80;
25265           else
25266             newval &= 0xff7fff00;
25267           if ((newval & 0x0f200f00) == 0x0d000900)
25268             {
25269               /* This is a fp16 vstr/vldr.
25270
25271                  It requires the immediate offset in the instruction is shifted
25272                  left by 1 to be a half-word offset.
25273
25274                  Here, left shift by 1 first, and later right shift by 2
25275                  should get the right offset.  */
25276               value <<= 1;
25277             }
25278           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
25279         }
25280       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
25281           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
25282         md_number_to_chars (buf, newval, INSN_SIZE);
25283       else
25284         put_thumb32_insn (buf, newval);
25285       break;
25286
25287     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
25288     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
25289       if (value < -255 || value > 255)
25290         as_bad_where (fixP->fx_file, fixP->fx_line,
25291                       _("co-processor offset out of range"));
25292       value *= 4;
25293       goto cp_off_common;
25294
25295     case BFD_RELOC_ARM_THUMB_OFFSET:
25296       newval = md_chars_to_number (buf, THUMB_SIZE);
25297       /* Exactly what ranges, and where the offset is inserted depends
25298          on the type of instruction, we can establish this from the
25299          top 4 bits.  */
25300       switch (newval >> 12)
25301         {
25302         case 4: /* PC load.  */
25303           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
25304              forced to zero for these loads; md_pcrel_from has already
25305              compensated for this.  */
25306           if (value & 3)
25307             as_bad_where (fixP->fx_file, fixP->fx_line,
25308                           _("invalid offset, target not word aligned (0x%08lX)"),
25309                           (((unsigned long) fixP->fx_frag->fr_address
25310                             + (unsigned long) fixP->fx_where) & ~3)
25311                           + (unsigned long) value);
25312
25313           if (value & ~0x3fc)
25314             as_bad_where (fixP->fx_file, fixP->fx_line,
25315                           _("invalid offset, value too big (0x%08lX)"),
25316                           (long) value);
25317
25318           newval |= value >> 2;
25319           break;
25320
25321         case 9: /* SP load/store.  */
25322           if (value & ~0x3fc)
25323             as_bad_where (fixP->fx_file, fixP->fx_line,
25324                           _("invalid offset, value too big (0x%08lX)"),
25325                           (long) value);
25326           newval |= value >> 2;
25327           break;
25328
25329         case 6: /* Word load/store.  */
25330           if (value & ~0x7c)
25331             as_bad_where (fixP->fx_file, fixP->fx_line,
25332                           _("invalid offset, value too big (0x%08lX)"),
25333                           (long) value);
25334           newval |= value << 4; /* 6 - 2.  */
25335           break;
25336
25337         case 7: /* Byte load/store.  */
25338           if (value & ~0x1f)
25339             as_bad_where (fixP->fx_file, fixP->fx_line,
25340                           _("invalid offset, value too big (0x%08lX)"),
25341                           (long) value);
25342           newval |= value << 6;
25343           break;
25344
25345         case 8: /* Halfword load/store.  */
25346           if (value & ~0x3e)
25347             as_bad_where (fixP->fx_file, fixP->fx_line,
25348                           _("invalid offset, value too big (0x%08lX)"),
25349                           (long) value);
25350           newval |= value << 5; /* 6 - 1.  */
25351           break;
25352
25353         default:
25354           as_bad_where (fixP->fx_file, fixP->fx_line,
25355                         "Unable to process relocation for thumb opcode: %lx",
25356                         (unsigned long) newval);
25357           break;
25358         }
25359       md_number_to_chars (buf, newval, THUMB_SIZE);
25360       break;
25361
25362     case BFD_RELOC_ARM_THUMB_ADD:
25363       /* This is a complicated relocation, since we use it for all of
25364          the following immediate relocations:
25365
25366             3bit ADD/SUB
25367             8bit ADD/SUB
25368             9bit ADD/SUB SP word-aligned
25369            10bit ADD PC/SP word-aligned
25370
25371          The type of instruction being processed is encoded in the
25372          instruction field:
25373
25374            0x8000  SUB
25375            0x00F0  Rd
25376            0x000F  Rs
25377       */
25378       newval = md_chars_to_number (buf, THUMB_SIZE);
25379       {
25380         int rd = (newval >> 4) & 0xf;
25381         int rs = newval & 0xf;
25382         int subtract = !!(newval & 0x8000);
25383
25384         /* Check for HI regs, only very restricted cases allowed:
25385            Adjusting SP, and using PC or SP to get an address.  */
25386         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
25387             || (rs > 7 && rs != REG_SP && rs != REG_PC))
25388           as_bad_where (fixP->fx_file, fixP->fx_line,
25389                         _("invalid Hi register with immediate"));
25390
25391         /* If value is negative, choose the opposite instruction.  */
25392         if (value < 0)
25393           {
25394             value = -value;
25395             subtract = !subtract;
25396             if (value < 0)
25397               as_bad_where (fixP->fx_file, fixP->fx_line,
25398                             _("immediate value out of range"));
25399           }
25400
25401         if (rd == REG_SP)
25402           {
25403             if (value & ~0x1fc)
25404               as_bad_where (fixP->fx_file, fixP->fx_line,
25405                             _("invalid immediate for stack address calculation"));
25406             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
25407             newval |= value >> 2;
25408           }
25409         else if (rs == REG_PC || rs == REG_SP)
25410           {
25411             /* PR gas/18541.  If the addition is for a defined symbol
25412                within range of an ADR instruction then accept it.  */
25413             if (subtract
25414                 && value == 4
25415                 && fixP->fx_addsy != NULL)
25416               {
25417                 subtract = 0;
25418
25419                 if (! S_IS_DEFINED (fixP->fx_addsy)
25420                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
25421                     || S_IS_WEAK (fixP->fx_addsy))
25422                   {
25423                     as_bad_where (fixP->fx_file, fixP->fx_line,
25424                                   _("address calculation needs a strongly defined nearby symbol"));
25425                   }
25426                 else
25427                   {
25428                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
25429
25430                     /* Round up to the next 4-byte boundary.  */
25431                     if (v & 3)
25432                       v = (v + 3) & ~ 3;
25433                     else
25434                       v += 4;
25435                     v = S_GET_VALUE (fixP->fx_addsy) - v;
25436
25437                     if (v & ~0x3fc)
25438                       {
25439                         as_bad_where (fixP->fx_file, fixP->fx_line,
25440                                       _("symbol too far away"));
25441                       }
25442                     else
25443                       {
25444                         fixP->fx_done = 1;
25445                         value = v;
25446                       }
25447                   }
25448               }
25449
25450             if (subtract || value & ~0x3fc)
25451               as_bad_where (fixP->fx_file, fixP->fx_line,
25452                             _("invalid immediate for address calculation (value = 0x%08lX)"),
25453                             (unsigned long) (subtract ? - value : value));
25454             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
25455             newval |= rd << 8;
25456             newval |= value >> 2;
25457           }
25458         else if (rs == rd)
25459           {
25460             if (value & ~0xff)
25461               as_bad_where (fixP->fx_file, fixP->fx_line,
25462                             _("immediate value out of range"));
25463             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
25464             newval |= (rd << 8) | value;
25465           }
25466         else
25467           {
25468             if (value & ~0x7)
25469               as_bad_where (fixP->fx_file, fixP->fx_line,
25470                             _("immediate value out of range"));
25471             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
25472             newval |= rd | (rs << 3) | (value << 6);
25473           }
25474       }
25475       md_number_to_chars (buf, newval, THUMB_SIZE);
25476       break;
25477
25478     case BFD_RELOC_ARM_THUMB_IMM:
25479       newval = md_chars_to_number (buf, THUMB_SIZE);
25480       if (value < 0 || value > 255)
25481         as_bad_where (fixP->fx_file, fixP->fx_line,
25482                       _("invalid immediate: %ld is out of range"),
25483                       (long) value);
25484       newval |= value;
25485       md_number_to_chars (buf, newval, THUMB_SIZE);
25486       break;
25487
25488     case BFD_RELOC_ARM_THUMB_SHIFT:
25489       /* 5bit shift value (0..32).  LSL cannot take 32.  */
25490       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
25491       temp = newval & 0xf800;
25492       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
25493         as_bad_where (fixP->fx_file, fixP->fx_line,
25494                       _("invalid shift value: %ld"), (long) value);
25495       /* Shifts of zero must be encoded as LSL.  */
25496       if (value == 0)
25497         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
25498       /* Shifts of 32 are encoded as zero.  */
25499       else if (value == 32)
25500         value = 0;
25501       newval |= value << 6;
25502       md_number_to_chars (buf, newval, THUMB_SIZE);
25503       break;
25504
25505     case BFD_RELOC_VTABLE_INHERIT:
25506     case BFD_RELOC_VTABLE_ENTRY:
25507       fixP->fx_done = 0;
25508       return;
25509
25510     case BFD_RELOC_ARM_MOVW:
25511     case BFD_RELOC_ARM_MOVT:
25512     case BFD_RELOC_ARM_THUMB_MOVW:
25513     case BFD_RELOC_ARM_THUMB_MOVT:
25514       if (fixP->fx_done || !seg->use_rela_p)
25515         {
25516           /* REL format relocations are limited to a 16-bit addend.  */
25517           if (!fixP->fx_done)
25518             {
25519               if (value < -0x8000 || value > 0x7fff)
25520                   as_bad_where (fixP->fx_file, fixP->fx_line,
25521                                 _("offset out of range"));
25522             }
25523           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
25524                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
25525             {
25526               value >>= 16;
25527             }
25528
25529           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
25530               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
25531             {
25532               newval = get_thumb32_insn (buf);
25533               newval &= 0xfbf08f00;
25534               newval |= (value & 0xf000) << 4;
25535               newval |= (value & 0x0800) << 15;
25536               newval |= (value & 0x0700) << 4;
25537               newval |= (value & 0x00ff);
25538               put_thumb32_insn (buf, newval);
25539             }
25540           else
25541             {
25542               newval = md_chars_to_number (buf, 4);
25543               newval &= 0xfff0f000;
25544               newval |= value & 0x0fff;
25545               newval |= (value & 0xf000) << 4;
25546               md_number_to_chars (buf, newval, 4);
25547             }
25548         }
25549       return;
25550
25551    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
25552    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
25553    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
25554    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
25555       gas_assert (!fixP->fx_done);
25556       {
25557         bfd_vma insn;
25558         bfd_boolean is_mov;
25559         bfd_vma encoded_addend = value;
25560
25561         /* Check that addend can be encoded in instruction.  */
25562         if (!seg->use_rela_p && (value < 0 || value > 255))
25563           as_bad_where (fixP->fx_file, fixP->fx_line,
25564                         _("the offset 0x%08lX is not representable"),
25565                         (unsigned long) encoded_addend);
25566
25567         /* Extract the instruction.  */
25568         insn = md_chars_to_number (buf, THUMB_SIZE);
25569         is_mov = (insn & 0xf800) == 0x2000;
25570
25571         /* Encode insn.  */
25572         if (is_mov)
25573           {
25574             if (!seg->use_rela_p)
25575               insn |= encoded_addend;
25576           }
25577         else
25578           {
25579             int rd, rs;
25580
25581             /* Extract the instruction.  */
25582              /* Encoding is the following
25583                 0x8000  SUB
25584                 0x00F0  Rd
25585                 0x000F  Rs
25586              */
25587              /* The following conditions must be true :
25588                 - ADD
25589                 - Rd == Rs
25590                 - Rd <= 7
25591              */
25592             rd = (insn >> 4) & 0xf;
25593             rs = insn & 0xf;
25594             if ((insn & 0x8000) || (rd != rs) || rd > 7)
25595               as_bad_where (fixP->fx_file, fixP->fx_line,
25596                         _("Unable to process relocation for thumb opcode: %lx"),
25597                         (unsigned long) insn);
25598
25599             /* Encode as ADD immediate8 thumb 1 code.  */
25600             insn = 0x3000 | (rd << 8);
25601
25602             /* Place the encoded addend into the first 8 bits of the
25603                instruction.  */
25604             if (!seg->use_rela_p)
25605               insn |= encoded_addend;
25606           }
25607
25608         /* Update the instruction.  */
25609         md_number_to_chars (buf, insn, THUMB_SIZE);
25610       }
25611       break;
25612
25613    case BFD_RELOC_ARM_ALU_PC_G0_NC:
25614    case BFD_RELOC_ARM_ALU_PC_G0:
25615    case BFD_RELOC_ARM_ALU_PC_G1_NC:
25616    case BFD_RELOC_ARM_ALU_PC_G1:
25617    case BFD_RELOC_ARM_ALU_PC_G2:
25618    case BFD_RELOC_ARM_ALU_SB_G0_NC:
25619    case BFD_RELOC_ARM_ALU_SB_G0:
25620    case BFD_RELOC_ARM_ALU_SB_G1_NC:
25621    case BFD_RELOC_ARM_ALU_SB_G1:
25622    case BFD_RELOC_ARM_ALU_SB_G2:
25623      gas_assert (!fixP->fx_done);
25624      if (!seg->use_rela_p)
25625        {
25626          bfd_vma insn;
25627          bfd_vma encoded_addend;
25628          bfd_vma addend_abs = llabs (value);
25629
25630          /* Check that the absolute value of the addend can be
25631             expressed as an 8-bit constant plus a rotation.  */
25632          encoded_addend = encode_arm_immediate (addend_abs);
25633          if (encoded_addend == (unsigned int) FAIL)
25634            as_bad_where (fixP->fx_file, fixP->fx_line,
25635                          _("the offset 0x%08lX is not representable"),
25636                          (unsigned long) addend_abs);
25637
25638          /* Extract the instruction.  */
25639          insn = md_chars_to_number (buf, INSN_SIZE);
25640
25641          /* If the addend is positive, use an ADD instruction.
25642             Otherwise use a SUB.  Take care not to destroy the S bit.  */
25643          insn &= 0xff1fffff;
25644          if (value < 0)
25645            insn |= 1 << 22;
25646          else
25647            insn |= 1 << 23;
25648
25649          /* Place the encoded addend into the first 12 bits of the
25650             instruction.  */
25651          insn &= 0xfffff000;
25652          insn |= encoded_addend;
25653
25654          /* Update the instruction.  */
25655          md_number_to_chars (buf, insn, INSN_SIZE);
25656        }
25657      break;
25658
25659     case BFD_RELOC_ARM_LDR_PC_G0:
25660     case BFD_RELOC_ARM_LDR_PC_G1:
25661     case BFD_RELOC_ARM_LDR_PC_G2:
25662     case BFD_RELOC_ARM_LDR_SB_G0:
25663     case BFD_RELOC_ARM_LDR_SB_G1:
25664     case BFD_RELOC_ARM_LDR_SB_G2:
25665       gas_assert (!fixP->fx_done);
25666       if (!seg->use_rela_p)
25667         {
25668           bfd_vma insn;
25669           bfd_vma addend_abs = llabs (value);
25670
25671           /* Check that the absolute value of the addend can be
25672              encoded in 12 bits.  */
25673           if (addend_abs >= 0x1000)
25674             as_bad_where (fixP->fx_file, fixP->fx_line,
25675                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
25676                           (unsigned long) addend_abs);
25677
25678           /* Extract the instruction.  */
25679           insn = md_chars_to_number (buf, INSN_SIZE);
25680
25681           /* If the addend is negative, clear bit 23 of the instruction.
25682              Otherwise set it.  */
25683           if (value < 0)
25684             insn &= ~(1 << 23);
25685           else
25686             insn |= 1 << 23;
25687
25688           /* Place the absolute value of the addend into the first 12 bits
25689              of the instruction.  */
25690           insn &= 0xfffff000;
25691           insn |= addend_abs;
25692
25693           /* Update the instruction.  */
25694           md_number_to_chars (buf, insn, INSN_SIZE);
25695         }
25696       break;
25697
25698     case BFD_RELOC_ARM_LDRS_PC_G0:
25699     case BFD_RELOC_ARM_LDRS_PC_G1:
25700     case BFD_RELOC_ARM_LDRS_PC_G2:
25701     case BFD_RELOC_ARM_LDRS_SB_G0:
25702     case BFD_RELOC_ARM_LDRS_SB_G1:
25703     case BFD_RELOC_ARM_LDRS_SB_G2:
25704       gas_assert (!fixP->fx_done);
25705       if (!seg->use_rela_p)
25706         {
25707           bfd_vma insn;
25708           bfd_vma addend_abs = llabs (value);
25709
25710           /* Check that the absolute value of the addend can be
25711              encoded in 8 bits.  */
25712           if (addend_abs >= 0x100)
25713             as_bad_where (fixP->fx_file, fixP->fx_line,
25714                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
25715                           (unsigned long) addend_abs);
25716
25717           /* Extract the instruction.  */
25718           insn = md_chars_to_number (buf, INSN_SIZE);
25719
25720           /* If the addend is negative, clear bit 23 of the instruction.
25721              Otherwise set it.  */
25722           if (value < 0)
25723             insn &= ~(1 << 23);
25724           else
25725             insn |= 1 << 23;
25726
25727           /* Place the first four bits of the absolute value of the addend
25728              into the first 4 bits of the instruction, and the remaining
25729              four into bits 8 .. 11.  */
25730           insn &= 0xfffff0f0;
25731           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
25732
25733           /* Update the instruction.  */
25734           md_number_to_chars (buf, insn, INSN_SIZE);
25735         }
25736       break;
25737
25738     case BFD_RELOC_ARM_LDC_PC_G0:
25739     case BFD_RELOC_ARM_LDC_PC_G1:
25740     case BFD_RELOC_ARM_LDC_PC_G2:
25741     case BFD_RELOC_ARM_LDC_SB_G0:
25742     case BFD_RELOC_ARM_LDC_SB_G1:
25743     case BFD_RELOC_ARM_LDC_SB_G2:
25744       gas_assert (!fixP->fx_done);
25745       if (!seg->use_rela_p)
25746         {
25747           bfd_vma insn;
25748           bfd_vma addend_abs = llabs (value);
25749
25750           /* Check that the absolute value of the addend is a multiple of
25751              four and, when divided by four, fits in 8 bits.  */
25752           if (addend_abs & 0x3)
25753             as_bad_where (fixP->fx_file, fixP->fx_line,
25754                           _("bad offset 0x%08lX (must be word-aligned)"),
25755                           (unsigned long) addend_abs);
25756
25757           if ((addend_abs >> 2) > 0xff)
25758             as_bad_where (fixP->fx_file, fixP->fx_line,
25759                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
25760                           (unsigned long) addend_abs);
25761
25762           /* Extract the instruction.  */
25763           insn = md_chars_to_number (buf, INSN_SIZE);
25764
25765           /* If the addend is negative, clear bit 23 of the instruction.
25766              Otherwise set it.  */
25767           if (value < 0)
25768             insn &= ~(1 << 23);
25769           else
25770             insn |= 1 << 23;
25771
25772           /* Place the addend (divided by four) into the first eight
25773              bits of the instruction.  */
25774           insn &= 0xfffffff0;
25775           insn |= addend_abs >> 2;
25776
25777           /* Update the instruction.  */
25778           md_number_to_chars (buf, insn, INSN_SIZE);
25779         }
25780       break;
25781
25782     case BFD_RELOC_THUMB_PCREL_BRANCH5:
25783       if (fixP->fx_addsy
25784           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25785           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25786           && ARM_IS_FUNC (fixP->fx_addsy)
25787           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25788         {
25789           /* Force a relocation for a branch 5 bits wide.  */
25790           fixP->fx_done = 0;
25791         }
25792       if (v8_1_branch_value_check (value, 5, FALSE) == FAIL)
25793         as_bad_where (fixP->fx_file, fixP->fx_line,
25794                       BAD_BRANCH_OFF);
25795
25796       if (fixP->fx_done || !seg->use_rela_p)
25797         {
25798           addressT boff = value >> 1;
25799
25800           newval  = md_chars_to_number (buf, THUMB_SIZE);
25801           newval |= (boff << 7);
25802           md_number_to_chars (buf, newval, THUMB_SIZE);
25803         }
25804       break;
25805
25806     case BFD_RELOC_THUMB_PCREL_BFCSEL:
25807       if (fixP->fx_addsy
25808           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25809           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25810           && ARM_IS_FUNC (fixP->fx_addsy)
25811           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25812         {
25813           fixP->fx_done = 0;
25814         }
25815       if ((value & ~0x7f) && ((value & ~0x3f) != ~0x3f))
25816         as_bad_where (fixP->fx_file, fixP->fx_line,
25817                       _("branch out of range"));
25818
25819       if (fixP->fx_done || !seg->use_rela_p)
25820         {
25821           newval  = md_chars_to_number (buf, THUMB_SIZE);
25822
25823           addressT boff = ((newval & 0x0780) >> 7) << 1;
25824           addressT diff = value - boff;
25825
25826           if (diff == 4)
25827             {
25828               newval |= 1 << 1; /* T bit.  */
25829             }
25830           else if (diff != 2)
25831             {
25832               as_bad_where (fixP->fx_file, fixP->fx_line,
25833                             _("out of range label-relative fixup value"));
25834             }
25835           md_number_to_chars (buf, newval, THUMB_SIZE);
25836         }
25837       break;
25838
25839     case BFD_RELOC_ARM_THUMB_BF17:
25840       if (fixP->fx_addsy
25841           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25842           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25843           && ARM_IS_FUNC (fixP->fx_addsy)
25844           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25845         {
25846           /* Force a relocation for a branch 17 bits wide.  */
25847           fixP->fx_done = 0;
25848         }
25849
25850       if (v8_1_branch_value_check (value, 17, TRUE) == FAIL)
25851         as_bad_where (fixP->fx_file, fixP->fx_line,
25852                       BAD_BRANCH_OFF);
25853
25854       if (fixP->fx_done || !seg->use_rela_p)
25855         {
25856           offsetT newval2;
25857           addressT immA, immB, immC;
25858
25859           immA = (value & 0x0001f000) >> 12;
25860           immB = (value & 0x00000ffc) >> 2;
25861           immC = (value & 0x00000002) >> 1;
25862
25863           newval   = md_chars_to_number (buf, THUMB_SIZE);
25864           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25865           newval  |= immA;
25866           newval2 |= (immC << 11) | (immB << 1);
25867           md_number_to_chars (buf, newval, THUMB_SIZE);
25868           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
25869         }
25870       break;
25871
25872     case BFD_RELOC_ARM_THUMB_BF19:
25873       if (fixP->fx_addsy
25874           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25875           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25876           && ARM_IS_FUNC (fixP->fx_addsy)
25877           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25878         {
25879           /* Force a relocation for a branch 19 bits wide.  */
25880           fixP->fx_done = 0;
25881         }
25882
25883       if (v8_1_branch_value_check (value, 19, TRUE) == FAIL)
25884         as_bad_where (fixP->fx_file, fixP->fx_line,
25885                       BAD_BRANCH_OFF);
25886
25887       if (fixP->fx_done || !seg->use_rela_p)
25888         {
25889           offsetT newval2;
25890           addressT immA, immB, immC;
25891
25892           immA = (value & 0x0007f000) >> 12;
25893           immB = (value & 0x00000ffc) >> 2;
25894           immC = (value & 0x00000002) >> 1;
25895
25896           newval   = md_chars_to_number (buf, THUMB_SIZE);
25897           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25898           newval  |= immA;
25899           newval2 |= (immC << 11) | (immB << 1);
25900           md_number_to_chars (buf, newval, THUMB_SIZE);
25901           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
25902         }
25903       break;
25904
25905     case BFD_RELOC_ARM_THUMB_BF13:
25906       if (fixP->fx_addsy
25907           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25908           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25909           && ARM_IS_FUNC (fixP->fx_addsy)
25910           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25911         {
25912           /* Force a relocation for a branch 13 bits wide.  */
25913           fixP->fx_done = 0;
25914         }
25915
25916       if (v8_1_branch_value_check (value, 13, TRUE) == FAIL)
25917         as_bad_where (fixP->fx_file, fixP->fx_line,
25918                       BAD_BRANCH_OFF);
25919
25920       if (fixP->fx_done || !seg->use_rela_p)
25921         {
25922           offsetT newval2;
25923           addressT immA, immB, immC;
25924
25925           immA = (value & 0x00001000) >> 12;
25926           immB = (value & 0x00000ffc) >> 2;
25927           immC = (value & 0x00000002) >> 1;
25928
25929           newval   = md_chars_to_number (buf, THUMB_SIZE);
25930           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25931           newval  |= immA;
25932           newval2 |= (immC << 11) | (immB << 1);
25933           md_number_to_chars (buf, newval, THUMB_SIZE);
25934           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
25935         }
25936       break;
25937
25938     case BFD_RELOC_ARM_THUMB_LOOP12:
25939       if (fixP->fx_addsy
25940           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
25941           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
25942           && ARM_IS_FUNC (fixP->fx_addsy)
25943           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v8_1m_main))
25944         {
25945           /* Force a relocation for a branch 12 bits wide.  */
25946           fixP->fx_done = 0;
25947         }
25948
25949       bfd_vma insn = get_thumb32_insn (buf);
25950       /* le lr, <label> or le <label> */
25951       if (((insn & 0xffffffff) == 0xf00fc001)
25952           || ((insn & 0xffffffff) == 0xf02fc001))
25953         value = -value;
25954
25955       if (v8_1_branch_value_check (value, 12, FALSE) == FAIL)
25956         as_bad_where (fixP->fx_file, fixP->fx_line,
25957                       BAD_BRANCH_OFF);
25958       if (fixP->fx_done || !seg->use_rela_p)
25959         {
25960           addressT imml, immh;
25961
25962           immh = (value & 0x00000ffc) >> 2;
25963           imml = (value & 0x00000002) >> 1;
25964
25965           newval  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
25966           newval |= (imml << 11) | (immh << 1);
25967           md_number_to_chars (buf + THUMB_SIZE, newval, THUMB_SIZE);
25968         }
25969       break;
25970
25971     case BFD_RELOC_ARM_V4BX:
25972       /* This will need to go in the object file.  */
25973       fixP->fx_done = 0;
25974       break;
25975
25976     case BFD_RELOC_UNUSED:
25977     default:
25978       as_bad_where (fixP->fx_file, fixP->fx_line,
25979                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
25980     }
25981 }
25982
25983 /* Translate internal representation of relocation info to BFD target
25984    format.  */
25985
25986 arelent *
25987 tc_gen_reloc (asection *section, fixS *fixp)
25988 {
25989   arelent * reloc;
25990   bfd_reloc_code_real_type code;
25991
25992   reloc = XNEW (arelent);
25993
25994   reloc->sym_ptr_ptr = XNEW (asymbol *);
25995   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
25996   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
25997
25998   if (fixp->fx_pcrel)
25999     {
26000       if (section->use_rela_p)
26001         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
26002       else
26003         fixp->fx_offset = reloc->address;
26004     }
26005   reloc->addend = fixp->fx_offset;
26006
26007   switch (fixp->fx_r_type)
26008     {
26009     case BFD_RELOC_8:
26010       if (fixp->fx_pcrel)
26011         {
26012           code = BFD_RELOC_8_PCREL;
26013           break;
26014         }
26015       /* Fall through.  */
26016
26017     case BFD_RELOC_16:
26018       if (fixp->fx_pcrel)
26019         {
26020           code = BFD_RELOC_16_PCREL;
26021           break;
26022         }
26023       /* Fall through.  */
26024
26025     case BFD_RELOC_32:
26026       if (fixp->fx_pcrel)
26027         {
26028           code = BFD_RELOC_32_PCREL;
26029           break;
26030         }
26031       /* Fall through.  */
26032
26033     case BFD_RELOC_ARM_MOVW:
26034       if (fixp->fx_pcrel)
26035         {
26036           code = BFD_RELOC_ARM_MOVW_PCREL;
26037           break;
26038         }
26039       /* Fall through.  */
26040
26041     case BFD_RELOC_ARM_MOVT:
26042       if (fixp->fx_pcrel)
26043         {
26044           code = BFD_RELOC_ARM_MOVT_PCREL;
26045           break;
26046         }
26047       /* Fall through.  */
26048
26049     case BFD_RELOC_ARM_THUMB_MOVW:
26050       if (fixp->fx_pcrel)
26051         {
26052           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
26053           break;
26054         }
26055       /* Fall through.  */
26056
26057     case BFD_RELOC_ARM_THUMB_MOVT:
26058       if (fixp->fx_pcrel)
26059         {
26060           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
26061           break;
26062         }
26063       /* Fall through.  */
26064
26065     case BFD_RELOC_NONE:
26066     case BFD_RELOC_ARM_PCREL_BRANCH:
26067     case BFD_RELOC_ARM_PCREL_BLX:
26068     case BFD_RELOC_RVA:
26069     case BFD_RELOC_THUMB_PCREL_BRANCH7:
26070     case BFD_RELOC_THUMB_PCREL_BRANCH9:
26071     case BFD_RELOC_THUMB_PCREL_BRANCH12:
26072     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26073     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26074     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26075     case BFD_RELOC_VTABLE_ENTRY:
26076     case BFD_RELOC_VTABLE_INHERIT:
26077 #ifdef TE_PE
26078     case BFD_RELOC_32_SECREL:
26079 #endif
26080       code = fixp->fx_r_type;
26081       break;
26082
26083     case BFD_RELOC_THUMB_PCREL_BLX:
26084 #ifdef OBJ_ELF
26085       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
26086         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
26087       else
26088 #endif
26089         code = BFD_RELOC_THUMB_PCREL_BLX;
26090       break;
26091
26092     case BFD_RELOC_ARM_LITERAL:
26093     case BFD_RELOC_ARM_HWLITERAL:
26094       /* If this is called then the a literal has
26095          been referenced across a section boundary.  */
26096       as_bad_where (fixp->fx_file, fixp->fx_line,
26097                     _("literal referenced across section boundary"));
26098       return NULL;
26099
26100 #ifdef OBJ_ELF
26101     case BFD_RELOC_ARM_TLS_CALL:
26102     case BFD_RELOC_ARM_THM_TLS_CALL:
26103     case BFD_RELOC_ARM_TLS_DESCSEQ:
26104     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
26105     case BFD_RELOC_ARM_GOT32:
26106     case BFD_RELOC_ARM_GOTOFF:
26107     case BFD_RELOC_ARM_GOT_PREL:
26108     case BFD_RELOC_ARM_PLT32:
26109     case BFD_RELOC_ARM_TARGET1:
26110     case BFD_RELOC_ARM_ROSEGREL32:
26111     case BFD_RELOC_ARM_SBREL32:
26112     case BFD_RELOC_ARM_PREL31:
26113     case BFD_RELOC_ARM_TARGET2:
26114     case BFD_RELOC_ARM_TLS_LDO32:
26115     case BFD_RELOC_ARM_PCREL_CALL:
26116     case BFD_RELOC_ARM_PCREL_JUMP:
26117     case BFD_RELOC_ARM_ALU_PC_G0_NC:
26118     case BFD_RELOC_ARM_ALU_PC_G0:
26119     case BFD_RELOC_ARM_ALU_PC_G1_NC:
26120     case BFD_RELOC_ARM_ALU_PC_G1:
26121     case BFD_RELOC_ARM_ALU_PC_G2:
26122     case BFD_RELOC_ARM_LDR_PC_G0:
26123     case BFD_RELOC_ARM_LDR_PC_G1:
26124     case BFD_RELOC_ARM_LDR_PC_G2:
26125     case BFD_RELOC_ARM_LDRS_PC_G0:
26126     case BFD_RELOC_ARM_LDRS_PC_G1:
26127     case BFD_RELOC_ARM_LDRS_PC_G2:
26128     case BFD_RELOC_ARM_LDC_PC_G0:
26129     case BFD_RELOC_ARM_LDC_PC_G1:
26130     case BFD_RELOC_ARM_LDC_PC_G2:
26131     case BFD_RELOC_ARM_ALU_SB_G0_NC:
26132     case BFD_RELOC_ARM_ALU_SB_G0:
26133     case BFD_RELOC_ARM_ALU_SB_G1_NC:
26134     case BFD_RELOC_ARM_ALU_SB_G1:
26135     case BFD_RELOC_ARM_ALU_SB_G2:
26136     case BFD_RELOC_ARM_LDR_SB_G0:
26137     case BFD_RELOC_ARM_LDR_SB_G1:
26138     case BFD_RELOC_ARM_LDR_SB_G2:
26139     case BFD_RELOC_ARM_LDRS_SB_G0:
26140     case BFD_RELOC_ARM_LDRS_SB_G1:
26141     case BFD_RELOC_ARM_LDRS_SB_G2:
26142     case BFD_RELOC_ARM_LDC_SB_G0:
26143     case BFD_RELOC_ARM_LDC_SB_G1:
26144     case BFD_RELOC_ARM_LDC_SB_G2:
26145     case BFD_RELOC_ARM_V4BX:
26146     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
26147     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
26148     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
26149     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
26150     case BFD_RELOC_ARM_GOTFUNCDESC:
26151     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
26152     case BFD_RELOC_ARM_FUNCDESC:
26153     case BFD_RELOC_ARM_THUMB_BF17:
26154     case BFD_RELOC_ARM_THUMB_BF19:
26155     case BFD_RELOC_ARM_THUMB_BF13:
26156       code = fixp->fx_r_type;
26157       break;
26158
26159     case BFD_RELOC_ARM_TLS_GOTDESC:
26160     case BFD_RELOC_ARM_TLS_GD32:
26161     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
26162     case BFD_RELOC_ARM_TLS_LE32:
26163     case BFD_RELOC_ARM_TLS_IE32:
26164     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
26165     case BFD_RELOC_ARM_TLS_LDM32:
26166     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
26167       /* BFD will include the symbol's address in the addend.
26168          But we don't want that, so subtract it out again here.  */
26169       if (!S_IS_COMMON (fixp->fx_addsy))
26170         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
26171       code = fixp->fx_r_type;
26172       break;
26173 #endif
26174
26175     case BFD_RELOC_ARM_IMMEDIATE:
26176       as_bad_where (fixp->fx_file, fixp->fx_line,
26177                     _("internal relocation (type: IMMEDIATE) not fixed up"));
26178       return NULL;
26179
26180     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
26181       as_bad_where (fixp->fx_file, fixp->fx_line,
26182                     _("ADRL used for a symbol not defined in the same file"));
26183       return NULL;
26184
26185     case BFD_RELOC_THUMB_PCREL_BRANCH5:
26186     case BFD_RELOC_THUMB_PCREL_BFCSEL:
26187     case BFD_RELOC_ARM_THUMB_LOOP12:
26188       as_bad_where (fixp->fx_file, fixp->fx_line,
26189                     _("%s used for a symbol not defined in the same file"),
26190                     bfd_get_reloc_code_name (fixp->fx_r_type));
26191       return NULL;
26192
26193     case BFD_RELOC_ARM_OFFSET_IMM:
26194       if (section->use_rela_p)
26195         {
26196           code = fixp->fx_r_type;
26197           break;
26198         }
26199
26200       if (fixp->fx_addsy != NULL
26201           && !S_IS_DEFINED (fixp->fx_addsy)
26202           && S_IS_LOCAL (fixp->fx_addsy))
26203         {
26204           as_bad_where (fixp->fx_file, fixp->fx_line,
26205                         _("undefined local label `%s'"),
26206                         S_GET_NAME (fixp->fx_addsy));
26207           return NULL;
26208         }
26209
26210       as_bad_where (fixp->fx_file, fixp->fx_line,
26211                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
26212       return NULL;
26213
26214     default:
26215       {
26216         const char * type;
26217
26218         switch (fixp->fx_r_type)
26219           {
26220           case BFD_RELOC_NONE:             type = "NONE";         break;
26221           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
26222           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
26223           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
26224           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
26225           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
26226           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
26227           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
26228           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
26229           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
26230           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
26231           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
26232           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
26233           default:                         type = _("<unknown>"); break;
26234           }
26235         as_bad_where (fixp->fx_file, fixp->fx_line,
26236                       _("cannot represent %s relocation in this object file format"),
26237                       type);
26238         return NULL;
26239       }
26240     }
26241
26242 #ifdef OBJ_ELF
26243   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
26244       && GOT_symbol
26245       && fixp->fx_addsy == GOT_symbol)
26246     {
26247       code = BFD_RELOC_ARM_GOTPC;
26248       reloc->addend = fixp->fx_offset = reloc->address;
26249     }
26250 #endif
26251
26252   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
26253
26254   if (reloc->howto == NULL)
26255     {
26256       as_bad_where (fixp->fx_file, fixp->fx_line,
26257                     _("cannot represent %s relocation in this object file format"),
26258                     bfd_get_reloc_code_name (code));
26259       return NULL;
26260     }
26261
26262   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
26263      vtable entry to be used in the relocation's section offset.  */
26264   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
26265     reloc->address = fixp->fx_offset;
26266
26267   return reloc;
26268 }
26269
26270 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
26271
26272 void
26273 cons_fix_new_arm (fragS *       frag,
26274                   int           where,
26275                   int           size,
26276                   expressionS * exp,
26277                   bfd_reloc_code_real_type reloc)
26278 {
26279   int pcrel = 0;
26280
26281   /* Pick a reloc.
26282      FIXME: @@ Should look at CPU word size.  */
26283   switch (size)
26284     {
26285     case 1:
26286       reloc = BFD_RELOC_8;
26287       break;
26288     case 2:
26289       reloc = BFD_RELOC_16;
26290       break;
26291     case 4:
26292     default:
26293       reloc = BFD_RELOC_32;
26294       break;
26295     case 8:
26296       reloc = BFD_RELOC_64;
26297       break;
26298     }
26299
26300 #ifdef TE_PE
26301   if (exp->X_op == O_secrel)
26302   {
26303     exp->X_op = O_symbol;
26304     reloc = BFD_RELOC_32_SECREL;
26305   }
26306 #endif
26307
26308   fix_new_exp (frag, where, size, exp, pcrel, reloc);
26309 }
26310
26311 #if defined (OBJ_COFF)
26312 void
26313 arm_validate_fix (fixS * fixP)
26314 {
26315   /* If the destination of the branch is a defined symbol which does not have
26316      the THUMB_FUNC attribute, then we must be calling a function which has
26317      the (interfacearm) attribute.  We look for the Thumb entry point to that
26318      function and change the branch to refer to that function instead.  */
26319   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
26320       && fixP->fx_addsy != NULL
26321       && S_IS_DEFINED (fixP->fx_addsy)
26322       && ! THUMB_IS_FUNC (fixP->fx_addsy))
26323     {
26324       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
26325     }
26326 }
26327 #endif
26328
26329
26330 int
26331 arm_force_relocation (struct fix * fixp)
26332 {
26333 #if defined (OBJ_COFF) && defined (TE_PE)
26334   if (fixp->fx_r_type == BFD_RELOC_RVA)
26335     return 1;
26336 #endif
26337
26338   /* In case we have a call or a branch to a function in ARM ISA mode from
26339      a thumb function or vice-versa force the relocation. These relocations
26340      are cleared off for some cores that might have blx and simple transformations
26341      are possible.  */
26342
26343 #ifdef OBJ_ELF
26344   switch (fixp->fx_r_type)
26345     {
26346     case BFD_RELOC_ARM_PCREL_JUMP:
26347     case BFD_RELOC_ARM_PCREL_CALL:
26348     case BFD_RELOC_THUMB_PCREL_BLX:
26349       if (THUMB_IS_FUNC (fixp->fx_addsy))
26350         return 1;
26351       break;
26352
26353     case BFD_RELOC_ARM_PCREL_BLX:
26354     case BFD_RELOC_THUMB_PCREL_BRANCH25:
26355     case BFD_RELOC_THUMB_PCREL_BRANCH20:
26356     case BFD_RELOC_THUMB_PCREL_BRANCH23:
26357       if (ARM_IS_FUNC (fixp->fx_addsy))
26358         return 1;
26359       break;
26360
26361     default:
26362       break;
26363     }
26364 #endif
26365
26366   /* Resolve these relocations even if the symbol is extern or weak.
26367      Technically this is probably wrong due to symbol preemption.
26368      In practice these relocations do not have enough range to be useful
26369      at dynamic link time, and some code (e.g. in the Linux kernel)
26370      expects these references to be resolved.  */
26371   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
26372       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
26373       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
26374       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
26375       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
26376       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
26377       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
26378       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
26379       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
26380       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
26381       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
26382       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
26383       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
26384       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
26385     return 0;
26386
26387   /* Always leave these relocations for the linker.  */
26388   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
26389        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
26390       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
26391     return 1;
26392
26393   /* Always generate relocations against function symbols.  */
26394   if (fixp->fx_r_type == BFD_RELOC_32
26395       && fixp->fx_addsy
26396       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
26397     return 1;
26398
26399   return generic_force_reloc (fixp);
26400 }
26401
26402 #if defined (OBJ_ELF) || defined (OBJ_COFF)
26403 /* Relocations against function names must be left unadjusted,
26404    so that the linker can use this information to generate interworking
26405    stubs.  The MIPS version of this function
26406    also prevents relocations that are mips-16 specific, but I do not
26407    know why it does this.
26408
26409    FIXME:
26410    There is one other problem that ought to be addressed here, but
26411    which currently is not:  Taking the address of a label (rather
26412    than a function) and then later jumping to that address.  Such
26413    addresses also ought to have their bottom bit set (assuming that
26414    they reside in Thumb code), but at the moment they will not.  */
26415
26416 bfd_boolean
26417 arm_fix_adjustable (fixS * fixP)
26418 {
26419   if (fixP->fx_addsy == NULL)
26420     return 1;
26421
26422   /* Preserve relocations against symbols with function type.  */
26423   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
26424     return FALSE;
26425
26426   if (THUMB_IS_FUNC (fixP->fx_addsy)
26427       && fixP->fx_subsy == NULL)
26428     return FALSE;
26429
26430   /* We need the symbol name for the VTABLE entries.  */
26431   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
26432       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
26433     return FALSE;
26434
26435   /* Don't allow symbols to be discarded on GOT related relocs.  */
26436   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
26437       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
26438       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
26439       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
26440       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
26441       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
26442       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
26443       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
26444       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
26445       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
26446       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
26447       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
26448       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
26449       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
26450       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
26451       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
26452       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
26453     return FALSE;
26454
26455   /* Similarly for group relocations.  */
26456   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
26457        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
26458       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
26459     return FALSE;
26460
26461   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
26462   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
26463       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
26464       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
26465       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
26466       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
26467       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
26468       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
26469       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
26470     return FALSE;
26471
26472   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
26473      offsets, so keep these symbols.  */
26474   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
26475       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
26476     return FALSE;
26477
26478   return TRUE;
26479 }
26480 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
26481
26482 #ifdef OBJ_ELF
26483 const char *
26484 elf32_arm_target_format (void)
26485 {
26486 #ifdef TE_SYMBIAN
26487   return (target_big_endian
26488           ? "elf32-bigarm-symbian"
26489           : "elf32-littlearm-symbian");
26490 #elif defined (TE_VXWORKS)
26491   return (target_big_endian
26492           ? "elf32-bigarm-vxworks"
26493           : "elf32-littlearm-vxworks");
26494 #elif defined (TE_NACL)
26495   return (target_big_endian
26496           ? "elf32-bigarm-nacl"
26497           : "elf32-littlearm-nacl");
26498 #else
26499   if (arm_fdpic)
26500     {
26501       if (target_big_endian)
26502         return "elf32-bigarm-fdpic";
26503       else
26504         return "elf32-littlearm-fdpic";
26505     }
26506   else
26507     {
26508       if (target_big_endian)
26509         return "elf32-bigarm";
26510       else
26511         return "elf32-littlearm";
26512     }
26513 #endif
26514 }
26515
26516 void
26517 armelf_frob_symbol (symbolS * symp,
26518                     int *     puntp)
26519 {
26520   elf_frob_symbol (symp, puntp);
26521 }
26522 #endif
26523
26524 /* MD interface: Finalization.  */
26525
26526 void
26527 arm_cleanup (void)
26528 {
26529   literal_pool * pool;
26530
26531   /* Ensure that all the predication blocks are properly closed.  */
26532   check_pred_blocks_finished ();
26533
26534   for (pool = list_of_pools; pool; pool = pool->next)
26535     {
26536       /* Put it at the end of the relevant section.  */
26537       subseg_set (pool->section, pool->sub_section);
26538 #ifdef OBJ_ELF
26539       arm_elf_change_section ();
26540 #endif
26541       s_ltorg (0);
26542     }
26543 }
26544
26545 #ifdef OBJ_ELF
26546 /* Remove any excess mapping symbols generated for alignment frags in
26547    SEC.  We may have created a mapping symbol before a zero byte
26548    alignment; remove it if there's a mapping symbol after the
26549    alignment.  */
26550 static void
26551 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
26552                        void *dummy ATTRIBUTE_UNUSED)
26553 {
26554   segment_info_type *seginfo = seg_info (sec);
26555   fragS *fragp;
26556
26557   if (seginfo == NULL || seginfo->frchainP == NULL)
26558     return;
26559
26560   for (fragp = seginfo->frchainP->frch_root;
26561        fragp != NULL;
26562        fragp = fragp->fr_next)
26563     {
26564       symbolS *sym = fragp->tc_frag_data.last_map;
26565       fragS *next = fragp->fr_next;
26566
26567       /* Variable-sized frags have been converted to fixed size by
26568          this point.  But if this was variable-sized to start with,
26569          there will be a fixed-size frag after it.  So don't handle
26570          next == NULL.  */
26571       if (sym == NULL || next == NULL)
26572         continue;
26573
26574       if (S_GET_VALUE (sym) < next->fr_address)
26575         /* Not at the end of this frag.  */
26576         continue;
26577       know (S_GET_VALUE (sym) == next->fr_address);
26578
26579       do
26580         {
26581           if (next->tc_frag_data.first_map != NULL)
26582             {
26583               /* Next frag starts with a mapping symbol.  Discard this
26584                  one.  */
26585               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
26586               break;
26587             }
26588
26589           if (next->fr_next == NULL)
26590             {
26591               /* This mapping symbol is at the end of the section.  Discard
26592                  it.  */
26593               know (next->fr_fix == 0 && next->fr_var == 0);
26594               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
26595               break;
26596             }
26597
26598           /* As long as we have empty frags without any mapping symbols,
26599              keep looking.  */
26600           /* If the next frag is non-empty and does not start with a
26601              mapping symbol, then this mapping symbol is required.  */
26602           if (next->fr_address != next->fr_next->fr_address)
26603             break;
26604
26605           next = next->fr_next;
26606         }
26607       while (next != NULL);
26608     }
26609 }
26610 #endif
26611
26612 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
26613    ARM ones.  */
26614
26615 void
26616 arm_adjust_symtab (void)
26617 {
26618 #ifdef OBJ_COFF
26619   symbolS * sym;
26620
26621   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
26622     {
26623       if (ARM_IS_THUMB (sym))
26624         {
26625           if (THUMB_IS_FUNC (sym))
26626             {
26627               /* Mark the symbol as a Thumb function.  */
26628               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
26629                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
26630                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
26631
26632               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
26633                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
26634               else
26635                 as_bad (_("%s: unexpected function type: %d"),
26636                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
26637             }
26638           else switch (S_GET_STORAGE_CLASS (sym))
26639             {
26640             case C_EXT:
26641               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
26642               break;
26643             case C_STAT:
26644               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
26645               break;
26646             case C_LABEL:
26647               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
26648               break;
26649             default:
26650               /* Do nothing.  */
26651               break;
26652             }
26653         }
26654
26655       if (ARM_IS_INTERWORK (sym))
26656         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
26657     }
26658 #endif
26659 #ifdef OBJ_ELF
26660   symbolS * sym;
26661   char      bind;
26662
26663   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
26664     {
26665       if (ARM_IS_THUMB (sym))
26666         {
26667           elf_symbol_type * elf_sym;
26668
26669           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
26670           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
26671
26672           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
26673                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
26674             {
26675               /* If it's a .thumb_func, declare it as so,
26676                  otherwise tag label as .code 16.  */
26677               if (THUMB_IS_FUNC (sym))
26678                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
26679                                          ST_BRANCH_TO_THUMB);
26680               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
26681                 elf_sym->internal_elf_sym.st_info =
26682                   ELF_ST_INFO (bind, STT_ARM_16BIT);
26683             }
26684         }
26685     }
26686
26687   /* Remove any overlapping mapping symbols generated by alignment frags.  */
26688   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
26689   /* Now do generic ELF adjustments.  */
26690   elf_adjust_symtab ();
26691 #endif
26692 }
26693
26694 /* MD interface: Initialization.  */
26695
26696 static void
26697 set_constant_flonums (void)
26698 {
26699   int i;
26700
26701   for (i = 0; i < NUM_FLOAT_VALS; i++)
26702     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
26703       abort ();
26704 }
26705
26706 /* Auto-select Thumb mode if it's the only available instruction set for the
26707    given architecture.  */
26708
26709 static void
26710 autoselect_thumb_from_cpu_variant (void)
26711 {
26712   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
26713     opcode_select (16);
26714 }
26715
26716 void
26717 md_begin (void)
26718 {
26719   unsigned mach;
26720   unsigned int i;
26721
26722   if (   (arm_ops_hsh = hash_new ()) == NULL
26723       || (arm_cond_hsh = hash_new ()) == NULL
26724       || (arm_vcond_hsh = hash_new ()) == NULL
26725       || (arm_shift_hsh = hash_new ()) == NULL
26726       || (arm_psr_hsh = hash_new ()) == NULL
26727       || (arm_v7m_psr_hsh = hash_new ()) == NULL
26728       || (arm_reg_hsh = hash_new ()) == NULL
26729       || (arm_reloc_hsh = hash_new ()) == NULL
26730       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
26731     as_fatal (_("virtual memory exhausted"));
26732
26733   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
26734     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
26735   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
26736     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
26737   for (i = 0; i < sizeof (vconds) / sizeof (struct asm_cond); i++)
26738     hash_insert (arm_vcond_hsh, vconds[i].template_name, (void *) (vconds + i));
26739   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
26740     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
26741   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
26742     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
26743   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
26744     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
26745                  (void *) (v7m_psrs + i));
26746   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
26747     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
26748   for (i = 0;
26749        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
26750        i++)
26751     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
26752                  (void *) (barrier_opt_names + i));
26753 #ifdef OBJ_ELF
26754   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
26755     {
26756       struct reloc_entry * entry = reloc_names + i;
26757
26758       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
26759         /* This makes encode_branch() use the EABI versions of this relocation.  */
26760         entry->reloc = BFD_RELOC_UNUSED;
26761
26762       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
26763     }
26764 #endif
26765
26766   set_constant_flonums ();
26767
26768   /* Set the cpu variant based on the command-line options.  We prefer
26769      -mcpu= over -march= if both are set (as for GCC); and we prefer
26770      -mfpu= over any other way of setting the floating point unit.
26771      Use of legacy options with new options are faulted.  */
26772   if (legacy_cpu)
26773     {
26774       if (mcpu_cpu_opt || march_cpu_opt)
26775         as_bad (_("use of old and new-style options to set CPU type"));
26776
26777       selected_arch = *legacy_cpu;
26778     }
26779   else if (mcpu_cpu_opt)
26780     {
26781       selected_arch = *mcpu_cpu_opt;
26782       selected_ext = *mcpu_ext_opt;
26783     }
26784   else if (march_cpu_opt)
26785     {
26786       selected_arch = *march_cpu_opt;
26787       selected_ext = *march_ext_opt;
26788     }
26789   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
26790
26791   if (legacy_fpu)
26792     {
26793       if (mfpu_opt)
26794         as_bad (_("use of old and new-style options to set FPU type"));
26795
26796       selected_fpu = *legacy_fpu;
26797     }
26798   else if (mfpu_opt)
26799     selected_fpu = *mfpu_opt;
26800   else
26801     {
26802 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
26803         || defined (TE_NetBSD) || defined (TE_VXWORKS))
26804       /* Some environments specify a default FPU.  If they don't, infer it
26805          from the processor.  */
26806       if (mcpu_fpu_opt)
26807         selected_fpu = *mcpu_fpu_opt;
26808       else if (march_fpu_opt)
26809         selected_fpu = *march_fpu_opt;
26810 #else
26811       selected_fpu = fpu_default;
26812 #endif
26813     }
26814
26815   if (ARM_FEATURE_ZERO (selected_fpu))
26816     {
26817       if (!no_cpu_selected ())
26818         selected_fpu = fpu_default;
26819       else
26820         selected_fpu = fpu_arch_fpa;
26821     }
26822
26823 #ifdef CPU_DEFAULT
26824   if (ARM_FEATURE_ZERO (selected_arch))
26825     {
26826       selected_arch = cpu_default;
26827       selected_cpu = selected_arch;
26828     }
26829   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
26830 #else
26831   /*  Autodection of feature mode: allow all features in cpu_variant but leave
26832       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
26833       after all instruction have been processed and we can decide what CPU
26834       should be selected.  */
26835   if (ARM_FEATURE_ZERO (selected_arch))
26836     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
26837   else
26838     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
26839 #endif
26840
26841   autoselect_thumb_from_cpu_variant ();
26842
26843   arm_arch_used = thumb_arch_used = arm_arch_none;
26844
26845 #if defined OBJ_COFF || defined OBJ_ELF
26846   {
26847     unsigned int flags = 0;
26848
26849 #if defined OBJ_ELF
26850     flags = meabi_flags;
26851
26852     switch (meabi_flags)
26853       {
26854       case EF_ARM_EABI_UNKNOWN:
26855 #endif
26856         /* Set the flags in the private structure.  */
26857         if (uses_apcs_26)      flags |= F_APCS26;
26858         if (support_interwork) flags |= F_INTERWORK;
26859         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
26860         if (pic_code)          flags |= F_PIC;
26861         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
26862           flags |= F_SOFT_FLOAT;
26863
26864         switch (mfloat_abi_opt)
26865           {
26866           case ARM_FLOAT_ABI_SOFT:
26867           case ARM_FLOAT_ABI_SOFTFP:
26868             flags |= F_SOFT_FLOAT;
26869             break;
26870
26871           case ARM_FLOAT_ABI_HARD:
26872             if (flags & F_SOFT_FLOAT)
26873               as_bad (_("hard-float conflicts with specified fpu"));
26874             break;
26875           }
26876
26877         /* Using pure-endian doubles (even if soft-float).      */
26878         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
26879           flags |= F_VFP_FLOAT;
26880
26881 #if defined OBJ_ELF
26882         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
26883             flags |= EF_ARM_MAVERICK_FLOAT;
26884         break;
26885
26886       case EF_ARM_EABI_VER4:
26887       case EF_ARM_EABI_VER5:
26888         /* No additional flags to set.  */
26889         break;
26890
26891       default:
26892         abort ();
26893       }
26894 #endif
26895     bfd_set_private_flags (stdoutput, flags);
26896
26897     /* We have run out flags in the COFF header to encode the
26898        status of ATPCS support, so instead we create a dummy,
26899        empty, debug section called .arm.atpcs.  */
26900     if (atpcs)
26901       {
26902         asection * sec;
26903
26904         sec = bfd_make_section (stdoutput, ".arm.atpcs");
26905
26906         if (sec != NULL)
26907           {
26908             bfd_set_section_flags
26909               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
26910             bfd_set_section_size (stdoutput, sec, 0);
26911             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
26912           }
26913       }
26914   }
26915 #endif
26916
26917   /* Record the CPU type as well.  */
26918   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
26919     mach = bfd_mach_arm_iWMMXt2;
26920   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
26921     mach = bfd_mach_arm_iWMMXt;
26922   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
26923     mach = bfd_mach_arm_XScale;
26924   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
26925     mach = bfd_mach_arm_ep9312;
26926   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
26927     mach = bfd_mach_arm_5TE;
26928   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
26929     {
26930       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
26931         mach = bfd_mach_arm_5T;
26932       else
26933         mach = bfd_mach_arm_5;
26934     }
26935   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
26936     {
26937       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
26938         mach = bfd_mach_arm_4T;
26939       else
26940         mach = bfd_mach_arm_4;
26941     }
26942   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
26943     mach = bfd_mach_arm_3M;
26944   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
26945     mach = bfd_mach_arm_3;
26946   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
26947     mach = bfd_mach_arm_2a;
26948   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
26949     mach = bfd_mach_arm_2;
26950   else
26951     mach = bfd_mach_arm_unknown;
26952
26953   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
26954 }
26955
26956 /* Command line processing.  */
26957
26958 /* md_parse_option
26959       Invocation line includes a switch not recognized by the base assembler.
26960       See if it's a processor-specific option.
26961
26962       This routine is somewhat complicated by the need for backwards
26963       compatibility (since older releases of gcc can't be changed).
26964       The new options try to make the interface as compatible as
26965       possible with GCC.
26966
26967       New options (supported) are:
26968
26969               -mcpu=<cpu name>           Assemble for selected processor
26970               -march=<architecture name> Assemble for selected architecture
26971               -mfpu=<fpu architecture>   Assemble for selected FPU.
26972               -EB/-mbig-endian           Big-endian
26973               -EL/-mlittle-endian        Little-endian
26974               -k                         Generate PIC code
26975               -mthumb                    Start in Thumb mode
26976               -mthumb-interwork          Code supports ARM/Thumb interworking
26977
26978               -m[no-]warn-deprecated     Warn about deprecated features
26979               -m[no-]warn-syms           Warn when symbols match instructions
26980
26981       For now we will also provide support for:
26982
26983               -mapcs-32                  32-bit Program counter
26984               -mapcs-26                  26-bit Program counter
26985               -macps-float               Floats passed in FP registers
26986               -mapcs-reentrant           Reentrant code
26987               -matpcs
26988       (sometime these will probably be replaced with -mapcs=<list of options>
26989       and -matpcs=<list of options>)
26990
26991       The remaining options are only supported for back-wards compatibility.
26992       Cpu variants, the arm part is optional:
26993               -m[arm]1                Currently not supported.
26994               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
26995               -m[arm]3                Arm 3 processor
26996               -m[arm]6[xx],           Arm 6 processors
26997               -m[arm]7[xx][t][[d]m]   Arm 7 processors
26998               -m[arm]8[10]            Arm 8 processors
26999               -m[arm]9[20][tdmi]      Arm 9 processors
27000               -mstrongarm[110[0]]     StrongARM processors
27001               -mxscale                XScale processors
27002               -m[arm]v[2345[t[e]]]    Arm architectures
27003               -mall                   All (except the ARM1)
27004       FP variants:
27005               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
27006               -mfpe-old               (No float load/store multiples)
27007               -mvfpxd                 VFP Single precision
27008               -mvfp                   All VFP
27009               -mno-fpu                Disable all floating point instructions
27010
27011       The following CPU names are recognized:
27012               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
27013               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
27014               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
27015               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
27016               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
27017               arm10t arm10e, arm1020t, arm1020e, arm10200e,
27018               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
27019
27020       */
27021
27022 const char * md_shortopts = "m:k";
27023
27024 #ifdef ARM_BI_ENDIAN
27025 #define OPTION_EB (OPTION_MD_BASE + 0)
27026 #define OPTION_EL (OPTION_MD_BASE + 1)
27027 #else
27028 #if TARGET_BYTES_BIG_ENDIAN
27029 #define OPTION_EB (OPTION_MD_BASE + 0)
27030 #else
27031 #define OPTION_EL (OPTION_MD_BASE + 1)
27032 #endif
27033 #endif
27034 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
27035 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
27036
27037 struct option md_longopts[] =
27038 {
27039 #ifdef OPTION_EB
27040   {"EB", no_argument, NULL, OPTION_EB},
27041 #endif
27042 #ifdef OPTION_EL
27043   {"EL", no_argument, NULL, OPTION_EL},
27044 #endif
27045   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
27046 #ifdef OBJ_ELF
27047   {"fdpic", no_argument, NULL, OPTION_FDPIC},
27048 #endif
27049   {NULL, no_argument, NULL, 0}
27050 };
27051
27052 size_t md_longopts_size = sizeof (md_longopts);
27053
27054 struct arm_option_table
27055 {
27056   const char *  option;         /* Option name to match.  */
27057   const char *  help;           /* Help information.  */
27058   int *         var;            /* Variable to change.  */
27059   int           value;          /* What to change it to.  */
27060   const char *  deprecated;     /* If non-null, print this message.  */
27061 };
27062
27063 struct arm_option_table arm_opts[] =
27064 {
27065   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
27066   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
27067   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
27068    &support_interwork, 1, NULL},
27069   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
27070   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
27071   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
27072    1, NULL},
27073   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
27074   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
27075   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
27076   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
27077    NULL},
27078
27079   /* These are recognized by the assembler, but have no affect on code.  */
27080   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
27081   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
27082
27083   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
27084   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
27085    &warn_on_deprecated, 0, NULL},
27086   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
27087   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
27088   {NULL, NULL, NULL, 0, NULL}
27089 };
27090
27091 struct arm_legacy_option_table
27092 {
27093   const char *              option;             /* Option name to match.  */
27094   const arm_feature_set **  var;                /* Variable to change.  */
27095   const arm_feature_set     value;              /* What to change it to.  */
27096   const char *              deprecated;         /* If non-null, print this message.  */
27097 };
27098
27099 const struct arm_legacy_option_table arm_legacy_opts[] =
27100 {
27101   /* DON'T add any new processors to this list -- we want the whole list
27102      to go away...  Add them to the processors table instead.  */
27103   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
27104   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
27105   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
27106   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
27107   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
27108   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
27109   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
27110   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
27111   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
27112   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
27113   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
27114   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
27115   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
27116   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
27117   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
27118   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
27119   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
27120   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
27121   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
27122   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
27123   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
27124   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
27125   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
27126   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
27127   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
27128   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
27129   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
27130   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
27131   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
27132   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
27133   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
27134   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
27135   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
27136   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
27137   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
27138   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
27139   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
27140   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
27141   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
27142   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
27143   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
27144   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
27145   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
27146   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
27147   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
27148   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
27149   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
27150   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
27151   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
27152   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
27153   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
27154   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
27155   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
27156   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
27157   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
27158   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
27159   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
27160   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
27161   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
27162   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
27163   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
27164   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
27165   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
27166   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
27167   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
27168   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
27169   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
27170   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
27171   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
27172   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
27173    N_("use -mcpu=strongarm110")},
27174   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
27175    N_("use -mcpu=strongarm1100")},
27176   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
27177    N_("use -mcpu=strongarm1110")},
27178   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
27179   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
27180   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
27181
27182   /* Architecture variants -- don't add any more to this list either.  */
27183   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
27184   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
27185   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
27186   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
27187   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
27188   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
27189   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
27190   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
27191   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
27192   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
27193   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
27194   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
27195   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
27196   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
27197   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
27198   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
27199   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
27200   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
27201
27202   /* Floating point variants -- don't add any more to this list either.  */
27203   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
27204   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
27205   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
27206   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
27207    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
27208
27209   {NULL, NULL, ARM_ARCH_NONE, NULL}
27210 };
27211
27212 struct arm_cpu_option_table
27213 {
27214   const char *           name;
27215   size_t                 name_len;
27216   const arm_feature_set  value;
27217   const arm_feature_set  ext;
27218   /* For some CPUs we assume an FPU unless the user explicitly sets
27219      -mfpu=...  */
27220   const arm_feature_set  default_fpu;
27221   /* The canonical name of the CPU, or NULL to use NAME converted to upper
27222      case.  */
27223   const char *           canonical_name;
27224 };
27225
27226 /* This list should, at a minimum, contain all the cpu names
27227    recognized by GCC.  */
27228 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
27229
27230 static const struct arm_cpu_option_table arm_cpus[] =
27231 {
27232   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
27233                ARM_ARCH_NONE,
27234                FPU_ARCH_FPA),
27235   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
27236                ARM_ARCH_NONE,
27237                FPU_ARCH_FPA),
27238   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
27239                ARM_ARCH_NONE,
27240                FPU_ARCH_FPA),
27241   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
27242                ARM_ARCH_NONE,
27243                FPU_ARCH_FPA),
27244   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
27245                ARM_ARCH_NONE,
27246                FPU_ARCH_FPA),
27247   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
27248                ARM_ARCH_NONE,
27249                FPU_ARCH_FPA),
27250   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
27251                ARM_ARCH_NONE,
27252                FPU_ARCH_FPA),
27253   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
27254                ARM_ARCH_NONE,
27255                FPU_ARCH_FPA),
27256   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
27257                ARM_ARCH_NONE,
27258                FPU_ARCH_FPA),
27259   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
27260                ARM_ARCH_NONE,
27261                FPU_ARCH_FPA),
27262   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
27263                ARM_ARCH_NONE,
27264                FPU_ARCH_FPA),
27265   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
27266                ARM_ARCH_NONE,
27267                FPU_ARCH_FPA),
27268   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
27269                ARM_ARCH_NONE,
27270                FPU_ARCH_FPA),
27271   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
27272                ARM_ARCH_NONE,
27273                FPU_ARCH_FPA),
27274   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
27275                ARM_ARCH_NONE,
27276                FPU_ARCH_FPA),
27277   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
27278                ARM_ARCH_NONE,
27279                FPU_ARCH_FPA),
27280   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
27281                ARM_ARCH_NONE,
27282                FPU_ARCH_FPA),
27283   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
27284                ARM_ARCH_NONE,
27285                FPU_ARCH_FPA),
27286   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
27287                ARM_ARCH_NONE,
27288                FPU_ARCH_FPA),
27289   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
27290                ARM_ARCH_NONE,
27291                FPU_ARCH_FPA),
27292   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
27293                ARM_ARCH_NONE,
27294                FPU_ARCH_FPA),
27295   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
27296                ARM_ARCH_NONE,
27297                FPU_ARCH_FPA),
27298   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
27299                ARM_ARCH_NONE,
27300                FPU_ARCH_FPA),
27301   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
27302                ARM_ARCH_NONE,
27303                FPU_ARCH_FPA),
27304   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
27305                ARM_ARCH_NONE,
27306                FPU_ARCH_FPA),
27307   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
27308                ARM_ARCH_NONE,
27309                FPU_ARCH_FPA),
27310   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
27311                ARM_ARCH_NONE,
27312                FPU_ARCH_FPA),
27313   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
27314                ARM_ARCH_NONE,
27315                FPU_ARCH_FPA),
27316   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
27317                ARM_ARCH_NONE,
27318                FPU_ARCH_FPA),
27319   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
27320                ARM_ARCH_NONE,
27321                FPU_ARCH_FPA),
27322   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
27323                ARM_ARCH_NONE,
27324                FPU_ARCH_FPA),
27325   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
27326                ARM_ARCH_NONE,
27327                FPU_ARCH_FPA),
27328   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
27329                ARM_ARCH_NONE,
27330                FPU_ARCH_FPA),
27331   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
27332                ARM_ARCH_NONE,
27333                FPU_ARCH_FPA),
27334   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
27335                ARM_ARCH_NONE,
27336                FPU_ARCH_FPA),
27337   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
27338                ARM_ARCH_NONE,
27339                FPU_ARCH_FPA),
27340   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
27341                ARM_ARCH_NONE,
27342                FPU_ARCH_FPA),
27343   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
27344                ARM_ARCH_NONE,
27345                FPU_ARCH_FPA),
27346   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
27347                ARM_ARCH_NONE,
27348                FPU_ARCH_FPA),
27349   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
27350                ARM_ARCH_NONE,
27351                FPU_ARCH_FPA),
27352   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
27353                ARM_ARCH_NONE,
27354                FPU_ARCH_FPA),
27355   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
27356                ARM_ARCH_NONE,
27357                FPU_ARCH_FPA),
27358   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
27359                ARM_ARCH_NONE,
27360                FPU_ARCH_FPA),
27361   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
27362                ARM_ARCH_NONE,
27363                FPU_ARCH_FPA),
27364   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
27365                ARM_ARCH_NONE,
27366                FPU_ARCH_FPA),
27367   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
27368                ARM_ARCH_NONE,
27369                FPU_ARCH_FPA),
27370
27371   /* For V5 or later processors we default to using VFP; but the user
27372      should really set the FPU type explicitly.  */
27373   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
27374                ARM_ARCH_NONE,
27375                FPU_ARCH_VFP_V2),
27376   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
27377                ARM_ARCH_NONE,
27378                FPU_ARCH_VFP_V2),
27379   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
27380                ARM_ARCH_NONE,
27381                FPU_ARCH_VFP_V2),
27382   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
27383                ARM_ARCH_NONE,
27384                FPU_ARCH_VFP_V2),
27385   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
27386                ARM_ARCH_NONE,
27387                FPU_ARCH_VFP_V2),
27388   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
27389                ARM_ARCH_NONE,
27390                FPU_ARCH_VFP_V2),
27391   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
27392                ARM_ARCH_NONE,
27393                FPU_ARCH_VFP_V2),
27394   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
27395                ARM_ARCH_NONE,
27396                FPU_ARCH_VFP_V2),
27397   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
27398                ARM_ARCH_NONE,
27399                FPU_ARCH_VFP_V2),
27400   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
27401                ARM_ARCH_NONE,
27402                FPU_ARCH_VFP_V2),
27403   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
27404                ARM_ARCH_NONE,
27405                FPU_ARCH_VFP_V2),
27406   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
27407                ARM_ARCH_NONE,
27408                FPU_ARCH_VFP_V2),
27409   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
27410                ARM_ARCH_NONE,
27411                FPU_ARCH_VFP_V1),
27412   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
27413                ARM_ARCH_NONE,
27414                FPU_ARCH_VFP_V1),
27415   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
27416                ARM_ARCH_NONE,
27417                FPU_ARCH_VFP_V2),
27418   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
27419                ARM_ARCH_NONE,
27420                FPU_ARCH_VFP_V2),
27421   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
27422                ARM_ARCH_NONE,
27423                FPU_ARCH_VFP_V1),
27424   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
27425                ARM_ARCH_NONE,
27426                FPU_ARCH_VFP_V2),
27427   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
27428                ARM_ARCH_NONE,
27429                FPU_ARCH_VFP_V2),
27430   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
27431                ARM_ARCH_NONE,
27432                FPU_ARCH_VFP_V2),
27433   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
27434                ARM_ARCH_NONE,
27435                FPU_ARCH_VFP_V2),
27436   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
27437                ARM_ARCH_NONE,
27438                FPU_ARCH_VFP_V2),
27439   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
27440                ARM_ARCH_NONE,
27441                FPU_ARCH_VFP_V2),
27442   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
27443                ARM_ARCH_NONE,
27444                FPU_ARCH_VFP_V2),
27445   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
27446                ARM_ARCH_NONE,
27447                FPU_ARCH_VFP_V2),
27448   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
27449                ARM_ARCH_NONE,
27450                FPU_ARCH_VFP_V2),
27451   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
27452                ARM_ARCH_NONE,
27453                FPU_NONE),
27454   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
27455                ARM_ARCH_NONE,
27456                FPU_NONE),
27457   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
27458                ARM_ARCH_NONE,
27459                FPU_ARCH_VFP_V2),
27460   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
27461                ARM_ARCH_NONE,
27462                FPU_ARCH_VFP_V2),
27463   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
27464                ARM_ARCH_NONE,
27465                FPU_ARCH_VFP_V2),
27466   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
27467                ARM_ARCH_NONE,
27468                FPU_NONE),
27469   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
27470                ARM_ARCH_NONE,
27471                FPU_NONE),
27472   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
27473                ARM_ARCH_NONE,
27474                FPU_ARCH_VFP_V2),
27475   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
27476                ARM_ARCH_NONE,
27477                FPU_NONE),
27478   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
27479                ARM_ARCH_NONE,
27480                FPU_ARCH_VFP_V2),
27481   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
27482                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
27483                FPU_NONE),
27484   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
27485                ARM_ARCH_NONE,
27486                FPU_ARCH_NEON_VFP_V4),
27487   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
27488                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
27489                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
27490   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
27491                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
27492                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
27493   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
27494                ARM_ARCH_NONE,
27495                FPU_ARCH_NEON_VFP_V4),
27496   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
27497                ARM_ARCH_NONE,
27498                FPU_ARCH_NEON_VFP_V4),
27499   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
27500                ARM_ARCH_NONE,
27501                FPU_ARCH_NEON_VFP_V4),
27502   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
27503                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27504                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27505   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
27506                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27507                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27508   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
27509                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27510                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27511   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
27512                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27513                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
27514   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
27515                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27516                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27517   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
27518               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27519               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27520   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
27521               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27522               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27523   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
27524                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27525                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
27526   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
27527                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27528                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
27529   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
27530                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27531                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
27532   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
27533                ARM_ARCH_NONE,
27534                FPU_NONE),
27535   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
27536                ARM_ARCH_NONE,
27537                FPU_ARCH_VFP_V3D16),
27538   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
27539                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
27540                FPU_NONE),
27541   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
27542                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
27543                FPU_ARCH_VFP_V3D16),
27544   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
27545                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
27546                FPU_ARCH_VFP_V3D16),
27547   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
27548               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27549               FPU_ARCH_NEON_VFP_ARMV8),
27550   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
27551                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
27552                FPU_NONE),
27553   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
27554                ARM_ARCH_NONE,
27555                FPU_NONE),
27556   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
27557                ARM_ARCH_NONE,
27558                FPU_NONE),
27559   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
27560                ARM_ARCH_NONE,
27561                FPU_NONE),
27562   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
27563                ARM_ARCH_NONE,
27564                FPU_NONE),
27565   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
27566                ARM_ARCH_NONE,
27567                FPU_NONE),
27568   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
27569                ARM_ARCH_NONE,
27570                FPU_NONE),
27571   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
27572                ARM_ARCH_NONE,
27573                FPU_NONE),
27574   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
27575                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27576                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27577   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
27578                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27579                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
27580   /* ??? XSCALE is really an architecture.  */
27581   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
27582                ARM_ARCH_NONE,
27583                FPU_ARCH_VFP_V2),
27584
27585   /* ??? iwmmxt is not a processor.  */
27586   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
27587                ARM_ARCH_NONE,
27588                FPU_ARCH_VFP_V2),
27589   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
27590                ARM_ARCH_NONE,
27591                FPU_ARCH_VFP_V2),
27592   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
27593                ARM_ARCH_NONE,
27594                FPU_ARCH_VFP_V2),
27595
27596   /* Maverick.  */
27597   ARM_CPU_OPT ("ep9312",          "ARM920T",
27598                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
27599                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
27600
27601   /* Marvell processors.  */
27602   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
27603                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
27604                FPU_ARCH_VFP_V3D16),
27605   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
27606                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
27607                FPU_ARCH_NEON_VFP_V4),
27608
27609   /* APM X-Gene family.  */
27610   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
27611                ARM_ARCH_NONE,
27612                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27613   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
27614                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27615                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
27616
27617   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
27618 };
27619 #undef ARM_CPU_OPT
27620
27621 struct arm_ext_table
27622 {
27623   const char *            name;
27624   size_t                  name_len;
27625   const arm_feature_set   merge;
27626   const arm_feature_set   clear;
27627 };
27628
27629 struct arm_arch_option_table
27630 {
27631   const char *                  name;
27632   size_t                        name_len;
27633   const arm_feature_set         value;
27634   const arm_feature_set         default_fpu;
27635   const struct arm_ext_table *  ext_table;
27636 };
27637
27638 /* Used to add support for +E and +noE extension.  */
27639 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
27640 /* Used to add support for a +E extension.  */
27641 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
27642 /* Used to add support for a +noE extension.  */
27643 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
27644
27645 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
27646                             ~0 & ~FPU_ENDIAN_PURE)
27647
27648 static const struct arm_ext_table armv5te_ext_table[] =
27649 {
27650   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
27651   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27652 };
27653
27654 static const struct arm_ext_table armv7_ext_table[] =
27655 {
27656   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
27657   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27658 };
27659
27660 static const struct arm_ext_table armv7ve_ext_table[] =
27661 {
27662   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
27663   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
27664   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
27665   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
27666   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
27667   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
27668   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
27669
27670   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
27671            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
27672
27673   /* Aliases for +simd.  */
27674   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
27675
27676   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
27677   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
27678   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
27679
27680   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27681 };
27682
27683 static const struct arm_ext_table armv7a_ext_table[] =
27684 {
27685   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
27686   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
27687   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
27688   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
27689   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
27690   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
27691   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
27692
27693   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
27694            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
27695
27696   /* Aliases for +simd.  */
27697   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
27698   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
27699
27700   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
27701   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
27702
27703   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
27704   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
27705   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27706 };
27707
27708 static const struct arm_ext_table armv7r_ext_table[] =
27709 {
27710   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
27711   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
27712   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
27713   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
27714   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
27715   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
27716   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
27717            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
27718   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27719 };
27720
27721 static const struct arm_ext_table armv7em_ext_table[] =
27722 {
27723   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
27724   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
27725   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
27726   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
27727   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
27728   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
27729   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27730 };
27731
27732 static const struct arm_ext_table armv8a_ext_table[] =
27733 {
27734   ARM_ADD ("crc", ARCH_CRC_ARMV8),
27735   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
27736   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
27737            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27738
27739   /* Armv8-a does not allow an FP implementation without SIMD, so the user
27740      should use the +simd option to turn on FP.  */
27741   ARM_REMOVE ("fp", ALL_FP),
27742   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
27743   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
27744   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27745 };
27746
27747
27748 static const struct arm_ext_table armv81a_ext_table[] =
27749 {
27750   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
27751   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
27752            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27753
27754   /* Armv8-a does not allow an FP implementation without SIMD, so the user
27755      should use the +simd option to turn on FP.  */
27756   ARM_REMOVE ("fp", ALL_FP),
27757   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
27758   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
27759   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27760 };
27761
27762 static const struct arm_ext_table armv82a_ext_table[] =
27763 {
27764   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
27765   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
27766   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
27767   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
27768            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27769   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
27770
27771   /* Armv8-a does not allow an FP implementation without SIMD, so the user
27772      should use the +simd option to turn on FP.  */
27773   ARM_REMOVE ("fp", ALL_FP),
27774   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
27775   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
27776   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27777 };
27778
27779 static const struct arm_ext_table armv84a_ext_table[] =
27780 {
27781   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
27782   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
27783   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
27784            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27785
27786   /* Armv8-a does not allow an FP implementation without SIMD, so the user
27787      should use the +simd option to turn on FP.  */
27788   ARM_REMOVE ("fp", ALL_FP),
27789   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
27790   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
27791   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27792 };
27793
27794 static const struct arm_ext_table armv85a_ext_table[] =
27795 {
27796   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
27797   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
27798   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
27799            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27800
27801   /* Armv8-a does not allow an FP implementation without SIMD, so the user
27802      should use the +simd option to turn on FP.  */
27803   ARM_REMOVE ("fp", ALL_FP),
27804   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27805 };
27806
27807 static const struct arm_ext_table armv8m_main_ext_table[] =
27808 {
27809   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
27810                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
27811   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
27812   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
27813   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27814 };
27815
27816 static const struct arm_ext_table armv8_1m_main_ext_table[] =
27817 {
27818   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
27819                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
27820   ARM_EXT ("fp",
27821            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
27822                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
27823            ALL_FP),
27824   ARM_ADD ("fp.dp",
27825            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
27826                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
27827   ARM_EXT ("mve", ARM_FEATURE_COPROC (FPU_MVE),
27828            ARM_FEATURE_COPROC (FPU_MVE | FPU_MVE_FP)),
27829   ARM_ADD ("mve.fp",
27830            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
27831                         FPU_MVE | FPU_MVE_FP | FPU_VFP_V5_SP_D16 |
27832                         FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
27833   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27834 };
27835
27836 static const struct arm_ext_table armv8r_ext_table[] =
27837 {
27838   ARM_ADD ("crc", ARCH_CRC_ARMV8),
27839   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
27840   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
27841            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
27842   ARM_REMOVE ("fp", ALL_FP),
27843   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
27844   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
27845 };
27846
27847 /* This list should, at a minimum, contain all the architecture names
27848    recognized by GCC.  */
27849 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
27850 #define ARM_ARCH_OPT2(N, V, DF, ext) \
27851   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
27852
27853 static const struct arm_arch_option_table arm_archs[] =
27854 {
27855   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
27856   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
27857   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
27858   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
27859   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
27860   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
27861   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
27862   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
27863   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
27864   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
27865   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
27866   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
27867   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
27868   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
27869   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
27870   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
27871   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
27872   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
27873   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
27874   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
27875   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
27876   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
27877      kept to preserve existing behaviour.  */
27878   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
27879   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
27880   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
27881   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
27882   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
27883   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
27884      kept to preserve existing behaviour.  */
27885   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
27886   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
27887   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
27888   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
27889   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
27890   /* The official spelling of the ARMv7 profile variants is the dashed form.
27891      Accept the non-dashed form for compatibility with old toolchains.  */
27892   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
27893   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
27894   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
27895   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
27896   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
27897   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
27898   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
27899   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
27900   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
27901   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
27902                  armv8m_main),
27903   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
27904                  armv8_1m_main),
27905   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
27906   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
27907   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
27908   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
27909   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
27910   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
27911   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
27912   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
27913   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
27914   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
27915   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
27916 };
27917 #undef ARM_ARCH_OPT
27918
27919 /* ISA extensions in the co-processor and main instruction set space.  */
27920
27921 struct arm_option_extension_value_table
27922 {
27923   const char *           name;
27924   size_t                 name_len;
27925   const arm_feature_set  merge_value;
27926   const arm_feature_set  clear_value;
27927   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
27928      indicates that an extension is available for all architectures while
27929      ARM_ANY marks an empty entry.  */
27930   const arm_feature_set  allowed_archs[2];
27931 };
27932
27933 /* The following table must be in alphabetical order with a NULL last entry.  */
27934
27935 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
27936 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
27937
27938 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
27939    use the context sensitive approach using arm_ext_table's.  */
27940 static const struct arm_option_extension_value_table arm_extensions[] =
27941 {
27942   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
27943                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
27944   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
27945                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
27946                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
27947   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
27948                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
27949                           ARM_ARCH_V8_2A),
27950   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
27951                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
27952                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
27953   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
27954                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
27955   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27956                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
27957                         ARM_ARCH_V8_2A),
27958   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
27959                                                   | ARM_EXT2_FP16_FML),
27960                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
27961                                                   | ARM_EXT2_FP16_FML),
27962                            ARM_ARCH_V8_2A),
27963   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
27964                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
27965                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
27966                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
27967   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
27968      Thumb divide instruction.  Due to this having the same name as the
27969      previous entry, this will be ignored when doing command-line parsing and
27970      only considered by build attribute selection code.  */
27971   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
27972                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
27973                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
27974   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
27975                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
27976   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
27977                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
27978   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
27979                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
27980   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
27981                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
27982                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
27983                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
27984   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
27985                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
27986                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
27987   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
27988                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
27989                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
27990   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
27991                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
27992                         ARM_ARCH_V8A),
27993   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
27994                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
27995                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
27996   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
27997                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
27998                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
27999   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
28000                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
28001                         ARM_ARCH_V8A),
28002   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
28003                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
28004                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
28005                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
28006   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
28007                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
28008                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
28009   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
28010                                      | ARM_EXT_DIV),
28011                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
28012                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
28013   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
28014                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
28015   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
28016 };
28017 #undef ARM_EXT_OPT
28018
28019 /* ISA floating-point and Advanced SIMD extensions.  */
28020 struct arm_option_fpu_value_table
28021 {
28022   const char *           name;
28023   const arm_feature_set  value;
28024 };
28025
28026 /* This list should, at a minimum, contain all the fpu names
28027    recognized by GCC.  */
28028 static const struct arm_option_fpu_value_table arm_fpus[] =
28029 {
28030   {"softfpa",           FPU_NONE},
28031   {"fpe",               FPU_ARCH_FPE},
28032   {"fpe2",              FPU_ARCH_FPE},
28033   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
28034   {"fpa",               FPU_ARCH_FPA},
28035   {"fpa10",             FPU_ARCH_FPA},
28036   {"fpa11",             FPU_ARCH_FPA},
28037   {"arm7500fe",         FPU_ARCH_FPA},
28038   {"softvfp",           FPU_ARCH_VFP},
28039   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
28040   {"vfp",               FPU_ARCH_VFP_V2},
28041   {"vfp9",              FPU_ARCH_VFP_V2},
28042   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
28043   {"vfp10",             FPU_ARCH_VFP_V2},
28044   {"vfp10-r0",          FPU_ARCH_VFP_V1},
28045   {"vfpxd",             FPU_ARCH_VFP_V1xD},
28046   {"vfpv2",             FPU_ARCH_VFP_V2},
28047   {"vfpv3",             FPU_ARCH_VFP_V3},
28048   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
28049   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
28050   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
28051   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
28052   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
28053   {"arm1020t",          FPU_ARCH_VFP_V1},
28054   {"arm1020e",          FPU_ARCH_VFP_V2},
28055   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
28056   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
28057   {"maverick",          FPU_ARCH_MAVERICK},
28058   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
28059   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
28060   {"neon-fp16",         FPU_ARCH_NEON_FP16},
28061   {"vfpv4",             FPU_ARCH_VFP_V4},
28062   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
28063   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
28064   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
28065   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
28066   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
28067   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
28068   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
28069   {"crypto-neon-fp-armv8",
28070                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
28071   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
28072   {"crypto-neon-fp-armv8.1",
28073                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
28074   {NULL,                ARM_ARCH_NONE}
28075 };
28076
28077 struct arm_option_value_table
28078 {
28079   const char *name;
28080   long value;
28081 };
28082
28083 static const struct arm_option_value_table arm_float_abis[] =
28084 {
28085   {"hard",      ARM_FLOAT_ABI_HARD},
28086   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
28087   {"soft",      ARM_FLOAT_ABI_SOFT},
28088   {NULL,        0}
28089 };
28090
28091 #ifdef OBJ_ELF
28092 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
28093 static const struct arm_option_value_table arm_eabis[] =
28094 {
28095   {"gnu",       EF_ARM_EABI_UNKNOWN},
28096   {"4",         EF_ARM_EABI_VER4},
28097   {"5",         EF_ARM_EABI_VER5},
28098   {NULL,        0}
28099 };
28100 #endif
28101
28102 struct arm_long_option_table
28103 {
28104   const char * option;                  /* Substring to match.  */
28105   const char * help;                    /* Help information.  */
28106   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
28107   const char * deprecated;              /* If non-null, print this message.  */
28108 };
28109
28110 static bfd_boolean
28111 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
28112                      arm_feature_set *ext_set,
28113                      const struct arm_ext_table *ext_table)
28114 {
28115   /* We insist on extensions being specified in alphabetical order, and with
28116      extensions being added before being removed.  We achieve this by having
28117      the global ARM_EXTENSIONS table in alphabetical order, and using the
28118      ADDING_VALUE variable to indicate whether we are adding an extension (1)
28119      or removing it (0) and only allowing it to change in the order
28120      -1 -> 1 -> 0.  */
28121   const struct arm_option_extension_value_table * opt = NULL;
28122   const arm_feature_set arm_any = ARM_ANY;
28123   int adding_value = -1;
28124
28125   while (str != NULL && *str != 0)
28126     {
28127       const char *ext;
28128       size_t len;
28129
28130       if (*str != '+')
28131         {
28132           as_bad (_("invalid architectural extension"));
28133           return FALSE;
28134         }
28135
28136       str++;
28137       ext = strchr (str, '+');
28138
28139       if (ext != NULL)
28140         len = ext - str;
28141       else
28142         len = strlen (str);
28143
28144       if (len >= 2 && strncmp (str, "no", 2) == 0)
28145         {
28146           if (adding_value != 0)
28147             {
28148               adding_value = 0;
28149               opt = arm_extensions;
28150             }
28151
28152           len -= 2;
28153           str += 2;
28154         }
28155       else if (len > 0)
28156         {
28157           if (adding_value == -1)
28158             {
28159               adding_value = 1;
28160               opt = arm_extensions;
28161             }
28162           else if (adding_value != 1)
28163             {
28164               as_bad (_("must specify extensions to add before specifying "
28165                         "those to remove"));
28166               return FALSE;
28167             }
28168         }
28169
28170       if (len == 0)
28171         {
28172           as_bad (_("missing architectural extension"));
28173           return FALSE;
28174         }
28175
28176       gas_assert (adding_value != -1);
28177       gas_assert (opt != NULL);
28178
28179       if (ext_table != NULL)
28180         {
28181           const struct arm_ext_table * ext_opt = ext_table;
28182           bfd_boolean found = FALSE;
28183           for (; ext_opt->name != NULL; ext_opt++)
28184             if (ext_opt->name_len == len
28185                 && strncmp (ext_opt->name, str, len) == 0)
28186               {
28187                 if (adding_value)
28188                   {
28189                     if (ARM_FEATURE_ZERO (ext_opt->merge))
28190                         /* TODO: Option not supported.  When we remove the
28191                            legacy table this case should error out.  */
28192                         continue;
28193
28194                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
28195                   }
28196                 else
28197                   {
28198                     if (ARM_FEATURE_ZERO (ext_opt->clear))
28199                         /* TODO: Option not supported.  When we remove the
28200                            legacy table this case should error out.  */
28201                         continue;
28202                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
28203                   }
28204                 found = TRUE;
28205                 break;
28206               }
28207           if (found)
28208             {
28209               str = ext;
28210               continue;
28211             }
28212         }
28213
28214       /* Scan over the options table trying to find an exact match. */
28215       for (; opt->name != NULL; opt++)
28216         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
28217           {
28218             int i, nb_allowed_archs =
28219               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
28220             /* Check we can apply the extension to this architecture.  */
28221             for (i = 0; i < nb_allowed_archs; i++)
28222               {
28223                 /* Empty entry.  */
28224                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
28225                   continue;
28226                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
28227                   break;
28228               }
28229             if (i == nb_allowed_archs)
28230               {
28231                 as_bad (_("extension does not apply to the base architecture"));
28232                 return FALSE;
28233               }
28234
28235             /* Add or remove the extension.  */
28236             if (adding_value)
28237               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
28238             else
28239               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
28240
28241             /* Allowing Thumb division instructions for ARMv7 in autodetection
28242                rely on this break so that duplicate extensions (extensions
28243                with the same name as a previous extension in the list) are not
28244                considered for command-line parsing.  */
28245             break;
28246           }
28247
28248       if (opt->name == NULL)
28249         {
28250           /* Did we fail to find an extension because it wasn't specified in
28251              alphabetical order, or because it does not exist?  */
28252
28253           for (opt = arm_extensions; opt->name != NULL; opt++)
28254             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
28255               break;
28256
28257           if (opt->name == NULL)
28258             as_bad (_("unknown architectural extension `%s'"), str);
28259           else
28260             as_bad (_("architectural extensions must be specified in "
28261                       "alphabetical order"));
28262
28263           return FALSE;
28264         }
28265       else
28266         {
28267           /* We should skip the extension we've just matched the next time
28268              round.  */
28269           opt++;
28270         }
28271
28272       str = ext;
28273     };
28274
28275   return TRUE;
28276 }
28277
28278 static bfd_boolean
28279 arm_parse_cpu (const char *str)
28280 {
28281   const struct arm_cpu_option_table *opt;
28282   const char *ext = strchr (str, '+');
28283   size_t len;
28284
28285   if (ext != NULL)
28286     len = ext - str;
28287   else
28288     len = strlen (str);
28289
28290   if (len == 0)
28291     {
28292       as_bad (_("missing cpu name `%s'"), str);
28293       return FALSE;
28294     }
28295
28296   for (opt = arm_cpus; opt->name != NULL; opt++)
28297     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
28298       {
28299         mcpu_cpu_opt = &opt->value;
28300         if (mcpu_ext_opt == NULL)
28301           mcpu_ext_opt = XNEW (arm_feature_set);
28302         *mcpu_ext_opt = opt->ext;
28303         mcpu_fpu_opt = &opt->default_fpu;
28304         if (opt->canonical_name)
28305           {
28306             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
28307             strcpy (selected_cpu_name, opt->canonical_name);
28308           }
28309         else
28310           {
28311             size_t i;
28312
28313             if (len >= sizeof selected_cpu_name)
28314               len = (sizeof selected_cpu_name) - 1;
28315
28316             for (i = 0; i < len; i++)
28317               selected_cpu_name[i] = TOUPPER (opt->name[i]);
28318             selected_cpu_name[i] = 0;
28319           }
28320
28321         if (ext != NULL)
28322           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
28323
28324         return TRUE;
28325       }
28326
28327   as_bad (_("unknown cpu `%s'"), str);
28328   return FALSE;
28329 }
28330
28331 static bfd_boolean
28332 arm_parse_arch (const char *str)
28333 {
28334   const struct arm_arch_option_table *opt;
28335   const char *ext = strchr (str, '+');
28336   size_t len;
28337
28338   if (ext != NULL)
28339     len = ext - str;
28340   else
28341     len = strlen (str);
28342
28343   if (len == 0)
28344     {
28345       as_bad (_("missing architecture name `%s'"), str);
28346       return FALSE;
28347     }
28348
28349   for (opt = arm_archs; opt->name != NULL; opt++)
28350     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
28351       {
28352         march_cpu_opt = &opt->value;
28353         if (march_ext_opt == NULL)
28354           march_ext_opt = XNEW (arm_feature_set);
28355         *march_ext_opt = arm_arch_none;
28356         march_fpu_opt = &opt->default_fpu;
28357         strcpy (selected_cpu_name, opt->name);
28358
28359         if (ext != NULL)
28360           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
28361                                       opt->ext_table);
28362
28363         return TRUE;
28364       }
28365
28366   as_bad (_("unknown architecture `%s'\n"), str);
28367   return FALSE;
28368 }
28369
28370 static bfd_boolean
28371 arm_parse_fpu (const char * str)
28372 {
28373   const struct arm_option_fpu_value_table * opt;
28374
28375   for (opt = arm_fpus; opt->name != NULL; opt++)
28376     if (streq (opt->name, str))
28377       {
28378         mfpu_opt = &opt->value;
28379         return TRUE;
28380       }
28381
28382   as_bad (_("unknown floating point format `%s'\n"), str);
28383   return FALSE;
28384 }
28385
28386 static bfd_boolean
28387 arm_parse_float_abi (const char * str)
28388 {
28389   const struct arm_option_value_table * opt;
28390
28391   for (opt = arm_float_abis; opt->name != NULL; opt++)
28392     if (streq (opt->name, str))
28393       {
28394         mfloat_abi_opt = opt->value;
28395         return TRUE;
28396       }
28397
28398   as_bad (_("unknown floating point abi `%s'\n"), str);
28399   return FALSE;
28400 }
28401
28402 #ifdef OBJ_ELF
28403 static bfd_boolean
28404 arm_parse_eabi (const char * str)
28405 {
28406   const struct arm_option_value_table *opt;
28407
28408   for (opt = arm_eabis; opt->name != NULL; opt++)
28409     if (streq (opt->name, str))
28410       {
28411         meabi_flags = opt->value;
28412         return TRUE;
28413       }
28414   as_bad (_("unknown EABI `%s'\n"), str);
28415   return FALSE;
28416 }
28417 #endif
28418
28419 static bfd_boolean
28420 arm_parse_it_mode (const char * str)
28421 {
28422   bfd_boolean ret = TRUE;
28423
28424   if (streq ("arm", str))
28425     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
28426   else if (streq ("thumb", str))
28427     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
28428   else if (streq ("always", str))
28429     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
28430   else if (streq ("never", str))
28431     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
28432   else
28433     {
28434       as_bad (_("unknown implicit IT mode `%s', should be "\
28435                 "arm, thumb, always, or never."), str);
28436       ret = FALSE;
28437     }
28438
28439   return ret;
28440 }
28441
28442 static bfd_boolean
28443 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
28444 {
28445   codecomposer_syntax = TRUE;
28446   arm_comment_chars[0] = ';';
28447   arm_line_separator_chars[0] = 0;
28448   return TRUE;
28449 }
28450
28451 struct arm_long_option_table arm_long_opts[] =
28452 {
28453   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
28454    arm_parse_cpu, NULL},
28455   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
28456    arm_parse_arch, NULL},
28457   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
28458    arm_parse_fpu, NULL},
28459   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
28460    arm_parse_float_abi, NULL},
28461 #ifdef OBJ_ELF
28462   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
28463    arm_parse_eabi, NULL},
28464 #endif
28465   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
28466    arm_parse_it_mode, NULL},
28467   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
28468    arm_ccs_mode, NULL},
28469   {NULL, NULL, 0, NULL}
28470 };
28471
28472 int
28473 md_parse_option (int c, const char * arg)
28474 {
28475   struct arm_option_table *opt;
28476   const struct arm_legacy_option_table *fopt;
28477   struct arm_long_option_table *lopt;
28478
28479   switch (c)
28480     {
28481 #ifdef OPTION_EB
28482     case OPTION_EB:
28483       target_big_endian = 1;
28484       break;
28485 #endif
28486
28487 #ifdef OPTION_EL
28488     case OPTION_EL:
28489       target_big_endian = 0;
28490       break;
28491 #endif
28492
28493     case OPTION_FIX_V4BX:
28494       fix_v4bx = TRUE;
28495       break;
28496
28497 #ifdef OBJ_ELF
28498     case OPTION_FDPIC:
28499       arm_fdpic = TRUE;
28500       break;
28501 #endif /* OBJ_ELF */
28502
28503     case 'a':
28504       /* Listing option.  Just ignore these, we don't support additional
28505          ones.  */
28506       return 0;
28507
28508     default:
28509       for (opt = arm_opts; opt->option != NULL; opt++)
28510         {
28511           if (c == opt->option[0]
28512               && ((arg == NULL && opt->option[1] == 0)
28513                   || streq (arg, opt->option + 1)))
28514             {
28515               /* If the option is deprecated, tell the user.  */
28516               if (warn_on_deprecated && opt->deprecated != NULL)
28517                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
28518                            arg ? arg : "", _(opt->deprecated));
28519
28520               if (opt->var != NULL)
28521                 *opt->var = opt->value;
28522
28523               return 1;
28524             }
28525         }
28526
28527       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
28528         {
28529           if (c == fopt->option[0]
28530               && ((arg == NULL && fopt->option[1] == 0)
28531                   || streq (arg, fopt->option + 1)))
28532             {
28533               /* If the option is deprecated, tell the user.  */
28534               if (warn_on_deprecated && fopt->deprecated != NULL)
28535                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
28536                            arg ? arg : "", _(fopt->deprecated));
28537
28538               if (fopt->var != NULL)
28539                 *fopt->var = &fopt->value;
28540
28541               return 1;
28542             }
28543         }
28544
28545       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
28546         {
28547           /* These options are expected to have an argument.  */
28548           if (c == lopt->option[0]
28549               && arg != NULL
28550               && strncmp (arg, lopt->option + 1,
28551                           strlen (lopt->option + 1)) == 0)
28552             {
28553               /* If the option is deprecated, tell the user.  */
28554               if (warn_on_deprecated && lopt->deprecated != NULL)
28555                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
28556                            _(lopt->deprecated));
28557
28558               /* Call the sup-option parser.  */
28559               return lopt->func (arg + strlen (lopt->option) - 1);
28560             }
28561         }
28562
28563       return 0;
28564     }
28565
28566   return 1;
28567 }
28568
28569 void
28570 md_show_usage (FILE * fp)
28571 {
28572   struct arm_option_table *opt;
28573   struct arm_long_option_table *lopt;
28574
28575   fprintf (fp, _(" ARM-specific assembler options:\n"));
28576
28577   for (opt = arm_opts; opt->option != NULL; opt++)
28578     if (opt->help != NULL)
28579       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
28580
28581   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
28582     if (lopt->help != NULL)
28583       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
28584
28585 #ifdef OPTION_EB
28586   fprintf (fp, _("\
28587   -EB                     assemble code for a big-endian cpu\n"));
28588 #endif
28589
28590 #ifdef OPTION_EL
28591   fprintf (fp, _("\
28592   -EL                     assemble code for a little-endian cpu\n"));
28593 #endif
28594
28595   fprintf (fp, _("\
28596   --fix-v4bx              Allow BX in ARMv4 code\n"));
28597
28598 #ifdef OBJ_ELF
28599   fprintf (fp, _("\
28600   --fdpic                 generate an FDPIC object file\n"));
28601 #endif /* OBJ_ELF */
28602 }
28603
28604 #ifdef OBJ_ELF
28605
28606 typedef struct
28607 {
28608   int val;
28609   arm_feature_set flags;
28610 } cpu_arch_ver_table;
28611
28612 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
28613    chronologically for architectures, with an exception for ARMv6-M and
28614    ARMv6S-M due to legacy reasons.  No new architecture should have a
28615    special case.  This allows for build attribute selection results to be
28616    stable when new architectures are added.  */
28617 static const cpu_arch_ver_table cpu_arch_ver[] =
28618 {
28619     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
28620     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
28621     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
28622     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
28623     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
28624     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
28625     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
28626     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
28627     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
28628     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
28629     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
28630     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
28631     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
28632     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
28633     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
28634     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
28635     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
28636     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
28637     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
28638     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
28639     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
28640     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
28641     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
28642     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
28643
28644     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
28645        always selected build attributes to match those of ARMv6-M
28646        (resp. ARMv6S-M).  However, due to these architectures being a strict
28647        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
28648        would be selected when fully respecting chronology of architectures.
28649        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
28650        move them before ARMv7 architectures.  */
28651     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
28652     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
28653
28654     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
28655     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
28656     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
28657     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
28658     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
28659     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
28660     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
28661     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
28662     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
28663     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
28664     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
28665     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
28666     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
28667     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
28668     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
28669     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
28670     {-1,                      ARM_ARCH_NONE}
28671 };
28672
28673 /* Set an attribute if it has not already been set by the user.  */
28674
28675 static void
28676 aeabi_set_attribute_int (int tag, int value)
28677 {
28678   if (tag < 1
28679       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
28680       || !attributes_set_explicitly[tag])
28681     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
28682 }
28683
28684 static void
28685 aeabi_set_attribute_string (int tag, const char *value)
28686 {
28687   if (tag < 1
28688       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
28689       || !attributes_set_explicitly[tag])
28690     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
28691 }
28692
28693 /* Return whether features in the *NEEDED feature set are available via
28694    extensions for the architecture whose feature set is *ARCH_FSET.  */
28695
28696 static bfd_boolean
28697 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
28698                             const arm_feature_set *needed)
28699 {
28700   int i, nb_allowed_archs;
28701   arm_feature_set ext_fset;
28702   const struct arm_option_extension_value_table *opt;
28703
28704   ext_fset = arm_arch_none;
28705   for (opt = arm_extensions; opt->name != NULL; opt++)
28706     {
28707       /* Extension does not provide any feature we need.  */
28708       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
28709         continue;
28710
28711       nb_allowed_archs =
28712         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
28713       for (i = 0; i < nb_allowed_archs; i++)
28714         {
28715           /* Empty entry.  */
28716           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
28717             break;
28718
28719           /* Extension is available, add it.  */
28720           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
28721             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
28722         }
28723     }
28724
28725   /* Can we enable all features in *needed?  */
28726   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
28727 }
28728
28729 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
28730    a given architecture feature set *ARCH_EXT_FSET including extension feature
28731    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
28732    - if true, check for an exact match of the architecture modulo extensions;
28733    - otherwise, select build attribute value of the first superset
28734      architecture released so that results remains stable when new architectures
28735      are added.
28736    For -march/-mcpu=all the build attribute value of the most featureful
28737    architecture is returned.  Tag_CPU_arch_profile result is returned in
28738    PROFILE.  */
28739
28740 static int
28741 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
28742                               const arm_feature_set *ext_fset,
28743                               char *profile, int exact_match)
28744 {
28745   arm_feature_set arch_fset;
28746   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
28747
28748   /* Select most featureful architecture with all its extensions if building
28749      for -march=all as the feature sets used to set build attributes.  */
28750   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
28751     {
28752       /* Force revisiting of decision for each new architecture.  */
28753       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
28754       *profile = 'A';
28755       return TAG_CPU_ARCH_V8;
28756     }
28757
28758   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
28759
28760   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
28761     {
28762       arm_feature_set known_arch_fset;
28763
28764       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
28765       if (exact_match)
28766         {
28767           /* Base architecture match user-specified architecture and
28768              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
28769           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
28770             {
28771               p_ver_ret = p_ver;
28772               goto found;
28773             }
28774           /* Base architecture match user-specified architecture only
28775              (eg. ARMv6-M in the same case as above).  Record it in case we
28776              find a match with above condition.  */
28777           else if (p_ver_ret == NULL
28778                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
28779             p_ver_ret = p_ver;
28780         }
28781       else
28782         {
28783
28784           /* Architecture has all features wanted.  */
28785           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
28786             {
28787               arm_feature_set added_fset;
28788
28789               /* Compute features added by this architecture over the one
28790                  recorded in p_ver_ret.  */
28791               if (p_ver_ret != NULL)
28792                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
28793                                    p_ver_ret->flags);
28794               /* First architecture that match incl. with extensions, or the
28795                  only difference in features over the recorded match is
28796                  features that were optional and are now mandatory.  */
28797               if (p_ver_ret == NULL
28798                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
28799                 {
28800                   p_ver_ret = p_ver;
28801                   goto found;
28802                 }
28803             }
28804           else if (p_ver_ret == NULL)
28805             {
28806               arm_feature_set needed_ext_fset;
28807
28808               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
28809
28810               /* Architecture has all features needed when using some
28811                  extensions.  Record it and continue searching in case there
28812                  exist an architecture providing all needed features without
28813                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
28814                  OS extension).  */
28815               if (have_ext_for_needed_feat_p (&known_arch_fset,
28816                                               &needed_ext_fset))
28817                 p_ver_ret = p_ver;
28818             }
28819         }
28820     }
28821
28822   if (p_ver_ret == NULL)
28823     return -1;
28824
28825 found:
28826   /* Tag_CPU_arch_profile.  */
28827   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
28828       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
28829       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
28830           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
28831     *profile = 'A';
28832   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
28833     *profile = 'R';
28834   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
28835     *profile = 'M';
28836   else
28837     *profile = '\0';
28838   return p_ver_ret->val;
28839 }
28840
28841 /* Set the public EABI object attributes.  */
28842
28843 static void
28844 aeabi_set_public_attributes (void)
28845 {
28846   char profile = '\0';
28847   int arch = -1;
28848   int virt_sec = 0;
28849   int fp16_optional = 0;
28850   int skip_exact_match = 0;
28851   arm_feature_set flags, flags_arch, flags_ext;
28852
28853   /* Autodetection mode, choose the architecture based the instructions
28854      actually used.  */
28855   if (no_cpu_selected ())
28856     {
28857       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
28858
28859       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
28860         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
28861
28862       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
28863         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
28864
28865       /* Code run during relaxation relies on selected_cpu being set.  */
28866       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
28867       flags_ext = arm_arch_none;
28868       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
28869       selected_ext = flags_ext;
28870       selected_cpu = flags;
28871     }
28872   /* Otherwise, choose the architecture based on the capabilities of the
28873      requested cpu.  */
28874   else
28875     {
28876       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
28877       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
28878       flags_ext = selected_ext;
28879       flags = selected_cpu;
28880     }
28881   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
28882
28883   /* Allow the user to override the reported architecture.  */
28884   if (!ARM_FEATURE_ZERO (selected_object_arch))
28885     {
28886       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
28887       flags_ext = arm_arch_none;
28888     }
28889   else
28890     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
28891
28892   /* When this function is run again after relaxation has happened there is no
28893      way to determine whether an architecture or CPU was specified by the user:
28894      - selected_cpu is set above for relaxation to work;
28895      - march_cpu_opt is not set if only -mcpu or .cpu is used;
28896      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
28897      Therefore, if not in -march=all case we first try an exact match and fall
28898      back to autodetection.  */
28899   if (!skip_exact_match)
28900     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
28901   if (arch == -1)
28902     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
28903   if (arch == -1)
28904     as_bad (_("no architecture contains all the instructions used\n"));
28905
28906   /* Tag_CPU_name.  */
28907   if (selected_cpu_name[0])
28908     {
28909       char *q;
28910
28911       q = selected_cpu_name;
28912       if (strncmp (q, "armv", 4) == 0)
28913         {
28914           int i;
28915
28916           q += 4;
28917           for (i = 0; q[i]; i++)
28918             q[i] = TOUPPER (q[i]);
28919         }
28920       aeabi_set_attribute_string (Tag_CPU_name, q);
28921     }
28922
28923   /* Tag_CPU_arch.  */
28924   aeabi_set_attribute_int (Tag_CPU_arch, arch);
28925
28926   /* Tag_CPU_arch_profile.  */
28927   if (profile != '\0')
28928     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
28929
28930   /* Tag_DSP_extension.  */
28931   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
28932     aeabi_set_attribute_int (Tag_DSP_extension, 1);
28933
28934   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
28935   /* Tag_ARM_ISA_use.  */
28936   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
28937       || ARM_FEATURE_ZERO (flags_arch))
28938     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
28939
28940   /* Tag_THUMB_ISA_use.  */
28941   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
28942       || ARM_FEATURE_ZERO (flags_arch))
28943     {
28944       int thumb_isa_use;
28945
28946       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
28947           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
28948         thumb_isa_use = 3;
28949       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
28950         thumb_isa_use = 2;
28951       else
28952         thumb_isa_use = 1;
28953       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
28954     }
28955
28956   /* Tag_VFP_arch.  */
28957   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
28958     aeabi_set_attribute_int (Tag_VFP_arch,
28959                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
28960                              ? 7 : 8);
28961   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
28962     aeabi_set_attribute_int (Tag_VFP_arch,
28963                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
28964                              ? 5 : 6);
28965   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
28966     {
28967       fp16_optional = 1;
28968       aeabi_set_attribute_int (Tag_VFP_arch, 3);
28969     }
28970   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
28971     {
28972       aeabi_set_attribute_int (Tag_VFP_arch, 4);
28973       fp16_optional = 1;
28974     }
28975   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
28976     aeabi_set_attribute_int (Tag_VFP_arch, 2);
28977   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
28978            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
28979     aeabi_set_attribute_int (Tag_VFP_arch, 1);
28980
28981   /* Tag_ABI_HardFP_use.  */
28982   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
28983       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
28984     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
28985
28986   /* Tag_WMMX_arch.  */
28987   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
28988     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
28989   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
28990     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
28991
28992   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
28993   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
28994     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
28995   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
28996     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
28997   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
28998     {
28999       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
29000         {
29001           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
29002         }
29003       else
29004         {
29005           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
29006           fp16_optional = 1;
29007         }
29008     }
29009
29010   if (ARM_CPU_HAS_FEATURE (flags, mve_fp_ext))
29011     aeabi_set_attribute_int (Tag_MVE_arch, 2);
29012   else if (ARM_CPU_HAS_FEATURE (flags, mve_ext))
29013     aeabi_set_attribute_int (Tag_MVE_arch, 1);
29014
29015   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
29016   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
29017     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
29018
29019   /* Tag_DIV_use.
29020
29021      We set Tag_DIV_use to two when integer divide instructions have been used
29022      in ARM state, or when Thumb integer divide instructions have been used,
29023      but we have no architecture profile set, nor have we any ARM instructions.
29024
29025      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
29026      by the base architecture.
29027
29028      For new architectures we will have to check these tests.  */
29029   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
29030   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
29031       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
29032     aeabi_set_attribute_int (Tag_DIV_use, 0);
29033   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
29034            || (profile == '\0'
29035                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
29036                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
29037     aeabi_set_attribute_int (Tag_DIV_use, 2);
29038
29039   /* Tag_MP_extension_use.  */
29040   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
29041     aeabi_set_attribute_int (Tag_MPextension_use, 1);
29042
29043   /* Tag Virtualization_use.  */
29044   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
29045     virt_sec |= 1;
29046   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
29047     virt_sec |= 2;
29048   if (virt_sec != 0)
29049     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
29050 }
29051
29052 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
29053    finished and free extension feature bits which will not be used anymore.  */
29054
29055 void
29056 arm_md_post_relax (void)
29057 {
29058   aeabi_set_public_attributes ();
29059   XDELETE (mcpu_ext_opt);
29060   mcpu_ext_opt = NULL;
29061   XDELETE (march_ext_opt);
29062   march_ext_opt = NULL;
29063 }
29064
29065 /* Add the default contents for the .ARM.attributes section.  */
29066
29067 void
29068 arm_md_end (void)
29069 {
29070   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
29071     return;
29072
29073   aeabi_set_public_attributes ();
29074 }
29075 #endif /* OBJ_ELF */
29076
29077 /* Parse a .cpu directive.  */
29078
29079 static void
29080 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
29081 {
29082   const struct arm_cpu_option_table *opt;
29083   char *name;
29084   char saved_char;
29085
29086   name = input_line_pointer;
29087   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
29088     input_line_pointer++;
29089   saved_char = *input_line_pointer;
29090   *input_line_pointer = 0;
29091
29092   /* Skip the first "all" entry.  */
29093   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
29094     if (streq (opt->name, name))
29095       {
29096         selected_arch = opt->value;
29097         selected_ext = opt->ext;
29098         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
29099         if (opt->canonical_name)
29100           strcpy (selected_cpu_name, opt->canonical_name);
29101         else
29102           {
29103             int i;
29104             for (i = 0; opt->name[i]; i++)
29105               selected_cpu_name[i] = TOUPPER (opt->name[i]);
29106
29107             selected_cpu_name[i] = 0;
29108           }
29109         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29110
29111         *input_line_pointer = saved_char;
29112         demand_empty_rest_of_line ();
29113         return;
29114       }
29115   as_bad (_("unknown cpu `%s'"), name);
29116   *input_line_pointer = saved_char;
29117   ignore_rest_of_line ();
29118 }
29119
29120 /* Parse a .arch directive.  */
29121
29122 static void
29123 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
29124 {
29125   const struct arm_arch_option_table *opt;
29126   char saved_char;
29127   char *name;
29128
29129   name = input_line_pointer;
29130   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
29131     input_line_pointer++;
29132   saved_char = *input_line_pointer;
29133   *input_line_pointer = 0;
29134
29135   /* Skip the first "all" entry.  */
29136   for (opt = arm_archs + 1; opt->name != NULL; opt++)
29137     if (streq (opt->name, name))
29138       {
29139         selected_arch = opt->value;
29140         selected_ext = arm_arch_none;
29141         selected_cpu = selected_arch;
29142         strcpy (selected_cpu_name, opt->name);
29143         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29144         *input_line_pointer = saved_char;
29145         demand_empty_rest_of_line ();
29146         return;
29147       }
29148
29149   as_bad (_("unknown architecture `%s'\n"), name);
29150   *input_line_pointer = saved_char;
29151   ignore_rest_of_line ();
29152 }
29153
29154 /* Parse a .object_arch directive.  */
29155
29156 static void
29157 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
29158 {
29159   const struct arm_arch_option_table *opt;
29160   char saved_char;
29161   char *name;
29162
29163   name = input_line_pointer;
29164   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
29165     input_line_pointer++;
29166   saved_char = *input_line_pointer;
29167   *input_line_pointer = 0;
29168
29169   /* Skip the first "all" entry.  */
29170   for (opt = arm_archs + 1; opt->name != NULL; opt++)
29171     if (streq (opt->name, name))
29172       {
29173         selected_object_arch = opt->value;
29174         *input_line_pointer = saved_char;
29175         demand_empty_rest_of_line ();
29176         return;
29177       }
29178
29179   as_bad (_("unknown architecture `%s'\n"), name);
29180   *input_line_pointer = saved_char;
29181   ignore_rest_of_line ();
29182 }
29183
29184 /* Parse a .arch_extension directive.  */
29185
29186 static void
29187 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
29188 {
29189   const struct arm_option_extension_value_table *opt;
29190   char saved_char;
29191   char *name;
29192   int adding_value = 1;
29193
29194   name = input_line_pointer;
29195   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
29196     input_line_pointer++;
29197   saved_char = *input_line_pointer;
29198   *input_line_pointer = 0;
29199
29200   if (strlen (name) >= 2
29201       && strncmp (name, "no", 2) == 0)
29202     {
29203       adding_value = 0;
29204       name += 2;
29205     }
29206
29207   for (opt = arm_extensions; opt->name != NULL; opt++)
29208     if (streq (opt->name, name))
29209       {
29210         int i, nb_allowed_archs =
29211           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
29212         for (i = 0; i < nb_allowed_archs; i++)
29213           {
29214             /* Empty entry.  */
29215             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
29216               continue;
29217             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
29218               break;
29219           }
29220
29221         if (i == nb_allowed_archs)
29222           {
29223             as_bad (_("architectural extension `%s' is not allowed for the "
29224                       "current base architecture"), name);
29225             break;
29226           }
29227
29228         if (adding_value)
29229           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
29230                                   opt->merge_value);
29231         else
29232           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
29233
29234         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
29235         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29236         *input_line_pointer = saved_char;
29237         demand_empty_rest_of_line ();
29238         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
29239            on this return so that duplicate extensions (extensions with the
29240            same name as a previous extension in the list) are not considered
29241            for command-line parsing.  */
29242         return;
29243       }
29244
29245   if (opt->name == NULL)
29246     as_bad (_("unknown architecture extension `%s'\n"), name);
29247
29248   *input_line_pointer = saved_char;
29249   ignore_rest_of_line ();
29250 }
29251
29252 /* Parse a .fpu directive.  */
29253
29254 static void
29255 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
29256 {
29257   const struct arm_option_fpu_value_table *opt;
29258   char saved_char;
29259   char *name;
29260
29261   name = input_line_pointer;
29262   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
29263     input_line_pointer++;
29264   saved_char = *input_line_pointer;
29265   *input_line_pointer = 0;
29266
29267   for (opt = arm_fpus; opt->name != NULL; opt++)
29268     if (streq (opt->name, name))
29269       {
29270         selected_fpu = opt->value;
29271 #ifndef CPU_DEFAULT
29272         if (no_cpu_selected ())
29273           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
29274         else
29275 #endif
29276           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
29277         *input_line_pointer = saved_char;
29278         demand_empty_rest_of_line ();
29279         return;
29280       }
29281
29282   as_bad (_("unknown floating point format `%s'\n"), name);
29283   *input_line_pointer = saved_char;
29284   ignore_rest_of_line ();
29285 }
29286
29287 /* Copy symbol information.  */
29288
29289 void
29290 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
29291 {
29292   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
29293 }
29294
29295 #ifdef OBJ_ELF
29296 /* Given a symbolic attribute NAME, return the proper integer value.
29297    Returns -1 if the attribute is not known.  */
29298
29299 int
29300 arm_convert_symbolic_attribute (const char *name)
29301 {
29302   static const struct
29303   {
29304     const char * name;
29305     const int    tag;
29306   }
29307   attribute_table[] =
29308     {
29309       /* When you modify this table you should
29310          also modify the list in doc/c-arm.texi.  */
29311 #define T(tag) {#tag, tag}
29312       T (Tag_CPU_raw_name),
29313       T (Tag_CPU_name),
29314       T (Tag_CPU_arch),
29315       T (Tag_CPU_arch_profile),
29316       T (Tag_ARM_ISA_use),
29317       T (Tag_THUMB_ISA_use),
29318       T (Tag_FP_arch),
29319       T (Tag_VFP_arch),
29320       T (Tag_WMMX_arch),
29321       T (Tag_Advanced_SIMD_arch),
29322       T (Tag_PCS_config),
29323       T (Tag_ABI_PCS_R9_use),
29324       T (Tag_ABI_PCS_RW_data),
29325       T (Tag_ABI_PCS_RO_data),
29326       T (Tag_ABI_PCS_GOT_use),
29327       T (Tag_ABI_PCS_wchar_t),
29328       T (Tag_ABI_FP_rounding),
29329       T (Tag_ABI_FP_denormal),
29330       T (Tag_ABI_FP_exceptions),
29331       T (Tag_ABI_FP_user_exceptions),
29332       T (Tag_ABI_FP_number_model),
29333       T (Tag_ABI_align_needed),
29334       T (Tag_ABI_align8_needed),
29335       T (Tag_ABI_align_preserved),
29336       T (Tag_ABI_align8_preserved),
29337       T (Tag_ABI_enum_size),
29338       T (Tag_ABI_HardFP_use),
29339       T (Tag_ABI_VFP_args),
29340       T (Tag_ABI_WMMX_args),
29341       T (Tag_ABI_optimization_goals),
29342       T (Tag_ABI_FP_optimization_goals),
29343       T (Tag_compatibility),
29344       T (Tag_CPU_unaligned_access),
29345       T (Tag_FP_HP_extension),
29346       T (Tag_VFP_HP_extension),
29347       T (Tag_ABI_FP_16bit_format),
29348       T (Tag_MPextension_use),
29349       T (Tag_DIV_use),
29350       T (Tag_nodefaults),
29351       T (Tag_also_compatible_with),
29352       T (Tag_conformance),
29353       T (Tag_T2EE_use),
29354       T (Tag_Virtualization_use),
29355       T (Tag_DSP_extension),
29356       T (Tag_MVE_arch),
29357       /* We deliberately do not include Tag_MPextension_use_legacy.  */
29358 #undef T
29359     };
29360   unsigned int i;
29361
29362   if (name == NULL)
29363     return -1;
29364
29365   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
29366     if (streq (name, attribute_table[i].name))
29367       return attribute_table[i].tag;
29368
29369   return -1;
29370 }
29371
29372 /* Apply sym value for relocations only in the case that they are for
29373    local symbols in the same segment as the fixup and you have the
29374    respective architectural feature for blx and simple switches.  */
29375
29376 int
29377 arm_apply_sym_value (struct fix * fixP, segT this_seg)
29378 {
29379   if (fixP->fx_addsy
29380       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
29381       /* PR 17444: If the local symbol is in a different section then a reloc
29382          will always be generated for it, so applying the symbol value now
29383          will result in a double offset being stored in the relocation.  */
29384       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
29385       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
29386     {
29387       switch (fixP->fx_r_type)
29388         {
29389         case BFD_RELOC_ARM_PCREL_BLX:
29390         case BFD_RELOC_THUMB_PCREL_BRANCH23:
29391           if (ARM_IS_FUNC (fixP->fx_addsy))
29392             return 1;
29393           break;
29394
29395         case BFD_RELOC_ARM_PCREL_CALL:
29396         case BFD_RELOC_THUMB_PCREL_BLX:
29397           if (THUMB_IS_FUNC (fixP->fx_addsy))
29398             return 1;
29399           break;
29400
29401         default:
29402           break;
29403         }
29404
29405     }
29406   return 0;
29407 }
29408 #endif /* OBJ_ELF */