[GAS, ARM, 2/16] Add CLI extension support for Armv8.1-M Mainline
[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 /* Instructions in ARMv8-M only found in M profile architectures.  */
239 static const arm_feature_set arm_ext_v8m_m_only =
240   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
241 static const arm_feature_set arm_ext_v6t2_v8m =
242   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V6T2_V8M);
243 /* Instructions shared between ARMv8-A and ARMv8-M.  */
244 static const arm_feature_set arm_ext_atomics =
245   ARM_FEATURE_CORE_HIGH (ARM_EXT2_ATOMICS);
246 #ifdef OBJ_ELF
247 /* DSP instructions Tag_DSP_extension refers to.  */
248 static const arm_feature_set arm_ext_dsp =
249   ARM_FEATURE_CORE_LOW (ARM_EXT_V5E | ARM_EXT_V5ExP | ARM_EXT_V6_DSP);
250 #endif
251 static const arm_feature_set arm_ext_ras =
252   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS);
253 /* FP16 instructions.  */
254 static const arm_feature_set arm_ext_fp16 =
255   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST);
256 static const arm_feature_set arm_ext_fp16_fml =
257   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_FML);
258 static const arm_feature_set arm_ext_v8_2 =
259   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_2A);
260 static const arm_feature_set arm_ext_v8_3 =
261   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_3A);
262 static const arm_feature_set arm_ext_sb =
263   ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB);
264 static const arm_feature_set arm_ext_predres =
265   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES);
266
267 static const arm_feature_set arm_arch_any = ARM_ANY;
268 #ifdef OBJ_ELF
269 static const arm_feature_set fpu_any = FPU_ANY;
270 #endif
271 static const arm_feature_set arm_arch_full ATTRIBUTE_UNUSED = ARM_FEATURE (-1, -1, -1);
272 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
273 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
274
275 static const arm_feature_set arm_cext_iwmmxt2 =
276   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
277 static const arm_feature_set arm_cext_iwmmxt =
278   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
279 static const arm_feature_set arm_cext_xscale =
280   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
281 static const arm_feature_set arm_cext_maverick =
282   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
283 static const arm_feature_set fpu_fpa_ext_v1 =
284   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
285 static const arm_feature_set fpu_fpa_ext_v2 =
286   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
287 static const arm_feature_set fpu_vfp_ext_v1xd =
288   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
289 static const arm_feature_set fpu_vfp_ext_v1 =
290   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
291 static const arm_feature_set fpu_vfp_ext_v2 =
292   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
293 static const arm_feature_set fpu_vfp_ext_v3xd =
294   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
295 static const arm_feature_set fpu_vfp_ext_v3 =
296   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
297 static const arm_feature_set fpu_vfp_ext_d32 =
298   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
299 static const arm_feature_set fpu_neon_ext_v1 =
300   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
301 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
302   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
303 #ifdef OBJ_ELF
304 static const arm_feature_set fpu_vfp_fp16 =
305   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
306 static const arm_feature_set fpu_neon_ext_fma =
307   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
308 #endif
309 static const arm_feature_set fpu_vfp_ext_fma =
310   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
311 static const arm_feature_set fpu_vfp_ext_armv8 =
312   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
313 static const arm_feature_set fpu_vfp_ext_armv8xd =
314   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
315 static const arm_feature_set fpu_neon_ext_armv8 =
316   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
317 static const arm_feature_set fpu_crypto_ext_armv8 =
318   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
319 static const arm_feature_set crc_ext_armv8 =
320   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
321 static const arm_feature_set fpu_neon_ext_v8_1 =
322   ARM_FEATURE_COPROC (FPU_NEON_EXT_RDMA);
323 static const arm_feature_set fpu_neon_ext_dotprod =
324   ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD);
325
326 static int mfloat_abi_opt = -1;
327 /* Architecture feature bits selected by the last -mcpu/-march or .cpu/.arch
328    directive.  */
329 static arm_feature_set selected_arch = ARM_ARCH_NONE;
330 /* Extension feature bits selected by the last -mcpu/-march or .arch_extension
331    directive.  */
332 static arm_feature_set selected_ext = ARM_ARCH_NONE;
333 /* Feature bits selected by the last -mcpu/-march or by the combination of the
334    last .cpu/.arch directive .arch_extension directives since that
335    directive.  */
336 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
337 /* FPU feature bits selected by the last -mfpu or .fpu directive.  */
338 static arm_feature_set selected_fpu = FPU_NONE;
339 /* Feature bits selected by the last .object_arch directive.  */
340 static arm_feature_set selected_object_arch = ARM_ARCH_NONE;
341 /* Must be long enough to hold any of the names in arm_cpus.  */
342 static char selected_cpu_name[20];
343
344 extern FLONUM_TYPE generic_floating_point_number;
345
346 /* Return if no cpu was selected on command-line.  */
347 static bfd_boolean
348 no_cpu_selected (void)
349 {
350   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
351 }
352
353 #ifdef OBJ_ELF
354 # ifdef EABI_DEFAULT
355 static int meabi_flags = EABI_DEFAULT;
356 # else
357 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
358 # endif
359
360 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
361
362 bfd_boolean
363 arm_is_eabi (void)
364 {
365   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
366 }
367 #endif
368
369 #ifdef OBJ_ELF
370 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
371 symbolS * GOT_symbol;
372 #endif
373
374 /* 0: assemble for ARM,
375    1: assemble for Thumb,
376    2: assemble for Thumb even though target CPU does not support thumb
377       instructions.  */
378 static int thumb_mode = 0;
379 /* A value distinct from the possible values for thumb_mode that we
380    can use to record whether thumb_mode has been copied into the
381    tc_frag_data field of a frag.  */
382 #define MODE_RECORDED (1 << 4)
383
384 /* Specifies the intrinsic IT insn behavior mode.  */
385 enum implicit_it_mode
386 {
387   IMPLICIT_IT_MODE_NEVER  = 0x00,
388   IMPLICIT_IT_MODE_ARM    = 0x01,
389   IMPLICIT_IT_MODE_THUMB  = 0x02,
390   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
391 };
392 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
393
394 /* If unified_syntax is true, we are processing the new unified
395    ARM/Thumb syntax.  Important differences from the old ARM mode:
396
397      - Immediate operands do not require a # prefix.
398      - Conditional affixes always appear at the end of the
399        instruction.  (For backward compatibility, those instructions
400        that formerly had them in the middle, continue to accept them
401        there.)
402      - The IT instruction may appear, and if it does is validated
403        against subsequent conditional affixes.  It does not generate
404        machine code.
405
406    Important differences from the old Thumb mode:
407
408      - Immediate operands do not require a # prefix.
409      - Most of the V6T2 instructions are only available in unified mode.
410      - The .N and .W suffixes are recognized and honored (it is an error
411        if they cannot be honored).
412      - All instructions set the flags if and only if they have an 's' affix.
413      - Conditional affixes may be used.  They are validated against
414        preceding IT instructions.  Unlike ARM mode, you cannot use a
415        conditional affix except in the scope of an IT instruction.  */
416
417 static bfd_boolean unified_syntax = FALSE;
418
419 /* An immediate operand can start with #, and ld*, st*, pld operands
420    can contain [ and ].  We need to tell APP not to elide whitespace
421    before a [, which can appear as the first operand for pld.
422    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
423 const char arm_symbol_chars[] = "#[]{}";
424
425 enum neon_el_type
426 {
427   NT_invtype,
428   NT_untyped,
429   NT_integer,
430   NT_float,
431   NT_poly,
432   NT_signed,
433   NT_unsigned
434 };
435
436 struct neon_type_el
437 {
438   enum neon_el_type type;
439   unsigned size;
440 };
441
442 #define NEON_MAX_TYPE_ELS 4
443
444 struct neon_type
445 {
446   struct neon_type_el el[NEON_MAX_TYPE_ELS];
447   unsigned elems;
448 };
449
450 enum it_instruction_type
451 {
452    OUTSIDE_IT_INSN,
453    INSIDE_IT_INSN,
454    INSIDE_IT_LAST_INSN,
455    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
456                               if inside, should be the last one.  */
457    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
458                               i.e. BKPT and NOP.  */
459    IT_INSN                 /* The IT insn has been parsed.  */
460 };
461
462 /* The maximum number of operands we need.  */
463 #define ARM_IT_MAX_OPERANDS 6
464
465 struct arm_it
466 {
467   const char *  error;
468   unsigned long instruction;
469   int           size;
470   int           size_req;
471   int           cond;
472   /* "uncond_value" is set to the value in place of the conditional field in
473      unconditional versions of the instruction, or -1 if nothing is
474      appropriate.  */
475   int           uncond_value;
476   struct neon_type vectype;
477   /* This does not indicate an actual NEON instruction, only that
478      the mnemonic accepts neon-style type suffixes.  */
479   int           is_neon;
480   /* Set to the opcode if the instruction needs relaxation.
481      Zero if the instruction is not relaxed.  */
482   unsigned long relax;
483   struct
484   {
485     bfd_reloc_code_real_type type;
486     expressionS              exp;
487     int                      pc_rel;
488   } reloc;
489
490   enum it_instruction_type it_insn_type;
491
492   struct
493   {
494     unsigned reg;
495     signed int imm;
496     struct neon_type_el vectype;
497     unsigned present    : 1;  /* Operand present.  */
498     unsigned isreg      : 1;  /* Operand was a register.  */
499     unsigned immisreg   : 1;  /* .imm field is a second register.  */
500     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
501     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
502     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
503     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
504        instructions. This allows us to disambiguate ARM <-> vector insns.  */
505     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
506     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
507     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
508     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
509     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
510     unsigned writeback  : 1;  /* Operand has trailing !  */
511     unsigned preind     : 1;  /* Preindexed address.  */
512     unsigned postind    : 1;  /* Postindexed address.  */
513     unsigned negative   : 1;  /* Index register was negated.  */
514     unsigned shifted    : 1;  /* Shift applied to operation.  */
515     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
516   } operands[ARM_IT_MAX_OPERANDS];
517 };
518
519 static struct arm_it inst;
520
521 #define NUM_FLOAT_VALS 8
522
523 const char * fp_const[] =
524 {
525   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
526 };
527
528 /* Number of littlenums required to hold an extended precision number.  */
529 #define MAX_LITTLENUMS 6
530
531 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
532
533 #define FAIL    (-1)
534 #define SUCCESS (0)
535
536 #define SUFF_S 1
537 #define SUFF_D 2
538 #define SUFF_E 3
539 #define SUFF_P 4
540
541 #define CP_T_X   0x00008000
542 #define CP_T_Y   0x00400000
543
544 #define CONDS_BIT        0x00100000
545 #define LOAD_BIT         0x00100000
546
547 #define DOUBLE_LOAD_FLAG 0x00000001
548
549 struct asm_cond
550 {
551   const char *   template_name;
552   unsigned long  value;
553 };
554
555 #define COND_ALWAYS 0xE
556
557 struct asm_psr
558 {
559   const char *   template_name;
560   unsigned long  field;
561 };
562
563 struct asm_barrier_opt
564 {
565   const char *    template_name;
566   unsigned long   value;
567   const arm_feature_set arch;
568 };
569
570 /* The bit that distinguishes CPSR and SPSR.  */
571 #define SPSR_BIT   (1 << 22)
572
573 /* The individual PSR flag bits.  */
574 #define PSR_c   (1 << 16)
575 #define PSR_x   (1 << 17)
576 #define PSR_s   (1 << 18)
577 #define PSR_f   (1 << 19)
578
579 struct reloc_entry
580 {
581   const char *              name;
582   bfd_reloc_code_real_type  reloc;
583 };
584
585 enum vfp_reg_pos
586 {
587   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
588   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
589 };
590
591 enum vfp_ldstm_type
592 {
593   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
594 };
595
596 /* Bits for DEFINED field in neon_typed_alias.  */
597 #define NTA_HASTYPE  1
598 #define NTA_HASINDEX 2
599
600 struct neon_typed_alias
601 {
602   unsigned char        defined;
603   unsigned char        index;
604   struct neon_type_el  eltype;
605 };
606
607 /* ARM register categories.  This includes coprocessor numbers and various
608    architecture extensions' registers.  Each entry should have an error message
609    in reg_expected_msgs below.  */
610 enum arm_reg_type
611 {
612   REG_TYPE_RN,
613   REG_TYPE_CP,
614   REG_TYPE_CN,
615   REG_TYPE_FN,
616   REG_TYPE_VFS,
617   REG_TYPE_VFD,
618   REG_TYPE_NQ,
619   REG_TYPE_VFSD,
620   REG_TYPE_NDQ,
621   REG_TYPE_NSD,
622   REG_TYPE_NSDQ,
623   REG_TYPE_VFC,
624   REG_TYPE_MVF,
625   REG_TYPE_MVD,
626   REG_TYPE_MVFX,
627   REG_TYPE_MVDX,
628   REG_TYPE_MVAX,
629   REG_TYPE_DSPSC,
630   REG_TYPE_MMXWR,
631   REG_TYPE_MMXWC,
632   REG_TYPE_MMXWCG,
633   REG_TYPE_XSCALE,
634   REG_TYPE_RNB
635 };
636
637 /* Structure for a hash table entry for a register.
638    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
639    information which states whether a vector type or index is specified (for a
640    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
641 struct reg_entry
642 {
643   const char *               name;
644   unsigned int               number;
645   unsigned char              type;
646   unsigned char              builtin;
647   struct neon_typed_alias *  neon;
648 };
649
650 /* Diagnostics used when we don't get a register of the expected type.  */
651 const char * const reg_expected_msgs[] =
652 {
653   [REG_TYPE_RN]     = N_("ARM register expected"),
654   [REG_TYPE_CP]     = N_("bad or missing co-processor number"),
655   [REG_TYPE_CN]     = N_("co-processor register expected"),
656   [REG_TYPE_FN]     = N_("FPA register expected"),
657   [REG_TYPE_VFS]    = N_("VFP single precision register expected"),
658   [REG_TYPE_VFD]    = N_("VFP/Neon double precision register expected"),
659   [REG_TYPE_NQ]     = N_("Neon quad precision register expected"),
660   [REG_TYPE_VFSD]   = N_("VFP single or double precision register expected"),
661   [REG_TYPE_NDQ]    = N_("Neon double or quad precision register expected"),
662   [REG_TYPE_NSD]    = N_("Neon single or double precision register expected"),
663   [REG_TYPE_NSDQ]   = N_("VFP single, double or Neon quad precision register"
664                          " expected"),
665   [REG_TYPE_VFC]    = N_("VFP system register expected"),
666   [REG_TYPE_MVF]    = N_("Maverick MVF register expected"),
667   [REG_TYPE_MVD]    = N_("Maverick MVD register expected"),
668   [REG_TYPE_MVFX]   = N_("Maverick MVFX register expected"),
669   [REG_TYPE_MVDX]   = N_("Maverick MVDX register expected"),
670   [REG_TYPE_MVAX]   = N_("Maverick MVAX register expected"),
671   [REG_TYPE_DSPSC]  = N_("Maverick DSPSC register expected"),
672   [REG_TYPE_MMXWR]  = N_("iWMMXt data register expected"),
673   [REG_TYPE_MMXWC]  = N_("iWMMXt control register expected"),
674   [REG_TYPE_MMXWCG] = N_("iWMMXt scalar register expected"),
675   [REG_TYPE_XSCALE] = N_("XScale accumulator register expected"),
676   [REG_TYPE_RNB]    = N_("")
677 };
678
679 /* Some well known registers that we refer to directly elsewhere.  */
680 #define REG_R12 12
681 #define REG_SP  13
682 #define REG_LR  14
683 #define REG_PC  15
684
685 /* ARM instructions take 4bytes in the object file, Thumb instructions
686    take 2:  */
687 #define INSN_SIZE       4
688
689 struct asm_opcode
690 {
691   /* Basic string to match.  */
692   const char * template_name;
693
694   /* Parameters to instruction.  */
695   unsigned int operands[8];
696
697   /* Conditional tag - see opcode_lookup.  */
698   unsigned int tag : 4;
699
700   /* Basic instruction code.  */
701   unsigned int avalue : 28;
702
703   /* Thumb-format instruction code.  */
704   unsigned int tvalue;
705
706   /* Which architecture variant provides this instruction.  */
707   const arm_feature_set * avariant;
708   const arm_feature_set * tvariant;
709
710   /* Function to call to encode instruction in ARM format.  */
711   void (* aencode) (void);
712
713   /* Function to call to encode instruction in Thumb format.  */
714   void (* tencode) (void);
715 };
716
717 /* Defines for various bits that we will want to toggle.  */
718 #define INST_IMMEDIATE  0x02000000
719 #define OFFSET_REG      0x02000000
720 #define HWOFFSET_IMM    0x00400000
721 #define SHIFT_BY_REG    0x00000010
722 #define PRE_INDEX       0x01000000
723 #define INDEX_UP        0x00800000
724 #define WRITE_BACK      0x00200000
725 #define LDM_TYPE_2_OR_3 0x00400000
726 #define CPSI_MMOD       0x00020000
727
728 #define LITERAL_MASK    0xf000f000
729 #define OPCODE_MASK     0xfe1fffff
730 #define V4_STR_BIT      0x00000020
731 #define VLDR_VMOV_SAME  0x0040f000
732
733 #define T2_SUBS_PC_LR   0xf3de8f00
734
735 #define DATA_OP_SHIFT   21
736 #define SBIT_SHIFT      20
737
738 #define T2_OPCODE_MASK  0xfe1fffff
739 #define T2_DATA_OP_SHIFT 21
740 #define T2_SBIT_SHIFT    20
741
742 #define A_COND_MASK         0xf0000000
743 #define A_PUSH_POP_OP_MASK  0x0fff0000
744
745 /* Opcodes for pushing/poping registers to/from the stack.  */
746 #define A1_OPCODE_PUSH    0x092d0000
747 #define A2_OPCODE_PUSH    0x052d0004
748 #define A2_OPCODE_POP     0x049d0004
749
750 /* Codes to distinguish the arithmetic instructions.  */
751 #define OPCODE_AND      0
752 #define OPCODE_EOR      1
753 #define OPCODE_SUB      2
754 #define OPCODE_RSB      3
755 #define OPCODE_ADD      4
756 #define OPCODE_ADC      5
757 #define OPCODE_SBC      6
758 #define OPCODE_RSC      7
759 #define OPCODE_TST      8
760 #define OPCODE_TEQ      9
761 #define OPCODE_CMP      10
762 #define OPCODE_CMN      11
763 #define OPCODE_ORR      12
764 #define OPCODE_MOV      13
765 #define OPCODE_BIC      14
766 #define OPCODE_MVN      15
767
768 #define T2_OPCODE_AND   0
769 #define T2_OPCODE_BIC   1
770 #define T2_OPCODE_ORR   2
771 #define T2_OPCODE_ORN   3
772 #define T2_OPCODE_EOR   4
773 #define T2_OPCODE_ADD   8
774 #define T2_OPCODE_ADC   10
775 #define T2_OPCODE_SBC   11
776 #define T2_OPCODE_SUB   13
777 #define T2_OPCODE_RSB   14
778
779 #define T_OPCODE_MUL 0x4340
780 #define T_OPCODE_TST 0x4200
781 #define T_OPCODE_CMN 0x42c0
782 #define T_OPCODE_NEG 0x4240
783 #define T_OPCODE_MVN 0x43c0
784
785 #define T_OPCODE_ADD_R3 0x1800
786 #define T_OPCODE_SUB_R3 0x1a00
787 #define T_OPCODE_ADD_HI 0x4400
788 #define T_OPCODE_ADD_ST 0xb000
789 #define T_OPCODE_SUB_ST 0xb080
790 #define T_OPCODE_ADD_SP 0xa800
791 #define T_OPCODE_ADD_PC 0xa000
792 #define T_OPCODE_ADD_I8 0x3000
793 #define T_OPCODE_SUB_I8 0x3800
794 #define T_OPCODE_ADD_I3 0x1c00
795 #define T_OPCODE_SUB_I3 0x1e00
796
797 #define T_OPCODE_ASR_R  0x4100
798 #define T_OPCODE_LSL_R  0x4080
799 #define T_OPCODE_LSR_R  0x40c0
800 #define T_OPCODE_ROR_R  0x41c0
801 #define T_OPCODE_ASR_I  0x1000
802 #define T_OPCODE_LSL_I  0x0000
803 #define T_OPCODE_LSR_I  0x0800
804
805 #define T_OPCODE_MOV_I8 0x2000
806 #define T_OPCODE_CMP_I8 0x2800
807 #define T_OPCODE_CMP_LR 0x4280
808 #define T_OPCODE_MOV_HR 0x4600
809 #define T_OPCODE_CMP_HR 0x4500
810
811 #define T_OPCODE_LDR_PC 0x4800
812 #define T_OPCODE_LDR_SP 0x9800
813 #define T_OPCODE_STR_SP 0x9000
814 #define T_OPCODE_LDR_IW 0x6800
815 #define T_OPCODE_STR_IW 0x6000
816 #define T_OPCODE_LDR_IH 0x8800
817 #define T_OPCODE_STR_IH 0x8000
818 #define T_OPCODE_LDR_IB 0x7800
819 #define T_OPCODE_STR_IB 0x7000
820 #define T_OPCODE_LDR_RW 0x5800
821 #define T_OPCODE_STR_RW 0x5000
822 #define T_OPCODE_LDR_RH 0x5a00
823 #define T_OPCODE_STR_RH 0x5200
824 #define T_OPCODE_LDR_RB 0x5c00
825 #define T_OPCODE_STR_RB 0x5400
826
827 #define T_OPCODE_PUSH   0xb400
828 #define T_OPCODE_POP    0xbc00
829
830 #define T_OPCODE_BRANCH 0xe000
831
832 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
833 #define THUMB_PP_PC_LR 0x0100
834 #define THUMB_LOAD_BIT 0x0800
835 #define THUMB2_LOAD_BIT 0x00100000
836
837 #define BAD_ARGS        _("bad arguments to instruction")
838 #define BAD_SP          _("r13 not allowed here")
839 #define BAD_PC          _("r15 not allowed here")
840 #define BAD_COND        _("instruction cannot be conditional")
841 #define BAD_OVERLAP     _("registers may not be the same")
842 #define BAD_HIREG       _("lo register required")
843 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
844 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
845 #define BAD_BRANCH      _("branch must be last instruction in IT block")
846 #define BAD_NOT_IT      _("instruction not allowed in IT block")
847 #define BAD_FPU         _("selected FPU does not support instruction")
848 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
849 #define BAD_IT_COND     _("incorrect condition in IT block")
850 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
851 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
852 #define BAD_PC_ADDRESSING \
853         _("cannot use register index with PC-relative addressing")
854 #define BAD_PC_WRITEBACK \
855         _("cannot use writeback with PC-relative addressing")
856 #define BAD_RANGE       _("branch out of range")
857 #define BAD_FP16        _("selected processor does not support fp16 instruction")
858 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
859 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
860
861 static struct hash_control * arm_ops_hsh;
862 static struct hash_control * arm_cond_hsh;
863 static struct hash_control * arm_shift_hsh;
864 static struct hash_control * arm_psr_hsh;
865 static struct hash_control * arm_v7m_psr_hsh;
866 static struct hash_control * arm_reg_hsh;
867 static struct hash_control * arm_reloc_hsh;
868 static struct hash_control * arm_barrier_opt_hsh;
869
870 /* Stuff needed to resolve the label ambiguity
871    As:
872      ...
873      label:   <insn>
874    may differ from:
875      ...
876      label:
877               <insn>  */
878
879 symbolS *  last_label_seen;
880 static int label_is_thumb_function_name = FALSE;
881
882 /* Literal pool structure.  Held on a per-section
883    and per-sub-section basis.  */
884
885 #define MAX_LITERAL_POOL_SIZE 1024
886 typedef struct literal_pool
887 {
888   expressionS            literals [MAX_LITERAL_POOL_SIZE];
889   unsigned int           next_free_entry;
890   unsigned int           id;
891   symbolS *              symbol;
892   segT                   section;
893   subsegT                sub_section;
894 #ifdef OBJ_ELF
895   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
896 #endif
897   struct literal_pool *  next;
898   unsigned int           alignment;
899 } literal_pool;
900
901 /* Pointer to a linked list of literal pools.  */
902 literal_pool * list_of_pools = NULL;
903
904 typedef enum asmfunc_states
905 {
906   OUTSIDE_ASMFUNC,
907   WAITING_ASMFUNC_NAME,
908   WAITING_ENDASMFUNC
909 } asmfunc_states;
910
911 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
912
913 #ifdef OBJ_ELF
914 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
915 #else
916 static struct current_it now_it;
917 #endif
918
919 static inline int
920 now_it_compatible (int cond)
921 {
922   return (cond & ~1) == (now_it.cc & ~1);
923 }
924
925 static inline int
926 conditional_insn (void)
927 {
928   return inst.cond != COND_ALWAYS;
929 }
930
931 static int in_it_block (void);
932
933 static int handle_it_state (void);
934
935 static void force_automatic_it_block_close (void);
936
937 static void it_fsm_post_encode (void);
938
939 #define set_it_insn_type(type)                  \
940   do                                            \
941     {                                           \
942       inst.it_insn_type = type;                 \
943       if (handle_it_state () == FAIL)           \
944         return;                                 \
945     }                                           \
946   while (0)
947
948 #define set_it_insn_type_nonvoid(type, failret) \
949   do                                            \
950     {                                           \
951       inst.it_insn_type = type;                 \
952       if (handle_it_state () == FAIL)           \
953         return failret;                         \
954     }                                           \
955   while(0)
956
957 #define set_it_insn_type_last()                         \
958   do                                                    \
959     {                                                   \
960       if (inst.cond == COND_ALWAYS)                     \
961         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
962       else                                              \
963         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
964     }                                                   \
965   while (0)
966
967 /* Pure syntax.  */
968
969 /* This array holds the chars that always start a comment.  If the
970    pre-processor is disabled, these aren't very useful.  */
971 char arm_comment_chars[] = "@";
972
973 /* This array holds the chars that only start a comment at the beginning of
974    a line.  If the line seems to have the form '# 123 filename'
975    .line and .file directives will appear in the pre-processed output.  */
976 /* Note that input_file.c hand checks for '#' at the beginning of the
977    first line of the input file.  This is because the compiler outputs
978    #NO_APP at the beginning of its output.  */
979 /* Also note that comments like this one will always work.  */
980 const char line_comment_chars[] = "#";
981
982 char arm_line_separator_chars[] = ";";
983
984 /* Chars that can be used to separate mant
985    from exp in floating point numbers.  */
986 const char EXP_CHARS[] = "eE";
987
988 /* Chars that mean this number is a floating point constant.  */
989 /* As in 0f12.456  */
990 /* or    0d1.2345e12  */
991
992 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
993
994 /* Prefix characters that indicate the start of an immediate
995    value.  */
996 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
997
998 /* Separator character handling.  */
999
1000 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
1001
1002 static inline int
1003 skip_past_char (char ** str, char c)
1004 {
1005   /* PR gas/14987: Allow for whitespace before the expected character.  */
1006   skip_whitespace (*str);
1007
1008   if (**str == c)
1009     {
1010       (*str)++;
1011       return SUCCESS;
1012     }
1013   else
1014     return FAIL;
1015 }
1016
1017 #define skip_past_comma(str) skip_past_char (str, ',')
1018
1019 /* Arithmetic expressions (possibly involving symbols).  */
1020
1021 /* Return TRUE if anything in the expression is a bignum.  */
1022
1023 static bfd_boolean
1024 walk_no_bignums (symbolS * sp)
1025 {
1026   if (symbol_get_value_expression (sp)->X_op == O_big)
1027     return TRUE;
1028
1029   if (symbol_get_value_expression (sp)->X_add_symbol)
1030     {
1031       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
1032               || (symbol_get_value_expression (sp)->X_op_symbol
1033                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
1034     }
1035
1036   return FALSE;
1037 }
1038
1039 static bfd_boolean in_my_get_expression = FALSE;
1040
1041 /* Third argument to my_get_expression.  */
1042 #define GE_NO_PREFIX 0
1043 #define GE_IMM_PREFIX 1
1044 #define GE_OPT_PREFIX 2
1045 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1046    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1047 #define GE_OPT_PREFIX_BIG 3
1048
1049 static int
1050 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1051 {
1052   char * save_in;
1053
1054   /* In unified syntax, all prefixes are optional.  */
1055   if (unified_syntax)
1056     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1057                   : GE_OPT_PREFIX;
1058
1059   switch (prefix_mode)
1060     {
1061     case GE_NO_PREFIX: break;
1062     case GE_IMM_PREFIX:
1063       if (!is_immediate_prefix (**str))
1064         {
1065           inst.error = _("immediate expression requires a # prefix");
1066           return FAIL;
1067         }
1068       (*str)++;
1069       break;
1070     case GE_OPT_PREFIX:
1071     case GE_OPT_PREFIX_BIG:
1072       if (is_immediate_prefix (**str))
1073         (*str)++;
1074       break;
1075     default:
1076       abort ();
1077     }
1078
1079   memset (ep, 0, sizeof (expressionS));
1080
1081   save_in = input_line_pointer;
1082   input_line_pointer = *str;
1083   in_my_get_expression = TRUE;
1084   expression (ep);
1085   in_my_get_expression = FALSE;
1086
1087   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1088     {
1089       /* We found a bad or missing expression in md_operand().  */
1090       *str = input_line_pointer;
1091       input_line_pointer = save_in;
1092       if (inst.error == NULL)
1093         inst.error = (ep->X_op == O_absent
1094                       ? _("missing expression") :_("bad expression"));
1095       return 1;
1096     }
1097
1098   /* Get rid of any bignums now, so that we don't generate an error for which
1099      we can't establish a line number later on.  Big numbers are never valid
1100      in instructions, which is where this routine is always called.  */
1101   if (prefix_mode != GE_OPT_PREFIX_BIG
1102       && (ep->X_op == O_big
1103           || (ep->X_add_symbol
1104               && (walk_no_bignums (ep->X_add_symbol)
1105                   || (ep->X_op_symbol
1106                       && walk_no_bignums (ep->X_op_symbol))))))
1107     {
1108       inst.error = _("invalid constant");
1109       *str = input_line_pointer;
1110       input_line_pointer = save_in;
1111       return 1;
1112     }
1113
1114   *str = input_line_pointer;
1115   input_line_pointer = save_in;
1116   return SUCCESS;
1117 }
1118
1119 /* Turn a string in input_line_pointer into a floating point constant
1120    of type TYPE, and store the appropriate bytes in *LITP.  The number
1121    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1122    returned, or NULL on OK.
1123
1124    Note that fp constants aren't represent in the normal way on the ARM.
1125    In big endian mode, things are as expected.  However, in little endian
1126    mode fp constants are big-endian word-wise, and little-endian byte-wise
1127    within the words.  For example, (double) 1.1 in big endian mode is
1128    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1129    the byte sequence 99 99 f1 3f 9a 99 99 99.
1130
1131    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1132
1133 const char *
1134 md_atof (int type, char * litP, int * sizeP)
1135 {
1136   int prec;
1137   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1138   char *t;
1139   int i;
1140
1141   switch (type)
1142     {
1143     case 'f':
1144     case 'F':
1145     case 's':
1146     case 'S':
1147       prec = 2;
1148       break;
1149
1150     case 'd':
1151     case 'D':
1152     case 'r':
1153     case 'R':
1154       prec = 4;
1155       break;
1156
1157     case 'x':
1158     case 'X':
1159       prec = 5;
1160       break;
1161
1162     case 'p':
1163     case 'P':
1164       prec = 5;
1165       break;
1166
1167     default:
1168       *sizeP = 0;
1169       return _("Unrecognized or unsupported floating point constant");
1170     }
1171
1172   t = atof_ieee (input_line_pointer, type, words);
1173   if (t)
1174     input_line_pointer = t;
1175   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1176
1177   if (target_big_endian)
1178     {
1179       for (i = 0; i < prec; i++)
1180         {
1181           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1182           litP += sizeof (LITTLENUM_TYPE);
1183         }
1184     }
1185   else
1186     {
1187       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1188         for (i = prec - 1; i >= 0; i--)
1189           {
1190             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1191             litP += sizeof (LITTLENUM_TYPE);
1192           }
1193       else
1194         /* For a 4 byte float the order of elements in `words' is 1 0.
1195            For an 8 byte float the order is 1 0 3 2.  */
1196         for (i = 0; i < prec; i += 2)
1197           {
1198             md_number_to_chars (litP, (valueT) words[i + 1],
1199                                 sizeof (LITTLENUM_TYPE));
1200             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1201                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1202             litP += 2 * sizeof (LITTLENUM_TYPE);
1203           }
1204     }
1205
1206   return NULL;
1207 }
1208
1209 /* We handle all bad expressions here, so that we can report the faulty
1210    instruction in the error message.  */
1211
1212 void
1213 md_operand (expressionS * exp)
1214 {
1215   if (in_my_get_expression)
1216     exp->X_op = O_illegal;
1217 }
1218
1219 /* Immediate values.  */
1220
1221 #ifdef OBJ_ELF
1222 /* Generic immediate-value read function for use in directives.
1223    Accepts anything that 'expression' can fold to a constant.
1224    *val receives the number.  */
1225
1226 static int
1227 immediate_for_directive (int *val)
1228 {
1229   expressionS exp;
1230   exp.X_op = O_illegal;
1231
1232   if (is_immediate_prefix (*input_line_pointer))
1233     {
1234       input_line_pointer++;
1235       expression (&exp);
1236     }
1237
1238   if (exp.X_op != O_constant)
1239     {
1240       as_bad (_("expected #constant"));
1241       ignore_rest_of_line ();
1242       return FAIL;
1243     }
1244   *val = exp.X_add_number;
1245   return SUCCESS;
1246 }
1247 #endif
1248
1249 /* Register parsing.  */
1250
1251 /* Generic register parser.  CCP points to what should be the
1252    beginning of a register name.  If it is indeed a valid register
1253    name, advance CCP over it and return the reg_entry structure;
1254    otherwise return NULL.  Does not issue diagnostics.  */
1255
1256 static struct reg_entry *
1257 arm_reg_parse_multi (char **ccp)
1258 {
1259   char *start = *ccp;
1260   char *p;
1261   struct reg_entry *reg;
1262
1263   skip_whitespace (start);
1264
1265 #ifdef REGISTER_PREFIX
1266   if (*start != REGISTER_PREFIX)
1267     return NULL;
1268   start++;
1269 #endif
1270 #ifdef OPTIONAL_REGISTER_PREFIX
1271   if (*start == OPTIONAL_REGISTER_PREFIX)
1272     start++;
1273 #endif
1274
1275   p = start;
1276   if (!ISALPHA (*p) || !is_name_beginner (*p))
1277     return NULL;
1278
1279   do
1280     p++;
1281   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1282
1283   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1284
1285   if (!reg)
1286     return NULL;
1287
1288   *ccp = p;
1289   return reg;
1290 }
1291
1292 static int
1293 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1294                     enum arm_reg_type type)
1295 {
1296   /* Alternative syntaxes are accepted for a few register classes.  */
1297   switch (type)
1298     {
1299     case REG_TYPE_MVF:
1300     case REG_TYPE_MVD:
1301     case REG_TYPE_MVFX:
1302     case REG_TYPE_MVDX:
1303       /* Generic coprocessor register names are allowed for these.  */
1304       if (reg && reg->type == REG_TYPE_CN)
1305         return reg->number;
1306       break;
1307
1308     case REG_TYPE_CP:
1309       /* For backward compatibility, a bare number is valid here.  */
1310       {
1311         unsigned long processor = strtoul (start, ccp, 10);
1312         if (*ccp != start && processor <= 15)
1313           return processor;
1314       }
1315       /* Fall through.  */
1316
1317     case REG_TYPE_MMXWC:
1318       /* WC includes WCG.  ??? I'm not sure this is true for all
1319          instructions that take WC registers.  */
1320       if (reg && reg->type == REG_TYPE_MMXWCG)
1321         return reg->number;
1322       break;
1323
1324     default:
1325       break;
1326     }
1327
1328   return FAIL;
1329 }
1330
1331 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1332    return value is the register number or FAIL.  */
1333
1334 static int
1335 arm_reg_parse (char **ccp, enum arm_reg_type type)
1336 {
1337   char *start = *ccp;
1338   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1339   int ret;
1340
1341   /* Do not allow a scalar (reg+index) to parse as a register.  */
1342   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1343     return FAIL;
1344
1345   if (reg && reg->type == type)
1346     return reg->number;
1347
1348   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1349     return ret;
1350
1351   *ccp = start;
1352   return FAIL;
1353 }
1354
1355 /* Parse a Neon type specifier. *STR should point at the leading '.'
1356    character. Does no verification at this stage that the type fits the opcode
1357    properly. E.g.,
1358
1359      .i32.i32.s16
1360      .s32.f32
1361      .u16
1362
1363    Can all be legally parsed by this function.
1364
1365    Fills in neon_type struct pointer with parsed information, and updates STR
1366    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1367    type, FAIL if not.  */
1368
1369 static int
1370 parse_neon_type (struct neon_type *type, char **str)
1371 {
1372   char *ptr = *str;
1373
1374   if (type)
1375     type->elems = 0;
1376
1377   while (type->elems < NEON_MAX_TYPE_ELS)
1378     {
1379       enum neon_el_type thistype = NT_untyped;
1380       unsigned thissize = -1u;
1381
1382       if (*ptr != '.')
1383         break;
1384
1385       ptr++;
1386
1387       /* Just a size without an explicit type.  */
1388       if (ISDIGIT (*ptr))
1389         goto parsesize;
1390
1391       switch (TOLOWER (*ptr))
1392         {
1393         case 'i': thistype = NT_integer; break;
1394         case 'f': thistype = NT_float; break;
1395         case 'p': thistype = NT_poly; break;
1396         case 's': thistype = NT_signed; break;
1397         case 'u': thistype = NT_unsigned; break;
1398         case 'd':
1399           thistype = NT_float;
1400           thissize = 64;
1401           ptr++;
1402           goto done;
1403         default:
1404           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1405           return FAIL;
1406         }
1407
1408       ptr++;
1409
1410       /* .f is an abbreviation for .f32.  */
1411       if (thistype == NT_float && !ISDIGIT (*ptr))
1412         thissize = 32;
1413       else
1414         {
1415         parsesize:
1416           thissize = strtoul (ptr, &ptr, 10);
1417
1418           if (thissize != 8 && thissize != 16 && thissize != 32
1419               && thissize != 64)
1420             {
1421               as_bad (_("bad size %d in type specifier"), thissize);
1422               return FAIL;
1423             }
1424         }
1425
1426       done:
1427       if (type)
1428         {
1429           type->el[type->elems].type = thistype;
1430           type->el[type->elems].size = thissize;
1431           type->elems++;
1432         }
1433     }
1434
1435   /* Empty/missing type is not a successful parse.  */
1436   if (type->elems == 0)
1437     return FAIL;
1438
1439   *str = ptr;
1440
1441   return SUCCESS;
1442 }
1443
1444 /* Errors may be set multiple times during parsing or bit encoding
1445    (particularly in the Neon bits), but usually the earliest error which is set
1446    will be the most meaningful. Avoid overwriting it with later (cascading)
1447    errors by calling this function.  */
1448
1449 static void
1450 first_error (const char *err)
1451 {
1452   if (!inst.error)
1453     inst.error = err;
1454 }
1455
1456 /* Parse a single type, e.g. ".s32", leading period included.  */
1457 static int
1458 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1459 {
1460   char *str = *ccp;
1461   struct neon_type optype;
1462
1463   if (*str == '.')
1464     {
1465       if (parse_neon_type (&optype, &str) == SUCCESS)
1466         {
1467           if (optype.elems == 1)
1468             *vectype = optype.el[0];
1469           else
1470             {
1471               first_error (_("only one type should be specified for operand"));
1472               return FAIL;
1473             }
1474         }
1475       else
1476         {
1477           first_error (_("vector type expected"));
1478           return FAIL;
1479         }
1480     }
1481   else
1482     return FAIL;
1483
1484   *ccp = str;
1485
1486   return SUCCESS;
1487 }
1488
1489 /* Special meanings for indices (which have a range of 0-7), which will fit into
1490    a 4-bit integer.  */
1491
1492 #define NEON_ALL_LANES          15
1493 #define NEON_INTERLEAVE_LANES   14
1494
1495 /* Parse either a register or a scalar, with an optional type. Return the
1496    register number, and optionally fill in the actual type of the register
1497    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1498    type/index information in *TYPEINFO.  */
1499
1500 static int
1501 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1502                            enum arm_reg_type *rtype,
1503                            struct neon_typed_alias *typeinfo)
1504 {
1505   char *str = *ccp;
1506   struct reg_entry *reg = arm_reg_parse_multi (&str);
1507   struct neon_typed_alias atype;
1508   struct neon_type_el parsetype;
1509
1510   atype.defined = 0;
1511   atype.index = -1;
1512   atype.eltype.type = NT_invtype;
1513   atype.eltype.size = -1;
1514
1515   /* Try alternate syntax for some types of register. Note these are mutually
1516      exclusive with the Neon syntax extensions.  */
1517   if (reg == NULL)
1518     {
1519       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1520       if (altreg != FAIL)
1521         *ccp = str;
1522       if (typeinfo)
1523         *typeinfo = atype;
1524       return altreg;
1525     }
1526
1527   /* Undo polymorphism when a set of register types may be accepted.  */
1528   if ((type == REG_TYPE_NDQ
1529        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1530       || (type == REG_TYPE_VFSD
1531           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1532       || (type == REG_TYPE_NSDQ
1533           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1534               || reg->type == REG_TYPE_NQ))
1535       || (type == REG_TYPE_NSD
1536           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1537       || (type == REG_TYPE_MMXWC
1538           && (reg->type == REG_TYPE_MMXWCG)))
1539     type = (enum arm_reg_type) reg->type;
1540
1541   if (type != reg->type)
1542     return FAIL;
1543
1544   if (reg->neon)
1545     atype = *reg->neon;
1546
1547   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1548     {
1549       if ((atype.defined & NTA_HASTYPE) != 0)
1550         {
1551           first_error (_("can't redefine type for operand"));
1552           return FAIL;
1553         }
1554       atype.defined |= NTA_HASTYPE;
1555       atype.eltype = parsetype;
1556     }
1557
1558   if (skip_past_char (&str, '[') == SUCCESS)
1559     {
1560       if (type != REG_TYPE_VFD
1561           && !(type == REG_TYPE_VFS
1562                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2)))
1563         {
1564           first_error (_("only D registers may be indexed"));
1565           return FAIL;
1566         }
1567
1568       if ((atype.defined & NTA_HASINDEX) != 0)
1569         {
1570           first_error (_("can't change index for operand"));
1571           return FAIL;
1572         }
1573
1574       atype.defined |= NTA_HASINDEX;
1575
1576       if (skip_past_char (&str, ']') == SUCCESS)
1577         atype.index = NEON_ALL_LANES;
1578       else
1579         {
1580           expressionS exp;
1581
1582           my_get_expression (&exp, &str, GE_NO_PREFIX);
1583
1584           if (exp.X_op != O_constant)
1585             {
1586               first_error (_("constant expression required"));
1587               return FAIL;
1588             }
1589
1590           if (skip_past_char (&str, ']') == FAIL)
1591             return FAIL;
1592
1593           atype.index = exp.X_add_number;
1594         }
1595     }
1596
1597   if (typeinfo)
1598     *typeinfo = atype;
1599
1600   if (rtype)
1601     *rtype = type;
1602
1603   *ccp = str;
1604
1605   return reg->number;
1606 }
1607
1608 /* Like arm_reg_parse, but allow allow the following extra features:
1609     - If RTYPE is non-zero, return the (possibly restricted) type of the
1610       register (e.g. Neon double or quad reg when either has been requested).
1611     - If this is a Neon vector type with additional type information, fill
1612       in the struct pointed to by VECTYPE (if non-NULL).
1613    This function will fault on encountering a scalar.  */
1614
1615 static int
1616 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1617                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1618 {
1619   struct neon_typed_alias atype;
1620   char *str = *ccp;
1621   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1622
1623   if (reg == FAIL)
1624     return FAIL;
1625
1626   /* Do not allow regname(... to parse as a register.  */
1627   if (*str == '(')
1628     return FAIL;
1629
1630   /* Do not allow a scalar (reg+index) to parse as a register.  */
1631   if ((atype.defined & NTA_HASINDEX) != 0)
1632     {
1633       first_error (_("register operand expected, but got scalar"));
1634       return FAIL;
1635     }
1636
1637   if (vectype)
1638     *vectype = atype.eltype;
1639
1640   *ccp = str;
1641
1642   return reg;
1643 }
1644
1645 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1646 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1647
1648 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1649    have enough information to be able to do a good job bounds-checking. So, we
1650    just do easy checks here, and do further checks later.  */
1651
1652 static int
1653 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1654 {
1655   int reg;
1656   char *str = *ccp;
1657   struct neon_typed_alias atype;
1658   enum arm_reg_type reg_type = REG_TYPE_VFD;
1659
1660   if (elsize == 4)
1661     reg_type = REG_TYPE_VFS;
1662
1663   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1664
1665   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1666     return FAIL;
1667
1668   if (atype.index == NEON_ALL_LANES)
1669     {
1670       first_error (_("scalar must have an index"));
1671       return FAIL;
1672     }
1673   else if (atype.index >= 64 / elsize)
1674     {
1675       first_error (_("scalar index out of range"));
1676       return FAIL;
1677     }
1678
1679   if (type)
1680     *type = atype.eltype;
1681
1682   *ccp = str;
1683
1684   return reg * 16 + atype.index;
1685 }
1686
1687 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1688
1689 static long
1690 parse_reg_list (char ** strp)
1691 {
1692   char * str = * strp;
1693   long   range = 0;
1694   int    another_range;
1695
1696   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1697   do
1698     {
1699       skip_whitespace (str);
1700
1701       another_range = 0;
1702
1703       if (*str == '{')
1704         {
1705           int in_range = 0;
1706           int cur_reg = -1;
1707
1708           str++;
1709           do
1710             {
1711               int reg;
1712
1713               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1714                 {
1715                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1716                   return FAIL;
1717                 }
1718
1719               if (in_range)
1720                 {
1721                   int i;
1722
1723                   if (reg <= cur_reg)
1724                     {
1725                       first_error (_("bad range in register list"));
1726                       return FAIL;
1727                     }
1728
1729                   for (i = cur_reg + 1; i < reg; i++)
1730                     {
1731                       if (range & (1 << i))
1732                         as_tsktsk
1733                           (_("Warning: duplicated register (r%d) in register list"),
1734                            i);
1735                       else
1736                         range |= 1 << i;
1737                     }
1738                   in_range = 0;
1739                 }
1740
1741               if (range & (1 << reg))
1742                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1743                            reg);
1744               else if (reg <= cur_reg)
1745                 as_tsktsk (_("Warning: register range not in ascending order"));
1746
1747               range |= 1 << reg;
1748               cur_reg = reg;
1749             }
1750           while (skip_past_comma (&str) != FAIL
1751                  || (in_range = 1, *str++ == '-'));
1752           str--;
1753
1754           if (skip_past_char (&str, '}') == FAIL)
1755             {
1756               first_error (_("missing `}'"));
1757               return FAIL;
1758             }
1759         }
1760       else
1761         {
1762           expressionS exp;
1763
1764           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1765             return FAIL;
1766
1767           if (exp.X_op == O_constant)
1768             {
1769               if (exp.X_add_number
1770                   != (exp.X_add_number & 0x0000ffff))
1771                 {
1772                   inst.error = _("invalid register mask");
1773                   return FAIL;
1774                 }
1775
1776               if ((range & exp.X_add_number) != 0)
1777                 {
1778                   int regno = range & exp.X_add_number;
1779
1780                   regno &= -regno;
1781                   regno = (1 << regno) - 1;
1782                   as_tsktsk
1783                     (_("Warning: duplicated register (r%d) in register list"),
1784                      regno);
1785                 }
1786
1787               range |= exp.X_add_number;
1788             }
1789           else
1790             {
1791               if (inst.reloc.type != 0)
1792                 {
1793                   inst.error = _("expression too complex");
1794                   return FAIL;
1795                 }
1796
1797               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1798               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1799               inst.reloc.pc_rel = 0;
1800             }
1801         }
1802
1803       if (*str == '|' || *str == '+')
1804         {
1805           str++;
1806           another_range = 1;
1807         }
1808     }
1809   while (another_range);
1810
1811   *strp = str;
1812   return range;
1813 }
1814
1815 /* Types of registers in a list.  */
1816
1817 enum reg_list_els
1818 {
1819   REGLIST_VFP_S,
1820   REGLIST_VFP_D,
1821   REGLIST_NEON_D
1822 };
1823
1824 /* Parse a VFP register list.  If the string is invalid return FAIL.
1825    Otherwise return the number of registers, and set PBASE to the first
1826    register.  Parses registers of type ETYPE.
1827    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1828      - Q registers can be used to specify pairs of D registers
1829      - { } can be omitted from around a singleton register list
1830          FIXME: This is not implemented, as it would require backtracking in
1831          some cases, e.g.:
1832            vtbl.8 d3,d4,d5
1833          This could be done (the meaning isn't really ambiguous), but doesn't
1834          fit in well with the current parsing framework.
1835      - 32 D registers may be used (also true for VFPv3).
1836    FIXME: Types are ignored in these register lists, which is probably a
1837    bug.  */
1838
1839 static int
1840 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1841 {
1842   char *str = *ccp;
1843   int base_reg;
1844   int new_base;
1845   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1846   int max_regs = 0;
1847   int count = 0;
1848   int warned = 0;
1849   unsigned long mask = 0;
1850   int i;
1851
1852   if (skip_past_char (&str, '{') == FAIL)
1853     {
1854       inst.error = _("expecting {");
1855       return FAIL;
1856     }
1857
1858   switch (etype)
1859     {
1860     case REGLIST_VFP_S:
1861       regtype = REG_TYPE_VFS;
1862       max_regs = 32;
1863       break;
1864
1865     case REGLIST_VFP_D:
1866       regtype = REG_TYPE_VFD;
1867       break;
1868
1869     case REGLIST_NEON_D:
1870       regtype = REG_TYPE_NDQ;
1871       break;
1872     }
1873
1874   if (etype != REGLIST_VFP_S)
1875     {
1876       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1877       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1878         {
1879           max_regs = 32;
1880           if (thumb_mode)
1881             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1882                                     fpu_vfp_ext_d32);
1883           else
1884             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1885                                     fpu_vfp_ext_d32);
1886         }
1887       else
1888         max_regs = 16;
1889     }
1890
1891   base_reg = max_regs;
1892
1893   do
1894     {
1895       int setmask = 1, addregs = 1;
1896
1897       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1898
1899       if (new_base == FAIL)
1900         {
1901           first_error (_(reg_expected_msgs[regtype]));
1902           return FAIL;
1903         }
1904
1905       if (new_base >= max_regs)
1906         {
1907           first_error (_("register out of range in list"));
1908           return FAIL;
1909         }
1910
1911       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1912       if (regtype == REG_TYPE_NQ)
1913         {
1914           setmask = 3;
1915           addregs = 2;
1916         }
1917
1918       if (new_base < base_reg)
1919         base_reg = new_base;
1920
1921       if (mask & (setmask << new_base))
1922         {
1923           first_error (_("invalid register list"));
1924           return FAIL;
1925         }
1926
1927       if ((mask >> new_base) != 0 && ! warned)
1928         {
1929           as_tsktsk (_("register list not in ascending order"));
1930           warned = 1;
1931         }
1932
1933       mask |= setmask << new_base;
1934       count += addregs;
1935
1936       if (*str == '-') /* We have the start of a range expression */
1937         {
1938           int high_range;
1939
1940           str++;
1941
1942           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1943               == FAIL)
1944             {
1945               inst.error = gettext (reg_expected_msgs[regtype]);
1946               return FAIL;
1947             }
1948
1949           if (high_range >= max_regs)
1950             {
1951               first_error (_("register out of range in list"));
1952               return FAIL;
1953             }
1954
1955           if (regtype == REG_TYPE_NQ)
1956             high_range = high_range + 1;
1957
1958           if (high_range <= new_base)
1959             {
1960               inst.error = _("register range not in ascending order");
1961               return FAIL;
1962             }
1963
1964           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1965             {
1966               if (mask & (setmask << new_base))
1967                 {
1968                   inst.error = _("invalid register list");
1969                   return FAIL;
1970                 }
1971
1972               mask |= setmask << new_base;
1973               count += addregs;
1974             }
1975         }
1976     }
1977   while (skip_past_comma (&str) != FAIL);
1978
1979   str++;
1980
1981   /* Sanity check -- should have raised a parse error above.  */
1982   if (count == 0 || count > max_regs)
1983     abort ();
1984
1985   *pbase = base_reg;
1986
1987   /* Final test -- the registers must be consecutive.  */
1988   mask >>= base_reg;
1989   for (i = 0; i < count; i++)
1990     {
1991       if ((mask & (1u << i)) == 0)
1992         {
1993           inst.error = _("non-contiguous register range");
1994           return FAIL;
1995         }
1996     }
1997
1998   *ccp = str;
1999
2000   return count;
2001 }
2002
2003 /* True if two alias types are the same.  */
2004
2005 static bfd_boolean
2006 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
2007 {
2008   if (!a && !b)
2009     return TRUE;
2010
2011   if (!a || !b)
2012     return FALSE;
2013
2014   if (a->defined != b->defined)
2015     return FALSE;
2016
2017   if ((a->defined & NTA_HASTYPE) != 0
2018       && (a->eltype.type != b->eltype.type
2019           || a->eltype.size != b->eltype.size))
2020     return FALSE;
2021
2022   if ((a->defined & NTA_HASINDEX) != 0
2023       && (a->index != b->index))
2024     return FALSE;
2025
2026   return TRUE;
2027 }
2028
2029 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2030    The base register is put in *PBASE.
2031    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2032    the return value.
2033    The register stride (minus one) is put in bit 4 of the return value.
2034    Bits [6:5] encode the list length (minus one).
2035    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2036
2037 #define NEON_LANE(X)            ((X) & 0xf)
2038 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2039 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2040
2041 static int
2042 parse_neon_el_struct_list (char **str, unsigned *pbase,
2043                            struct neon_type_el *eltype)
2044 {
2045   char *ptr = *str;
2046   int base_reg = -1;
2047   int reg_incr = -1;
2048   int count = 0;
2049   int lane = -1;
2050   int leading_brace = 0;
2051   enum arm_reg_type rtype = REG_TYPE_NDQ;
2052   const char *const incr_error = _("register stride must be 1 or 2");
2053   const char *const type_error = _("mismatched element/structure types in list");
2054   struct neon_typed_alias firsttype;
2055   firsttype.defined = 0;
2056   firsttype.eltype.type = NT_invtype;
2057   firsttype.eltype.size = -1;
2058   firsttype.index = -1;
2059
2060   if (skip_past_char (&ptr, '{') == SUCCESS)
2061     leading_brace = 1;
2062
2063   do
2064     {
2065       struct neon_typed_alias atype;
2066       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2067
2068       if (getreg == FAIL)
2069         {
2070           first_error (_(reg_expected_msgs[rtype]));
2071           return FAIL;
2072         }
2073
2074       if (base_reg == -1)
2075         {
2076           base_reg = getreg;
2077           if (rtype == REG_TYPE_NQ)
2078             {
2079               reg_incr = 1;
2080             }
2081           firsttype = atype;
2082         }
2083       else if (reg_incr == -1)
2084         {
2085           reg_incr = getreg - base_reg;
2086           if (reg_incr < 1 || reg_incr > 2)
2087             {
2088               first_error (_(incr_error));
2089               return FAIL;
2090             }
2091         }
2092       else if (getreg != base_reg + reg_incr * count)
2093         {
2094           first_error (_(incr_error));
2095           return FAIL;
2096         }
2097
2098       if (! neon_alias_types_same (&atype, &firsttype))
2099         {
2100           first_error (_(type_error));
2101           return FAIL;
2102         }
2103
2104       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2105          modes.  */
2106       if (ptr[0] == '-')
2107         {
2108           struct neon_typed_alias htype;
2109           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2110           if (lane == -1)
2111             lane = NEON_INTERLEAVE_LANES;
2112           else if (lane != NEON_INTERLEAVE_LANES)
2113             {
2114               first_error (_(type_error));
2115               return FAIL;
2116             }
2117           if (reg_incr == -1)
2118             reg_incr = 1;
2119           else if (reg_incr != 1)
2120             {
2121               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2122               return FAIL;
2123             }
2124           ptr++;
2125           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2126           if (hireg == FAIL)
2127             {
2128               first_error (_(reg_expected_msgs[rtype]));
2129               return FAIL;
2130             }
2131           if (! neon_alias_types_same (&htype, &firsttype))
2132             {
2133               first_error (_(type_error));
2134               return FAIL;
2135             }
2136           count += hireg + dregs - getreg;
2137           continue;
2138         }
2139
2140       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2141       if (rtype == REG_TYPE_NQ)
2142         {
2143           count += 2;
2144           continue;
2145         }
2146
2147       if ((atype.defined & NTA_HASINDEX) != 0)
2148         {
2149           if (lane == -1)
2150             lane = atype.index;
2151           else if (lane != atype.index)
2152             {
2153               first_error (_(type_error));
2154               return FAIL;
2155             }
2156         }
2157       else if (lane == -1)
2158         lane = NEON_INTERLEAVE_LANES;
2159       else if (lane != NEON_INTERLEAVE_LANES)
2160         {
2161           first_error (_(type_error));
2162           return FAIL;
2163         }
2164       count++;
2165     }
2166   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2167
2168   /* No lane set by [x]. We must be interleaving structures.  */
2169   if (lane == -1)
2170     lane = NEON_INTERLEAVE_LANES;
2171
2172   /* Sanity check.  */
2173   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2174       || (count > 1 && reg_incr == -1))
2175     {
2176       first_error (_("error parsing element/structure list"));
2177       return FAIL;
2178     }
2179
2180   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2181     {
2182       first_error (_("expected }"));
2183       return FAIL;
2184     }
2185
2186   if (reg_incr == -1)
2187     reg_incr = 1;
2188
2189   if (eltype)
2190     *eltype = firsttype.eltype;
2191
2192   *pbase = base_reg;
2193   *str = ptr;
2194
2195   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2196 }
2197
2198 /* Parse an explicit relocation suffix on an expression.  This is
2199    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2200    arm_reloc_hsh contains no entries, so this function can only
2201    succeed if there is no () after the word.  Returns -1 on error,
2202    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2203
2204 static int
2205 parse_reloc (char **str)
2206 {
2207   struct reloc_entry *r;
2208   char *p, *q;
2209
2210   if (**str != '(')
2211     return BFD_RELOC_UNUSED;
2212
2213   p = *str + 1;
2214   q = p;
2215
2216   while (*q && *q != ')' && *q != ',')
2217     q++;
2218   if (*q != ')')
2219     return -1;
2220
2221   if ((r = (struct reloc_entry *)
2222        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2223     return -1;
2224
2225   *str = q + 1;
2226   return r->reloc;
2227 }
2228
2229 /* Directives: register aliases.  */
2230
2231 static struct reg_entry *
2232 insert_reg_alias (char *str, unsigned number, int type)
2233 {
2234   struct reg_entry *new_reg;
2235   const char *name;
2236
2237   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2238     {
2239       if (new_reg->builtin)
2240         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2241
2242       /* Only warn about a redefinition if it's not defined as the
2243          same register.  */
2244       else if (new_reg->number != number || new_reg->type != type)
2245         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2246
2247       return NULL;
2248     }
2249
2250   name = xstrdup (str);
2251   new_reg = XNEW (struct reg_entry);
2252
2253   new_reg->name = name;
2254   new_reg->number = number;
2255   new_reg->type = type;
2256   new_reg->builtin = FALSE;
2257   new_reg->neon = NULL;
2258
2259   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2260     abort ();
2261
2262   return new_reg;
2263 }
2264
2265 static void
2266 insert_neon_reg_alias (char *str, int number, int type,
2267                        struct neon_typed_alias *atype)
2268 {
2269   struct reg_entry *reg = insert_reg_alias (str, number, type);
2270
2271   if (!reg)
2272     {
2273       first_error (_("attempt to redefine typed alias"));
2274       return;
2275     }
2276
2277   if (atype)
2278     {
2279       reg->neon = XNEW (struct neon_typed_alias);
2280       *reg->neon = *atype;
2281     }
2282 }
2283
2284 /* Look for the .req directive.  This is of the form:
2285
2286         new_register_name .req existing_register_name
2287
2288    If we find one, or if it looks sufficiently like one that we want to
2289    handle any error here, return TRUE.  Otherwise return FALSE.  */
2290
2291 static bfd_boolean
2292 create_register_alias (char * newname, char *p)
2293 {
2294   struct reg_entry *old;
2295   char *oldname, *nbuf;
2296   size_t nlen;
2297
2298   /* The input scrubber ensures that whitespace after the mnemonic is
2299      collapsed to single spaces.  */
2300   oldname = p;
2301   if (strncmp (oldname, " .req ", 6) != 0)
2302     return FALSE;
2303
2304   oldname += 6;
2305   if (*oldname == '\0')
2306     return FALSE;
2307
2308   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2309   if (!old)
2310     {
2311       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2312       return TRUE;
2313     }
2314
2315   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2316      the desired alias name, and p points to its end.  If not, then
2317      the desired alias name is in the global original_case_string.  */
2318 #ifdef TC_CASE_SENSITIVE
2319   nlen = p - newname;
2320 #else
2321   newname = original_case_string;
2322   nlen = strlen (newname);
2323 #endif
2324
2325   nbuf = xmemdup0 (newname, nlen);
2326
2327   /* Create aliases under the new name as stated; an all-lowercase
2328      version of the new name; and an all-uppercase version of the new
2329      name.  */
2330   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2331     {
2332       for (p = nbuf; *p; p++)
2333         *p = TOUPPER (*p);
2334
2335       if (strncmp (nbuf, newname, nlen))
2336         {
2337           /* If this attempt to create an additional alias fails, do not bother
2338              trying to create the all-lower case alias.  We will fail and issue
2339              a second, duplicate error message.  This situation arises when the
2340              programmer does something like:
2341                foo .req r0
2342                Foo .req r1
2343              The second .req creates the "Foo" alias but then fails to create
2344              the artificial FOO alias because it has already been created by the
2345              first .req.  */
2346           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2347             {
2348               free (nbuf);
2349               return TRUE;
2350             }
2351         }
2352
2353       for (p = nbuf; *p; p++)
2354         *p = TOLOWER (*p);
2355
2356       if (strncmp (nbuf, newname, nlen))
2357         insert_reg_alias (nbuf, old->number, old->type);
2358     }
2359
2360   free (nbuf);
2361   return TRUE;
2362 }
2363
2364 /* Create a Neon typed/indexed register alias using directives, e.g.:
2365      X .dn d5.s32[1]
2366      Y .qn 6.s16
2367      Z .dn d7
2368      T .dn Z[0]
2369    These typed registers can be used instead of the types specified after the
2370    Neon mnemonic, so long as all operands given have types. Types can also be
2371    specified directly, e.g.:
2372      vadd d0.s32, d1.s32, d2.s32  */
2373
2374 static bfd_boolean
2375 create_neon_reg_alias (char *newname, char *p)
2376 {
2377   enum arm_reg_type basetype;
2378   struct reg_entry *basereg;
2379   struct reg_entry mybasereg;
2380   struct neon_type ntype;
2381   struct neon_typed_alias typeinfo;
2382   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2383   int namelen;
2384
2385   typeinfo.defined = 0;
2386   typeinfo.eltype.type = NT_invtype;
2387   typeinfo.eltype.size = -1;
2388   typeinfo.index = -1;
2389
2390   nameend = p;
2391
2392   if (strncmp (p, " .dn ", 5) == 0)
2393     basetype = REG_TYPE_VFD;
2394   else if (strncmp (p, " .qn ", 5) == 0)
2395     basetype = REG_TYPE_NQ;
2396   else
2397     return FALSE;
2398
2399   p += 5;
2400
2401   if (*p == '\0')
2402     return FALSE;
2403
2404   basereg = arm_reg_parse_multi (&p);
2405
2406   if (basereg && basereg->type != basetype)
2407     {
2408       as_bad (_("bad type for register"));
2409       return FALSE;
2410     }
2411
2412   if (basereg == NULL)
2413     {
2414       expressionS exp;
2415       /* Try parsing as an integer.  */
2416       my_get_expression (&exp, &p, GE_NO_PREFIX);
2417       if (exp.X_op != O_constant)
2418         {
2419           as_bad (_("expression must be constant"));
2420           return FALSE;
2421         }
2422       basereg = &mybasereg;
2423       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2424                                                   : exp.X_add_number;
2425       basereg->neon = 0;
2426     }
2427
2428   if (basereg->neon)
2429     typeinfo = *basereg->neon;
2430
2431   if (parse_neon_type (&ntype, &p) == SUCCESS)
2432     {
2433       /* We got a type.  */
2434       if (typeinfo.defined & NTA_HASTYPE)
2435         {
2436           as_bad (_("can't redefine the type of a register alias"));
2437           return FALSE;
2438         }
2439
2440       typeinfo.defined |= NTA_HASTYPE;
2441       if (ntype.elems != 1)
2442         {
2443           as_bad (_("you must specify a single type only"));
2444           return FALSE;
2445         }
2446       typeinfo.eltype = ntype.el[0];
2447     }
2448
2449   if (skip_past_char (&p, '[') == SUCCESS)
2450     {
2451       expressionS exp;
2452       /* We got a scalar index.  */
2453
2454       if (typeinfo.defined & NTA_HASINDEX)
2455         {
2456           as_bad (_("can't redefine the index of a scalar alias"));
2457           return FALSE;
2458         }
2459
2460       my_get_expression (&exp, &p, GE_NO_PREFIX);
2461
2462       if (exp.X_op != O_constant)
2463         {
2464           as_bad (_("scalar index must be constant"));
2465           return FALSE;
2466         }
2467
2468       typeinfo.defined |= NTA_HASINDEX;
2469       typeinfo.index = exp.X_add_number;
2470
2471       if (skip_past_char (&p, ']') == FAIL)
2472         {
2473           as_bad (_("expecting ]"));
2474           return FALSE;
2475         }
2476     }
2477
2478   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2479      the desired alias name, and p points to its end.  If not, then
2480      the desired alias name is in the global original_case_string.  */
2481 #ifdef TC_CASE_SENSITIVE
2482   namelen = nameend - newname;
2483 #else
2484   newname = original_case_string;
2485   namelen = strlen (newname);
2486 #endif
2487
2488   namebuf = xmemdup0 (newname, namelen);
2489
2490   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2491                          typeinfo.defined != 0 ? &typeinfo : NULL);
2492
2493   /* Insert name in all uppercase.  */
2494   for (p = namebuf; *p; p++)
2495     *p = TOUPPER (*p);
2496
2497   if (strncmp (namebuf, newname, namelen))
2498     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2499                            typeinfo.defined != 0 ? &typeinfo : NULL);
2500
2501   /* Insert name in all lowercase.  */
2502   for (p = namebuf; *p; p++)
2503     *p = TOLOWER (*p);
2504
2505   if (strncmp (namebuf, newname, namelen))
2506     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2507                            typeinfo.defined != 0 ? &typeinfo : NULL);
2508
2509   free (namebuf);
2510   return TRUE;
2511 }
2512
2513 /* Should never be called, as .req goes between the alias and the
2514    register name, not at the beginning of the line.  */
2515
2516 static void
2517 s_req (int a ATTRIBUTE_UNUSED)
2518 {
2519   as_bad (_("invalid syntax for .req directive"));
2520 }
2521
2522 static void
2523 s_dn (int a ATTRIBUTE_UNUSED)
2524 {
2525   as_bad (_("invalid syntax for .dn directive"));
2526 }
2527
2528 static void
2529 s_qn (int a ATTRIBUTE_UNUSED)
2530 {
2531   as_bad (_("invalid syntax for .qn directive"));
2532 }
2533
2534 /* The .unreq directive deletes an alias which was previously defined
2535    by .req.  For example:
2536
2537        my_alias .req r11
2538        .unreq my_alias    */
2539
2540 static void
2541 s_unreq (int a ATTRIBUTE_UNUSED)
2542 {
2543   char * name;
2544   char saved_char;
2545
2546   name = input_line_pointer;
2547
2548   while (*input_line_pointer != 0
2549          && *input_line_pointer != ' '
2550          && *input_line_pointer != '\n')
2551     ++input_line_pointer;
2552
2553   saved_char = *input_line_pointer;
2554   *input_line_pointer = 0;
2555
2556   if (!*name)
2557     as_bad (_("invalid syntax for .unreq directive"));
2558   else
2559     {
2560       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2561                                                               name);
2562
2563       if (!reg)
2564         as_bad (_("unknown register alias '%s'"), name);
2565       else if (reg->builtin)
2566         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2567                  name);
2568       else
2569         {
2570           char * p;
2571           char * nbuf;
2572
2573           hash_delete (arm_reg_hsh, name, FALSE);
2574           free ((char *) reg->name);
2575           if (reg->neon)
2576             free (reg->neon);
2577           free (reg);
2578
2579           /* Also locate the all upper case and all lower case versions.
2580              Do not complain if we cannot find one or the other as it
2581              was probably deleted above.  */
2582
2583           nbuf = strdup (name);
2584           for (p = nbuf; *p; p++)
2585             *p = TOUPPER (*p);
2586           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2587           if (reg)
2588             {
2589               hash_delete (arm_reg_hsh, nbuf, FALSE);
2590               free ((char *) reg->name);
2591               if (reg->neon)
2592                 free (reg->neon);
2593               free (reg);
2594             }
2595
2596           for (p = nbuf; *p; p++)
2597             *p = TOLOWER (*p);
2598           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2599           if (reg)
2600             {
2601               hash_delete (arm_reg_hsh, nbuf, FALSE);
2602               free ((char *) reg->name);
2603               if (reg->neon)
2604                 free (reg->neon);
2605               free (reg);
2606             }
2607
2608           free (nbuf);
2609         }
2610     }
2611
2612   *input_line_pointer = saved_char;
2613   demand_empty_rest_of_line ();
2614 }
2615
2616 /* Directives: Instruction set selection.  */
2617
2618 #ifdef OBJ_ELF
2619 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2620    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2621    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2622    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2623
2624 /* Create a new mapping symbol for the transition to STATE.  */
2625
2626 static void
2627 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2628 {
2629   symbolS * symbolP;
2630   const char * symname;
2631   int type;
2632
2633   switch (state)
2634     {
2635     case MAP_DATA:
2636       symname = "$d";
2637       type = BSF_NO_FLAGS;
2638       break;
2639     case MAP_ARM:
2640       symname = "$a";
2641       type = BSF_NO_FLAGS;
2642       break;
2643     case MAP_THUMB:
2644       symname = "$t";
2645       type = BSF_NO_FLAGS;
2646       break;
2647     default:
2648       abort ();
2649     }
2650
2651   symbolP = symbol_new (symname, now_seg, value, frag);
2652   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2653
2654   switch (state)
2655     {
2656     case MAP_ARM:
2657       THUMB_SET_FUNC (symbolP, 0);
2658       ARM_SET_THUMB (symbolP, 0);
2659       ARM_SET_INTERWORK (symbolP, support_interwork);
2660       break;
2661
2662     case MAP_THUMB:
2663       THUMB_SET_FUNC (symbolP, 1);
2664       ARM_SET_THUMB (symbolP, 1);
2665       ARM_SET_INTERWORK (symbolP, support_interwork);
2666       break;
2667
2668     case MAP_DATA:
2669     default:
2670       break;
2671     }
2672
2673   /* Save the mapping symbols for future reference.  Also check that
2674      we do not place two mapping symbols at the same offset within a
2675      frag.  We'll handle overlap between frags in
2676      check_mapping_symbols.
2677
2678      If .fill or other data filling directive generates zero sized data,
2679      the mapping symbol for the following code will have the same value
2680      as the one generated for the data filling directive.  In this case,
2681      we replace the old symbol with the new one at the same address.  */
2682   if (value == 0)
2683     {
2684       if (frag->tc_frag_data.first_map != NULL)
2685         {
2686           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2687           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2688         }
2689       frag->tc_frag_data.first_map = symbolP;
2690     }
2691   if (frag->tc_frag_data.last_map != NULL)
2692     {
2693       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2694       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2695         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2696     }
2697   frag->tc_frag_data.last_map = symbolP;
2698 }
2699
2700 /* We must sometimes convert a region marked as code to data during
2701    code alignment, if an odd number of bytes have to be padded.  The
2702    code mapping symbol is pushed to an aligned address.  */
2703
2704 static void
2705 insert_data_mapping_symbol (enum mstate state,
2706                             valueT value, fragS *frag, offsetT bytes)
2707 {
2708   /* If there was already a mapping symbol, remove it.  */
2709   if (frag->tc_frag_data.last_map != NULL
2710       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2711     {
2712       symbolS *symp = frag->tc_frag_data.last_map;
2713
2714       if (value == 0)
2715         {
2716           know (frag->tc_frag_data.first_map == symp);
2717           frag->tc_frag_data.first_map = NULL;
2718         }
2719       frag->tc_frag_data.last_map = NULL;
2720       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2721     }
2722
2723   make_mapping_symbol (MAP_DATA, value, frag);
2724   make_mapping_symbol (state, value + bytes, frag);
2725 }
2726
2727 static void mapping_state_2 (enum mstate state, int max_chars);
2728
2729 /* Set the mapping state to STATE.  Only call this when about to
2730    emit some STATE bytes to the file.  */
2731
2732 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2733 void
2734 mapping_state (enum mstate state)
2735 {
2736   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2737
2738   if (mapstate == state)
2739     /* The mapping symbol has already been emitted.
2740        There is nothing else to do.  */
2741     return;
2742
2743   if (state == MAP_ARM || state == MAP_THUMB)
2744     /*  PR gas/12931
2745         All ARM instructions require 4-byte alignment.
2746         (Almost) all Thumb instructions require 2-byte alignment.
2747
2748         When emitting instructions into any section, mark the section
2749         appropriately.
2750
2751         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2752         but themselves require 2-byte alignment; this applies to some
2753         PC- relative forms.  However, these cases will involve implicit
2754         literal pool generation or an explicit .align >=2, both of
2755         which will cause the section to me marked with sufficient
2756         alignment.  Thus, we don't handle those cases here.  */
2757     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2758
2759   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2760     /* This case will be evaluated later.  */
2761     return;
2762
2763   mapping_state_2 (state, 0);
2764 }
2765
2766 /* Same as mapping_state, but MAX_CHARS bytes have already been
2767    allocated.  Put the mapping symbol that far back.  */
2768
2769 static void
2770 mapping_state_2 (enum mstate state, int max_chars)
2771 {
2772   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2773
2774   if (!SEG_NORMAL (now_seg))
2775     return;
2776
2777   if (mapstate == state)
2778     /* The mapping symbol has already been emitted.
2779        There is nothing else to do.  */
2780     return;
2781
2782   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2783           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2784     {
2785       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2786       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2787
2788       if (add_symbol)
2789         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2790     }
2791
2792   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2793   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2794 }
2795 #undef TRANSITION
2796 #else
2797 #define mapping_state(x) ((void)0)
2798 #define mapping_state_2(x, y) ((void)0)
2799 #endif
2800
2801 /* Find the real, Thumb encoded start of a Thumb function.  */
2802
2803 #ifdef OBJ_COFF
2804 static symbolS *
2805 find_real_start (symbolS * symbolP)
2806 {
2807   char *       real_start;
2808   const char * name = S_GET_NAME (symbolP);
2809   symbolS *    new_target;
2810
2811   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2812 #define STUB_NAME ".real_start_of"
2813
2814   if (name == NULL)
2815     abort ();
2816
2817   /* The compiler may generate BL instructions to local labels because
2818      it needs to perform a branch to a far away location. These labels
2819      do not have a corresponding ".real_start_of" label.  We check
2820      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2821      the ".real_start_of" convention for nonlocal branches.  */
2822   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2823     return symbolP;
2824
2825   real_start = concat (STUB_NAME, name, NULL);
2826   new_target = symbol_find (real_start);
2827   free (real_start);
2828
2829   if (new_target == NULL)
2830     {
2831       as_warn (_("Failed to find real start of function: %s\n"), name);
2832       new_target = symbolP;
2833     }
2834
2835   return new_target;
2836 }
2837 #endif
2838
2839 static void
2840 opcode_select (int width)
2841 {
2842   switch (width)
2843     {
2844     case 16:
2845       if (! thumb_mode)
2846         {
2847           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2848             as_bad (_("selected processor does not support THUMB opcodes"));
2849
2850           thumb_mode = 1;
2851           /* No need to force the alignment, since we will have been
2852              coming from ARM mode, which is word-aligned.  */
2853           record_alignment (now_seg, 1);
2854         }
2855       break;
2856
2857     case 32:
2858       if (thumb_mode)
2859         {
2860           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2861             as_bad (_("selected processor does not support ARM opcodes"));
2862
2863           thumb_mode = 0;
2864
2865           if (!need_pass_2)
2866             frag_align (2, 0, 0);
2867
2868           record_alignment (now_seg, 1);
2869         }
2870       break;
2871
2872     default:
2873       as_bad (_("invalid instruction size selected (%d)"), width);
2874     }
2875 }
2876
2877 static void
2878 s_arm (int ignore ATTRIBUTE_UNUSED)
2879 {
2880   opcode_select (32);
2881   demand_empty_rest_of_line ();
2882 }
2883
2884 static void
2885 s_thumb (int ignore ATTRIBUTE_UNUSED)
2886 {
2887   opcode_select (16);
2888   demand_empty_rest_of_line ();
2889 }
2890
2891 static void
2892 s_code (int unused ATTRIBUTE_UNUSED)
2893 {
2894   int temp;
2895
2896   temp = get_absolute_expression ();
2897   switch (temp)
2898     {
2899     case 16:
2900     case 32:
2901       opcode_select (temp);
2902       break;
2903
2904     default:
2905       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2906     }
2907 }
2908
2909 static void
2910 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2911 {
2912   /* If we are not already in thumb mode go into it, EVEN if
2913      the target processor does not support thumb instructions.
2914      This is used by gcc/config/arm/lib1funcs.asm for example
2915      to compile interworking support functions even if the
2916      target processor should not support interworking.  */
2917   if (! thumb_mode)
2918     {
2919       thumb_mode = 2;
2920       record_alignment (now_seg, 1);
2921     }
2922
2923   demand_empty_rest_of_line ();
2924 }
2925
2926 static void
2927 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2928 {
2929   s_thumb (0);
2930
2931   /* The following label is the name/address of the start of a Thumb function.
2932      We need to know this for the interworking support.  */
2933   label_is_thumb_function_name = TRUE;
2934 }
2935
2936 /* Perform a .set directive, but also mark the alias as
2937    being a thumb function.  */
2938
2939 static void
2940 s_thumb_set (int equiv)
2941 {
2942   /* XXX the following is a duplicate of the code for s_set() in read.c
2943      We cannot just call that code as we need to get at the symbol that
2944      is created.  */
2945   char *    name;
2946   char      delim;
2947   char *    end_name;
2948   symbolS * symbolP;
2949
2950   /* Especial apologies for the random logic:
2951      This just grew, and could be parsed much more simply!
2952      Dean - in haste.  */
2953   delim     = get_symbol_name (& name);
2954   end_name  = input_line_pointer;
2955   (void) restore_line_pointer (delim);
2956
2957   if (*input_line_pointer != ',')
2958     {
2959       *end_name = 0;
2960       as_bad (_("expected comma after name \"%s\""), name);
2961       *end_name = delim;
2962       ignore_rest_of_line ();
2963       return;
2964     }
2965
2966   input_line_pointer++;
2967   *end_name = 0;
2968
2969   if (name[0] == '.' && name[1] == '\0')
2970     {
2971       /* XXX - this should not happen to .thumb_set.  */
2972       abort ();
2973     }
2974
2975   if ((symbolP = symbol_find (name)) == NULL
2976       && (symbolP = md_undefined_symbol (name)) == NULL)
2977     {
2978 #ifndef NO_LISTING
2979       /* When doing symbol listings, play games with dummy fragments living
2980          outside the normal fragment chain to record the file and line info
2981          for this symbol.  */
2982       if (listing & LISTING_SYMBOLS)
2983         {
2984           extern struct list_info_struct * listing_tail;
2985           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2986
2987           memset (dummy_frag, 0, sizeof (fragS));
2988           dummy_frag->fr_type = rs_fill;
2989           dummy_frag->line = listing_tail;
2990           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2991           dummy_frag->fr_symbol = symbolP;
2992         }
2993       else
2994 #endif
2995         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2996
2997 #ifdef OBJ_COFF
2998       /* "set" symbols are local unless otherwise specified.  */
2999       SF_SET_LOCAL (symbolP);
3000 #endif /* OBJ_COFF  */
3001     }                           /* Make a new symbol.  */
3002
3003   symbol_table_insert (symbolP);
3004
3005   * end_name = delim;
3006
3007   if (equiv
3008       && S_IS_DEFINED (symbolP)
3009       && S_GET_SEGMENT (symbolP) != reg_section)
3010     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
3011
3012   pseudo_set (symbolP);
3013
3014   demand_empty_rest_of_line ();
3015
3016   /* XXX Now we come to the Thumb specific bit of code.  */
3017
3018   THUMB_SET_FUNC (symbolP, 1);
3019   ARM_SET_THUMB (symbolP, 1);
3020 #if defined OBJ_ELF || defined OBJ_COFF
3021   ARM_SET_INTERWORK (symbolP, support_interwork);
3022 #endif
3023 }
3024
3025 /* Directives: Mode selection.  */
3026
3027 /* .syntax [unified|divided] - choose the new unified syntax
3028    (same for Arm and Thumb encoding, modulo slight differences in what
3029    can be represented) or the old divergent syntax for each mode.  */
3030 static void
3031 s_syntax (int unused ATTRIBUTE_UNUSED)
3032 {
3033   char *name, delim;
3034
3035   delim = get_symbol_name (& name);
3036
3037   if (!strcasecmp (name, "unified"))
3038     unified_syntax = TRUE;
3039   else if (!strcasecmp (name, "divided"))
3040     unified_syntax = FALSE;
3041   else
3042     {
3043       as_bad (_("unrecognized syntax mode \"%s\""), name);
3044       return;
3045     }
3046   (void) restore_line_pointer (delim);
3047   demand_empty_rest_of_line ();
3048 }
3049
3050 /* Directives: sectioning and alignment.  */
3051
3052 static void
3053 s_bss (int ignore ATTRIBUTE_UNUSED)
3054 {
3055   /* We don't support putting frags in the BSS segment, we fake it by
3056      marking in_bss, then looking at s_skip for clues.  */
3057   subseg_set (bss_section, 0);
3058   demand_empty_rest_of_line ();
3059
3060 #ifdef md_elf_section_change_hook
3061   md_elf_section_change_hook ();
3062 #endif
3063 }
3064
3065 static void
3066 s_even (int ignore ATTRIBUTE_UNUSED)
3067 {
3068   /* Never make frag if expect extra pass.  */
3069   if (!need_pass_2)
3070     frag_align (1, 0, 0);
3071
3072   record_alignment (now_seg, 1);
3073
3074   demand_empty_rest_of_line ();
3075 }
3076
3077 /* Directives: CodeComposer Studio.  */
3078
3079 /*  .ref  (for CodeComposer Studio syntax only).  */
3080 static void
3081 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3082 {
3083   if (codecomposer_syntax)
3084     ignore_rest_of_line ();
3085   else
3086     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3087 }
3088
3089 /*  If name is not NULL, then it is used for marking the beginning of a
3090     function, whereas if it is NULL then it means the function end.  */
3091 static void
3092 asmfunc_debug (const char * name)
3093 {
3094   static const char * last_name = NULL;
3095
3096   if (name != NULL)
3097     {
3098       gas_assert (last_name == NULL);
3099       last_name = name;
3100
3101       if (debug_type == DEBUG_STABS)
3102          stabs_generate_asm_func (name, name);
3103     }
3104   else
3105     {
3106       gas_assert (last_name != NULL);
3107
3108       if (debug_type == DEBUG_STABS)
3109         stabs_generate_asm_endfunc (last_name, last_name);
3110
3111       last_name = NULL;
3112     }
3113 }
3114
3115 static void
3116 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3117 {
3118   if (codecomposer_syntax)
3119     {
3120       switch (asmfunc_state)
3121         {
3122         case OUTSIDE_ASMFUNC:
3123           asmfunc_state = WAITING_ASMFUNC_NAME;
3124           break;
3125
3126         case WAITING_ASMFUNC_NAME:
3127           as_bad (_(".asmfunc repeated."));
3128           break;
3129
3130         case WAITING_ENDASMFUNC:
3131           as_bad (_(".asmfunc without function."));
3132           break;
3133         }
3134       demand_empty_rest_of_line ();
3135     }
3136   else
3137     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3138 }
3139
3140 static void
3141 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3142 {
3143   if (codecomposer_syntax)
3144     {
3145       switch (asmfunc_state)
3146         {
3147         case OUTSIDE_ASMFUNC:
3148           as_bad (_(".endasmfunc without a .asmfunc."));
3149           break;
3150
3151         case WAITING_ASMFUNC_NAME:
3152           as_bad (_(".endasmfunc without function."));
3153           break;
3154
3155         case WAITING_ENDASMFUNC:
3156           asmfunc_state = OUTSIDE_ASMFUNC;
3157           asmfunc_debug (NULL);
3158           break;
3159         }
3160       demand_empty_rest_of_line ();
3161     }
3162   else
3163     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3164 }
3165
3166 static void
3167 s_ccs_def (int name)
3168 {
3169   if (codecomposer_syntax)
3170     s_globl (name);
3171   else
3172     as_bad (_(".def pseudo-op only available with -mccs flag."));
3173 }
3174
3175 /* Directives: Literal pools.  */
3176
3177 static literal_pool *
3178 find_literal_pool (void)
3179 {
3180   literal_pool * pool;
3181
3182   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3183     {
3184       if (pool->section == now_seg
3185           && pool->sub_section == now_subseg)
3186         break;
3187     }
3188
3189   return pool;
3190 }
3191
3192 static literal_pool *
3193 find_or_make_literal_pool (void)
3194 {
3195   /* Next literal pool ID number.  */
3196   static unsigned int latest_pool_num = 1;
3197   literal_pool *      pool;
3198
3199   pool = find_literal_pool ();
3200
3201   if (pool == NULL)
3202     {
3203       /* Create a new pool.  */
3204       pool = XNEW (literal_pool);
3205       if (! pool)
3206         return NULL;
3207
3208       pool->next_free_entry = 0;
3209       pool->section         = now_seg;
3210       pool->sub_section     = now_subseg;
3211       pool->next            = list_of_pools;
3212       pool->symbol          = NULL;
3213       pool->alignment       = 2;
3214
3215       /* Add it to the list.  */
3216       list_of_pools = pool;
3217     }
3218
3219   /* New pools, and emptied pools, will have a NULL symbol.  */
3220   if (pool->symbol == NULL)
3221     {
3222       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3223                                     (valueT) 0, &zero_address_frag);
3224       pool->id = latest_pool_num ++;
3225     }
3226
3227   /* Done.  */
3228   return pool;
3229 }
3230
3231 /* Add the literal in the global 'inst'
3232    structure to the relevant literal pool.  */
3233
3234 static int
3235 add_to_lit_pool (unsigned int nbytes)
3236 {
3237 #define PADDING_SLOT 0x1
3238 #define LIT_ENTRY_SIZE_MASK 0xFF
3239   literal_pool * pool;
3240   unsigned int entry, pool_size = 0;
3241   bfd_boolean padding_slot_p = FALSE;
3242   unsigned imm1 = 0;
3243   unsigned imm2 = 0;
3244
3245   if (nbytes == 8)
3246     {
3247       imm1 = inst.operands[1].imm;
3248       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3249                : inst.reloc.exp.X_unsigned ? 0
3250                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3251       if (target_big_endian)
3252         {
3253           imm1 = imm2;
3254           imm2 = inst.operands[1].imm;
3255         }
3256     }
3257
3258   pool = find_or_make_literal_pool ();
3259
3260   /* Check if this literal value is already in the pool.  */
3261   for (entry = 0; entry < pool->next_free_entry; entry ++)
3262     {
3263       if (nbytes == 4)
3264         {
3265           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3266               && (inst.reloc.exp.X_op == O_constant)
3267               && (pool->literals[entry].X_add_number
3268                   == inst.reloc.exp.X_add_number)
3269               && (pool->literals[entry].X_md == nbytes)
3270               && (pool->literals[entry].X_unsigned
3271                   == inst.reloc.exp.X_unsigned))
3272             break;
3273
3274           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3275               && (inst.reloc.exp.X_op == O_symbol)
3276               && (pool->literals[entry].X_add_number
3277                   == inst.reloc.exp.X_add_number)
3278               && (pool->literals[entry].X_add_symbol
3279                   == inst.reloc.exp.X_add_symbol)
3280               && (pool->literals[entry].X_op_symbol
3281                   == inst.reloc.exp.X_op_symbol)
3282               && (pool->literals[entry].X_md == nbytes))
3283             break;
3284         }
3285       else if ((nbytes == 8)
3286                && !(pool_size & 0x7)
3287                && ((entry + 1) != pool->next_free_entry)
3288                && (pool->literals[entry].X_op == O_constant)
3289                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3290                && (pool->literals[entry].X_unsigned
3291                    == inst.reloc.exp.X_unsigned)
3292                && (pool->literals[entry + 1].X_op == O_constant)
3293                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3294                && (pool->literals[entry + 1].X_unsigned
3295                    == inst.reloc.exp.X_unsigned))
3296         break;
3297
3298       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3299       if (padding_slot_p && (nbytes == 4))
3300         break;
3301
3302       pool_size += 4;
3303     }
3304
3305   /* Do we need to create a new entry?  */
3306   if (entry == pool->next_free_entry)
3307     {
3308       if (entry >= MAX_LITERAL_POOL_SIZE)
3309         {
3310           inst.error = _("literal pool overflow");
3311           return FAIL;
3312         }
3313
3314       if (nbytes == 8)
3315         {
3316           /* For 8-byte entries, we align to an 8-byte boundary,
3317              and split it into two 4-byte entries, because on 32-bit
3318              host, 8-byte constants are treated as big num, thus
3319              saved in "generic_bignum" which will be overwritten
3320              by later assignments.
3321
3322              We also need to make sure there is enough space for
3323              the split.
3324
3325              We also check to make sure the literal operand is a
3326              constant number.  */
3327           if (!(inst.reloc.exp.X_op == O_constant
3328                 || inst.reloc.exp.X_op == O_big))
3329             {
3330               inst.error = _("invalid type for literal pool");
3331               return FAIL;
3332             }
3333           else if (pool_size & 0x7)
3334             {
3335               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3336                 {
3337                   inst.error = _("literal pool overflow");
3338                   return FAIL;
3339                 }
3340
3341               pool->literals[entry] = inst.reloc.exp;
3342               pool->literals[entry].X_op = O_constant;
3343               pool->literals[entry].X_add_number = 0;
3344               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3345               pool->next_free_entry += 1;
3346               pool_size += 4;
3347             }
3348           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3349             {
3350               inst.error = _("literal pool overflow");
3351               return FAIL;
3352             }
3353
3354           pool->literals[entry] = inst.reloc.exp;
3355           pool->literals[entry].X_op = O_constant;
3356           pool->literals[entry].X_add_number = imm1;
3357           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3358           pool->literals[entry++].X_md = 4;
3359           pool->literals[entry] = inst.reloc.exp;
3360           pool->literals[entry].X_op = O_constant;
3361           pool->literals[entry].X_add_number = imm2;
3362           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3363           pool->literals[entry].X_md = 4;
3364           pool->alignment = 3;
3365           pool->next_free_entry += 1;
3366         }
3367       else
3368         {
3369           pool->literals[entry] = inst.reloc.exp;
3370           pool->literals[entry].X_md = 4;
3371         }
3372
3373 #ifdef OBJ_ELF
3374       /* PR ld/12974: Record the location of the first source line to reference
3375          this entry in the literal pool.  If it turns out during linking that the
3376          symbol does not exist we will be able to give an accurate line number for
3377          the (first use of the) missing reference.  */
3378       if (debug_type == DEBUG_DWARF2)
3379         dwarf2_where (pool->locs + entry);
3380 #endif
3381       pool->next_free_entry += 1;
3382     }
3383   else if (padding_slot_p)
3384     {
3385       pool->literals[entry] = inst.reloc.exp;
3386       pool->literals[entry].X_md = nbytes;
3387     }
3388
3389   inst.reloc.exp.X_op         = O_symbol;
3390   inst.reloc.exp.X_add_number = pool_size;
3391   inst.reloc.exp.X_add_symbol = pool->symbol;
3392
3393   return SUCCESS;
3394 }
3395
3396 bfd_boolean
3397 tc_start_label_without_colon (void)
3398 {
3399   bfd_boolean ret = TRUE;
3400
3401   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3402     {
3403       const char *label = input_line_pointer;
3404
3405       while (!is_end_of_line[(int) label[-1]])
3406         --label;
3407
3408       if (*label == '.')
3409         {
3410           as_bad (_("Invalid label '%s'"), label);
3411           ret = FALSE;
3412         }
3413
3414       asmfunc_debug (label);
3415
3416       asmfunc_state = WAITING_ENDASMFUNC;
3417     }
3418
3419   return ret;
3420 }
3421
3422 /* Can't use symbol_new here, so have to create a symbol and then at
3423    a later date assign it a value. That's what these functions do.  */
3424
3425 static void
3426 symbol_locate (symbolS *    symbolP,
3427                const char * name,       /* It is copied, the caller can modify.  */
3428                segT         segment,    /* Segment identifier (SEG_<something>).  */
3429                valueT       valu,       /* Symbol value.  */
3430                fragS *      frag)       /* Associated fragment.  */
3431 {
3432   size_t name_length;
3433   char * preserved_copy_of_name;
3434
3435   name_length = strlen (name) + 1;   /* +1 for \0.  */
3436   obstack_grow (&notes, name, name_length);
3437   preserved_copy_of_name = (char *) obstack_finish (&notes);
3438
3439 #ifdef tc_canonicalize_symbol_name
3440   preserved_copy_of_name =
3441     tc_canonicalize_symbol_name (preserved_copy_of_name);
3442 #endif
3443
3444   S_SET_NAME (symbolP, preserved_copy_of_name);
3445
3446   S_SET_SEGMENT (symbolP, segment);
3447   S_SET_VALUE (symbolP, valu);
3448   symbol_clear_list_pointers (symbolP);
3449
3450   symbol_set_frag (symbolP, frag);
3451
3452   /* Link to end of symbol chain.  */
3453   {
3454     extern int symbol_table_frozen;
3455
3456     if (symbol_table_frozen)
3457       abort ();
3458   }
3459
3460   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3461
3462   obj_symbol_new_hook (symbolP);
3463
3464 #ifdef tc_symbol_new_hook
3465   tc_symbol_new_hook (symbolP);
3466 #endif
3467
3468 #ifdef DEBUG_SYMS
3469   verify_symbol_chain (symbol_rootP, symbol_lastP);
3470 #endif /* DEBUG_SYMS  */
3471 }
3472
3473 static void
3474 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3475 {
3476   unsigned int entry;
3477   literal_pool * pool;
3478   char sym_name[20];
3479
3480   pool = find_literal_pool ();
3481   if (pool == NULL
3482       || pool->symbol == NULL
3483       || pool->next_free_entry == 0)
3484     return;
3485
3486   /* Align pool as you have word accesses.
3487      Only make a frag if we have to.  */
3488   if (!need_pass_2)
3489     frag_align (pool->alignment, 0, 0);
3490
3491   record_alignment (now_seg, 2);
3492
3493 #ifdef OBJ_ELF
3494   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3495   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3496 #endif
3497   sprintf (sym_name, "$$lit_\002%x", pool->id);
3498
3499   symbol_locate (pool->symbol, sym_name, now_seg,
3500                  (valueT) frag_now_fix (), frag_now);
3501   symbol_table_insert (pool->symbol);
3502
3503   ARM_SET_THUMB (pool->symbol, thumb_mode);
3504
3505 #if defined OBJ_COFF || defined OBJ_ELF
3506   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3507 #endif
3508
3509   for (entry = 0; entry < pool->next_free_entry; entry ++)
3510     {
3511 #ifdef OBJ_ELF
3512       if (debug_type == DEBUG_DWARF2)
3513         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3514 #endif
3515       /* First output the expression in the instruction to the pool.  */
3516       emit_expr (&(pool->literals[entry]),
3517                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3518     }
3519
3520   /* Mark the pool as empty.  */
3521   pool->next_free_entry = 0;
3522   pool->symbol = NULL;
3523 }
3524
3525 #ifdef OBJ_ELF
3526 /* Forward declarations for functions below, in the MD interface
3527    section.  */
3528 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3529 static valueT create_unwind_entry (int);
3530 static void start_unwind_section (const segT, int);
3531 static void add_unwind_opcode (valueT, int);
3532 static void flush_pending_unwind (void);
3533
3534 /* Directives: Data.  */
3535
3536 static void
3537 s_arm_elf_cons (int nbytes)
3538 {
3539   expressionS exp;
3540
3541 #ifdef md_flush_pending_output
3542   md_flush_pending_output ();
3543 #endif
3544
3545   if (is_it_end_of_statement ())
3546     {
3547       demand_empty_rest_of_line ();
3548       return;
3549     }
3550
3551 #ifdef md_cons_align
3552   md_cons_align (nbytes);
3553 #endif
3554
3555   mapping_state (MAP_DATA);
3556   do
3557     {
3558       int reloc;
3559       char *base = input_line_pointer;
3560
3561       expression (& exp);
3562
3563       if (exp.X_op != O_symbol)
3564         emit_expr (&exp, (unsigned int) nbytes);
3565       else
3566         {
3567           char *before_reloc = input_line_pointer;
3568           reloc = parse_reloc (&input_line_pointer);
3569           if (reloc == -1)
3570             {
3571               as_bad (_("unrecognized relocation suffix"));
3572               ignore_rest_of_line ();
3573               return;
3574             }
3575           else if (reloc == BFD_RELOC_UNUSED)
3576             emit_expr (&exp, (unsigned int) nbytes);
3577           else
3578             {
3579               reloc_howto_type *howto = (reloc_howto_type *)
3580                   bfd_reloc_type_lookup (stdoutput,
3581                                          (bfd_reloc_code_real_type) reloc);
3582               int size = bfd_get_reloc_size (howto);
3583
3584               if (reloc == BFD_RELOC_ARM_PLT32)
3585                 {
3586                   as_bad (_("(plt) is only valid on branch targets"));
3587                   reloc = BFD_RELOC_UNUSED;
3588                   size = 0;
3589                 }
3590
3591               if (size > nbytes)
3592                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3593                                   "%s relocations do not fit in %d bytes",
3594                                   nbytes),
3595                         howto->name, nbytes);
3596               else
3597                 {
3598                   /* We've parsed an expression stopping at O_symbol.
3599                      But there may be more expression left now that we
3600                      have parsed the relocation marker.  Parse it again.
3601                      XXX Surely there is a cleaner way to do this.  */
3602                   char *p = input_line_pointer;
3603                   int offset;
3604                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3605
3606                   memcpy (save_buf, base, input_line_pointer - base);
3607                   memmove (base + (input_line_pointer - before_reloc),
3608                            base, before_reloc - base);
3609
3610                   input_line_pointer = base + (input_line_pointer-before_reloc);
3611                   expression (&exp);
3612                   memcpy (base, save_buf, p - base);
3613
3614                   offset = nbytes - size;
3615                   p = frag_more (nbytes);
3616                   memset (p, 0, nbytes);
3617                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3618                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3619                   free (save_buf);
3620                 }
3621             }
3622         }
3623     }
3624   while (*input_line_pointer++ == ',');
3625
3626   /* Put terminator back into stream.  */
3627   input_line_pointer --;
3628   demand_empty_rest_of_line ();
3629 }
3630
3631 /* Emit an expression containing a 32-bit thumb instruction.
3632    Implementation based on put_thumb32_insn.  */
3633
3634 static void
3635 emit_thumb32_expr (expressionS * exp)
3636 {
3637   expressionS exp_high = *exp;
3638
3639   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3640   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3641   exp->X_add_number &= 0xffff;
3642   emit_expr (exp, (unsigned int) THUMB_SIZE);
3643 }
3644
3645 /*  Guess the instruction size based on the opcode.  */
3646
3647 static int
3648 thumb_insn_size (int opcode)
3649 {
3650   if ((unsigned int) opcode < 0xe800u)
3651     return 2;
3652   else if ((unsigned int) opcode >= 0xe8000000u)
3653     return 4;
3654   else
3655     return 0;
3656 }
3657
3658 static bfd_boolean
3659 emit_insn (expressionS *exp, int nbytes)
3660 {
3661   int size = 0;
3662
3663   if (exp->X_op == O_constant)
3664     {
3665       size = nbytes;
3666
3667       if (size == 0)
3668         size = thumb_insn_size (exp->X_add_number);
3669
3670       if (size != 0)
3671         {
3672           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3673             {
3674               as_bad (_(".inst.n operand too big. "\
3675                         "Use .inst.w instead"));
3676               size = 0;
3677             }
3678           else
3679             {
3680               if (now_it.state == AUTOMATIC_IT_BLOCK)
3681                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3682               else
3683                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3684
3685               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3686                 emit_thumb32_expr (exp);
3687               else
3688                 emit_expr (exp, (unsigned int) size);
3689
3690               it_fsm_post_encode ();
3691             }
3692         }
3693       else
3694         as_bad (_("cannot determine Thumb instruction size. "   \
3695                   "Use .inst.n/.inst.w instead"));
3696     }
3697   else
3698     as_bad (_("constant expression required"));
3699
3700   return (size != 0);
3701 }
3702
3703 /* Like s_arm_elf_cons but do not use md_cons_align and
3704    set the mapping state to MAP_ARM/MAP_THUMB.  */
3705
3706 static void
3707 s_arm_elf_inst (int nbytes)
3708 {
3709   if (is_it_end_of_statement ())
3710     {
3711       demand_empty_rest_of_line ();
3712       return;
3713     }
3714
3715   /* Calling mapping_state () here will not change ARM/THUMB,
3716      but will ensure not to be in DATA state.  */
3717
3718   if (thumb_mode)
3719     mapping_state (MAP_THUMB);
3720   else
3721     {
3722       if (nbytes != 0)
3723         {
3724           as_bad (_("width suffixes are invalid in ARM mode"));
3725           ignore_rest_of_line ();
3726           return;
3727         }
3728
3729       nbytes = 4;
3730
3731       mapping_state (MAP_ARM);
3732     }
3733
3734   do
3735     {
3736       expressionS exp;
3737
3738       expression (& exp);
3739
3740       if (! emit_insn (& exp, nbytes))
3741         {
3742           ignore_rest_of_line ();
3743           return;
3744         }
3745     }
3746   while (*input_line_pointer++ == ',');
3747
3748   /* Put terminator back into stream.  */
3749   input_line_pointer --;
3750   demand_empty_rest_of_line ();
3751 }
3752
3753 /* Parse a .rel31 directive.  */
3754
3755 static void
3756 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3757 {
3758   expressionS exp;
3759   char *p;
3760   valueT highbit;
3761
3762   highbit = 0;
3763   if (*input_line_pointer == '1')
3764     highbit = 0x80000000;
3765   else if (*input_line_pointer != '0')
3766     as_bad (_("expected 0 or 1"));
3767
3768   input_line_pointer++;
3769   if (*input_line_pointer != ',')
3770     as_bad (_("missing comma"));
3771   input_line_pointer++;
3772
3773 #ifdef md_flush_pending_output
3774   md_flush_pending_output ();
3775 #endif
3776
3777 #ifdef md_cons_align
3778   md_cons_align (4);
3779 #endif
3780
3781   mapping_state (MAP_DATA);
3782
3783   expression (&exp);
3784
3785   p = frag_more (4);
3786   md_number_to_chars (p, highbit, 4);
3787   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3788                BFD_RELOC_ARM_PREL31);
3789
3790   demand_empty_rest_of_line ();
3791 }
3792
3793 /* Directives: AEABI stack-unwind tables.  */
3794
3795 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3796
3797 static void
3798 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3799 {
3800   demand_empty_rest_of_line ();
3801   if (unwind.proc_start)
3802     {
3803       as_bad (_("duplicate .fnstart directive"));
3804       return;
3805     }
3806
3807   /* Mark the start of the function.  */
3808   unwind.proc_start = expr_build_dot ();
3809
3810   /* Reset the rest of the unwind info.  */
3811   unwind.opcode_count = 0;
3812   unwind.table_entry = NULL;
3813   unwind.personality_routine = NULL;
3814   unwind.personality_index = -1;
3815   unwind.frame_size = 0;
3816   unwind.fp_offset = 0;
3817   unwind.fp_reg = REG_SP;
3818   unwind.fp_used = 0;
3819   unwind.sp_restored = 0;
3820 }
3821
3822
3823 /* Parse a handlerdata directive.  Creates the exception handling table entry
3824    for the function.  */
3825
3826 static void
3827 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3828 {
3829   demand_empty_rest_of_line ();
3830   if (!unwind.proc_start)
3831     as_bad (MISSING_FNSTART);
3832
3833   if (unwind.table_entry)
3834     as_bad (_("duplicate .handlerdata directive"));
3835
3836   create_unwind_entry (1);
3837 }
3838
3839 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3840
3841 static void
3842 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3843 {
3844   long where;
3845   char *ptr;
3846   valueT val;
3847   unsigned int marked_pr_dependency;
3848
3849   demand_empty_rest_of_line ();
3850
3851   if (!unwind.proc_start)
3852     {
3853       as_bad (_(".fnend directive without .fnstart"));
3854       return;
3855     }
3856
3857   /* Add eh table entry.  */
3858   if (unwind.table_entry == NULL)
3859     val = create_unwind_entry (0);
3860   else
3861     val = 0;
3862
3863   /* Add index table entry.  This is two words.  */
3864   start_unwind_section (unwind.saved_seg, 1);
3865   frag_align (2, 0, 0);
3866   record_alignment (now_seg, 2);
3867
3868   ptr = frag_more (8);
3869   memset (ptr, 0, 8);
3870   where = frag_now_fix () - 8;
3871
3872   /* Self relative offset of the function start.  */
3873   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3874            BFD_RELOC_ARM_PREL31);
3875
3876   /* Indicate dependency on EHABI-defined personality routines to the
3877      linker, if it hasn't been done already.  */
3878   marked_pr_dependency
3879     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3880   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3881       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3882     {
3883       static const char *const name[] =
3884         {
3885           "__aeabi_unwind_cpp_pr0",
3886           "__aeabi_unwind_cpp_pr1",
3887           "__aeabi_unwind_cpp_pr2"
3888         };
3889       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3890       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3891       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3892         |= 1 << unwind.personality_index;
3893     }
3894
3895   if (val)
3896     /* Inline exception table entry.  */
3897     md_number_to_chars (ptr + 4, val, 4);
3898   else
3899     /* Self relative offset of the table entry.  */
3900     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3901              BFD_RELOC_ARM_PREL31);
3902
3903   /* Restore the original section.  */
3904   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3905
3906   unwind.proc_start = NULL;
3907 }
3908
3909
3910 /* Parse an unwind_cantunwind directive.  */
3911
3912 static void
3913 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3914 {
3915   demand_empty_rest_of_line ();
3916   if (!unwind.proc_start)
3917     as_bad (MISSING_FNSTART);
3918
3919   if (unwind.personality_routine || unwind.personality_index != -1)
3920     as_bad (_("personality routine specified for cantunwind frame"));
3921
3922   unwind.personality_index = -2;
3923 }
3924
3925
3926 /* Parse a personalityindex directive.  */
3927
3928 static void
3929 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3930 {
3931   expressionS exp;
3932
3933   if (!unwind.proc_start)
3934     as_bad (MISSING_FNSTART);
3935
3936   if (unwind.personality_routine || unwind.personality_index != -1)
3937     as_bad (_("duplicate .personalityindex directive"));
3938
3939   expression (&exp);
3940
3941   if (exp.X_op != O_constant
3942       || exp.X_add_number < 0 || exp.X_add_number > 15)
3943     {
3944       as_bad (_("bad personality routine number"));
3945       ignore_rest_of_line ();
3946       return;
3947     }
3948
3949   unwind.personality_index = exp.X_add_number;
3950
3951   demand_empty_rest_of_line ();
3952 }
3953
3954
3955 /* Parse a personality directive.  */
3956
3957 static void
3958 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3959 {
3960   char *name, *p, c;
3961
3962   if (!unwind.proc_start)
3963     as_bad (MISSING_FNSTART);
3964
3965   if (unwind.personality_routine || unwind.personality_index != -1)
3966     as_bad (_("duplicate .personality directive"));
3967
3968   c = get_symbol_name (& name);
3969   p = input_line_pointer;
3970   if (c == '"')
3971     ++ input_line_pointer;
3972   unwind.personality_routine = symbol_find_or_make (name);
3973   *p = c;
3974   demand_empty_rest_of_line ();
3975 }
3976
3977
3978 /* Parse a directive saving core registers.  */
3979
3980 static void
3981 s_arm_unwind_save_core (void)
3982 {
3983   valueT op;
3984   long range;
3985   int n;
3986
3987   range = parse_reg_list (&input_line_pointer);
3988   if (range == FAIL)
3989     {
3990       as_bad (_("expected register list"));
3991       ignore_rest_of_line ();
3992       return;
3993     }
3994
3995   demand_empty_rest_of_line ();
3996
3997   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3998      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3999      ip because it is clobbered by calls.  */
4000   if (unwind.sp_restored && unwind.fp_reg == 12
4001       && (range & 0x3000) == 0x1000)
4002     {
4003       unwind.opcode_count--;
4004       unwind.sp_restored = 0;
4005       range = (range | 0x2000) & ~0x1000;
4006       unwind.pending_offset = 0;
4007     }
4008
4009   /* Pop r4-r15.  */
4010   if (range & 0xfff0)
4011     {
4012       /* See if we can use the short opcodes.  These pop a block of up to 8
4013          registers starting with r4, plus maybe r14.  */
4014       for (n = 0; n < 8; n++)
4015         {
4016           /* Break at the first non-saved register.      */
4017           if ((range & (1 << (n + 4))) == 0)
4018             break;
4019         }
4020       /* See if there are any other bits set.  */
4021       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4022         {
4023           /* Use the long form.  */
4024           op = 0x8000 | ((range >> 4) & 0xfff);
4025           add_unwind_opcode (op, 2);
4026         }
4027       else
4028         {
4029           /* Use the short form.  */
4030           if (range & 0x4000)
4031             op = 0xa8; /* Pop r14.      */
4032           else
4033             op = 0xa0; /* Do not pop r14.  */
4034           op |= (n - 1);
4035           add_unwind_opcode (op, 1);
4036         }
4037     }
4038
4039   /* Pop r0-r3.  */
4040   if (range & 0xf)
4041     {
4042       op = 0xb100 | (range & 0xf);
4043       add_unwind_opcode (op, 2);
4044     }
4045
4046   /* Record the number of bytes pushed.  */
4047   for (n = 0; n < 16; n++)
4048     {
4049       if (range & (1 << n))
4050         unwind.frame_size += 4;
4051     }
4052 }
4053
4054
4055 /* Parse a directive saving FPA registers.  */
4056
4057 static void
4058 s_arm_unwind_save_fpa (int reg)
4059 {
4060   expressionS exp;
4061   int num_regs;
4062   valueT op;
4063
4064   /* Get Number of registers to transfer.  */
4065   if (skip_past_comma (&input_line_pointer) != FAIL)
4066     expression (&exp);
4067   else
4068     exp.X_op = O_illegal;
4069
4070   if (exp.X_op != O_constant)
4071     {
4072       as_bad (_("expected , <constant>"));
4073       ignore_rest_of_line ();
4074       return;
4075     }
4076
4077   num_regs = exp.X_add_number;
4078
4079   if (num_regs < 1 || num_regs > 4)
4080     {
4081       as_bad (_("number of registers must be in the range [1:4]"));
4082       ignore_rest_of_line ();
4083       return;
4084     }
4085
4086   demand_empty_rest_of_line ();
4087
4088   if (reg == 4)
4089     {
4090       /* Short form.  */
4091       op = 0xb4 | (num_regs - 1);
4092       add_unwind_opcode (op, 1);
4093     }
4094   else
4095     {
4096       /* Long form.  */
4097       op = 0xc800 | (reg << 4) | (num_regs - 1);
4098       add_unwind_opcode (op, 2);
4099     }
4100   unwind.frame_size += num_regs * 12;
4101 }
4102
4103
4104 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4105
4106 static void
4107 s_arm_unwind_save_vfp_armv6 (void)
4108 {
4109   int count;
4110   unsigned int start;
4111   valueT op;
4112   int num_vfpv3_regs = 0;
4113   int num_regs_below_16;
4114
4115   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
4116   if (count == FAIL)
4117     {
4118       as_bad (_("expected register list"));
4119       ignore_rest_of_line ();
4120       return;
4121     }
4122
4123   demand_empty_rest_of_line ();
4124
4125   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4126      than FSTMX/FLDMX-style ones).  */
4127
4128   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4129   if (start >= 16)
4130     num_vfpv3_regs = count;
4131   else if (start + count > 16)
4132     num_vfpv3_regs = start + count - 16;
4133
4134   if (num_vfpv3_regs > 0)
4135     {
4136       int start_offset = start > 16 ? start - 16 : 0;
4137       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4138       add_unwind_opcode (op, 2);
4139     }
4140
4141   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4142   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4143   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4144   if (num_regs_below_16 > 0)
4145     {
4146       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4147       add_unwind_opcode (op, 2);
4148     }
4149
4150   unwind.frame_size += count * 8;
4151 }
4152
4153
4154 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4155
4156 static void
4157 s_arm_unwind_save_vfp (void)
4158 {
4159   int count;
4160   unsigned int reg;
4161   valueT op;
4162
4163   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4164   if (count == FAIL)
4165     {
4166       as_bad (_("expected register list"));
4167       ignore_rest_of_line ();
4168       return;
4169     }
4170
4171   demand_empty_rest_of_line ();
4172
4173   if (reg == 8)
4174     {
4175       /* Short form.  */
4176       op = 0xb8 | (count - 1);
4177       add_unwind_opcode (op, 1);
4178     }
4179   else
4180     {
4181       /* Long form.  */
4182       op = 0xb300 | (reg << 4) | (count - 1);
4183       add_unwind_opcode (op, 2);
4184     }
4185   unwind.frame_size += count * 8 + 4;
4186 }
4187
4188
4189 /* Parse a directive saving iWMMXt data registers.  */
4190
4191 static void
4192 s_arm_unwind_save_mmxwr (void)
4193 {
4194   int reg;
4195   int hi_reg;
4196   int i;
4197   unsigned mask = 0;
4198   valueT op;
4199
4200   if (*input_line_pointer == '{')
4201     input_line_pointer++;
4202
4203   do
4204     {
4205       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4206
4207       if (reg == FAIL)
4208         {
4209           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4210           goto error;
4211         }
4212
4213       if (mask >> reg)
4214         as_tsktsk (_("register list not in ascending order"));
4215       mask |= 1 << reg;
4216
4217       if (*input_line_pointer == '-')
4218         {
4219           input_line_pointer++;
4220           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4221           if (hi_reg == FAIL)
4222             {
4223               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4224               goto error;
4225             }
4226           else if (reg >= hi_reg)
4227             {
4228               as_bad (_("bad register range"));
4229               goto error;
4230             }
4231           for (; reg < hi_reg; reg++)
4232             mask |= 1 << reg;
4233         }
4234     }
4235   while (skip_past_comma (&input_line_pointer) != FAIL);
4236
4237   skip_past_char (&input_line_pointer, '}');
4238
4239   demand_empty_rest_of_line ();
4240
4241   /* Generate any deferred opcodes because we're going to be looking at
4242      the list.  */
4243   flush_pending_unwind ();
4244
4245   for (i = 0; i < 16; i++)
4246     {
4247       if (mask & (1 << i))
4248         unwind.frame_size += 8;
4249     }
4250
4251   /* Attempt to combine with a previous opcode.  We do this because gcc
4252      likes to output separate unwind directives for a single block of
4253      registers.  */
4254   if (unwind.opcode_count > 0)
4255     {
4256       i = unwind.opcodes[unwind.opcode_count - 1];
4257       if ((i & 0xf8) == 0xc0)
4258         {
4259           i &= 7;
4260           /* Only merge if the blocks are contiguous.  */
4261           if (i < 6)
4262             {
4263               if ((mask & 0xfe00) == (1 << 9))
4264                 {
4265                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4266                   unwind.opcode_count--;
4267                 }
4268             }
4269           else if (i == 6 && unwind.opcode_count >= 2)
4270             {
4271               i = unwind.opcodes[unwind.opcode_count - 2];
4272               reg = i >> 4;
4273               i &= 0xf;
4274
4275               op = 0xffff << (reg - 1);
4276               if (reg > 0
4277                   && ((mask & op) == (1u << (reg - 1))))
4278                 {
4279                   op = (1 << (reg + i + 1)) - 1;
4280                   op &= ~((1 << reg) - 1);
4281                   mask |= op;
4282                   unwind.opcode_count -= 2;
4283                 }
4284             }
4285         }
4286     }
4287
4288   hi_reg = 15;
4289   /* We want to generate opcodes in the order the registers have been
4290      saved, ie. descending order.  */
4291   for (reg = 15; reg >= -1; reg--)
4292     {
4293       /* Save registers in blocks.  */
4294       if (reg < 0
4295           || !(mask & (1 << reg)))
4296         {
4297           /* We found an unsaved reg.  Generate opcodes to save the
4298              preceding block.   */
4299           if (reg != hi_reg)
4300             {
4301               if (reg == 9)
4302                 {
4303                   /* Short form.  */
4304                   op = 0xc0 | (hi_reg - 10);
4305                   add_unwind_opcode (op, 1);
4306                 }
4307               else
4308                 {
4309                   /* Long form.  */
4310                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4311                   add_unwind_opcode (op, 2);
4312                 }
4313             }
4314           hi_reg = reg - 1;
4315         }
4316     }
4317
4318   return;
4319 error:
4320   ignore_rest_of_line ();
4321 }
4322
4323 static void
4324 s_arm_unwind_save_mmxwcg (void)
4325 {
4326   int reg;
4327   int hi_reg;
4328   unsigned mask = 0;
4329   valueT op;
4330
4331   if (*input_line_pointer == '{')
4332     input_line_pointer++;
4333
4334   skip_whitespace (input_line_pointer);
4335
4336   do
4337     {
4338       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4339
4340       if (reg == FAIL)
4341         {
4342           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4343           goto error;
4344         }
4345
4346       reg -= 8;
4347       if (mask >> reg)
4348         as_tsktsk (_("register list not in ascending order"));
4349       mask |= 1 << reg;
4350
4351       if (*input_line_pointer == '-')
4352         {
4353           input_line_pointer++;
4354           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4355           if (hi_reg == FAIL)
4356             {
4357               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4358               goto error;
4359             }
4360           else if (reg >= hi_reg)
4361             {
4362               as_bad (_("bad register range"));
4363               goto error;
4364             }
4365           for (; reg < hi_reg; reg++)
4366             mask |= 1 << reg;
4367         }
4368     }
4369   while (skip_past_comma (&input_line_pointer) != FAIL);
4370
4371   skip_past_char (&input_line_pointer, '}');
4372
4373   demand_empty_rest_of_line ();
4374
4375   /* Generate any deferred opcodes because we're going to be looking at
4376      the list.  */
4377   flush_pending_unwind ();
4378
4379   for (reg = 0; reg < 16; reg++)
4380     {
4381       if (mask & (1 << reg))
4382         unwind.frame_size += 4;
4383     }
4384   op = 0xc700 | mask;
4385   add_unwind_opcode (op, 2);
4386   return;
4387 error:
4388   ignore_rest_of_line ();
4389 }
4390
4391
4392 /* Parse an unwind_save directive.
4393    If the argument is non-zero, this is a .vsave directive.  */
4394
4395 static void
4396 s_arm_unwind_save (int arch_v6)
4397 {
4398   char *peek;
4399   struct reg_entry *reg;
4400   bfd_boolean had_brace = FALSE;
4401
4402   if (!unwind.proc_start)
4403     as_bad (MISSING_FNSTART);
4404
4405   /* Figure out what sort of save we have.  */
4406   peek = input_line_pointer;
4407
4408   if (*peek == '{')
4409     {
4410       had_brace = TRUE;
4411       peek++;
4412     }
4413
4414   reg = arm_reg_parse_multi (&peek);
4415
4416   if (!reg)
4417     {
4418       as_bad (_("register expected"));
4419       ignore_rest_of_line ();
4420       return;
4421     }
4422
4423   switch (reg->type)
4424     {
4425     case REG_TYPE_FN:
4426       if (had_brace)
4427         {
4428           as_bad (_("FPA .unwind_save does not take a register list"));
4429           ignore_rest_of_line ();
4430           return;
4431         }
4432       input_line_pointer = peek;
4433       s_arm_unwind_save_fpa (reg->number);
4434       return;
4435
4436     case REG_TYPE_RN:
4437       s_arm_unwind_save_core ();
4438       return;
4439
4440     case REG_TYPE_VFD:
4441       if (arch_v6)
4442         s_arm_unwind_save_vfp_armv6 ();
4443       else
4444         s_arm_unwind_save_vfp ();
4445       return;
4446
4447     case REG_TYPE_MMXWR:
4448       s_arm_unwind_save_mmxwr ();
4449       return;
4450
4451     case REG_TYPE_MMXWCG:
4452       s_arm_unwind_save_mmxwcg ();
4453       return;
4454
4455     default:
4456       as_bad (_(".unwind_save does not support this kind of register"));
4457       ignore_rest_of_line ();
4458     }
4459 }
4460
4461
4462 /* Parse an unwind_movsp directive.  */
4463
4464 static void
4465 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4466 {
4467   int reg;
4468   valueT op;
4469   int offset;
4470
4471   if (!unwind.proc_start)
4472     as_bad (MISSING_FNSTART);
4473
4474   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4475   if (reg == FAIL)
4476     {
4477       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4478       ignore_rest_of_line ();
4479       return;
4480     }
4481
4482   /* Optional constant.  */
4483   if (skip_past_comma (&input_line_pointer) != FAIL)
4484     {
4485       if (immediate_for_directive (&offset) == FAIL)
4486         return;
4487     }
4488   else
4489     offset = 0;
4490
4491   demand_empty_rest_of_line ();
4492
4493   if (reg == REG_SP || reg == REG_PC)
4494     {
4495       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4496       return;
4497     }
4498
4499   if (unwind.fp_reg != REG_SP)
4500     as_bad (_("unexpected .unwind_movsp directive"));
4501
4502   /* Generate opcode to restore the value.  */
4503   op = 0x90 | reg;
4504   add_unwind_opcode (op, 1);
4505
4506   /* Record the information for later.  */
4507   unwind.fp_reg = reg;
4508   unwind.fp_offset = unwind.frame_size - offset;
4509   unwind.sp_restored = 1;
4510 }
4511
4512 /* Parse an unwind_pad directive.  */
4513
4514 static void
4515 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4516 {
4517   int offset;
4518
4519   if (!unwind.proc_start)
4520     as_bad (MISSING_FNSTART);
4521
4522   if (immediate_for_directive (&offset) == FAIL)
4523     return;
4524
4525   if (offset & 3)
4526     {
4527       as_bad (_("stack increment must be multiple of 4"));
4528       ignore_rest_of_line ();
4529       return;
4530     }
4531
4532   /* Don't generate any opcodes, just record the details for later.  */
4533   unwind.frame_size += offset;
4534   unwind.pending_offset += offset;
4535
4536   demand_empty_rest_of_line ();
4537 }
4538
4539 /* Parse an unwind_setfp directive.  */
4540
4541 static void
4542 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4543 {
4544   int sp_reg;
4545   int fp_reg;
4546   int offset;
4547
4548   if (!unwind.proc_start)
4549     as_bad (MISSING_FNSTART);
4550
4551   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4552   if (skip_past_comma (&input_line_pointer) == FAIL)
4553     sp_reg = FAIL;
4554   else
4555     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4556
4557   if (fp_reg == FAIL || sp_reg == FAIL)
4558     {
4559       as_bad (_("expected <reg>, <reg>"));
4560       ignore_rest_of_line ();
4561       return;
4562     }
4563
4564   /* Optional constant.  */
4565   if (skip_past_comma (&input_line_pointer) != FAIL)
4566     {
4567       if (immediate_for_directive (&offset) == FAIL)
4568         return;
4569     }
4570   else
4571     offset = 0;
4572
4573   demand_empty_rest_of_line ();
4574
4575   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4576     {
4577       as_bad (_("register must be either sp or set by a previous"
4578                 "unwind_movsp directive"));
4579       return;
4580     }
4581
4582   /* Don't generate any opcodes, just record the information for later.  */
4583   unwind.fp_reg = fp_reg;
4584   unwind.fp_used = 1;
4585   if (sp_reg == REG_SP)
4586     unwind.fp_offset = unwind.frame_size - offset;
4587   else
4588     unwind.fp_offset -= offset;
4589 }
4590
4591 /* Parse an unwind_raw directive.  */
4592
4593 static void
4594 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4595 {
4596   expressionS exp;
4597   /* This is an arbitrary limit.         */
4598   unsigned char op[16];
4599   int count;
4600
4601   if (!unwind.proc_start)
4602     as_bad (MISSING_FNSTART);
4603
4604   expression (&exp);
4605   if (exp.X_op == O_constant
4606       && skip_past_comma (&input_line_pointer) != FAIL)
4607     {
4608       unwind.frame_size += exp.X_add_number;
4609       expression (&exp);
4610     }
4611   else
4612     exp.X_op = O_illegal;
4613
4614   if (exp.X_op != O_constant)
4615     {
4616       as_bad (_("expected <offset>, <opcode>"));
4617       ignore_rest_of_line ();
4618       return;
4619     }
4620
4621   count = 0;
4622
4623   /* Parse the opcode.  */
4624   for (;;)
4625     {
4626       if (count >= 16)
4627         {
4628           as_bad (_("unwind opcode too long"));
4629           ignore_rest_of_line ();
4630         }
4631       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4632         {
4633           as_bad (_("invalid unwind opcode"));
4634           ignore_rest_of_line ();
4635           return;
4636         }
4637       op[count++] = exp.X_add_number;
4638
4639       /* Parse the next byte.  */
4640       if (skip_past_comma (&input_line_pointer) == FAIL)
4641         break;
4642
4643       expression (&exp);
4644     }
4645
4646   /* Add the opcode bytes in reverse order.  */
4647   while (count--)
4648     add_unwind_opcode (op[count], 1);
4649
4650   demand_empty_rest_of_line ();
4651 }
4652
4653
4654 /* Parse a .eabi_attribute directive.  */
4655
4656 static void
4657 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4658 {
4659   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4660
4661   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4662     attributes_set_explicitly[tag] = 1;
4663 }
4664
4665 /* Emit a tls fix for the symbol.  */
4666
4667 static void
4668 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4669 {
4670   char *p;
4671   expressionS exp;
4672 #ifdef md_flush_pending_output
4673   md_flush_pending_output ();
4674 #endif
4675
4676 #ifdef md_cons_align
4677   md_cons_align (4);
4678 #endif
4679
4680   /* Since we're just labelling the code, there's no need to define a
4681      mapping symbol.  */
4682   expression (&exp);
4683   p = obstack_next_free (&frchain_now->frch_obstack);
4684   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4685                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4686                : BFD_RELOC_ARM_TLS_DESCSEQ);
4687 }
4688 #endif /* OBJ_ELF */
4689
4690 static void s_arm_arch (int);
4691 static void s_arm_object_arch (int);
4692 static void s_arm_cpu (int);
4693 static void s_arm_fpu (int);
4694 static void s_arm_arch_extension (int);
4695
4696 #ifdef TE_PE
4697
4698 static void
4699 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4700 {
4701   expressionS exp;
4702
4703   do
4704     {
4705       expression (&exp);
4706       if (exp.X_op == O_symbol)
4707         exp.X_op = O_secrel;
4708
4709       emit_expr (&exp, 4);
4710     }
4711   while (*input_line_pointer++ == ',');
4712
4713   input_line_pointer--;
4714   demand_empty_rest_of_line ();
4715 }
4716 #endif /* TE_PE */
4717
4718 /* This table describes all the machine specific pseudo-ops the assembler
4719    has to support.  The fields are:
4720      pseudo-op name without dot
4721      function to call to execute this pseudo-op
4722      Integer arg to pass to the function.  */
4723
4724 const pseudo_typeS md_pseudo_table[] =
4725 {
4726   /* Never called because '.req' does not start a line.  */
4727   { "req",         s_req,         0 },
4728   /* Following two are likewise never called.  */
4729   { "dn",          s_dn,          0 },
4730   { "qn",          s_qn,          0 },
4731   { "unreq",       s_unreq,       0 },
4732   { "bss",         s_bss,         0 },
4733   { "align",       s_align_ptwo,  2 },
4734   { "arm",         s_arm,         0 },
4735   { "thumb",       s_thumb,       0 },
4736   { "code",        s_code,        0 },
4737   { "force_thumb", s_force_thumb, 0 },
4738   { "thumb_func",  s_thumb_func,  0 },
4739   { "thumb_set",   s_thumb_set,   0 },
4740   { "even",        s_even,        0 },
4741   { "ltorg",       s_ltorg,       0 },
4742   { "pool",        s_ltorg,       0 },
4743   { "syntax",      s_syntax,      0 },
4744   { "cpu",         s_arm_cpu,     0 },
4745   { "arch",        s_arm_arch,    0 },
4746   { "object_arch", s_arm_object_arch,   0 },
4747   { "fpu",         s_arm_fpu,     0 },
4748   { "arch_extension", s_arm_arch_extension, 0 },
4749 #ifdef OBJ_ELF
4750   { "word",             s_arm_elf_cons, 4 },
4751   { "long",             s_arm_elf_cons, 4 },
4752   { "inst.n",           s_arm_elf_inst, 2 },
4753   { "inst.w",           s_arm_elf_inst, 4 },
4754   { "inst",             s_arm_elf_inst, 0 },
4755   { "rel31",            s_arm_rel31,      0 },
4756   { "fnstart",          s_arm_unwind_fnstart,   0 },
4757   { "fnend",            s_arm_unwind_fnend,     0 },
4758   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4759   { "personality",      s_arm_unwind_personality, 0 },
4760   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4761   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4762   { "save",             s_arm_unwind_save,      0 },
4763   { "vsave",            s_arm_unwind_save,      1 },
4764   { "movsp",            s_arm_unwind_movsp,     0 },
4765   { "pad",              s_arm_unwind_pad,       0 },
4766   { "setfp",            s_arm_unwind_setfp,     0 },
4767   { "unwind_raw",       s_arm_unwind_raw,       0 },
4768   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4769   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4770 #else
4771   { "word",        cons, 4},
4772
4773   /* These are used for dwarf.  */
4774   {"2byte", cons, 2},
4775   {"4byte", cons, 4},
4776   {"8byte", cons, 8},
4777   /* These are used for dwarf2.  */
4778   { "file", dwarf2_directive_file, 0 },
4779   { "loc",  dwarf2_directive_loc,  0 },
4780   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4781 #endif
4782   { "extend",      float_cons, 'x' },
4783   { "ldouble",     float_cons, 'x' },
4784   { "packed",      float_cons, 'p' },
4785 #ifdef TE_PE
4786   {"secrel32", pe_directive_secrel, 0},
4787 #endif
4788
4789   /* These are for compatibility with CodeComposer Studio.  */
4790   {"ref",          s_ccs_ref,        0},
4791   {"def",          s_ccs_def,        0},
4792   {"asmfunc",      s_ccs_asmfunc,    0},
4793   {"endasmfunc",   s_ccs_endasmfunc, 0},
4794
4795   { 0, 0, 0 }
4796 };
4797 \f
4798 /* Parser functions used exclusively in instruction operands.  */
4799
4800 /* Generic immediate-value read function for use in insn parsing.
4801    STR points to the beginning of the immediate (the leading #);
4802    VAL receives the value; if the value is outside [MIN, MAX]
4803    issue an error.  PREFIX_OPT is true if the immediate prefix is
4804    optional.  */
4805
4806 static int
4807 parse_immediate (char **str, int *val, int min, int max,
4808                  bfd_boolean prefix_opt)
4809 {
4810   expressionS exp;
4811
4812   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4813   if (exp.X_op != O_constant)
4814     {
4815       inst.error = _("constant expression required");
4816       return FAIL;
4817     }
4818
4819   if (exp.X_add_number < min || exp.X_add_number > max)
4820     {
4821       inst.error = _("immediate value out of range");
4822       return FAIL;
4823     }
4824
4825   *val = exp.X_add_number;
4826   return SUCCESS;
4827 }
4828
4829 /* Less-generic immediate-value read function with the possibility of loading a
4830    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4831    instructions. Puts the result directly in inst.operands[i].  */
4832
4833 static int
4834 parse_big_immediate (char **str, int i, expressionS *in_exp,
4835                      bfd_boolean allow_symbol_p)
4836 {
4837   expressionS exp;
4838   expressionS *exp_p = in_exp ? in_exp : &exp;
4839   char *ptr = *str;
4840
4841   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4842
4843   if (exp_p->X_op == O_constant)
4844     {
4845       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4846       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4847          O_constant.  We have to be careful not to break compilation for
4848          32-bit X_add_number, though.  */
4849       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4850         {
4851           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4852           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4853                                   & 0xffffffff);
4854           inst.operands[i].regisimm = 1;
4855         }
4856     }
4857   else if (exp_p->X_op == O_big
4858            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4859     {
4860       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4861
4862       /* Bignums have their least significant bits in
4863          generic_bignum[0]. Make sure we put 32 bits in imm and
4864          32 bits in reg,  in a (hopefully) portable way.  */
4865       gas_assert (parts != 0);
4866
4867       /* Make sure that the number is not too big.
4868          PR 11972: Bignums can now be sign-extended to the
4869          size of a .octa so check that the out of range bits
4870          are all zero or all one.  */
4871       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4872         {
4873           LITTLENUM_TYPE m = -1;
4874
4875           if (generic_bignum[parts * 2] != 0
4876               && generic_bignum[parts * 2] != m)
4877             return FAIL;
4878
4879           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4880             if (generic_bignum[j] != generic_bignum[j-1])
4881               return FAIL;
4882         }
4883
4884       inst.operands[i].imm = 0;
4885       for (j = 0; j < parts; j++, idx++)
4886         inst.operands[i].imm |= generic_bignum[idx]
4887                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4888       inst.operands[i].reg = 0;
4889       for (j = 0; j < parts; j++, idx++)
4890         inst.operands[i].reg |= generic_bignum[idx]
4891                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4892       inst.operands[i].regisimm = 1;
4893     }
4894   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4895     return FAIL;
4896
4897   *str = ptr;
4898
4899   return SUCCESS;
4900 }
4901
4902 /* Returns the pseudo-register number of an FPA immediate constant,
4903    or FAIL if there isn't a valid constant here.  */
4904
4905 static int
4906 parse_fpa_immediate (char ** str)
4907 {
4908   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4909   char *         save_in;
4910   expressionS    exp;
4911   int            i;
4912   int            j;
4913
4914   /* First try and match exact strings, this is to guarantee
4915      that some formats will work even for cross assembly.  */
4916
4917   for (i = 0; fp_const[i]; i++)
4918     {
4919       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4920         {
4921           char *start = *str;
4922
4923           *str += strlen (fp_const[i]);
4924           if (is_end_of_line[(unsigned char) **str])
4925             return i + 8;
4926           *str = start;
4927         }
4928     }
4929
4930   /* Just because we didn't get a match doesn't mean that the constant
4931      isn't valid, just that it is in a format that we don't
4932      automatically recognize.  Try parsing it with the standard
4933      expression routines.  */
4934
4935   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4936
4937   /* Look for a raw floating point number.  */
4938   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4939       && is_end_of_line[(unsigned char) *save_in])
4940     {
4941       for (i = 0; i < NUM_FLOAT_VALS; i++)
4942         {
4943           for (j = 0; j < MAX_LITTLENUMS; j++)
4944             {
4945               if (words[j] != fp_values[i][j])
4946                 break;
4947             }
4948
4949           if (j == MAX_LITTLENUMS)
4950             {
4951               *str = save_in;
4952               return i + 8;
4953             }
4954         }
4955     }
4956
4957   /* Try and parse a more complex expression, this will probably fail
4958      unless the code uses a floating point prefix (eg "0f").  */
4959   save_in = input_line_pointer;
4960   input_line_pointer = *str;
4961   if (expression (&exp) == absolute_section
4962       && exp.X_op == O_big
4963       && exp.X_add_number < 0)
4964     {
4965       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4966          Ditto for 15.  */
4967 #define X_PRECISION 5
4968 #define E_PRECISION 15L
4969       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
4970         {
4971           for (i = 0; i < NUM_FLOAT_VALS; i++)
4972             {
4973               for (j = 0; j < MAX_LITTLENUMS; j++)
4974                 {
4975                   if (words[j] != fp_values[i][j])
4976                     break;
4977                 }
4978
4979               if (j == MAX_LITTLENUMS)
4980                 {
4981                   *str = input_line_pointer;
4982                   input_line_pointer = save_in;
4983                   return i + 8;
4984                 }
4985             }
4986         }
4987     }
4988
4989   *str = input_line_pointer;
4990   input_line_pointer = save_in;
4991   inst.error = _("invalid FPA immediate expression");
4992   return FAIL;
4993 }
4994
4995 /* Returns 1 if a number has "quarter-precision" float format
4996    0baBbbbbbc defgh000 00000000 00000000.  */
4997
4998 static int
4999 is_quarter_float (unsigned imm)
5000 {
5001   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
5002   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
5003 }
5004
5005
5006 /* Detect the presence of a floating point or integer zero constant,
5007    i.e. #0.0 or #0.  */
5008
5009 static bfd_boolean
5010 parse_ifimm_zero (char **in)
5011 {
5012   int error_code;
5013
5014   if (!is_immediate_prefix (**in))
5015     {
5016       /* In unified syntax, all prefixes are optional.  */
5017       if (!unified_syntax)
5018         return FALSE;
5019     }
5020   else
5021     ++*in;
5022
5023   /* Accept #0x0 as a synonym for #0.  */
5024   if (strncmp (*in, "0x", 2) == 0)
5025     {
5026       int val;
5027       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5028         return FALSE;
5029       return TRUE;
5030     }
5031
5032   error_code = atof_generic (in, ".", EXP_CHARS,
5033                              &generic_floating_point_number);
5034
5035   if (!error_code
5036       && generic_floating_point_number.sign == '+'
5037       && (generic_floating_point_number.low
5038           > generic_floating_point_number.leader))
5039     return TRUE;
5040
5041   return FALSE;
5042 }
5043
5044 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5045    0baBbbbbbc defgh000 00000000 00000000.
5046    The zero and minus-zero cases need special handling, since they can't be
5047    encoded in the "quarter-precision" float format, but can nonetheless be
5048    loaded as integer constants.  */
5049
5050 static unsigned
5051 parse_qfloat_immediate (char **ccp, int *immed)
5052 {
5053   char *str = *ccp;
5054   char *fpnum;
5055   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5056   int found_fpchar = 0;
5057
5058   skip_past_char (&str, '#');
5059
5060   /* We must not accidentally parse an integer as a floating-point number. Make
5061      sure that the value we parse is not an integer by checking for special
5062      characters '.' or 'e'.
5063      FIXME: This is a horrible hack, but doing better is tricky because type
5064      information isn't in a very usable state at parse time.  */
5065   fpnum = str;
5066   skip_whitespace (fpnum);
5067
5068   if (strncmp (fpnum, "0x", 2) == 0)
5069     return FAIL;
5070   else
5071     {
5072       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5073         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5074           {
5075             found_fpchar = 1;
5076             break;
5077           }
5078
5079       if (!found_fpchar)
5080         return FAIL;
5081     }
5082
5083   if ((str = atof_ieee (str, 's', words)) != NULL)
5084     {
5085       unsigned fpword = 0;
5086       int i;
5087
5088       /* Our FP word must be 32 bits (single-precision FP).  */
5089       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5090         {
5091           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5092           fpword |= words[i];
5093         }
5094
5095       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5096         *immed = fpword;
5097       else
5098         return FAIL;
5099
5100       *ccp = str;
5101
5102       return SUCCESS;
5103     }
5104
5105   return FAIL;
5106 }
5107
5108 /* Shift operands.  */
5109 enum shift_kind
5110 {
5111   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5112 };
5113
5114 struct asm_shift_name
5115 {
5116   const char      *name;
5117   enum shift_kind  kind;
5118 };
5119
5120 /* Third argument to parse_shift.  */
5121 enum parse_shift_mode
5122 {
5123   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5124   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5125   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5126   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5127   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5128 };
5129
5130 /* Parse a <shift> specifier on an ARM data processing instruction.
5131    This has three forms:
5132
5133      (LSL|LSR|ASL|ASR|ROR) Rs
5134      (LSL|LSR|ASL|ASR|ROR) #imm
5135      RRX
5136
5137    Note that ASL is assimilated to LSL in the instruction encoding, and
5138    RRX to ROR #0 (which cannot be written as such).  */
5139
5140 static int
5141 parse_shift (char **str, int i, enum parse_shift_mode mode)
5142 {
5143   const struct asm_shift_name *shift_name;
5144   enum shift_kind shift;
5145   char *s = *str;
5146   char *p = s;
5147   int reg;
5148
5149   for (p = *str; ISALPHA (*p); p++)
5150     ;
5151
5152   if (p == *str)
5153     {
5154       inst.error = _("shift expression expected");
5155       return FAIL;
5156     }
5157
5158   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5159                                                             p - *str);
5160
5161   if (shift_name == NULL)
5162     {
5163       inst.error = _("shift expression expected");
5164       return FAIL;
5165     }
5166
5167   shift = shift_name->kind;
5168
5169   switch (mode)
5170     {
5171     case NO_SHIFT_RESTRICT:
5172     case SHIFT_IMMEDIATE:   break;
5173
5174     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5175       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5176         {
5177           inst.error = _("'LSL' or 'ASR' required");
5178           return FAIL;
5179         }
5180       break;
5181
5182     case SHIFT_LSL_IMMEDIATE:
5183       if (shift != SHIFT_LSL)
5184         {
5185           inst.error = _("'LSL' required");
5186           return FAIL;
5187         }
5188       break;
5189
5190     case SHIFT_ASR_IMMEDIATE:
5191       if (shift != SHIFT_ASR)
5192         {
5193           inst.error = _("'ASR' required");
5194           return FAIL;
5195         }
5196       break;
5197
5198     default: abort ();
5199     }
5200
5201   if (shift != SHIFT_RRX)
5202     {
5203       /* Whitespace can appear here if the next thing is a bare digit.  */
5204       skip_whitespace (p);
5205
5206       if (mode == NO_SHIFT_RESTRICT
5207           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5208         {
5209           inst.operands[i].imm = reg;
5210           inst.operands[i].immisreg = 1;
5211         }
5212       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5213         return FAIL;
5214     }
5215   inst.operands[i].shift_kind = shift;
5216   inst.operands[i].shifted = 1;
5217   *str = p;
5218   return SUCCESS;
5219 }
5220
5221 /* Parse a <shifter_operand> for an ARM data processing instruction:
5222
5223       #<immediate>
5224       #<immediate>, <rotate>
5225       <Rm>
5226       <Rm>, <shift>
5227
5228    where <shift> is defined by parse_shift above, and <rotate> is a
5229    multiple of 2 between 0 and 30.  Validation of immediate operands
5230    is deferred to md_apply_fix.  */
5231
5232 static int
5233 parse_shifter_operand (char **str, int i)
5234 {
5235   int value;
5236   expressionS exp;
5237
5238   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5239     {
5240       inst.operands[i].reg = value;
5241       inst.operands[i].isreg = 1;
5242
5243       /* parse_shift will override this if appropriate */
5244       inst.reloc.exp.X_op = O_constant;
5245       inst.reloc.exp.X_add_number = 0;
5246
5247       if (skip_past_comma (str) == FAIL)
5248         return SUCCESS;
5249
5250       /* Shift operation on register.  */
5251       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5252     }
5253
5254   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5255     return FAIL;
5256
5257   if (skip_past_comma (str) == SUCCESS)
5258     {
5259       /* #x, y -- ie explicit rotation by Y.  */
5260       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5261         return FAIL;
5262
5263       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5264         {
5265           inst.error = _("constant expression expected");
5266           return FAIL;
5267         }
5268
5269       value = exp.X_add_number;
5270       if (value < 0 || value > 30 || value % 2 != 0)
5271         {
5272           inst.error = _("invalid rotation");
5273           return FAIL;
5274         }
5275       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5276         {
5277           inst.error = _("invalid constant");
5278           return FAIL;
5279         }
5280
5281       /* Encode as specified.  */
5282       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5283       return SUCCESS;
5284     }
5285
5286   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5287   inst.reloc.pc_rel = 0;
5288   return SUCCESS;
5289 }
5290
5291 /* Group relocation information.  Each entry in the table contains the
5292    textual name of the relocation as may appear in assembler source
5293    and must end with a colon.
5294    Along with this textual name are the relocation codes to be used if
5295    the corresponding instruction is an ALU instruction (ADD or SUB only),
5296    an LDR, an LDRS, or an LDC.  */
5297
5298 struct group_reloc_table_entry
5299 {
5300   const char *name;
5301   int alu_code;
5302   int ldr_code;
5303   int ldrs_code;
5304   int ldc_code;
5305 };
5306
5307 typedef enum
5308 {
5309   /* Varieties of non-ALU group relocation.  */
5310
5311   GROUP_LDR,
5312   GROUP_LDRS,
5313   GROUP_LDC
5314 } group_reloc_type;
5315
5316 static struct group_reloc_table_entry group_reloc_table[] =
5317   { /* Program counter relative: */
5318     { "pc_g0_nc",
5319       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5320       0,                                /* LDR */
5321       0,                                /* LDRS */
5322       0 },                              /* LDC */
5323     { "pc_g0",
5324       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5325       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5326       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5327       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5328     { "pc_g1_nc",
5329       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5330       0,                                /* LDR */
5331       0,                                /* LDRS */
5332       0 },                              /* LDC */
5333     { "pc_g1",
5334       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5335       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5336       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5337       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5338     { "pc_g2",
5339       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5340       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5341       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5342       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5343     /* Section base relative */
5344     { "sb_g0_nc",
5345       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5346       0,                                /* LDR */
5347       0,                                /* LDRS */
5348       0 },                              /* LDC */
5349     { "sb_g0",
5350       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5351       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5352       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5353       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5354     { "sb_g1_nc",
5355       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5356       0,                                /* LDR */
5357       0,                                /* LDRS */
5358       0 },                              /* LDC */
5359     { "sb_g1",
5360       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5361       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5362       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5363       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5364     { "sb_g2",
5365       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5366       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5367       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5368       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5369     /* Absolute thumb alu relocations.  */
5370     { "lower0_7",
5371       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5372       0,                                /* LDR.  */
5373       0,                                /* LDRS.  */
5374       0 },                              /* LDC.  */
5375     { "lower8_15",
5376       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5377       0,                                /* LDR.  */
5378       0,                                /* LDRS.  */
5379       0 },                              /* LDC.  */
5380     { "upper0_7",
5381       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5382       0,                                /* LDR.  */
5383       0,                                /* LDRS.  */
5384       0 },                              /* LDC.  */
5385     { "upper8_15",
5386       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5387       0,                                /* LDR.  */
5388       0,                                /* LDRS.  */
5389       0 } };                            /* LDC.  */
5390
5391 /* Given the address of a pointer pointing to the textual name of a group
5392    relocation as may appear in assembler source, attempt to find its details
5393    in group_reloc_table.  The pointer will be updated to the character after
5394    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5395    otherwise.  On success, *entry will be updated to point at the relevant
5396    group_reloc_table entry. */
5397
5398 static int
5399 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5400 {
5401   unsigned int i;
5402   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5403     {
5404       int length = strlen (group_reloc_table[i].name);
5405
5406       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5407           && (*str)[length] == ':')
5408         {
5409           *out = &group_reloc_table[i];
5410           *str += (length + 1);
5411           return SUCCESS;
5412         }
5413     }
5414
5415   return FAIL;
5416 }
5417
5418 /* Parse a <shifter_operand> for an ARM data processing instruction
5419    (as for parse_shifter_operand) where group relocations are allowed:
5420
5421       #<immediate>
5422       #<immediate>, <rotate>
5423       #:<group_reloc>:<expression>
5424       <Rm>
5425       <Rm>, <shift>
5426
5427    where <group_reloc> is one of the strings defined in group_reloc_table.
5428    The hashes are optional.
5429
5430    Everything else is as for parse_shifter_operand.  */
5431
5432 static parse_operand_result
5433 parse_shifter_operand_group_reloc (char **str, int i)
5434 {
5435   /* Determine if we have the sequence of characters #: or just :
5436      coming next.  If we do, then we check for a group relocation.
5437      If we don't, punt the whole lot to parse_shifter_operand.  */
5438
5439   if (((*str)[0] == '#' && (*str)[1] == ':')
5440       || (*str)[0] == ':')
5441     {
5442       struct group_reloc_table_entry *entry;
5443
5444       if ((*str)[0] == '#')
5445         (*str) += 2;
5446       else
5447         (*str)++;
5448
5449       /* Try to parse a group relocation.  Anything else is an error.  */
5450       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5451         {
5452           inst.error = _("unknown group relocation");
5453           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5454         }
5455
5456       /* We now have the group relocation table entry corresponding to
5457          the name in the assembler source.  Next, we parse the expression.  */
5458       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5459         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5460
5461       /* Record the relocation type (always the ALU variant here).  */
5462       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5463       gas_assert (inst.reloc.type != 0);
5464
5465       return PARSE_OPERAND_SUCCESS;
5466     }
5467   else
5468     return parse_shifter_operand (str, i) == SUCCESS
5469            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5470
5471   /* Never reached.  */
5472 }
5473
5474 /* Parse a Neon alignment expression.  Information is written to
5475    inst.operands[i].  We assume the initial ':' has been skipped.
5476
5477    align        .imm = align << 8, .immisalign=1, .preind=0  */
5478 static parse_operand_result
5479 parse_neon_alignment (char **str, int i)
5480 {
5481   char *p = *str;
5482   expressionS exp;
5483
5484   my_get_expression (&exp, &p, GE_NO_PREFIX);
5485
5486   if (exp.X_op != O_constant)
5487     {
5488       inst.error = _("alignment must be constant");
5489       return PARSE_OPERAND_FAIL;
5490     }
5491
5492   inst.operands[i].imm = exp.X_add_number << 8;
5493   inst.operands[i].immisalign = 1;
5494   /* Alignments are not pre-indexes.  */
5495   inst.operands[i].preind = 0;
5496
5497   *str = p;
5498   return PARSE_OPERAND_SUCCESS;
5499 }
5500
5501 /* Parse all forms of an ARM address expression.  Information is written
5502    to inst.operands[i] and/or inst.reloc.
5503
5504    Preindexed addressing (.preind=1):
5505
5506    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5507    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5508    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5509                        .shift_kind=shift .reloc.exp=shift_imm
5510
5511    These three may have a trailing ! which causes .writeback to be set also.
5512
5513    Postindexed addressing (.postind=1, .writeback=1):
5514
5515    [Rn], #offset       .reg=Rn .reloc.exp=offset
5516    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5517    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5518                        .shift_kind=shift .reloc.exp=shift_imm
5519
5520    Unindexed addressing (.preind=0, .postind=0):
5521
5522    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5523
5524    Other:
5525
5526    [Rn]{!}             shorthand for [Rn,#0]{!}
5527    =immediate          .isreg=0 .reloc.exp=immediate
5528    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5529
5530   It is the caller's responsibility to check for addressing modes not
5531   supported by the instruction, and to set inst.reloc.type.  */
5532
5533 static parse_operand_result
5534 parse_address_main (char **str, int i, int group_relocations,
5535                     group_reloc_type group_type)
5536 {
5537   char *p = *str;
5538   int reg;
5539
5540   if (skip_past_char (&p, '[') == FAIL)
5541     {
5542       if (skip_past_char (&p, '=') == FAIL)
5543         {
5544           /* Bare address - translate to PC-relative offset.  */
5545           inst.reloc.pc_rel = 1;
5546           inst.operands[i].reg = REG_PC;
5547           inst.operands[i].isreg = 1;
5548           inst.operands[i].preind = 1;
5549
5550           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5551             return PARSE_OPERAND_FAIL;
5552         }
5553       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5554                                     /*allow_symbol_p=*/TRUE))
5555         return PARSE_OPERAND_FAIL;
5556
5557       *str = p;
5558       return PARSE_OPERAND_SUCCESS;
5559     }
5560
5561   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5562   skip_whitespace (p);
5563
5564   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5565     {
5566       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5567       return PARSE_OPERAND_FAIL;
5568     }
5569   inst.operands[i].reg = reg;
5570   inst.operands[i].isreg = 1;
5571
5572   if (skip_past_comma (&p) == SUCCESS)
5573     {
5574       inst.operands[i].preind = 1;
5575
5576       if (*p == '+') p++;
5577       else if (*p == '-') p++, inst.operands[i].negative = 1;
5578
5579       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5580         {
5581           inst.operands[i].imm = reg;
5582           inst.operands[i].immisreg = 1;
5583
5584           if (skip_past_comma (&p) == SUCCESS)
5585             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5586               return PARSE_OPERAND_FAIL;
5587         }
5588       else if (skip_past_char (&p, ':') == SUCCESS)
5589         {
5590           /* FIXME: '@' should be used here, but it's filtered out by generic
5591              code before we get to see it here. This may be subject to
5592              change.  */
5593           parse_operand_result result = parse_neon_alignment (&p, i);
5594
5595           if (result != PARSE_OPERAND_SUCCESS)
5596             return result;
5597         }
5598       else
5599         {
5600           if (inst.operands[i].negative)
5601             {
5602               inst.operands[i].negative = 0;
5603               p--;
5604             }
5605
5606           if (group_relocations
5607               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5608             {
5609               struct group_reloc_table_entry *entry;
5610
5611               /* Skip over the #: or : sequence.  */
5612               if (*p == '#')
5613                 p += 2;
5614               else
5615                 p++;
5616
5617               /* Try to parse a group relocation.  Anything else is an
5618                  error.  */
5619               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5620                 {
5621                   inst.error = _("unknown group relocation");
5622                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5623                 }
5624
5625               /* We now have the group relocation table entry corresponding to
5626                  the name in the assembler source.  Next, we parse the
5627                  expression.  */
5628               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5629                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5630
5631               /* Record the relocation type.  */
5632               switch (group_type)
5633                 {
5634                   case GROUP_LDR:
5635                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5636                     break;
5637
5638                   case GROUP_LDRS:
5639                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5640                     break;
5641
5642                   case GROUP_LDC:
5643                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5644                     break;
5645
5646                   default:
5647                     gas_assert (0);
5648                 }
5649
5650               if (inst.reloc.type == 0)
5651                 {
5652                   inst.error = _("this group relocation is not allowed on this instruction");
5653                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5654                 }
5655             }
5656           else
5657             {
5658               char *q = p;
5659
5660               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5661                 return PARSE_OPERAND_FAIL;
5662               /* If the offset is 0, find out if it's a +0 or -0.  */
5663               if (inst.reloc.exp.X_op == O_constant
5664                   && inst.reloc.exp.X_add_number == 0)
5665                 {
5666                   skip_whitespace (q);
5667                   if (*q == '#')
5668                     {
5669                       q++;
5670                       skip_whitespace (q);
5671                     }
5672                   if (*q == '-')
5673                     inst.operands[i].negative = 1;
5674                 }
5675             }
5676         }
5677     }
5678   else if (skip_past_char (&p, ':') == SUCCESS)
5679     {
5680       /* FIXME: '@' should be used here, but it's filtered out by generic code
5681          before we get to see it here. This may be subject to change.  */
5682       parse_operand_result result = parse_neon_alignment (&p, i);
5683
5684       if (result != PARSE_OPERAND_SUCCESS)
5685         return result;
5686     }
5687
5688   if (skip_past_char (&p, ']') == FAIL)
5689     {
5690       inst.error = _("']' expected");
5691       return PARSE_OPERAND_FAIL;
5692     }
5693
5694   if (skip_past_char (&p, '!') == SUCCESS)
5695     inst.operands[i].writeback = 1;
5696
5697   else if (skip_past_comma (&p) == SUCCESS)
5698     {
5699       if (skip_past_char (&p, '{') == SUCCESS)
5700         {
5701           /* [Rn], {expr} - unindexed, with option */
5702           if (parse_immediate (&p, &inst.operands[i].imm,
5703                                0, 255, TRUE) == FAIL)
5704             return PARSE_OPERAND_FAIL;
5705
5706           if (skip_past_char (&p, '}') == FAIL)
5707             {
5708               inst.error = _("'}' expected at end of 'option' field");
5709               return PARSE_OPERAND_FAIL;
5710             }
5711           if (inst.operands[i].preind)
5712             {
5713               inst.error = _("cannot combine index with option");
5714               return PARSE_OPERAND_FAIL;
5715             }
5716           *str = p;
5717           return PARSE_OPERAND_SUCCESS;
5718         }
5719       else
5720         {
5721           inst.operands[i].postind = 1;
5722           inst.operands[i].writeback = 1;
5723
5724           if (inst.operands[i].preind)
5725             {
5726               inst.error = _("cannot combine pre- and post-indexing");
5727               return PARSE_OPERAND_FAIL;
5728             }
5729
5730           if (*p == '+') p++;
5731           else if (*p == '-') p++, inst.operands[i].negative = 1;
5732
5733           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5734             {
5735               /* We might be using the immediate for alignment already. If we
5736                  are, OR the register number into the low-order bits.  */
5737               if (inst.operands[i].immisalign)
5738                 inst.operands[i].imm |= reg;
5739               else
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
5748             {
5749               char *q = p;
5750
5751               if (inst.operands[i].negative)
5752                 {
5753                   inst.operands[i].negative = 0;
5754                   p--;
5755                 }
5756               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5757                 return PARSE_OPERAND_FAIL;
5758               /* If the offset is 0, find out if it's a +0 or -0.  */
5759               if (inst.reloc.exp.X_op == O_constant
5760                   && inst.reloc.exp.X_add_number == 0)
5761                 {
5762                   skip_whitespace (q);
5763                   if (*q == '#')
5764                     {
5765                       q++;
5766                       skip_whitespace (q);
5767                     }
5768                   if (*q == '-')
5769                     inst.operands[i].negative = 1;
5770                 }
5771             }
5772         }
5773     }
5774
5775   /* If at this point neither .preind nor .postind is set, we have a
5776      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5777   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5778     {
5779       inst.operands[i].preind = 1;
5780       inst.reloc.exp.X_op = O_constant;
5781       inst.reloc.exp.X_add_number = 0;
5782     }
5783   *str = p;
5784   return PARSE_OPERAND_SUCCESS;
5785 }
5786
5787 static int
5788 parse_address (char **str, int i)
5789 {
5790   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5791          ? SUCCESS : FAIL;
5792 }
5793
5794 static parse_operand_result
5795 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5796 {
5797   return parse_address_main (str, i, 1, type);
5798 }
5799
5800 /* Parse an operand for a MOVW or MOVT instruction.  */
5801 static int
5802 parse_half (char **str)
5803 {
5804   char * p;
5805
5806   p = *str;
5807   skip_past_char (&p, '#');
5808   if (strncasecmp (p, ":lower16:", 9) == 0)
5809     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5810   else if (strncasecmp (p, ":upper16:", 9) == 0)
5811     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5812
5813   if (inst.reloc.type != BFD_RELOC_UNUSED)
5814     {
5815       p += 9;
5816       skip_whitespace (p);
5817     }
5818
5819   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5820     return FAIL;
5821
5822   if (inst.reloc.type == BFD_RELOC_UNUSED)
5823     {
5824       if (inst.reloc.exp.X_op != O_constant)
5825         {
5826           inst.error = _("constant expression expected");
5827           return FAIL;
5828         }
5829       if (inst.reloc.exp.X_add_number < 0
5830           || inst.reloc.exp.X_add_number > 0xffff)
5831         {
5832           inst.error = _("immediate value out of range");
5833           return FAIL;
5834         }
5835     }
5836   *str = p;
5837   return SUCCESS;
5838 }
5839
5840 /* Miscellaneous. */
5841
5842 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5843    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5844 static int
5845 parse_psr (char **str, bfd_boolean lhs)
5846 {
5847   char *p;
5848   unsigned long psr_field;
5849   const struct asm_psr *psr;
5850   char *start;
5851   bfd_boolean is_apsr = FALSE;
5852   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5853
5854   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5855      be TRUE, but we want to ignore it in this case as we are building for any
5856      CPU type, including non-m variants.  */
5857   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5858     m_profile = FALSE;
5859
5860   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5861      feature for ease of use and backwards compatibility.  */
5862   p = *str;
5863   if (strncasecmp (p, "SPSR", 4) == 0)
5864     {
5865       if (m_profile)
5866         goto unsupported_psr;
5867
5868       psr_field = SPSR_BIT;
5869     }
5870   else if (strncasecmp (p, "CPSR", 4) == 0)
5871     {
5872       if (m_profile)
5873         goto unsupported_psr;
5874
5875       psr_field = 0;
5876     }
5877   else if (strncasecmp (p, "APSR", 4) == 0)
5878     {
5879       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5880          and ARMv7-R architecture CPUs.  */
5881       is_apsr = TRUE;
5882       psr_field = 0;
5883     }
5884   else if (m_profile)
5885     {
5886       start = p;
5887       do
5888         p++;
5889       while (ISALNUM (*p) || *p == '_');
5890
5891       if (strncasecmp (start, "iapsr", 5) == 0
5892           || strncasecmp (start, "eapsr", 5) == 0
5893           || strncasecmp (start, "xpsr", 4) == 0
5894           || strncasecmp (start, "psr", 3) == 0)
5895         p = start + strcspn (start, "rR") + 1;
5896
5897       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5898                                                   p - start);
5899
5900       if (!psr)
5901         return FAIL;
5902
5903       /* If APSR is being written, a bitfield may be specified.  Note that
5904          APSR itself is handled above.  */
5905       if (psr->field <= 3)
5906         {
5907           psr_field = psr->field;
5908           is_apsr = TRUE;
5909           goto check_suffix;
5910         }
5911
5912       *str = p;
5913       /* M-profile MSR instructions have the mask field set to "10", except
5914          *PSR variants which modify APSR, which may use a different mask (and
5915          have been handled already).  Do that by setting the PSR_f field
5916          here.  */
5917       return psr->field | (lhs ? PSR_f : 0);
5918     }
5919   else
5920     goto unsupported_psr;
5921
5922   p += 4;
5923 check_suffix:
5924   if (*p == '_')
5925     {
5926       /* A suffix follows.  */
5927       p++;
5928       start = p;
5929
5930       do
5931         p++;
5932       while (ISALNUM (*p) || *p == '_');
5933
5934       if (is_apsr)
5935         {
5936           /* APSR uses a notation for bits, rather than fields.  */
5937           unsigned int nzcvq_bits = 0;
5938           unsigned int g_bit = 0;
5939           char *bit;
5940
5941           for (bit = start; bit != p; bit++)
5942             {
5943               switch (TOLOWER (*bit))
5944                 {
5945                 case 'n':
5946                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5947                   break;
5948
5949                 case 'z':
5950                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5951                   break;
5952
5953                 case 'c':
5954                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5955                   break;
5956
5957                 case 'v':
5958                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5959                   break;
5960
5961                 case 'q':
5962                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5963                   break;
5964
5965                 case 'g':
5966                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5967                   break;
5968
5969                 default:
5970                   inst.error = _("unexpected bit specified after APSR");
5971                   return FAIL;
5972                 }
5973             }
5974
5975           if (nzcvq_bits == 0x1f)
5976             psr_field |= PSR_f;
5977
5978           if (g_bit == 0x1)
5979             {
5980               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5981                 {
5982                   inst.error = _("selected processor does not "
5983                                  "support DSP extension");
5984                   return FAIL;
5985                 }
5986
5987               psr_field |= PSR_s;
5988             }
5989
5990           if ((nzcvq_bits & 0x20) != 0
5991               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5992               || (g_bit & 0x2) != 0)
5993             {
5994               inst.error = _("bad bitmask specified after APSR");
5995               return FAIL;
5996             }
5997         }
5998       else
5999         {
6000           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
6001                                                       p - start);
6002           if (!psr)
6003             goto error;
6004
6005           psr_field |= psr->field;
6006         }
6007     }
6008   else
6009     {
6010       if (ISALNUM (*p))
6011         goto error;    /* Garbage after "[CS]PSR".  */
6012
6013       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
6014          is deprecated, but allow it anyway.  */
6015       if (is_apsr && lhs)
6016         {
6017           psr_field |= PSR_f;
6018           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6019                        "deprecated"));
6020         }
6021       else if (!m_profile)
6022         /* These bits are never right for M-profile devices: don't set them
6023            (only code paths which read/write APSR reach here).  */
6024         psr_field |= (PSR_c | PSR_f);
6025     }
6026   *str = p;
6027   return psr_field;
6028
6029  unsupported_psr:
6030   inst.error = _("selected processor does not support requested special "
6031                  "purpose register");
6032   return FAIL;
6033
6034  error:
6035   inst.error = _("flag for {c}psr instruction expected");
6036   return FAIL;
6037 }
6038
6039 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6040    value suitable for splatting into the AIF field of the instruction.  */
6041
6042 static int
6043 parse_cps_flags (char **str)
6044 {
6045   int val = 0;
6046   int saw_a_flag = 0;
6047   char *s = *str;
6048
6049   for (;;)
6050     switch (*s++)
6051       {
6052       case '\0': case ',':
6053         goto done;
6054
6055       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6056       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6057       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6058
6059       default:
6060         inst.error = _("unrecognized CPS flag");
6061         return FAIL;
6062       }
6063
6064  done:
6065   if (saw_a_flag == 0)
6066     {
6067       inst.error = _("missing CPS flags");
6068       return FAIL;
6069     }
6070
6071   *str = s - 1;
6072   return val;
6073 }
6074
6075 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6076    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6077
6078 static int
6079 parse_endian_specifier (char **str)
6080 {
6081   int little_endian;
6082   char *s = *str;
6083
6084   if (strncasecmp (s, "BE", 2))
6085     little_endian = 0;
6086   else if (strncasecmp (s, "LE", 2))
6087     little_endian = 1;
6088   else
6089     {
6090       inst.error = _("valid endian specifiers are be or le");
6091       return FAIL;
6092     }
6093
6094   if (ISALNUM (s[2]) || s[2] == '_')
6095     {
6096       inst.error = _("valid endian specifiers are be or le");
6097       return FAIL;
6098     }
6099
6100   *str = s + 2;
6101   return little_endian;
6102 }
6103
6104 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6105    value suitable for poking into the rotate field of an sxt or sxta
6106    instruction, or FAIL on error.  */
6107
6108 static int
6109 parse_ror (char **str)
6110 {
6111   int rot;
6112   char *s = *str;
6113
6114   if (strncasecmp (s, "ROR", 3) == 0)
6115     s += 3;
6116   else
6117     {
6118       inst.error = _("missing rotation field after comma");
6119       return FAIL;
6120     }
6121
6122   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6123     return FAIL;
6124
6125   switch (rot)
6126     {
6127     case  0: *str = s; return 0x0;
6128     case  8: *str = s; return 0x1;
6129     case 16: *str = s; return 0x2;
6130     case 24: *str = s; return 0x3;
6131
6132     default:
6133       inst.error = _("rotation can only be 0, 8, 16, or 24");
6134       return FAIL;
6135     }
6136 }
6137
6138 /* Parse a conditional code (from conds[] below).  The value returned is in the
6139    range 0 .. 14, or FAIL.  */
6140 static int
6141 parse_cond (char **str)
6142 {
6143   char *q;
6144   const struct asm_cond *c;
6145   int n;
6146   /* Condition codes are always 2 characters, so matching up to
6147      3 characters is sufficient.  */
6148   char cond[3];
6149
6150   q = *str;
6151   n = 0;
6152   while (ISALPHA (*q) && n < 3)
6153     {
6154       cond[n] = TOLOWER (*q);
6155       q++;
6156       n++;
6157     }
6158
6159   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6160   if (!c)
6161     {
6162       inst.error = _("condition required");
6163       return FAIL;
6164     }
6165
6166   *str = q;
6167   return c->value;
6168 }
6169
6170 /* Record a use of the given feature.  */
6171 static void
6172 record_feature_use (const arm_feature_set *feature)
6173 {
6174   if (thumb_mode)
6175     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6176   else
6177     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6178 }
6179
6180 /* If the given feature is currently allowed, mark it as used and return TRUE.
6181    Return FALSE otherwise.  */
6182 static bfd_boolean
6183 mark_feature_used (const arm_feature_set *feature)
6184 {
6185   /* Ensure the option is currently allowed.  */
6186   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6187     return FALSE;
6188
6189   /* Add the appropriate architecture feature for the barrier option used.  */
6190   record_feature_use (feature);
6191
6192   return TRUE;
6193 }
6194
6195 /* Parse an option for a barrier instruction.  Returns the encoding for the
6196    option, or FAIL.  */
6197 static int
6198 parse_barrier (char **str)
6199 {
6200   char *p, *q;
6201   const struct asm_barrier_opt *o;
6202
6203   p = q = *str;
6204   while (ISALPHA (*q))
6205     q++;
6206
6207   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6208                                                     q - p);
6209   if (!o)
6210     return FAIL;
6211
6212   if (!mark_feature_used (&o->arch))
6213     return FAIL;
6214
6215   *str = q;
6216   return o->value;
6217 }
6218
6219 /* Parse the operands of a table branch instruction.  Similar to a memory
6220    operand.  */
6221 static int
6222 parse_tb (char **str)
6223 {
6224   char * p = *str;
6225   int reg;
6226
6227   if (skip_past_char (&p, '[') == FAIL)
6228     {
6229       inst.error = _("'[' expected");
6230       return FAIL;
6231     }
6232
6233   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6234     {
6235       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6236       return FAIL;
6237     }
6238   inst.operands[0].reg = reg;
6239
6240   if (skip_past_comma (&p) == FAIL)
6241     {
6242       inst.error = _("',' expected");
6243       return FAIL;
6244     }
6245
6246   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6247     {
6248       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6249       return FAIL;
6250     }
6251   inst.operands[0].imm = reg;
6252
6253   if (skip_past_comma (&p) == SUCCESS)
6254     {
6255       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6256         return FAIL;
6257       if (inst.reloc.exp.X_add_number != 1)
6258         {
6259           inst.error = _("invalid shift");
6260           return FAIL;
6261         }
6262       inst.operands[0].shifted = 1;
6263     }
6264
6265   if (skip_past_char (&p, ']') == FAIL)
6266     {
6267       inst.error = _("']' expected");
6268       return FAIL;
6269     }
6270   *str = p;
6271   return SUCCESS;
6272 }
6273
6274 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6275    information on the types the operands can take and how they are encoded.
6276    Up to four operands may be read; this function handles setting the
6277    ".present" field for each read operand itself.
6278    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6279    else returns FAIL.  */
6280
6281 static int
6282 parse_neon_mov (char **str, int *which_operand)
6283 {
6284   int i = *which_operand, val;
6285   enum arm_reg_type rtype;
6286   char *ptr = *str;
6287   struct neon_type_el optype;
6288
6289   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6290     {
6291       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6292       inst.operands[i].reg = val;
6293       inst.operands[i].isscalar = 1;
6294       inst.operands[i].vectype = optype;
6295       inst.operands[i++].present = 1;
6296
6297       if (skip_past_comma (&ptr) == FAIL)
6298         goto wanted_comma;
6299
6300       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6301         goto wanted_arm;
6302
6303       inst.operands[i].reg = val;
6304       inst.operands[i].isreg = 1;
6305       inst.operands[i].present = 1;
6306     }
6307   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6308            != FAIL)
6309     {
6310       /* Cases 0, 1, 2, 3, 5 (D only).  */
6311       if (skip_past_comma (&ptr) == FAIL)
6312         goto wanted_comma;
6313
6314       inst.operands[i].reg = val;
6315       inst.operands[i].isreg = 1;
6316       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6317       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6318       inst.operands[i].isvec = 1;
6319       inst.operands[i].vectype = optype;
6320       inst.operands[i++].present = 1;
6321
6322       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6323         {
6324           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6325              Case 13: VMOV <Sd>, <Rm>  */
6326           inst.operands[i].reg = val;
6327           inst.operands[i].isreg = 1;
6328           inst.operands[i].present = 1;
6329
6330           if (rtype == REG_TYPE_NQ)
6331             {
6332               first_error (_("can't use Neon quad register here"));
6333               return FAIL;
6334             }
6335           else if (rtype != REG_TYPE_VFS)
6336             {
6337               i++;
6338               if (skip_past_comma (&ptr) == FAIL)
6339                 goto wanted_comma;
6340               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6341                 goto wanted_arm;
6342               inst.operands[i].reg = val;
6343               inst.operands[i].isreg = 1;
6344               inst.operands[i].present = 1;
6345             }
6346         }
6347       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6348                                            &optype)) != FAIL)
6349         {
6350           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6351              Case 1: VMOV<c><q> <Dd>, <Dm>
6352              Case 8: VMOV.F32 <Sd>, <Sm>
6353              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6354
6355           inst.operands[i].reg = val;
6356           inst.operands[i].isreg = 1;
6357           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6358           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6359           inst.operands[i].isvec = 1;
6360           inst.operands[i].vectype = optype;
6361           inst.operands[i].present = 1;
6362
6363           if (skip_past_comma (&ptr) == SUCCESS)
6364             {
6365               /* Case 15.  */
6366               i++;
6367
6368               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6369                 goto wanted_arm;
6370
6371               inst.operands[i].reg = val;
6372               inst.operands[i].isreg = 1;
6373               inst.operands[i++].present = 1;
6374
6375               if (skip_past_comma (&ptr) == FAIL)
6376                 goto wanted_comma;
6377
6378               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6379                 goto wanted_arm;
6380
6381               inst.operands[i].reg = val;
6382               inst.operands[i].isreg = 1;
6383               inst.operands[i].present = 1;
6384             }
6385         }
6386       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6387           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6388              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6389              Case 10: VMOV.F32 <Sd>, #<imm>
6390              Case 11: VMOV.F64 <Dd>, #<imm>  */
6391         inst.operands[i].immisfloat = 1;
6392       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6393                == SUCCESS)
6394           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6395              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6396         ;
6397       else
6398         {
6399           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6400           return FAIL;
6401         }
6402     }
6403   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6404     {
6405       /* Cases 6, 7.  */
6406       inst.operands[i].reg = val;
6407       inst.operands[i].isreg = 1;
6408       inst.operands[i++].present = 1;
6409
6410       if (skip_past_comma (&ptr) == FAIL)
6411         goto wanted_comma;
6412
6413       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6414         {
6415           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6416           inst.operands[i].reg = val;
6417           inst.operands[i].isscalar = 1;
6418           inst.operands[i].present = 1;
6419           inst.operands[i].vectype = optype;
6420         }
6421       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6422         {
6423           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6424           inst.operands[i].reg = val;
6425           inst.operands[i].isreg = 1;
6426           inst.operands[i++].present = 1;
6427
6428           if (skip_past_comma (&ptr) == FAIL)
6429             goto wanted_comma;
6430
6431           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6432               == FAIL)
6433             {
6434               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6435               return FAIL;
6436             }
6437
6438           inst.operands[i].reg = val;
6439           inst.operands[i].isreg = 1;
6440           inst.operands[i].isvec = 1;
6441           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6442           inst.operands[i].vectype = optype;
6443           inst.operands[i].present = 1;
6444
6445           if (rtype == REG_TYPE_VFS)
6446             {
6447               /* Case 14.  */
6448               i++;
6449               if (skip_past_comma (&ptr) == FAIL)
6450                 goto wanted_comma;
6451               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6452                                               &optype)) == FAIL)
6453                 {
6454                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6455                   return FAIL;
6456                 }
6457               inst.operands[i].reg = val;
6458               inst.operands[i].isreg = 1;
6459               inst.operands[i].isvec = 1;
6460               inst.operands[i].issingle = 1;
6461               inst.operands[i].vectype = optype;
6462               inst.operands[i].present = 1;
6463             }
6464         }
6465       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6466                != FAIL)
6467         {
6468           /* Case 13.  */
6469           inst.operands[i].reg = val;
6470           inst.operands[i].isreg = 1;
6471           inst.operands[i].isvec = 1;
6472           inst.operands[i].issingle = 1;
6473           inst.operands[i].vectype = optype;
6474           inst.operands[i].present = 1;
6475         }
6476     }
6477   else
6478     {
6479       first_error (_("parse error"));
6480       return FAIL;
6481     }
6482
6483   /* Successfully parsed the operands. Update args.  */
6484   *which_operand = i;
6485   *str = ptr;
6486   return SUCCESS;
6487
6488  wanted_comma:
6489   first_error (_("expected comma"));
6490   return FAIL;
6491
6492  wanted_arm:
6493   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6494   return FAIL;
6495 }
6496
6497 /* Use this macro when the operand constraints are different
6498    for ARM and THUMB (e.g. ldrd).  */
6499 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6500         ((arm_operand) | ((thumb_operand) << 16))
6501
6502 /* Matcher codes for parse_operands.  */
6503 enum operand_parse_code
6504 {
6505   OP_stop,      /* end of line */
6506
6507   OP_RR,        /* ARM register */
6508   OP_RRnpc,     /* ARM register, not r15 */
6509   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6510   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6511   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6512                    optional trailing ! */
6513   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6514   OP_RCP,       /* Coprocessor number */
6515   OP_RCN,       /* Coprocessor register */
6516   OP_RF,        /* FPA register */
6517   OP_RVS,       /* VFP single precision register */
6518   OP_RVD,       /* VFP double precision register (0..15) */
6519   OP_RND,       /* Neon double precision register (0..31) */
6520   OP_RNQ,       /* Neon quad precision register */
6521   OP_RVSD,      /* VFP single or double precision register */
6522   OP_RNSD,      /* Neon single or double precision register */
6523   OP_RNDQ,      /* Neon double or quad precision register */
6524   OP_RNSDQ,     /* Neon single, double or quad precision register */
6525   OP_RNSC,      /* Neon scalar D[X] */
6526   OP_RVC,       /* VFP control register */
6527   OP_RMF,       /* Maverick F register */
6528   OP_RMD,       /* Maverick D register */
6529   OP_RMFX,      /* Maverick FX register */
6530   OP_RMDX,      /* Maverick DX register */
6531   OP_RMAX,      /* Maverick AX register */
6532   OP_RMDS,      /* Maverick DSPSC register */
6533   OP_RIWR,      /* iWMMXt wR register */
6534   OP_RIWC,      /* iWMMXt wC register */
6535   OP_RIWG,      /* iWMMXt wCG register */
6536   OP_RXA,       /* XScale accumulator register */
6537
6538   OP_REGLST,    /* ARM register list */
6539   OP_VRSLST,    /* VFP single-precision register list */
6540   OP_VRDLST,    /* VFP double-precision register list */
6541   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6542   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6543   OP_NSTRLST,   /* Neon element/structure list */
6544
6545   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6546   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6547   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6548   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6549   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
6550   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6551   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6552   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6553   OP_VMOV,      /* Neon VMOV operands.  */
6554   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6555   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6556   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6557
6558   OP_I0,        /* immediate zero */
6559   OP_I7,        /* immediate value 0 .. 7 */
6560   OP_I15,       /*                 0 .. 15 */
6561   OP_I16,       /*                 1 .. 16 */
6562   OP_I16z,      /*                 0 .. 16 */
6563   OP_I31,       /*                 0 .. 31 */
6564   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6565   OP_I32,       /*                 1 .. 32 */
6566   OP_I32z,      /*                 0 .. 32 */
6567   OP_I63,       /*                 0 .. 63 */
6568   OP_I63s,      /*               -64 .. 63 */
6569   OP_I64,       /*                 1 .. 64 */
6570   OP_I64z,      /*                 0 .. 64 */
6571   OP_I255,      /*                 0 .. 255 */
6572
6573   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6574   OP_I7b,       /*                             0 .. 7 */
6575   OP_I15b,      /*                             0 .. 15 */
6576   OP_I31b,      /*                             0 .. 31 */
6577
6578   OP_SH,        /* shifter operand */
6579   OP_SHG,       /* shifter operand with possible group relocation */
6580   OP_ADDR,      /* Memory address expression (any mode) */
6581   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6582   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6583   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6584   OP_EXP,       /* arbitrary expression */
6585   OP_EXPi,      /* same, with optional immediate prefix */
6586   OP_EXPr,      /* same, with optional relocation suffix */
6587   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6588   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
6589   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
6590
6591   OP_CPSF,      /* CPS flags */
6592   OP_ENDI,      /* Endianness specifier */
6593   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6594   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6595   OP_COND,      /* conditional code */
6596   OP_TB,        /* Table branch.  */
6597
6598   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6599
6600   OP_RRnpc_I0,  /* ARM register or literal 0 */
6601   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
6602   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6603   OP_RF_IF,     /* FPA register or immediate */
6604   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6605   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6606
6607   /* Optional operands.  */
6608   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6609   OP_oI31b,      /*                             0 .. 31 */
6610   OP_oI32b,      /*                             1 .. 32 */
6611   OP_oI32z,      /*                             0 .. 32 */
6612   OP_oIffffb,    /*                             0 .. 65535 */
6613   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6614
6615   OP_oRR,        /* ARM register */
6616   OP_oRRnpc,     /* ARM register, not the PC */
6617   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6618   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6619   OP_oRND,       /* Optional Neon double precision register */
6620   OP_oRNQ,       /* Optional Neon quad precision register */
6621   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6622   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6623   OP_oSHll,      /* LSL immediate */
6624   OP_oSHar,      /* ASR immediate */
6625   OP_oSHllar,    /* LSL or ASR immediate */
6626   OP_oROR,       /* ROR 0/8/16/24 */
6627   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6628
6629   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6630   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6631   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6632   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6633
6634   OP_FIRST_OPTIONAL = OP_oI7b
6635 };
6636
6637 /* Generic instruction operand parser.  This does no encoding and no
6638    semantic validation; it merely squirrels values away in the inst
6639    structure.  Returns SUCCESS or FAIL depending on whether the
6640    specified grammar matched.  */
6641 static int
6642 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6643 {
6644   unsigned const int *upat = pattern;
6645   char *backtrack_pos = 0;
6646   const char *backtrack_error = 0;
6647   int i, val = 0, backtrack_index = 0;
6648   enum arm_reg_type rtype;
6649   parse_operand_result result;
6650   unsigned int op_parse_code;
6651
6652 #define po_char_or_fail(chr)                    \
6653   do                                            \
6654     {                                           \
6655       if (skip_past_char (&str, chr) == FAIL)   \
6656         goto bad_args;                          \
6657     }                                           \
6658   while (0)
6659
6660 #define po_reg_or_fail(regtype)                                 \
6661   do                                                            \
6662     {                                                           \
6663       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6664                                  & inst.operands[i].vectype);   \
6665       if (val == FAIL)                                          \
6666         {                                                       \
6667           first_error (_(reg_expected_msgs[regtype]));          \
6668           goto failure;                                         \
6669         }                                                       \
6670       inst.operands[i].reg = val;                               \
6671       inst.operands[i].isreg = 1;                               \
6672       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6673       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6674       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6675                              || rtype == REG_TYPE_VFD           \
6676                              || rtype == REG_TYPE_NQ);          \
6677     }                                                           \
6678   while (0)
6679
6680 #define po_reg_or_goto(regtype, label)                          \
6681   do                                                            \
6682     {                                                           \
6683       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6684                                  & inst.operands[i].vectype);   \
6685       if (val == FAIL)                                          \
6686         goto label;                                             \
6687                                                                 \
6688       inst.operands[i].reg = val;                               \
6689       inst.operands[i].isreg = 1;                               \
6690       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6691       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6692       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6693                              || rtype == REG_TYPE_VFD           \
6694                              || rtype == REG_TYPE_NQ);          \
6695     }                                                           \
6696   while (0)
6697
6698 #define po_imm_or_fail(min, max, popt)                          \
6699   do                                                            \
6700     {                                                           \
6701       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6702         goto failure;                                           \
6703       inst.operands[i].imm = val;                               \
6704     }                                                           \
6705   while (0)
6706
6707 #define po_scalar_or_goto(elsz, label)                                  \
6708   do                                                                    \
6709     {                                                                   \
6710       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6711       if (val == FAIL)                                                  \
6712         goto label;                                                     \
6713       inst.operands[i].reg = val;                                       \
6714       inst.operands[i].isscalar = 1;                                    \
6715     }                                                                   \
6716   while (0)
6717
6718 #define po_misc_or_fail(expr)                   \
6719   do                                            \
6720     {                                           \
6721       if (expr)                                 \
6722         goto failure;                           \
6723     }                                           \
6724   while (0)
6725
6726 #define po_misc_or_fail_no_backtrack(expr)              \
6727   do                                                    \
6728     {                                                   \
6729       result = expr;                                    \
6730       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6731         backtrack_pos = 0;                              \
6732       if (result != PARSE_OPERAND_SUCCESS)              \
6733         goto failure;                                   \
6734     }                                                   \
6735   while (0)
6736
6737 #define po_barrier_or_imm(str)                             \
6738   do                                                       \
6739     {                                                      \
6740       val = parse_barrier (&str);                          \
6741       if (val == FAIL && ! ISALPHA (*str))                 \
6742         goto immediate;                                    \
6743       if (val == FAIL                                      \
6744           /* ISB can only take SY as an option.  */        \
6745           || ((inst.instruction & 0xf0) == 0x60            \
6746                && val != 0xf))                             \
6747         {                                                  \
6748            inst.error = _("invalid barrier type");         \
6749            backtrack_pos = 0;                              \
6750            goto failure;                                   \
6751         }                                                  \
6752     }                                                      \
6753   while (0)
6754
6755   skip_whitespace (str);
6756
6757   for (i = 0; upat[i] != OP_stop; i++)
6758     {
6759       op_parse_code = upat[i];
6760       if (op_parse_code >= 1<<16)
6761         op_parse_code = thumb ? (op_parse_code >> 16)
6762                                 : (op_parse_code & ((1<<16)-1));
6763
6764       if (op_parse_code >= OP_FIRST_OPTIONAL)
6765         {
6766           /* Remember where we are in case we need to backtrack.  */
6767           gas_assert (!backtrack_pos);
6768           backtrack_pos = str;
6769           backtrack_error = inst.error;
6770           backtrack_index = i;
6771         }
6772
6773       if (i > 0 && (i > 1 || inst.operands[0].present))
6774         po_char_or_fail (',');
6775
6776       switch (op_parse_code)
6777         {
6778           /* Registers */
6779         case OP_oRRnpc:
6780         case OP_oRRnpcsp:
6781         case OP_RRnpc:
6782         case OP_RRnpcsp:
6783         case OP_oRR:
6784         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6785         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6786         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6787         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6788         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6789         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6790         case OP_oRND:
6791         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6792         case OP_RVC:
6793           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6794           break;
6795           /* Also accept generic coprocessor regs for unknown registers.  */
6796           coproc_reg:
6797           po_reg_or_fail (REG_TYPE_CN);
6798           break;
6799         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6800         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6801         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6802         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6803         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6804         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6805         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6806         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6807         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6808         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6809         case OP_oRNQ:
6810         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6811         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
6812         case OP_oRNDQ:
6813         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6814         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6815         case OP_oRNSDQ:
6816         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6817
6818         /* Neon scalar. Using an element size of 8 means that some invalid
6819            scalars are accepted here, so deal with those in later code.  */
6820         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6821
6822         case OP_RNDQ_I0:
6823           {
6824             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6825             break;
6826             try_imm0:
6827             po_imm_or_fail (0, 0, TRUE);
6828           }
6829           break;
6830
6831         case OP_RVSD_I0:
6832           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6833           break;
6834
6835         case OP_RSVD_FI0:
6836           {
6837             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6838             break;
6839             try_ifimm0:
6840             if (parse_ifimm_zero (&str))
6841               inst.operands[i].imm = 0;
6842             else
6843             {
6844               inst.error
6845                 = _("only floating point zero is allowed as immediate value");
6846               goto failure;
6847             }
6848           }
6849           break;
6850
6851         case OP_RR_RNSC:
6852           {
6853             po_scalar_or_goto (8, try_rr);
6854             break;
6855             try_rr:
6856             po_reg_or_fail (REG_TYPE_RN);
6857           }
6858           break;
6859
6860         case OP_RNSDQ_RNSC:
6861           {
6862             po_scalar_or_goto (8, try_nsdq);
6863             break;
6864             try_nsdq:
6865             po_reg_or_fail (REG_TYPE_NSDQ);
6866           }
6867           break;
6868
6869         case OP_RNSD_RNSC:
6870           {
6871             po_scalar_or_goto (8, try_s_scalar);
6872             break;
6873             try_s_scalar:
6874             po_scalar_or_goto (4, try_nsd);
6875             break;
6876             try_nsd:
6877             po_reg_or_fail (REG_TYPE_NSD);
6878           }
6879           break;
6880
6881         case OP_RNDQ_RNSC:
6882           {
6883             po_scalar_or_goto (8, try_ndq);
6884             break;
6885             try_ndq:
6886             po_reg_or_fail (REG_TYPE_NDQ);
6887           }
6888           break;
6889
6890         case OP_RND_RNSC:
6891           {
6892             po_scalar_or_goto (8, try_vfd);
6893             break;
6894             try_vfd:
6895             po_reg_or_fail (REG_TYPE_VFD);
6896           }
6897           break;
6898
6899         case OP_VMOV:
6900           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6901              not careful then bad things might happen.  */
6902           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6903           break;
6904
6905         case OP_RNDQ_Ibig:
6906           {
6907             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6908             break;
6909             try_immbig:
6910             /* There's a possibility of getting a 64-bit immediate here, so
6911                we need special handling.  */
6912             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6913                 == FAIL)
6914               {
6915                 inst.error = _("immediate value is out of range");
6916                 goto failure;
6917               }
6918           }
6919           break;
6920
6921         case OP_RNDQ_I63b:
6922           {
6923             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6924             break;
6925             try_shimm:
6926             po_imm_or_fail (0, 63, TRUE);
6927           }
6928           break;
6929
6930         case OP_RRnpcb:
6931           po_char_or_fail ('[');
6932           po_reg_or_fail  (REG_TYPE_RN);
6933           po_char_or_fail (']');
6934           break;
6935
6936         case OP_RRnpctw:
6937         case OP_RRw:
6938         case OP_oRRw:
6939           po_reg_or_fail (REG_TYPE_RN);
6940           if (skip_past_char (&str, '!') == SUCCESS)
6941             inst.operands[i].writeback = 1;
6942           break;
6943
6944           /* Immediates */
6945         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6946         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6947         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6948         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6949         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6950         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6951         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6952         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6953         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6954         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6955         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6956         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6957
6958         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6959         case OP_oI7b:
6960         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6961         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6962         case OP_oI31b:
6963         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6964         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6965         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6966         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6967
6968           /* Immediate variants */
6969         case OP_oI255c:
6970           po_char_or_fail ('{');
6971           po_imm_or_fail (0, 255, TRUE);
6972           po_char_or_fail ('}');
6973           break;
6974
6975         case OP_I31w:
6976           /* The expression parser chokes on a trailing !, so we have
6977              to find it first and zap it.  */
6978           {
6979             char *s = str;
6980             while (*s && *s != ',')
6981               s++;
6982             if (s[-1] == '!')
6983               {
6984                 s[-1] = '\0';
6985                 inst.operands[i].writeback = 1;
6986               }
6987             po_imm_or_fail (0, 31, TRUE);
6988             if (str == s - 1)
6989               str = s;
6990           }
6991           break;
6992
6993           /* Expressions */
6994         case OP_EXPi:   EXPi:
6995           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6996                                               GE_OPT_PREFIX));
6997           break;
6998
6999         case OP_EXP:
7000           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
7001                                               GE_NO_PREFIX));
7002           break;
7003
7004         case OP_EXPr:   EXPr:
7005           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
7006                                               GE_NO_PREFIX));
7007           if (inst.reloc.exp.X_op == O_symbol)
7008             {
7009               val = parse_reloc (&str);
7010               if (val == -1)
7011                 {
7012                   inst.error = _("unrecognized relocation suffix");
7013                   goto failure;
7014                 }
7015               else if (val != BFD_RELOC_UNUSED)
7016                 {
7017                   inst.operands[i].imm = val;
7018                   inst.operands[i].hasreloc = 1;
7019                 }
7020             }
7021           break;
7022
7023           /* Operand for MOVW or MOVT.  */
7024         case OP_HALF:
7025           po_misc_or_fail (parse_half (&str));
7026           break;
7027
7028           /* Register or expression.  */
7029         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7030         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7031
7032           /* Register or immediate.  */
7033         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7034         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7035
7036         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7037         IF:
7038           if (!is_immediate_prefix (*str))
7039             goto bad_args;
7040           str++;
7041           val = parse_fpa_immediate (&str);
7042           if (val == FAIL)
7043             goto failure;
7044           /* FPA immediates are encoded as registers 8-15.
7045              parse_fpa_immediate has already applied the offset.  */
7046           inst.operands[i].reg = val;
7047           inst.operands[i].isreg = 1;
7048           break;
7049
7050         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7051         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7052
7053           /* Two kinds of register.  */
7054         case OP_RIWR_RIWC:
7055           {
7056             struct reg_entry *rege = arm_reg_parse_multi (&str);
7057             if (!rege
7058                 || (rege->type != REG_TYPE_MMXWR
7059                     && rege->type != REG_TYPE_MMXWC
7060                     && rege->type != REG_TYPE_MMXWCG))
7061               {
7062                 inst.error = _("iWMMXt data or control register expected");
7063                 goto failure;
7064               }
7065             inst.operands[i].reg = rege->number;
7066             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7067           }
7068           break;
7069
7070         case OP_RIWC_RIWG:
7071           {
7072             struct reg_entry *rege = arm_reg_parse_multi (&str);
7073             if (!rege
7074                 || (rege->type != REG_TYPE_MMXWC
7075                     && rege->type != REG_TYPE_MMXWCG))
7076               {
7077                 inst.error = _("iWMMXt control register expected");
7078                 goto failure;
7079               }
7080             inst.operands[i].reg = rege->number;
7081             inst.operands[i].isreg = 1;
7082           }
7083           break;
7084
7085           /* Misc */
7086         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7087         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7088         case OP_oROR:    val = parse_ror (&str);                break;
7089         case OP_COND:    val = parse_cond (&str);               break;
7090         case OP_oBARRIER_I15:
7091           po_barrier_or_imm (str); break;
7092           immediate:
7093           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7094             goto failure;
7095           break;
7096
7097         case OP_wPSR:
7098         case OP_rPSR:
7099           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7100           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7101             {
7102               inst.error = _("Banked registers are not available with this "
7103                              "architecture.");
7104               goto failure;
7105             }
7106           break;
7107           try_psr:
7108           val = parse_psr (&str, op_parse_code == OP_wPSR);
7109           break;
7110
7111         case OP_APSR_RR:
7112           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7113           break;
7114           try_apsr:
7115           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7116              instruction).  */
7117           if (strncasecmp (str, "APSR_", 5) == 0)
7118             {
7119               unsigned found = 0;
7120               str += 5;
7121               while (found < 15)
7122                 switch (*str++)
7123                   {
7124                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7125                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7126                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7127                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7128                   default: found = 16;
7129                   }
7130               if (found != 15)
7131                 goto failure;
7132               inst.operands[i].isvec = 1;
7133               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7134               inst.operands[i].reg = REG_PC;
7135             }
7136           else
7137             goto failure;
7138           break;
7139
7140         case OP_TB:
7141           po_misc_or_fail (parse_tb (&str));
7142           break;
7143
7144           /* Register lists.  */
7145         case OP_REGLST:
7146           val = parse_reg_list (&str);
7147           if (*str == '^')
7148             {
7149               inst.operands[i].writeback = 1;
7150               str++;
7151             }
7152           break;
7153
7154         case OP_VRSLST:
7155           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7156           break;
7157
7158         case OP_VRDLST:
7159           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7160           break;
7161
7162         case OP_VRSDLST:
7163           /* Allow Q registers too.  */
7164           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7165                                     REGLIST_NEON_D);
7166           if (val == FAIL)
7167             {
7168               inst.error = NULL;
7169               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7170                                         REGLIST_VFP_S);
7171               inst.operands[i].issingle = 1;
7172             }
7173           break;
7174
7175         case OP_NRDLST:
7176           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7177                                     REGLIST_NEON_D);
7178           break;
7179
7180         case OP_NSTRLST:
7181           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7182                                            &inst.operands[i].vectype);
7183           break;
7184
7185           /* Addressing modes */
7186         case OP_ADDR:
7187           po_misc_or_fail (parse_address (&str, i));
7188           break;
7189
7190         case OP_ADDRGLDR:
7191           po_misc_or_fail_no_backtrack (
7192             parse_address_group_reloc (&str, i, GROUP_LDR));
7193           break;
7194
7195         case OP_ADDRGLDRS:
7196           po_misc_or_fail_no_backtrack (
7197             parse_address_group_reloc (&str, i, GROUP_LDRS));
7198           break;
7199
7200         case OP_ADDRGLDC:
7201           po_misc_or_fail_no_backtrack (
7202             parse_address_group_reloc (&str, i, GROUP_LDC));
7203           break;
7204
7205         case OP_SH:
7206           po_misc_or_fail (parse_shifter_operand (&str, i));
7207           break;
7208
7209         case OP_SHG:
7210           po_misc_or_fail_no_backtrack (
7211             parse_shifter_operand_group_reloc (&str, i));
7212           break;
7213
7214         case OP_oSHll:
7215           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7216           break;
7217
7218         case OP_oSHar:
7219           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7220           break;
7221
7222         case OP_oSHllar:
7223           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7224           break;
7225
7226         default:
7227           as_fatal (_("unhandled operand code %d"), op_parse_code);
7228         }
7229
7230       /* Various value-based sanity checks and shared operations.  We
7231          do not signal immediate failures for the register constraints;
7232          this allows a syntax error to take precedence.  */
7233       switch (op_parse_code)
7234         {
7235         case OP_oRRnpc:
7236         case OP_RRnpc:
7237         case OP_RRnpcb:
7238         case OP_RRw:
7239         case OP_oRRw:
7240         case OP_RRnpc_I0:
7241           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7242             inst.error = BAD_PC;
7243           break;
7244
7245         case OP_oRRnpcsp:
7246         case OP_RRnpcsp:
7247           if (inst.operands[i].isreg)
7248             {
7249               if (inst.operands[i].reg == REG_PC)
7250                 inst.error = BAD_PC;
7251               else if (inst.operands[i].reg == REG_SP
7252                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7253                           relaxed since ARMv8-A.  */
7254                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7255                 {
7256                   gas_assert (thumb);
7257                   inst.error = BAD_SP;
7258                 }
7259             }
7260           break;
7261
7262         case OP_RRnpctw:
7263           if (inst.operands[i].isreg
7264               && inst.operands[i].reg == REG_PC
7265               && (inst.operands[i].writeback || thumb))
7266             inst.error = BAD_PC;
7267           break;
7268
7269         case OP_CPSF:
7270         case OP_ENDI:
7271         case OP_oROR:
7272         case OP_wPSR:
7273         case OP_rPSR:
7274         case OP_COND:
7275         case OP_oBARRIER_I15:
7276         case OP_REGLST:
7277         case OP_VRSLST:
7278         case OP_VRDLST:
7279         case OP_VRSDLST:
7280         case OP_NRDLST:
7281         case OP_NSTRLST:
7282           if (val == FAIL)
7283             goto failure;
7284           inst.operands[i].imm = val;
7285           break;
7286
7287         default:
7288           break;
7289         }
7290
7291       /* If we get here, this operand was successfully parsed.  */
7292       inst.operands[i].present = 1;
7293       continue;
7294
7295     bad_args:
7296       inst.error = BAD_ARGS;
7297
7298     failure:
7299       if (!backtrack_pos)
7300         {
7301           /* The parse routine should already have set inst.error, but set a
7302              default here just in case.  */
7303           if (!inst.error)
7304             inst.error = _("syntax error");
7305           return FAIL;
7306         }
7307
7308       /* Do not backtrack over a trailing optional argument that
7309          absorbed some text.  We will only fail again, with the
7310          'garbage following instruction' error message, which is
7311          probably less helpful than the current one.  */
7312       if (backtrack_index == i && backtrack_pos != str
7313           && upat[i+1] == OP_stop)
7314         {
7315           if (!inst.error)
7316             inst.error = _("syntax error");
7317           return FAIL;
7318         }
7319
7320       /* Try again, skipping the optional argument at backtrack_pos.  */
7321       str = backtrack_pos;
7322       inst.error = backtrack_error;
7323       inst.operands[backtrack_index].present = 0;
7324       i = backtrack_index;
7325       backtrack_pos = 0;
7326     }
7327
7328   /* Check that we have parsed all the arguments.  */
7329   if (*str != '\0' && !inst.error)
7330     inst.error = _("garbage following instruction");
7331
7332   return inst.error ? FAIL : SUCCESS;
7333 }
7334
7335 #undef po_char_or_fail
7336 #undef po_reg_or_fail
7337 #undef po_reg_or_goto
7338 #undef po_imm_or_fail
7339 #undef po_scalar_or_fail
7340 #undef po_barrier_or_imm
7341
7342 /* Shorthand macro for instruction encoding functions issuing errors.  */
7343 #define constraint(expr, err)                   \
7344   do                                            \
7345     {                                           \
7346       if (expr)                                 \
7347         {                                       \
7348           inst.error = err;                     \
7349           return;                               \
7350         }                                       \
7351     }                                           \
7352   while (0)
7353
7354 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7355    instructions are unpredictable if these registers are used.  This
7356    is the BadReg predicate in ARM's Thumb-2 documentation.
7357
7358    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
7359    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
7360 #define reject_bad_reg(reg)                                     \
7361   do                                                            \
7362    if (reg == REG_PC)                                           \
7363      {                                                          \
7364        inst.error = BAD_PC;                                     \
7365        return;                                                  \
7366      }                                                          \
7367    else if (reg == REG_SP                                       \
7368             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
7369      {                                                          \
7370        inst.error = BAD_SP;                                     \
7371        return;                                                  \
7372      }                                                          \
7373   while (0)
7374
7375 /* If REG is R13 (the stack pointer), warn that its use is
7376    deprecated.  */
7377 #define warn_deprecated_sp(reg)                 \
7378   do                                            \
7379     if (warn_on_deprecated && reg == REG_SP)    \
7380        as_tsktsk (_("use of r13 is deprecated"));       \
7381   while (0)
7382
7383 /* Functions for operand encoding.  ARM, then Thumb.  */
7384
7385 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7386
7387 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7388
7389    The only binary encoding difference is the Coprocessor number.  Coprocessor
7390    9 is used for half-precision calculations or conversions.  The format of the
7391    instruction is the same as the equivalent Coprocessor 10 instruction that
7392    exists for Single-Precision operation.  */
7393
7394 static void
7395 do_scalar_fp16_v82_encode (void)
7396 {
7397   if (inst.cond != COND_ALWAYS)
7398     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7399                " the behaviour is UNPREDICTABLE"));
7400   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7401               _(BAD_FP16));
7402
7403   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7404   mark_feature_used (&arm_ext_fp16);
7405 }
7406
7407 /* If VAL can be encoded in the immediate field of an ARM instruction,
7408    return the encoded form.  Otherwise, return FAIL.  */
7409
7410 static unsigned int
7411 encode_arm_immediate (unsigned int val)
7412 {
7413   unsigned int a, i;
7414
7415   if (val <= 0xff)
7416     return val;
7417
7418   for (i = 2; i < 32; i += 2)
7419     if ((a = rotate_left (val, i)) <= 0xff)
7420       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7421
7422   return FAIL;
7423 }
7424
7425 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7426    return the encoded form.  Otherwise, return FAIL.  */
7427 static unsigned int
7428 encode_thumb32_immediate (unsigned int val)
7429 {
7430   unsigned int a, i;
7431
7432   if (val <= 0xff)
7433     return val;
7434
7435   for (i = 1; i <= 24; i++)
7436     {
7437       a = val >> i;
7438       if ((val & ~(0xff << i)) == 0)
7439         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7440     }
7441
7442   a = val & 0xff;
7443   if (val == ((a << 16) | a))
7444     return 0x100 | a;
7445   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7446     return 0x300 | a;
7447
7448   a = val & 0xff00;
7449   if (val == ((a << 16) | a))
7450     return 0x200 | (a >> 8);
7451
7452   return FAIL;
7453 }
7454 /* Encode a VFP SP or DP register number into inst.instruction.  */
7455
7456 static void
7457 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7458 {
7459   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7460       && reg > 15)
7461     {
7462       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7463         {
7464           if (thumb_mode)
7465             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7466                                     fpu_vfp_ext_d32);
7467           else
7468             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7469                                     fpu_vfp_ext_d32);
7470         }
7471       else
7472         {
7473           first_error (_("D register out of range for selected VFP version"));
7474           return;
7475         }
7476     }
7477
7478   switch (pos)
7479     {
7480     case VFP_REG_Sd:
7481       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7482       break;
7483
7484     case VFP_REG_Sn:
7485       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7486       break;
7487
7488     case VFP_REG_Sm:
7489       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7490       break;
7491
7492     case VFP_REG_Dd:
7493       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7494       break;
7495
7496     case VFP_REG_Dn:
7497       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7498       break;
7499
7500     case VFP_REG_Dm:
7501       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7502       break;
7503
7504     default:
7505       abort ();
7506     }
7507 }
7508
7509 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7510    if any, is handled by md_apply_fix.   */
7511 static void
7512 encode_arm_shift (int i)
7513 {
7514   /* register-shifted register.  */
7515   if (inst.operands[i].immisreg)
7516     {
7517       int op_index;
7518       for (op_index = 0; op_index <= i; ++op_index)
7519         {
7520           /* Check the operand only when it's presented.  In pre-UAL syntax,
7521              if the destination register is the same as the first operand, two
7522              register form of the instruction can be used.  */
7523           if (inst.operands[op_index].present && inst.operands[op_index].isreg
7524               && inst.operands[op_index].reg == REG_PC)
7525             as_warn (UNPRED_REG ("r15"));
7526         }
7527
7528       if (inst.operands[i].imm == REG_PC)
7529         as_warn (UNPRED_REG ("r15"));
7530     }
7531
7532   if (inst.operands[i].shift_kind == SHIFT_RRX)
7533     inst.instruction |= SHIFT_ROR << 5;
7534   else
7535     {
7536       inst.instruction |= inst.operands[i].shift_kind << 5;
7537       if (inst.operands[i].immisreg)
7538         {
7539           inst.instruction |= SHIFT_BY_REG;
7540           inst.instruction |= inst.operands[i].imm << 8;
7541         }
7542       else
7543         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7544     }
7545 }
7546
7547 static void
7548 encode_arm_shifter_operand (int i)
7549 {
7550   if (inst.operands[i].isreg)
7551     {
7552       inst.instruction |= inst.operands[i].reg;
7553       encode_arm_shift (i);
7554     }
7555   else
7556     {
7557       inst.instruction |= INST_IMMEDIATE;
7558       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7559         inst.instruction |= inst.operands[i].imm;
7560     }
7561 }
7562
7563 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7564 static void
7565 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7566 {
7567   /* PR 14260:
7568      Generate an error if the operand is not a register.  */
7569   constraint (!inst.operands[i].isreg,
7570               _("Instruction does not support =N addresses"));
7571
7572   inst.instruction |= inst.operands[i].reg << 16;
7573
7574   if (inst.operands[i].preind)
7575     {
7576       if (is_t)
7577         {
7578           inst.error = _("instruction does not accept preindexed addressing");
7579           return;
7580         }
7581       inst.instruction |= PRE_INDEX;
7582       if (inst.operands[i].writeback)
7583         inst.instruction |= WRITE_BACK;
7584
7585     }
7586   else if (inst.operands[i].postind)
7587     {
7588       gas_assert (inst.operands[i].writeback);
7589       if (is_t)
7590         inst.instruction |= WRITE_BACK;
7591     }
7592   else /* unindexed - only for coprocessor */
7593     {
7594       inst.error = _("instruction does not accept unindexed addressing");
7595       return;
7596     }
7597
7598   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7599       && (((inst.instruction & 0x000f0000) >> 16)
7600           == ((inst.instruction & 0x0000f000) >> 12)))
7601     as_warn ((inst.instruction & LOAD_BIT)
7602              ? _("destination register same as write-back base")
7603              : _("source register same as write-back base"));
7604 }
7605
7606 /* inst.operands[i] was set up by parse_address.  Encode it into an
7607    ARM-format mode 2 load or store instruction.  If is_t is true,
7608    reject forms that cannot be used with a T instruction (i.e. not
7609    post-indexed).  */
7610 static void
7611 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7612 {
7613   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7614
7615   encode_arm_addr_mode_common (i, is_t);
7616
7617   if (inst.operands[i].immisreg)
7618     {
7619       constraint ((inst.operands[i].imm == REG_PC
7620                    || (is_pc && inst.operands[i].writeback)),
7621                   BAD_PC_ADDRESSING);
7622       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7623       inst.instruction |= inst.operands[i].imm;
7624       if (!inst.operands[i].negative)
7625         inst.instruction |= INDEX_UP;
7626       if (inst.operands[i].shifted)
7627         {
7628           if (inst.operands[i].shift_kind == SHIFT_RRX)
7629             inst.instruction |= SHIFT_ROR << 5;
7630           else
7631             {
7632               inst.instruction |= inst.operands[i].shift_kind << 5;
7633               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7634             }
7635         }
7636     }
7637   else /* immediate offset in inst.reloc */
7638     {
7639       if (is_pc && !inst.reloc.pc_rel)
7640         {
7641           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7642
7643           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7644              cannot use PC in addressing.
7645              PC cannot be used in writeback addressing, either.  */
7646           constraint ((is_t || inst.operands[i].writeback),
7647                       BAD_PC_ADDRESSING);
7648
7649           /* Use of PC in str is deprecated for ARMv7.  */
7650           if (warn_on_deprecated
7651               && !is_load
7652               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7653             as_tsktsk (_("use of PC in this instruction is deprecated"));
7654         }
7655
7656       if (inst.reloc.type == BFD_RELOC_UNUSED)
7657         {
7658           /* Prefer + for zero encoded value.  */
7659           if (!inst.operands[i].negative)
7660             inst.instruction |= INDEX_UP;
7661           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7662         }
7663     }
7664 }
7665
7666 /* inst.operands[i] was set up by parse_address.  Encode it into an
7667    ARM-format mode 3 load or store instruction.  Reject forms that
7668    cannot be used with such instructions.  If is_t is true, reject
7669    forms that cannot be used with a T instruction (i.e. not
7670    post-indexed).  */
7671 static void
7672 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7673 {
7674   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7675     {
7676       inst.error = _("instruction does not accept scaled register index");
7677       return;
7678     }
7679
7680   encode_arm_addr_mode_common (i, is_t);
7681
7682   if (inst.operands[i].immisreg)
7683     {
7684       constraint ((inst.operands[i].imm == REG_PC
7685                    || (is_t && inst.operands[i].reg == REG_PC)),
7686                   BAD_PC_ADDRESSING);
7687       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7688                   BAD_PC_WRITEBACK);
7689       inst.instruction |= inst.operands[i].imm;
7690       if (!inst.operands[i].negative)
7691         inst.instruction |= INDEX_UP;
7692     }
7693   else /* immediate offset in inst.reloc */
7694     {
7695       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7696                    && inst.operands[i].writeback),
7697                   BAD_PC_WRITEBACK);
7698       inst.instruction |= HWOFFSET_IMM;
7699       if (inst.reloc.type == BFD_RELOC_UNUSED)
7700         {
7701           /* Prefer + for zero encoded value.  */
7702           if (!inst.operands[i].negative)
7703             inst.instruction |= INDEX_UP;
7704
7705           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7706         }
7707     }
7708 }
7709
7710 /* Write immediate bits [7:0] to the following locations:
7711
7712   |28/24|23     19|18 16|15                    4|3     0|
7713   |  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|
7714
7715   This function is used by VMOV/VMVN/VORR/VBIC.  */
7716
7717 static void
7718 neon_write_immbits (unsigned immbits)
7719 {
7720   inst.instruction |= immbits & 0xf;
7721   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7722   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7723 }
7724
7725 /* Invert low-order SIZE bits of XHI:XLO.  */
7726
7727 static void
7728 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7729 {
7730   unsigned immlo = xlo ? *xlo : 0;
7731   unsigned immhi = xhi ? *xhi : 0;
7732
7733   switch (size)
7734     {
7735     case 8:
7736       immlo = (~immlo) & 0xff;
7737       break;
7738
7739     case 16:
7740       immlo = (~immlo) & 0xffff;
7741       break;
7742
7743     case 64:
7744       immhi = (~immhi) & 0xffffffff;
7745       /* fall through.  */
7746
7747     case 32:
7748       immlo = (~immlo) & 0xffffffff;
7749       break;
7750
7751     default:
7752       abort ();
7753     }
7754
7755   if (xlo)
7756     *xlo = immlo;
7757
7758   if (xhi)
7759     *xhi = immhi;
7760 }
7761
7762 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7763    A, B, C, D.  */
7764
7765 static int
7766 neon_bits_same_in_bytes (unsigned imm)
7767 {
7768   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7769          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7770          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7771          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7772 }
7773
7774 /* For immediate of above form, return 0bABCD.  */
7775
7776 static unsigned
7777 neon_squash_bits (unsigned imm)
7778 {
7779   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7780          | ((imm & 0x01000000) >> 21);
7781 }
7782
7783 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7784
7785 static unsigned
7786 neon_qfloat_bits (unsigned imm)
7787 {
7788   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7789 }
7790
7791 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7792    the instruction. *OP is passed as the initial value of the op field, and
7793    may be set to a different value depending on the constant (i.e.
7794    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7795    MVN).  If the immediate looks like a repeated pattern then also
7796    try smaller element sizes.  */
7797
7798 static int
7799 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7800                          unsigned *immbits, int *op, int size,
7801                          enum neon_el_type type)
7802 {
7803   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7804      float.  */
7805   if (type == NT_float && !float_p)
7806     return FAIL;
7807
7808   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7809     {
7810       if (size != 32 || *op == 1)
7811         return FAIL;
7812       *immbits = neon_qfloat_bits (immlo);
7813       return 0xf;
7814     }
7815
7816   if (size == 64)
7817     {
7818       if (neon_bits_same_in_bytes (immhi)
7819           && neon_bits_same_in_bytes (immlo))
7820         {
7821           if (*op == 1)
7822             return FAIL;
7823           *immbits = (neon_squash_bits (immhi) << 4)
7824                      | neon_squash_bits (immlo);
7825           *op = 1;
7826           return 0xe;
7827         }
7828
7829       if (immhi != immlo)
7830         return FAIL;
7831     }
7832
7833   if (size >= 32)
7834     {
7835       if (immlo == (immlo & 0x000000ff))
7836         {
7837           *immbits = immlo;
7838           return 0x0;
7839         }
7840       else if (immlo == (immlo & 0x0000ff00))
7841         {
7842           *immbits = immlo >> 8;
7843           return 0x2;
7844         }
7845       else if (immlo == (immlo & 0x00ff0000))
7846         {
7847           *immbits = immlo >> 16;
7848           return 0x4;
7849         }
7850       else if (immlo == (immlo & 0xff000000))
7851         {
7852           *immbits = immlo >> 24;
7853           return 0x6;
7854         }
7855       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7856         {
7857           *immbits = (immlo >> 8) & 0xff;
7858           return 0xc;
7859         }
7860       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7861         {
7862           *immbits = (immlo >> 16) & 0xff;
7863           return 0xd;
7864         }
7865
7866       if ((immlo & 0xffff) != (immlo >> 16))
7867         return FAIL;
7868       immlo &= 0xffff;
7869     }
7870
7871   if (size >= 16)
7872     {
7873       if (immlo == (immlo & 0x000000ff))
7874         {
7875           *immbits = immlo;
7876           return 0x8;
7877         }
7878       else if (immlo == (immlo & 0x0000ff00))
7879         {
7880           *immbits = immlo >> 8;
7881           return 0xa;
7882         }
7883
7884       if ((immlo & 0xff) != (immlo >> 8))
7885         return FAIL;
7886       immlo &= 0xff;
7887     }
7888
7889   if (immlo == (immlo & 0x000000ff))
7890     {
7891       /* Don't allow MVN with 8-bit immediate.  */
7892       if (*op == 1)
7893         return FAIL;
7894       *immbits = immlo;
7895       return 0xe;
7896     }
7897
7898   return FAIL;
7899 }
7900
7901 #if defined BFD_HOST_64_BIT
7902 /* Returns TRUE if double precision value V may be cast
7903    to single precision without loss of accuracy.  */
7904
7905 static bfd_boolean
7906 is_double_a_single (bfd_int64_t v)
7907 {
7908   int exp = (int)((v >> 52) & 0x7FF);
7909   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7910
7911   return (exp == 0 || exp == 0x7FF
7912           || (exp >= 1023 - 126 && exp <= 1023 + 127))
7913     && (mantissa & 0x1FFFFFFFl) == 0;
7914 }
7915
7916 /* Returns a double precision value casted to single precision
7917    (ignoring the least significant bits in exponent and mantissa).  */
7918
7919 static int
7920 double_to_single (bfd_int64_t v)
7921 {
7922   int sign = (int) ((v >> 63) & 1l);
7923   int exp = (int) ((v >> 52) & 0x7FF);
7924   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7925
7926   if (exp == 0x7FF)
7927     exp = 0xFF;
7928   else
7929     {
7930       exp = exp - 1023 + 127;
7931       if (exp >= 0xFF)
7932         {
7933           /* Infinity.  */
7934           exp = 0x7F;
7935           mantissa = 0;
7936         }
7937       else if (exp < 0)
7938         {
7939           /* No denormalized numbers.  */
7940           exp = 0;
7941           mantissa = 0;
7942         }
7943     }
7944   mantissa >>= 29;
7945   return (sign << 31) | (exp << 23) | mantissa;
7946 }
7947 #endif /* BFD_HOST_64_BIT */
7948
7949 enum lit_type
7950 {
7951   CONST_THUMB,
7952   CONST_ARM,
7953   CONST_VEC
7954 };
7955
7956 static void do_vfp_nsyn_opcode (const char *);
7957
7958 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7959    Determine whether it can be performed with a move instruction; if
7960    it can, convert inst.instruction to that move instruction and
7961    return TRUE; if it can't, convert inst.instruction to a literal-pool
7962    load and return FALSE.  If this is not a valid thing to do in the
7963    current context, set inst.error and return TRUE.
7964
7965    inst.operands[i] describes the destination register.  */
7966
7967 static bfd_boolean
7968 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7969 {
7970   unsigned long tbit;
7971   bfd_boolean thumb_p = (t == CONST_THUMB);
7972   bfd_boolean arm_p   = (t == CONST_ARM);
7973
7974   if (thumb_p)
7975     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7976   else
7977     tbit = LOAD_BIT;
7978
7979   if ((inst.instruction & tbit) == 0)
7980     {
7981       inst.error = _("invalid pseudo operation");
7982       return TRUE;
7983     }
7984
7985   if (inst.reloc.exp.X_op != O_constant
7986       && inst.reloc.exp.X_op != O_symbol
7987       && inst.reloc.exp.X_op != O_big)
7988     {
7989       inst.error = _("constant expression expected");
7990       return TRUE;
7991     }
7992
7993   if (inst.reloc.exp.X_op == O_constant
7994       || inst.reloc.exp.X_op == O_big)
7995     {
7996 #if defined BFD_HOST_64_BIT
7997       bfd_int64_t v;
7998 #else
7999       offsetT v;
8000 #endif
8001       if (inst.reloc.exp.X_op == O_big)
8002         {
8003           LITTLENUM_TYPE w[X_PRECISION];
8004           LITTLENUM_TYPE * l;
8005
8006           if (inst.reloc.exp.X_add_number == -1)
8007             {
8008               gen_to_words (w, X_PRECISION, E_PRECISION);
8009               l = w;
8010               /* FIXME: Should we check words w[2..5] ?  */
8011             }
8012           else
8013             l = generic_bignum;
8014
8015 #if defined BFD_HOST_64_BIT
8016           v =
8017             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8018                   << LITTLENUM_NUMBER_OF_BITS)
8019                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8020                 << LITTLENUM_NUMBER_OF_BITS)
8021                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8022               << LITTLENUM_NUMBER_OF_BITS)
8023              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8024 #else
8025           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8026             |  (l[0] & LITTLENUM_MASK);
8027 #endif
8028         }
8029       else
8030         v = inst.reloc.exp.X_add_number;
8031
8032       if (!inst.operands[i].issingle)
8033         {
8034           if (thumb_p)
8035             {
8036               /* LDR should not use lead in a flag-setting instruction being
8037                  chosen so we do not check whether movs can be used.  */
8038
8039               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8040                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8041                   && inst.operands[i].reg != 13
8042                   && inst.operands[i].reg != 15)
8043                 {
8044                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8045                      movw instruction.  */
8046                   unsigned int newimm;
8047                   bfd_boolean isNegated;
8048
8049                   newimm = encode_thumb32_immediate (v);
8050                   if (newimm != (unsigned int) FAIL)
8051                     isNegated = FALSE;
8052                   else
8053                     {
8054                       newimm = encode_thumb32_immediate (~v);
8055                       if (newimm != (unsigned int) FAIL)
8056                         isNegated = TRUE;
8057                     }
8058
8059                   /* The number can be loaded with a mov.w or mvn
8060                      instruction.  */
8061                   if (newimm != (unsigned int) FAIL
8062                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8063                     {
8064                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8065                                           | (inst.operands[i].reg << 8));
8066                       /* Change to MOVN.  */
8067                       inst.instruction |= (isNegated ? 0x200000 : 0);
8068                       inst.instruction |= (newimm & 0x800) << 15;
8069                       inst.instruction |= (newimm & 0x700) << 4;
8070                       inst.instruction |= (newimm & 0x0ff);
8071                       return TRUE;
8072                     }
8073                   /* The number can be loaded with a movw instruction.  */
8074                   else if ((v & ~0xFFFF) == 0
8075                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8076                     {
8077                       int imm = v & 0xFFFF;
8078
8079                       inst.instruction = 0xf2400000;  /* MOVW.  */
8080                       inst.instruction |= (inst.operands[i].reg << 8);
8081                       inst.instruction |= (imm & 0xf000) << 4;
8082                       inst.instruction |= (imm & 0x0800) << 15;
8083                       inst.instruction |= (imm & 0x0700) << 4;
8084                       inst.instruction |= (imm & 0x00ff);
8085                       return TRUE;
8086                     }
8087                 }
8088             }
8089           else if (arm_p)
8090             {
8091               int value = encode_arm_immediate (v);
8092
8093               if (value != FAIL)
8094                 {
8095                   /* This can be done with a mov instruction.  */
8096                   inst.instruction &= LITERAL_MASK;
8097                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8098                   inst.instruction |= value & 0xfff;
8099                   return TRUE;
8100                 }
8101
8102               value = encode_arm_immediate (~ v);
8103               if (value != FAIL)
8104                 {
8105                   /* This can be done with a mvn instruction.  */
8106                   inst.instruction &= LITERAL_MASK;
8107                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8108                   inst.instruction |= value & 0xfff;
8109                   return TRUE;
8110                 }
8111             }
8112           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8113             {
8114               int op = 0;
8115               unsigned immbits = 0;
8116               unsigned immlo = inst.operands[1].imm;
8117               unsigned immhi = inst.operands[1].regisimm
8118                 ? inst.operands[1].reg
8119                 : inst.reloc.exp.X_unsigned
8120                 ? 0
8121                 : ((bfd_int64_t)((int) immlo)) >> 32;
8122               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8123                                                    &op, 64, NT_invtype);
8124
8125               if (cmode == FAIL)
8126                 {
8127                   neon_invert_size (&immlo, &immhi, 64);
8128                   op = !op;
8129                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8130                                                    &op, 64, NT_invtype);
8131                 }
8132
8133               if (cmode != FAIL)
8134                 {
8135                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8136                     | (1 << 23)
8137                     | (cmode << 8)
8138                     | (op << 5)
8139                     | (1 << 4);
8140
8141                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8142                   if (thumb_mode)
8143                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8144                   else
8145                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8146                   neon_write_immbits (immbits);
8147                   return TRUE;
8148                 }
8149             }
8150         }
8151
8152       if (t == CONST_VEC)
8153         {
8154           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8155           if (inst.operands[i].issingle
8156               && is_quarter_float (inst.operands[1].imm)
8157               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8158             {
8159               inst.operands[1].imm =
8160                 neon_qfloat_bits (v);
8161               do_vfp_nsyn_opcode ("fconsts");
8162               return TRUE;
8163             }
8164
8165           /* If our host does not support a 64-bit type then we cannot perform
8166              the following optimization.  This mean that there will be a
8167              discrepancy between the output produced by an assembler built for
8168              a 32-bit-only host and the output produced from a 64-bit host, but
8169              this cannot be helped.  */
8170 #if defined BFD_HOST_64_BIT
8171           else if (!inst.operands[1].issingle
8172                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8173             {
8174               if (is_double_a_single (v)
8175                   && is_quarter_float (double_to_single (v)))
8176                 {
8177                   inst.operands[1].imm =
8178                     neon_qfloat_bits (double_to_single (v));
8179                   do_vfp_nsyn_opcode ("fconstd");
8180                   return TRUE;
8181                 }
8182             }
8183 #endif
8184         }
8185     }
8186
8187   if (add_to_lit_pool ((!inst.operands[i].isvec
8188                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8189     return TRUE;
8190
8191   inst.operands[1].reg = REG_PC;
8192   inst.operands[1].isreg = 1;
8193   inst.operands[1].preind = 1;
8194   inst.reloc.pc_rel = 1;
8195   inst.reloc.type = (thumb_p
8196                      ? BFD_RELOC_ARM_THUMB_OFFSET
8197                      : (mode_3
8198                         ? BFD_RELOC_ARM_HWLITERAL
8199                         : BFD_RELOC_ARM_LITERAL));
8200   return FALSE;
8201 }
8202
8203 /* inst.operands[i] was set up by parse_address.  Encode it into an
8204    ARM-format instruction.  Reject all forms which cannot be encoded
8205    into a coprocessor load/store instruction.  If wb_ok is false,
8206    reject use of writeback; if unind_ok is false, reject use of
8207    unindexed addressing.  If reloc_override is not 0, use it instead
8208    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8209    (in which case it is preserved).  */
8210
8211 static int
8212 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8213 {
8214   if (!inst.operands[i].isreg)
8215     {
8216       /* PR 18256 */
8217       if (! inst.operands[0].isvec)
8218         {
8219           inst.error = _("invalid co-processor operand");
8220           return FAIL;
8221         }
8222       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8223         return SUCCESS;
8224     }
8225
8226   inst.instruction |= inst.operands[i].reg << 16;
8227
8228   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8229
8230   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8231     {
8232       gas_assert (!inst.operands[i].writeback);
8233       if (!unind_ok)
8234         {
8235           inst.error = _("instruction does not support unindexed addressing");
8236           return FAIL;
8237         }
8238       inst.instruction |= inst.operands[i].imm;
8239       inst.instruction |= INDEX_UP;
8240       return SUCCESS;
8241     }
8242
8243   if (inst.operands[i].preind)
8244     inst.instruction |= PRE_INDEX;
8245
8246   if (inst.operands[i].writeback)
8247     {
8248       if (inst.operands[i].reg == REG_PC)
8249         {
8250           inst.error = _("pc may not be used with write-back");
8251           return FAIL;
8252         }
8253       if (!wb_ok)
8254         {
8255           inst.error = _("instruction does not support writeback");
8256           return FAIL;
8257         }
8258       inst.instruction |= WRITE_BACK;
8259     }
8260
8261   if (reloc_override)
8262     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
8263   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
8264             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
8265            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
8266     {
8267       if (thumb_mode)
8268         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8269       else
8270         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
8271     }
8272
8273   /* Prefer + for zero encoded value.  */
8274   if (!inst.operands[i].negative)
8275     inst.instruction |= INDEX_UP;
8276
8277   return SUCCESS;
8278 }
8279
8280 /* Functions for instruction encoding, sorted by sub-architecture.
8281    First some generics; their names are taken from the conventional
8282    bit positions for register arguments in ARM format instructions.  */
8283
8284 static void
8285 do_noargs (void)
8286 {
8287 }
8288
8289 static void
8290 do_rd (void)
8291 {
8292   inst.instruction |= inst.operands[0].reg << 12;
8293 }
8294
8295 static void
8296 do_rn (void)
8297 {
8298   inst.instruction |= inst.operands[0].reg << 16;
8299 }
8300
8301 static void
8302 do_rd_rm (void)
8303 {
8304   inst.instruction |= inst.operands[0].reg << 12;
8305   inst.instruction |= inst.operands[1].reg;
8306 }
8307
8308 static void
8309 do_rm_rn (void)
8310 {
8311   inst.instruction |= inst.operands[0].reg;
8312   inst.instruction |= inst.operands[1].reg << 16;
8313 }
8314
8315 static void
8316 do_rd_rn (void)
8317 {
8318   inst.instruction |= inst.operands[0].reg << 12;
8319   inst.instruction |= inst.operands[1].reg << 16;
8320 }
8321
8322 static void
8323 do_rn_rd (void)
8324 {
8325   inst.instruction |= inst.operands[0].reg << 16;
8326   inst.instruction |= inst.operands[1].reg << 12;
8327 }
8328
8329 static void
8330 do_tt (void)
8331 {
8332   inst.instruction |= inst.operands[0].reg << 8;
8333   inst.instruction |= inst.operands[1].reg << 16;
8334 }
8335
8336 static bfd_boolean
8337 check_obsolete (const arm_feature_set *feature, const char *msg)
8338 {
8339   if (ARM_CPU_IS_ANY (cpu_variant))
8340     {
8341       as_tsktsk ("%s", msg);
8342       return TRUE;
8343     }
8344   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8345     {
8346       as_bad ("%s", msg);
8347       return TRUE;
8348     }
8349
8350   return FALSE;
8351 }
8352
8353 static void
8354 do_rd_rm_rn (void)
8355 {
8356   unsigned Rn = inst.operands[2].reg;
8357   /* Enforce restrictions on SWP instruction.  */
8358   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8359     {
8360       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8361                   _("Rn must not overlap other operands"));
8362
8363       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8364        */
8365       if (!check_obsolete (&arm_ext_v8,
8366                            _("swp{b} use is obsoleted for ARMv8 and later"))
8367           && warn_on_deprecated
8368           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8369         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8370     }
8371
8372   inst.instruction |= inst.operands[0].reg << 12;
8373   inst.instruction |= inst.operands[1].reg;
8374   inst.instruction |= Rn << 16;
8375 }
8376
8377 static void
8378 do_rd_rn_rm (void)
8379 {
8380   inst.instruction |= inst.operands[0].reg << 12;
8381   inst.instruction |= inst.operands[1].reg << 16;
8382   inst.instruction |= inst.operands[2].reg;
8383 }
8384
8385 static void
8386 do_rm_rd_rn (void)
8387 {
8388   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8389   constraint (((inst.reloc.exp.X_op != O_constant
8390                 && inst.reloc.exp.X_op != O_illegal)
8391                || inst.reloc.exp.X_add_number != 0),
8392               BAD_ADDR_MODE);
8393   inst.instruction |= inst.operands[0].reg;
8394   inst.instruction |= inst.operands[1].reg << 12;
8395   inst.instruction |= inst.operands[2].reg << 16;
8396 }
8397
8398 static void
8399 do_imm0 (void)
8400 {
8401   inst.instruction |= inst.operands[0].imm;
8402 }
8403
8404 static void
8405 do_rd_cpaddr (void)
8406 {
8407   inst.instruction |= inst.operands[0].reg << 12;
8408   encode_arm_cp_address (1, TRUE, TRUE, 0);
8409 }
8410
8411 /* ARM instructions, in alphabetical order by function name (except
8412    that wrapper functions appear immediately after the function they
8413    wrap).  */
8414
8415 /* This is a pseudo-op of the form "adr rd, label" to be converted
8416    into a relative address of the form "add rd, pc, #label-.-8".  */
8417
8418 static void
8419 do_adr (void)
8420 {
8421   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8422
8423   /* Frag hacking will turn this into a sub instruction if the offset turns
8424      out to be negative.  */
8425   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8426   inst.reloc.pc_rel = 1;
8427   inst.reloc.exp.X_add_number -= 8;
8428
8429   if (support_interwork
8430       && inst.reloc.exp.X_op == O_symbol
8431       && inst.reloc.exp.X_add_symbol != NULL
8432       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
8433       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
8434     inst.reloc.exp.X_add_number |= 1;
8435 }
8436
8437 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8438    into a relative address of the form:
8439    add rd, pc, #low(label-.-8)"
8440    add rd, rd, #high(label-.-8)"  */
8441
8442 static void
8443 do_adrl (void)
8444 {
8445   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8446
8447   /* Frag hacking will turn this into a sub instruction if the offset turns
8448      out to be negative.  */
8449   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8450   inst.reloc.pc_rel            = 1;
8451   inst.size                    = INSN_SIZE * 2;
8452   inst.reloc.exp.X_add_number -= 8;
8453
8454   if (support_interwork
8455       && inst.reloc.exp.X_op == O_symbol
8456       && inst.reloc.exp.X_add_symbol != NULL
8457       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
8458       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
8459     inst.reloc.exp.X_add_number |= 1;
8460 }
8461
8462 static void
8463 do_arit (void)
8464 {
8465   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
8466               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
8467               THUMB1_RELOC_ONLY);
8468   if (!inst.operands[1].present)
8469     inst.operands[1].reg = inst.operands[0].reg;
8470   inst.instruction |= inst.operands[0].reg << 12;
8471   inst.instruction |= inst.operands[1].reg << 16;
8472   encode_arm_shifter_operand (2);
8473 }
8474
8475 static void
8476 do_barrier (void)
8477 {
8478   if (inst.operands[0].present)
8479     inst.instruction |= inst.operands[0].imm;
8480   else
8481     inst.instruction |= 0xf;
8482 }
8483
8484 static void
8485 do_bfc (void)
8486 {
8487   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8488   constraint (msb > 32, _("bit-field extends past end of register"));
8489   /* The instruction encoding stores the LSB and MSB,
8490      not the LSB and width.  */
8491   inst.instruction |= inst.operands[0].reg << 12;
8492   inst.instruction |= inst.operands[1].imm << 7;
8493   inst.instruction |= (msb - 1) << 16;
8494 }
8495
8496 static void
8497 do_bfi (void)
8498 {
8499   unsigned int msb;
8500
8501   /* #0 in second position is alternative syntax for bfc, which is
8502      the same instruction but with REG_PC in the Rm field.  */
8503   if (!inst.operands[1].isreg)
8504     inst.operands[1].reg = REG_PC;
8505
8506   msb = inst.operands[2].imm + inst.operands[3].imm;
8507   constraint (msb > 32, _("bit-field extends past end of register"));
8508   /* The instruction encoding stores the LSB and MSB,
8509      not the LSB and width.  */
8510   inst.instruction |= inst.operands[0].reg << 12;
8511   inst.instruction |= inst.operands[1].reg;
8512   inst.instruction |= inst.operands[2].imm << 7;
8513   inst.instruction |= (msb - 1) << 16;
8514 }
8515
8516 static void
8517 do_bfx (void)
8518 {
8519   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8520               _("bit-field extends past end of register"));
8521   inst.instruction |= inst.operands[0].reg << 12;
8522   inst.instruction |= inst.operands[1].reg;
8523   inst.instruction |= inst.operands[2].imm << 7;
8524   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8525 }
8526
8527 /* ARM V5 breakpoint instruction (argument parse)
8528      BKPT <16 bit unsigned immediate>
8529      Instruction is not conditional.
8530         The bit pattern given in insns[] has the COND_ALWAYS condition,
8531         and it is an error if the caller tried to override that.  */
8532
8533 static void
8534 do_bkpt (void)
8535 {
8536   /* Top 12 of 16 bits to bits 19:8.  */
8537   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8538
8539   /* Bottom 4 of 16 bits to bits 3:0.  */
8540   inst.instruction |= inst.operands[0].imm & 0xf;
8541 }
8542
8543 static void
8544 encode_branch (int default_reloc)
8545 {
8546   if (inst.operands[0].hasreloc)
8547     {
8548       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8549                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8550                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8551       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8552         ? BFD_RELOC_ARM_PLT32
8553         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8554     }
8555   else
8556     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8557   inst.reloc.pc_rel = 1;
8558 }
8559
8560 static void
8561 do_branch (void)
8562 {
8563 #ifdef OBJ_ELF
8564   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8565     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8566   else
8567 #endif
8568     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8569 }
8570
8571 static void
8572 do_bl (void)
8573 {
8574 #ifdef OBJ_ELF
8575   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8576     {
8577       if (inst.cond == COND_ALWAYS)
8578         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8579       else
8580         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8581     }
8582   else
8583 #endif
8584     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8585 }
8586
8587 /* ARM V5 branch-link-exchange instruction (argument parse)
8588      BLX <target_addr>          ie BLX(1)
8589      BLX{<condition>} <Rm>      ie BLX(2)
8590    Unfortunately, there are two different opcodes for this mnemonic.
8591    So, the insns[].value is not used, and the code here zaps values
8592         into inst.instruction.
8593    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8594
8595 static void
8596 do_blx (void)
8597 {
8598   if (inst.operands[0].isreg)
8599     {
8600       /* Arg is a register; the opcode provided by insns[] is correct.
8601          It is not illegal to do "blx pc", just useless.  */
8602       if (inst.operands[0].reg == REG_PC)
8603         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8604
8605       inst.instruction |= inst.operands[0].reg;
8606     }
8607   else
8608     {
8609       /* Arg is an address; this instruction cannot be executed
8610          conditionally, and the opcode must be adjusted.
8611          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8612          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8613       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8614       inst.instruction = 0xfa000000;
8615       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8616     }
8617 }
8618
8619 static void
8620 do_bx (void)
8621 {
8622   bfd_boolean want_reloc;
8623
8624   if (inst.operands[0].reg == REG_PC)
8625     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8626
8627   inst.instruction |= inst.operands[0].reg;
8628   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8629      it is for ARMv4t or earlier.  */
8630   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8631   if (!ARM_FEATURE_ZERO (selected_object_arch)
8632       && !ARM_CPU_HAS_FEATURE (selected_object_arch, arm_ext_v5))
8633       want_reloc = TRUE;
8634
8635 #ifdef OBJ_ELF
8636   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8637 #endif
8638     want_reloc = FALSE;
8639
8640   if (want_reloc)
8641     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8642 }
8643
8644
8645 /* ARM v5TEJ.  Jump to Jazelle code.  */
8646
8647 static void
8648 do_bxj (void)
8649 {
8650   if (inst.operands[0].reg == REG_PC)
8651     as_tsktsk (_("use of r15 in bxj is not really useful"));
8652
8653   inst.instruction |= inst.operands[0].reg;
8654 }
8655
8656 /* Co-processor data operation:
8657       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8658       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8659 static void
8660 do_cdp (void)
8661 {
8662   inst.instruction |= inst.operands[0].reg << 8;
8663   inst.instruction |= inst.operands[1].imm << 20;
8664   inst.instruction |= inst.operands[2].reg << 12;
8665   inst.instruction |= inst.operands[3].reg << 16;
8666   inst.instruction |= inst.operands[4].reg;
8667   inst.instruction |= inst.operands[5].imm << 5;
8668 }
8669
8670 static void
8671 do_cmp (void)
8672 {
8673   inst.instruction |= inst.operands[0].reg << 16;
8674   encode_arm_shifter_operand (1);
8675 }
8676
8677 /* Transfer between coprocessor and ARM registers.
8678    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8679    MRC2
8680    MCR{cond}
8681    MCR2
8682
8683    No special properties.  */
8684
8685 struct deprecated_coproc_regs_s
8686 {
8687   unsigned cp;
8688   int opc1;
8689   unsigned crn;
8690   unsigned crm;
8691   int opc2;
8692   arm_feature_set deprecated;
8693   arm_feature_set obsoleted;
8694   const char *dep_msg;
8695   const char *obs_msg;
8696 };
8697
8698 #define DEPR_ACCESS_V8 \
8699   N_("This coprocessor register access is deprecated in ARMv8")
8700
8701 /* Table of all deprecated coprocessor registers.  */
8702 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8703 {
8704     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8705      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8706      DEPR_ACCESS_V8, NULL},
8707     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8708      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8709      DEPR_ACCESS_V8, NULL},
8710     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8711      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8712      DEPR_ACCESS_V8, NULL},
8713     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8714      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8715      DEPR_ACCESS_V8, NULL},
8716     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8717      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8718      DEPR_ACCESS_V8, NULL},
8719 };
8720
8721 #undef DEPR_ACCESS_V8
8722
8723 static const size_t deprecated_coproc_reg_count =
8724   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8725
8726 static void
8727 do_co_reg (void)
8728 {
8729   unsigned Rd;
8730   size_t i;
8731
8732   Rd = inst.operands[2].reg;
8733   if (thumb_mode)
8734     {
8735       if (inst.instruction == 0xee000010
8736           || inst.instruction == 0xfe000010)
8737         /* MCR, MCR2  */
8738         reject_bad_reg (Rd);
8739       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
8740         /* MRC, MRC2  */
8741         constraint (Rd == REG_SP, BAD_SP);
8742     }
8743   else
8744     {
8745       /* MCR */
8746       if (inst.instruction == 0xe000010)
8747         constraint (Rd == REG_PC, BAD_PC);
8748     }
8749
8750     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8751       {
8752         const struct deprecated_coproc_regs_s *r =
8753           deprecated_coproc_regs + i;
8754
8755         if (inst.operands[0].reg == r->cp
8756             && inst.operands[1].imm == r->opc1
8757             && inst.operands[3].reg == r->crn
8758             && inst.operands[4].reg == r->crm
8759             && inst.operands[5].imm == r->opc2)
8760           {
8761             if (! ARM_CPU_IS_ANY (cpu_variant)
8762                 && warn_on_deprecated
8763                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8764               as_tsktsk ("%s", r->dep_msg);
8765           }
8766       }
8767
8768   inst.instruction |= inst.operands[0].reg << 8;
8769   inst.instruction |= inst.operands[1].imm << 21;
8770   inst.instruction |= Rd << 12;
8771   inst.instruction |= inst.operands[3].reg << 16;
8772   inst.instruction |= inst.operands[4].reg;
8773   inst.instruction |= inst.operands[5].imm << 5;
8774 }
8775
8776 /* Transfer between coprocessor register and pair of ARM registers.
8777    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8778    MCRR2
8779    MRRC{cond}
8780    MRRC2
8781
8782    Two XScale instructions are special cases of these:
8783
8784      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8785      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8786
8787    Result unpredictable if Rd or Rn is R15.  */
8788
8789 static void
8790 do_co_reg2c (void)
8791 {
8792   unsigned Rd, Rn;
8793
8794   Rd = inst.operands[2].reg;
8795   Rn = inst.operands[3].reg;
8796
8797   if (thumb_mode)
8798     {
8799       reject_bad_reg (Rd);
8800       reject_bad_reg (Rn);
8801     }
8802   else
8803     {
8804       constraint (Rd == REG_PC, BAD_PC);
8805       constraint (Rn == REG_PC, BAD_PC);
8806     }
8807
8808   /* Only check the MRRC{2} variants.  */
8809   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
8810     {
8811        /* If Rd == Rn, error that the operation is
8812           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
8813        constraint (Rd == Rn, BAD_OVERLAP);
8814     }
8815
8816   inst.instruction |= inst.operands[0].reg << 8;
8817   inst.instruction |= inst.operands[1].imm << 4;
8818   inst.instruction |= Rd << 12;
8819   inst.instruction |= Rn << 16;
8820   inst.instruction |= inst.operands[4].reg;
8821 }
8822
8823 static void
8824 do_cpsi (void)
8825 {
8826   inst.instruction |= inst.operands[0].imm << 6;
8827   if (inst.operands[1].present)
8828     {
8829       inst.instruction |= CPSI_MMOD;
8830       inst.instruction |= inst.operands[1].imm;
8831     }
8832 }
8833
8834 static void
8835 do_dbg (void)
8836 {
8837   inst.instruction |= inst.operands[0].imm;
8838 }
8839
8840 static void
8841 do_div (void)
8842 {
8843   unsigned Rd, Rn, Rm;
8844
8845   Rd = inst.operands[0].reg;
8846   Rn = (inst.operands[1].present
8847         ? inst.operands[1].reg : Rd);
8848   Rm = inst.operands[2].reg;
8849
8850   constraint ((Rd == REG_PC), BAD_PC);
8851   constraint ((Rn == REG_PC), BAD_PC);
8852   constraint ((Rm == REG_PC), BAD_PC);
8853
8854   inst.instruction |= Rd << 16;
8855   inst.instruction |= Rn << 0;
8856   inst.instruction |= Rm << 8;
8857 }
8858
8859 static void
8860 do_it (void)
8861 {
8862   /* There is no IT instruction in ARM mode.  We
8863      process it to do the validation as if in
8864      thumb mode, just in case the code gets
8865      assembled for thumb using the unified syntax.  */
8866
8867   inst.size = 0;
8868   if (unified_syntax)
8869     {
8870       set_it_insn_type (IT_INSN);
8871       now_it.mask = (inst.instruction & 0xf) | 0x10;
8872       now_it.cc = inst.operands[0].imm;
8873     }
8874 }
8875
8876 /* If there is only one register in the register list,
8877    then return its register number.  Otherwise return -1.  */
8878 static int
8879 only_one_reg_in_list (int range)
8880 {
8881   int i = ffs (range) - 1;
8882   return (i > 15 || range != (1 << i)) ? -1 : i;
8883 }
8884
8885 static void
8886 encode_ldmstm(int from_push_pop_mnem)
8887 {
8888   int base_reg = inst.operands[0].reg;
8889   int range = inst.operands[1].imm;
8890   int one_reg;
8891
8892   inst.instruction |= base_reg << 16;
8893   inst.instruction |= range;
8894
8895   if (inst.operands[1].writeback)
8896     inst.instruction |= LDM_TYPE_2_OR_3;
8897
8898   if (inst.operands[0].writeback)
8899     {
8900       inst.instruction |= WRITE_BACK;
8901       /* Check for unpredictable uses of writeback.  */
8902       if (inst.instruction & LOAD_BIT)
8903         {
8904           /* Not allowed in LDM type 2.  */
8905           if ((inst.instruction & LDM_TYPE_2_OR_3)
8906               && ((range & (1 << REG_PC)) == 0))
8907             as_warn (_("writeback of base register is UNPREDICTABLE"));
8908           /* Only allowed if base reg not in list for other types.  */
8909           else if (range & (1 << base_reg))
8910             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8911         }
8912       else /* STM.  */
8913         {
8914           /* Not allowed for type 2.  */
8915           if (inst.instruction & LDM_TYPE_2_OR_3)
8916             as_warn (_("writeback of base register is UNPREDICTABLE"));
8917           /* Only allowed if base reg not in list, or first in list.  */
8918           else if ((range & (1 << base_reg))
8919                    && (range & ((1 << base_reg) - 1)))
8920             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8921         }
8922     }
8923
8924   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8925   one_reg = only_one_reg_in_list (range);
8926   if (from_push_pop_mnem && one_reg >= 0)
8927     {
8928       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8929
8930       if (is_push && one_reg == 13 /* SP */)
8931         /* PR 22483: The A2 encoding cannot be used when
8932            pushing the stack pointer as this is UNPREDICTABLE.  */
8933         return;
8934
8935       inst.instruction &= A_COND_MASK;
8936       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8937       inst.instruction |= one_reg << 12;
8938     }
8939 }
8940
8941 static void
8942 do_ldmstm (void)
8943 {
8944   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8945 }
8946
8947 /* ARMv5TE load-consecutive (argument parse)
8948    Mode is like LDRH.
8949
8950      LDRccD R, mode
8951      STRccD R, mode.  */
8952
8953 static void
8954 do_ldrd (void)
8955 {
8956   constraint (inst.operands[0].reg % 2 != 0,
8957               _("first transfer register must be even"));
8958   constraint (inst.operands[1].present
8959               && inst.operands[1].reg != inst.operands[0].reg + 1,
8960               _("can only transfer two consecutive registers"));
8961   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8962   constraint (!inst.operands[2].isreg, _("'[' expected"));
8963
8964   if (!inst.operands[1].present)
8965     inst.operands[1].reg = inst.operands[0].reg + 1;
8966
8967   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8968      register and the first register written; we have to diagnose
8969      overlap between the base and the second register written here.  */
8970
8971   if (inst.operands[2].reg == inst.operands[1].reg
8972       && (inst.operands[2].writeback || inst.operands[2].postind))
8973     as_warn (_("base register written back, and overlaps "
8974                "second transfer register"));
8975
8976   if (!(inst.instruction & V4_STR_BIT))
8977     {
8978       /* For an index-register load, the index register must not overlap the
8979         destination (even if not write-back).  */
8980       if (inst.operands[2].immisreg
8981               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8982               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8983         as_warn (_("index register overlaps transfer register"));
8984     }
8985   inst.instruction |= inst.operands[0].reg << 12;
8986   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8987 }
8988
8989 static void
8990 do_ldrex (void)
8991 {
8992   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8993               || inst.operands[1].postind || inst.operands[1].writeback
8994               || inst.operands[1].immisreg || inst.operands[1].shifted
8995               || inst.operands[1].negative
8996               /* This can arise if the programmer has written
8997                    strex rN, rM, foo
8998                  or if they have mistakenly used a register name as the last
8999                  operand,  eg:
9000                    strex rN, rM, rX
9001                  It is very difficult to distinguish between these two cases
9002                  because "rX" might actually be a label. ie the register
9003                  name has been occluded by a symbol of the same name. So we
9004                  just generate a general 'bad addressing mode' type error
9005                  message and leave it up to the programmer to discover the
9006                  true cause and fix their mistake.  */
9007               || (inst.operands[1].reg == REG_PC),
9008               BAD_ADDR_MODE);
9009
9010   constraint (inst.reloc.exp.X_op != O_constant
9011               || inst.reloc.exp.X_add_number != 0,
9012               _("offset must be zero in ARM encoding"));
9013
9014   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
9015
9016   inst.instruction |= inst.operands[0].reg << 12;
9017   inst.instruction |= inst.operands[1].reg << 16;
9018   inst.reloc.type = BFD_RELOC_UNUSED;
9019 }
9020
9021 static void
9022 do_ldrexd (void)
9023 {
9024   constraint (inst.operands[0].reg % 2 != 0,
9025               _("even register required"));
9026   constraint (inst.operands[1].present
9027               && inst.operands[1].reg != inst.operands[0].reg + 1,
9028               _("can only load two consecutive registers"));
9029   /* If op 1 were present and equal to PC, this function wouldn't
9030      have been called in the first place.  */
9031   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9032
9033   inst.instruction |= inst.operands[0].reg << 12;
9034   inst.instruction |= inst.operands[2].reg << 16;
9035 }
9036
9037 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9038    which is not a multiple of four is UNPREDICTABLE.  */
9039 static void
9040 check_ldr_r15_aligned (void)
9041 {
9042   constraint (!(inst.operands[1].immisreg)
9043               && (inst.operands[0].reg == REG_PC
9044               && inst.operands[1].reg == REG_PC
9045               && (inst.reloc.exp.X_add_number & 0x3)),
9046               _("ldr to register 15 must be 4-byte aligned"));
9047 }
9048
9049 static void
9050 do_ldst (void)
9051 {
9052   inst.instruction |= inst.operands[0].reg << 12;
9053   if (!inst.operands[1].isreg)
9054     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9055       return;
9056   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9057   check_ldr_r15_aligned ();
9058 }
9059
9060 static void
9061 do_ldstt (void)
9062 {
9063   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9064      reject [Rn,...].  */
9065   if (inst.operands[1].preind)
9066     {
9067       constraint (inst.reloc.exp.X_op != O_constant
9068                   || inst.reloc.exp.X_add_number != 0,
9069                   _("this instruction requires a post-indexed address"));
9070
9071       inst.operands[1].preind = 0;
9072       inst.operands[1].postind = 1;
9073       inst.operands[1].writeback = 1;
9074     }
9075   inst.instruction |= inst.operands[0].reg << 12;
9076   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9077 }
9078
9079 /* Halfword and signed-byte load/store operations.  */
9080
9081 static void
9082 do_ldstv4 (void)
9083 {
9084   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9085   inst.instruction |= inst.operands[0].reg << 12;
9086   if (!inst.operands[1].isreg)
9087     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9088       return;
9089   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9090 }
9091
9092 static void
9093 do_ldsttv4 (void)
9094 {
9095   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9096      reject [Rn,...].  */
9097   if (inst.operands[1].preind)
9098     {
9099       constraint (inst.reloc.exp.X_op != O_constant
9100                   || inst.reloc.exp.X_add_number != 0,
9101                   _("this instruction requires a post-indexed address"));
9102
9103       inst.operands[1].preind = 0;
9104       inst.operands[1].postind = 1;
9105       inst.operands[1].writeback = 1;
9106     }
9107   inst.instruction |= inst.operands[0].reg << 12;
9108   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9109 }
9110
9111 /* Co-processor register load/store.
9112    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9113 static void
9114 do_lstc (void)
9115 {
9116   inst.instruction |= inst.operands[0].reg << 8;
9117   inst.instruction |= inst.operands[1].reg << 12;
9118   encode_arm_cp_address (2, TRUE, TRUE, 0);
9119 }
9120
9121 static void
9122 do_mlas (void)
9123 {
9124   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9125   if (inst.operands[0].reg == inst.operands[1].reg
9126       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9127       && !(inst.instruction & 0x00400000))
9128     as_tsktsk (_("Rd and Rm should be different in mla"));
9129
9130   inst.instruction |= inst.operands[0].reg << 16;
9131   inst.instruction |= inst.operands[1].reg;
9132   inst.instruction |= inst.operands[2].reg << 8;
9133   inst.instruction |= inst.operands[3].reg << 12;
9134 }
9135
9136 static void
9137 do_mov (void)
9138 {
9139   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9140               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9141               THUMB1_RELOC_ONLY);
9142   inst.instruction |= inst.operands[0].reg << 12;
9143   encode_arm_shifter_operand (1);
9144 }
9145
9146 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9147 static void
9148 do_mov16 (void)
9149 {
9150   bfd_vma imm;
9151   bfd_boolean top;
9152
9153   top = (inst.instruction & 0x00400000) != 0;
9154   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
9155               _(":lower16: not allowed in this instruction"));
9156   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
9157               _(":upper16: not allowed in this instruction"));
9158   inst.instruction |= inst.operands[0].reg << 12;
9159   if (inst.reloc.type == BFD_RELOC_UNUSED)
9160     {
9161       imm = inst.reloc.exp.X_add_number;
9162       /* The value is in two pieces: 0:11, 16:19.  */
9163       inst.instruction |= (imm & 0x00000fff);
9164       inst.instruction |= (imm & 0x0000f000) << 4;
9165     }
9166 }
9167
9168 static int
9169 do_vfp_nsyn_mrs (void)
9170 {
9171   if (inst.operands[0].isvec)
9172     {
9173       if (inst.operands[1].reg != 1)
9174         first_error (_("operand 1 must be FPSCR"));
9175       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9176       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9177       do_vfp_nsyn_opcode ("fmstat");
9178     }
9179   else if (inst.operands[1].isvec)
9180     do_vfp_nsyn_opcode ("fmrx");
9181   else
9182     return FAIL;
9183
9184   return SUCCESS;
9185 }
9186
9187 static int
9188 do_vfp_nsyn_msr (void)
9189 {
9190   if (inst.operands[0].isvec)
9191     do_vfp_nsyn_opcode ("fmxr");
9192   else
9193     return FAIL;
9194
9195   return SUCCESS;
9196 }
9197
9198 static void
9199 do_vmrs (void)
9200 {
9201   unsigned Rt = inst.operands[0].reg;
9202
9203   if (thumb_mode && Rt == REG_SP)
9204     {
9205       inst.error = BAD_SP;
9206       return;
9207     }
9208
9209   /* MVFR2 is only valid at ARMv8-A.  */
9210   if (inst.operands[1].reg == 5)
9211     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9212                 _(BAD_FPU));
9213
9214   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9215   if (!inst.operands[0].isvec && Rt == REG_PC)
9216     {
9217       inst.error = BAD_PC;
9218       return;
9219     }
9220
9221   /* If we get through parsing the register name, we just insert the number
9222      generated into the instruction without further validation.  */
9223   inst.instruction |= (inst.operands[1].reg << 16);
9224   inst.instruction |= (Rt << 12);
9225 }
9226
9227 static void
9228 do_vmsr (void)
9229 {
9230   unsigned Rt = inst.operands[1].reg;
9231
9232   if (thumb_mode)
9233     reject_bad_reg (Rt);
9234   else if (Rt == REG_PC)
9235     {
9236       inst.error = BAD_PC;
9237       return;
9238     }
9239
9240   /* MVFR2 is only valid for ARMv8-A.  */
9241   if (inst.operands[0].reg == 5)
9242     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9243                 _(BAD_FPU));
9244
9245   /* If we get through parsing the register name, we just insert the number
9246      generated into the instruction without further validation.  */
9247   inst.instruction |= (inst.operands[0].reg << 16);
9248   inst.instruction |= (Rt << 12);
9249 }
9250
9251 static void
9252 do_mrs (void)
9253 {
9254   unsigned br;
9255
9256   if (do_vfp_nsyn_mrs () == SUCCESS)
9257     return;
9258
9259   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9260   inst.instruction |= inst.operands[0].reg << 12;
9261
9262   if (inst.operands[1].isreg)
9263     {
9264       br = inst.operands[1].reg;
9265       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
9266         as_bad (_("bad register for mrs"));
9267     }
9268   else
9269     {
9270       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9271       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9272                   != (PSR_c|PSR_f),
9273                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9274       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9275     }
9276
9277   inst.instruction |= br;
9278 }
9279
9280 /* Two possible forms:
9281       "{C|S}PSR_<field>, Rm",
9282       "{C|S}PSR_f, #expression".  */
9283
9284 static void
9285 do_msr (void)
9286 {
9287   if (do_vfp_nsyn_msr () == SUCCESS)
9288     return;
9289
9290   inst.instruction |= inst.operands[0].imm;
9291   if (inst.operands[1].isreg)
9292     inst.instruction |= inst.operands[1].reg;
9293   else
9294     {
9295       inst.instruction |= INST_IMMEDIATE;
9296       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
9297       inst.reloc.pc_rel = 0;
9298     }
9299 }
9300
9301 static void
9302 do_mul (void)
9303 {
9304   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9305
9306   if (!inst.operands[2].present)
9307     inst.operands[2].reg = inst.operands[0].reg;
9308   inst.instruction |= inst.operands[0].reg << 16;
9309   inst.instruction |= inst.operands[1].reg;
9310   inst.instruction |= inst.operands[2].reg << 8;
9311
9312   if (inst.operands[0].reg == inst.operands[1].reg
9313       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9314     as_tsktsk (_("Rd and Rm should be different in mul"));
9315 }
9316
9317 /* Long Multiply Parser
9318    UMULL RdLo, RdHi, Rm, Rs
9319    SMULL RdLo, RdHi, Rm, Rs
9320    UMLAL RdLo, RdHi, Rm, Rs
9321    SMLAL RdLo, RdHi, Rm, Rs.  */
9322
9323 static void
9324 do_mull (void)
9325 {
9326   inst.instruction |= inst.operands[0].reg << 12;
9327   inst.instruction |= inst.operands[1].reg << 16;
9328   inst.instruction |= inst.operands[2].reg;
9329   inst.instruction |= inst.operands[3].reg << 8;
9330
9331   /* rdhi and rdlo must be different.  */
9332   if (inst.operands[0].reg == inst.operands[1].reg)
9333     as_tsktsk (_("rdhi and rdlo must be different"));
9334
9335   /* rdhi, rdlo and rm must all be different before armv6.  */
9336   if ((inst.operands[0].reg == inst.operands[2].reg
9337       || inst.operands[1].reg == inst.operands[2].reg)
9338       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9339     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9340 }
9341
9342 static void
9343 do_nop (void)
9344 {
9345   if (inst.operands[0].present
9346       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9347     {
9348       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9349       inst.instruction &= 0xf0000000;
9350       inst.instruction |= 0x0320f000;
9351       if (inst.operands[0].present)
9352         inst.instruction |= inst.operands[0].imm;
9353     }
9354 }
9355
9356 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9357    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9358    Condition defaults to COND_ALWAYS.
9359    Error if Rd, Rn or Rm are R15.  */
9360
9361 static void
9362 do_pkhbt (void)
9363 {
9364   inst.instruction |= inst.operands[0].reg << 12;
9365   inst.instruction |= inst.operands[1].reg << 16;
9366   inst.instruction |= inst.operands[2].reg;
9367   if (inst.operands[3].present)
9368     encode_arm_shift (3);
9369 }
9370
9371 /* ARM V6 PKHTB (Argument Parse).  */
9372
9373 static void
9374 do_pkhtb (void)
9375 {
9376   if (!inst.operands[3].present)
9377     {
9378       /* If the shift specifier is omitted, turn the instruction
9379          into pkhbt rd, rm, rn. */
9380       inst.instruction &= 0xfff00010;
9381       inst.instruction |= inst.operands[0].reg << 12;
9382       inst.instruction |= inst.operands[1].reg;
9383       inst.instruction |= inst.operands[2].reg << 16;
9384     }
9385   else
9386     {
9387       inst.instruction |= inst.operands[0].reg << 12;
9388       inst.instruction |= inst.operands[1].reg << 16;
9389       inst.instruction |= inst.operands[2].reg;
9390       encode_arm_shift (3);
9391     }
9392 }
9393
9394 /* ARMv5TE: Preload-Cache
9395    MP Extensions: Preload for write
9396
9397     PLD(W) <addr_mode>
9398
9399   Syntactically, like LDR with B=1, W=0, L=1.  */
9400
9401 static void
9402 do_pld (void)
9403 {
9404   constraint (!inst.operands[0].isreg,
9405               _("'[' expected after PLD mnemonic"));
9406   constraint (inst.operands[0].postind,
9407               _("post-indexed expression used in preload instruction"));
9408   constraint (inst.operands[0].writeback,
9409               _("writeback used in preload instruction"));
9410   constraint (!inst.operands[0].preind,
9411               _("unindexed addressing used in preload instruction"));
9412   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9413 }
9414
9415 /* ARMv7: PLI <addr_mode>  */
9416 static void
9417 do_pli (void)
9418 {
9419   constraint (!inst.operands[0].isreg,
9420               _("'[' expected after PLI mnemonic"));
9421   constraint (inst.operands[0].postind,
9422               _("post-indexed expression used in preload instruction"));
9423   constraint (inst.operands[0].writeback,
9424               _("writeback used in preload instruction"));
9425   constraint (!inst.operands[0].preind,
9426               _("unindexed addressing used in preload instruction"));
9427   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9428   inst.instruction &= ~PRE_INDEX;
9429 }
9430
9431 static void
9432 do_push_pop (void)
9433 {
9434   constraint (inst.operands[0].writeback,
9435               _("push/pop do not support {reglist}^"));
9436   inst.operands[1] = inst.operands[0];
9437   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9438   inst.operands[0].isreg = 1;
9439   inst.operands[0].writeback = 1;
9440   inst.operands[0].reg = REG_SP;
9441   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9442 }
9443
9444 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9445    word at the specified address and the following word
9446    respectively.
9447    Unconditionally executed.
9448    Error if Rn is R15.  */
9449
9450 static void
9451 do_rfe (void)
9452 {
9453   inst.instruction |= inst.operands[0].reg << 16;
9454   if (inst.operands[0].writeback)
9455     inst.instruction |= WRITE_BACK;
9456 }
9457
9458 /* ARM V6 ssat (argument parse).  */
9459
9460 static void
9461 do_ssat (void)
9462 {
9463   inst.instruction |= inst.operands[0].reg << 12;
9464   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9465   inst.instruction |= inst.operands[2].reg;
9466
9467   if (inst.operands[3].present)
9468     encode_arm_shift (3);
9469 }
9470
9471 /* ARM V6 usat (argument parse).  */
9472
9473 static void
9474 do_usat (void)
9475 {
9476   inst.instruction |= inst.operands[0].reg << 12;
9477   inst.instruction |= inst.operands[1].imm << 16;
9478   inst.instruction |= inst.operands[2].reg;
9479
9480   if (inst.operands[3].present)
9481     encode_arm_shift (3);
9482 }
9483
9484 /* ARM V6 ssat16 (argument parse).  */
9485
9486 static void
9487 do_ssat16 (void)
9488 {
9489   inst.instruction |= inst.operands[0].reg << 12;
9490   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9491   inst.instruction |= inst.operands[2].reg;
9492 }
9493
9494 static void
9495 do_usat16 (void)
9496 {
9497   inst.instruction |= inst.operands[0].reg << 12;
9498   inst.instruction |= inst.operands[1].imm << 16;
9499   inst.instruction |= inst.operands[2].reg;
9500 }
9501
9502 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9503    preserving the other bits.
9504
9505    setend <endian_specifier>, where <endian_specifier> is either
9506    BE or LE.  */
9507
9508 static void
9509 do_setend (void)
9510 {
9511   if (warn_on_deprecated
9512       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9513       as_tsktsk (_("setend use is deprecated for ARMv8"));
9514
9515   if (inst.operands[0].imm)
9516     inst.instruction |= 0x200;
9517 }
9518
9519 static void
9520 do_shift (void)
9521 {
9522   unsigned int Rm = (inst.operands[1].present
9523                      ? inst.operands[1].reg
9524                      : inst.operands[0].reg);
9525
9526   inst.instruction |= inst.operands[0].reg << 12;
9527   inst.instruction |= Rm;
9528   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9529     {
9530       inst.instruction |= inst.operands[2].reg << 8;
9531       inst.instruction |= SHIFT_BY_REG;
9532       /* PR 12854: Error on extraneous shifts.  */
9533       constraint (inst.operands[2].shifted,
9534                   _("extraneous shift as part of operand to shift insn"));
9535     }
9536   else
9537     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9538 }
9539
9540 static void
9541 do_smc (void)
9542 {
9543   inst.reloc.type = BFD_RELOC_ARM_SMC;
9544   inst.reloc.pc_rel = 0;
9545 }
9546
9547 static void
9548 do_hvc (void)
9549 {
9550   inst.reloc.type = BFD_RELOC_ARM_HVC;
9551   inst.reloc.pc_rel = 0;
9552 }
9553
9554 static void
9555 do_swi (void)
9556 {
9557   inst.reloc.type = BFD_RELOC_ARM_SWI;
9558   inst.reloc.pc_rel = 0;
9559 }
9560
9561 static void
9562 do_setpan (void)
9563 {
9564   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9565               _("selected processor does not support SETPAN instruction"));
9566
9567   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9568 }
9569
9570 static void
9571 do_t_setpan (void)
9572 {
9573   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9574               _("selected processor does not support SETPAN instruction"));
9575
9576   inst.instruction |= (inst.operands[0].imm << 3);
9577 }
9578
9579 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9580    SMLAxy{cond} Rd,Rm,Rs,Rn
9581    SMLAWy{cond} Rd,Rm,Rs,Rn
9582    Error if any register is R15.  */
9583
9584 static void
9585 do_smla (void)
9586 {
9587   inst.instruction |= inst.operands[0].reg << 16;
9588   inst.instruction |= inst.operands[1].reg;
9589   inst.instruction |= inst.operands[2].reg << 8;
9590   inst.instruction |= inst.operands[3].reg << 12;
9591 }
9592
9593 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9594    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9595    Error if any register is R15.
9596    Warning if Rdlo == Rdhi.  */
9597
9598 static void
9599 do_smlal (void)
9600 {
9601   inst.instruction |= inst.operands[0].reg << 12;
9602   inst.instruction |= inst.operands[1].reg << 16;
9603   inst.instruction |= inst.operands[2].reg;
9604   inst.instruction |= inst.operands[3].reg << 8;
9605
9606   if (inst.operands[0].reg == inst.operands[1].reg)
9607     as_tsktsk (_("rdhi and rdlo must be different"));
9608 }
9609
9610 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9611    SMULxy{cond} Rd,Rm,Rs
9612    Error if any register is R15.  */
9613
9614 static void
9615 do_smul (void)
9616 {
9617   inst.instruction |= inst.operands[0].reg << 16;
9618   inst.instruction |= inst.operands[1].reg;
9619   inst.instruction |= inst.operands[2].reg << 8;
9620 }
9621
9622 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9623    the same for both ARM and Thumb-2.  */
9624
9625 static void
9626 do_srs (void)
9627 {
9628   int reg;
9629
9630   if (inst.operands[0].present)
9631     {
9632       reg = inst.operands[0].reg;
9633       constraint (reg != REG_SP, _("SRS base register must be r13"));
9634     }
9635   else
9636     reg = REG_SP;
9637
9638   inst.instruction |= reg << 16;
9639   inst.instruction |= inst.operands[1].imm;
9640   if (inst.operands[0].writeback || inst.operands[1].writeback)
9641     inst.instruction |= WRITE_BACK;
9642 }
9643
9644 /* ARM V6 strex (argument parse).  */
9645
9646 static void
9647 do_strex (void)
9648 {
9649   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9650               || inst.operands[2].postind || inst.operands[2].writeback
9651               || inst.operands[2].immisreg || inst.operands[2].shifted
9652               || inst.operands[2].negative
9653               /* See comment in do_ldrex().  */
9654               || (inst.operands[2].reg == REG_PC),
9655               BAD_ADDR_MODE);
9656
9657   constraint (inst.operands[0].reg == inst.operands[1].reg
9658               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9659
9660   constraint (inst.reloc.exp.X_op != O_constant
9661               || inst.reloc.exp.X_add_number != 0,
9662               _("offset must be zero in ARM encoding"));
9663
9664   inst.instruction |= inst.operands[0].reg << 12;
9665   inst.instruction |= inst.operands[1].reg;
9666   inst.instruction |= inst.operands[2].reg << 16;
9667   inst.reloc.type = BFD_RELOC_UNUSED;
9668 }
9669
9670 static void
9671 do_t_strexbh (void)
9672 {
9673   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9674               || inst.operands[2].postind || inst.operands[2].writeback
9675               || inst.operands[2].immisreg || inst.operands[2].shifted
9676               || inst.operands[2].negative,
9677               BAD_ADDR_MODE);
9678
9679   constraint (inst.operands[0].reg == inst.operands[1].reg
9680               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9681
9682   do_rm_rd_rn ();
9683 }
9684
9685 static void
9686 do_strexd (void)
9687 {
9688   constraint (inst.operands[1].reg % 2 != 0,
9689               _("even register required"));
9690   constraint (inst.operands[2].present
9691               && inst.operands[2].reg != inst.operands[1].reg + 1,
9692               _("can only store two consecutive registers"));
9693   /* If op 2 were present and equal to PC, this function wouldn't
9694      have been called in the first place.  */
9695   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9696
9697   constraint (inst.operands[0].reg == inst.operands[1].reg
9698               || inst.operands[0].reg == inst.operands[1].reg + 1
9699               || inst.operands[0].reg == inst.operands[3].reg,
9700               BAD_OVERLAP);
9701
9702   inst.instruction |= inst.operands[0].reg << 12;
9703   inst.instruction |= inst.operands[1].reg;
9704   inst.instruction |= inst.operands[3].reg << 16;
9705 }
9706
9707 /* ARM V8 STRL.  */
9708 static void
9709 do_stlex (void)
9710 {
9711   constraint (inst.operands[0].reg == inst.operands[1].reg
9712               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9713
9714   do_rd_rm_rn ();
9715 }
9716
9717 static void
9718 do_t_stlex (void)
9719 {
9720   constraint (inst.operands[0].reg == inst.operands[1].reg
9721               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9722
9723   do_rm_rd_rn ();
9724 }
9725
9726 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9727    extends it to 32-bits, and adds the result to a value in another
9728    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9729    before extracting the 16-bit value.
9730    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9731    Condition defaults to COND_ALWAYS.
9732    Error if any register uses R15.  */
9733
9734 static void
9735 do_sxtah (void)
9736 {
9737   inst.instruction |= inst.operands[0].reg << 12;
9738   inst.instruction |= inst.operands[1].reg << 16;
9739   inst.instruction |= inst.operands[2].reg;
9740   inst.instruction |= inst.operands[3].imm << 10;
9741 }
9742
9743 /* ARM V6 SXTH.
9744
9745    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9746    Condition defaults to COND_ALWAYS.
9747    Error if any register uses R15.  */
9748
9749 static void
9750 do_sxth (void)
9751 {
9752   inst.instruction |= inst.operands[0].reg << 12;
9753   inst.instruction |= inst.operands[1].reg;
9754   inst.instruction |= inst.operands[2].imm << 10;
9755 }
9756 \f
9757 /* VFP instructions.  In a logical order: SP variant first, monad
9758    before dyad, arithmetic then move then load/store.  */
9759
9760 static void
9761 do_vfp_sp_monadic (void)
9762 {
9763   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9764   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9765 }
9766
9767 static void
9768 do_vfp_sp_dyadic (void)
9769 {
9770   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9771   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9772   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9773 }
9774
9775 static void
9776 do_vfp_sp_compare_z (void)
9777 {
9778   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9779 }
9780
9781 static void
9782 do_vfp_dp_sp_cvt (void)
9783 {
9784   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9785   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9786 }
9787
9788 static void
9789 do_vfp_sp_dp_cvt (void)
9790 {
9791   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9792   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9793 }
9794
9795 static void
9796 do_vfp_reg_from_sp (void)
9797 {
9798   inst.instruction |= inst.operands[0].reg << 12;
9799   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9800 }
9801
9802 static void
9803 do_vfp_reg2_from_sp2 (void)
9804 {
9805   constraint (inst.operands[2].imm != 2,
9806               _("only two consecutive VFP SP registers allowed here"));
9807   inst.instruction |= inst.operands[0].reg << 12;
9808   inst.instruction |= inst.operands[1].reg << 16;
9809   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9810 }
9811
9812 static void
9813 do_vfp_sp_from_reg (void)
9814 {
9815   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9816   inst.instruction |= inst.operands[1].reg << 12;
9817 }
9818
9819 static void
9820 do_vfp_sp2_from_reg2 (void)
9821 {
9822   constraint (inst.operands[0].imm != 2,
9823               _("only two consecutive VFP SP registers allowed here"));
9824   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9825   inst.instruction |= inst.operands[1].reg << 12;
9826   inst.instruction |= inst.operands[2].reg << 16;
9827 }
9828
9829 static void
9830 do_vfp_sp_ldst (void)
9831 {
9832   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9833   encode_arm_cp_address (1, FALSE, TRUE, 0);
9834 }
9835
9836 static void
9837 do_vfp_dp_ldst (void)
9838 {
9839   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9840   encode_arm_cp_address (1, FALSE, TRUE, 0);
9841 }
9842
9843
9844 static void
9845 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9846 {
9847   if (inst.operands[0].writeback)
9848     inst.instruction |= WRITE_BACK;
9849   else
9850     constraint (ldstm_type != VFP_LDSTMIA,
9851                 _("this addressing mode requires base-register writeback"));
9852   inst.instruction |= inst.operands[0].reg << 16;
9853   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9854   inst.instruction |= inst.operands[1].imm;
9855 }
9856
9857 static void
9858 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9859 {
9860   int count;
9861
9862   if (inst.operands[0].writeback)
9863     inst.instruction |= WRITE_BACK;
9864   else
9865     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9866                 _("this addressing mode requires base-register writeback"));
9867
9868   inst.instruction |= inst.operands[0].reg << 16;
9869   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9870
9871   count = inst.operands[1].imm << 1;
9872   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9873     count += 1;
9874
9875   inst.instruction |= count;
9876 }
9877
9878 static void
9879 do_vfp_sp_ldstmia (void)
9880 {
9881   vfp_sp_ldstm (VFP_LDSTMIA);
9882 }
9883
9884 static void
9885 do_vfp_sp_ldstmdb (void)
9886 {
9887   vfp_sp_ldstm (VFP_LDSTMDB);
9888 }
9889
9890 static void
9891 do_vfp_dp_ldstmia (void)
9892 {
9893   vfp_dp_ldstm (VFP_LDSTMIA);
9894 }
9895
9896 static void
9897 do_vfp_dp_ldstmdb (void)
9898 {
9899   vfp_dp_ldstm (VFP_LDSTMDB);
9900 }
9901
9902 static void
9903 do_vfp_xp_ldstmia (void)
9904 {
9905   vfp_dp_ldstm (VFP_LDSTMIAX);
9906 }
9907
9908 static void
9909 do_vfp_xp_ldstmdb (void)
9910 {
9911   vfp_dp_ldstm (VFP_LDSTMDBX);
9912 }
9913
9914 static void
9915 do_vfp_dp_rd_rm (void)
9916 {
9917   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9918   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9919 }
9920
9921 static void
9922 do_vfp_dp_rn_rd (void)
9923 {
9924   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9925   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9926 }
9927
9928 static void
9929 do_vfp_dp_rd_rn (void)
9930 {
9931   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9932   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9933 }
9934
9935 static void
9936 do_vfp_dp_rd_rn_rm (void)
9937 {
9938   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9939   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9940   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9941 }
9942
9943 static void
9944 do_vfp_dp_rd (void)
9945 {
9946   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9947 }
9948
9949 static void
9950 do_vfp_dp_rm_rd_rn (void)
9951 {
9952   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9953   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9954   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9955 }
9956
9957 /* VFPv3 instructions.  */
9958 static void
9959 do_vfp_sp_const (void)
9960 {
9961   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9962   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9963   inst.instruction |= (inst.operands[1].imm & 0x0f);
9964 }
9965
9966 static void
9967 do_vfp_dp_const (void)
9968 {
9969   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9970   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9971   inst.instruction |= (inst.operands[1].imm & 0x0f);
9972 }
9973
9974 static void
9975 vfp_conv (int srcsize)
9976 {
9977   int immbits = srcsize - inst.operands[1].imm;
9978
9979   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9980     {
9981       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9982          i.e. immbits must be in range 0 - 16.  */
9983       inst.error = _("immediate value out of range, expected range [0, 16]");
9984       return;
9985     }
9986   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9987     {
9988       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9989          i.e. immbits must be in range 0 - 31.  */
9990       inst.error = _("immediate value out of range, expected range [1, 32]");
9991       return;
9992     }
9993
9994   inst.instruction |= (immbits & 1) << 5;
9995   inst.instruction |= (immbits >> 1);
9996 }
9997
9998 static void
9999 do_vfp_sp_conv_16 (void)
10000 {
10001   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10002   vfp_conv (16);
10003 }
10004
10005 static void
10006 do_vfp_dp_conv_16 (void)
10007 {
10008   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10009   vfp_conv (16);
10010 }
10011
10012 static void
10013 do_vfp_sp_conv_32 (void)
10014 {
10015   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
10016   vfp_conv (32);
10017 }
10018
10019 static void
10020 do_vfp_dp_conv_32 (void)
10021 {
10022   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10023   vfp_conv (32);
10024 }
10025 \f
10026 /* FPA instructions.  Also in a logical order.  */
10027
10028 static void
10029 do_fpa_cmp (void)
10030 {
10031   inst.instruction |= inst.operands[0].reg << 16;
10032   inst.instruction |= inst.operands[1].reg;
10033 }
10034
10035 static void
10036 do_fpa_ldmstm (void)
10037 {
10038   inst.instruction |= inst.operands[0].reg << 12;
10039   switch (inst.operands[1].imm)
10040     {
10041     case 1: inst.instruction |= CP_T_X;          break;
10042     case 2: inst.instruction |= CP_T_Y;          break;
10043     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10044     case 4:                                      break;
10045     default: abort ();
10046     }
10047
10048   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10049     {
10050       /* The instruction specified "ea" or "fd", so we can only accept
10051          [Rn]{!}.  The instruction does not really support stacking or
10052          unstacking, so we have to emulate these by setting appropriate
10053          bits and offsets.  */
10054       constraint (inst.reloc.exp.X_op != O_constant
10055                   || inst.reloc.exp.X_add_number != 0,
10056                   _("this instruction does not support indexing"));
10057
10058       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10059         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
10060
10061       if (!(inst.instruction & INDEX_UP))
10062         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
10063
10064       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10065         {
10066           inst.operands[2].preind = 0;
10067           inst.operands[2].postind = 1;
10068         }
10069     }
10070
10071   encode_arm_cp_address (2, TRUE, TRUE, 0);
10072 }
10073 \f
10074 /* iWMMXt instructions: strictly in alphabetical order.  */
10075
10076 static void
10077 do_iwmmxt_tandorc (void)
10078 {
10079   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10080 }
10081
10082 static void
10083 do_iwmmxt_textrc (void)
10084 {
10085   inst.instruction |= inst.operands[0].reg << 12;
10086   inst.instruction |= inst.operands[1].imm;
10087 }
10088
10089 static void
10090 do_iwmmxt_textrm (void)
10091 {
10092   inst.instruction |= inst.operands[0].reg << 12;
10093   inst.instruction |= inst.operands[1].reg << 16;
10094   inst.instruction |= inst.operands[2].imm;
10095 }
10096
10097 static void
10098 do_iwmmxt_tinsr (void)
10099 {
10100   inst.instruction |= inst.operands[0].reg << 16;
10101   inst.instruction |= inst.operands[1].reg << 12;
10102   inst.instruction |= inst.operands[2].imm;
10103 }
10104
10105 static void
10106 do_iwmmxt_tmia (void)
10107 {
10108   inst.instruction |= inst.operands[0].reg << 5;
10109   inst.instruction |= inst.operands[1].reg;
10110   inst.instruction |= inst.operands[2].reg << 12;
10111 }
10112
10113 static void
10114 do_iwmmxt_waligni (void)
10115 {
10116   inst.instruction |= inst.operands[0].reg << 12;
10117   inst.instruction |= inst.operands[1].reg << 16;
10118   inst.instruction |= inst.operands[2].reg;
10119   inst.instruction |= inst.operands[3].imm << 20;
10120 }
10121
10122 static void
10123 do_iwmmxt_wmerge (void)
10124 {
10125   inst.instruction |= inst.operands[0].reg << 12;
10126   inst.instruction |= inst.operands[1].reg << 16;
10127   inst.instruction |= inst.operands[2].reg;
10128   inst.instruction |= inst.operands[3].imm << 21;
10129 }
10130
10131 static void
10132 do_iwmmxt_wmov (void)
10133 {
10134   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10135   inst.instruction |= inst.operands[0].reg << 12;
10136   inst.instruction |= inst.operands[1].reg << 16;
10137   inst.instruction |= inst.operands[1].reg;
10138 }
10139
10140 static void
10141 do_iwmmxt_wldstbh (void)
10142 {
10143   int reloc;
10144   inst.instruction |= inst.operands[0].reg << 12;
10145   if (thumb_mode)
10146     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10147   else
10148     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10149   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10150 }
10151
10152 static void
10153 do_iwmmxt_wldstw (void)
10154 {
10155   /* RIWR_RIWC clears .isreg for a control register.  */
10156   if (!inst.operands[0].isreg)
10157     {
10158       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10159       inst.instruction |= 0xf0000000;
10160     }
10161
10162   inst.instruction |= inst.operands[0].reg << 12;
10163   encode_arm_cp_address (1, TRUE, TRUE, 0);
10164 }
10165
10166 static void
10167 do_iwmmxt_wldstd (void)
10168 {
10169   inst.instruction |= inst.operands[0].reg << 12;
10170   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10171       && inst.operands[1].immisreg)
10172     {
10173       inst.instruction &= ~0x1a000ff;
10174       inst.instruction |= (0xfU << 28);
10175       if (inst.operands[1].preind)
10176         inst.instruction |= PRE_INDEX;
10177       if (!inst.operands[1].negative)
10178         inst.instruction |= INDEX_UP;
10179       if (inst.operands[1].writeback)
10180         inst.instruction |= WRITE_BACK;
10181       inst.instruction |= inst.operands[1].reg << 16;
10182       inst.instruction |= inst.reloc.exp.X_add_number << 4;
10183       inst.instruction |= inst.operands[1].imm;
10184     }
10185   else
10186     encode_arm_cp_address (1, TRUE, FALSE, 0);
10187 }
10188
10189 static void
10190 do_iwmmxt_wshufh (void)
10191 {
10192   inst.instruction |= inst.operands[0].reg << 12;
10193   inst.instruction |= inst.operands[1].reg << 16;
10194   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10195   inst.instruction |= (inst.operands[2].imm & 0x0f);
10196 }
10197
10198 static void
10199 do_iwmmxt_wzero (void)
10200 {
10201   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10202   inst.instruction |= inst.operands[0].reg;
10203   inst.instruction |= inst.operands[0].reg << 12;
10204   inst.instruction |= inst.operands[0].reg << 16;
10205 }
10206
10207 static void
10208 do_iwmmxt_wrwrwr_or_imm5 (void)
10209 {
10210   if (inst.operands[2].isreg)
10211     do_rd_rn_rm ();
10212   else {
10213     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10214                 _("immediate operand requires iWMMXt2"));
10215     do_rd_rn ();
10216     if (inst.operands[2].imm == 0)
10217       {
10218         switch ((inst.instruction >> 20) & 0xf)
10219           {
10220           case 4:
10221           case 5:
10222           case 6:
10223           case 7:
10224             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10225             inst.operands[2].imm = 16;
10226             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10227             break;
10228           case 8:
10229           case 9:
10230           case 10:
10231           case 11:
10232             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10233             inst.operands[2].imm = 32;
10234             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10235             break;
10236           case 12:
10237           case 13:
10238           case 14:
10239           case 15:
10240             {
10241               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10242               unsigned long wrn;
10243               wrn = (inst.instruction >> 16) & 0xf;
10244               inst.instruction &= 0xff0fff0f;
10245               inst.instruction |= wrn;
10246               /* Bail out here; the instruction is now assembled.  */
10247               return;
10248             }
10249           }
10250       }
10251     /* Map 32 -> 0, etc.  */
10252     inst.operands[2].imm &= 0x1f;
10253     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10254   }
10255 }
10256 \f
10257 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10258    operations first, then control, shift, and load/store.  */
10259
10260 /* Insns like "foo X,Y,Z".  */
10261
10262 static void
10263 do_mav_triple (void)
10264 {
10265   inst.instruction |= inst.operands[0].reg << 16;
10266   inst.instruction |= inst.operands[1].reg;
10267   inst.instruction |= inst.operands[2].reg << 12;
10268 }
10269
10270 /* Insns like "foo W,X,Y,Z".
10271     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10272
10273 static void
10274 do_mav_quad (void)
10275 {
10276   inst.instruction |= inst.operands[0].reg << 5;
10277   inst.instruction |= inst.operands[1].reg << 12;
10278   inst.instruction |= inst.operands[2].reg << 16;
10279   inst.instruction |= inst.operands[3].reg;
10280 }
10281
10282 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10283 static void
10284 do_mav_dspsc (void)
10285 {
10286   inst.instruction |= inst.operands[1].reg << 12;
10287 }
10288
10289 /* Maverick shift immediate instructions.
10290    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10291    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10292
10293 static void
10294 do_mav_shift (void)
10295 {
10296   int imm = inst.operands[2].imm;
10297
10298   inst.instruction |= inst.operands[0].reg << 12;
10299   inst.instruction |= inst.operands[1].reg << 16;
10300
10301   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10302      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10303      Bit 4 should be 0.  */
10304   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10305
10306   inst.instruction |= imm;
10307 }
10308 \f
10309 /* XScale instructions.  Also sorted arithmetic before move.  */
10310
10311 /* Xscale multiply-accumulate (argument parse)
10312      MIAcc   acc0,Rm,Rs
10313      MIAPHcc acc0,Rm,Rs
10314      MIAxycc acc0,Rm,Rs.  */
10315
10316 static void
10317 do_xsc_mia (void)
10318 {
10319   inst.instruction |= inst.operands[1].reg;
10320   inst.instruction |= inst.operands[2].reg << 12;
10321 }
10322
10323 /* Xscale move-accumulator-register (argument parse)
10324
10325      MARcc   acc0,RdLo,RdHi.  */
10326
10327 static void
10328 do_xsc_mar (void)
10329 {
10330   inst.instruction |= inst.operands[1].reg << 12;
10331   inst.instruction |= inst.operands[2].reg << 16;
10332 }
10333
10334 /* Xscale move-register-accumulator (argument parse)
10335
10336      MRAcc   RdLo,RdHi,acc0.  */
10337
10338 static void
10339 do_xsc_mra (void)
10340 {
10341   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10342   inst.instruction |= inst.operands[0].reg << 12;
10343   inst.instruction |= inst.operands[1].reg << 16;
10344 }
10345 \f
10346 /* Encoding functions relevant only to Thumb.  */
10347
10348 /* inst.operands[i] is a shifted-register operand; encode
10349    it into inst.instruction in the format used by Thumb32.  */
10350
10351 static void
10352 encode_thumb32_shifted_operand (int i)
10353 {
10354   unsigned int value = inst.reloc.exp.X_add_number;
10355   unsigned int shift = inst.operands[i].shift_kind;
10356
10357   constraint (inst.operands[i].immisreg,
10358               _("shift by register not allowed in thumb mode"));
10359   inst.instruction |= inst.operands[i].reg;
10360   if (shift == SHIFT_RRX)
10361     inst.instruction |= SHIFT_ROR << 4;
10362   else
10363     {
10364       constraint (inst.reloc.exp.X_op != O_constant,
10365                   _("expression too complex"));
10366
10367       constraint (value > 32
10368                   || (value == 32 && (shift == SHIFT_LSL
10369                                       || shift == SHIFT_ROR)),
10370                   _("shift expression is too large"));
10371
10372       if (value == 0)
10373         shift = SHIFT_LSL;
10374       else if (value == 32)
10375         value = 0;
10376
10377       inst.instruction |= shift << 4;
10378       inst.instruction |= (value & 0x1c) << 10;
10379       inst.instruction |= (value & 0x03) << 6;
10380     }
10381 }
10382
10383
10384 /* inst.operands[i] was set up by parse_address.  Encode it into a
10385    Thumb32 format load or store instruction.  Reject forms that cannot
10386    be used with such instructions.  If is_t is true, reject forms that
10387    cannot be used with a T instruction; if is_d is true, reject forms
10388    that cannot be used with a D instruction.  If it is a store insn,
10389    reject PC in Rn.  */
10390
10391 static void
10392 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10393 {
10394   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10395
10396   constraint (!inst.operands[i].isreg,
10397               _("Instruction does not support =N addresses"));
10398
10399   inst.instruction |= inst.operands[i].reg << 16;
10400   if (inst.operands[i].immisreg)
10401     {
10402       constraint (is_pc, BAD_PC_ADDRESSING);
10403       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10404       constraint (inst.operands[i].negative,
10405                   _("Thumb does not support negative register indexing"));
10406       constraint (inst.operands[i].postind,
10407                   _("Thumb does not support register post-indexing"));
10408       constraint (inst.operands[i].writeback,
10409                   _("Thumb does not support register indexing with writeback"));
10410       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10411                   _("Thumb supports only LSL in shifted register indexing"));
10412
10413       inst.instruction |= inst.operands[i].imm;
10414       if (inst.operands[i].shifted)
10415         {
10416           constraint (inst.reloc.exp.X_op != O_constant,
10417                       _("expression too complex"));
10418           constraint (inst.reloc.exp.X_add_number < 0
10419                       || inst.reloc.exp.X_add_number > 3,
10420                       _("shift out of range"));
10421           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10422         }
10423       inst.reloc.type = BFD_RELOC_UNUSED;
10424     }
10425   else if (inst.operands[i].preind)
10426     {
10427       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10428       constraint (is_t && inst.operands[i].writeback,
10429                   _("cannot use writeback with this instruction"));
10430       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10431                   BAD_PC_ADDRESSING);
10432
10433       if (is_d)
10434         {
10435           inst.instruction |= 0x01000000;
10436           if (inst.operands[i].writeback)
10437             inst.instruction |= 0x00200000;
10438         }
10439       else
10440         {
10441           inst.instruction |= 0x00000c00;
10442           if (inst.operands[i].writeback)
10443             inst.instruction |= 0x00000100;
10444         }
10445       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10446     }
10447   else if (inst.operands[i].postind)
10448     {
10449       gas_assert (inst.operands[i].writeback);
10450       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10451       constraint (is_t, _("cannot use post-indexing with this instruction"));
10452
10453       if (is_d)
10454         inst.instruction |= 0x00200000;
10455       else
10456         inst.instruction |= 0x00000900;
10457       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10458     }
10459   else /* unindexed - only for coprocessor */
10460     inst.error = _("instruction does not accept unindexed addressing");
10461 }
10462
10463 /* Table of Thumb instructions which exist in both 16- and 32-bit
10464    encodings (the latter only in post-V6T2 cores).  The index is the
10465    value used in the insns table below.  When there is more than one
10466    possible 16-bit encoding for the instruction, this table always
10467    holds variant (1).
10468    Also contains several pseudo-instructions used during relaxation.  */
10469 #define T16_32_TAB                              \
10470   X(_adc,   4140, eb400000),                    \
10471   X(_adcs,  4140, eb500000),                    \
10472   X(_add,   1c00, eb000000),                    \
10473   X(_adds,  1c00, eb100000),                    \
10474   X(_addi,  0000, f1000000),                    \
10475   X(_addis, 0000, f1100000),                    \
10476   X(_add_pc,000f, f20f0000),                    \
10477   X(_add_sp,000d, f10d0000),                    \
10478   X(_adr,   000f, f20f0000),                    \
10479   X(_and,   4000, ea000000),                    \
10480   X(_ands,  4000, ea100000),                    \
10481   X(_asr,   1000, fa40f000),                    \
10482   X(_asrs,  1000, fa50f000),                    \
10483   X(_b,     e000, f000b000),                    \
10484   X(_bcond, d000, f0008000),                    \
10485   X(_bic,   4380, ea200000),                    \
10486   X(_bics,  4380, ea300000),                    \
10487   X(_cmn,   42c0, eb100f00),                    \
10488   X(_cmp,   2800, ebb00f00),                    \
10489   X(_cpsie, b660, f3af8400),                    \
10490   X(_cpsid, b670, f3af8600),                    \
10491   X(_cpy,   4600, ea4f0000),                    \
10492   X(_dec_sp,80dd, f1ad0d00),                    \
10493   X(_eor,   4040, ea800000),                    \
10494   X(_eors,  4040, ea900000),                    \
10495   X(_inc_sp,00dd, f10d0d00),                    \
10496   X(_ldmia, c800, e8900000),                    \
10497   X(_ldr,   6800, f8500000),                    \
10498   X(_ldrb,  7800, f8100000),                    \
10499   X(_ldrh,  8800, f8300000),                    \
10500   X(_ldrsb, 5600, f9100000),                    \
10501   X(_ldrsh, 5e00, f9300000),                    \
10502   X(_ldr_pc,4800, f85f0000),                    \
10503   X(_ldr_pc2,4800, f85f0000),                   \
10504   X(_ldr_sp,9800, f85d0000),                    \
10505   X(_lsl,   0000, fa00f000),                    \
10506   X(_lsls,  0000, fa10f000),                    \
10507   X(_lsr,   0800, fa20f000),                    \
10508   X(_lsrs,  0800, fa30f000),                    \
10509   X(_mov,   2000, ea4f0000),                    \
10510   X(_movs,  2000, ea5f0000),                    \
10511   X(_mul,   4340, fb00f000),                     \
10512   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10513   X(_mvn,   43c0, ea6f0000),                    \
10514   X(_mvns,  43c0, ea7f0000),                    \
10515   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10516   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10517   X(_orr,   4300, ea400000),                    \
10518   X(_orrs,  4300, ea500000),                    \
10519   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10520   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10521   X(_rev,   ba00, fa90f080),                    \
10522   X(_rev16, ba40, fa90f090),                    \
10523   X(_revsh, bac0, fa90f0b0),                    \
10524   X(_ror,   41c0, fa60f000),                    \
10525   X(_rors,  41c0, fa70f000),                    \
10526   X(_sbc,   4180, eb600000),                    \
10527   X(_sbcs,  4180, eb700000),                    \
10528   X(_stmia, c000, e8800000),                    \
10529   X(_str,   6000, f8400000),                    \
10530   X(_strb,  7000, f8000000),                    \
10531   X(_strh,  8000, f8200000),                    \
10532   X(_str_sp,9000, f84d0000),                    \
10533   X(_sub,   1e00, eba00000),                    \
10534   X(_subs,  1e00, ebb00000),                    \
10535   X(_subi,  8000, f1a00000),                    \
10536   X(_subis, 8000, f1b00000),                    \
10537   X(_sxtb,  b240, fa4ff080),                    \
10538   X(_sxth,  b200, fa0ff080),                    \
10539   X(_tst,   4200, ea100f00),                    \
10540   X(_uxtb,  b2c0, fa5ff080),                    \
10541   X(_uxth,  b280, fa1ff080),                    \
10542   X(_nop,   bf00, f3af8000),                    \
10543   X(_yield, bf10, f3af8001),                    \
10544   X(_wfe,   bf20, f3af8002),                    \
10545   X(_wfi,   bf30, f3af8003),                    \
10546   X(_sev,   bf40, f3af8004),                    \
10547   X(_sevl,  bf50, f3af8005),                    \
10548   X(_udf,   de00, f7f0a000)
10549
10550 /* To catch errors in encoding functions, the codes are all offset by
10551    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10552    as 16-bit instructions.  */
10553 #define X(a,b,c) T_MNEM##a
10554 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10555 #undef X
10556
10557 #define X(a,b,c) 0x##b
10558 static const unsigned short thumb_op16[] = { T16_32_TAB };
10559 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10560 #undef X
10561
10562 #define X(a,b,c) 0x##c
10563 static const unsigned int thumb_op32[] = { T16_32_TAB };
10564 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10565 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10566 #undef X
10567 #undef T16_32_TAB
10568
10569 /* Thumb instruction encoders, in alphabetical order.  */
10570
10571 /* ADDW or SUBW.  */
10572
10573 static void
10574 do_t_add_sub_w (void)
10575 {
10576   int Rd, Rn;
10577
10578   Rd = inst.operands[0].reg;
10579   Rn = inst.operands[1].reg;
10580
10581   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10582      is the SP-{plus,minus}-immediate form of the instruction.  */
10583   if (Rn == REG_SP)
10584     constraint (Rd == REG_PC, BAD_PC);
10585   else
10586     reject_bad_reg (Rd);
10587
10588   inst.instruction |= (Rn << 16) | (Rd << 8);
10589   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10590 }
10591
10592 /* Parse an add or subtract instruction.  We get here with inst.instruction
10593    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10594
10595 static void
10596 do_t_add_sub (void)
10597 {
10598   int Rd, Rs, Rn;
10599
10600   Rd = inst.operands[0].reg;
10601   Rs = (inst.operands[1].present
10602         ? inst.operands[1].reg    /* Rd, Rs, foo */
10603         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10604
10605   if (Rd == REG_PC)
10606     set_it_insn_type_last ();
10607
10608   if (unified_syntax)
10609     {
10610       bfd_boolean flags;
10611       bfd_boolean narrow;
10612       int opcode;
10613
10614       flags = (inst.instruction == T_MNEM_adds
10615                || inst.instruction == T_MNEM_subs);
10616       if (flags)
10617         narrow = !in_it_block ();
10618       else
10619         narrow = in_it_block ();
10620       if (!inst.operands[2].isreg)
10621         {
10622           int add;
10623
10624           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10625             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10626
10627           add = (inst.instruction == T_MNEM_add
10628                  || inst.instruction == T_MNEM_adds);
10629           opcode = 0;
10630           if (inst.size_req != 4)
10631             {
10632               /* Attempt to use a narrow opcode, with relaxation if
10633                  appropriate.  */
10634               if (Rd == REG_SP && Rs == REG_SP && !flags)
10635                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10636               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10637                 opcode = T_MNEM_add_sp;
10638               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10639                 opcode = T_MNEM_add_pc;
10640               else if (Rd <= 7 && Rs <= 7 && narrow)
10641                 {
10642                   if (flags)
10643                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10644                   else
10645                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10646                 }
10647               if (opcode)
10648                 {
10649                   inst.instruction = THUMB_OP16(opcode);
10650                   inst.instruction |= (Rd << 4) | Rs;
10651                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10652                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
10653                   {
10654                     if (inst.size_req == 2)
10655                       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10656                     else
10657                       inst.relax = opcode;
10658                   }
10659                 }
10660               else
10661                 constraint (inst.size_req == 2, BAD_HIREG);
10662             }
10663           if (inst.size_req == 4
10664               || (inst.size_req != 2 && !opcode))
10665             {
10666               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10667                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
10668                           THUMB1_RELOC_ONLY);
10669               if (Rd == REG_PC)
10670                 {
10671                   constraint (add, BAD_PC);
10672                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10673                              _("only SUBS PC, LR, #const allowed"));
10674                   constraint (inst.reloc.exp.X_op != O_constant,
10675                               _("expression too complex"));
10676                   constraint (inst.reloc.exp.X_add_number < 0
10677                               || inst.reloc.exp.X_add_number > 0xff,
10678                              _("immediate value out of range"));
10679                   inst.instruction = T2_SUBS_PC_LR
10680                                      | inst.reloc.exp.X_add_number;
10681                   inst.reloc.type = BFD_RELOC_UNUSED;
10682                   return;
10683                 }
10684               else if (Rs == REG_PC)
10685                 {
10686                   /* Always use addw/subw.  */
10687                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10688                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10689                 }
10690               else
10691                 {
10692                   inst.instruction = THUMB_OP32 (inst.instruction);
10693                   inst.instruction = (inst.instruction & 0xe1ffffff)
10694                                      | 0x10000000;
10695                   if (flags)
10696                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10697                   else
10698                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10699                 }
10700               inst.instruction |= Rd << 8;
10701               inst.instruction |= Rs << 16;
10702             }
10703         }
10704       else
10705         {
10706           unsigned int value = inst.reloc.exp.X_add_number;
10707           unsigned int shift = inst.operands[2].shift_kind;
10708
10709           Rn = inst.operands[2].reg;
10710           /* See if we can do this with a 16-bit instruction.  */
10711           if (!inst.operands[2].shifted && inst.size_req != 4)
10712             {
10713               if (Rd > 7 || Rs > 7 || Rn > 7)
10714                 narrow = FALSE;
10715
10716               if (narrow)
10717                 {
10718                   inst.instruction = ((inst.instruction == T_MNEM_adds
10719                                        || inst.instruction == T_MNEM_add)
10720                                       ? T_OPCODE_ADD_R3
10721                                       : T_OPCODE_SUB_R3);
10722                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10723                   return;
10724                 }
10725
10726               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10727                 {
10728                   /* Thumb-1 cores (except v6-M) require at least one high
10729                      register in a narrow non flag setting add.  */
10730                   if (Rd > 7 || Rn > 7
10731                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10732                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10733                     {
10734                       if (Rd == Rn)
10735                         {
10736                           Rn = Rs;
10737                           Rs = Rd;
10738                         }
10739                       inst.instruction = T_OPCODE_ADD_HI;
10740                       inst.instruction |= (Rd & 8) << 4;
10741                       inst.instruction |= (Rd & 7);
10742                       inst.instruction |= Rn << 3;
10743                       return;
10744                     }
10745                 }
10746             }
10747
10748           constraint (Rd == REG_PC, BAD_PC);
10749           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10750             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10751           constraint (Rs == REG_PC, BAD_PC);
10752           reject_bad_reg (Rn);
10753
10754           /* If we get here, it can't be done in 16 bits.  */
10755           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10756                       _("shift must be constant"));
10757           inst.instruction = THUMB_OP32 (inst.instruction);
10758           inst.instruction |= Rd << 8;
10759           inst.instruction |= Rs << 16;
10760           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10761                       _("shift value over 3 not allowed in thumb mode"));
10762           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10763                       _("only LSL shift allowed in thumb mode"));
10764           encode_thumb32_shifted_operand (2);
10765         }
10766     }
10767   else
10768     {
10769       constraint (inst.instruction == T_MNEM_adds
10770                   || inst.instruction == T_MNEM_subs,
10771                   BAD_THUMB32);
10772
10773       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10774         {
10775           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10776                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10777                       BAD_HIREG);
10778
10779           inst.instruction = (inst.instruction == T_MNEM_add
10780                               ? 0x0000 : 0x8000);
10781           inst.instruction |= (Rd << 4) | Rs;
10782           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10783           return;
10784         }
10785
10786       Rn = inst.operands[2].reg;
10787       constraint (inst.operands[2].shifted, _("unshifted register required"));
10788
10789       /* We now have Rd, Rs, and Rn set to registers.  */
10790       if (Rd > 7 || Rs > 7 || Rn > 7)
10791         {
10792           /* Can't do this for SUB.      */
10793           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10794           inst.instruction = T_OPCODE_ADD_HI;
10795           inst.instruction |= (Rd & 8) << 4;
10796           inst.instruction |= (Rd & 7);
10797           if (Rs == Rd)
10798             inst.instruction |= Rn << 3;
10799           else if (Rn == Rd)
10800             inst.instruction |= Rs << 3;
10801           else
10802             constraint (1, _("dest must overlap one source register"));
10803         }
10804       else
10805         {
10806           inst.instruction = (inst.instruction == T_MNEM_add
10807                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10808           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10809         }
10810     }
10811 }
10812
10813 static void
10814 do_t_adr (void)
10815 {
10816   unsigned Rd;
10817
10818   Rd = inst.operands[0].reg;
10819   reject_bad_reg (Rd);
10820
10821   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10822     {
10823       /* Defer to section relaxation.  */
10824       inst.relax = inst.instruction;
10825       inst.instruction = THUMB_OP16 (inst.instruction);
10826       inst.instruction |= Rd << 4;
10827     }
10828   else if (unified_syntax && inst.size_req != 2)
10829     {
10830       /* Generate a 32-bit opcode.  */
10831       inst.instruction = THUMB_OP32 (inst.instruction);
10832       inst.instruction |= Rd << 8;
10833       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10834       inst.reloc.pc_rel = 1;
10835     }
10836   else
10837     {
10838       /* Generate a 16-bit opcode.  */
10839       inst.instruction = THUMB_OP16 (inst.instruction);
10840       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10841       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10842       inst.reloc.pc_rel = 1;
10843       inst.instruction |= Rd << 4;
10844     }
10845
10846   if (inst.reloc.exp.X_op == O_symbol
10847       && inst.reloc.exp.X_add_symbol != NULL
10848       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10849       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10850     inst.reloc.exp.X_add_number += 1;
10851 }
10852
10853 /* Arithmetic instructions for which there is just one 16-bit
10854    instruction encoding, and it allows only two low registers.
10855    For maximal compatibility with ARM syntax, we allow three register
10856    operands even when Thumb-32 instructions are not available, as long
10857    as the first two are identical.  For instance, both "sbc r0,r1" and
10858    "sbc r0,r0,r1" are allowed.  */
10859 static void
10860 do_t_arit3 (void)
10861 {
10862   int Rd, Rs, Rn;
10863
10864   Rd = inst.operands[0].reg;
10865   Rs = (inst.operands[1].present
10866         ? inst.operands[1].reg    /* Rd, Rs, foo */
10867         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10868   Rn = inst.operands[2].reg;
10869
10870   reject_bad_reg (Rd);
10871   reject_bad_reg (Rs);
10872   if (inst.operands[2].isreg)
10873     reject_bad_reg (Rn);
10874
10875   if (unified_syntax)
10876     {
10877       if (!inst.operands[2].isreg)
10878         {
10879           /* For an immediate, we always generate a 32-bit opcode;
10880              section relaxation will shrink it later if possible.  */
10881           inst.instruction = THUMB_OP32 (inst.instruction);
10882           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10883           inst.instruction |= Rd << 8;
10884           inst.instruction |= Rs << 16;
10885           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10886         }
10887       else
10888         {
10889           bfd_boolean narrow;
10890
10891           /* See if we can do this with a 16-bit instruction.  */
10892           if (THUMB_SETS_FLAGS (inst.instruction))
10893             narrow = !in_it_block ();
10894           else
10895             narrow = in_it_block ();
10896
10897           if (Rd > 7 || Rn > 7 || Rs > 7)
10898             narrow = FALSE;
10899           if (inst.operands[2].shifted)
10900             narrow = FALSE;
10901           if (inst.size_req == 4)
10902             narrow = FALSE;
10903
10904           if (narrow
10905               && Rd == Rs)
10906             {
10907               inst.instruction = THUMB_OP16 (inst.instruction);
10908               inst.instruction |= Rd;
10909               inst.instruction |= Rn << 3;
10910               return;
10911             }
10912
10913           /* If we get here, it can't be done in 16 bits.  */
10914           constraint (inst.operands[2].shifted
10915                       && inst.operands[2].immisreg,
10916                       _("shift must be constant"));
10917           inst.instruction = THUMB_OP32 (inst.instruction);
10918           inst.instruction |= Rd << 8;
10919           inst.instruction |= Rs << 16;
10920           encode_thumb32_shifted_operand (2);
10921         }
10922     }
10923   else
10924     {
10925       /* On its face this is a lie - the instruction does set the
10926          flags.  However, the only supported mnemonic in this mode
10927          says it doesn't.  */
10928       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10929
10930       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10931                   _("unshifted register required"));
10932       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10933       constraint (Rd != Rs,
10934                   _("dest and source1 must be the same register"));
10935
10936       inst.instruction = THUMB_OP16 (inst.instruction);
10937       inst.instruction |= Rd;
10938       inst.instruction |= Rn << 3;
10939     }
10940 }
10941
10942 /* Similarly, but for instructions where the arithmetic operation is
10943    commutative, so we can allow either of them to be different from
10944    the destination operand in a 16-bit instruction.  For instance, all
10945    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10946    accepted.  */
10947 static void
10948 do_t_arit3c (void)
10949 {
10950   int Rd, Rs, Rn;
10951
10952   Rd = inst.operands[0].reg;
10953   Rs = (inst.operands[1].present
10954         ? inst.operands[1].reg    /* Rd, Rs, foo */
10955         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10956   Rn = inst.operands[2].reg;
10957
10958   reject_bad_reg (Rd);
10959   reject_bad_reg (Rs);
10960   if (inst.operands[2].isreg)
10961     reject_bad_reg (Rn);
10962
10963   if (unified_syntax)
10964     {
10965       if (!inst.operands[2].isreg)
10966         {
10967           /* For an immediate, we always generate a 32-bit opcode;
10968              section relaxation will shrink it later if possible.  */
10969           inst.instruction = THUMB_OP32 (inst.instruction);
10970           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10971           inst.instruction |= Rd << 8;
10972           inst.instruction |= Rs << 16;
10973           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10974         }
10975       else
10976         {
10977           bfd_boolean narrow;
10978
10979           /* See if we can do this with a 16-bit instruction.  */
10980           if (THUMB_SETS_FLAGS (inst.instruction))
10981             narrow = !in_it_block ();
10982           else
10983             narrow = in_it_block ();
10984
10985           if (Rd > 7 || Rn > 7 || Rs > 7)
10986             narrow = FALSE;
10987           if (inst.operands[2].shifted)
10988             narrow = FALSE;
10989           if (inst.size_req == 4)
10990             narrow = FALSE;
10991
10992           if (narrow)
10993             {
10994               if (Rd == Rs)
10995                 {
10996                   inst.instruction = THUMB_OP16 (inst.instruction);
10997                   inst.instruction |= Rd;
10998                   inst.instruction |= Rn << 3;
10999                   return;
11000                 }
11001               if (Rd == Rn)
11002                 {
11003                   inst.instruction = THUMB_OP16 (inst.instruction);
11004                   inst.instruction |= Rd;
11005                   inst.instruction |= Rs << 3;
11006                   return;
11007                 }
11008             }
11009
11010           /* If we get here, it can't be done in 16 bits.  */
11011           constraint (inst.operands[2].shifted
11012                       && inst.operands[2].immisreg,
11013                       _("shift must be constant"));
11014           inst.instruction = THUMB_OP32 (inst.instruction);
11015           inst.instruction |= Rd << 8;
11016           inst.instruction |= Rs << 16;
11017           encode_thumb32_shifted_operand (2);
11018         }
11019     }
11020   else
11021     {
11022       /* On its face this is a lie - the instruction does set the
11023          flags.  However, the only supported mnemonic in this mode
11024          says it doesn't.  */
11025       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11026
11027       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11028                   _("unshifted register required"));
11029       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11030
11031       inst.instruction = THUMB_OP16 (inst.instruction);
11032       inst.instruction |= Rd;
11033
11034       if (Rd == Rs)
11035         inst.instruction |= Rn << 3;
11036       else if (Rd == Rn)
11037         inst.instruction |= Rs << 3;
11038       else
11039         constraint (1, _("dest must overlap one source register"));
11040     }
11041 }
11042
11043 static void
11044 do_t_bfc (void)
11045 {
11046   unsigned Rd;
11047   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11048   constraint (msb > 32, _("bit-field extends past end of register"));
11049   /* The instruction encoding stores the LSB and MSB,
11050      not the LSB and width.  */
11051   Rd = inst.operands[0].reg;
11052   reject_bad_reg (Rd);
11053   inst.instruction |= Rd << 8;
11054   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11055   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11056   inst.instruction |= msb - 1;
11057 }
11058
11059 static void
11060 do_t_bfi (void)
11061 {
11062   int Rd, Rn;
11063   unsigned int msb;
11064
11065   Rd = inst.operands[0].reg;
11066   reject_bad_reg (Rd);
11067
11068   /* #0 in second position is alternative syntax for bfc, which is
11069      the same instruction but with REG_PC in the Rm field.  */
11070   if (!inst.operands[1].isreg)
11071     Rn = REG_PC;
11072   else
11073     {
11074       Rn = inst.operands[1].reg;
11075       reject_bad_reg (Rn);
11076     }
11077
11078   msb = inst.operands[2].imm + inst.operands[3].imm;
11079   constraint (msb > 32, _("bit-field extends past end of register"));
11080   /* The instruction encoding stores the LSB and MSB,
11081      not the LSB and width.  */
11082   inst.instruction |= Rd << 8;
11083   inst.instruction |= Rn << 16;
11084   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11085   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11086   inst.instruction |= msb - 1;
11087 }
11088
11089 static void
11090 do_t_bfx (void)
11091 {
11092   unsigned Rd, Rn;
11093
11094   Rd = inst.operands[0].reg;
11095   Rn = inst.operands[1].reg;
11096
11097   reject_bad_reg (Rd);
11098   reject_bad_reg (Rn);
11099
11100   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11101               _("bit-field extends past end of register"));
11102   inst.instruction |= Rd << 8;
11103   inst.instruction |= Rn << 16;
11104   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11105   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11106   inst.instruction |= inst.operands[3].imm - 1;
11107 }
11108
11109 /* ARM V5 Thumb BLX (argument parse)
11110         BLX <target_addr>       which is BLX(1)
11111         BLX <Rm>                which is BLX(2)
11112    Unfortunately, there are two different opcodes for this mnemonic.
11113    So, the insns[].value is not used, and the code here zaps values
11114         into inst.instruction.
11115
11116    ??? How to take advantage of the additional two bits of displacement
11117    available in Thumb32 mode?  Need new relocation?  */
11118
11119 static void
11120 do_t_blx (void)
11121 {
11122   set_it_insn_type_last ();
11123
11124   if (inst.operands[0].isreg)
11125     {
11126       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11127       /* We have a register, so this is BLX(2).  */
11128       inst.instruction |= inst.operands[0].reg << 3;
11129     }
11130   else
11131     {
11132       /* No register.  This must be BLX(1).  */
11133       inst.instruction = 0xf000e800;
11134       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11135     }
11136 }
11137
11138 static void
11139 do_t_branch (void)
11140 {
11141   int opcode;
11142   int cond;
11143   bfd_reloc_code_real_type reloc;
11144
11145   cond = inst.cond;
11146   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
11147
11148   if (in_it_block ())
11149     {
11150       /* Conditional branches inside IT blocks are encoded as unconditional
11151          branches.  */
11152       cond = COND_ALWAYS;
11153     }
11154   else
11155     cond = inst.cond;
11156
11157   if (cond != COND_ALWAYS)
11158     opcode = T_MNEM_bcond;
11159   else
11160     opcode = inst.instruction;
11161
11162   if (unified_syntax
11163       && (inst.size_req == 4
11164           || (inst.size_req != 2
11165               && (inst.operands[0].hasreloc
11166                   || inst.reloc.exp.X_op == O_constant))))
11167     {
11168       inst.instruction = THUMB_OP32(opcode);
11169       if (cond == COND_ALWAYS)
11170         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11171       else
11172         {
11173           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11174                       _("selected architecture does not support "
11175                         "wide conditional branch instruction"));
11176
11177           gas_assert (cond != 0xF);
11178           inst.instruction |= cond << 22;
11179           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11180         }
11181     }
11182   else
11183     {
11184       inst.instruction = THUMB_OP16(opcode);
11185       if (cond == COND_ALWAYS)
11186         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11187       else
11188         {
11189           inst.instruction |= cond << 8;
11190           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11191         }
11192       /* Allow section relaxation.  */
11193       if (unified_syntax && inst.size_req != 2)
11194         inst.relax = opcode;
11195     }
11196   inst.reloc.type = reloc;
11197   inst.reloc.pc_rel = 1;
11198 }
11199
11200 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11201    between the two is the maximum immediate allowed - which is passed in
11202    RANGE.  */
11203 static void
11204 do_t_bkpt_hlt1 (int range)
11205 {
11206   constraint (inst.cond != COND_ALWAYS,
11207               _("instruction is always unconditional"));
11208   if (inst.operands[0].present)
11209     {
11210       constraint (inst.operands[0].imm > range,
11211                   _("immediate value out of range"));
11212       inst.instruction |= inst.operands[0].imm;
11213     }
11214
11215   set_it_insn_type (NEUTRAL_IT_INSN);
11216 }
11217
11218 static void
11219 do_t_hlt (void)
11220 {
11221   do_t_bkpt_hlt1 (63);
11222 }
11223
11224 static void
11225 do_t_bkpt (void)
11226 {
11227   do_t_bkpt_hlt1 (255);
11228 }
11229
11230 static void
11231 do_t_branch23 (void)
11232 {
11233   set_it_insn_type_last ();
11234   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11235
11236   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11237      this file.  We used to simply ignore the PLT reloc type here --
11238      the branch encoding is now needed to deal with TLSCALL relocs.
11239      So if we see a PLT reloc now, put it back to how it used to be to
11240      keep the preexisting behaviour.  */
11241   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
11242     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11243
11244 #if defined(OBJ_COFF)
11245   /* If the destination of the branch is a defined symbol which does not have
11246      the THUMB_FUNC attribute, then we must be calling a function which has
11247      the (interfacearm) attribute.  We look for the Thumb entry point to that
11248      function and change the branch to refer to that function instead.  */
11249   if (   inst.reloc.exp.X_op == O_symbol
11250       && inst.reloc.exp.X_add_symbol != NULL
11251       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
11252       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
11253     inst.reloc.exp.X_add_symbol =
11254       find_real_start (inst.reloc.exp.X_add_symbol);
11255 #endif
11256 }
11257
11258 static void
11259 do_t_bx (void)
11260 {
11261   set_it_insn_type_last ();
11262   inst.instruction |= inst.operands[0].reg << 3;
11263   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11264      should cause the alignment to be checked once it is known.  This is
11265      because BX PC only works if the instruction is word aligned.  */
11266 }
11267
11268 static void
11269 do_t_bxj (void)
11270 {
11271   int Rm;
11272
11273   set_it_insn_type_last ();
11274   Rm = inst.operands[0].reg;
11275   reject_bad_reg (Rm);
11276   inst.instruction |= Rm << 16;
11277 }
11278
11279 static void
11280 do_t_clz (void)
11281 {
11282   unsigned Rd;
11283   unsigned Rm;
11284
11285   Rd = inst.operands[0].reg;
11286   Rm = inst.operands[1].reg;
11287
11288   reject_bad_reg (Rd);
11289   reject_bad_reg (Rm);
11290
11291   inst.instruction |= Rd << 8;
11292   inst.instruction |= Rm << 16;
11293   inst.instruction |= Rm;
11294 }
11295
11296 static void
11297 do_t_csdb (void)
11298 {
11299   set_it_insn_type (OUTSIDE_IT_INSN);
11300 }
11301
11302 static void
11303 do_t_cps (void)
11304 {
11305   set_it_insn_type (OUTSIDE_IT_INSN);
11306   inst.instruction |= inst.operands[0].imm;
11307 }
11308
11309 static void
11310 do_t_cpsi (void)
11311 {
11312   set_it_insn_type (OUTSIDE_IT_INSN);
11313   if (unified_syntax
11314       && (inst.operands[1].present || inst.size_req == 4)
11315       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11316     {
11317       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11318       inst.instruction = 0xf3af8000;
11319       inst.instruction |= imod << 9;
11320       inst.instruction |= inst.operands[0].imm << 5;
11321       if (inst.operands[1].present)
11322         inst.instruction |= 0x100 | inst.operands[1].imm;
11323     }
11324   else
11325     {
11326       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11327                   && (inst.operands[0].imm & 4),
11328                   _("selected processor does not support 'A' form "
11329                     "of this instruction"));
11330       constraint (inst.operands[1].present || inst.size_req == 4,
11331                   _("Thumb does not support the 2-argument "
11332                     "form of this instruction"));
11333       inst.instruction |= inst.operands[0].imm;
11334     }
11335 }
11336
11337 /* THUMB CPY instruction (argument parse).  */
11338
11339 static void
11340 do_t_cpy (void)
11341 {
11342   if (inst.size_req == 4)
11343     {
11344       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11345       inst.instruction |= inst.operands[0].reg << 8;
11346       inst.instruction |= inst.operands[1].reg;
11347     }
11348   else
11349     {
11350       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11351       inst.instruction |= (inst.operands[0].reg & 0x7);
11352       inst.instruction |= inst.operands[1].reg << 3;
11353     }
11354 }
11355
11356 static void
11357 do_t_cbz (void)
11358 {
11359   set_it_insn_type (OUTSIDE_IT_INSN);
11360   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11361   inst.instruction |= inst.operands[0].reg;
11362   inst.reloc.pc_rel = 1;
11363   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11364 }
11365
11366 static void
11367 do_t_dbg (void)
11368 {
11369   inst.instruction |= inst.operands[0].imm;
11370 }
11371
11372 static void
11373 do_t_div (void)
11374 {
11375   unsigned Rd, Rn, Rm;
11376
11377   Rd = inst.operands[0].reg;
11378   Rn = (inst.operands[1].present
11379         ? inst.operands[1].reg : Rd);
11380   Rm = inst.operands[2].reg;
11381
11382   reject_bad_reg (Rd);
11383   reject_bad_reg (Rn);
11384   reject_bad_reg (Rm);
11385
11386   inst.instruction |= Rd << 8;
11387   inst.instruction |= Rn << 16;
11388   inst.instruction |= Rm;
11389 }
11390
11391 static void
11392 do_t_hint (void)
11393 {
11394   if (unified_syntax && inst.size_req == 4)
11395     inst.instruction = THUMB_OP32 (inst.instruction);
11396   else
11397     inst.instruction = THUMB_OP16 (inst.instruction);
11398 }
11399
11400 static void
11401 do_t_it (void)
11402 {
11403   unsigned int cond = inst.operands[0].imm;
11404
11405   set_it_insn_type (IT_INSN);
11406   now_it.mask = (inst.instruction & 0xf) | 0x10;
11407   now_it.cc = cond;
11408   now_it.warn_deprecated = FALSE;
11409
11410   /* If the condition is a negative condition, invert the mask.  */
11411   if ((cond & 0x1) == 0x0)
11412     {
11413       unsigned int mask = inst.instruction & 0x000f;
11414
11415       if ((mask & 0x7) == 0)
11416         {
11417           /* No conversion needed.  */
11418           now_it.block_length = 1;
11419         }
11420       else if ((mask & 0x3) == 0)
11421         {
11422           mask ^= 0x8;
11423           now_it.block_length = 2;
11424         }
11425       else if ((mask & 0x1) == 0)
11426         {
11427           mask ^= 0xC;
11428           now_it.block_length = 3;
11429         }
11430       else
11431         {
11432           mask ^= 0xE;
11433           now_it.block_length = 4;
11434         }
11435
11436       inst.instruction &= 0xfff0;
11437       inst.instruction |= mask;
11438     }
11439
11440   inst.instruction |= cond << 4;
11441 }
11442
11443 /* Helper function used for both push/pop and ldm/stm.  */
11444 static void
11445 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11446 {
11447   bfd_boolean load;
11448
11449   load = (inst.instruction & (1 << 20)) != 0;
11450
11451   if (mask & (1 << 13))
11452     inst.error =  _("SP not allowed in register list");
11453
11454   if ((mask & (1 << base)) != 0
11455       && writeback)
11456     inst.error = _("having the base register in the register list when "
11457                    "using write back is UNPREDICTABLE");
11458
11459   if (load)
11460     {
11461       if (mask & (1 << 15))
11462         {
11463           if (mask & (1 << 14))
11464             inst.error = _("LR and PC should not both be in register list");
11465           else
11466             set_it_insn_type_last ();
11467         }
11468     }
11469   else
11470     {
11471       if (mask & (1 << 15))
11472         inst.error = _("PC not allowed in register list");
11473     }
11474
11475   if ((mask & (mask - 1)) == 0)
11476     {
11477       /* Single register transfers implemented as str/ldr.  */
11478       if (writeback)
11479         {
11480           if (inst.instruction & (1 << 23))
11481             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11482           else
11483             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11484         }
11485       else
11486         {
11487           if (inst.instruction & (1 << 23))
11488             inst.instruction = 0x00800000; /* ia -> [base] */
11489           else
11490             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11491         }
11492
11493       inst.instruction |= 0xf8400000;
11494       if (load)
11495         inst.instruction |= 0x00100000;
11496
11497       mask = ffs (mask) - 1;
11498       mask <<= 12;
11499     }
11500   else if (writeback)
11501     inst.instruction |= WRITE_BACK;
11502
11503   inst.instruction |= mask;
11504   inst.instruction |= base << 16;
11505 }
11506
11507 static void
11508 do_t_ldmstm (void)
11509 {
11510   /* This really doesn't seem worth it.  */
11511   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11512               _("expression too complex"));
11513   constraint (inst.operands[1].writeback,
11514               _("Thumb load/store multiple does not support {reglist}^"));
11515
11516   if (unified_syntax)
11517     {
11518       bfd_boolean narrow;
11519       unsigned mask;
11520
11521       narrow = FALSE;
11522       /* See if we can use a 16-bit instruction.  */
11523       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11524           && inst.size_req != 4
11525           && !(inst.operands[1].imm & ~0xff))
11526         {
11527           mask = 1 << inst.operands[0].reg;
11528
11529           if (inst.operands[0].reg <= 7)
11530             {
11531               if (inst.instruction == T_MNEM_stmia
11532                   ? inst.operands[0].writeback
11533                   : (inst.operands[0].writeback
11534                      == !(inst.operands[1].imm & mask)))
11535                 {
11536                   if (inst.instruction == T_MNEM_stmia
11537                       && (inst.operands[1].imm & mask)
11538                       && (inst.operands[1].imm & (mask - 1)))
11539                     as_warn (_("value stored for r%d is UNKNOWN"),
11540                              inst.operands[0].reg);
11541
11542                   inst.instruction = THUMB_OP16 (inst.instruction);
11543                   inst.instruction |= inst.operands[0].reg << 8;
11544                   inst.instruction |= inst.operands[1].imm;
11545                   narrow = TRUE;
11546                 }
11547               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11548                 {
11549                   /* This means 1 register in reg list one of 3 situations:
11550                      1. Instruction is stmia, but without writeback.
11551                      2. lmdia without writeback, but with Rn not in
11552                         reglist.
11553                      3. ldmia with writeback, but with Rn in reglist.
11554                      Case 3 is UNPREDICTABLE behaviour, so we handle
11555                      case 1 and 2 which can be converted into a 16-bit
11556                      str or ldr. The SP cases are handled below.  */
11557                   unsigned long opcode;
11558                   /* First, record an error for Case 3.  */
11559                   if (inst.operands[1].imm & mask
11560                       && inst.operands[0].writeback)
11561                     inst.error =
11562                         _("having the base register in the register list when "
11563                           "using write back is UNPREDICTABLE");
11564
11565                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11566                                                              : T_MNEM_ldr);
11567                   inst.instruction = THUMB_OP16 (opcode);
11568                   inst.instruction |= inst.operands[0].reg << 3;
11569                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11570                   narrow = TRUE;
11571                 }
11572             }
11573           else if (inst.operands[0] .reg == REG_SP)
11574             {
11575               if (inst.operands[0].writeback)
11576                 {
11577                   inst.instruction =
11578                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11579                                     ? T_MNEM_push : T_MNEM_pop);
11580                   inst.instruction |= inst.operands[1].imm;
11581                   narrow = TRUE;
11582                 }
11583               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11584                 {
11585                   inst.instruction =
11586                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11587                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11588                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11589                   narrow = TRUE;
11590                 }
11591             }
11592         }
11593
11594       if (!narrow)
11595         {
11596           if (inst.instruction < 0xffff)
11597             inst.instruction = THUMB_OP32 (inst.instruction);
11598
11599           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11600                                 inst.operands[0].writeback);
11601         }
11602     }
11603   else
11604     {
11605       constraint (inst.operands[0].reg > 7
11606                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11607       constraint (inst.instruction != T_MNEM_ldmia
11608                   && inst.instruction != T_MNEM_stmia,
11609                   _("Thumb-2 instruction only valid in unified syntax"));
11610       if (inst.instruction == T_MNEM_stmia)
11611         {
11612           if (!inst.operands[0].writeback)
11613             as_warn (_("this instruction will write back the base register"));
11614           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11615               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11616             as_warn (_("value stored for r%d is UNKNOWN"),
11617                      inst.operands[0].reg);
11618         }
11619       else
11620         {
11621           if (!inst.operands[0].writeback
11622               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11623             as_warn (_("this instruction will write back the base register"));
11624           else if (inst.operands[0].writeback
11625                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11626             as_warn (_("this instruction will not write back the base register"));
11627         }
11628
11629       inst.instruction = THUMB_OP16 (inst.instruction);
11630       inst.instruction |= inst.operands[0].reg << 8;
11631       inst.instruction |= inst.operands[1].imm;
11632     }
11633 }
11634
11635 static void
11636 do_t_ldrex (void)
11637 {
11638   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11639               || inst.operands[1].postind || inst.operands[1].writeback
11640               || inst.operands[1].immisreg || inst.operands[1].shifted
11641               || inst.operands[1].negative,
11642               BAD_ADDR_MODE);
11643
11644   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11645
11646   inst.instruction |= inst.operands[0].reg << 12;
11647   inst.instruction |= inst.operands[1].reg << 16;
11648   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11649 }
11650
11651 static void
11652 do_t_ldrexd (void)
11653 {
11654   if (!inst.operands[1].present)
11655     {
11656       constraint (inst.operands[0].reg == REG_LR,
11657                   _("r14 not allowed as first register "
11658                     "when second register is omitted"));
11659       inst.operands[1].reg = inst.operands[0].reg + 1;
11660     }
11661   constraint (inst.operands[0].reg == inst.operands[1].reg,
11662               BAD_OVERLAP);
11663
11664   inst.instruction |= inst.operands[0].reg << 12;
11665   inst.instruction |= inst.operands[1].reg << 8;
11666   inst.instruction |= inst.operands[2].reg << 16;
11667 }
11668
11669 static void
11670 do_t_ldst (void)
11671 {
11672   unsigned long opcode;
11673   int Rn;
11674
11675   if (inst.operands[0].isreg
11676       && !inst.operands[0].preind
11677       && inst.operands[0].reg == REG_PC)
11678     set_it_insn_type_last ();
11679
11680   opcode = inst.instruction;
11681   if (unified_syntax)
11682     {
11683       if (!inst.operands[1].isreg)
11684         {
11685           if (opcode <= 0xffff)
11686             inst.instruction = THUMB_OP32 (opcode);
11687           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11688             return;
11689         }
11690       if (inst.operands[1].isreg
11691           && !inst.operands[1].writeback
11692           && !inst.operands[1].shifted && !inst.operands[1].postind
11693           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11694           && opcode <= 0xffff
11695           && inst.size_req != 4)
11696         {
11697           /* Insn may have a 16-bit form.  */
11698           Rn = inst.operands[1].reg;
11699           if (inst.operands[1].immisreg)
11700             {
11701               inst.instruction = THUMB_OP16 (opcode);
11702               /* [Rn, Rik] */
11703               if (Rn <= 7 && inst.operands[1].imm <= 7)
11704                 goto op16;
11705               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11706                 reject_bad_reg (inst.operands[1].imm);
11707             }
11708           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11709                     && opcode != T_MNEM_ldrsb)
11710                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11711                    || (Rn == REG_SP && opcode == T_MNEM_str))
11712             {
11713               /* [Rn, #const] */
11714               if (Rn > 7)
11715                 {
11716                   if (Rn == REG_PC)
11717                     {
11718                       if (inst.reloc.pc_rel)
11719                         opcode = T_MNEM_ldr_pc2;
11720                       else
11721                         opcode = T_MNEM_ldr_pc;
11722                     }
11723                   else
11724                     {
11725                       if (opcode == T_MNEM_ldr)
11726                         opcode = T_MNEM_ldr_sp;
11727                       else
11728                         opcode = T_MNEM_str_sp;
11729                     }
11730                   inst.instruction = inst.operands[0].reg << 8;
11731                 }
11732               else
11733                 {
11734                   inst.instruction = inst.operands[0].reg;
11735                   inst.instruction |= inst.operands[1].reg << 3;
11736                 }
11737               inst.instruction |= THUMB_OP16 (opcode);
11738               if (inst.size_req == 2)
11739                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11740               else
11741                 inst.relax = opcode;
11742               return;
11743             }
11744         }
11745       /* Definitely a 32-bit variant.  */
11746
11747       /* Warning for Erratum 752419.  */
11748       if (opcode == T_MNEM_ldr
11749           && inst.operands[0].reg == REG_SP
11750           && inst.operands[1].writeback == 1
11751           && !inst.operands[1].immisreg)
11752         {
11753           if (no_cpu_selected ()
11754               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11755                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11756                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11757             as_warn (_("This instruction may be unpredictable "
11758                        "if executed on M-profile cores "
11759                        "with interrupts enabled."));
11760         }
11761
11762       /* Do some validations regarding addressing modes.  */
11763       if (inst.operands[1].immisreg)
11764         reject_bad_reg (inst.operands[1].imm);
11765
11766       constraint (inst.operands[1].writeback == 1
11767                   && inst.operands[0].reg == inst.operands[1].reg,
11768                   BAD_OVERLAP);
11769
11770       inst.instruction = THUMB_OP32 (opcode);
11771       inst.instruction |= inst.operands[0].reg << 12;
11772       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11773       check_ldr_r15_aligned ();
11774       return;
11775     }
11776
11777   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11778
11779   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11780     {
11781       /* Only [Rn,Rm] is acceptable.  */
11782       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11783       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11784                   || inst.operands[1].postind || inst.operands[1].shifted
11785                   || inst.operands[1].negative,
11786                   _("Thumb does not support this addressing mode"));
11787       inst.instruction = THUMB_OP16 (inst.instruction);
11788       goto op16;
11789     }
11790
11791   inst.instruction = THUMB_OP16 (inst.instruction);
11792   if (!inst.operands[1].isreg)
11793     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11794       return;
11795
11796   constraint (!inst.operands[1].preind
11797               || inst.operands[1].shifted
11798               || inst.operands[1].writeback,
11799               _("Thumb does not support this addressing mode"));
11800   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11801     {
11802       constraint (inst.instruction & 0x0600,
11803                   _("byte or halfword not valid for base register"));
11804       constraint (inst.operands[1].reg == REG_PC
11805                   && !(inst.instruction & THUMB_LOAD_BIT),
11806                   _("r15 based store not allowed"));
11807       constraint (inst.operands[1].immisreg,
11808                   _("invalid base register for register offset"));
11809
11810       if (inst.operands[1].reg == REG_PC)
11811         inst.instruction = T_OPCODE_LDR_PC;
11812       else if (inst.instruction & THUMB_LOAD_BIT)
11813         inst.instruction = T_OPCODE_LDR_SP;
11814       else
11815         inst.instruction = T_OPCODE_STR_SP;
11816
11817       inst.instruction |= inst.operands[0].reg << 8;
11818       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11819       return;
11820     }
11821
11822   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11823   if (!inst.operands[1].immisreg)
11824     {
11825       /* Immediate offset.  */
11826       inst.instruction |= inst.operands[0].reg;
11827       inst.instruction |= inst.operands[1].reg << 3;
11828       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11829       return;
11830     }
11831
11832   /* Register offset.  */
11833   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11834   constraint (inst.operands[1].negative,
11835               _("Thumb does not support this addressing mode"));
11836
11837  op16:
11838   switch (inst.instruction)
11839     {
11840     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11841     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11842     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11843     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11844     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11845     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11846     case 0x5600 /* ldrsb */:
11847     case 0x5e00 /* ldrsh */: break;
11848     default: abort ();
11849     }
11850
11851   inst.instruction |= inst.operands[0].reg;
11852   inst.instruction |= inst.operands[1].reg << 3;
11853   inst.instruction |= inst.operands[1].imm << 6;
11854 }
11855
11856 static void
11857 do_t_ldstd (void)
11858 {
11859   if (!inst.operands[1].present)
11860     {
11861       inst.operands[1].reg = inst.operands[0].reg + 1;
11862       constraint (inst.operands[0].reg == REG_LR,
11863                   _("r14 not allowed here"));
11864       constraint (inst.operands[0].reg == REG_R12,
11865                   _("r12 not allowed here"));
11866     }
11867
11868   if (inst.operands[2].writeback
11869       && (inst.operands[0].reg == inst.operands[2].reg
11870       || inst.operands[1].reg == inst.operands[2].reg))
11871     as_warn (_("base register written back, and overlaps "
11872                "one of transfer registers"));
11873
11874   inst.instruction |= inst.operands[0].reg << 12;
11875   inst.instruction |= inst.operands[1].reg << 8;
11876   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11877 }
11878
11879 static void
11880 do_t_ldstt (void)
11881 {
11882   inst.instruction |= inst.operands[0].reg << 12;
11883   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11884 }
11885
11886 static void
11887 do_t_mla (void)
11888 {
11889   unsigned Rd, Rn, Rm, Ra;
11890
11891   Rd = inst.operands[0].reg;
11892   Rn = inst.operands[1].reg;
11893   Rm = inst.operands[2].reg;
11894   Ra = inst.operands[3].reg;
11895
11896   reject_bad_reg (Rd);
11897   reject_bad_reg (Rn);
11898   reject_bad_reg (Rm);
11899   reject_bad_reg (Ra);
11900
11901   inst.instruction |= Rd << 8;
11902   inst.instruction |= Rn << 16;
11903   inst.instruction |= Rm;
11904   inst.instruction |= Ra << 12;
11905 }
11906
11907 static void
11908 do_t_mlal (void)
11909 {
11910   unsigned RdLo, RdHi, Rn, Rm;
11911
11912   RdLo = inst.operands[0].reg;
11913   RdHi = inst.operands[1].reg;
11914   Rn = inst.operands[2].reg;
11915   Rm = inst.operands[3].reg;
11916
11917   reject_bad_reg (RdLo);
11918   reject_bad_reg (RdHi);
11919   reject_bad_reg (Rn);
11920   reject_bad_reg (Rm);
11921
11922   inst.instruction |= RdLo << 12;
11923   inst.instruction |= RdHi << 8;
11924   inst.instruction |= Rn << 16;
11925   inst.instruction |= Rm;
11926 }
11927
11928 static void
11929 do_t_mov_cmp (void)
11930 {
11931   unsigned Rn, Rm;
11932
11933   Rn = inst.operands[0].reg;
11934   Rm = inst.operands[1].reg;
11935
11936   if (Rn == REG_PC)
11937     set_it_insn_type_last ();
11938
11939   if (unified_syntax)
11940     {
11941       int r0off = (inst.instruction == T_MNEM_mov
11942                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11943       unsigned long opcode;
11944       bfd_boolean narrow;
11945       bfd_boolean low_regs;
11946
11947       low_regs = (Rn <= 7 && Rm <= 7);
11948       opcode = inst.instruction;
11949       if (in_it_block ())
11950         narrow = opcode != T_MNEM_movs;
11951       else
11952         narrow = opcode != T_MNEM_movs || low_regs;
11953       if (inst.size_req == 4
11954           || inst.operands[1].shifted)
11955         narrow = FALSE;
11956
11957       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11958       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11959           && !inst.operands[1].shifted
11960           && Rn == REG_PC
11961           && Rm == REG_LR)
11962         {
11963           inst.instruction = T2_SUBS_PC_LR;
11964           return;
11965         }
11966
11967       if (opcode == T_MNEM_cmp)
11968         {
11969           constraint (Rn == REG_PC, BAD_PC);
11970           if (narrow)
11971             {
11972               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11973                  but valid.  */
11974               warn_deprecated_sp (Rm);
11975               /* R15 was documented as a valid choice for Rm in ARMv6,
11976                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11977                  tools reject R15, so we do too.  */
11978               constraint (Rm == REG_PC, BAD_PC);
11979             }
11980           else
11981             reject_bad_reg (Rm);
11982         }
11983       else if (opcode == T_MNEM_mov
11984                || opcode == T_MNEM_movs)
11985         {
11986           if (inst.operands[1].isreg)
11987             {
11988               if (opcode == T_MNEM_movs)
11989                 {
11990                   reject_bad_reg (Rn);
11991                   reject_bad_reg (Rm);
11992                 }
11993               else if (narrow)
11994                 {
11995                   /* This is mov.n.  */
11996                   if ((Rn == REG_SP || Rn == REG_PC)
11997                       && (Rm == REG_SP || Rm == REG_PC))
11998                     {
11999                       as_tsktsk (_("Use of r%u as a source register is "
12000                                  "deprecated when r%u is the destination "
12001                                  "register."), Rm, Rn);
12002                     }
12003                 }
12004               else
12005                 {
12006                   /* This is mov.w.  */
12007                   constraint (Rn == REG_PC, BAD_PC);
12008                   constraint (Rm == REG_PC, BAD_PC);
12009                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12010                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
12011                 }
12012             }
12013           else
12014             reject_bad_reg (Rn);
12015         }
12016
12017       if (!inst.operands[1].isreg)
12018         {
12019           /* Immediate operand.  */
12020           if (!in_it_block () && opcode == T_MNEM_mov)
12021             narrow = 0;
12022           if (low_regs && narrow)
12023             {
12024               inst.instruction = THUMB_OP16 (opcode);
12025               inst.instruction |= Rn << 8;
12026               if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12027                   || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12028                 {
12029                   if (inst.size_req == 2)
12030                     inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12031                   else
12032                     inst.relax = opcode;
12033                 }
12034             }
12035           else
12036             {
12037               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12038                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
12039                           THUMB1_RELOC_ONLY);
12040
12041               inst.instruction = THUMB_OP32 (inst.instruction);
12042               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12043               inst.instruction |= Rn << r0off;
12044               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12045             }
12046         }
12047       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12048                && (inst.instruction == T_MNEM_mov
12049                    || inst.instruction == T_MNEM_movs))
12050         {
12051           /* Register shifts are encoded as separate shift instructions.  */
12052           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12053
12054           if (in_it_block ())
12055             narrow = !flags;
12056           else
12057             narrow = flags;
12058
12059           if (inst.size_req == 4)
12060             narrow = FALSE;
12061
12062           if (!low_regs || inst.operands[1].imm > 7)
12063             narrow = FALSE;
12064
12065           if (Rn != Rm)
12066             narrow = FALSE;
12067
12068           switch (inst.operands[1].shift_kind)
12069             {
12070             case SHIFT_LSL:
12071               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12072               break;
12073             case SHIFT_ASR:
12074               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12075               break;
12076             case SHIFT_LSR:
12077               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12078               break;
12079             case SHIFT_ROR:
12080               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12081               break;
12082             default:
12083               abort ();
12084             }
12085
12086           inst.instruction = opcode;
12087           if (narrow)
12088             {
12089               inst.instruction |= Rn;
12090               inst.instruction |= inst.operands[1].imm << 3;
12091             }
12092           else
12093             {
12094               if (flags)
12095                 inst.instruction |= CONDS_BIT;
12096
12097               inst.instruction |= Rn << 8;
12098               inst.instruction |= Rm << 16;
12099               inst.instruction |= inst.operands[1].imm;
12100             }
12101         }
12102       else if (!narrow)
12103         {
12104           /* Some mov with immediate shift have narrow variants.
12105              Register shifts are handled above.  */
12106           if (low_regs && inst.operands[1].shifted
12107               && (inst.instruction == T_MNEM_mov
12108                   || inst.instruction == T_MNEM_movs))
12109             {
12110               if (in_it_block ())
12111                 narrow = (inst.instruction == T_MNEM_mov);
12112               else
12113                 narrow = (inst.instruction == T_MNEM_movs);
12114             }
12115
12116           if (narrow)
12117             {
12118               switch (inst.operands[1].shift_kind)
12119                 {
12120                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12121                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12122                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12123                 default: narrow = FALSE; break;
12124                 }
12125             }
12126
12127           if (narrow)
12128             {
12129               inst.instruction |= Rn;
12130               inst.instruction |= Rm << 3;
12131               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12132             }
12133           else
12134             {
12135               inst.instruction = THUMB_OP32 (inst.instruction);
12136               inst.instruction |= Rn << r0off;
12137               encode_thumb32_shifted_operand (1);
12138             }
12139         }
12140       else
12141         switch (inst.instruction)
12142           {
12143           case T_MNEM_mov:
12144             /* In v4t or v5t a move of two lowregs produces unpredictable
12145                results. Don't allow this.  */
12146             if (low_regs)
12147               {
12148                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12149                             "MOV Rd, Rs with two low registers is not "
12150                             "permitted on this architecture");
12151                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12152                                         arm_ext_v6);
12153               }
12154
12155             inst.instruction = T_OPCODE_MOV_HR;
12156             inst.instruction |= (Rn & 0x8) << 4;
12157             inst.instruction |= (Rn & 0x7);
12158             inst.instruction |= Rm << 3;
12159             break;
12160
12161           case T_MNEM_movs:
12162             /* We know we have low registers at this point.
12163                Generate LSLS Rd, Rs, #0.  */
12164             inst.instruction = T_OPCODE_LSL_I;
12165             inst.instruction |= Rn;
12166             inst.instruction |= Rm << 3;
12167             break;
12168
12169           case T_MNEM_cmp:
12170             if (low_regs)
12171               {
12172                 inst.instruction = T_OPCODE_CMP_LR;
12173                 inst.instruction |= Rn;
12174                 inst.instruction |= Rm << 3;
12175               }
12176             else
12177               {
12178                 inst.instruction = T_OPCODE_CMP_HR;
12179                 inst.instruction |= (Rn & 0x8) << 4;
12180                 inst.instruction |= (Rn & 0x7);
12181                 inst.instruction |= Rm << 3;
12182               }
12183             break;
12184           }
12185       return;
12186     }
12187
12188   inst.instruction = THUMB_OP16 (inst.instruction);
12189
12190   /* PR 10443: Do not silently ignore shifted operands.  */
12191   constraint (inst.operands[1].shifted,
12192               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12193
12194   if (inst.operands[1].isreg)
12195     {
12196       if (Rn < 8 && Rm < 8)
12197         {
12198           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12199              since a MOV instruction produces unpredictable results.  */
12200           if (inst.instruction == T_OPCODE_MOV_I8)
12201             inst.instruction = T_OPCODE_ADD_I3;
12202           else
12203             inst.instruction = T_OPCODE_CMP_LR;
12204
12205           inst.instruction |= Rn;
12206           inst.instruction |= Rm << 3;
12207         }
12208       else
12209         {
12210           if (inst.instruction == T_OPCODE_MOV_I8)
12211             inst.instruction = T_OPCODE_MOV_HR;
12212           else
12213             inst.instruction = T_OPCODE_CMP_HR;
12214           do_t_cpy ();
12215         }
12216     }
12217   else
12218     {
12219       constraint (Rn > 7,
12220                   _("only lo regs allowed with immediate"));
12221       inst.instruction |= Rn << 8;
12222       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12223     }
12224 }
12225
12226 static void
12227 do_t_mov16 (void)
12228 {
12229   unsigned Rd;
12230   bfd_vma imm;
12231   bfd_boolean top;
12232
12233   top = (inst.instruction & 0x00800000) != 0;
12234   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
12235     {
12236       constraint (top, _(":lower16: not allowed in this instruction"));
12237       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
12238     }
12239   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
12240     {
12241       constraint (!top, _(":upper16: not allowed in this instruction"));
12242       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
12243     }
12244
12245   Rd = inst.operands[0].reg;
12246   reject_bad_reg (Rd);
12247
12248   inst.instruction |= Rd << 8;
12249   if (inst.reloc.type == BFD_RELOC_UNUSED)
12250     {
12251       imm = inst.reloc.exp.X_add_number;
12252       inst.instruction |= (imm & 0xf000) << 4;
12253       inst.instruction |= (imm & 0x0800) << 15;
12254       inst.instruction |= (imm & 0x0700) << 4;
12255       inst.instruction |= (imm & 0x00ff);
12256     }
12257 }
12258
12259 static void
12260 do_t_mvn_tst (void)
12261 {
12262   unsigned Rn, Rm;
12263
12264   Rn = inst.operands[0].reg;
12265   Rm = inst.operands[1].reg;
12266
12267   if (inst.instruction == T_MNEM_cmp
12268       || inst.instruction == T_MNEM_cmn)
12269     constraint (Rn == REG_PC, BAD_PC);
12270   else
12271     reject_bad_reg (Rn);
12272   reject_bad_reg (Rm);
12273
12274   if (unified_syntax)
12275     {
12276       int r0off = (inst.instruction == T_MNEM_mvn
12277                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12278       bfd_boolean narrow;
12279
12280       if (inst.size_req == 4
12281           || inst.instruction > 0xffff
12282           || inst.operands[1].shifted
12283           || Rn > 7 || Rm > 7)
12284         narrow = FALSE;
12285       else if (inst.instruction == T_MNEM_cmn
12286                || inst.instruction == T_MNEM_tst)
12287         narrow = TRUE;
12288       else if (THUMB_SETS_FLAGS (inst.instruction))
12289         narrow = !in_it_block ();
12290       else
12291         narrow = in_it_block ();
12292
12293       if (!inst.operands[1].isreg)
12294         {
12295           /* For an immediate, we always generate a 32-bit opcode;
12296              section relaxation will shrink it later if possible.  */
12297           if (inst.instruction < 0xffff)
12298             inst.instruction = THUMB_OP32 (inst.instruction);
12299           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12300           inst.instruction |= Rn << r0off;
12301           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12302         }
12303       else
12304         {
12305           /* See if we can do this with a 16-bit instruction.  */
12306           if (narrow)
12307             {
12308               inst.instruction = THUMB_OP16 (inst.instruction);
12309               inst.instruction |= Rn;
12310               inst.instruction |= Rm << 3;
12311             }
12312           else
12313             {
12314               constraint (inst.operands[1].shifted
12315                           && inst.operands[1].immisreg,
12316                           _("shift must be constant"));
12317               if (inst.instruction < 0xffff)
12318                 inst.instruction = THUMB_OP32 (inst.instruction);
12319               inst.instruction |= Rn << r0off;
12320               encode_thumb32_shifted_operand (1);
12321             }
12322         }
12323     }
12324   else
12325     {
12326       constraint (inst.instruction > 0xffff
12327                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12328       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12329                   _("unshifted register required"));
12330       constraint (Rn > 7 || Rm > 7,
12331                   BAD_HIREG);
12332
12333       inst.instruction = THUMB_OP16 (inst.instruction);
12334       inst.instruction |= Rn;
12335       inst.instruction |= Rm << 3;
12336     }
12337 }
12338
12339 static void
12340 do_t_mrs (void)
12341 {
12342   unsigned Rd;
12343
12344   if (do_vfp_nsyn_mrs () == SUCCESS)
12345     return;
12346
12347   Rd = inst.operands[0].reg;
12348   reject_bad_reg (Rd);
12349   inst.instruction |= Rd << 8;
12350
12351   if (inst.operands[1].isreg)
12352     {
12353       unsigned br = inst.operands[1].reg;
12354       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12355         as_bad (_("bad register for mrs"));
12356
12357       inst.instruction |= br & (0xf << 16);
12358       inst.instruction |= (br & 0x300) >> 4;
12359       inst.instruction |= (br & SPSR_BIT) >> 2;
12360     }
12361   else
12362     {
12363       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12364
12365       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12366         {
12367           /* PR gas/12698:  The constraint is only applied for m_profile.
12368              If the user has specified -march=all, we want to ignore it as
12369              we are building for any CPU type, including non-m variants.  */
12370           bfd_boolean m_profile =
12371             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12372           constraint ((flags != 0) && m_profile, _("selected processor does "
12373                                                    "not support requested special purpose register"));
12374         }
12375       else
12376         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12377            devices).  */
12378         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12379                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12380
12381       inst.instruction |= (flags & SPSR_BIT) >> 2;
12382       inst.instruction |= inst.operands[1].imm & 0xff;
12383       inst.instruction |= 0xf0000;
12384     }
12385 }
12386
12387 static void
12388 do_t_msr (void)
12389 {
12390   int flags;
12391   unsigned Rn;
12392
12393   if (do_vfp_nsyn_msr () == SUCCESS)
12394     return;
12395
12396   constraint (!inst.operands[1].isreg,
12397               _("Thumb encoding does not support an immediate here"));
12398
12399   if (inst.operands[0].isreg)
12400     flags = (int)(inst.operands[0].reg);
12401   else
12402     flags = inst.operands[0].imm;
12403
12404   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12405     {
12406       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12407
12408       /* PR gas/12698:  The constraint is only applied for m_profile.
12409          If the user has specified -march=all, we want to ignore it as
12410          we are building for any CPU type, including non-m variants.  */
12411       bfd_boolean m_profile =
12412         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12413       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12414            && (bits & ~(PSR_s | PSR_f)) != 0)
12415           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12416               && bits != PSR_f)) && m_profile,
12417           _("selected processor does not support requested special "
12418             "purpose register"));
12419     }
12420   else
12421      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12422                  "requested special purpose register"));
12423
12424   Rn = inst.operands[1].reg;
12425   reject_bad_reg (Rn);
12426
12427   inst.instruction |= (flags & SPSR_BIT) >> 2;
12428   inst.instruction |= (flags & 0xf0000) >> 8;
12429   inst.instruction |= (flags & 0x300) >> 4;
12430   inst.instruction |= (flags & 0xff);
12431   inst.instruction |= Rn << 16;
12432 }
12433
12434 static void
12435 do_t_mul (void)
12436 {
12437   bfd_boolean narrow;
12438   unsigned Rd, Rn, Rm;
12439
12440   if (!inst.operands[2].present)
12441     inst.operands[2].reg = inst.operands[0].reg;
12442
12443   Rd = inst.operands[0].reg;
12444   Rn = inst.operands[1].reg;
12445   Rm = inst.operands[2].reg;
12446
12447   if (unified_syntax)
12448     {
12449       if (inst.size_req == 4
12450           || (Rd != Rn
12451               && Rd != Rm)
12452           || Rn > 7
12453           || Rm > 7)
12454         narrow = FALSE;
12455       else if (inst.instruction == T_MNEM_muls)
12456         narrow = !in_it_block ();
12457       else
12458         narrow = in_it_block ();
12459     }
12460   else
12461     {
12462       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12463       constraint (Rn > 7 || Rm > 7,
12464                   BAD_HIREG);
12465       narrow = TRUE;
12466     }
12467
12468   if (narrow)
12469     {
12470       /* 16-bit MULS/Conditional MUL.  */
12471       inst.instruction = THUMB_OP16 (inst.instruction);
12472       inst.instruction |= Rd;
12473
12474       if (Rd == Rn)
12475         inst.instruction |= Rm << 3;
12476       else if (Rd == Rm)
12477         inst.instruction |= Rn << 3;
12478       else
12479         constraint (1, _("dest must overlap one source register"));
12480     }
12481   else
12482     {
12483       constraint (inst.instruction != T_MNEM_mul,
12484                   _("Thumb-2 MUL must not set flags"));
12485       /* 32-bit MUL.  */
12486       inst.instruction = THUMB_OP32 (inst.instruction);
12487       inst.instruction |= Rd << 8;
12488       inst.instruction |= Rn << 16;
12489       inst.instruction |= Rm << 0;
12490
12491       reject_bad_reg (Rd);
12492       reject_bad_reg (Rn);
12493       reject_bad_reg (Rm);
12494     }
12495 }
12496
12497 static void
12498 do_t_mull (void)
12499 {
12500   unsigned RdLo, RdHi, Rn, Rm;
12501
12502   RdLo = inst.operands[0].reg;
12503   RdHi = inst.operands[1].reg;
12504   Rn = inst.operands[2].reg;
12505   Rm = inst.operands[3].reg;
12506
12507   reject_bad_reg (RdLo);
12508   reject_bad_reg (RdHi);
12509   reject_bad_reg (Rn);
12510   reject_bad_reg (Rm);
12511
12512   inst.instruction |= RdLo << 12;
12513   inst.instruction |= RdHi << 8;
12514   inst.instruction |= Rn << 16;
12515   inst.instruction |= Rm;
12516
12517  if (RdLo == RdHi)
12518     as_tsktsk (_("rdhi and rdlo must be different"));
12519 }
12520
12521 static void
12522 do_t_nop (void)
12523 {
12524   set_it_insn_type (NEUTRAL_IT_INSN);
12525
12526   if (unified_syntax)
12527     {
12528       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12529         {
12530           inst.instruction = THUMB_OP32 (inst.instruction);
12531           inst.instruction |= inst.operands[0].imm;
12532         }
12533       else
12534         {
12535           /* PR9722: Check for Thumb2 availability before
12536              generating a thumb2 nop instruction.  */
12537           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12538             {
12539               inst.instruction = THUMB_OP16 (inst.instruction);
12540               inst.instruction |= inst.operands[0].imm << 4;
12541             }
12542           else
12543             inst.instruction = 0x46c0;
12544         }
12545     }
12546   else
12547     {
12548       constraint (inst.operands[0].present,
12549                   _("Thumb does not support NOP with hints"));
12550       inst.instruction = 0x46c0;
12551     }
12552 }
12553
12554 static void
12555 do_t_neg (void)
12556 {
12557   if (unified_syntax)
12558     {
12559       bfd_boolean narrow;
12560
12561       if (THUMB_SETS_FLAGS (inst.instruction))
12562         narrow = !in_it_block ();
12563       else
12564         narrow = in_it_block ();
12565       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12566         narrow = FALSE;
12567       if (inst.size_req == 4)
12568         narrow = FALSE;
12569
12570       if (!narrow)
12571         {
12572           inst.instruction = THUMB_OP32 (inst.instruction);
12573           inst.instruction |= inst.operands[0].reg << 8;
12574           inst.instruction |= inst.operands[1].reg << 16;
12575         }
12576       else
12577         {
12578           inst.instruction = THUMB_OP16 (inst.instruction);
12579           inst.instruction |= inst.operands[0].reg;
12580           inst.instruction |= inst.operands[1].reg << 3;
12581         }
12582     }
12583   else
12584     {
12585       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12586                   BAD_HIREG);
12587       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12588
12589       inst.instruction = THUMB_OP16 (inst.instruction);
12590       inst.instruction |= inst.operands[0].reg;
12591       inst.instruction |= inst.operands[1].reg << 3;
12592     }
12593 }
12594
12595 static void
12596 do_t_orn (void)
12597 {
12598   unsigned Rd, Rn;
12599
12600   Rd = inst.operands[0].reg;
12601   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12602
12603   reject_bad_reg (Rd);
12604   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12605   reject_bad_reg (Rn);
12606
12607   inst.instruction |= Rd << 8;
12608   inst.instruction |= Rn << 16;
12609
12610   if (!inst.operands[2].isreg)
12611     {
12612       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12613       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12614     }
12615   else
12616     {
12617       unsigned Rm;
12618
12619       Rm = inst.operands[2].reg;
12620       reject_bad_reg (Rm);
12621
12622       constraint (inst.operands[2].shifted
12623                   && inst.operands[2].immisreg,
12624                   _("shift must be constant"));
12625       encode_thumb32_shifted_operand (2);
12626     }
12627 }
12628
12629 static void
12630 do_t_pkhbt (void)
12631 {
12632   unsigned Rd, Rn, Rm;
12633
12634   Rd = inst.operands[0].reg;
12635   Rn = inst.operands[1].reg;
12636   Rm = inst.operands[2].reg;
12637
12638   reject_bad_reg (Rd);
12639   reject_bad_reg (Rn);
12640   reject_bad_reg (Rm);
12641
12642   inst.instruction |= Rd << 8;
12643   inst.instruction |= Rn << 16;
12644   inst.instruction |= Rm;
12645   if (inst.operands[3].present)
12646     {
12647       unsigned int val = inst.reloc.exp.X_add_number;
12648       constraint (inst.reloc.exp.X_op != O_constant,
12649                   _("expression too complex"));
12650       inst.instruction |= (val & 0x1c) << 10;
12651       inst.instruction |= (val & 0x03) << 6;
12652     }
12653 }
12654
12655 static void
12656 do_t_pkhtb (void)
12657 {
12658   if (!inst.operands[3].present)
12659     {
12660       unsigned Rtmp;
12661
12662       inst.instruction &= ~0x00000020;
12663
12664       /* PR 10168.  Swap the Rm and Rn registers.  */
12665       Rtmp = inst.operands[1].reg;
12666       inst.operands[1].reg = inst.operands[2].reg;
12667       inst.operands[2].reg = Rtmp;
12668     }
12669   do_t_pkhbt ();
12670 }
12671
12672 static void
12673 do_t_pld (void)
12674 {
12675   if (inst.operands[0].immisreg)
12676     reject_bad_reg (inst.operands[0].imm);
12677
12678   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12679 }
12680
12681 static void
12682 do_t_push_pop (void)
12683 {
12684   unsigned mask;
12685
12686   constraint (inst.operands[0].writeback,
12687               _("push/pop do not support {reglist}^"));
12688   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12689               _("expression too complex"));
12690
12691   mask = inst.operands[0].imm;
12692   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12693     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12694   else if (inst.size_req != 4
12695            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
12696                                        ? REG_LR : REG_PC)))
12697     {
12698       inst.instruction = THUMB_OP16 (inst.instruction);
12699       inst.instruction |= THUMB_PP_PC_LR;
12700       inst.instruction |= mask & 0xff;
12701     }
12702   else if (unified_syntax)
12703     {
12704       inst.instruction = THUMB_OP32 (inst.instruction);
12705       encode_thumb2_ldmstm (13, mask, TRUE);
12706     }
12707   else
12708     {
12709       inst.error = _("invalid register list to push/pop instruction");
12710       return;
12711     }
12712 }
12713
12714 static void
12715 do_t_rbit (void)
12716 {
12717   unsigned Rd, Rm;
12718
12719   Rd = inst.operands[0].reg;
12720   Rm = inst.operands[1].reg;
12721
12722   reject_bad_reg (Rd);
12723   reject_bad_reg (Rm);
12724
12725   inst.instruction |= Rd << 8;
12726   inst.instruction |= Rm << 16;
12727   inst.instruction |= Rm;
12728 }
12729
12730 static void
12731 do_t_rev (void)
12732 {
12733   unsigned Rd, Rm;
12734
12735   Rd = inst.operands[0].reg;
12736   Rm = inst.operands[1].reg;
12737
12738   reject_bad_reg (Rd);
12739   reject_bad_reg (Rm);
12740
12741   if (Rd <= 7 && Rm <= 7
12742       && inst.size_req != 4)
12743     {
12744       inst.instruction = THUMB_OP16 (inst.instruction);
12745       inst.instruction |= Rd;
12746       inst.instruction |= Rm << 3;
12747     }
12748   else if (unified_syntax)
12749     {
12750       inst.instruction = THUMB_OP32 (inst.instruction);
12751       inst.instruction |= Rd << 8;
12752       inst.instruction |= Rm << 16;
12753       inst.instruction |= Rm;
12754     }
12755   else
12756     inst.error = BAD_HIREG;
12757 }
12758
12759 static void
12760 do_t_rrx (void)
12761 {
12762   unsigned Rd, Rm;
12763
12764   Rd = inst.operands[0].reg;
12765   Rm = inst.operands[1].reg;
12766
12767   reject_bad_reg (Rd);
12768   reject_bad_reg (Rm);
12769
12770   inst.instruction |= Rd << 8;
12771   inst.instruction |= Rm;
12772 }
12773
12774 static void
12775 do_t_rsb (void)
12776 {
12777   unsigned Rd, Rs;
12778
12779   Rd = inst.operands[0].reg;
12780   Rs = (inst.operands[1].present
12781         ? inst.operands[1].reg    /* Rd, Rs, foo */
12782         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12783
12784   reject_bad_reg (Rd);
12785   reject_bad_reg (Rs);
12786   if (inst.operands[2].isreg)
12787     reject_bad_reg (inst.operands[2].reg);
12788
12789   inst.instruction |= Rd << 8;
12790   inst.instruction |= Rs << 16;
12791   if (!inst.operands[2].isreg)
12792     {
12793       bfd_boolean narrow;
12794
12795       if ((inst.instruction & 0x00100000) != 0)
12796         narrow = !in_it_block ();
12797       else
12798         narrow = in_it_block ();
12799
12800       if (Rd > 7 || Rs > 7)
12801         narrow = FALSE;
12802
12803       if (inst.size_req == 4 || !unified_syntax)
12804         narrow = FALSE;
12805
12806       if (inst.reloc.exp.X_op != O_constant
12807           || inst.reloc.exp.X_add_number != 0)
12808         narrow = FALSE;
12809
12810       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12811          relaxation, but it doesn't seem worth the hassle.  */
12812       if (narrow)
12813         {
12814           inst.reloc.type = BFD_RELOC_UNUSED;
12815           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12816           inst.instruction |= Rs << 3;
12817           inst.instruction |= Rd;
12818         }
12819       else
12820         {
12821           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12822           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12823         }
12824     }
12825   else
12826     encode_thumb32_shifted_operand (2);
12827 }
12828
12829 static void
12830 do_t_setend (void)
12831 {
12832   if (warn_on_deprecated
12833       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12834       as_tsktsk (_("setend use is deprecated for ARMv8"));
12835
12836   set_it_insn_type (OUTSIDE_IT_INSN);
12837   if (inst.operands[0].imm)
12838     inst.instruction |= 0x8;
12839 }
12840
12841 static void
12842 do_t_shift (void)
12843 {
12844   if (!inst.operands[1].present)
12845     inst.operands[1].reg = inst.operands[0].reg;
12846
12847   if (unified_syntax)
12848     {
12849       bfd_boolean narrow;
12850       int shift_kind;
12851
12852       switch (inst.instruction)
12853         {
12854         case T_MNEM_asr:
12855         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12856         case T_MNEM_lsl:
12857         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12858         case T_MNEM_lsr:
12859         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12860         case T_MNEM_ror:
12861         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12862         default: abort ();
12863         }
12864
12865       if (THUMB_SETS_FLAGS (inst.instruction))
12866         narrow = !in_it_block ();
12867       else
12868         narrow = in_it_block ();
12869       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12870         narrow = FALSE;
12871       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12872         narrow = FALSE;
12873       if (inst.operands[2].isreg
12874           && (inst.operands[1].reg != inst.operands[0].reg
12875               || inst.operands[2].reg > 7))
12876         narrow = FALSE;
12877       if (inst.size_req == 4)
12878         narrow = FALSE;
12879
12880       reject_bad_reg (inst.operands[0].reg);
12881       reject_bad_reg (inst.operands[1].reg);
12882
12883       if (!narrow)
12884         {
12885           if (inst.operands[2].isreg)
12886             {
12887               reject_bad_reg (inst.operands[2].reg);
12888               inst.instruction = THUMB_OP32 (inst.instruction);
12889               inst.instruction |= inst.operands[0].reg << 8;
12890               inst.instruction |= inst.operands[1].reg << 16;
12891               inst.instruction |= inst.operands[2].reg;
12892
12893               /* PR 12854: Error on extraneous shifts.  */
12894               constraint (inst.operands[2].shifted,
12895                           _("extraneous shift as part of operand to shift insn"));
12896             }
12897           else
12898             {
12899               inst.operands[1].shifted = 1;
12900               inst.operands[1].shift_kind = shift_kind;
12901               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12902                                              ? T_MNEM_movs : T_MNEM_mov);
12903               inst.instruction |= inst.operands[0].reg << 8;
12904               encode_thumb32_shifted_operand (1);
12905               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12906               inst.reloc.type = BFD_RELOC_UNUSED;
12907             }
12908         }
12909       else
12910         {
12911           if (inst.operands[2].isreg)
12912             {
12913               switch (shift_kind)
12914                 {
12915                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12916                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12917                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12918                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12919                 default: abort ();
12920                 }
12921
12922               inst.instruction |= inst.operands[0].reg;
12923               inst.instruction |= inst.operands[2].reg << 3;
12924
12925               /* PR 12854: Error on extraneous shifts.  */
12926               constraint (inst.operands[2].shifted,
12927                           _("extraneous shift as part of operand to shift insn"));
12928             }
12929           else
12930             {
12931               switch (shift_kind)
12932                 {
12933                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12934                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12935                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12936                 default: abort ();
12937                 }
12938               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12939               inst.instruction |= inst.operands[0].reg;
12940               inst.instruction |= inst.operands[1].reg << 3;
12941             }
12942         }
12943     }
12944   else
12945     {
12946       constraint (inst.operands[0].reg > 7
12947                   || inst.operands[1].reg > 7, BAD_HIREG);
12948       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12949
12950       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12951         {
12952           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12953           constraint (inst.operands[0].reg != inst.operands[1].reg,
12954                       _("source1 and dest must be same register"));
12955
12956           switch (inst.instruction)
12957             {
12958             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12959             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12960             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12961             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12962             default: abort ();
12963             }
12964
12965           inst.instruction |= inst.operands[0].reg;
12966           inst.instruction |= inst.operands[2].reg << 3;
12967
12968           /* PR 12854: Error on extraneous shifts.  */
12969           constraint (inst.operands[2].shifted,
12970                       _("extraneous shift as part of operand to shift insn"));
12971         }
12972       else
12973         {
12974           switch (inst.instruction)
12975             {
12976             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12977             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12978             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12979             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12980             default: abort ();
12981             }
12982           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12983           inst.instruction |= inst.operands[0].reg;
12984           inst.instruction |= inst.operands[1].reg << 3;
12985         }
12986     }
12987 }
12988
12989 static void
12990 do_t_simd (void)
12991 {
12992   unsigned Rd, Rn, Rm;
12993
12994   Rd = inst.operands[0].reg;
12995   Rn = inst.operands[1].reg;
12996   Rm = inst.operands[2].reg;
12997
12998   reject_bad_reg (Rd);
12999   reject_bad_reg (Rn);
13000   reject_bad_reg (Rm);
13001
13002   inst.instruction |= Rd << 8;
13003   inst.instruction |= Rn << 16;
13004   inst.instruction |= Rm;
13005 }
13006
13007 static void
13008 do_t_simd2 (void)
13009 {
13010   unsigned Rd, Rn, Rm;
13011
13012   Rd = inst.operands[0].reg;
13013   Rm = inst.operands[1].reg;
13014   Rn = inst.operands[2].reg;
13015
13016   reject_bad_reg (Rd);
13017   reject_bad_reg (Rn);
13018   reject_bad_reg (Rm);
13019
13020   inst.instruction |= Rd << 8;
13021   inst.instruction |= Rn << 16;
13022   inst.instruction |= Rm;
13023 }
13024
13025 static void
13026 do_t_smc (void)
13027 {
13028   unsigned int value = inst.reloc.exp.X_add_number;
13029   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13030               _("SMC is not permitted on this architecture"));
13031   constraint (inst.reloc.exp.X_op != O_constant,
13032               _("expression too complex"));
13033   inst.reloc.type = BFD_RELOC_UNUSED;
13034   inst.instruction |= (value & 0xf000) >> 12;
13035   inst.instruction |= (value & 0x0ff0);
13036   inst.instruction |= (value & 0x000f) << 16;
13037   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13038   set_it_insn_type_last ();
13039 }
13040
13041 static void
13042 do_t_hvc (void)
13043 {
13044   unsigned int value = inst.reloc.exp.X_add_number;
13045
13046   inst.reloc.type = BFD_RELOC_UNUSED;
13047   inst.instruction |= (value & 0x0fff);
13048   inst.instruction |= (value & 0xf000) << 4;
13049 }
13050
13051 static void
13052 do_t_ssat_usat (int bias)
13053 {
13054   unsigned Rd, Rn;
13055
13056   Rd = inst.operands[0].reg;
13057   Rn = inst.operands[2].reg;
13058
13059   reject_bad_reg (Rd);
13060   reject_bad_reg (Rn);
13061
13062   inst.instruction |= Rd << 8;
13063   inst.instruction |= inst.operands[1].imm - bias;
13064   inst.instruction |= Rn << 16;
13065
13066   if (inst.operands[3].present)
13067     {
13068       offsetT shift_amount = inst.reloc.exp.X_add_number;
13069
13070       inst.reloc.type = BFD_RELOC_UNUSED;
13071
13072       constraint (inst.reloc.exp.X_op != O_constant,
13073                   _("expression too complex"));
13074
13075       if (shift_amount != 0)
13076         {
13077           constraint (shift_amount > 31,
13078                       _("shift expression is too large"));
13079
13080           if (inst.operands[3].shift_kind == SHIFT_ASR)
13081             inst.instruction |= 0x00200000;  /* sh bit.  */
13082
13083           inst.instruction |= (shift_amount & 0x1c) << 10;
13084           inst.instruction |= (shift_amount & 0x03) << 6;
13085         }
13086     }
13087 }
13088
13089 static void
13090 do_t_ssat (void)
13091 {
13092   do_t_ssat_usat (1);
13093 }
13094
13095 static void
13096 do_t_ssat16 (void)
13097 {
13098   unsigned Rd, Rn;
13099
13100   Rd = inst.operands[0].reg;
13101   Rn = inst.operands[2].reg;
13102
13103   reject_bad_reg (Rd);
13104   reject_bad_reg (Rn);
13105
13106   inst.instruction |= Rd << 8;
13107   inst.instruction |= inst.operands[1].imm - 1;
13108   inst.instruction |= Rn << 16;
13109 }
13110
13111 static void
13112 do_t_strex (void)
13113 {
13114   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
13115               || inst.operands[2].postind || inst.operands[2].writeback
13116               || inst.operands[2].immisreg || inst.operands[2].shifted
13117               || inst.operands[2].negative,
13118               BAD_ADDR_MODE);
13119
13120   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
13121
13122   inst.instruction |= inst.operands[0].reg << 8;
13123   inst.instruction |= inst.operands[1].reg << 12;
13124   inst.instruction |= inst.operands[2].reg << 16;
13125   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
13126 }
13127
13128 static void
13129 do_t_strexd (void)
13130 {
13131   if (!inst.operands[2].present)
13132     inst.operands[2].reg = inst.operands[1].reg + 1;
13133
13134   constraint (inst.operands[0].reg == inst.operands[1].reg
13135               || inst.operands[0].reg == inst.operands[2].reg
13136               || inst.operands[0].reg == inst.operands[3].reg,
13137               BAD_OVERLAP);
13138
13139   inst.instruction |= inst.operands[0].reg;
13140   inst.instruction |= inst.operands[1].reg << 12;
13141   inst.instruction |= inst.operands[2].reg << 8;
13142   inst.instruction |= inst.operands[3].reg << 16;
13143 }
13144
13145 static void
13146 do_t_sxtah (void)
13147 {
13148   unsigned Rd, Rn, Rm;
13149
13150   Rd = inst.operands[0].reg;
13151   Rn = inst.operands[1].reg;
13152   Rm = inst.operands[2].reg;
13153
13154   reject_bad_reg (Rd);
13155   reject_bad_reg (Rn);
13156   reject_bad_reg (Rm);
13157
13158   inst.instruction |= Rd << 8;
13159   inst.instruction |= Rn << 16;
13160   inst.instruction |= Rm;
13161   inst.instruction |= inst.operands[3].imm << 4;
13162 }
13163
13164 static void
13165 do_t_sxth (void)
13166 {
13167   unsigned Rd, Rm;
13168
13169   Rd = inst.operands[0].reg;
13170   Rm = inst.operands[1].reg;
13171
13172   reject_bad_reg (Rd);
13173   reject_bad_reg (Rm);
13174
13175   if (inst.instruction <= 0xffff
13176       && inst.size_req != 4
13177       && Rd <= 7 && Rm <= 7
13178       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13179     {
13180       inst.instruction = THUMB_OP16 (inst.instruction);
13181       inst.instruction |= Rd;
13182       inst.instruction |= Rm << 3;
13183     }
13184   else if (unified_syntax)
13185     {
13186       if (inst.instruction <= 0xffff)
13187         inst.instruction = THUMB_OP32 (inst.instruction);
13188       inst.instruction |= Rd << 8;
13189       inst.instruction |= Rm;
13190       inst.instruction |= inst.operands[2].imm << 4;
13191     }
13192   else
13193     {
13194       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13195                   _("Thumb encoding does not support rotation"));
13196       constraint (1, BAD_HIREG);
13197     }
13198 }
13199
13200 static void
13201 do_t_swi (void)
13202 {
13203   inst.reloc.type = BFD_RELOC_ARM_SWI;
13204 }
13205
13206 static void
13207 do_t_tb (void)
13208 {
13209   unsigned Rn, Rm;
13210   int half;
13211
13212   half = (inst.instruction & 0x10) != 0;
13213   set_it_insn_type_last ();
13214   constraint (inst.operands[0].immisreg,
13215               _("instruction requires register index"));
13216
13217   Rn = inst.operands[0].reg;
13218   Rm = inst.operands[0].imm;
13219
13220   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13221     constraint (Rn == REG_SP, BAD_SP);
13222   reject_bad_reg (Rm);
13223
13224   constraint (!half && inst.operands[0].shifted,
13225               _("instruction does not allow shifted index"));
13226   inst.instruction |= (Rn << 16) | Rm;
13227 }
13228
13229 static void
13230 do_t_udf (void)
13231 {
13232   if (!inst.operands[0].present)
13233     inst.operands[0].imm = 0;
13234
13235   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13236     {
13237       constraint (inst.size_req == 2,
13238                   _("immediate value out of range"));
13239       inst.instruction = THUMB_OP32 (inst.instruction);
13240       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13241       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13242     }
13243   else
13244     {
13245       inst.instruction = THUMB_OP16 (inst.instruction);
13246       inst.instruction |= inst.operands[0].imm;
13247     }
13248
13249   set_it_insn_type (NEUTRAL_IT_INSN);
13250 }
13251
13252
13253 static void
13254 do_t_usat (void)
13255 {
13256   do_t_ssat_usat (0);
13257 }
13258
13259 static void
13260 do_t_usat16 (void)
13261 {
13262   unsigned Rd, Rn;
13263
13264   Rd = inst.operands[0].reg;
13265   Rn = inst.operands[2].reg;
13266
13267   reject_bad_reg (Rd);
13268   reject_bad_reg (Rn);
13269
13270   inst.instruction |= Rd << 8;
13271   inst.instruction |= inst.operands[1].imm;
13272   inst.instruction |= Rn << 16;
13273 }
13274
13275 /* Neon instruction encoder helpers.  */
13276
13277 /* Encodings for the different types for various Neon opcodes.  */
13278
13279 /* An "invalid" code for the following tables.  */
13280 #define N_INV -1u
13281
13282 struct neon_tab_entry
13283 {
13284   unsigned integer;
13285   unsigned float_or_poly;
13286   unsigned scalar_or_imm;
13287 };
13288
13289 /* Map overloaded Neon opcodes to their respective encodings.  */
13290 #define NEON_ENC_TAB                                    \
13291   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13292   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13293   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13294   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13295   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13296   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13297   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13298   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13299   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13300   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13301   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13302   /* Register variants of the following two instructions are encoded as
13303      vcge / vcgt with the operands reversed.  */        \
13304   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13305   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13306   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13307   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13308   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13309   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13310   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13311   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13312   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13313   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13314   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13315   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13316   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13317   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13318   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13319   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13320   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13321   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13322   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13323   X(vand,       0x0000110, N_INV,     0x0800030),       \
13324   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13325   X(veor,       0x1000110, N_INV,     N_INV),           \
13326   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13327   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13328   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13329   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13330   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13331   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13332   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13333   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13334   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13335   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13336   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13337   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13338   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13339   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13340   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13341   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13342   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13343   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13344   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13345   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13346   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13347   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13348   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13349   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13350   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13351   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13352   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13353   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13354   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13355   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13356   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13357   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13358   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13359   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13360   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13361   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13362   X(aes,        0x3b00300, N_INV,     N_INV),           \
13363   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13364   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13365   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13366
13367 enum neon_opc
13368 {
13369 #define X(OPC,I,F,S) N_MNEM_##OPC
13370 NEON_ENC_TAB
13371 #undef X
13372 };
13373
13374 static const struct neon_tab_entry neon_enc_tab[] =
13375 {
13376 #define X(OPC,I,F,S) { (I), (F), (S) }
13377 NEON_ENC_TAB
13378 #undef X
13379 };
13380
13381 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13382 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13383 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13384 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13385 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13386 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13387 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13388 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13389 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13390 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13391 #define NEON_ENC_SINGLE_(X) \
13392   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13393 #define NEON_ENC_DOUBLE_(X) \
13394   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13395 #define NEON_ENC_FPV8_(X) \
13396   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13397
13398 #define NEON_ENCODE(type, inst)                                 \
13399   do                                                            \
13400     {                                                           \
13401       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13402       inst.is_neon = 1;                                         \
13403     }                                                           \
13404   while (0)
13405
13406 #define check_neon_suffixes                                             \
13407   do                                                                    \
13408     {                                                                   \
13409       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13410         {                                                               \
13411           as_bad (_("invalid neon suffix for non neon instruction"));   \
13412           return;                                                       \
13413         }                                                               \
13414     }                                                                   \
13415   while (0)
13416
13417 /* Define shapes for instruction operands. The following mnemonic characters
13418    are used in this table:
13419
13420      F - VFP S<n> register
13421      D - Neon D<n> register
13422      Q - Neon Q<n> register
13423      I - Immediate
13424      S - Scalar
13425      R - ARM register
13426      L - D<n> register list
13427
13428    This table is used to generate various data:
13429      - enumerations of the form NS_DDR to be used as arguments to
13430        neon_select_shape.
13431      - a table classifying shapes into single, double, quad, mixed.
13432      - a table used to drive neon_select_shape.  */
13433
13434 #define NEON_SHAPE_DEF                  \
13435   X(3, (D, D, D), DOUBLE),              \
13436   X(3, (Q, Q, Q), QUAD),                \
13437   X(3, (D, D, I), DOUBLE),              \
13438   X(3, (Q, Q, I), QUAD),                \
13439   X(3, (D, D, S), DOUBLE),              \
13440   X(3, (Q, Q, S), QUAD),                \
13441   X(2, (D, D), DOUBLE),                 \
13442   X(2, (Q, Q), QUAD),                   \
13443   X(2, (D, S), DOUBLE),                 \
13444   X(2, (Q, S), QUAD),                   \
13445   X(2, (D, R), DOUBLE),                 \
13446   X(2, (Q, R), QUAD),                   \
13447   X(2, (D, I), DOUBLE),                 \
13448   X(2, (Q, I), QUAD),                   \
13449   X(3, (D, L, D), DOUBLE),              \
13450   X(2, (D, Q), MIXED),                  \
13451   X(2, (Q, D), MIXED),                  \
13452   X(3, (D, Q, I), MIXED),               \
13453   X(3, (Q, D, I), MIXED),               \
13454   X(3, (Q, D, D), MIXED),               \
13455   X(3, (D, Q, Q), MIXED),               \
13456   X(3, (Q, Q, D), MIXED),               \
13457   X(3, (Q, D, S), MIXED),               \
13458   X(3, (D, Q, S), MIXED),               \
13459   X(4, (D, D, D, I), DOUBLE),           \
13460   X(4, (Q, Q, Q, I), QUAD),             \
13461   X(4, (D, D, S, I), DOUBLE),           \
13462   X(4, (Q, Q, S, I), QUAD),             \
13463   X(2, (F, F), SINGLE),                 \
13464   X(3, (F, F, F), SINGLE),              \
13465   X(2, (F, I), SINGLE),                 \
13466   X(2, (F, D), MIXED),                  \
13467   X(2, (D, F), MIXED),                  \
13468   X(3, (F, F, I), MIXED),               \
13469   X(4, (R, R, F, F), SINGLE),           \
13470   X(4, (F, F, R, R), SINGLE),           \
13471   X(3, (D, R, R), DOUBLE),              \
13472   X(3, (R, R, D), DOUBLE),              \
13473   X(2, (S, R), SINGLE),                 \
13474   X(2, (R, S), SINGLE),                 \
13475   X(2, (F, R), SINGLE),                 \
13476   X(2, (R, F), SINGLE),                 \
13477 /* Half float shape supported so far.  */\
13478   X (2, (H, D), MIXED),                 \
13479   X (2, (D, H), MIXED),                 \
13480   X (2, (H, F), MIXED),                 \
13481   X (2, (F, H), MIXED),                 \
13482   X (2, (H, H), HALF),                  \
13483   X (2, (H, R), HALF),                  \
13484   X (2, (R, H), HALF),                  \
13485   X (2, (H, I), HALF),                  \
13486   X (3, (H, H, H), HALF),               \
13487   X (3, (H, F, I), MIXED),              \
13488   X (3, (F, H, I), MIXED),              \
13489   X (3, (D, H, H), MIXED),              \
13490   X (3, (D, H, S), MIXED)
13491
13492 #define S2(A,B)         NS_##A##B
13493 #define S3(A,B,C)       NS_##A##B##C
13494 #define S4(A,B,C,D)     NS_##A##B##C##D
13495
13496 #define X(N, L, C) S##N L
13497
13498 enum neon_shape
13499 {
13500   NEON_SHAPE_DEF,
13501   NS_NULL
13502 };
13503
13504 #undef X
13505 #undef S2
13506 #undef S3
13507 #undef S4
13508
13509 enum neon_shape_class
13510 {
13511   SC_HALF,
13512   SC_SINGLE,
13513   SC_DOUBLE,
13514   SC_QUAD,
13515   SC_MIXED
13516 };
13517
13518 #define X(N, L, C) SC_##C
13519
13520 static enum neon_shape_class neon_shape_class[] =
13521 {
13522   NEON_SHAPE_DEF
13523 };
13524
13525 #undef X
13526
13527 enum neon_shape_el
13528 {
13529   SE_H,
13530   SE_F,
13531   SE_D,
13532   SE_Q,
13533   SE_I,
13534   SE_S,
13535   SE_R,
13536   SE_L
13537 };
13538
13539 /* Register widths of above.  */
13540 static unsigned neon_shape_el_size[] =
13541 {
13542   16,
13543   32,
13544   64,
13545   128,
13546   0,
13547   32,
13548   32,
13549   0
13550 };
13551
13552 struct neon_shape_info
13553 {
13554   unsigned els;
13555   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13556 };
13557
13558 #define S2(A,B)         { SE_##A, SE_##B }
13559 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13560 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13561
13562 #define X(N, L, C) { N, S##N L }
13563
13564 static struct neon_shape_info neon_shape_tab[] =
13565 {
13566   NEON_SHAPE_DEF
13567 };
13568
13569 #undef X
13570 #undef S2
13571 #undef S3
13572 #undef S4
13573
13574 /* Bit masks used in type checking given instructions.
13575   'N_EQK' means the type must be the same as (or based on in some way) the key
13576    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13577    set, various other bits can be set as well in order to modify the meaning of
13578    the type constraint.  */
13579
13580 enum neon_type_mask
13581 {
13582   N_S8   = 0x0000001,
13583   N_S16  = 0x0000002,
13584   N_S32  = 0x0000004,
13585   N_S64  = 0x0000008,
13586   N_U8   = 0x0000010,
13587   N_U16  = 0x0000020,
13588   N_U32  = 0x0000040,
13589   N_U64  = 0x0000080,
13590   N_I8   = 0x0000100,
13591   N_I16  = 0x0000200,
13592   N_I32  = 0x0000400,
13593   N_I64  = 0x0000800,
13594   N_8    = 0x0001000,
13595   N_16   = 0x0002000,
13596   N_32   = 0x0004000,
13597   N_64   = 0x0008000,
13598   N_P8   = 0x0010000,
13599   N_P16  = 0x0020000,
13600   N_F16  = 0x0040000,
13601   N_F32  = 0x0080000,
13602   N_F64  = 0x0100000,
13603   N_P64  = 0x0200000,
13604   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13605   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13606   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13607   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13608   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13609   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13610   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13611   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13612   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13613   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13614   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13615   N_UTYP = 0,
13616   N_MAX_NONSPECIAL = N_P64
13617 };
13618
13619 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13620
13621 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13622 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13623 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13624 #define N_S_32     (N_S8 | N_S16 | N_S32)
13625 #define N_F_16_32  (N_F16 | N_F32)
13626 #define N_SUF_32   (N_SU_32 | N_F_16_32)
13627 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13628 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
13629 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
13630
13631 /* Pass this as the first type argument to neon_check_type to ignore types
13632    altogether.  */
13633 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13634
13635 /* Select a "shape" for the current instruction (describing register types or
13636    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13637    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13638    function of operand parsing, so this function doesn't need to be called.
13639    Shapes should be listed in order of decreasing length.  */
13640
13641 static enum neon_shape
13642 neon_select_shape (enum neon_shape shape, ...)
13643 {
13644   va_list ap;
13645   enum neon_shape first_shape = shape;
13646
13647   /* Fix missing optional operands. FIXME: we don't know at this point how
13648      many arguments we should have, so this makes the assumption that we have
13649      > 1. This is true of all current Neon opcodes, I think, but may not be
13650      true in the future.  */
13651   if (!inst.operands[1].present)
13652     inst.operands[1] = inst.operands[0];
13653
13654   va_start (ap, shape);
13655
13656   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13657     {
13658       unsigned j;
13659       int matches = 1;
13660
13661       for (j = 0; j < neon_shape_tab[shape].els; j++)
13662         {
13663           if (!inst.operands[j].present)
13664             {
13665               matches = 0;
13666               break;
13667             }
13668
13669           switch (neon_shape_tab[shape].el[j])
13670             {
13671               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
13672                  a VFP single precision register operand, it's essentially
13673                  means only half of the register is used.
13674
13675                  If the type specifier is given after the mnemonics, the
13676                  information is stored in inst.vectype.  If the type specifier
13677                  is given after register operand, the information is stored
13678                  in inst.operands[].vectype.
13679
13680                  When there is only one type specifier, and all the register
13681                  operands are the same type of hardware register, the type
13682                  specifier applies to all register operands.
13683
13684                  If no type specifier is given, the shape is inferred from
13685                  operand information.
13686
13687                  for example:
13688                  vadd.f16 s0, s1, s2:           NS_HHH
13689                  vabs.f16 s0, s1:               NS_HH
13690                  vmov.f16 s0, r1:               NS_HR
13691                  vmov.f16 r0, s1:               NS_RH
13692                  vcvt.f16 r0, s1:               NS_RH
13693                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
13694                  vcvt.f16.s32   s2, s2:         NS_HF
13695               */
13696             case SE_H:
13697               if (!(inst.operands[j].isreg
13698                     && inst.operands[j].isvec
13699                     && inst.operands[j].issingle
13700                     && !inst.operands[j].isquad
13701                     && ((inst.vectype.elems == 1
13702                          && inst.vectype.el[0].size == 16)
13703                         || (inst.vectype.elems > 1
13704                             && inst.vectype.el[j].size == 16)
13705                         || (inst.vectype.elems == 0
13706                             && inst.operands[j].vectype.type != NT_invtype
13707                             && inst.operands[j].vectype.size == 16))))
13708                 matches = 0;
13709               break;
13710
13711             case SE_F:
13712               if (!(inst.operands[j].isreg
13713                     && inst.operands[j].isvec
13714                     && inst.operands[j].issingle
13715                     && !inst.operands[j].isquad
13716                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
13717                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
13718                         || (inst.vectype.elems == 0
13719                             && (inst.operands[j].vectype.size == 32
13720                                 || inst.operands[j].vectype.type == NT_invtype)))))
13721                 matches = 0;
13722               break;
13723
13724             case SE_D:
13725               if (!(inst.operands[j].isreg
13726                     && inst.operands[j].isvec
13727                     && !inst.operands[j].isquad
13728                     && !inst.operands[j].issingle))
13729                 matches = 0;
13730               break;
13731
13732             case SE_R:
13733               if (!(inst.operands[j].isreg
13734                     && !inst.operands[j].isvec))
13735                 matches = 0;
13736               break;
13737
13738             case SE_Q:
13739               if (!(inst.operands[j].isreg
13740                     && inst.operands[j].isvec
13741                     && inst.operands[j].isquad
13742                     && !inst.operands[j].issingle))
13743                 matches = 0;
13744               break;
13745
13746             case SE_I:
13747               if (!(!inst.operands[j].isreg
13748                     && !inst.operands[j].isscalar))
13749                 matches = 0;
13750               break;
13751
13752             case SE_S:
13753               if (!(!inst.operands[j].isreg
13754                     && inst.operands[j].isscalar))
13755                 matches = 0;
13756               break;
13757
13758             case SE_L:
13759               break;
13760             }
13761           if (!matches)
13762             break;
13763         }
13764       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13765         /* We've matched all the entries in the shape table, and we don't
13766            have any left over operands which have not been matched.  */
13767         break;
13768     }
13769
13770   va_end (ap);
13771
13772   if (shape == NS_NULL && first_shape != NS_NULL)
13773     first_error (_("invalid instruction shape"));
13774
13775   return shape;
13776 }
13777
13778 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13779    means the Q bit should be set).  */
13780
13781 static int
13782 neon_quad (enum neon_shape shape)
13783 {
13784   return neon_shape_class[shape] == SC_QUAD;
13785 }
13786
13787 static void
13788 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13789                        unsigned *g_size)
13790 {
13791   /* Allow modification to be made to types which are constrained to be
13792      based on the key element, based on bits set alongside N_EQK.  */
13793   if ((typebits & N_EQK) != 0)
13794     {
13795       if ((typebits & N_HLF) != 0)
13796         *g_size /= 2;
13797       else if ((typebits & N_DBL) != 0)
13798         *g_size *= 2;
13799       if ((typebits & N_SGN) != 0)
13800         *g_type = NT_signed;
13801       else if ((typebits & N_UNS) != 0)
13802         *g_type = NT_unsigned;
13803       else if ((typebits & N_INT) != 0)
13804         *g_type = NT_integer;
13805       else if ((typebits & N_FLT) != 0)
13806         *g_type = NT_float;
13807       else if ((typebits & N_SIZ) != 0)
13808         *g_type = NT_untyped;
13809     }
13810 }
13811
13812 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13813    operand type, i.e. the single type specified in a Neon instruction when it
13814    is the only one given.  */
13815
13816 static struct neon_type_el
13817 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13818 {
13819   struct neon_type_el dest = *key;
13820
13821   gas_assert ((thisarg & N_EQK) != 0);
13822
13823   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13824
13825   return dest;
13826 }
13827
13828 /* Convert Neon type and size into compact bitmask representation.  */
13829
13830 static enum neon_type_mask
13831 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13832 {
13833   switch (type)
13834     {
13835     case NT_untyped:
13836       switch (size)
13837         {
13838         case 8:  return N_8;
13839         case 16: return N_16;
13840         case 32: return N_32;
13841         case 64: return N_64;
13842         default: ;
13843         }
13844       break;
13845
13846     case NT_integer:
13847       switch (size)
13848         {
13849         case 8:  return N_I8;
13850         case 16: return N_I16;
13851         case 32: return N_I32;
13852         case 64: return N_I64;
13853         default: ;
13854         }
13855       break;
13856
13857     case NT_float:
13858       switch (size)
13859         {
13860         case 16: return N_F16;
13861         case 32: return N_F32;
13862         case 64: return N_F64;
13863         default: ;
13864         }
13865       break;
13866
13867     case NT_poly:
13868       switch (size)
13869         {
13870         case 8:  return N_P8;
13871         case 16: return N_P16;
13872         case 64: return N_P64;
13873         default: ;
13874         }
13875       break;
13876
13877     case NT_signed:
13878       switch (size)
13879         {
13880         case 8:  return N_S8;
13881         case 16: return N_S16;
13882         case 32: return N_S32;
13883         case 64: return N_S64;
13884         default: ;
13885         }
13886       break;
13887
13888     case NT_unsigned:
13889       switch (size)
13890         {
13891         case 8:  return N_U8;
13892         case 16: return N_U16;
13893         case 32: return N_U32;
13894         case 64: return N_U64;
13895         default: ;
13896         }
13897       break;
13898
13899     default: ;
13900     }
13901
13902   return N_UTYP;
13903 }
13904
13905 /* Convert compact Neon bitmask type representation to a type and size. Only
13906    handles the case where a single bit is set in the mask.  */
13907
13908 static int
13909 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13910                      enum neon_type_mask mask)
13911 {
13912   if ((mask & N_EQK) != 0)
13913     return FAIL;
13914
13915   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13916     *size = 8;
13917   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13918     *size = 16;
13919   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13920     *size = 32;
13921   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13922     *size = 64;
13923   else
13924     return FAIL;
13925
13926   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13927     *type = NT_signed;
13928   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13929     *type = NT_unsigned;
13930   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13931     *type = NT_integer;
13932   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13933     *type = NT_untyped;
13934   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13935     *type = NT_poly;
13936   else if ((mask & (N_F_ALL)) != 0)
13937     *type = NT_float;
13938   else
13939     return FAIL;
13940
13941   return SUCCESS;
13942 }
13943
13944 /* Modify a bitmask of allowed types. This is only needed for type
13945    relaxation.  */
13946
13947 static unsigned
13948 modify_types_allowed (unsigned allowed, unsigned mods)
13949 {
13950   unsigned size;
13951   enum neon_el_type type;
13952   unsigned destmask;
13953   int i;
13954
13955   destmask = 0;
13956
13957   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13958     {
13959       if (el_type_of_type_chk (&type, &size,
13960                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13961         {
13962           neon_modify_type_size (mods, &type, &size);
13963           destmask |= type_chk_of_el_type (type, size);
13964         }
13965     }
13966
13967   return destmask;
13968 }
13969
13970 /* Check type and return type classification.
13971    The manual states (paraphrase): If one datatype is given, it indicates the
13972    type given in:
13973     - the second operand, if there is one
13974     - the operand, if there is no second operand
13975     - the result, if there are no operands.
13976    This isn't quite good enough though, so we use a concept of a "key" datatype
13977    which is set on a per-instruction basis, which is the one which matters when
13978    only one data type is written.
13979    Note: this function has side-effects (e.g. filling in missing operands). All
13980    Neon instructions should call it before performing bit encoding.  */
13981
13982 static struct neon_type_el
13983 neon_check_type (unsigned els, enum neon_shape ns, ...)
13984 {
13985   va_list ap;
13986   unsigned i, pass, key_el = 0;
13987   unsigned types[NEON_MAX_TYPE_ELS];
13988   enum neon_el_type k_type = NT_invtype;
13989   unsigned k_size = -1u;
13990   struct neon_type_el badtype = {NT_invtype, -1};
13991   unsigned key_allowed = 0;
13992
13993   /* Optional registers in Neon instructions are always (not) in operand 1.
13994      Fill in the missing operand here, if it was omitted.  */
13995   if (els > 1 && !inst.operands[1].present)
13996     inst.operands[1] = inst.operands[0];
13997
13998   /* Suck up all the varargs.  */
13999   va_start (ap, ns);
14000   for (i = 0; i < els; i++)
14001     {
14002       unsigned thisarg = va_arg (ap, unsigned);
14003       if (thisarg == N_IGNORE_TYPE)
14004         {
14005           va_end (ap);
14006           return badtype;
14007         }
14008       types[i] = thisarg;
14009       if ((thisarg & N_KEY) != 0)
14010         key_el = i;
14011     }
14012   va_end (ap);
14013
14014   if (inst.vectype.elems > 0)
14015     for (i = 0; i < els; i++)
14016       if (inst.operands[i].vectype.type != NT_invtype)
14017         {
14018           first_error (_("types specified in both the mnemonic and operands"));
14019           return badtype;
14020         }
14021
14022   /* Duplicate inst.vectype elements here as necessary.
14023      FIXME: No idea if this is exactly the same as the ARM assembler,
14024      particularly when an insn takes one register and one non-register
14025      operand. */
14026   if (inst.vectype.elems == 1 && els > 1)
14027     {
14028       unsigned j;
14029       inst.vectype.elems = els;
14030       inst.vectype.el[key_el] = inst.vectype.el[0];
14031       for (j = 0; j < els; j++)
14032         if (j != key_el)
14033           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14034                                                   types[j]);
14035     }
14036   else if (inst.vectype.elems == 0 && els > 0)
14037     {
14038       unsigned j;
14039       /* No types were given after the mnemonic, so look for types specified
14040          after each operand. We allow some flexibility here; as long as the
14041          "key" operand has a type, we can infer the others.  */
14042       for (j = 0; j < els; j++)
14043         if (inst.operands[j].vectype.type != NT_invtype)
14044           inst.vectype.el[j] = inst.operands[j].vectype;
14045
14046       if (inst.operands[key_el].vectype.type != NT_invtype)
14047         {
14048           for (j = 0; j < els; j++)
14049             if (inst.operands[j].vectype.type == NT_invtype)
14050               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14051                                                       types[j]);
14052         }
14053       else
14054         {
14055           first_error (_("operand types can't be inferred"));
14056           return badtype;
14057         }
14058     }
14059   else if (inst.vectype.elems != els)
14060     {
14061       first_error (_("type specifier has the wrong number of parts"));
14062       return badtype;
14063     }
14064
14065   for (pass = 0; pass < 2; pass++)
14066     {
14067       for (i = 0; i < els; i++)
14068         {
14069           unsigned thisarg = types[i];
14070           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
14071             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
14072           enum neon_el_type g_type = inst.vectype.el[i].type;
14073           unsigned g_size = inst.vectype.el[i].size;
14074
14075           /* Decay more-specific signed & unsigned types to sign-insensitive
14076              integer types if sign-specific variants are unavailable.  */
14077           if ((g_type == NT_signed || g_type == NT_unsigned)
14078               && (types_allowed & N_SU_ALL) == 0)
14079             g_type = NT_integer;
14080
14081           /* If only untyped args are allowed, decay any more specific types to
14082              them. Some instructions only care about signs for some element
14083              sizes, so handle that properly.  */
14084           if (((types_allowed & N_UNT) == 0)
14085               && ((g_size == 8 && (types_allowed & N_8) != 0)
14086                   || (g_size == 16 && (types_allowed & N_16) != 0)
14087                   || (g_size == 32 && (types_allowed & N_32) != 0)
14088                   || (g_size == 64 && (types_allowed & N_64) != 0)))
14089             g_type = NT_untyped;
14090
14091           if (pass == 0)
14092             {
14093               if ((thisarg & N_KEY) != 0)
14094                 {
14095                   k_type = g_type;
14096                   k_size = g_size;
14097                   key_allowed = thisarg & ~N_KEY;
14098
14099                   /* Check architecture constraint on FP16 extension.  */
14100                   if (k_size == 16
14101                       && k_type == NT_float
14102                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14103                     {
14104                       inst.error = _(BAD_FP16);
14105                       return badtype;
14106                     }
14107                 }
14108             }
14109           else
14110             {
14111               if ((thisarg & N_VFP) != 0)
14112                 {
14113                   enum neon_shape_el regshape;
14114                   unsigned regwidth, match;
14115
14116                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
14117                   if (ns == NS_NULL)
14118                     {
14119                       first_error (_("invalid instruction shape"));
14120                       return badtype;
14121                     }
14122                   regshape = neon_shape_tab[ns].el[i];
14123                   regwidth = neon_shape_el_size[regshape];
14124
14125                   /* In VFP mode, operands must match register widths. If we
14126                      have a key operand, use its width, else use the width of
14127                      the current operand.  */
14128                   if (k_size != -1u)
14129                     match = k_size;
14130                   else
14131                     match = g_size;
14132
14133                   /* FP16 will use a single precision register.  */
14134                   if (regwidth == 32 && match == 16)
14135                     {
14136                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14137                         match = regwidth;
14138                       else
14139                         {
14140                           inst.error = _(BAD_FP16);
14141                           return badtype;
14142                         }
14143                     }
14144
14145                   if (regwidth != match)
14146                     {
14147                       first_error (_("operand size must match register width"));
14148                       return badtype;
14149                     }
14150                 }
14151
14152               if ((thisarg & N_EQK) == 0)
14153                 {
14154                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
14155
14156                   if ((given_type & types_allowed) == 0)
14157                     {
14158                       first_error (_("bad type in Neon instruction"));
14159                       return badtype;
14160                     }
14161                 }
14162               else
14163                 {
14164                   enum neon_el_type mod_k_type = k_type;
14165                   unsigned mod_k_size = k_size;
14166                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
14167                   if (g_type != mod_k_type || g_size != mod_k_size)
14168                     {
14169                       first_error (_("inconsistent types in Neon instruction"));
14170                       return badtype;
14171                     }
14172                 }
14173             }
14174         }
14175     }
14176
14177   return inst.vectype.el[key_el];
14178 }
14179
14180 /* Neon-style VFP instruction forwarding.  */
14181
14182 /* Thumb VFP instructions have 0xE in the condition field.  */
14183
14184 static void
14185 do_vfp_cond_or_thumb (void)
14186 {
14187   inst.is_neon = 1;
14188
14189   if (thumb_mode)
14190     inst.instruction |= 0xe0000000;
14191   else
14192     inst.instruction |= inst.cond << 28;
14193 }
14194
14195 /* Look up and encode a simple mnemonic, for use as a helper function for the
14196    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
14197    etc.  It is assumed that operand parsing has already been done, and that the
14198    operands are in the form expected by the given opcode (this isn't necessarily
14199    the same as the form in which they were parsed, hence some massaging must
14200    take place before this function is called).
14201    Checks current arch version against that in the looked-up opcode.  */
14202
14203 static void
14204 do_vfp_nsyn_opcode (const char *opname)
14205 {
14206   const struct asm_opcode *opcode;
14207
14208   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
14209
14210   if (!opcode)
14211     abort ();
14212
14213   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
14214                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
14215               _(BAD_FPU));
14216
14217   inst.is_neon = 1;
14218
14219   if (thumb_mode)
14220     {
14221       inst.instruction = opcode->tvalue;
14222       opcode->tencode ();
14223     }
14224   else
14225     {
14226       inst.instruction = (inst.cond << 28) | opcode->avalue;
14227       opcode->aencode ();
14228     }
14229 }
14230
14231 static void
14232 do_vfp_nsyn_add_sub (enum neon_shape rs)
14233 {
14234   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
14235
14236   if (rs == NS_FFF || rs == NS_HHH)
14237     {
14238       if (is_add)
14239         do_vfp_nsyn_opcode ("fadds");
14240       else
14241         do_vfp_nsyn_opcode ("fsubs");
14242
14243       /* ARMv8.2 fp16 instruction.  */
14244       if (rs == NS_HHH)
14245         do_scalar_fp16_v82_encode ();
14246     }
14247   else
14248     {
14249       if (is_add)
14250         do_vfp_nsyn_opcode ("faddd");
14251       else
14252         do_vfp_nsyn_opcode ("fsubd");
14253     }
14254 }
14255
14256 /* Check operand types to see if this is a VFP instruction, and if so call
14257    PFN ().  */
14258
14259 static int
14260 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
14261 {
14262   enum neon_shape rs;
14263   struct neon_type_el et;
14264
14265   switch (args)
14266     {
14267     case 2:
14268       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14269       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14270       break;
14271
14272     case 3:
14273       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14274       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14275                             N_F_ALL | N_KEY | N_VFP);
14276       break;
14277
14278     default:
14279       abort ();
14280     }
14281
14282   if (et.type != NT_invtype)
14283     {
14284       pfn (rs);
14285       return SUCCESS;
14286     }
14287
14288   inst.error = NULL;
14289   return FAIL;
14290 }
14291
14292 static void
14293 do_vfp_nsyn_mla_mls (enum neon_shape rs)
14294 {
14295   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
14296
14297   if (rs == NS_FFF || rs == NS_HHH)
14298     {
14299       if (is_mla)
14300         do_vfp_nsyn_opcode ("fmacs");
14301       else
14302         do_vfp_nsyn_opcode ("fnmacs");
14303
14304       /* ARMv8.2 fp16 instruction.  */
14305       if (rs == NS_HHH)
14306         do_scalar_fp16_v82_encode ();
14307     }
14308   else
14309     {
14310       if (is_mla)
14311         do_vfp_nsyn_opcode ("fmacd");
14312       else
14313         do_vfp_nsyn_opcode ("fnmacd");
14314     }
14315 }
14316
14317 static void
14318 do_vfp_nsyn_fma_fms (enum neon_shape rs)
14319 {
14320   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
14321
14322   if (rs == NS_FFF || rs == NS_HHH)
14323     {
14324       if (is_fma)
14325         do_vfp_nsyn_opcode ("ffmas");
14326       else
14327         do_vfp_nsyn_opcode ("ffnmas");
14328
14329       /* ARMv8.2 fp16 instruction.  */
14330       if (rs == NS_HHH)
14331         do_scalar_fp16_v82_encode ();
14332     }
14333   else
14334     {
14335       if (is_fma)
14336         do_vfp_nsyn_opcode ("ffmad");
14337       else
14338         do_vfp_nsyn_opcode ("ffnmad");
14339     }
14340 }
14341
14342 static void
14343 do_vfp_nsyn_mul (enum neon_shape rs)
14344 {
14345   if (rs == NS_FFF || rs == NS_HHH)
14346     {
14347       do_vfp_nsyn_opcode ("fmuls");
14348
14349       /* ARMv8.2 fp16 instruction.  */
14350       if (rs == NS_HHH)
14351         do_scalar_fp16_v82_encode ();
14352     }
14353   else
14354     do_vfp_nsyn_opcode ("fmuld");
14355 }
14356
14357 static void
14358 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14359 {
14360   int is_neg = (inst.instruction & 0x80) != 0;
14361   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
14362
14363   if (rs == NS_FF || rs == NS_HH)
14364     {
14365       if (is_neg)
14366         do_vfp_nsyn_opcode ("fnegs");
14367       else
14368         do_vfp_nsyn_opcode ("fabss");
14369
14370       /* ARMv8.2 fp16 instruction.  */
14371       if (rs == NS_HH)
14372         do_scalar_fp16_v82_encode ();
14373     }
14374   else
14375     {
14376       if (is_neg)
14377         do_vfp_nsyn_opcode ("fnegd");
14378       else
14379         do_vfp_nsyn_opcode ("fabsd");
14380     }
14381 }
14382
14383 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14384    insns belong to Neon, and are handled elsewhere.  */
14385
14386 static void
14387 do_vfp_nsyn_ldm_stm (int is_dbmode)
14388 {
14389   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14390   if (is_ldm)
14391     {
14392       if (is_dbmode)
14393         do_vfp_nsyn_opcode ("fldmdbs");
14394       else
14395         do_vfp_nsyn_opcode ("fldmias");
14396     }
14397   else
14398     {
14399       if (is_dbmode)
14400         do_vfp_nsyn_opcode ("fstmdbs");
14401       else
14402         do_vfp_nsyn_opcode ("fstmias");
14403     }
14404 }
14405
14406 static void
14407 do_vfp_nsyn_sqrt (void)
14408 {
14409   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14410   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14411
14412   if (rs == NS_FF || rs == NS_HH)
14413     {
14414       do_vfp_nsyn_opcode ("fsqrts");
14415
14416       /* ARMv8.2 fp16 instruction.  */
14417       if (rs == NS_HH)
14418         do_scalar_fp16_v82_encode ();
14419     }
14420   else
14421     do_vfp_nsyn_opcode ("fsqrtd");
14422 }
14423
14424 static void
14425 do_vfp_nsyn_div (void)
14426 {
14427   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14428   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14429                    N_F_ALL | N_KEY | N_VFP);
14430
14431   if (rs == NS_FFF || rs == NS_HHH)
14432     {
14433       do_vfp_nsyn_opcode ("fdivs");
14434
14435       /* ARMv8.2 fp16 instruction.  */
14436       if (rs == NS_HHH)
14437         do_scalar_fp16_v82_encode ();
14438     }
14439   else
14440     do_vfp_nsyn_opcode ("fdivd");
14441 }
14442
14443 static void
14444 do_vfp_nsyn_nmul (void)
14445 {
14446   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14447   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14448                    N_F_ALL | N_KEY | N_VFP);
14449
14450   if (rs == NS_FFF || rs == NS_HHH)
14451     {
14452       NEON_ENCODE (SINGLE, inst);
14453       do_vfp_sp_dyadic ();
14454
14455       /* ARMv8.2 fp16 instruction.  */
14456       if (rs == NS_HHH)
14457         do_scalar_fp16_v82_encode ();
14458     }
14459   else
14460     {
14461       NEON_ENCODE (DOUBLE, inst);
14462       do_vfp_dp_rd_rn_rm ();
14463     }
14464   do_vfp_cond_or_thumb ();
14465
14466 }
14467
14468 static void
14469 do_vfp_nsyn_cmp (void)
14470 {
14471   enum neon_shape rs;
14472   if (inst.operands[1].isreg)
14473     {
14474       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14475       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14476
14477       if (rs == NS_FF || rs == NS_HH)
14478         {
14479           NEON_ENCODE (SINGLE, inst);
14480           do_vfp_sp_monadic ();
14481         }
14482       else
14483         {
14484           NEON_ENCODE (DOUBLE, inst);
14485           do_vfp_dp_rd_rm ();
14486         }
14487     }
14488   else
14489     {
14490       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
14491       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
14492
14493       switch (inst.instruction & 0x0fffffff)
14494         {
14495         case N_MNEM_vcmp:
14496           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
14497           break;
14498         case N_MNEM_vcmpe:
14499           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
14500           break;
14501         default:
14502           abort ();
14503         }
14504
14505       if (rs == NS_FI || rs == NS_HI)
14506         {
14507           NEON_ENCODE (SINGLE, inst);
14508           do_vfp_sp_compare_z ();
14509         }
14510       else
14511         {
14512           NEON_ENCODE (DOUBLE, inst);
14513           do_vfp_dp_rd ();
14514         }
14515     }
14516   do_vfp_cond_or_thumb ();
14517
14518   /* ARMv8.2 fp16 instruction.  */
14519   if (rs == NS_HI || rs == NS_HH)
14520     do_scalar_fp16_v82_encode ();
14521 }
14522
14523 static void
14524 nsyn_insert_sp (void)
14525 {
14526   inst.operands[1] = inst.operands[0];
14527   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
14528   inst.operands[0].reg = REG_SP;
14529   inst.operands[0].isreg = 1;
14530   inst.operands[0].writeback = 1;
14531   inst.operands[0].present = 1;
14532 }
14533
14534 static void
14535 do_vfp_nsyn_push (void)
14536 {
14537   nsyn_insert_sp ();
14538
14539   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14540               _("register list must contain at least 1 and at most 16 "
14541                 "registers"));
14542
14543   if (inst.operands[1].issingle)
14544     do_vfp_nsyn_opcode ("fstmdbs");
14545   else
14546     do_vfp_nsyn_opcode ("fstmdbd");
14547 }
14548
14549 static void
14550 do_vfp_nsyn_pop (void)
14551 {
14552   nsyn_insert_sp ();
14553
14554   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14555               _("register list must contain at least 1 and at most 16 "
14556                 "registers"));
14557
14558   if (inst.operands[1].issingle)
14559     do_vfp_nsyn_opcode ("fldmias");
14560   else
14561     do_vfp_nsyn_opcode ("fldmiad");
14562 }
14563
14564 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14565    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14566
14567 static void
14568 neon_dp_fixup (struct arm_it* insn)
14569 {
14570   unsigned int i = insn->instruction;
14571   insn->is_neon = 1;
14572
14573   if (thumb_mode)
14574     {
14575       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14576       if (i & (1 << 24))
14577         i |= 1 << 28;
14578
14579       i &= ~(1 << 24);
14580
14581       i |= 0xef000000;
14582     }
14583   else
14584     i |= 0xf2000000;
14585
14586   insn->instruction = i;
14587 }
14588
14589 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14590    (0, 1, 2, 3).  */
14591
14592 static unsigned
14593 neon_logbits (unsigned x)
14594 {
14595   return ffs (x) - 4;
14596 }
14597
14598 #define LOW4(R) ((R) & 0xf)
14599 #define HI1(R) (((R) >> 4) & 1)
14600
14601 /* Encode insns with bit pattern:
14602
14603   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14604   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14605
14606   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14607   different meaning for some instruction.  */
14608
14609 static void
14610 neon_three_same (int isquad, int ubit, int size)
14611 {
14612   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14613   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14614   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14615   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14616   inst.instruction |= LOW4 (inst.operands[2].reg);
14617   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14618   inst.instruction |= (isquad != 0) << 6;
14619   inst.instruction |= (ubit != 0) << 24;
14620   if (size != -1)
14621     inst.instruction |= neon_logbits (size) << 20;
14622
14623   neon_dp_fixup (&inst);
14624 }
14625
14626 /* Encode instructions of the form:
14627
14628   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14629   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14630
14631   Don't write size if SIZE == -1.  */
14632
14633 static void
14634 neon_two_same (int qbit, int ubit, int size)
14635 {
14636   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14637   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14638   inst.instruction |= LOW4 (inst.operands[1].reg);
14639   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14640   inst.instruction |= (qbit != 0) << 6;
14641   inst.instruction |= (ubit != 0) << 24;
14642
14643   if (size != -1)
14644     inst.instruction |= neon_logbits (size) << 18;
14645
14646   neon_dp_fixup (&inst);
14647 }
14648
14649 /* Neon instruction encoders, in approximate order of appearance.  */
14650
14651 static void
14652 do_neon_dyadic_i_su (void)
14653 {
14654   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14655   struct neon_type_el et = neon_check_type (3, rs,
14656     N_EQK, N_EQK, N_SU_32 | N_KEY);
14657   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14658 }
14659
14660 static void
14661 do_neon_dyadic_i64_su (void)
14662 {
14663   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14664   struct neon_type_el et = neon_check_type (3, rs,
14665     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14666   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14667 }
14668
14669 static void
14670 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14671                 unsigned immbits)
14672 {
14673   unsigned size = et.size >> 3;
14674   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14675   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14676   inst.instruction |= LOW4 (inst.operands[1].reg);
14677   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14678   inst.instruction |= (isquad != 0) << 6;
14679   inst.instruction |= immbits << 16;
14680   inst.instruction |= (size >> 3) << 7;
14681   inst.instruction |= (size & 0x7) << 19;
14682   if (write_ubit)
14683     inst.instruction |= (uval != 0) << 24;
14684
14685   neon_dp_fixup (&inst);
14686 }
14687
14688 static void
14689 do_neon_shl_imm (void)
14690 {
14691   if (!inst.operands[2].isreg)
14692     {
14693       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14694       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14695       int imm = inst.operands[2].imm;
14696
14697       constraint (imm < 0 || (unsigned)imm >= et.size,
14698                   _("immediate out of range for shift"));
14699       NEON_ENCODE (IMMED, inst);
14700       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14701     }
14702   else
14703     {
14704       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14705       struct neon_type_el et = neon_check_type (3, rs,
14706         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14707       unsigned int tmp;
14708
14709       /* VSHL/VQSHL 3-register variants have syntax such as:
14710            vshl.xx Dd, Dm, Dn
14711          whereas other 3-register operations encoded by neon_three_same have
14712          syntax like:
14713            vadd.xx Dd, Dn, Dm
14714          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14715          here.  */
14716       tmp = inst.operands[2].reg;
14717       inst.operands[2].reg = inst.operands[1].reg;
14718       inst.operands[1].reg = tmp;
14719       NEON_ENCODE (INTEGER, inst);
14720       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14721     }
14722 }
14723
14724 static void
14725 do_neon_qshl_imm (void)
14726 {
14727   if (!inst.operands[2].isreg)
14728     {
14729       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14730       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14731       int imm = inst.operands[2].imm;
14732
14733       constraint (imm < 0 || (unsigned)imm >= et.size,
14734                   _("immediate out of range for shift"));
14735       NEON_ENCODE (IMMED, inst);
14736       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14737     }
14738   else
14739     {
14740       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14741       struct neon_type_el et = neon_check_type (3, rs,
14742         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14743       unsigned int tmp;
14744
14745       /* See note in do_neon_shl_imm.  */
14746       tmp = inst.operands[2].reg;
14747       inst.operands[2].reg = inst.operands[1].reg;
14748       inst.operands[1].reg = tmp;
14749       NEON_ENCODE (INTEGER, inst);
14750       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14751     }
14752 }
14753
14754 static void
14755 do_neon_rshl (void)
14756 {
14757   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14758   struct neon_type_el et = neon_check_type (3, rs,
14759     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14760   unsigned int tmp;
14761
14762   tmp = inst.operands[2].reg;
14763   inst.operands[2].reg = inst.operands[1].reg;
14764   inst.operands[1].reg = tmp;
14765   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14766 }
14767
14768 static int
14769 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14770 {
14771   /* Handle .I8 pseudo-instructions.  */
14772   if (size == 8)
14773     {
14774       /* Unfortunately, this will make everything apart from zero out-of-range.
14775          FIXME is this the intended semantics? There doesn't seem much point in
14776          accepting .I8 if so.  */
14777       immediate |= immediate << 8;
14778       size = 16;
14779     }
14780
14781   if (size >= 32)
14782     {
14783       if (immediate == (immediate & 0x000000ff))
14784         {
14785           *immbits = immediate;
14786           return 0x1;
14787         }
14788       else if (immediate == (immediate & 0x0000ff00))
14789         {
14790           *immbits = immediate >> 8;
14791           return 0x3;
14792         }
14793       else if (immediate == (immediate & 0x00ff0000))
14794         {
14795           *immbits = immediate >> 16;
14796           return 0x5;
14797         }
14798       else if (immediate == (immediate & 0xff000000))
14799         {
14800           *immbits = immediate >> 24;
14801           return 0x7;
14802         }
14803       if ((immediate & 0xffff) != (immediate >> 16))
14804         goto bad_immediate;
14805       immediate &= 0xffff;
14806     }
14807
14808   if (immediate == (immediate & 0x000000ff))
14809     {
14810       *immbits = immediate;
14811       return 0x9;
14812     }
14813   else if (immediate == (immediate & 0x0000ff00))
14814     {
14815       *immbits = immediate >> 8;
14816       return 0xb;
14817     }
14818
14819   bad_immediate:
14820   first_error (_("immediate value out of range"));
14821   return FAIL;
14822 }
14823
14824 static void
14825 do_neon_logic (void)
14826 {
14827   if (inst.operands[2].present && inst.operands[2].isreg)
14828     {
14829       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14830       neon_check_type (3, rs, N_IGNORE_TYPE);
14831       /* U bit and size field were set as part of the bitmask.  */
14832       NEON_ENCODE (INTEGER, inst);
14833       neon_three_same (neon_quad (rs), 0, -1);
14834     }
14835   else
14836     {
14837       const int three_ops_form = (inst.operands[2].present
14838                                   && !inst.operands[2].isreg);
14839       const int immoperand = (three_ops_form ? 2 : 1);
14840       enum neon_shape rs = (three_ops_form
14841                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14842                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14843       struct neon_type_el et = neon_check_type (2, rs,
14844         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14845       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14846       unsigned immbits;
14847       int cmode;
14848
14849       if (et.type == NT_invtype)
14850         return;
14851
14852       if (three_ops_form)
14853         constraint (inst.operands[0].reg != inst.operands[1].reg,
14854                     _("first and second operands shall be the same register"));
14855
14856       NEON_ENCODE (IMMED, inst);
14857
14858       immbits = inst.operands[immoperand].imm;
14859       if (et.size == 64)
14860         {
14861           /* .i64 is a pseudo-op, so the immediate must be a repeating
14862              pattern.  */
14863           if (immbits != (inst.operands[immoperand].regisimm ?
14864                           inst.operands[immoperand].reg : 0))
14865             {
14866               /* Set immbits to an invalid constant.  */
14867               immbits = 0xdeadbeef;
14868             }
14869         }
14870
14871       switch (opcode)
14872         {
14873         case N_MNEM_vbic:
14874           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14875           break;
14876
14877         case N_MNEM_vorr:
14878           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14879           break;
14880
14881         case N_MNEM_vand:
14882           /* Pseudo-instruction for VBIC.  */
14883           neon_invert_size (&immbits, 0, et.size);
14884           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14885           break;
14886
14887         case N_MNEM_vorn:
14888           /* Pseudo-instruction for VORR.  */
14889           neon_invert_size (&immbits, 0, et.size);
14890           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14891           break;
14892
14893         default:
14894           abort ();
14895         }
14896
14897       if (cmode == FAIL)
14898         return;
14899
14900       inst.instruction |= neon_quad (rs) << 6;
14901       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14902       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14903       inst.instruction |= cmode << 8;
14904       neon_write_immbits (immbits);
14905
14906       neon_dp_fixup (&inst);
14907     }
14908 }
14909
14910 static void
14911 do_neon_bitfield (void)
14912 {
14913   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14914   neon_check_type (3, rs, N_IGNORE_TYPE);
14915   neon_three_same (neon_quad (rs), 0, -1);
14916 }
14917
14918 static void
14919 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14920                   unsigned destbits)
14921 {
14922   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14923   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14924                                             types | N_KEY);
14925   if (et.type == NT_float)
14926     {
14927       NEON_ENCODE (FLOAT, inst);
14928       neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
14929     }
14930   else
14931     {
14932       NEON_ENCODE (INTEGER, inst);
14933       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14934     }
14935 }
14936
14937 static void
14938 do_neon_dyadic_if_su (void)
14939 {
14940   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14941 }
14942
14943 static void
14944 do_neon_dyadic_if_su_d (void)
14945 {
14946   /* This version only allow D registers, but that constraint is enforced during
14947      operand parsing so we don't need to do anything extra here.  */
14948   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14949 }
14950
14951 static void
14952 do_neon_dyadic_if_i_d (void)
14953 {
14954   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14955      affected if we specify unsigned args.  */
14956   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14957 }
14958
14959 enum vfp_or_neon_is_neon_bits
14960 {
14961   NEON_CHECK_CC = 1,
14962   NEON_CHECK_ARCH = 2,
14963   NEON_CHECK_ARCH8 = 4
14964 };
14965
14966 /* Call this function if an instruction which may have belonged to the VFP or
14967    Neon instruction sets, but turned out to be a Neon instruction (due to the
14968    operand types involved, etc.). We have to check and/or fix-up a couple of
14969    things:
14970
14971      - Make sure the user hasn't attempted to make a Neon instruction
14972        conditional.
14973      - Alter the value in the condition code field if necessary.
14974      - Make sure that the arch supports Neon instructions.
14975
14976    Which of these operations take place depends on bits from enum
14977    vfp_or_neon_is_neon_bits.
14978
14979    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14980    current instruction's condition is COND_ALWAYS, the condition field is
14981    changed to inst.uncond_value. This is necessary because instructions shared
14982    between VFP and Neon may be conditional for the VFP variants only, and the
14983    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14984
14985 static int
14986 vfp_or_neon_is_neon (unsigned check)
14987 {
14988   /* Conditions are always legal in Thumb mode (IT blocks).  */
14989   if (!thumb_mode && (check & NEON_CHECK_CC))
14990     {
14991       if (inst.cond != COND_ALWAYS)
14992         {
14993           first_error (_(BAD_COND));
14994           return FAIL;
14995         }
14996       if (inst.uncond_value != -1)
14997         inst.instruction |= inst.uncond_value << 28;
14998     }
14999
15000   if ((check & NEON_CHECK_ARCH)
15001       && !mark_feature_used (&fpu_neon_ext_v1))
15002     {
15003       first_error (_(BAD_FPU));
15004       return FAIL;
15005     }
15006
15007   if ((check & NEON_CHECK_ARCH8)
15008       && !mark_feature_used (&fpu_neon_ext_armv8))
15009     {
15010       first_error (_(BAD_FPU));
15011       return FAIL;
15012     }
15013
15014   return SUCCESS;
15015 }
15016
15017 static void
15018 do_neon_addsub_if_i (void)
15019 {
15020   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
15021     return;
15022
15023   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15024     return;
15025
15026   /* The "untyped" case can't happen. Do this to stop the "U" bit being
15027      affected if we specify unsigned args.  */
15028   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
15029 }
15030
15031 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
15032    result to be:
15033      V<op> A,B     (A is operand 0, B is operand 2)
15034    to mean:
15035      V<op> A,B,A
15036    not:
15037      V<op> A,B,B
15038    so handle that case specially.  */
15039
15040 static void
15041 neon_exchange_operands (void)
15042 {
15043   if (inst.operands[1].present)
15044     {
15045       void *scratch = xmalloc (sizeof (inst.operands[0]));
15046
15047       /* Swap operands[1] and operands[2].  */
15048       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
15049       inst.operands[1] = inst.operands[2];
15050       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
15051       free (scratch);
15052     }
15053   else
15054     {
15055       inst.operands[1] = inst.operands[2];
15056       inst.operands[2] = inst.operands[0];
15057     }
15058 }
15059
15060 static void
15061 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
15062 {
15063   if (inst.operands[2].isreg)
15064     {
15065       if (invert)
15066         neon_exchange_operands ();
15067       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
15068     }
15069   else
15070     {
15071       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15072       struct neon_type_el et = neon_check_type (2, rs,
15073         N_EQK | N_SIZ, immtypes | N_KEY);
15074
15075       NEON_ENCODE (IMMED, inst);
15076       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15077       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15078       inst.instruction |= LOW4 (inst.operands[1].reg);
15079       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15080       inst.instruction |= neon_quad (rs) << 6;
15081       inst.instruction |= (et.type == NT_float) << 10;
15082       inst.instruction |= neon_logbits (et.size) << 18;
15083
15084       neon_dp_fixup (&inst);
15085     }
15086 }
15087
15088 static void
15089 do_neon_cmp (void)
15090 {
15091   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
15092 }
15093
15094 static void
15095 do_neon_cmp_inv (void)
15096 {
15097   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
15098 }
15099
15100 static void
15101 do_neon_ceq (void)
15102 {
15103   neon_compare (N_IF_32, N_IF_32, FALSE);
15104 }
15105
15106 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
15107    scalars, which are encoded in 5 bits, M : Rm.
15108    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
15109    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
15110    index in M.
15111
15112    Dot Product instructions are similar to multiply instructions except elsize
15113    should always be 32.
15114
15115    This function translates SCALAR, which is GAS's internal encoding of indexed
15116    scalar register, to raw encoding.  There is also register and index range
15117    check based on ELSIZE.  */
15118
15119 static unsigned
15120 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
15121 {
15122   unsigned regno = NEON_SCALAR_REG (scalar);
15123   unsigned elno = NEON_SCALAR_INDEX (scalar);
15124
15125   switch (elsize)
15126     {
15127     case 16:
15128       if (regno > 7 || elno > 3)
15129         goto bad_scalar;
15130       return regno | (elno << 3);
15131
15132     case 32:
15133       if (regno > 15 || elno > 1)
15134         goto bad_scalar;
15135       return regno | (elno << 4);
15136
15137     default:
15138     bad_scalar:
15139       first_error (_("scalar out of range for multiply instruction"));
15140     }
15141
15142   return 0;
15143 }
15144
15145 /* Encode multiply / multiply-accumulate scalar instructions.  */
15146
15147 static void
15148 neon_mul_mac (struct neon_type_el et, int ubit)
15149 {
15150   unsigned scalar;
15151
15152   /* Give a more helpful error message if we have an invalid type.  */
15153   if (et.type == NT_invtype)
15154     return;
15155
15156   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
15157   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15158   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15159   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15160   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15161   inst.instruction |= LOW4 (scalar);
15162   inst.instruction |= HI1 (scalar) << 5;
15163   inst.instruction |= (et.type == NT_float) << 8;
15164   inst.instruction |= neon_logbits (et.size) << 20;
15165   inst.instruction |= (ubit != 0) << 24;
15166
15167   neon_dp_fixup (&inst);
15168 }
15169
15170 static void
15171 do_neon_mac_maybe_scalar (void)
15172 {
15173   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
15174     return;
15175
15176   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15177     return;
15178
15179   if (inst.operands[2].isscalar)
15180     {
15181       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15182       struct neon_type_el et = neon_check_type (3, rs,
15183         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
15184       NEON_ENCODE (SCALAR, inst);
15185       neon_mul_mac (et, neon_quad (rs));
15186     }
15187   else
15188     {
15189       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
15190          affected if we specify unsigned args.  */
15191       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15192     }
15193 }
15194
15195 static void
15196 do_neon_fmac (void)
15197 {
15198   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
15199     return;
15200
15201   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15202     return;
15203
15204   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15205 }
15206
15207 static void
15208 do_neon_tst (void)
15209 {
15210   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15211   struct neon_type_el et = neon_check_type (3, rs,
15212     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
15213   neon_three_same (neon_quad (rs), 0, et.size);
15214 }
15215
15216 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
15217    same types as the MAC equivalents. The polynomial type for this instruction
15218    is encoded the same as the integer type.  */
15219
15220 static void
15221 do_neon_mul (void)
15222 {
15223   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
15224     return;
15225
15226   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15227     return;
15228
15229   if (inst.operands[2].isscalar)
15230     do_neon_mac_maybe_scalar ();
15231   else
15232     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
15233 }
15234
15235 static void
15236 do_neon_qdmulh (void)
15237 {
15238   if (inst.operands[2].isscalar)
15239     {
15240       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15241       struct neon_type_el et = neon_check_type (3, rs,
15242         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15243       NEON_ENCODE (SCALAR, inst);
15244       neon_mul_mac (et, neon_quad (rs));
15245     }
15246   else
15247     {
15248       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15249       struct neon_type_el et = neon_check_type (3, rs,
15250         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15251       NEON_ENCODE (INTEGER, inst);
15252       /* The U bit (rounding) comes from bit mask.  */
15253       neon_three_same (neon_quad (rs), 0, et.size);
15254     }
15255 }
15256
15257 static void
15258 do_neon_qrdmlah (void)
15259 {
15260   /* Check we're on the correct architecture.  */
15261   if (!mark_feature_used (&fpu_neon_ext_armv8))
15262     inst.error =
15263       _("instruction form not available on this architecture.");
15264   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
15265     {
15266       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
15267       record_feature_use (&fpu_neon_ext_v8_1);
15268     }
15269
15270   if (inst.operands[2].isscalar)
15271     {
15272       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15273       struct neon_type_el et = neon_check_type (3, rs,
15274         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15275       NEON_ENCODE (SCALAR, inst);
15276       neon_mul_mac (et, neon_quad (rs));
15277     }
15278   else
15279     {
15280       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15281       struct neon_type_el et = neon_check_type (3, rs,
15282         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15283       NEON_ENCODE (INTEGER, inst);
15284       /* The U bit (rounding) comes from bit mask.  */
15285       neon_three_same (neon_quad (rs), 0, et.size);
15286     }
15287 }
15288
15289 static void
15290 do_neon_fcmp_absolute (void)
15291 {
15292   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15293   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15294                                             N_F_16_32 | N_KEY);
15295   /* Size field comes from bit mask.  */
15296   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
15297 }
15298
15299 static void
15300 do_neon_fcmp_absolute_inv (void)
15301 {
15302   neon_exchange_operands ();
15303   do_neon_fcmp_absolute ();
15304 }
15305
15306 static void
15307 do_neon_step (void)
15308 {
15309   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15310   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15311                                             N_F_16_32 | N_KEY);
15312   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15313 }
15314
15315 static void
15316 do_neon_abs_neg (void)
15317 {
15318   enum neon_shape rs;
15319   struct neon_type_el et;
15320
15321   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
15322     return;
15323
15324   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15325     return;
15326
15327   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15328   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
15329
15330   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15331   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15332   inst.instruction |= LOW4 (inst.operands[1].reg);
15333   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15334   inst.instruction |= neon_quad (rs) << 6;
15335   inst.instruction |= (et.type == NT_float) << 10;
15336   inst.instruction |= neon_logbits (et.size) << 18;
15337
15338   neon_dp_fixup (&inst);
15339 }
15340
15341 static void
15342 do_neon_sli (void)
15343 {
15344   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15345   struct neon_type_el et = neon_check_type (2, rs,
15346     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15347   int imm = inst.operands[2].imm;
15348   constraint (imm < 0 || (unsigned)imm >= et.size,
15349               _("immediate out of range for insert"));
15350   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15351 }
15352
15353 static void
15354 do_neon_sri (void)
15355 {
15356   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15357   struct neon_type_el et = neon_check_type (2, rs,
15358     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15359   int imm = inst.operands[2].imm;
15360   constraint (imm < 1 || (unsigned)imm > et.size,
15361               _("immediate out of range for insert"));
15362   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
15363 }
15364
15365 static void
15366 do_neon_qshlu_imm (void)
15367 {
15368   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15369   struct neon_type_el et = neon_check_type (2, rs,
15370     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
15371   int imm = inst.operands[2].imm;
15372   constraint (imm < 0 || (unsigned)imm >= et.size,
15373               _("immediate out of range for shift"));
15374   /* Only encodes the 'U present' variant of the instruction.
15375      In this case, signed types have OP (bit 8) set to 0.
15376      Unsigned types have OP set to 1.  */
15377   inst.instruction |= (et.type == NT_unsigned) << 8;
15378   /* The rest of the bits are the same as other immediate shifts.  */
15379   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15380 }
15381
15382 static void
15383 do_neon_qmovn (void)
15384 {
15385   struct neon_type_el et = neon_check_type (2, NS_DQ,
15386     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15387   /* Saturating move where operands can be signed or unsigned, and the
15388      destination has the same signedness.  */
15389   NEON_ENCODE (INTEGER, inst);
15390   if (et.type == NT_unsigned)
15391     inst.instruction |= 0xc0;
15392   else
15393     inst.instruction |= 0x80;
15394   neon_two_same (0, 1, et.size / 2);
15395 }
15396
15397 static void
15398 do_neon_qmovun (void)
15399 {
15400   struct neon_type_el et = neon_check_type (2, NS_DQ,
15401     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15402   /* Saturating move with unsigned results. Operands must be signed.  */
15403   NEON_ENCODE (INTEGER, inst);
15404   neon_two_same (0, 1, et.size / 2);
15405 }
15406
15407 static void
15408 do_neon_rshift_sat_narrow (void)
15409 {
15410   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15411      or unsigned. If operands are unsigned, results must also be unsigned.  */
15412   struct neon_type_el et = neon_check_type (2, NS_DQI,
15413     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15414   int imm = inst.operands[2].imm;
15415   /* This gets the bounds check, size encoding and immediate bits calculation
15416      right.  */
15417   et.size /= 2;
15418
15419   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
15420      VQMOVN.I<size> <Dd>, <Qm>.  */
15421   if (imm == 0)
15422     {
15423       inst.operands[2].present = 0;
15424       inst.instruction = N_MNEM_vqmovn;
15425       do_neon_qmovn ();
15426       return;
15427     }
15428
15429   constraint (imm < 1 || (unsigned)imm > et.size,
15430               _("immediate out of range"));
15431   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
15432 }
15433
15434 static void
15435 do_neon_rshift_sat_narrow_u (void)
15436 {
15437   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15438      or unsigned. If operands are unsigned, results must also be unsigned.  */
15439   struct neon_type_el et = neon_check_type (2, NS_DQI,
15440     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15441   int imm = inst.operands[2].imm;
15442   /* This gets the bounds check, size encoding and immediate bits calculation
15443      right.  */
15444   et.size /= 2;
15445
15446   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
15447      VQMOVUN.I<size> <Dd>, <Qm>.  */
15448   if (imm == 0)
15449     {
15450       inst.operands[2].present = 0;
15451       inst.instruction = N_MNEM_vqmovun;
15452       do_neon_qmovun ();
15453       return;
15454     }
15455
15456   constraint (imm < 1 || (unsigned)imm > et.size,
15457               _("immediate out of range"));
15458   /* FIXME: The manual is kind of unclear about what value U should have in
15459      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
15460      must be 1.  */
15461   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
15462 }
15463
15464 static void
15465 do_neon_movn (void)
15466 {
15467   struct neon_type_el et = neon_check_type (2, NS_DQ,
15468     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15469   NEON_ENCODE (INTEGER, inst);
15470   neon_two_same (0, 1, et.size / 2);
15471 }
15472
15473 static void
15474 do_neon_rshift_narrow (void)
15475 {
15476   struct neon_type_el et = neon_check_type (2, NS_DQI,
15477     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15478   int imm = inst.operands[2].imm;
15479   /* This gets the bounds check, size encoding and immediate bits calculation
15480      right.  */
15481   et.size /= 2;
15482
15483   /* If immediate is zero then we are a pseudo-instruction for
15484      VMOVN.I<size> <Dd>, <Qm>  */
15485   if (imm == 0)
15486     {
15487       inst.operands[2].present = 0;
15488       inst.instruction = N_MNEM_vmovn;
15489       do_neon_movn ();
15490       return;
15491     }
15492
15493   constraint (imm < 1 || (unsigned)imm > et.size,
15494               _("immediate out of range for narrowing operation"));
15495   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
15496 }
15497
15498 static void
15499 do_neon_shll (void)
15500 {
15501   /* FIXME: Type checking when lengthening.  */
15502   struct neon_type_el et = neon_check_type (2, NS_QDI,
15503     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
15504   unsigned imm = inst.operands[2].imm;
15505
15506   if (imm == et.size)
15507     {
15508       /* Maximum shift variant.  */
15509       NEON_ENCODE (INTEGER, inst);
15510       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15511       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15512       inst.instruction |= LOW4 (inst.operands[1].reg);
15513       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15514       inst.instruction |= neon_logbits (et.size) << 18;
15515
15516       neon_dp_fixup (&inst);
15517     }
15518   else
15519     {
15520       /* A more-specific type check for non-max versions.  */
15521       et = neon_check_type (2, NS_QDI,
15522         N_EQK | N_DBL, N_SU_32 | N_KEY);
15523       NEON_ENCODE (IMMED, inst);
15524       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
15525     }
15526 }
15527
15528 /* Check the various types for the VCVT instruction, and return which version
15529    the current instruction is.  */
15530
15531 #define CVT_FLAVOUR_VAR                                                       \
15532   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
15533   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
15534   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
15535   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
15536   /* Half-precision conversions.  */                                          \
15537   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15538   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15539   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
15540   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
15541   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
15542   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
15543   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
15544      Compared with single/double precision variants, only the co-processor    \
15545      field is different, so the encoding flow is reused here.  */             \
15546   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
15547   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
15548   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
15549   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
15550   /* VFP instructions.  */                                                    \
15551   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
15552   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
15553   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
15554   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
15555   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
15556   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
15557   /* VFP instructions with bitshift.  */                                      \
15558   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
15559   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
15560   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
15561   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
15562   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
15563   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
15564   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
15565   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
15566
15567 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
15568   neon_cvt_flavour_##C,
15569
15570 /* The different types of conversions we can do.  */
15571 enum neon_cvt_flavour
15572 {
15573   CVT_FLAVOUR_VAR
15574   neon_cvt_flavour_invalid,
15575   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
15576 };
15577
15578 #undef CVT_VAR
15579
15580 static enum neon_cvt_flavour
15581 get_neon_cvt_flavour (enum neon_shape rs)
15582 {
15583 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
15584   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
15585   if (et.type != NT_invtype)                            \
15586     {                                                   \
15587       inst.error = NULL;                                \
15588       return (neon_cvt_flavour_##C);                    \
15589     }
15590
15591   struct neon_type_el et;
15592   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15593                         || rs == NS_FF) ? N_VFP : 0;
15594   /* The instruction versions which take an immediate take one register
15595      argument, which is extended to the width of the full register. Thus the
15596      "source" and "destination" registers must have the same width.  Hack that
15597      here by making the size equal to the key (wider, in this case) operand.  */
15598   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15599
15600   CVT_FLAVOUR_VAR;
15601
15602   return neon_cvt_flavour_invalid;
15603 #undef CVT_VAR
15604 }
15605
15606 enum neon_cvt_mode
15607 {
15608   neon_cvt_mode_a,
15609   neon_cvt_mode_n,
15610   neon_cvt_mode_p,
15611   neon_cvt_mode_m,
15612   neon_cvt_mode_z,
15613   neon_cvt_mode_x,
15614   neon_cvt_mode_r
15615 };
15616
15617 /* Neon-syntax VFP conversions.  */
15618
15619 static void
15620 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15621 {
15622   const char *opname = 0;
15623
15624   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
15625       || rs == NS_FHI || rs == NS_HFI)
15626     {
15627       /* Conversions with immediate bitshift.  */
15628       const char *enc[] =
15629         {
15630 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15631           CVT_FLAVOUR_VAR
15632           NULL
15633 #undef CVT_VAR
15634         };
15635
15636       if (flavour < (int) ARRAY_SIZE (enc))
15637         {
15638           opname = enc[flavour];
15639           constraint (inst.operands[0].reg != inst.operands[1].reg,
15640                       _("operands 0 and 1 must be the same register"));
15641           inst.operands[1] = inst.operands[2];
15642           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15643         }
15644     }
15645   else
15646     {
15647       /* Conversions without bitshift.  */
15648       const char *enc[] =
15649         {
15650 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15651           CVT_FLAVOUR_VAR
15652           NULL
15653 #undef CVT_VAR
15654         };
15655
15656       if (flavour < (int) ARRAY_SIZE (enc))
15657         opname = enc[flavour];
15658     }
15659
15660   if (opname)
15661     do_vfp_nsyn_opcode (opname);
15662
15663   /* ARMv8.2 fp16 VCVT instruction.  */
15664   if (flavour == neon_cvt_flavour_s32_f16
15665       || flavour == neon_cvt_flavour_u32_f16
15666       || flavour == neon_cvt_flavour_f16_u32
15667       || flavour == neon_cvt_flavour_f16_s32)
15668     do_scalar_fp16_v82_encode ();
15669 }
15670
15671 static void
15672 do_vfp_nsyn_cvtz (void)
15673 {
15674   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
15675   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15676   const char *enc[] =
15677     {
15678 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15679       CVT_FLAVOUR_VAR
15680       NULL
15681 #undef CVT_VAR
15682     };
15683
15684   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15685     do_vfp_nsyn_opcode (enc[flavour]);
15686 }
15687
15688 static void
15689 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15690                       enum neon_cvt_mode mode)
15691 {
15692   int sz, op;
15693   int rm;
15694
15695   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15696      D register operands.  */
15697   if (flavour == neon_cvt_flavour_s32_f64
15698       || flavour == neon_cvt_flavour_u32_f64)
15699     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15700                 _(BAD_FPU));
15701
15702   if (flavour == neon_cvt_flavour_s32_f16
15703       || flavour == neon_cvt_flavour_u32_f16)
15704     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
15705                 _(BAD_FP16));
15706
15707   set_it_insn_type (OUTSIDE_IT_INSN);
15708
15709   switch (flavour)
15710     {
15711     case neon_cvt_flavour_s32_f64:
15712       sz = 1;
15713       op = 1;
15714       break;
15715     case neon_cvt_flavour_s32_f32:
15716       sz = 0;
15717       op = 1;
15718       break;
15719     case neon_cvt_flavour_s32_f16:
15720       sz = 0;
15721       op = 1;
15722       break;
15723     case neon_cvt_flavour_u32_f64:
15724       sz = 1;
15725       op = 0;
15726       break;
15727     case neon_cvt_flavour_u32_f32:
15728       sz = 0;
15729       op = 0;
15730       break;
15731     case neon_cvt_flavour_u32_f16:
15732       sz = 0;
15733       op = 0;
15734       break;
15735     default:
15736       first_error (_("invalid instruction shape"));
15737       return;
15738     }
15739
15740   switch (mode)
15741     {
15742     case neon_cvt_mode_a: rm = 0; break;
15743     case neon_cvt_mode_n: rm = 1; break;
15744     case neon_cvt_mode_p: rm = 2; break;
15745     case neon_cvt_mode_m: rm = 3; break;
15746     default: first_error (_("invalid rounding mode")); return;
15747     }
15748
15749   NEON_ENCODE (FPV8, inst);
15750   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15751   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15752   inst.instruction |= sz << 8;
15753
15754   /* ARMv8.2 fp16 VCVT instruction.  */
15755   if (flavour == neon_cvt_flavour_s32_f16
15756       ||flavour == neon_cvt_flavour_u32_f16)
15757     do_scalar_fp16_v82_encode ();
15758   inst.instruction |= op << 7;
15759   inst.instruction |= rm << 16;
15760   inst.instruction |= 0xf0000000;
15761   inst.is_neon = TRUE;
15762 }
15763
15764 static void
15765 do_neon_cvt_1 (enum neon_cvt_mode mode)
15766 {
15767   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15768                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
15769                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
15770                                           NS_NULL);
15771   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15772
15773   if (flavour == neon_cvt_flavour_invalid)
15774     return;
15775
15776   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15777   if (mode == neon_cvt_mode_z
15778       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15779       && (flavour == neon_cvt_flavour_s16_f16
15780           || flavour == neon_cvt_flavour_u16_f16
15781           || flavour == neon_cvt_flavour_s32_f32
15782           || flavour == neon_cvt_flavour_u32_f32
15783           || flavour == neon_cvt_flavour_s32_f64
15784           || flavour == neon_cvt_flavour_u32_f64)
15785       && (rs == NS_FD || rs == NS_FF))
15786     {
15787       do_vfp_nsyn_cvtz ();
15788       return;
15789     }
15790
15791   /* ARMv8.2 fp16 VCVT conversions.  */
15792   if (mode == neon_cvt_mode_z
15793       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
15794       && (flavour == neon_cvt_flavour_s32_f16
15795           || flavour == neon_cvt_flavour_u32_f16)
15796       && (rs == NS_FH))
15797     {
15798       do_vfp_nsyn_cvtz ();
15799       do_scalar_fp16_v82_encode ();
15800       return;
15801     }
15802
15803   /* VFP rather than Neon conversions.  */
15804   if (flavour >= neon_cvt_flavour_first_fp)
15805     {
15806       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15807         do_vfp_nsyn_cvt (rs, flavour);
15808       else
15809         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15810
15811       return;
15812     }
15813
15814   switch (rs)
15815     {
15816     case NS_DDI:
15817     case NS_QQI:
15818       {
15819         unsigned immbits;
15820         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
15821                              0x0000100, 0x1000100, 0x0, 0x1000000};
15822
15823         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15824           return;
15825
15826         /* Fixed-point conversion with #0 immediate is encoded as an
15827            integer conversion.  */
15828         if (inst.operands[2].present && inst.operands[2].imm == 0)
15829           goto int_encode;
15830         NEON_ENCODE (IMMED, inst);
15831         if (flavour != neon_cvt_flavour_invalid)
15832           inst.instruction |= enctab[flavour];
15833         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15834         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15835         inst.instruction |= LOW4 (inst.operands[1].reg);
15836         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15837         inst.instruction |= neon_quad (rs) << 6;
15838         inst.instruction |= 1 << 21;
15839         if (flavour < neon_cvt_flavour_s16_f16)
15840           {
15841             inst.instruction |= 1 << 21;
15842             immbits = 32 - inst.operands[2].imm;
15843             inst.instruction |= immbits << 16;
15844           }
15845         else
15846           {
15847             inst.instruction |= 3 << 20;
15848             immbits = 16 - inst.operands[2].imm;
15849             inst.instruction |= immbits << 16;
15850             inst.instruction &= ~(1 << 9);
15851           }
15852
15853         neon_dp_fixup (&inst);
15854       }
15855       break;
15856
15857     case NS_DD:
15858     case NS_QQ:
15859       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15860         {
15861           NEON_ENCODE (FLOAT, inst);
15862           set_it_insn_type (OUTSIDE_IT_INSN);
15863
15864           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15865             return;
15866
15867           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15868           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15869           inst.instruction |= LOW4 (inst.operands[1].reg);
15870           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15871           inst.instruction |= neon_quad (rs) << 6;
15872           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
15873                                || flavour == neon_cvt_flavour_u32_f32) << 7;
15874           inst.instruction |= mode << 8;
15875           if (flavour == neon_cvt_flavour_u16_f16
15876               || flavour == neon_cvt_flavour_s16_f16)
15877             /* Mask off the original size bits and reencode them.  */
15878             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
15879
15880           if (thumb_mode)
15881             inst.instruction |= 0xfc000000;
15882           else
15883             inst.instruction |= 0xf0000000;
15884         }
15885       else
15886         {
15887     int_encode:
15888           {
15889             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
15890                                   0x100, 0x180, 0x0, 0x080};
15891
15892             NEON_ENCODE (INTEGER, inst);
15893
15894             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15895               return;
15896
15897             if (flavour != neon_cvt_flavour_invalid)
15898               inst.instruction |= enctab[flavour];
15899
15900             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15901             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15902             inst.instruction |= LOW4 (inst.operands[1].reg);
15903             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15904             inst.instruction |= neon_quad (rs) << 6;
15905             if (flavour >= neon_cvt_flavour_s16_f16
15906                 && flavour <= neon_cvt_flavour_f16_u16)
15907               /* Half precision.  */
15908               inst.instruction |= 1 << 18;
15909             else
15910               inst.instruction |= 2 << 18;
15911
15912             neon_dp_fixup (&inst);
15913           }
15914         }
15915       break;
15916
15917     /* Half-precision conversions for Advanced SIMD -- neon.  */
15918     case NS_QD:
15919     case NS_DQ:
15920       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15921         return;
15922
15923       if ((rs == NS_DQ)
15924           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15925           {
15926             as_bad (_("operand size must match register width"));
15927             break;
15928           }
15929
15930       if ((rs == NS_QD)
15931           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15932           {
15933             as_bad (_("operand size must match register width"));
15934             break;
15935           }
15936
15937       if (rs == NS_DQ)
15938         inst.instruction = 0x3b60600;
15939       else
15940         inst.instruction = 0x3b60700;
15941
15942       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15943       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15944       inst.instruction |= LOW4 (inst.operands[1].reg);
15945       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15946       neon_dp_fixup (&inst);
15947       break;
15948
15949     default:
15950       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
15951       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15952         do_vfp_nsyn_cvt (rs, flavour);
15953       else
15954         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15955     }
15956 }
15957
15958 static void
15959 do_neon_cvtr (void)
15960 {
15961   do_neon_cvt_1 (neon_cvt_mode_x);
15962 }
15963
15964 static void
15965 do_neon_cvt (void)
15966 {
15967   do_neon_cvt_1 (neon_cvt_mode_z);
15968 }
15969
15970 static void
15971 do_neon_cvta (void)
15972 {
15973   do_neon_cvt_1 (neon_cvt_mode_a);
15974 }
15975
15976 static void
15977 do_neon_cvtn (void)
15978 {
15979   do_neon_cvt_1 (neon_cvt_mode_n);
15980 }
15981
15982 static void
15983 do_neon_cvtp (void)
15984 {
15985   do_neon_cvt_1 (neon_cvt_mode_p);
15986 }
15987
15988 static void
15989 do_neon_cvtm (void)
15990 {
15991   do_neon_cvt_1 (neon_cvt_mode_m);
15992 }
15993
15994 static void
15995 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15996 {
15997   if (is_double)
15998     mark_feature_used (&fpu_vfp_ext_armv8);
15999
16000   encode_arm_vfp_reg (inst.operands[0].reg,
16001                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
16002   encode_arm_vfp_reg (inst.operands[1].reg,
16003                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
16004   inst.instruction |= to ? 0x10000 : 0;
16005   inst.instruction |= t ? 0x80 : 0;
16006   inst.instruction |= is_double ? 0x100 : 0;
16007   do_vfp_cond_or_thumb ();
16008 }
16009
16010 static void
16011 do_neon_cvttb_1 (bfd_boolean t)
16012 {
16013   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
16014                                           NS_DF, NS_DH, NS_NULL);
16015
16016   if (rs == NS_NULL)
16017     return;
16018   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
16019     {
16020       inst.error = NULL;
16021       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
16022     }
16023   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
16024     {
16025       inst.error = NULL;
16026       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
16027     }
16028   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
16029     {
16030       /* The VCVTB and VCVTT instructions with D-register operands
16031          don't work for SP only targets.  */
16032       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16033                   _(BAD_FPU));
16034
16035       inst.error = NULL;
16036       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
16037     }
16038   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
16039     {
16040       /* The VCVTB and VCVTT instructions with D-register operands
16041          don't work for SP only targets.  */
16042       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16043                   _(BAD_FPU));
16044
16045       inst.error = NULL;
16046       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
16047     }
16048   else
16049     return;
16050 }
16051
16052 static void
16053 do_neon_cvtb (void)
16054 {
16055   do_neon_cvttb_1 (FALSE);
16056 }
16057
16058
16059 static void
16060 do_neon_cvtt (void)
16061 {
16062   do_neon_cvttb_1 (TRUE);
16063 }
16064
16065 static void
16066 neon_move_immediate (void)
16067 {
16068   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
16069   struct neon_type_el et = neon_check_type (2, rs,
16070     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
16071   unsigned immlo, immhi = 0, immbits;
16072   int op, cmode, float_p;
16073
16074   constraint (et.type == NT_invtype,
16075               _("operand size must be specified for immediate VMOV"));
16076
16077   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
16078   op = (inst.instruction & (1 << 5)) != 0;
16079
16080   immlo = inst.operands[1].imm;
16081   if (inst.operands[1].regisimm)
16082     immhi = inst.operands[1].reg;
16083
16084   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
16085               _("immediate has bits set outside the operand size"));
16086
16087   float_p = inst.operands[1].immisfloat;
16088
16089   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
16090                                         et.size, et.type)) == FAIL)
16091     {
16092       /* Invert relevant bits only.  */
16093       neon_invert_size (&immlo, &immhi, et.size);
16094       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
16095          with one or the other; those cases are caught by
16096          neon_cmode_for_move_imm.  */
16097       op = !op;
16098       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
16099                                             &op, et.size, et.type)) == FAIL)
16100         {
16101           first_error (_("immediate out of range"));
16102           return;
16103         }
16104     }
16105
16106   inst.instruction &= ~(1 << 5);
16107   inst.instruction |= op << 5;
16108
16109   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16110   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16111   inst.instruction |= neon_quad (rs) << 6;
16112   inst.instruction |= cmode << 8;
16113
16114   neon_write_immbits (immbits);
16115 }
16116
16117 static void
16118 do_neon_mvn (void)
16119 {
16120   if (inst.operands[1].isreg)
16121     {
16122       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16123
16124       NEON_ENCODE (INTEGER, inst);
16125       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16126       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16127       inst.instruction |= LOW4 (inst.operands[1].reg);
16128       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16129       inst.instruction |= neon_quad (rs) << 6;
16130     }
16131   else
16132     {
16133       NEON_ENCODE (IMMED, inst);
16134       neon_move_immediate ();
16135     }
16136
16137   neon_dp_fixup (&inst);
16138 }
16139
16140 /* Encode instructions of form:
16141
16142   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16143   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
16144
16145 static void
16146 neon_mixed_length (struct neon_type_el et, unsigned size)
16147 {
16148   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16149   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16150   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16151   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16152   inst.instruction |= LOW4 (inst.operands[2].reg);
16153   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16154   inst.instruction |= (et.type == NT_unsigned) << 24;
16155   inst.instruction |= neon_logbits (size) << 20;
16156
16157   neon_dp_fixup (&inst);
16158 }
16159
16160 static void
16161 do_neon_dyadic_long (void)
16162 {
16163   /* FIXME: Type checking for lengthening op.  */
16164   struct neon_type_el et = neon_check_type (3, NS_QDD,
16165     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
16166   neon_mixed_length (et, et.size);
16167 }
16168
16169 static void
16170 do_neon_abal (void)
16171 {
16172   struct neon_type_el et = neon_check_type (3, NS_QDD,
16173     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
16174   neon_mixed_length (et, et.size);
16175 }
16176
16177 static void
16178 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
16179 {
16180   if (inst.operands[2].isscalar)
16181     {
16182       struct neon_type_el et = neon_check_type (3, NS_QDS,
16183         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
16184       NEON_ENCODE (SCALAR, inst);
16185       neon_mul_mac (et, et.type == NT_unsigned);
16186     }
16187   else
16188     {
16189       struct neon_type_el et = neon_check_type (3, NS_QDD,
16190         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
16191       NEON_ENCODE (INTEGER, inst);
16192       neon_mixed_length (et, et.size);
16193     }
16194 }
16195
16196 static void
16197 do_neon_mac_maybe_scalar_long (void)
16198 {
16199   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
16200 }
16201
16202 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
16203    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
16204
16205 static unsigned
16206 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
16207 {
16208   unsigned regno = NEON_SCALAR_REG (scalar);
16209   unsigned elno = NEON_SCALAR_INDEX (scalar);
16210
16211   if (quad_p)
16212     {
16213       if (regno > 7 || elno > 3)
16214         goto bad_scalar;
16215
16216       return ((regno & 0x7)
16217               | ((elno & 0x1) << 3)
16218               | (((elno >> 1) & 0x1) << 5));
16219     }
16220   else
16221     {
16222       if (regno > 15 || elno > 1)
16223         goto bad_scalar;
16224
16225       return (((regno & 0x1) << 5)
16226               | ((regno >> 1) & 0x7)
16227               | ((elno & 0x1) << 3));
16228     }
16229
16230 bad_scalar:
16231   first_error (_("scalar out of range for multiply instruction"));
16232   return 0;
16233 }
16234
16235 static void
16236 do_neon_fmac_maybe_scalar_long (int subtype)
16237 {
16238   enum neon_shape rs;
16239   int high8;
16240   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
16241      field (bits[21:20]) has different meaning.  For scalar index variant, it's
16242      used to differentiate add and subtract, otherwise it's with fixed value
16243      0x2.  */
16244   int size = -1;
16245
16246   if (inst.cond != COND_ALWAYS)
16247     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
16248                "behaviour is UNPREDICTABLE"));
16249
16250   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
16251               _(BAD_FP16));
16252
16253   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
16254               _(BAD_FPU));
16255
16256   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
16257      be a scalar index register.  */
16258   if (inst.operands[2].isscalar)
16259     {
16260       high8 = 0xfe000000;
16261       if (subtype)
16262         size = 16;
16263       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
16264     }
16265   else
16266     {
16267       high8 = 0xfc000000;
16268       size = 32;
16269       if (subtype)
16270         inst.instruction |= (0x1 << 23);
16271       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
16272     }
16273
16274   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
16275
16276   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
16277      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
16278      so we simply pass -1 as size.  */
16279   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
16280   neon_three_same (quad_p, 0, size);
16281
16282   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
16283   inst.instruction &= 0x00ffffff;
16284   inst.instruction |= high8;
16285
16286 #define LOW1(R) ((R) & 0x1)
16287 #define HI4(R) (((R) >> 1) & 0xf)
16288   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
16289      whether the instruction is in Q form and whether Vm is a scalar indexed
16290      operand.  */
16291   if (inst.operands[2].isscalar)
16292     {
16293       unsigned rm
16294         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
16295       inst.instruction &= 0xffffffd0;
16296       inst.instruction |= rm;
16297
16298       if (!quad_p)
16299         {
16300           /* Redo Rn as well.  */
16301           inst.instruction &= 0xfff0ff7f;
16302           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16303           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16304         }
16305     }
16306   else if (!quad_p)
16307     {
16308       /* Redo Rn and Rm.  */
16309       inst.instruction &= 0xfff0ff50;
16310       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16311       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16312       inst.instruction |= HI4 (inst.operands[2].reg);
16313       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
16314     }
16315 }
16316
16317 static void
16318 do_neon_vfmal (void)
16319 {
16320   return do_neon_fmac_maybe_scalar_long (0);
16321 }
16322
16323 static void
16324 do_neon_vfmsl (void)
16325 {
16326   return do_neon_fmac_maybe_scalar_long (1);
16327 }
16328
16329 static void
16330 do_neon_dyadic_wide (void)
16331 {
16332   struct neon_type_el et = neon_check_type (3, NS_QQD,
16333     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
16334   neon_mixed_length (et, et.size);
16335 }
16336
16337 static void
16338 do_neon_dyadic_narrow (void)
16339 {
16340   struct neon_type_el et = neon_check_type (3, NS_QDD,
16341     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
16342   /* Operand sign is unimportant, and the U bit is part of the opcode,
16343      so force the operand type to integer.  */
16344   et.type = NT_integer;
16345   neon_mixed_length (et, et.size / 2);
16346 }
16347
16348 static void
16349 do_neon_mul_sat_scalar_long (void)
16350 {
16351   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
16352 }
16353
16354 static void
16355 do_neon_vmull (void)
16356 {
16357   if (inst.operands[2].isscalar)
16358     do_neon_mac_maybe_scalar_long ();
16359   else
16360     {
16361       struct neon_type_el et = neon_check_type (3, NS_QDD,
16362         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
16363
16364       if (et.type == NT_poly)
16365         NEON_ENCODE (POLY, inst);
16366       else
16367         NEON_ENCODE (INTEGER, inst);
16368
16369       /* For polynomial encoding the U bit must be zero, and the size must
16370          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
16371          obviously, as 0b10).  */
16372       if (et.size == 64)
16373         {
16374           /* Check we're on the correct architecture.  */
16375           if (!mark_feature_used (&fpu_crypto_ext_armv8))
16376             inst.error =
16377               _("Instruction form not available on this architecture.");
16378
16379           et.size = 32;
16380         }
16381
16382       neon_mixed_length (et, et.size);
16383     }
16384 }
16385
16386 static void
16387 do_neon_ext (void)
16388 {
16389   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
16390   struct neon_type_el et = neon_check_type (3, rs,
16391     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16392   unsigned imm = (inst.operands[3].imm * et.size) / 8;
16393
16394   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
16395               _("shift out of range"));
16396   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16397   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16398   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16399   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16400   inst.instruction |= LOW4 (inst.operands[2].reg);
16401   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16402   inst.instruction |= neon_quad (rs) << 6;
16403   inst.instruction |= imm << 8;
16404
16405   neon_dp_fixup (&inst);
16406 }
16407
16408 static void
16409 do_neon_rev (void)
16410 {
16411   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16412   struct neon_type_el et = neon_check_type (2, rs,
16413     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16414   unsigned op = (inst.instruction >> 7) & 3;
16415   /* N (width of reversed regions) is encoded as part of the bitmask. We
16416      extract it here to check the elements to be reversed are smaller.
16417      Otherwise we'd get a reserved instruction.  */
16418   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
16419   gas_assert (elsize != 0);
16420   constraint (et.size >= elsize,
16421               _("elements must be smaller than reversal region"));
16422   neon_two_same (neon_quad (rs), 1, et.size);
16423 }
16424
16425 static void
16426 do_neon_dup (void)
16427 {
16428   if (inst.operands[1].isscalar)
16429     {
16430       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
16431       struct neon_type_el et = neon_check_type (2, rs,
16432         N_EQK, N_8 | N_16 | N_32 | N_KEY);
16433       unsigned sizebits = et.size >> 3;
16434       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
16435       int logsize = neon_logbits (et.size);
16436       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
16437
16438       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
16439         return;
16440
16441       NEON_ENCODE (SCALAR, inst);
16442       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16443       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16444       inst.instruction |= LOW4 (dm);
16445       inst.instruction |= HI1 (dm) << 5;
16446       inst.instruction |= neon_quad (rs) << 6;
16447       inst.instruction |= x << 17;
16448       inst.instruction |= sizebits << 16;
16449
16450       neon_dp_fixup (&inst);
16451     }
16452   else
16453     {
16454       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
16455       struct neon_type_el et = neon_check_type (2, rs,
16456         N_8 | N_16 | N_32 | N_KEY, N_EQK);
16457       /* Duplicate ARM register to lanes of vector.  */
16458       NEON_ENCODE (ARMREG, inst);
16459       switch (et.size)
16460         {
16461         case 8:  inst.instruction |= 0x400000; break;
16462         case 16: inst.instruction |= 0x000020; break;
16463         case 32: inst.instruction |= 0x000000; break;
16464         default: break;
16465         }
16466       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16467       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
16468       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
16469       inst.instruction |= neon_quad (rs) << 21;
16470       /* The encoding for this instruction is identical for the ARM and Thumb
16471          variants, except for the condition field.  */
16472       do_vfp_cond_or_thumb ();
16473     }
16474 }
16475
16476 /* VMOV has particularly many variations. It can be one of:
16477      0. VMOV<c><q> <Qd>, <Qm>
16478      1. VMOV<c><q> <Dd>, <Dm>
16479    (Register operations, which are VORR with Rm = Rn.)
16480      2. VMOV<c><q>.<dt> <Qd>, #<imm>
16481      3. VMOV<c><q>.<dt> <Dd>, #<imm>
16482    (Immediate loads.)
16483      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
16484    (ARM register to scalar.)
16485      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
16486    (Two ARM registers to vector.)
16487      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
16488    (Scalar to ARM register.)
16489      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
16490    (Vector to two ARM registers.)
16491      8. VMOV.F32 <Sd>, <Sm>
16492      9. VMOV.F64 <Dd>, <Dm>
16493    (VFP register moves.)
16494     10. VMOV.F32 <Sd>, #imm
16495     11. VMOV.F64 <Dd>, #imm
16496    (VFP float immediate load.)
16497     12. VMOV <Rd>, <Sm>
16498    (VFP single to ARM reg.)
16499     13. VMOV <Sd>, <Rm>
16500    (ARM reg to VFP single.)
16501     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
16502    (Two ARM regs to two VFP singles.)
16503     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
16504    (Two VFP singles to two ARM regs.)
16505
16506    These cases can be disambiguated using neon_select_shape, except cases 1/9
16507    and 3/11 which depend on the operand type too.
16508
16509    All the encoded bits are hardcoded by this function.
16510
16511    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
16512    Cases 5, 7 may be used with VFPv2 and above.
16513
16514    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
16515    can specify a type where it doesn't make sense to, and is ignored).  */
16516
16517 static void
16518 do_neon_mov (void)
16519 {
16520   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
16521                                           NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR,
16522                                           NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
16523                                           NS_HR, NS_RH, NS_HI, NS_NULL);
16524   struct neon_type_el et;
16525   const char *ldconst = 0;
16526
16527   switch (rs)
16528     {
16529     case NS_DD:  /* case 1/9.  */
16530       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16531       /* It is not an error here if no type is given.  */
16532       inst.error = NULL;
16533       if (et.type == NT_float && et.size == 64)
16534         {
16535           do_vfp_nsyn_opcode ("fcpyd");
16536           break;
16537         }
16538       /* fall through.  */
16539
16540     case NS_QQ:  /* case 0/1.  */
16541       {
16542         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16543           return;
16544         /* The architecture manual I have doesn't explicitly state which
16545            value the U bit should have for register->register moves, but
16546            the equivalent VORR instruction has U = 0, so do that.  */
16547         inst.instruction = 0x0200110;
16548         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16549         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16550         inst.instruction |= LOW4 (inst.operands[1].reg);
16551         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16552         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16553         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16554         inst.instruction |= neon_quad (rs) << 6;
16555
16556         neon_dp_fixup (&inst);
16557       }
16558       break;
16559
16560     case NS_DI:  /* case 3/11.  */
16561       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16562       inst.error = NULL;
16563       if (et.type == NT_float && et.size == 64)
16564         {
16565           /* case 11 (fconstd).  */
16566           ldconst = "fconstd";
16567           goto encode_fconstd;
16568         }
16569       /* fall through.  */
16570
16571     case NS_QI:  /* case 2/3.  */
16572       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16573         return;
16574       inst.instruction = 0x0800010;
16575       neon_move_immediate ();
16576       neon_dp_fixup (&inst);
16577       break;
16578
16579     case NS_SR:  /* case 4.  */
16580       {
16581         unsigned bcdebits = 0;
16582         int logsize;
16583         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
16584         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
16585
16586         /* .<size> is optional here, defaulting to .32. */
16587         if (inst.vectype.elems == 0
16588             && inst.operands[0].vectype.type == NT_invtype
16589             && inst.operands[1].vectype.type == NT_invtype)
16590           {
16591             inst.vectype.el[0].type = NT_untyped;
16592             inst.vectype.el[0].size = 32;
16593             inst.vectype.elems = 1;
16594           }
16595
16596         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
16597         logsize = neon_logbits (et.size);
16598
16599         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16600                     _(BAD_FPU));
16601         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16602                     && et.size != 32, _(BAD_FPU));
16603         constraint (et.type == NT_invtype, _("bad type for scalar"));
16604         constraint (x >= 64 / et.size, _("scalar index out of range"));
16605
16606         switch (et.size)
16607           {
16608           case 8:  bcdebits = 0x8; break;
16609           case 16: bcdebits = 0x1; break;
16610           case 32: bcdebits = 0x0; break;
16611           default: ;
16612           }
16613
16614         bcdebits |= x << logsize;
16615
16616         inst.instruction = 0xe000b10;
16617         do_vfp_cond_or_thumb ();
16618         inst.instruction |= LOW4 (dn) << 16;
16619         inst.instruction |= HI1 (dn) << 7;
16620         inst.instruction |= inst.operands[1].reg << 12;
16621         inst.instruction |= (bcdebits & 3) << 5;
16622         inst.instruction |= (bcdebits >> 2) << 21;
16623       }
16624       break;
16625
16626     case NS_DRR:  /* case 5 (fmdrr).  */
16627       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16628                   _(BAD_FPU));
16629
16630       inst.instruction = 0xc400b10;
16631       do_vfp_cond_or_thumb ();
16632       inst.instruction |= LOW4 (inst.operands[0].reg);
16633       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
16634       inst.instruction |= inst.operands[1].reg << 12;
16635       inst.instruction |= inst.operands[2].reg << 16;
16636       break;
16637
16638     case NS_RS:  /* case 6.  */
16639       {
16640         unsigned logsize;
16641         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
16642         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
16643         unsigned abcdebits = 0;
16644
16645         /* .<dt> is optional here, defaulting to .32. */
16646         if (inst.vectype.elems == 0
16647             && inst.operands[0].vectype.type == NT_invtype
16648             && inst.operands[1].vectype.type == NT_invtype)
16649           {
16650             inst.vectype.el[0].type = NT_untyped;
16651             inst.vectype.el[0].size = 32;
16652             inst.vectype.elems = 1;
16653           }
16654
16655         et = neon_check_type (2, NS_NULL,
16656                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
16657         logsize = neon_logbits (et.size);
16658
16659         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16660                     _(BAD_FPU));
16661         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16662                     && et.size != 32, _(BAD_FPU));
16663         constraint (et.type == NT_invtype, _("bad type for scalar"));
16664         constraint (x >= 64 / et.size, _("scalar index out of range"));
16665
16666         switch (et.size)
16667           {
16668           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
16669           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
16670           case 32: abcdebits = 0x00; break;
16671           default: ;
16672           }
16673
16674         abcdebits |= x << logsize;
16675         inst.instruction = 0xe100b10;
16676         do_vfp_cond_or_thumb ();
16677         inst.instruction |= LOW4 (dn) << 16;
16678         inst.instruction |= HI1 (dn) << 7;
16679         inst.instruction |= inst.operands[0].reg << 12;
16680         inst.instruction |= (abcdebits & 3) << 5;
16681         inst.instruction |= (abcdebits >> 2) << 21;
16682       }
16683       break;
16684
16685     case NS_RRD:  /* case 7 (fmrrd).  */
16686       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16687                   _(BAD_FPU));
16688
16689       inst.instruction = 0xc500b10;
16690       do_vfp_cond_or_thumb ();
16691       inst.instruction |= inst.operands[0].reg << 12;
16692       inst.instruction |= inst.operands[1].reg << 16;
16693       inst.instruction |= LOW4 (inst.operands[2].reg);
16694       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16695       break;
16696
16697     case NS_FF:  /* case 8 (fcpys).  */
16698       do_vfp_nsyn_opcode ("fcpys");
16699       break;
16700
16701     case NS_HI:
16702     case NS_FI:  /* case 10 (fconsts).  */
16703       ldconst = "fconsts";
16704     encode_fconstd:
16705       if (!inst.operands[1].immisfloat)
16706         {
16707           unsigned new_imm;
16708           /* Immediate has to fit in 8 bits so float is enough.  */
16709           float imm = (float) inst.operands[1].imm;
16710           memcpy (&new_imm, &imm, sizeof (float));
16711           /* But the assembly may have been written to provide an integer
16712              bit pattern that equates to a float, so check that the
16713              conversion has worked.  */
16714           if (is_quarter_float (new_imm))
16715             {
16716               if (is_quarter_float (inst.operands[1].imm))
16717                 as_warn (_("immediate constant is valid both as a bit-pattern and a floating point value (using the fp value)"));
16718
16719               inst.operands[1].imm = new_imm;
16720               inst.operands[1].immisfloat = 1;
16721             }
16722         }
16723
16724       if (is_quarter_float (inst.operands[1].imm))
16725         {
16726           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
16727           do_vfp_nsyn_opcode (ldconst);
16728
16729           /* ARMv8.2 fp16 vmov.f16 instruction.  */
16730           if (rs == NS_HI)
16731             do_scalar_fp16_v82_encode ();
16732         }
16733       else
16734         first_error (_("immediate out of range"));
16735       break;
16736
16737     case NS_RH:
16738     case NS_RF:  /* case 12 (fmrs).  */
16739       do_vfp_nsyn_opcode ("fmrs");
16740       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16741       if (rs == NS_RH)
16742         do_scalar_fp16_v82_encode ();
16743       break;
16744
16745     case NS_HR:
16746     case NS_FR:  /* case 13 (fmsr).  */
16747       do_vfp_nsyn_opcode ("fmsr");
16748       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16749       if (rs == NS_HR)
16750         do_scalar_fp16_v82_encode ();
16751       break;
16752
16753     /* The encoders for the fmrrs and fmsrr instructions expect three operands
16754        (one of which is a list), but we have parsed four.  Do some fiddling to
16755        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
16756        expect.  */
16757     case NS_RRFF:  /* case 14 (fmrrs).  */
16758       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
16759                   _("VFP registers must be adjacent"));
16760       inst.operands[2].imm = 2;
16761       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16762       do_vfp_nsyn_opcode ("fmrrs");
16763       break;
16764
16765     case NS_FFRR:  /* case 15 (fmsrr).  */
16766       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
16767                   _("VFP registers must be adjacent"));
16768       inst.operands[1] = inst.operands[2];
16769       inst.operands[2] = inst.operands[3];
16770       inst.operands[0].imm = 2;
16771       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16772       do_vfp_nsyn_opcode ("fmsrr");
16773       break;
16774
16775     case NS_NULL:
16776       /* neon_select_shape has determined that the instruction
16777          shape is wrong and has already set the error message.  */
16778       break;
16779
16780     default:
16781       abort ();
16782     }
16783 }
16784
16785 static void
16786 do_neon_rshift_round_imm (void)
16787 {
16788   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16789   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16790   int imm = inst.operands[2].imm;
16791
16792   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
16793   if (imm == 0)
16794     {
16795       inst.operands[2].present = 0;
16796       do_neon_mov ();
16797       return;
16798     }
16799
16800   constraint (imm < 1 || (unsigned)imm > et.size,
16801               _("immediate out of range for shift"));
16802   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
16803                   et.size - imm);
16804 }
16805
16806 static void
16807 do_neon_movhf (void)
16808 {
16809   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
16810   constraint (rs != NS_HH, _("invalid suffix"));
16811
16812   if (inst.cond != COND_ALWAYS)
16813     {
16814       if (thumb_mode)
16815         {
16816           as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
16817                      " the behaviour is UNPREDICTABLE"));
16818         }
16819       else
16820         {
16821           inst.error = BAD_COND;
16822           return;
16823         }
16824     }
16825
16826   do_vfp_sp_monadic ();
16827
16828   inst.is_neon = 1;
16829   inst.instruction |= 0xf0000000;
16830 }
16831
16832 static void
16833 do_neon_movl (void)
16834 {
16835   struct neon_type_el et = neon_check_type (2, NS_QD,
16836     N_EQK | N_DBL, N_SU_32 | N_KEY);
16837   unsigned sizebits = et.size >> 3;
16838   inst.instruction |= sizebits << 19;
16839   neon_two_same (0, et.type == NT_unsigned, -1);
16840 }
16841
16842 static void
16843 do_neon_trn (void)
16844 {
16845   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16846   struct neon_type_el et = neon_check_type (2, rs,
16847     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16848   NEON_ENCODE (INTEGER, inst);
16849   neon_two_same (neon_quad (rs), 1, et.size);
16850 }
16851
16852 static void
16853 do_neon_zip_uzp (void)
16854 {
16855   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16856   struct neon_type_el et = neon_check_type (2, rs,
16857     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16858   if (rs == NS_DD && et.size == 32)
16859     {
16860       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16861       inst.instruction = N_MNEM_vtrn;
16862       do_neon_trn ();
16863       return;
16864     }
16865   neon_two_same (neon_quad (rs), 1, et.size);
16866 }
16867
16868 static void
16869 do_neon_sat_abs_neg (void)
16870 {
16871   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16872   struct neon_type_el et = neon_check_type (2, rs,
16873     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16874   neon_two_same (neon_quad (rs), 1, et.size);
16875 }
16876
16877 static void
16878 do_neon_pair_long (void)
16879 {
16880   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16881   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16882   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16883   inst.instruction |= (et.type == NT_unsigned) << 7;
16884   neon_two_same (neon_quad (rs), 1, et.size);
16885 }
16886
16887 static void
16888 do_neon_recip_est (void)
16889 {
16890   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16891   struct neon_type_el et = neon_check_type (2, rs,
16892     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
16893   inst.instruction |= (et.type == NT_float) << 8;
16894   neon_two_same (neon_quad (rs), 1, et.size);
16895 }
16896
16897 static void
16898 do_neon_cls (void)
16899 {
16900   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16901   struct neon_type_el et = neon_check_type (2, rs,
16902     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16903   neon_two_same (neon_quad (rs), 1, et.size);
16904 }
16905
16906 static void
16907 do_neon_clz (void)
16908 {
16909   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16910   struct neon_type_el et = neon_check_type (2, rs,
16911     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16912   neon_two_same (neon_quad (rs), 1, et.size);
16913 }
16914
16915 static void
16916 do_neon_cnt (void)
16917 {
16918   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16919   struct neon_type_el et = neon_check_type (2, rs,
16920     N_EQK | N_INT, N_8 | N_KEY);
16921   neon_two_same (neon_quad (rs), 1, et.size);
16922 }
16923
16924 static void
16925 do_neon_swp (void)
16926 {
16927   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16928   neon_two_same (neon_quad (rs), 1, -1);
16929 }
16930
16931 static void
16932 do_neon_tbl_tbx (void)
16933 {
16934   unsigned listlenbits;
16935   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16936
16937   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16938     {
16939       first_error (_("bad list length for table lookup"));
16940       return;
16941     }
16942
16943   listlenbits = inst.operands[1].imm - 1;
16944   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16945   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16946   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16947   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16948   inst.instruction |= LOW4 (inst.operands[2].reg);
16949   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16950   inst.instruction |= listlenbits << 8;
16951
16952   neon_dp_fixup (&inst);
16953 }
16954
16955 static void
16956 do_neon_ldm_stm (void)
16957 {
16958   /* P, U and L bits are part of bitmask.  */
16959   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16960   unsigned offsetbits = inst.operands[1].imm * 2;
16961
16962   if (inst.operands[1].issingle)
16963     {
16964       do_vfp_nsyn_ldm_stm (is_dbmode);
16965       return;
16966     }
16967
16968   constraint (is_dbmode && !inst.operands[0].writeback,
16969               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16970
16971   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16972               _("register list must contain at least 1 and at most 16 "
16973                 "registers"));
16974
16975   inst.instruction |= inst.operands[0].reg << 16;
16976   inst.instruction |= inst.operands[0].writeback << 21;
16977   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16978   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16979
16980   inst.instruction |= offsetbits;
16981
16982   do_vfp_cond_or_thumb ();
16983 }
16984
16985 static void
16986 do_neon_ldr_str (void)
16987 {
16988   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16989
16990   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16991      And is UNPREDICTABLE in thumb mode.  */
16992   if (!is_ldr
16993       && inst.operands[1].reg == REG_PC
16994       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16995     {
16996       if (thumb_mode)
16997         inst.error = _("Use of PC here is UNPREDICTABLE");
16998       else if (warn_on_deprecated)
16999         as_tsktsk (_("Use of PC here is deprecated"));
17000     }
17001
17002   if (inst.operands[0].issingle)
17003     {
17004       if (is_ldr)
17005         do_vfp_nsyn_opcode ("flds");
17006       else
17007         do_vfp_nsyn_opcode ("fsts");
17008
17009       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
17010       if (inst.vectype.el[0].size == 16)
17011         do_scalar_fp16_v82_encode ();
17012     }
17013   else
17014     {
17015       if (is_ldr)
17016         do_vfp_nsyn_opcode ("fldd");
17017       else
17018         do_vfp_nsyn_opcode ("fstd");
17019     }
17020 }
17021
17022 /* "interleave" version also handles non-interleaving register VLD1/VST1
17023    instructions.  */
17024
17025 static void
17026 do_neon_ld_st_interleave (void)
17027 {
17028   struct neon_type_el et = neon_check_type (1, NS_NULL,
17029                                             N_8 | N_16 | N_32 | N_64);
17030   unsigned alignbits = 0;
17031   unsigned idx;
17032   /* The bits in this table go:
17033      0: register stride of one (0) or two (1)
17034      1,2: register list length, minus one (1, 2, 3, 4).
17035      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
17036      We use -1 for invalid entries.  */
17037   const int typetable[] =
17038     {
17039       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
17040        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
17041        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
17042        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
17043     };
17044   int typebits;
17045
17046   if (et.type == NT_invtype)
17047     return;
17048
17049   if (inst.operands[1].immisalign)
17050     switch (inst.operands[1].imm >> 8)
17051       {
17052       case 64: alignbits = 1; break;
17053       case 128:
17054         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
17055             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17056           goto bad_alignment;
17057         alignbits = 2;
17058         break;
17059       case 256:
17060         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17061           goto bad_alignment;
17062         alignbits = 3;
17063         break;
17064       default:
17065       bad_alignment:
17066         first_error (_("bad alignment"));
17067         return;
17068       }
17069
17070   inst.instruction |= alignbits << 4;
17071   inst.instruction |= neon_logbits (et.size) << 6;
17072
17073   /* Bits [4:6] of the immediate in a list specifier encode register stride
17074      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
17075      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
17076      up the right value for "type" in a table based on this value and the given
17077      list style, then stick it back.  */
17078   idx = ((inst.operands[0].imm >> 4) & 7)
17079         | (((inst.instruction >> 8) & 3) << 3);
17080
17081   typebits = typetable[idx];
17082
17083   constraint (typebits == -1, _("bad list type for instruction"));
17084   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
17085               _("bad element type for instruction"));
17086
17087   inst.instruction &= ~0xf00;
17088   inst.instruction |= typebits << 8;
17089 }
17090
17091 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
17092    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
17093    otherwise. The variable arguments are a list of pairs of legal (size, align)
17094    values, terminated with -1.  */
17095
17096 static int
17097 neon_alignment_bit (int size, int align, int *do_alignment, ...)
17098 {
17099   va_list ap;
17100   int result = FAIL, thissize, thisalign;
17101
17102   if (!inst.operands[1].immisalign)
17103     {
17104       *do_alignment = 0;
17105       return SUCCESS;
17106     }
17107
17108   va_start (ap, do_alignment);
17109
17110   do
17111     {
17112       thissize = va_arg (ap, int);
17113       if (thissize == -1)
17114         break;
17115       thisalign = va_arg (ap, int);
17116
17117       if (size == thissize && align == thisalign)
17118         result = SUCCESS;
17119     }
17120   while (result != SUCCESS);
17121
17122   va_end (ap);
17123
17124   if (result == SUCCESS)
17125     *do_alignment = 1;
17126   else
17127     first_error (_("unsupported alignment for instruction"));
17128
17129   return result;
17130 }
17131
17132 static void
17133 do_neon_ld_st_lane (void)
17134 {
17135   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17136   int align_good, do_alignment = 0;
17137   int logsize = neon_logbits (et.size);
17138   int align = inst.operands[1].imm >> 8;
17139   int n = (inst.instruction >> 8) & 3;
17140   int max_el = 64 / et.size;
17141
17142   if (et.type == NT_invtype)
17143     return;
17144
17145   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
17146               _("bad list length"));
17147   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
17148               _("scalar index out of range"));
17149   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
17150               && et.size == 8,
17151               _("stride of 2 unavailable when element size is 8"));
17152
17153   switch (n)
17154     {
17155     case 0:  /* VLD1 / VST1.  */
17156       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
17157                                        32, 32, -1);
17158       if (align_good == FAIL)
17159         return;
17160       if (do_alignment)
17161         {
17162           unsigned alignbits = 0;
17163           switch (et.size)
17164             {
17165             case 16: alignbits = 0x1; break;
17166             case 32: alignbits = 0x3; break;
17167             default: ;
17168             }
17169           inst.instruction |= alignbits << 4;
17170         }
17171       break;
17172
17173     case 1:  /* VLD2 / VST2.  */
17174       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
17175                       16, 32, 32, 64, -1);
17176       if (align_good == FAIL)
17177         return;
17178       if (do_alignment)
17179         inst.instruction |= 1 << 4;
17180       break;
17181
17182     case 2:  /* VLD3 / VST3.  */
17183       constraint (inst.operands[1].immisalign,
17184                   _("can't use alignment with this instruction"));
17185       break;
17186
17187     case 3:  /* VLD4 / VST4.  */
17188       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17189                                        16, 64, 32, 64, 32, 128, -1);
17190       if (align_good == FAIL)
17191         return;
17192       if (do_alignment)
17193         {
17194           unsigned alignbits = 0;
17195           switch (et.size)
17196             {
17197             case 8:  alignbits = 0x1; break;
17198             case 16: alignbits = 0x1; break;
17199             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
17200             default: ;
17201             }
17202           inst.instruction |= alignbits << 4;
17203         }
17204       break;
17205
17206     default: ;
17207     }
17208
17209   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
17210   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17211     inst.instruction |= 1 << (4 + logsize);
17212
17213   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
17214   inst.instruction |= logsize << 10;
17215 }
17216
17217 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
17218
17219 static void
17220 do_neon_ld_dup (void)
17221 {
17222   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17223   int align_good, do_alignment = 0;
17224
17225   if (et.type == NT_invtype)
17226     return;
17227
17228   switch ((inst.instruction >> 8) & 3)
17229     {
17230     case 0:  /* VLD1.  */
17231       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
17232       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17233                                        &do_alignment, 16, 16, 32, 32, -1);
17234       if (align_good == FAIL)
17235         return;
17236       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
17237         {
17238         case 1: break;
17239         case 2: inst.instruction |= 1 << 5; break;
17240         default: first_error (_("bad list length")); return;
17241         }
17242       inst.instruction |= neon_logbits (et.size) << 6;
17243       break;
17244
17245     case 1:  /* VLD2.  */
17246       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17247                                        &do_alignment, 8, 16, 16, 32, 32, 64,
17248                                        -1);
17249       if (align_good == FAIL)
17250         return;
17251       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
17252                   _("bad list length"));
17253       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17254         inst.instruction |= 1 << 5;
17255       inst.instruction |= neon_logbits (et.size) << 6;
17256       break;
17257
17258     case 2:  /* VLD3.  */
17259       constraint (inst.operands[1].immisalign,
17260                   _("can't use alignment with this instruction"));
17261       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
17262                   _("bad list length"));
17263       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17264         inst.instruction |= 1 << 5;
17265       inst.instruction |= neon_logbits (et.size) << 6;
17266       break;
17267
17268     case 3:  /* VLD4.  */
17269       {
17270         int align = inst.operands[1].imm >> 8;
17271         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17272                                          16, 64, 32, 64, 32, 128, -1);
17273         if (align_good == FAIL)
17274           return;
17275         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
17276                     _("bad list length"));
17277         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17278           inst.instruction |= 1 << 5;
17279         if (et.size == 32 && align == 128)
17280           inst.instruction |= 0x3 << 6;
17281         else
17282           inst.instruction |= neon_logbits (et.size) << 6;
17283       }
17284       break;
17285
17286     default: ;
17287     }
17288
17289   inst.instruction |= do_alignment << 4;
17290 }
17291
17292 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
17293    apart from bits [11:4].  */
17294
17295 static void
17296 do_neon_ldx_stx (void)
17297 {
17298   if (inst.operands[1].isreg)
17299     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
17300
17301   switch (NEON_LANE (inst.operands[0].imm))
17302     {
17303     case NEON_INTERLEAVE_LANES:
17304       NEON_ENCODE (INTERLV, inst);
17305       do_neon_ld_st_interleave ();
17306       break;
17307
17308     case NEON_ALL_LANES:
17309       NEON_ENCODE (DUP, inst);
17310       if (inst.instruction == N_INV)
17311         {
17312           first_error ("only loads support such operands");
17313           break;
17314         }
17315       do_neon_ld_dup ();
17316       break;
17317
17318     default:
17319       NEON_ENCODE (LANE, inst);
17320       do_neon_ld_st_lane ();
17321     }
17322
17323   /* L bit comes from bit mask.  */
17324   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17325   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17326   inst.instruction |= inst.operands[1].reg << 16;
17327
17328   if (inst.operands[1].postind)
17329     {
17330       int postreg = inst.operands[1].imm & 0xf;
17331       constraint (!inst.operands[1].immisreg,
17332                   _("post-index must be a register"));
17333       constraint (postreg == 0xd || postreg == 0xf,
17334                   _("bad register for post-index"));
17335       inst.instruction |= postreg;
17336     }
17337   else
17338     {
17339       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17340       constraint (inst.reloc.exp.X_op != O_constant
17341                   || inst.reloc.exp.X_add_number != 0,
17342                   BAD_ADDR_MODE);
17343
17344       if (inst.operands[1].writeback)
17345         {
17346           inst.instruction |= 0xd;
17347         }
17348       else
17349         inst.instruction |= 0xf;
17350     }
17351
17352   if (thumb_mode)
17353     inst.instruction |= 0xf9000000;
17354   else
17355     inst.instruction |= 0xf4000000;
17356 }
17357
17358 /* FP v8.  */
17359 static void
17360 do_vfp_nsyn_fpv8 (enum neon_shape rs)
17361 {
17362   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17363      D register operands.  */
17364   if (neon_shape_class[rs] == SC_DOUBLE)
17365     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17366                 _(BAD_FPU));
17367
17368   NEON_ENCODE (FPV8, inst);
17369
17370   if (rs == NS_FFF || rs == NS_HHH)
17371     {
17372       do_vfp_sp_dyadic ();
17373
17374       /* ARMv8.2 fp16 instruction.  */
17375       if (rs == NS_HHH)
17376         do_scalar_fp16_v82_encode ();
17377     }
17378   else
17379     do_vfp_dp_rd_rn_rm ();
17380
17381   if (rs == NS_DDD)
17382     inst.instruction |= 0x100;
17383
17384   inst.instruction |= 0xf0000000;
17385 }
17386
17387 static void
17388 do_vsel (void)
17389 {
17390   set_it_insn_type (OUTSIDE_IT_INSN);
17391
17392   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
17393     first_error (_("invalid instruction shape"));
17394 }
17395
17396 static void
17397 do_vmaxnm (void)
17398 {
17399   set_it_insn_type (OUTSIDE_IT_INSN);
17400
17401   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
17402     return;
17403
17404   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17405     return;
17406
17407   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
17408 }
17409
17410 static void
17411 do_vrint_1 (enum neon_cvt_mode mode)
17412 {
17413   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
17414   struct neon_type_el et;
17415
17416   if (rs == NS_NULL)
17417     return;
17418
17419   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17420      D register operands.  */
17421   if (neon_shape_class[rs] == SC_DOUBLE)
17422     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17423                 _(BAD_FPU));
17424
17425   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
17426                         | N_VFP);
17427   if (et.type != NT_invtype)
17428     {
17429       /* VFP encodings.  */
17430       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
17431           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
17432         set_it_insn_type (OUTSIDE_IT_INSN);
17433
17434       NEON_ENCODE (FPV8, inst);
17435       if (rs == NS_FF || rs == NS_HH)
17436         do_vfp_sp_monadic ();
17437       else
17438         do_vfp_dp_rd_rm ();
17439
17440       switch (mode)
17441         {
17442         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
17443         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
17444         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
17445         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
17446         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
17447         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
17448         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
17449         default: abort ();
17450         }
17451
17452       inst.instruction |= (rs == NS_DD) << 8;
17453       do_vfp_cond_or_thumb ();
17454
17455       /* ARMv8.2 fp16 vrint instruction.  */
17456       if (rs == NS_HH)
17457       do_scalar_fp16_v82_encode ();
17458     }
17459   else
17460     {
17461       /* Neon encodings (or something broken...).  */
17462       inst.error = NULL;
17463       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
17464
17465       if (et.type == NT_invtype)
17466         return;
17467
17468       set_it_insn_type (OUTSIDE_IT_INSN);
17469       NEON_ENCODE (FLOAT, inst);
17470
17471       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17472         return;
17473
17474       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17475       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17476       inst.instruction |= LOW4 (inst.operands[1].reg);
17477       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17478       inst.instruction |= neon_quad (rs) << 6;
17479       /* Mask off the original size bits and reencode them.  */
17480       inst.instruction = ((inst.instruction & 0xfff3ffff)
17481                           | neon_logbits (et.size) << 18);
17482
17483       switch (mode)
17484         {
17485         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
17486         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
17487         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
17488         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
17489         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
17490         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
17491         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
17492         default: abort ();
17493         }
17494
17495       if (thumb_mode)
17496         inst.instruction |= 0xfc000000;
17497       else
17498         inst.instruction |= 0xf0000000;
17499     }
17500 }
17501
17502 static void
17503 do_vrintx (void)
17504 {
17505   do_vrint_1 (neon_cvt_mode_x);
17506 }
17507
17508 static void
17509 do_vrintz (void)
17510 {
17511   do_vrint_1 (neon_cvt_mode_z);
17512 }
17513
17514 static void
17515 do_vrintr (void)
17516 {
17517   do_vrint_1 (neon_cvt_mode_r);
17518 }
17519
17520 static void
17521 do_vrinta (void)
17522 {
17523   do_vrint_1 (neon_cvt_mode_a);
17524 }
17525
17526 static void
17527 do_vrintn (void)
17528 {
17529   do_vrint_1 (neon_cvt_mode_n);
17530 }
17531
17532 static void
17533 do_vrintp (void)
17534 {
17535   do_vrint_1 (neon_cvt_mode_p);
17536 }
17537
17538 static void
17539 do_vrintm (void)
17540 {
17541   do_vrint_1 (neon_cvt_mode_m);
17542 }
17543
17544 static unsigned
17545 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
17546 {
17547   unsigned regno = NEON_SCALAR_REG (opnd);
17548   unsigned elno = NEON_SCALAR_INDEX (opnd);
17549
17550   if (elsize == 16 && elno < 2 && regno < 16)
17551     return regno | (elno << 4);
17552   else if (elsize == 32 && elno == 0)
17553     return regno;
17554
17555   first_error (_("scalar out of range"));
17556   return 0;
17557 }
17558
17559 static void
17560 do_vcmla (void)
17561 {
17562   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17563               _(BAD_FPU));
17564   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17565   unsigned rot = inst.reloc.exp.X_add_number;
17566   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
17567               _("immediate out of range"));
17568   rot /= 90;
17569   if (inst.operands[2].isscalar)
17570     {
17571       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
17572       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17573                                        N_KEY | N_F16 | N_F32).size;
17574       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
17575       inst.is_neon = 1;
17576       inst.instruction = 0xfe000800;
17577       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17578       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17579       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17580       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17581       inst.instruction |= LOW4 (m);
17582       inst.instruction |= HI1 (m) << 5;
17583       inst.instruction |= neon_quad (rs) << 6;
17584       inst.instruction |= rot << 20;
17585       inst.instruction |= (size == 32) << 23;
17586     }
17587   else
17588     {
17589       enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17590       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17591                                        N_KEY | N_F16 | N_F32).size;
17592       neon_three_same (neon_quad (rs), 0, -1);
17593       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17594       inst.instruction |= 0xfc200800;
17595       inst.instruction |= rot << 23;
17596       inst.instruction |= (size == 32) << 20;
17597     }
17598 }
17599
17600 static void
17601 do_vcadd (void)
17602 {
17603   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17604               _(BAD_FPU));
17605   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17606   unsigned rot = inst.reloc.exp.X_add_number;
17607   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17608   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17609   unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17610                                    N_KEY | N_F16 | N_F32).size;
17611   neon_three_same (neon_quad (rs), 0, -1);
17612   inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17613   inst.instruction |= 0xfc800800;
17614   inst.instruction |= (rot == 270) << 24;
17615   inst.instruction |= (size == 32) << 20;
17616 }
17617
17618 /* Dot Product instructions encoding support.  */
17619
17620 static void
17621 do_neon_dotproduct (int unsigned_p)
17622 {
17623   enum neon_shape rs;
17624   unsigned scalar_oprd2 = 0;
17625   int high8;
17626
17627   if (inst.cond != COND_ALWAYS)
17628     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
17629                "is UNPREDICTABLE"));
17630
17631   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17632               _(BAD_FPU));
17633
17634   /* Dot Product instructions are in three-same D/Q register format or the third
17635      operand can be a scalar index register.  */
17636   if (inst.operands[2].isscalar)
17637     {
17638       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
17639       high8 = 0xfe000000;
17640       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17641     }
17642   else
17643     {
17644       high8 = 0xfc000000;
17645       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17646     }
17647
17648   if (unsigned_p)
17649     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
17650   else
17651     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
17652
17653   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
17654      Product instruction, so we pass 0 as the "ubit" parameter.  And the
17655      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
17656   neon_three_same (neon_quad (rs), 0, 32);
17657
17658   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
17659      different NEON three-same encoding.  */
17660   inst.instruction &= 0x00ffffff;
17661   inst.instruction |= high8;
17662   /* Encode 'U' bit which indicates signedness.  */
17663   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
17664   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
17665      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
17666      the instruction encoding.  */
17667   if (inst.operands[2].isscalar)
17668     {
17669       inst.instruction &= 0xffffffd0;
17670       inst.instruction |= LOW4 (scalar_oprd2);
17671       inst.instruction |= HI1 (scalar_oprd2) << 5;
17672     }
17673 }
17674
17675 /* Dot Product instructions for signed integer.  */
17676
17677 static void
17678 do_neon_dotproduct_s (void)
17679 {
17680   return do_neon_dotproduct (0);
17681 }
17682
17683 /* Dot Product instructions for unsigned integer.  */
17684
17685 static void
17686 do_neon_dotproduct_u (void)
17687 {
17688   return do_neon_dotproduct (1);
17689 }
17690
17691 /* Crypto v1 instructions.  */
17692 static void
17693 do_crypto_2op_1 (unsigned elttype, int op)
17694 {
17695   set_it_insn_type (OUTSIDE_IT_INSN);
17696
17697   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
17698       == NT_invtype)
17699     return;
17700
17701   inst.error = NULL;
17702
17703   NEON_ENCODE (INTEGER, inst);
17704   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17705   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17706   inst.instruction |= LOW4 (inst.operands[1].reg);
17707   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17708   if (op != -1)
17709     inst.instruction |= op << 6;
17710
17711   if (thumb_mode)
17712     inst.instruction |= 0xfc000000;
17713   else
17714     inst.instruction |= 0xf0000000;
17715 }
17716
17717 static void
17718 do_crypto_3op_1 (int u, int op)
17719 {
17720   set_it_insn_type (OUTSIDE_IT_INSN);
17721
17722   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
17723                        N_32 | N_UNT | N_KEY).type == NT_invtype)
17724     return;
17725
17726   inst.error = NULL;
17727
17728   NEON_ENCODE (INTEGER, inst);
17729   neon_three_same (1, u, 8 << op);
17730 }
17731
17732 static void
17733 do_aese (void)
17734 {
17735   do_crypto_2op_1 (N_8, 0);
17736 }
17737
17738 static void
17739 do_aesd (void)
17740 {
17741   do_crypto_2op_1 (N_8, 1);
17742 }
17743
17744 static void
17745 do_aesmc (void)
17746 {
17747   do_crypto_2op_1 (N_8, 2);
17748 }
17749
17750 static void
17751 do_aesimc (void)
17752 {
17753   do_crypto_2op_1 (N_8, 3);
17754 }
17755
17756 static void
17757 do_sha1c (void)
17758 {
17759   do_crypto_3op_1 (0, 0);
17760 }
17761
17762 static void
17763 do_sha1p (void)
17764 {
17765   do_crypto_3op_1 (0, 1);
17766 }
17767
17768 static void
17769 do_sha1m (void)
17770 {
17771   do_crypto_3op_1 (0, 2);
17772 }
17773
17774 static void
17775 do_sha1su0 (void)
17776 {
17777   do_crypto_3op_1 (0, 3);
17778 }
17779
17780 static void
17781 do_sha256h (void)
17782 {
17783   do_crypto_3op_1 (1, 0);
17784 }
17785
17786 static void
17787 do_sha256h2 (void)
17788 {
17789   do_crypto_3op_1 (1, 1);
17790 }
17791
17792 static void
17793 do_sha256su1 (void)
17794 {
17795   do_crypto_3op_1 (1, 2);
17796 }
17797
17798 static void
17799 do_sha1h (void)
17800 {
17801   do_crypto_2op_1 (N_32, -1);
17802 }
17803
17804 static void
17805 do_sha1su1 (void)
17806 {
17807   do_crypto_2op_1 (N_32, 0);
17808 }
17809
17810 static void
17811 do_sha256su0 (void)
17812 {
17813   do_crypto_2op_1 (N_32, 1);
17814 }
17815
17816 static void
17817 do_crc32_1 (unsigned int poly, unsigned int sz)
17818 {
17819   unsigned int Rd = inst.operands[0].reg;
17820   unsigned int Rn = inst.operands[1].reg;
17821   unsigned int Rm = inst.operands[2].reg;
17822
17823   set_it_insn_type (OUTSIDE_IT_INSN);
17824   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
17825   inst.instruction |= LOW4 (Rn) << 16;
17826   inst.instruction |= LOW4 (Rm);
17827   inst.instruction |= sz << (thumb_mode ? 4 : 21);
17828   inst.instruction |= poly << (thumb_mode ? 20 : 9);
17829
17830   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
17831     as_warn (UNPRED_REG ("r15"));
17832 }
17833
17834 static void
17835 do_crc32b (void)
17836 {
17837   do_crc32_1 (0, 0);
17838 }
17839
17840 static void
17841 do_crc32h (void)
17842 {
17843   do_crc32_1 (0, 1);
17844 }
17845
17846 static void
17847 do_crc32w (void)
17848 {
17849   do_crc32_1 (0, 2);
17850 }
17851
17852 static void
17853 do_crc32cb (void)
17854 {
17855   do_crc32_1 (1, 0);
17856 }
17857
17858 static void
17859 do_crc32ch (void)
17860 {
17861   do_crc32_1 (1, 1);
17862 }
17863
17864 static void
17865 do_crc32cw (void)
17866 {
17867   do_crc32_1 (1, 2);
17868 }
17869
17870 static void
17871 do_vjcvt (void)
17872 {
17873   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17874               _(BAD_FPU));
17875   neon_check_type (2, NS_FD, N_S32, N_F64);
17876   do_vfp_sp_dp_cvt ();
17877   do_vfp_cond_or_thumb ();
17878 }
17879
17880 \f
17881 /* Overall per-instruction processing.  */
17882
17883 /* We need to be able to fix up arbitrary expressions in some statements.
17884    This is so that we can handle symbols that are an arbitrary distance from
17885    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
17886    which returns part of an address in a form which will be valid for
17887    a data instruction.  We do this by pushing the expression into a symbol
17888    in the expr_section, and creating a fix for that.  */
17889
17890 static void
17891 fix_new_arm (fragS *       frag,
17892              int           where,
17893              short int     size,
17894              expressionS * exp,
17895              int           pc_rel,
17896              int           reloc)
17897 {
17898   fixS *           new_fix;
17899
17900   switch (exp->X_op)
17901     {
17902     case O_constant:
17903       if (pc_rel)
17904         {
17905           /* Create an absolute valued symbol, so we have something to
17906              refer to in the object file.  Unfortunately for us, gas's
17907              generic expression parsing will already have folded out
17908              any use of .set foo/.type foo %function that may have
17909              been used to set type information of the target location,
17910              that's being specified symbolically.  We have to presume
17911              the user knows what they are doing.  */
17912           char name[16 + 8];
17913           symbolS *symbol;
17914
17915           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
17916
17917           symbol = symbol_find_or_make (name);
17918           S_SET_SEGMENT (symbol, absolute_section);
17919           symbol_set_frag (symbol, &zero_address_frag);
17920           S_SET_VALUE (symbol, exp->X_add_number);
17921           exp->X_op = O_symbol;
17922           exp->X_add_symbol = symbol;
17923           exp->X_add_number = 0;
17924         }
17925       /* FALLTHROUGH */
17926     case O_symbol:
17927     case O_add:
17928     case O_subtract:
17929       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
17930                              (enum bfd_reloc_code_real) reloc);
17931       break;
17932
17933     default:
17934       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
17935                                   pc_rel, (enum bfd_reloc_code_real) reloc);
17936       break;
17937     }
17938
17939   /* Mark whether the fix is to a THUMB instruction, or an ARM
17940      instruction.  */
17941   new_fix->tc_fix_data = thumb_mode;
17942 }
17943
17944 /* Create a frg for an instruction requiring relaxation.  */
17945 static void
17946 output_relax_insn (void)
17947 {
17948   char * to;
17949   symbolS *sym;
17950   int offset;
17951
17952   /* The size of the instruction is unknown, so tie the debug info to the
17953      start of the instruction.  */
17954   dwarf2_emit_insn (0);
17955
17956   switch (inst.reloc.exp.X_op)
17957     {
17958     case O_symbol:
17959       sym = inst.reloc.exp.X_add_symbol;
17960       offset = inst.reloc.exp.X_add_number;
17961       break;
17962     case O_constant:
17963       sym = NULL;
17964       offset = inst.reloc.exp.X_add_number;
17965       break;
17966     default:
17967       sym = make_expr_symbol (&inst.reloc.exp);
17968       offset = 0;
17969       break;
17970   }
17971   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
17972                  inst.relax, sym, offset, NULL/*offset, opcode*/);
17973   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
17974 }
17975
17976 /* Write a 32-bit thumb instruction to buf.  */
17977 static void
17978 put_thumb32_insn (char * buf, unsigned long insn)
17979 {
17980   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
17981   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
17982 }
17983
17984 static void
17985 output_inst (const char * str)
17986 {
17987   char * to = NULL;
17988
17989   if (inst.error)
17990     {
17991       as_bad ("%s -- `%s'", inst.error, str);
17992       return;
17993     }
17994   if (inst.relax)
17995     {
17996       output_relax_insn ();
17997       return;
17998     }
17999   if (inst.size == 0)
18000     return;
18001
18002   to = frag_more (inst.size);
18003   /* PR 9814: Record the thumb mode into the current frag so that we know
18004      what type of NOP padding to use, if necessary.  We override any previous
18005      setting so that if the mode has changed then the NOPS that we use will
18006      match the encoding of the last instruction in the frag.  */
18007   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
18008
18009   if (thumb_mode && (inst.size > THUMB_SIZE))
18010     {
18011       gas_assert (inst.size == (2 * THUMB_SIZE));
18012       put_thumb32_insn (to, inst.instruction);
18013     }
18014   else if (inst.size > INSN_SIZE)
18015     {
18016       gas_assert (inst.size == (2 * INSN_SIZE));
18017       md_number_to_chars (to, inst.instruction, INSN_SIZE);
18018       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
18019     }
18020   else
18021     md_number_to_chars (to, inst.instruction, inst.size);
18022
18023   if (inst.reloc.type != BFD_RELOC_UNUSED)
18024     fix_new_arm (frag_now, to - frag_now->fr_literal,
18025                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
18026                  inst.reloc.type);
18027
18028   dwarf2_emit_insn (inst.size);
18029 }
18030
18031 static char *
18032 output_it_inst (int cond, int mask, char * to)
18033 {
18034   unsigned long instruction = 0xbf00;
18035
18036   mask &= 0xf;
18037   instruction |= mask;
18038   instruction |= cond << 4;
18039
18040   if (to == NULL)
18041     {
18042       to = frag_more (2);
18043 #ifdef OBJ_ELF
18044       dwarf2_emit_insn (2);
18045 #endif
18046     }
18047
18048   md_number_to_chars (to, instruction, 2);
18049
18050   return to;
18051 }
18052
18053 /* Tag values used in struct asm_opcode's tag field.  */
18054 enum opcode_tag
18055 {
18056   OT_unconditional,     /* Instruction cannot be conditionalized.
18057                            The ARM condition field is still 0xE.  */
18058   OT_unconditionalF,    /* Instruction cannot be conditionalized
18059                            and carries 0xF in its ARM condition field.  */
18060   OT_csuffix,           /* Instruction takes a conditional suffix.  */
18061   OT_csuffixF,          /* Some forms of the instruction take a conditional
18062                            suffix, others place 0xF where the condition field
18063                            would be.  */
18064   OT_cinfix3,           /* Instruction takes a conditional infix,
18065                            beginning at character index 3.  (In
18066                            unified mode, it becomes a suffix.)  */
18067   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
18068                             tsts, cmps, cmns, and teqs. */
18069   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
18070                            character index 3, even in unified mode.  Used for
18071                            legacy instructions where suffix and infix forms
18072                            may be ambiguous.  */
18073   OT_csuf_or_in3,       /* Instruction takes either a conditional
18074                            suffix or an infix at character index 3.  */
18075   OT_odd_infix_unc,     /* This is the unconditional variant of an
18076                            instruction that takes a conditional infix
18077                            at an unusual position.  In unified mode,
18078                            this variant will accept a suffix.  */
18079   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
18080                            are the conditional variants of instructions that
18081                            take conditional infixes in unusual positions.
18082                            The infix appears at character index
18083                            (tag - OT_odd_infix_0).  These are not accepted
18084                            in unified mode.  */
18085 };
18086
18087 /* Subroutine of md_assemble, responsible for looking up the primary
18088    opcode from the mnemonic the user wrote.  STR points to the
18089    beginning of the mnemonic.
18090
18091    This is not simply a hash table lookup, because of conditional
18092    variants.  Most instructions have conditional variants, which are
18093    expressed with a _conditional affix_ to the mnemonic.  If we were
18094    to encode each conditional variant as a literal string in the opcode
18095    table, it would have approximately 20,000 entries.
18096
18097    Most mnemonics take this affix as a suffix, and in unified syntax,
18098    'most' is upgraded to 'all'.  However, in the divided syntax, some
18099    instructions take the affix as an infix, notably the s-variants of
18100    the arithmetic instructions.  Of those instructions, all but six
18101    have the infix appear after the third character of the mnemonic.
18102
18103    Accordingly, the algorithm for looking up primary opcodes given
18104    an identifier is:
18105
18106    1. Look up the identifier in the opcode table.
18107       If we find a match, go to step U.
18108
18109    2. Look up the last two characters of the identifier in the
18110       conditions table.  If we find a match, look up the first N-2
18111       characters of the identifier in the opcode table.  If we
18112       find a match, go to step CE.
18113
18114    3. Look up the fourth and fifth characters of the identifier in
18115       the conditions table.  If we find a match, extract those
18116       characters from the identifier, and look up the remaining
18117       characters in the opcode table.  If we find a match, go
18118       to step CM.
18119
18120    4. Fail.
18121
18122    U. Examine the tag field of the opcode structure, in case this is
18123       one of the six instructions with its conditional infix in an
18124       unusual place.  If it is, the tag tells us where to find the
18125       infix; look it up in the conditions table and set inst.cond
18126       accordingly.  Otherwise, this is an unconditional instruction.
18127       Again set inst.cond accordingly.  Return the opcode structure.
18128
18129   CE. Examine the tag field to make sure this is an instruction that
18130       should receive a conditional suffix.  If it is not, fail.
18131       Otherwise, set inst.cond from the suffix we already looked up,
18132       and return the opcode structure.
18133
18134   CM. Examine the tag field to make sure this is an instruction that
18135       should receive a conditional infix after the third character.
18136       If it is not, fail.  Otherwise, undo the edits to the current
18137       line of input and proceed as for case CE.  */
18138
18139 static const struct asm_opcode *
18140 opcode_lookup (char **str)
18141 {
18142   char *end, *base;
18143   char *affix;
18144   const struct asm_opcode *opcode;
18145   const struct asm_cond *cond;
18146   char save[2];
18147
18148   /* Scan up to the end of the mnemonic, which must end in white space,
18149      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
18150   for (base = end = *str; *end != '\0'; end++)
18151     if (*end == ' ' || *end == '.')
18152       break;
18153
18154   if (end == base)
18155     return NULL;
18156
18157   /* Handle a possible width suffix and/or Neon type suffix.  */
18158   if (end[0] == '.')
18159     {
18160       int offset = 2;
18161
18162       /* The .w and .n suffixes are only valid if the unified syntax is in
18163          use.  */
18164       if (unified_syntax && end[1] == 'w')
18165         inst.size_req = 4;
18166       else if (unified_syntax && end[1] == 'n')
18167         inst.size_req = 2;
18168       else
18169         offset = 0;
18170
18171       inst.vectype.elems = 0;
18172
18173       *str = end + offset;
18174
18175       if (end[offset] == '.')
18176         {
18177           /* See if we have a Neon type suffix (possible in either unified or
18178              non-unified ARM syntax mode).  */
18179           if (parse_neon_type (&inst.vectype, str) == FAIL)
18180             return NULL;
18181         }
18182       else if (end[offset] != '\0' && end[offset] != ' ')
18183         return NULL;
18184     }
18185   else
18186     *str = end;
18187
18188   /* Look for unaffixed or special-case affixed mnemonic.  */
18189   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18190                                                     end - base);
18191   if (opcode)
18192     {
18193       /* step U */
18194       if (opcode->tag < OT_odd_infix_0)
18195         {
18196           inst.cond = COND_ALWAYS;
18197           return opcode;
18198         }
18199
18200       if (warn_on_deprecated && unified_syntax)
18201         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
18202       affix = base + (opcode->tag - OT_odd_infix_0);
18203       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18204       gas_assert (cond);
18205
18206       inst.cond = cond->value;
18207       return opcode;
18208     }
18209
18210   /* Cannot have a conditional suffix on a mnemonic of less than two
18211      characters.  */
18212   if (end - base < 3)
18213     return NULL;
18214
18215   /* Look for suffixed mnemonic.  */
18216   affix = end - 2;
18217   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18218   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18219                                                     affix - base);
18220   if (opcode && cond)
18221     {
18222       /* step CE */
18223       switch (opcode->tag)
18224         {
18225         case OT_cinfix3_legacy:
18226           /* Ignore conditional suffixes matched on infix only mnemonics.  */
18227           break;
18228
18229         case OT_cinfix3:
18230         case OT_cinfix3_deprecated:
18231         case OT_odd_infix_unc:
18232           if (!unified_syntax)
18233             return NULL;
18234           /* Fall through.  */
18235
18236         case OT_csuffix:
18237         case OT_csuffixF:
18238         case OT_csuf_or_in3:
18239           inst.cond = cond->value;
18240           return opcode;
18241
18242         case OT_unconditional:
18243         case OT_unconditionalF:
18244           if (thumb_mode)
18245             inst.cond = cond->value;
18246           else
18247             {
18248               /* Delayed diagnostic.  */
18249               inst.error = BAD_COND;
18250               inst.cond = COND_ALWAYS;
18251             }
18252           return opcode;
18253
18254         default:
18255           return NULL;
18256         }
18257     }
18258
18259   /* Cannot have a usual-position infix on a mnemonic of less than
18260      six characters (five would be a suffix).  */
18261   if (end - base < 6)
18262     return NULL;
18263
18264   /* Look for infixed mnemonic in the usual position.  */
18265   affix = base + 3;
18266   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18267   if (!cond)
18268     return NULL;
18269
18270   memcpy (save, affix, 2);
18271   memmove (affix, affix + 2, (end - affix) - 2);
18272   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18273                                                     (end - base) - 2);
18274   memmove (affix + 2, affix, (end - affix) - 2);
18275   memcpy (affix, save, 2);
18276
18277   if (opcode
18278       && (opcode->tag == OT_cinfix3
18279           || opcode->tag == OT_cinfix3_deprecated
18280           || opcode->tag == OT_csuf_or_in3
18281           || opcode->tag == OT_cinfix3_legacy))
18282     {
18283       /* Step CM.  */
18284       if (warn_on_deprecated && unified_syntax
18285           && (opcode->tag == OT_cinfix3
18286               || opcode->tag == OT_cinfix3_deprecated))
18287         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
18288
18289       inst.cond = cond->value;
18290       return opcode;
18291     }
18292
18293   return NULL;
18294 }
18295
18296 /* This function generates an initial IT instruction, leaving its block
18297    virtually open for the new instructions. Eventually,
18298    the mask will be updated by now_it_add_mask () each time
18299    a new instruction needs to be included in the IT block.
18300    Finally, the block is closed with close_automatic_it_block ().
18301    The block closure can be requested either from md_assemble (),
18302    a tencode (), or due to a label hook.  */
18303
18304 static void
18305 new_automatic_it_block (int cond)
18306 {
18307   now_it.state = AUTOMATIC_IT_BLOCK;
18308   now_it.mask = 0x18;
18309   now_it.cc = cond;
18310   now_it.block_length = 1;
18311   mapping_state (MAP_THUMB);
18312   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
18313   now_it.warn_deprecated = FALSE;
18314   now_it.insn_cond = TRUE;
18315 }
18316
18317 /* Close an automatic IT block.
18318    See comments in new_automatic_it_block ().  */
18319
18320 static void
18321 close_automatic_it_block (void)
18322 {
18323   now_it.mask = 0x10;
18324   now_it.block_length = 0;
18325 }
18326
18327 /* Update the mask of the current automatically-generated IT
18328    instruction. See comments in new_automatic_it_block ().  */
18329
18330 static void
18331 now_it_add_mask (int cond)
18332 {
18333 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
18334 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
18335                                               | ((bitvalue) << (nbit)))
18336   const int resulting_bit = (cond & 1);
18337
18338   now_it.mask &= 0xf;
18339   now_it.mask = SET_BIT_VALUE (now_it.mask,
18340                                    resulting_bit,
18341                                   (5 - now_it.block_length));
18342   now_it.mask = SET_BIT_VALUE (now_it.mask,
18343                                    1,
18344                                    ((5 - now_it.block_length) - 1) );
18345   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
18346
18347 #undef CLEAR_BIT
18348 #undef SET_BIT_VALUE
18349 }
18350
18351 /* The IT blocks handling machinery is accessed through the these functions:
18352      it_fsm_pre_encode ()               from md_assemble ()
18353      set_it_insn_type ()                optional, from the tencode functions
18354      set_it_insn_type_last ()           ditto
18355      in_it_block ()                     ditto
18356      it_fsm_post_encode ()              from md_assemble ()
18357      force_automatic_it_block_close ()  from label handling functions
18358
18359    Rationale:
18360      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
18361         initializing the IT insn type with a generic initial value depending
18362         on the inst.condition.
18363      2) During the tencode function, two things may happen:
18364         a) The tencode function overrides the IT insn type by
18365            calling either set_it_insn_type (type) or set_it_insn_type_last ().
18366         b) The tencode function queries the IT block state by
18367            calling in_it_block () (i.e. to determine narrow/not narrow mode).
18368
18369         Both set_it_insn_type and in_it_block run the internal FSM state
18370         handling function (handle_it_state), because: a) setting the IT insn
18371         type may incur in an invalid state (exiting the function),
18372         and b) querying the state requires the FSM to be updated.
18373         Specifically we want to avoid creating an IT block for conditional
18374         branches, so it_fsm_pre_encode is actually a guess and we can't
18375         determine whether an IT block is required until the tencode () routine
18376         has decided what type of instruction this actually it.
18377         Because of this, if set_it_insn_type and in_it_block have to be used,
18378         set_it_insn_type has to be called first.
18379
18380         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
18381         determines the insn IT type depending on the inst.cond code.
18382         When a tencode () routine encodes an instruction that can be
18383         either outside an IT block, or, in the case of being inside, has to be
18384         the last one, set_it_insn_type_last () will determine the proper
18385         IT instruction type based on the inst.cond code. Otherwise,
18386         set_it_insn_type can be called for overriding that logic or
18387         for covering other cases.
18388
18389         Calling handle_it_state () may not transition the IT block state to
18390         OUTSIDE_IT_BLOCK immediately, since the (current) state could be
18391         still queried. Instead, if the FSM determines that the state should
18392         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
18393         after the tencode () function: that's what it_fsm_post_encode () does.
18394
18395         Since in_it_block () calls the state handling function to get an
18396         updated state, an error may occur (due to invalid insns combination).
18397         In that case, inst.error is set.
18398         Therefore, inst.error has to be checked after the execution of
18399         the tencode () routine.
18400
18401      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
18402         any pending state change (if any) that didn't take place in
18403         handle_it_state () as explained above.  */
18404
18405 static void
18406 it_fsm_pre_encode (void)
18407 {
18408   if (inst.cond != COND_ALWAYS)
18409     inst.it_insn_type = INSIDE_IT_INSN;
18410   else
18411     inst.it_insn_type = OUTSIDE_IT_INSN;
18412
18413   now_it.state_handled = 0;
18414 }
18415
18416 /* IT state FSM handling function.  */
18417
18418 static int
18419 handle_it_state (void)
18420 {
18421   now_it.state_handled = 1;
18422   now_it.insn_cond = FALSE;
18423
18424   switch (now_it.state)
18425     {
18426     case OUTSIDE_IT_BLOCK:
18427       switch (inst.it_insn_type)
18428         {
18429         case OUTSIDE_IT_INSN:
18430           break;
18431
18432         case INSIDE_IT_INSN:
18433         case INSIDE_IT_LAST_INSN:
18434           if (thumb_mode == 0)
18435             {
18436               if (unified_syntax
18437                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
18438                 as_tsktsk (_("Warning: conditional outside an IT block"\
18439                              " for Thumb."));
18440             }
18441           else
18442             {
18443               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
18444                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
18445                 {
18446                   /* Automatically generate the IT instruction.  */
18447                   new_automatic_it_block (inst.cond);
18448                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
18449                     close_automatic_it_block ();
18450                 }
18451               else
18452                 {
18453                   inst.error = BAD_OUT_IT;
18454                   return FAIL;
18455                 }
18456             }
18457           break;
18458
18459         case IF_INSIDE_IT_LAST_INSN:
18460         case NEUTRAL_IT_INSN:
18461           break;
18462
18463         case IT_INSN:
18464           now_it.state = MANUAL_IT_BLOCK;
18465           now_it.block_length = 0;
18466           break;
18467         }
18468       break;
18469
18470     case AUTOMATIC_IT_BLOCK:
18471       /* Three things may happen now:
18472          a) We should increment current it block size;
18473          b) We should close current it block (closing insn or 4 insns);
18474          c) We should close current it block and start a new one (due
18475          to incompatible conditions or
18476          4 insns-length block reached).  */
18477
18478       switch (inst.it_insn_type)
18479         {
18480         case OUTSIDE_IT_INSN:
18481           /* The closure of the block shall happen immediately,
18482              so any in_it_block () call reports the block as closed.  */
18483           force_automatic_it_block_close ();
18484           break;
18485
18486         case INSIDE_IT_INSN:
18487         case INSIDE_IT_LAST_INSN:
18488         case IF_INSIDE_IT_LAST_INSN:
18489           now_it.block_length++;
18490
18491           if (now_it.block_length > 4
18492               || !now_it_compatible (inst.cond))
18493             {
18494               force_automatic_it_block_close ();
18495               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
18496                 new_automatic_it_block (inst.cond);
18497             }
18498           else
18499             {
18500               now_it.insn_cond = TRUE;
18501               now_it_add_mask (inst.cond);
18502             }
18503
18504           if (now_it.state == AUTOMATIC_IT_BLOCK
18505               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
18506                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
18507             close_automatic_it_block ();
18508           break;
18509
18510         case NEUTRAL_IT_INSN:
18511           now_it.block_length++;
18512           now_it.insn_cond = TRUE;
18513
18514           if (now_it.block_length > 4)
18515             force_automatic_it_block_close ();
18516           else
18517             now_it_add_mask (now_it.cc & 1);
18518           break;
18519
18520         case IT_INSN:
18521           close_automatic_it_block ();
18522           now_it.state = MANUAL_IT_BLOCK;
18523           break;
18524         }
18525       break;
18526
18527     case MANUAL_IT_BLOCK:
18528       {
18529         /* Check conditional suffixes.  */
18530         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
18531         int is_last;
18532         now_it.mask <<= 1;
18533         now_it.mask &= 0x1f;
18534         is_last = (now_it.mask == 0x10);
18535         now_it.insn_cond = TRUE;
18536
18537         switch (inst.it_insn_type)
18538           {
18539           case OUTSIDE_IT_INSN:
18540             inst.error = BAD_NOT_IT;
18541             return FAIL;
18542
18543           case INSIDE_IT_INSN:
18544             if (cond != inst.cond)
18545               {
18546                 inst.error = BAD_IT_COND;
18547                 return FAIL;
18548               }
18549             break;
18550
18551           case INSIDE_IT_LAST_INSN:
18552           case IF_INSIDE_IT_LAST_INSN:
18553             if (cond != inst.cond)
18554               {
18555                 inst.error = BAD_IT_COND;
18556                 return FAIL;
18557               }
18558             if (!is_last)
18559               {
18560                 inst.error = BAD_BRANCH;
18561                 return FAIL;
18562               }
18563             break;
18564
18565           case NEUTRAL_IT_INSN:
18566             /* The BKPT instruction is unconditional even in an IT block.  */
18567             break;
18568
18569           case IT_INSN:
18570             inst.error = BAD_IT_IT;
18571             return FAIL;
18572           }
18573       }
18574       break;
18575     }
18576
18577   return SUCCESS;
18578 }
18579
18580 struct depr_insn_mask
18581 {
18582   unsigned long pattern;
18583   unsigned long mask;
18584   const char* description;
18585 };
18586
18587 /* List of 16-bit instruction patterns deprecated in an IT block in
18588    ARMv8.  */
18589 static const struct depr_insn_mask depr_it_insns[] = {
18590   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
18591   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
18592   { 0xa000, 0xb800, N_("ADR") },
18593   { 0x4800, 0xf800, N_("Literal loads") },
18594   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
18595   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
18596   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
18597      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
18598   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
18599   { 0, 0, NULL }
18600 };
18601
18602 static void
18603 it_fsm_post_encode (void)
18604 {
18605   int is_last;
18606
18607   if (!now_it.state_handled)
18608     handle_it_state ();
18609
18610   if (now_it.insn_cond
18611       && !now_it.warn_deprecated
18612       && warn_on_deprecated
18613       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8)
18614       && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_m))
18615     {
18616       if (inst.instruction >= 0x10000)
18617         {
18618           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
18619                      "performance deprecated in ARMv8-A and ARMv8-R"));
18620           now_it.warn_deprecated = TRUE;
18621         }
18622       else
18623         {
18624           const struct depr_insn_mask *p = depr_it_insns;
18625
18626           while (p->mask != 0)
18627             {
18628               if ((inst.instruction & p->mask) == p->pattern)
18629                 {
18630                   as_tsktsk (_("IT blocks containing 16-bit Thumb "
18631                                "instructions of the following class are "
18632                                "performance deprecated in ARMv8-A and "
18633                                "ARMv8-R: %s"), p->description);
18634                   now_it.warn_deprecated = TRUE;
18635                   break;
18636                 }
18637
18638               ++p;
18639             }
18640         }
18641
18642       if (now_it.block_length > 1)
18643         {
18644           as_tsktsk (_("IT blocks containing more than one conditional "
18645                      "instruction are performance deprecated in ARMv8-A and "
18646                      "ARMv8-R"));
18647           now_it.warn_deprecated = TRUE;
18648         }
18649     }
18650
18651   is_last = (now_it.mask == 0x10);
18652   if (is_last)
18653     {
18654       now_it.state = OUTSIDE_IT_BLOCK;
18655       now_it.mask = 0;
18656     }
18657 }
18658
18659 static void
18660 force_automatic_it_block_close (void)
18661 {
18662   if (now_it.state == AUTOMATIC_IT_BLOCK)
18663     {
18664       close_automatic_it_block ();
18665       now_it.state = OUTSIDE_IT_BLOCK;
18666       now_it.mask = 0;
18667     }
18668 }
18669
18670 static int
18671 in_it_block (void)
18672 {
18673   if (!now_it.state_handled)
18674     handle_it_state ();
18675
18676   return now_it.state != OUTSIDE_IT_BLOCK;
18677 }
18678
18679 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
18680    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
18681    here, hence the "known" in the function name.  */
18682
18683 static bfd_boolean
18684 known_t32_only_insn (const struct asm_opcode *opcode)
18685 {
18686   /* Original Thumb-1 wide instruction.  */
18687   if (opcode->tencode == do_t_blx
18688       || opcode->tencode == do_t_branch23
18689       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
18690       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
18691     return TRUE;
18692
18693   /* Wide-only instruction added to ARMv8-M Baseline.  */
18694   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
18695       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
18696       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
18697       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
18698     return TRUE;
18699
18700   return FALSE;
18701 }
18702
18703 /* Whether wide instruction variant can be used if available for a valid OPCODE
18704    in ARCH.  */
18705
18706 static bfd_boolean
18707 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
18708 {
18709   if (known_t32_only_insn (opcode))
18710     return TRUE;
18711
18712   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
18713      of variant T3 of B.W is checked in do_t_branch.  */
18714   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18715       && opcode->tencode == do_t_branch)
18716     return TRUE;
18717
18718   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
18719   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18720       && opcode->tencode == do_t_mov_cmp
18721       /* Make sure CMP instruction is not affected.  */
18722       && opcode->aencode == do_mov)
18723     return TRUE;
18724
18725   /* Wide instruction variants of all instructions with narrow *and* wide
18726      variants become available with ARMv6t2.  Other opcodes are either
18727      narrow-only or wide-only and are thus available if OPCODE is valid.  */
18728   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
18729     return TRUE;
18730
18731   /* OPCODE with narrow only instruction variant or wide variant not
18732      available.  */
18733   return FALSE;
18734 }
18735
18736 void
18737 md_assemble (char *str)
18738 {
18739   char *p = str;
18740   const struct asm_opcode * opcode;
18741
18742   /* Align the previous label if needed.  */
18743   if (last_label_seen != NULL)
18744     {
18745       symbol_set_frag (last_label_seen, frag_now);
18746       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
18747       S_SET_SEGMENT (last_label_seen, now_seg);
18748     }
18749
18750   memset (&inst, '\0', sizeof (inst));
18751   inst.reloc.type = BFD_RELOC_UNUSED;
18752
18753   opcode = opcode_lookup (&p);
18754   if (!opcode)
18755     {
18756       /* It wasn't an instruction, but it might be a register alias of
18757          the form alias .req reg, or a Neon .dn/.qn directive.  */
18758       if (! create_register_alias (str, p)
18759           && ! create_neon_reg_alias (str, p))
18760         as_bad (_("bad instruction `%s'"), str);
18761
18762       return;
18763     }
18764
18765   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
18766     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
18767
18768   /* The value which unconditional instructions should have in place of the
18769      condition field.  */
18770   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
18771
18772   if (thumb_mode)
18773     {
18774       arm_feature_set variant;
18775
18776       variant = cpu_variant;
18777       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
18778       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
18779         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
18780       /* Check that this instruction is supported for this CPU.  */
18781       if (!opcode->tvariant
18782           || (thumb_mode == 1
18783               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
18784         {
18785           if (opcode->tencode == do_t_swi)
18786             as_bad (_("SVC is not permitted on this architecture"));
18787           else
18788             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
18789           return;
18790         }
18791       if (inst.cond != COND_ALWAYS && !unified_syntax
18792           && opcode->tencode != do_t_branch)
18793         {
18794           as_bad (_("Thumb does not support conditional execution"));
18795           return;
18796         }
18797
18798       /* Two things are addressed here:
18799          1) Implicit require narrow instructions on Thumb-1.
18800             This avoids relaxation accidentally introducing Thumb-2
18801             instructions.
18802          2) Reject wide instructions in non Thumb-2 cores.
18803
18804          Only instructions with narrow and wide variants need to be handled
18805          but selecting all non wide-only instructions is easier.  */
18806       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
18807           && !t32_insn_ok (variant, opcode))
18808         {
18809           if (inst.size_req == 0)
18810             inst.size_req = 2;
18811           else if (inst.size_req == 4)
18812             {
18813               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
18814                 as_bad (_("selected processor does not support 32bit wide "
18815                           "variant of instruction `%s'"), str);
18816               else
18817                 as_bad (_("selected processor does not support `%s' in "
18818                           "Thumb-2 mode"), str);
18819               return;
18820             }
18821         }
18822
18823       inst.instruction = opcode->tvalue;
18824
18825       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
18826         {
18827           /* Prepare the it_insn_type for those encodings that don't set
18828              it.  */
18829           it_fsm_pre_encode ();
18830
18831           opcode->tencode ();
18832
18833           it_fsm_post_encode ();
18834         }
18835
18836       if (!(inst.error || inst.relax))
18837         {
18838           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
18839           inst.size = (inst.instruction > 0xffff ? 4 : 2);
18840           if (inst.size_req && inst.size_req != inst.size)
18841             {
18842               as_bad (_("cannot honor width suffix -- `%s'"), str);
18843               return;
18844             }
18845         }
18846
18847       /* Something has gone badly wrong if we try to relax a fixed size
18848          instruction.  */
18849       gas_assert (inst.size_req == 0 || !inst.relax);
18850
18851       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18852                               *opcode->tvariant);
18853       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
18854          set those bits when Thumb-2 32-bit instructions are seen.  The impact
18855          of relaxable instructions will be considered later after we finish all
18856          relaxation.  */
18857       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
18858         variant = arm_arch_none;
18859       else
18860         variant = cpu_variant;
18861       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
18862         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18863                                 arm_ext_v6t2);
18864
18865       check_neon_suffixes;
18866
18867       if (!inst.error)
18868         {
18869           mapping_state (MAP_THUMB);
18870         }
18871     }
18872   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
18873     {
18874       bfd_boolean is_bx;
18875
18876       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
18877       is_bx = (opcode->aencode == do_bx);
18878
18879       /* Check that this instruction is supported for this CPU.  */
18880       if (!(is_bx && fix_v4bx)
18881           && !(opcode->avariant &&
18882                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
18883         {
18884           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
18885           return;
18886         }
18887       if (inst.size_req)
18888         {
18889           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
18890           return;
18891         }
18892
18893       inst.instruction = opcode->avalue;
18894       if (opcode->tag == OT_unconditionalF)
18895         inst.instruction |= 0xFU << 28;
18896       else
18897         inst.instruction |= inst.cond << 28;
18898       inst.size = INSN_SIZE;
18899       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
18900         {
18901           it_fsm_pre_encode ();
18902           opcode->aencode ();
18903           it_fsm_post_encode ();
18904         }
18905       /* Arm mode bx is marked as both v4T and v5 because it's still required
18906          on a hypothetical non-thumb v5 core.  */
18907       if (is_bx)
18908         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
18909       else
18910         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
18911                                 *opcode->avariant);
18912
18913       check_neon_suffixes;
18914
18915       if (!inst.error)
18916         {
18917           mapping_state (MAP_ARM);
18918         }
18919     }
18920   else
18921     {
18922       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
18923                 "-- `%s'"), str);
18924       return;
18925     }
18926   output_inst (str);
18927 }
18928
18929 static void
18930 check_it_blocks_finished (void)
18931 {
18932 #ifdef OBJ_ELF
18933   asection *sect;
18934
18935   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
18936     if (seg_info (sect)->tc_segment_info_data.current_it.state
18937         == MANUAL_IT_BLOCK)
18938       {
18939         as_warn (_("section '%s' finished with an open IT block."),
18940                  sect->name);
18941       }
18942 #else
18943   if (now_it.state == MANUAL_IT_BLOCK)
18944     as_warn (_("file finished with an open IT block."));
18945 #endif
18946 }
18947
18948 /* Various frobbings of labels and their addresses.  */
18949
18950 void
18951 arm_start_line_hook (void)
18952 {
18953   last_label_seen = NULL;
18954 }
18955
18956 void
18957 arm_frob_label (symbolS * sym)
18958 {
18959   last_label_seen = sym;
18960
18961   ARM_SET_THUMB (sym, thumb_mode);
18962
18963 #if defined OBJ_COFF || defined OBJ_ELF
18964   ARM_SET_INTERWORK (sym, support_interwork);
18965 #endif
18966
18967   force_automatic_it_block_close ();
18968
18969   /* Note - do not allow local symbols (.Lxxx) to be labelled
18970      as Thumb functions.  This is because these labels, whilst
18971      they exist inside Thumb code, are not the entry points for
18972      possible ARM->Thumb calls.  Also, these labels can be used
18973      as part of a computed goto or switch statement.  eg gcc
18974      can generate code that looks like this:
18975
18976                 ldr  r2, [pc, .Laaa]
18977                 lsl  r3, r3, #2
18978                 ldr  r2, [r3, r2]
18979                 mov  pc, r2
18980
18981        .Lbbb:  .word .Lxxx
18982        .Lccc:  .word .Lyyy
18983        ..etc...
18984        .Laaa:   .word Lbbb
18985
18986      The first instruction loads the address of the jump table.
18987      The second instruction converts a table index into a byte offset.
18988      The third instruction gets the jump address out of the table.
18989      The fourth instruction performs the jump.
18990
18991      If the address stored at .Laaa is that of a symbol which has the
18992      Thumb_Func bit set, then the linker will arrange for this address
18993      to have the bottom bit set, which in turn would mean that the
18994      address computation performed by the third instruction would end
18995      up with the bottom bit set.  Since the ARM is capable of unaligned
18996      word loads, the instruction would then load the incorrect address
18997      out of the jump table, and chaos would ensue.  */
18998   if (label_is_thumb_function_name
18999       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
19000       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
19001     {
19002       /* When the address of a Thumb function is taken the bottom
19003          bit of that address should be set.  This will allow
19004          interworking between Arm and Thumb functions to work
19005          correctly.  */
19006
19007       THUMB_SET_FUNC (sym, 1);
19008
19009       label_is_thumb_function_name = FALSE;
19010     }
19011
19012   dwarf2_emit_label (sym);
19013 }
19014
19015 bfd_boolean
19016 arm_data_in_code (void)
19017 {
19018   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
19019     {
19020       *input_line_pointer = '/';
19021       input_line_pointer += 5;
19022       *input_line_pointer = 0;
19023       return TRUE;
19024     }
19025
19026   return FALSE;
19027 }
19028
19029 char *
19030 arm_canonicalize_symbol_name (char * name)
19031 {
19032   int len;
19033
19034   if (thumb_mode && (len = strlen (name)) > 5
19035       && streq (name + len - 5, "/data"))
19036     *(name + len - 5) = 0;
19037
19038   return name;
19039 }
19040 \f
19041 /* Table of all register names defined by default.  The user can
19042    define additional names with .req.  Note that all register names
19043    should appear in both upper and lowercase variants.  Some registers
19044    also have mixed-case names.  */
19045
19046 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
19047 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
19048 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
19049 #define REGSET(p,t) \
19050   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
19051   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
19052   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
19053   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
19054 #define REGSETH(p,t) \
19055   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
19056   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
19057   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
19058   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
19059 #define REGSET2(p,t) \
19060   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
19061   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
19062   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
19063   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
19064 #define SPLRBANK(base,bank,t) \
19065   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
19066   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
19067   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
19068   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
19069   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
19070   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
19071
19072 static const struct reg_entry reg_names[] =
19073 {
19074   /* ARM integer registers.  */
19075   REGSET(r, RN), REGSET(R, RN),
19076
19077   /* ATPCS synonyms.  */
19078   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
19079   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
19080   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
19081
19082   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
19083   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
19084   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
19085
19086   /* Well-known aliases.  */
19087   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
19088   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
19089
19090   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
19091   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
19092
19093   /* Coprocessor numbers.  */
19094   REGSET(p, CP), REGSET(P, CP),
19095
19096   /* Coprocessor register numbers.  The "cr" variants are for backward
19097      compatibility.  */
19098   REGSET(c,  CN), REGSET(C, CN),
19099   REGSET(cr, CN), REGSET(CR, CN),
19100
19101   /* ARM banked registers.  */
19102   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
19103   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
19104   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
19105   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
19106   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
19107   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
19108   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
19109
19110   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
19111   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
19112   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
19113   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
19114   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
19115   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
19116   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
19117   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
19118
19119   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
19120   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
19121   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
19122   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
19123   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
19124   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
19125   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
19126   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
19127   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
19128
19129   /* FPA registers.  */
19130   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
19131   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
19132
19133   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
19134   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
19135
19136   /* VFP SP registers.  */
19137   REGSET(s,VFS),  REGSET(S,VFS),
19138   REGSETH(s,VFS), REGSETH(S,VFS),
19139
19140   /* VFP DP Registers.  */
19141   REGSET(d,VFD),  REGSET(D,VFD),
19142   /* Extra Neon DP registers.  */
19143   REGSETH(d,VFD), REGSETH(D,VFD),
19144
19145   /* Neon QP registers.  */
19146   REGSET2(q,NQ),  REGSET2(Q,NQ),
19147
19148   /* VFP control registers.  */
19149   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
19150   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
19151   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
19152   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
19153   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
19154   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
19155   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
19156
19157   /* Maverick DSP coprocessor registers.  */
19158   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
19159   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
19160
19161   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
19162   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
19163   REGDEF(dspsc,0,DSPSC),
19164
19165   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
19166   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
19167   REGDEF(DSPSC,0,DSPSC),
19168
19169   /* iWMMXt data registers - p0, c0-15.  */
19170   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
19171
19172   /* iWMMXt control registers - p1, c0-3.  */
19173   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
19174   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
19175   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
19176   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
19177
19178   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
19179   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
19180   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
19181   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
19182   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
19183
19184   /* XScale accumulator registers.  */
19185   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
19186 };
19187 #undef REGDEF
19188 #undef REGNUM
19189 #undef REGSET
19190
19191 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
19192    within psr_required_here.  */
19193 static const struct asm_psr psrs[] =
19194 {
19195   /* Backward compatibility notation.  Note that "all" is no longer
19196      truly all possible PSR bits.  */
19197   {"all",  PSR_c | PSR_f},
19198   {"flg",  PSR_f},
19199   {"ctl",  PSR_c},
19200
19201   /* Individual flags.  */
19202   {"f",    PSR_f},
19203   {"c",    PSR_c},
19204   {"x",    PSR_x},
19205   {"s",    PSR_s},
19206
19207   /* Combinations of flags.  */
19208   {"fs",   PSR_f | PSR_s},
19209   {"fx",   PSR_f | PSR_x},
19210   {"fc",   PSR_f | PSR_c},
19211   {"sf",   PSR_s | PSR_f},
19212   {"sx",   PSR_s | PSR_x},
19213   {"sc",   PSR_s | PSR_c},
19214   {"xf",   PSR_x | PSR_f},
19215   {"xs",   PSR_x | PSR_s},
19216   {"xc",   PSR_x | PSR_c},
19217   {"cf",   PSR_c | PSR_f},
19218   {"cs",   PSR_c | PSR_s},
19219   {"cx",   PSR_c | PSR_x},
19220   {"fsx",  PSR_f | PSR_s | PSR_x},
19221   {"fsc",  PSR_f | PSR_s | PSR_c},
19222   {"fxs",  PSR_f | PSR_x | PSR_s},
19223   {"fxc",  PSR_f | PSR_x | PSR_c},
19224   {"fcs",  PSR_f | PSR_c | PSR_s},
19225   {"fcx",  PSR_f | PSR_c | PSR_x},
19226   {"sfx",  PSR_s | PSR_f | PSR_x},
19227   {"sfc",  PSR_s | PSR_f | PSR_c},
19228   {"sxf",  PSR_s | PSR_x | PSR_f},
19229   {"sxc",  PSR_s | PSR_x | PSR_c},
19230   {"scf",  PSR_s | PSR_c | PSR_f},
19231   {"scx",  PSR_s | PSR_c | PSR_x},
19232   {"xfs",  PSR_x | PSR_f | PSR_s},
19233   {"xfc",  PSR_x | PSR_f | PSR_c},
19234   {"xsf",  PSR_x | PSR_s | PSR_f},
19235   {"xsc",  PSR_x | PSR_s | PSR_c},
19236   {"xcf",  PSR_x | PSR_c | PSR_f},
19237   {"xcs",  PSR_x | PSR_c | PSR_s},
19238   {"cfs",  PSR_c | PSR_f | PSR_s},
19239   {"cfx",  PSR_c | PSR_f | PSR_x},
19240   {"csf",  PSR_c | PSR_s | PSR_f},
19241   {"csx",  PSR_c | PSR_s | PSR_x},
19242   {"cxf",  PSR_c | PSR_x | PSR_f},
19243   {"cxs",  PSR_c | PSR_x | PSR_s},
19244   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
19245   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
19246   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
19247   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
19248   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
19249   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
19250   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
19251   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
19252   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
19253   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
19254   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
19255   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
19256   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
19257   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
19258   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
19259   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
19260   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
19261   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
19262   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
19263   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
19264   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
19265   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
19266   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
19267   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
19268 };
19269
19270 /* Table of V7M psr names.  */
19271 static const struct asm_psr v7m_psrs[] =
19272 {
19273   {"apsr",         0x0 }, {"APSR",         0x0 },
19274   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
19275   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
19276   {"psr",          0x3 }, {"PSR",          0x3 },
19277   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
19278   {"ipsr",         0x5 }, {"IPSR",         0x5 },
19279   {"epsr",         0x6 }, {"EPSR",         0x6 },
19280   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
19281   {"msp",          0x8 }, {"MSP",          0x8 },
19282   {"psp",          0x9 }, {"PSP",          0x9 },
19283   {"msplim",       0xa }, {"MSPLIM",       0xa },
19284   {"psplim",       0xb }, {"PSPLIM",       0xb },
19285   {"primask",      0x10}, {"PRIMASK",      0x10},
19286   {"basepri",      0x11}, {"BASEPRI",      0x11},
19287   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
19288   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
19289   {"control",      0x14}, {"CONTROL",      0x14},
19290   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
19291   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
19292   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
19293   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
19294   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
19295   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
19296   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
19297   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
19298   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
19299 };
19300
19301 /* Table of all shift-in-operand names.  */
19302 static const struct asm_shift_name shift_names [] =
19303 {
19304   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
19305   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
19306   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
19307   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
19308   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
19309   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
19310 };
19311
19312 /* Table of all explicit relocation names.  */
19313 #ifdef OBJ_ELF
19314 static struct reloc_entry reloc_names[] =
19315 {
19316   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
19317   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
19318   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
19319   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
19320   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
19321   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
19322   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
19323   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
19324   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
19325   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
19326   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
19327   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
19328   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
19329         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
19330   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
19331         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
19332   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
19333         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ},
19334   { "gotfuncdesc", BFD_RELOC_ARM_GOTFUNCDESC },
19335         { "GOTFUNCDESC", BFD_RELOC_ARM_GOTFUNCDESC },
19336   { "gotofffuncdesc", BFD_RELOC_ARM_GOTOFFFUNCDESC },
19337         { "GOTOFFFUNCDESC", BFD_RELOC_ARM_GOTOFFFUNCDESC },
19338   { "funcdesc", BFD_RELOC_ARM_FUNCDESC },
19339         { "FUNCDESC", BFD_RELOC_ARM_FUNCDESC },
19340    { "tlsgd_fdpic", BFD_RELOC_ARM_TLS_GD32_FDPIC },      { "TLSGD_FDPIC", BFD_RELOC_ARM_TLS_GD32_FDPIC },
19341    { "tlsldm_fdpic", BFD_RELOC_ARM_TLS_LDM32_FDPIC },    { "TLSLDM_FDPIC", BFD_RELOC_ARM_TLS_LDM32_FDPIC },
19342    { "gottpoff_fdpic", BFD_RELOC_ARM_TLS_IE32_FDPIC },   { "GOTTPOFF_FDIC", BFD_RELOC_ARM_TLS_IE32_FDPIC },
19343 };
19344 #endif
19345
19346 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
19347 static const struct asm_cond conds[] =
19348 {
19349   {"eq", 0x0},
19350   {"ne", 0x1},
19351   {"cs", 0x2}, {"hs", 0x2},
19352   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
19353   {"mi", 0x4},
19354   {"pl", 0x5},
19355   {"vs", 0x6},
19356   {"vc", 0x7},
19357   {"hi", 0x8},
19358   {"ls", 0x9},
19359   {"ge", 0xa},
19360   {"lt", 0xb},
19361   {"gt", 0xc},
19362   {"le", 0xd},
19363   {"al", 0xe}
19364 };
19365
19366 #define UL_BARRIER(L,U,CODE,FEAT) \
19367   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
19368   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
19369
19370 static struct asm_barrier_opt barrier_opt_names[] =
19371 {
19372   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
19373   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
19374   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
19375   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
19376   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
19377   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
19378   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
19379   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
19380   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
19381   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
19382   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
19383   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
19384   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
19385   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
19386   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
19387   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
19388 };
19389
19390 #undef UL_BARRIER
19391
19392 /* Table of ARM-format instructions.    */
19393
19394 /* Macros for gluing together operand strings.  N.B. In all cases
19395    other than OPS0, the trailing OP_stop comes from default
19396    zero-initialization of the unspecified elements of the array.  */
19397 #define OPS0()            { OP_stop, }
19398 #define OPS1(a)           { OP_##a, }
19399 #define OPS2(a,b)         { OP_##a,OP_##b, }
19400 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
19401 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
19402 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
19403 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
19404
19405 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
19406    This is useful when mixing operands for ARM and THUMB, i.e. using the
19407    MIX_ARM_THUMB_OPERANDS macro.
19408    In order to use these macros, prefix the number of operands with _
19409    e.g. _3.  */
19410 #define OPS_1(a)           { a, }
19411 #define OPS_2(a,b)         { a,b, }
19412 #define OPS_3(a,b,c)       { a,b,c, }
19413 #define OPS_4(a,b,c,d)     { a,b,c,d, }
19414 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
19415 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
19416
19417 /* These macros abstract out the exact format of the mnemonic table and
19418    save some repeated characters.  */
19419
19420 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
19421 #define TxCE(mnem, op, top, nops, ops, ae, te) \
19422   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
19423     THUMB_VARIANT, do_##ae, do_##te }
19424
19425 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
19426    a T_MNEM_xyz enumerator.  */
19427 #define TCE(mnem, aop, top, nops, ops, ae, te) \
19428       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
19429 #define tCE(mnem, aop, top, nops, ops, ae, te) \
19430       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19431
19432 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
19433    infix after the third character.  */
19434 #define TxC3(mnem, op, top, nops, ops, ae, te) \
19435   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
19436     THUMB_VARIANT, do_##ae, do_##te }
19437 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
19438   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
19439     THUMB_VARIANT, do_##ae, do_##te }
19440 #define TC3(mnem, aop, top, nops, ops, ae, te) \
19441       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
19442 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
19443       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
19444 #define tC3(mnem, aop, top, nops, ops, ae, te) \
19445       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19446 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
19447       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19448
19449 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
19450    field is still 0xE.  Many of the Thumb variants can be executed
19451    conditionally, so this is checked separately.  */
19452 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
19453   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19454     THUMB_VARIANT, do_##ae, do_##te }
19455
19456 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
19457    Used by mnemonics that have very minimal differences in the encoding for
19458    ARM and Thumb variants and can be handled in a common function.  */
19459 #define TUEc(mnem, op, top, nops, ops, en) \
19460   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19461     THUMB_VARIANT, do_##en, do_##en }
19462
19463 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
19464    condition code field.  */
19465 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
19466   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
19467     THUMB_VARIANT, do_##ae, do_##te }
19468
19469 /* ARM-only variants of all the above.  */
19470 #define CE(mnem,  op, nops, ops, ae)    \
19471   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19472
19473 #define C3(mnem, op, nops, ops, ae)     \
19474   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19475
19476 /* Thumb-only variants of TCE and TUE.  */
19477 #define ToC(mnem, top, nops, ops, te) \
19478   { mnem, OPS##nops ops, OT_csuffix, 0x0, 0x##top, 0, THUMB_VARIANT, NULL, \
19479     do_##te }
19480
19481 #define ToU(mnem, top, nops, ops, te) \
19482   { mnem, OPS##nops ops, OT_unconditional, 0x0, 0x##top, 0, THUMB_VARIANT, \
19483     NULL, do_##te }
19484
19485 /* Legacy mnemonics that always have conditional infix after the third
19486    character.  */
19487 #define CL(mnem, op, nops, ops, ae)     \
19488   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19489     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19490
19491 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
19492 #define cCE(mnem,  op, nops, ops, ae)   \
19493   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19494
19495 /* Legacy coprocessor instructions where conditional infix and conditional
19496    suffix are ambiguous.  For consistency this includes all FPA instructions,
19497    not just the potentially ambiguous ones.  */
19498 #define cCL(mnem, op, nops, ops, ae)    \
19499   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19500     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19501
19502 /* Coprocessor, takes either a suffix or a position-3 infix
19503    (for an FPA corner case). */
19504 #define C3E(mnem, op, nops, ops, ae) \
19505   { mnem, OPS##nops ops, OT_csuf_or_in3, \
19506     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19507
19508 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
19509   { m1 #m2 m3, OPS##nops ops, \
19510     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
19511     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19512
19513 #define CM(m1, m2, op, nops, ops, ae)   \
19514   xCM_ (m1,   , m2, op, nops, ops, ae), \
19515   xCM_ (m1, eq, m2, op, nops, ops, ae), \
19516   xCM_ (m1, ne, m2, op, nops, ops, ae), \
19517   xCM_ (m1, cs, m2, op, nops, ops, ae), \
19518   xCM_ (m1, hs, m2, op, nops, ops, ae), \
19519   xCM_ (m1, cc, m2, op, nops, ops, ae), \
19520   xCM_ (m1, ul, m2, op, nops, ops, ae), \
19521   xCM_ (m1, lo, m2, op, nops, ops, ae), \
19522   xCM_ (m1, mi, m2, op, nops, ops, ae), \
19523   xCM_ (m1, pl, m2, op, nops, ops, ae), \
19524   xCM_ (m1, vs, m2, op, nops, ops, ae), \
19525   xCM_ (m1, vc, m2, op, nops, ops, ae), \
19526   xCM_ (m1, hi, m2, op, nops, ops, ae), \
19527   xCM_ (m1, ls, m2, op, nops, ops, ae), \
19528   xCM_ (m1, ge, m2, op, nops, ops, ae), \
19529   xCM_ (m1, lt, m2, op, nops, ops, ae), \
19530   xCM_ (m1, gt, m2, op, nops, ops, ae), \
19531   xCM_ (m1, le, m2, op, nops, ops, ae), \
19532   xCM_ (m1, al, m2, op, nops, ops, ae)
19533
19534 #define UE(mnem, op, nops, ops, ae)     \
19535   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19536
19537 #define UF(mnem, op, nops, ops, ae)     \
19538   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19539
19540 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
19541    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
19542    use the same encoding function for each.  */
19543 #define NUF(mnem, op, nops, ops, enc)                                   \
19544   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
19545     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19546
19547 /* Neon data processing, version which indirects through neon_enc_tab for
19548    the various overloaded versions of opcodes.  */
19549 #define nUF(mnem, op, nops, ops, enc)                                   \
19550   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
19551     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19552
19553 /* Neon insn with conditional suffix for the ARM version, non-overloaded
19554    version.  */
19555 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
19556   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
19557     THUMB_VARIANT, do_##enc, do_##enc }
19558
19559 #define NCE(mnem, op, nops, ops, enc)                                   \
19560    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19561
19562 #define NCEF(mnem, op, nops, ops, enc)                                  \
19563     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19564
19565 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
19566 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
19567   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
19568     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19569
19570 #define nCE(mnem, op, nops, ops, enc)                                   \
19571    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19572
19573 #define nCEF(mnem, op, nops, ops, enc)                                  \
19574     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19575
19576 #define do_0 0
19577
19578 static const struct asm_opcode insns[] =
19579 {
19580 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
19581 #define THUMB_VARIANT  & arm_ext_v4t
19582  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
19583  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
19584  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
19585  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
19586  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
19587  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
19588  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
19589  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
19590  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
19591  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
19592  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
19593  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
19594  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
19595  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
19596  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
19597  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
19598
19599  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
19600     for setting PSR flag bits.  They are obsolete in V6 and do not
19601     have Thumb equivalents. */
19602  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19603  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19604   CL("tstp",    110f000,           2, (RR, SH),      cmp),
19605  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19606  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19607   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
19608  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19609  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19610   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
19611
19612  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
19613  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
19614  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
19615  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
19616
19617  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
19618  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19619  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
19620                                                                 OP_RRnpc),
19621                                         OP_ADDRGLDR),ldst, t_ldst),
19622  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19623
19624  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19625  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19626  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19627  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19628  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19629  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19630
19631  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
19632  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
19633
19634   /* Pseudo ops.  */
19635  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
19636   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
19637  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
19638  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
19639
19640   /* Thumb-compatibility pseudo ops.  */
19641  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
19642  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
19643  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
19644  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
19645  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
19646  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
19647  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
19648  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
19649  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
19650  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
19651  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
19652  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
19653
19654  /* These may simplify to neg.  */
19655  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
19656  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
19657
19658 #undef THUMB_VARIANT
19659 #define THUMB_VARIANT  & arm_ext_os
19660
19661  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
19662  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
19663
19664 #undef  THUMB_VARIANT
19665 #define THUMB_VARIANT  & arm_ext_v6
19666
19667  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
19668
19669  /* V1 instructions with no Thumb analogue prior to V6T2.  */
19670 #undef  THUMB_VARIANT
19671 #define THUMB_VARIANT  & arm_ext_v6t2
19672
19673  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19674  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19675   CL("teqp",    130f000,           2, (RR, SH),      cmp),
19676
19677  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19678  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19679  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
19680  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19681
19682  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19683  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19684
19685  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19686  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19687
19688  /* V1 instructions with no Thumb analogue at all.  */
19689   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
19690   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
19691
19692   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
19693   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
19694   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
19695   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
19696   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
19697   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
19698   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
19699   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
19700
19701 #undef  ARM_VARIANT
19702 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
19703 #undef  THUMB_VARIANT
19704 #define THUMB_VARIANT  & arm_ext_v4t
19705
19706  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
19707  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
19708
19709 #undef  THUMB_VARIANT
19710 #define THUMB_VARIANT  & arm_ext_v6t2
19711
19712  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19713   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
19714
19715   /* Generic coprocessor instructions.  */
19716  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19717  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19718  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19719  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19720  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19721  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19722  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
19723
19724 #undef  ARM_VARIANT
19725 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
19726
19727   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19728   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19729
19730 #undef  ARM_VARIANT
19731 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
19732 #undef  THUMB_VARIANT
19733 #define THUMB_VARIANT  & arm_ext_msr
19734
19735  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
19736  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
19737
19738 #undef  ARM_VARIANT
19739 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
19740 #undef  THUMB_VARIANT
19741 #define THUMB_VARIANT  & arm_ext_v6t2
19742
19743  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19744   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19745  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19746   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19747  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19748   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19749  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19750   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19751
19752 #undef  ARM_VARIANT
19753 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
19754 #undef  THUMB_VARIANT
19755 #define THUMB_VARIANT  & arm_ext_v4t
19756
19757  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19758  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19759  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19760  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19761  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19762  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19763
19764 #undef  ARM_VARIANT
19765 #define ARM_VARIANT  & arm_ext_v4t_5
19766
19767   /* ARM Architecture 4T.  */
19768   /* Note: bx (and blx) are required on V5, even if the processor does
19769      not support Thumb.  */
19770  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
19771
19772 #undef  ARM_VARIANT
19773 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
19774 #undef  THUMB_VARIANT
19775 #define THUMB_VARIANT  & arm_ext_v5t
19776
19777   /* Note: blx has 2 variants; the .value coded here is for
19778      BLX(2).  Only this variant has conditional execution.  */
19779  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
19780  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
19781
19782 #undef  THUMB_VARIANT
19783 #define THUMB_VARIANT  & arm_ext_v6t2
19784
19785  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
19786  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19787  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19788  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19789  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19790  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19791  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19792  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19793
19794 #undef  ARM_VARIANT
19795 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
19796 #undef  THUMB_VARIANT
19797 #define THUMB_VARIANT  & arm_ext_v5exp
19798
19799  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19800  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19801  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19802  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19803
19804  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19805  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19806
19807  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19808  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19809  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19810  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19811
19812  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19813  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19814  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19815  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19816
19817  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19818  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19819
19820  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19821  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19822  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19823  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19824
19825 #undef  ARM_VARIANT
19826 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
19827 #undef  THUMB_VARIANT
19828 #define THUMB_VARIANT  & arm_ext_v6t2
19829
19830  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
19831  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
19832      ldrd, t_ldstd),
19833  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
19834                                        ADDRGLDRS), ldrd, t_ldstd),
19835
19836  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19837  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19838
19839 #undef  ARM_VARIANT
19840 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
19841
19842  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
19843
19844 #undef  ARM_VARIANT
19845 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
19846 #undef  THUMB_VARIANT
19847 #define THUMB_VARIANT  & arm_ext_v6
19848
19849  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19850  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19851  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19852  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19853  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19854  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19855  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19856  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19857  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19858  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
19859
19860 #undef  THUMB_VARIANT
19861 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19862
19863  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
19864  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19865                                       strex,  t_strex),
19866 #undef  THUMB_VARIANT
19867 #define THUMB_VARIANT  & arm_ext_v6t2
19868
19869  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19870  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19871
19872  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
19873  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
19874
19875 /*  ARM V6 not included in V7M.  */
19876 #undef  THUMB_VARIANT
19877 #define THUMB_VARIANT  & arm_ext_v6_notm
19878  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19879  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19880   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
19881   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
19882  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19883  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19884   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
19885  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19886   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
19887  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19888  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19889  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19890   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
19891   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
19892   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
19893   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
19894  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19895  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19896  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
19897
19898 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
19899 #undef  THUMB_VARIANT
19900 #define THUMB_VARIANT  & arm_ext_v6_dsp
19901  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
19902  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
19903  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19904  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19905  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19906  /* Old name for QASX.  */
19907  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19908  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19909  /* Old name for QSAX.  */
19910  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19911  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19912  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19913  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19914  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19915  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19916  /* Old name for SASX.  */
19917  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19918  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19919  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19920  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19921  /* Old name for SHASX.  */
19922  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19923  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19924  /* Old name for SHSAX.  */
19925  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19926  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19927  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19928  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19929  /* Old name for SSAX.  */
19930  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19931  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19932  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19933  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19934  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19935  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19936  /* Old name for UASX.  */
19937  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19938  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19939  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19940  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19941  /* Old name for UHASX.  */
19942  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19943  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19944  /* Old name for UHSAX.  */
19945  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19946  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19947  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19948  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19949  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19950  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19951  /* Old name for UQASX.  */
19952  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19953  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19954  /* Old name for UQSAX.  */
19955  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19956  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19957  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19958  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19959  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19960  /* Old name for USAX.  */
19961  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19962  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19963  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19964  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19965  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19966  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19967  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19968  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19969  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19970  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19971  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19972  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19973  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19974  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19975  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19976  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19977  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19978  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19979  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19980  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19981  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19982  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19983  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19984  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19985  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19986  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19987  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19988  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19989  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19990  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
19991  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
19992  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
19993  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
19994  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
19995
19996 #undef  ARM_VARIANT
19997 #define ARM_VARIANT   & arm_ext_v6k_v6t2
19998 #undef  THUMB_VARIANT
19999 #define THUMB_VARIANT & arm_ext_v6k_v6t2
20000
20001  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
20002  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
20003  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
20004  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
20005
20006 #undef  THUMB_VARIANT
20007 #define THUMB_VARIANT  & arm_ext_v6_notm
20008  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
20009                                       ldrexd, t_ldrexd),
20010  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
20011                                        RRnpcb), strexd, t_strexd),
20012
20013 #undef  THUMB_VARIANT
20014 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
20015  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
20016      rd_rn,  rd_rn),
20017  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
20018      rd_rn,  rd_rn),
20019  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
20020      strex, t_strexbh),
20021  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
20022      strex, t_strexbh),
20023  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
20024
20025 #undef  ARM_VARIANT
20026 #define ARM_VARIANT    & arm_ext_sec
20027 #undef  THUMB_VARIANT
20028 #define THUMB_VARIANT  & arm_ext_sec
20029
20030  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
20031
20032 #undef  ARM_VARIANT
20033 #define ARM_VARIANT    & arm_ext_virt
20034 #undef  THUMB_VARIANT
20035 #define THUMB_VARIANT    & arm_ext_virt
20036
20037  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
20038  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
20039
20040 #undef  ARM_VARIANT
20041 #define ARM_VARIANT    & arm_ext_pan
20042 #undef  THUMB_VARIANT
20043 #define THUMB_VARIANT  & arm_ext_pan
20044
20045  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
20046
20047 #undef  ARM_VARIANT
20048 #define ARM_VARIANT    & arm_ext_v6t2
20049 #undef  THUMB_VARIANT
20050 #define THUMB_VARIANT  & arm_ext_v6t2
20051
20052  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
20053  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
20054  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
20055  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
20056
20057  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
20058  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
20059
20060  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
20061  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
20062  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
20063  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
20064
20065 #undef  ARM_VARIANT
20066 #define ARM_VARIANT    & arm_ext_v3
20067 #undef  THUMB_VARIANT
20068 #define THUMB_VARIANT  & arm_ext_v6t2
20069
20070  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
20071  TUF("ssbb",    57ff040, f3bf8f40, 0, (), noargs, t_csdb),
20072  TUF("pssbb",   57ff044, f3bf8f44, 0, (), noargs, t_csdb),
20073
20074 #undef  ARM_VARIANT
20075 #define ARM_VARIANT    & arm_ext_v6t2
20076 #undef  THUMB_VARIANT
20077 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
20078  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
20079  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
20080
20081  /* Thumb-only instructions.  */
20082 #undef  ARM_VARIANT
20083 #define ARM_VARIANT NULL
20084   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
20085   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
20086
20087  /* ARM does not really have an IT instruction, so always allow it.
20088     The opcode is copied from Thumb in order to allow warnings in
20089     -mimplicit-it=[never | arm] modes.  */
20090 #undef  ARM_VARIANT
20091 #define ARM_VARIANT  & arm_ext_v1
20092 #undef  THUMB_VARIANT
20093 #define THUMB_VARIANT  & arm_ext_v6t2
20094
20095  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
20096  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
20097  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
20098  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
20099  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
20100  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
20101  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
20102  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
20103  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
20104  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
20105  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
20106  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
20107  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
20108  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
20109  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
20110  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
20111  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
20112  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
20113
20114  /* Thumb2 only instructions.  */
20115 #undef  ARM_VARIANT
20116 #define ARM_VARIANT  NULL
20117
20118  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
20119  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
20120  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
20121  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
20122  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
20123  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
20124
20125  /* Hardware division instructions.  */
20126 #undef  ARM_VARIANT
20127 #define ARM_VARIANT    & arm_ext_adiv
20128 #undef  THUMB_VARIANT
20129 #define THUMB_VARIANT  & arm_ext_div
20130
20131  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
20132  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
20133
20134  /* ARM V6M/V7 instructions.  */
20135 #undef  ARM_VARIANT
20136 #define ARM_VARIANT    & arm_ext_barrier
20137 #undef  THUMB_VARIANT
20138 #define THUMB_VARIANT  & arm_ext_barrier
20139
20140  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
20141  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
20142  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
20143
20144  /* ARM V7 instructions.  */
20145 #undef  ARM_VARIANT
20146 #define ARM_VARIANT    & arm_ext_v7
20147 #undef  THUMB_VARIANT
20148 #define THUMB_VARIANT  & arm_ext_v7
20149
20150  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
20151  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
20152
20153 #undef  ARM_VARIANT
20154 #define ARM_VARIANT    & arm_ext_mp
20155 #undef  THUMB_VARIANT
20156 #define THUMB_VARIANT  & arm_ext_mp
20157
20158  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
20159
20160  /* AArchv8 instructions.  */
20161 #undef  ARM_VARIANT
20162 #define ARM_VARIANT   & arm_ext_v8
20163
20164 /* Instructions shared between armv8-a and armv8-m.  */
20165 #undef  THUMB_VARIANT
20166 #define THUMB_VARIANT & arm_ext_atomics
20167
20168  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20169  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20170  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20171  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20172  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20173  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20174  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20175  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
20176  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20177  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
20178                                                         stlex,  t_stlex),
20179  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
20180                                                         stlex, t_stlex),
20181  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
20182                                                         stlex, t_stlex),
20183 #undef  THUMB_VARIANT
20184 #define THUMB_VARIANT & arm_ext_v8
20185
20186  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
20187  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
20188                                                         ldrexd, t_ldrexd),
20189  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
20190                                                         strexd, t_strexd),
20191
20192 /* Defined in V8 but is in undefined encoding space for earlier
20193    architectures.  However earlier architectures are required to treat
20194    this instuction as a semihosting trap as well.  Hence while not explicitly
20195    defined as such, it is in fact correct to define the instruction for all
20196    architectures.  */
20197 #undef  THUMB_VARIANT
20198 #define THUMB_VARIANT  & arm_ext_v1
20199 #undef  ARM_VARIANT
20200 #define ARM_VARIANT  & arm_ext_v1
20201  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
20202
20203  /* ARMv8 T32 only.  */
20204 #undef  ARM_VARIANT
20205 #define ARM_VARIANT  NULL
20206  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
20207  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
20208  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
20209
20210   /* FP for ARMv8.  */
20211 #undef  ARM_VARIANT
20212 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
20213 #undef  THUMB_VARIANT
20214 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
20215
20216   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
20217   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
20218   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
20219   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
20220   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
20221   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
20222   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
20223   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
20224   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
20225   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
20226   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
20227   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
20228   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
20229   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
20230   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
20231   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
20232   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
20233
20234   /* Crypto v1 extensions.  */
20235 #undef  ARM_VARIANT
20236 #define ARM_VARIANT & fpu_crypto_ext_armv8
20237 #undef  THUMB_VARIANT
20238 #define THUMB_VARIANT & fpu_crypto_ext_armv8
20239
20240   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
20241   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
20242   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
20243   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
20244   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
20245   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
20246   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
20247   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
20248   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
20249   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
20250   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
20251   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
20252   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
20253   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
20254
20255 #undef  ARM_VARIANT
20256 #define ARM_VARIANT   & crc_ext_armv8
20257 #undef  THUMB_VARIANT
20258 #define THUMB_VARIANT & crc_ext_armv8
20259   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
20260   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
20261   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
20262   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
20263   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
20264   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
20265
20266  /* ARMv8.2 RAS extension.  */
20267 #undef  ARM_VARIANT
20268 #define ARM_VARIANT   & arm_ext_ras
20269 #undef  THUMB_VARIANT
20270 #define THUMB_VARIANT & arm_ext_ras
20271  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
20272
20273 #undef  ARM_VARIANT
20274 #define ARM_VARIANT   & arm_ext_v8_3
20275 #undef  THUMB_VARIANT
20276 #define THUMB_VARIANT & arm_ext_v8_3
20277  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
20278  NUF (vcmla, 0, 4, (RNDQ, RNDQ, RNDQ_RNSC, EXPi), vcmla),
20279  NUF (vcadd, 0, 4, (RNDQ, RNDQ, RNDQ, EXPi), vcadd),
20280
20281 #undef  ARM_VARIANT
20282 #define ARM_VARIANT   & fpu_neon_ext_dotprod
20283 #undef  THUMB_VARIANT
20284 #define THUMB_VARIANT & fpu_neon_ext_dotprod
20285  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
20286  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
20287
20288 #undef  ARM_VARIANT
20289 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
20290 #undef  THUMB_VARIANT
20291 #define THUMB_VARIANT NULL
20292
20293  cCE("wfs",     e200110, 1, (RR),            rd),
20294  cCE("rfs",     e300110, 1, (RR),            rd),
20295  cCE("wfc",     e400110, 1, (RR),            rd),
20296  cCE("rfc",     e500110, 1, (RR),            rd),
20297
20298  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20299  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20300  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20301  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20302
20303  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20304  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20305  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20306  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20307
20308  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
20309  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
20310  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
20311  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
20312  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
20313  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
20314  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
20315  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
20316  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
20317  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
20318  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
20319  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
20320
20321  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
20322  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
20323  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
20324  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
20325  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
20326  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
20327  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
20328  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
20329  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
20330  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
20331  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
20332  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
20333
20334  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
20335  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
20336  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
20337  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
20338  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
20339  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
20340  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
20341  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
20342  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
20343  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
20344  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
20345  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
20346
20347  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
20348  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
20349  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
20350  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
20351  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
20352  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
20353  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
20354  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
20355  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
20356  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
20357  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
20358  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
20359
20360  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
20361  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
20362  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
20363  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
20364  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
20365  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
20366  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
20367  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
20368  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
20369  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
20370  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
20371  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
20372
20373  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
20374  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
20375  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
20376  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
20377  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
20378  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
20379  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
20380  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
20381  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
20382  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
20383  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
20384  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
20385
20386  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
20387  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
20388  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
20389  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
20390  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
20391  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
20392  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
20393  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
20394  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
20395  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
20396  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
20397  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
20398
20399  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
20400  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
20401  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
20402  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
20403  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
20404  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
20405  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
20406  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
20407  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
20408  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
20409  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
20410  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
20411
20412  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
20413  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
20414  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
20415  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
20416  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
20417  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
20418  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
20419  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
20420  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
20421  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
20422  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
20423  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
20424
20425  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
20426  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
20427  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
20428  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
20429  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
20430  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
20431  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
20432  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
20433  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
20434  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
20435  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
20436  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
20437
20438  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
20439  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
20440  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
20441  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
20442  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
20443  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
20444  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
20445  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
20446  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
20447  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
20448  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
20449  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
20450
20451  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
20452  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
20453  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
20454  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
20455  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
20456  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
20457  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
20458  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
20459  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
20460  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
20461  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
20462  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
20463
20464  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
20465  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
20466  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
20467  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
20468  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
20469  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
20470  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
20471  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
20472  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
20473  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
20474  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
20475  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
20476
20477  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
20478  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
20479  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
20480  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
20481  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
20482  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
20483  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
20484  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
20485  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
20486  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
20487  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
20488  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
20489
20490  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
20491  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
20492  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
20493  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
20494  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
20495  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
20496  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
20497  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
20498  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
20499  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
20500  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
20501  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
20502
20503  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
20504  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
20505  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
20506  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
20507  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
20508  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
20509  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
20510  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
20511  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
20512  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
20513  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
20514  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
20515
20516  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
20517  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
20518  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
20519  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
20520  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
20521  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20522  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20523  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20524  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
20525  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
20526  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
20527  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
20528
20529  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
20530  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
20531  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
20532  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
20533  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
20534  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20535  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20536  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20537  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
20538  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
20539  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
20540  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
20541
20542  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
20543  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
20544  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
20545  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
20546  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
20547  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20548  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20549  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20550  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
20551  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
20552  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
20553  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
20554
20555  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
20556  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
20557  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
20558  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
20559  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
20560  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20561  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20562  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20563  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
20564  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
20565  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
20566  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
20567
20568  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
20569  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
20570  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
20571  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
20572  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
20573  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20574  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20575  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20576  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
20577  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
20578  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
20579  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
20580
20581  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
20582  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
20583  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
20584  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
20585  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
20586  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20587  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20588  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20589  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
20590  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
20591  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
20592  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
20593
20594  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
20595  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
20596  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
20597  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
20598  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
20599  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20600  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20601  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20602  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
20603  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
20604  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
20605  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
20606
20607  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
20608  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
20609  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
20610  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
20611  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
20612  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20613  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20614  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20615  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
20616  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
20617  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
20618  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
20619
20620  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
20621  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
20622  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
20623  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
20624  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
20625  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20626  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20627  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20628  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
20629  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
20630  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
20631  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
20632
20633  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
20634  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
20635  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
20636  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
20637  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
20638  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20639  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20640  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20641  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
20642  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
20643  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
20644  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
20645
20646  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20647  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20648  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20649  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20650  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20651  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20652  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20653  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20654  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20655  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20656  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20657  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20658
20659  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20660  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20661  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20662  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20663  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20664  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20665  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20666  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20667  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20668  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20669  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20670  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20671
20672  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20673  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20674  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20675  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20676  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20677  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20678  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20679  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20680  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20681  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20682  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20683  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20684
20685  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
20686  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
20687  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
20688  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
20689
20690  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
20691  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
20692  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
20693  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
20694  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
20695  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
20696  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
20697  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
20698  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
20699  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
20700  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
20701  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
20702
20703   /* The implementation of the FIX instruction is broken on some
20704      assemblers, in that it accepts a precision specifier as well as a
20705      rounding specifier, despite the fact that this is meaningless.
20706      To be more compatible, we accept it as well, though of course it
20707      does not set any bits.  */
20708  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
20709  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
20710  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
20711  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
20712  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
20713  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
20714  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
20715  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
20716  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
20717  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
20718  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
20719  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
20720  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
20721
20722   /* Instructions that were new with the real FPA, call them V2.  */
20723 #undef  ARM_VARIANT
20724 #define ARM_VARIANT  & fpu_fpa_ext_v2
20725
20726  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20727  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20728  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20729  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20730  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20731  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20732
20733 #undef  ARM_VARIANT
20734 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
20735
20736   /* Moves and type conversions.  */
20737  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
20738  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
20739  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
20740  cCE("fmstat",  ef1fa10, 0, (),               noargs),
20741  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
20742  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
20743  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20744  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
20745  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20746  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20747  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20748  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20749  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
20750  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
20751
20752   /* Memory operations.  */
20753  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20754  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20755  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20756  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20757  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20758  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20759  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20760  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20761  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20762  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20763  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20764  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20765  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20766  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20767  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20768  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20769  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20770  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20771
20772   /* Monadic operations.  */
20773  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20774  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
20775  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20776
20777   /* Dyadic operations.  */
20778  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20779  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20780  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20781  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20782  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20783  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20784  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20785  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20786  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20787
20788   /* Comparisons.  */
20789  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
20790  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
20791  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20792  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
20793
20794  /* Double precision load/store are still present on single precision
20795     implementations.  */
20796  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20797  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20798  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20799  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20800  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20801  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20802  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20803  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20804  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20805  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20806
20807 #undef  ARM_VARIANT
20808 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
20809
20810   /* Moves and type conversions.  */
20811  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20812  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20813  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20814  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20815  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20816  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20817  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20818  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20819  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20820  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20821  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20822  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20823  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20824
20825   /* Monadic operations.  */
20826  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20827  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20828  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20829
20830   /* Dyadic operations.  */
20831  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20832  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20833  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20834  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20835  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20836  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20837  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20838  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20839  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20840
20841   /* Comparisons.  */
20842  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20843  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
20844  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20845  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
20846
20847 #undef  ARM_VARIANT
20848 #define ARM_VARIANT  & fpu_vfp_ext_v2
20849
20850  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
20851  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
20852  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
20853  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
20854
20855 /* Instructions which may belong to either the Neon or VFP instruction sets.
20856    Individual encoder functions perform additional architecture checks.  */
20857 #undef  ARM_VARIANT
20858 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
20859 #undef  THUMB_VARIANT
20860 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
20861
20862   /* These mnemonics are unique to VFP.  */
20863  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
20864  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
20865  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20866  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20867  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20868  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20869  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20870  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
20871  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
20872  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
20873
20874   /* Mnemonics shared by Neon and VFP.  */
20875  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
20876  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20877  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20878
20879  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20880  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20881
20882  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20883  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20884
20885  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20886  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20887  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20888  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20889  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20890  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20891  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20892  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20893
20894  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
20895  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
20896  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
20897  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
20898
20899
20900   /* NOTE: All VMOV encoding is special-cased!  */
20901  NCE(vmov,      0,       1, (VMOV), neon_mov),
20902  NCE(vmovq,     0,       1, (VMOV), neon_mov),
20903
20904 #undef  ARM_VARIANT
20905 #define ARM_VARIANT    & arm_ext_fp16
20906 #undef  THUMB_VARIANT
20907 #define THUMB_VARIANT  & arm_ext_fp16
20908  /* New instructions added from v8.2, allowing the extraction and insertion of
20909     the upper 16 bits of a 32-bit vector register.  */
20910  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
20911  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
20912
20913  /* New backported fma/fms instructions optional in v8.2.  */
20914  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
20915  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
20916
20917 #undef  THUMB_VARIANT
20918 #define THUMB_VARIANT  & fpu_neon_ext_v1
20919 #undef  ARM_VARIANT
20920 #define ARM_VARIANT    & fpu_neon_ext_v1
20921
20922   /* Data processing with three registers of the same length.  */
20923   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
20924  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
20925  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
20926  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20927  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20928  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20929  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20930  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20931  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20932   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
20933  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20934  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20935  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20936  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20937  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20938  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20939  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20940  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20941   /* If not immediate, fall back to neon_dyadic_i64_su.
20942      shl_imm should accept I8 I16 I32 I64,
20943      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
20944  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
20945  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
20946  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
20947  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
20948   /* Logic ops, types optional & ignored.  */
20949  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20950  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20951  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20952  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20953  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20954  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20955  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20956  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20957  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
20958  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
20959   /* Bitfield ops, untyped.  */
20960  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20961  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20962  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20963  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20964  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20965  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20966   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
20967  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20968  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20969  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20970  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20971  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20972  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20973   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
20974      back to neon_dyadic_if_su.  */
20975  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20976  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20977  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20978  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20979  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20980  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20981  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20982  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20983   /* Comparison. Type I8 I16 I32 F32.  */
20984  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
20985  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
20986   /* As above, D registers only.  */
20987  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20988  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20989   /* Int and float variants, signedness unimportant.  */
20990  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20991  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20992  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
20993   /* Add/sub take types I8 I16 I32 I64 F32.  */
20994  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20995  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20996   /* vtst takes sizes 8, 16, 32.  */
20997  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
20998  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
20999   /* VMUL takes I8 I16 I32 F32 P8.  */
21000  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
21001   /* VQD{R}MULH takes S16 S32.  */
21002  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
21003  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
21004  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
21005  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
21006  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
21007  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
21008  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
21009  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
21010  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
21011  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
21012  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
21013  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
21014  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
21015  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
21016  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
21017  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
21018  /* ARM v8.1 extension.  */
21019  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
21020  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
21021  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
21022  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
21023
21024   /* Two address, int/float. Types S8 S16 S32 F32.  */
21025  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
21026  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
21027
21028   /* Data processing with two registers and a shift amount.  */
21029   /* Right shifts, and variants with rounding.
21030      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
21031  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
21032  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
21033  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
21034  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
21035  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
21036  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
21037  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
21038  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
21039   /* Shift and insert. Sizes accepted 8 16 32 64.  */
21040  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
21041  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
21042  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
21043  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
21044   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
21045  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
21046  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
21047   /* Right shift immediate, saturating & narrowing, with rounding variants.
21048      Types accepted S16 S32 S64 U16 U32 U64.  */
21049  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
21050  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
21051   /* As above, unsigned. Types accepted S16 S32 S64.  */
21052  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
21053  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
21054   /* Right shift narrowing. Types accepted I16 I32 I64.  */
21055  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
21056  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
21057   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
21058  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
21059   /* CVT with optional immediate for fixed-point variant.  */
21060  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
21061
21062  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
21063  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
21064
21065   /* Data processing, three registers of different lengths.  */
21066   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
21067  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
21068  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
21069  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
21070  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
21071   /* If not scalar, fall back to neon_dyadic_long.
21072      Vector types as above, scalar types S16 S32 U16 U32.  */
21073  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
21074  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
21075   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
21076  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
21077  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
21078   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
21079  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
21080  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
21081  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
21082  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
21083   /* Saturating doubling multiplies. Types S16 S32.  */
21084  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21085  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21086  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21087   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
21088      S16 S32 U16 U32.  */
21089  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
21090
21091   /* Extract. Size 8.  */
21092  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
21093  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
21094
21095   /* Two registers, miscellaneous.  */
21096   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
21097  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
21098  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
21099  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
21100  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
21101  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
21102  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
21103   /* Vector replicate. Sizes 8 16 32.  */
21104  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
21105  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
21106   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
21107  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
21108   /* VMOVN. Types I16 I32 I64.  */
21109  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
21110   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
21111  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
21112   /* VQMOVUN. Types S16 S32 S64.  */
21113  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
21114   /* VZIP / VUZP. Sizes 8 16 32.  */
21115  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
21116  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
21117  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
21118  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
21119   /* VQABS / VQNEG. Types S8 S16 S32.  */
21120  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
21121  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
21122  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
21123  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
21124   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
21125  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
21126  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
21127  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
21128  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
21129   /* Reciprocal estimates.  Types U32 F16 F32.  */
21130  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
21131  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
21132  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
21133  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
21134   /* VCLS. Types S8 S16 S32.  */
21135  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
21136  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
21137   /* VCLZ. Types I8 I16 I32.  */
21138  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
21139  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
21140   /* VCNT. Size 8.  */
21141  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
21142  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
21143   /* Two address, untyped.  */
21144  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
21145  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
21146   /* VTRN. Sizes 8 16 32.  */
21147  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
21148  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
21149
21150   /* Table lookup. Size 8.  */
21151  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
21152  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
21153
21154 #undef  THUMB_VARIANT
21155 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
21156 #undef  ARM_VARIANT
21157 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
21158
21159   /* Neon element/structure load/store.  */
21160  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21161  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21162  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21163  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21164  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21165  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21166  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21167  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21168
21169 #undef  THUMB_VARIANT
21170 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
21171 #undef  ARM_VARIANT
21172 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
21173  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
21174  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21175  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21176  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21177  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21178  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21179  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21180  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21181  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21182
21183 #undef  THUMB_VARIANT
21184 #define THUMB_VARIANT  & fpu_vfp_ext_v3
21185 #undef  ARM_VARIANT
21186 #define ARM_VARIANT    & fpu_vfp_ext_v3
21187
21188  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
21189  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21190  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21191  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21192  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21193  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21194  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21195  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21196  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21197
21198 #undef  ARM_VARIANT
21199 #define ARM_VARIANT    & fpu_vfp_ext_fma
21200 #undef  THUMB_VARIANT
21201 #define THUMB_VARIANT  & fpu_vfp_ext_fma
21202  /* Mnemonics shared by Neon and VFP.  These are included in the
21203     VFP FMA variant; NEON and VFP FMA always includes the NEON
21204     FMA instructions.  */
21205  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
21206  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
21207  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
21208     the v form should always be used.  */
21209  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21210  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21211  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21212  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21213  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21214  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21215
21216 #undef THUMB_VARIANT
21217 #undef  ARM_VARIANT
21218 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
21219
21220  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21221  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21222  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21223  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21224  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21225  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21226  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
21227  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
21228
21229 #undef  ARM_VARIANT
21230 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
21231
21232  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
21233  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
21234  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
21235  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
21236  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
21237  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
21238  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
21239  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
21240  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
21241  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21242  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21243  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21244  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21245  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21246  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21247  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21248  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21249  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21250  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
21251  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
21252  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21253  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21254  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21255  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21256  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21257  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21258  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
21259  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
21260  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
21261  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
21262  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
21263  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
21264  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
21265  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
21266  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
21267  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
21268  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
21269  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21270  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21271  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21272  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21273  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21274  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21275  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21276  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21277  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21278  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
21279  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21280  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21281  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21282  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21283  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21284  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21285  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21286  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21287  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21288  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21289  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21290  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21291  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21292  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21293  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21294  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21295  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21296  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21297  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21298  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21299  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21300  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
21301  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
21302  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21303  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21304  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21305  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21306  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21307  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21308  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21309  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21310  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21311  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21312  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21313  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21314  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21315  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21316  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21317  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21318  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21319  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21320  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
21321  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21322  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21323  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21324  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21325  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21326  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21327  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21328  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21329  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21330  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21331  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21332  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21333  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21334  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21335  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21336  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21337  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21338  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21339  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21340  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21341  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21342  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
21343  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21344  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21345  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21346  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21347  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21348  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21349  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21350  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21351  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21352  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21353  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21354  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21355  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21356  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21357  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21358  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21359  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21360  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21361  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21362  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21363  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
21364  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
21365  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21366  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21367  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21368  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21369  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21370  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21371  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21372  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21373  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21374  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
21375  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
21376  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
21377  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
21378  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
21379  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
21380  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21381  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21382  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21383  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
21384  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
21385  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
21386  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
21387  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
21388  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
21389  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21390  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21391  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21392  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21393  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
21394
21395 #undef  ARM_VARIANT
21396 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
21397
21398  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
21399  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
21400  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
21401  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
21402  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
21403  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
21404  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21405  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21406  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21407  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21408  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21409  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21410  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21411  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21412  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21413  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21414  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21415  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21416  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21417  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21418  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
21419  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21420  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21421  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21422  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21423  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21424  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21425  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21426  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21427  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21428  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21429  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21430  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21431  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21432  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21433  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21434  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21435  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21436  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21437  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21438  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21439  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21440  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21441  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21442  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21443  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21444  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21445  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21446  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21447  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21448  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21449  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21450  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21451  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21452  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21453  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21454  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21455
21456 #undef  ARM_VARIANT
21457 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
21458
21459  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21460  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21461  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21462  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21463  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21464  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21465  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21466  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21467  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
21468  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
21469  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
21470  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
21471  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
21472  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
21473  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
21474  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
21475  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
21476  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
21477  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
21478  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
21479  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
21480  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
21481  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
21482  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
21483  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
21484  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
21485  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
21486  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
21487  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
21488  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
21489  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
21490  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
21491  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
21492  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
21493  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
21494  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
21495  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
21496  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
21497  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
21498  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
21499  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
21500  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
21501  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
21502  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
21503  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
21504  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
21505  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
21506  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
21507  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
21508  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
21509  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
21510  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
21511  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
21512  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
21513  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
21514  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21515  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
21516  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21517  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
21518  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
21519  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
21520  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
21521  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
21522  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
21523  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21524  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21525  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21526  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21527  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21528  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21529  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21530  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21531  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21532  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21533  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21534  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21535
21536  /* ARMv8.5-A instructions.  */
21537 #undef  ARM_VARIANT
21538 #define ARM_VARIANT   & arm_ext_sb
21539 #undef  THUMB_VARIANT
21540 #define THUMB_VARIANT & arm_ext_sb
21541  TUF("sb", 57ff070, f3bf8f70, 0, (), noargs, noargs),
21542
21543 #undef  ARM_VARIANT
21544 #define ARM_VARIANT   & arm_ext_predres
21545 #undef  THUMB_VARIANT
21546 #define THUMB_VARIANT & arm_ext_predres
21547  CE("cfprctx", e070f93, 1, (RRnpc), rd),
21548  CE("dvprctx", e070fb3, 1, (RRnpc), rd),
21549  CE("cpprctx", e070ff3, 1, (RRnpc), rd),
21550
21551  /* ARMv8-M instructions.  */
21552 #undef  ARM_VARIANT
21553 #define ARM_VARIANT NULL
21554 #undef  THUMB_VARIANT
21555 #define THUMB_VARIANT & arm_ext_v8m
21556  ToU("sg",    e97fe97f, 0, (),             noargs),
21557  ToC("blxns", 4784,     1, (RRnpc),        t_blx),
21558  ToC("bxns",  4704,     1, (RRnpc),        t_bx),
21559  ToC("tt",    e840f000, 2, (RRnpc, RRnpc), tt),
21560  ToC("ttt",   e840f040, 2, (RRnpc, RRnpc), tt),
21561  ToC("tta",   e840f080, 2, (RRnpc, RRnpc), tt),
21562  ToC("ttat",  e840f0c0, 2, (RRnpc, RRnpc), tt),
21563
21564  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
21565     instructions behave as nop if no VFP is present.  */
21566 #undef  THUMB_VARIANT
21567 #define THUMB_VARIANT & arm_ext_v8m_main
21568  ToC("vlldm", ec300a00, 1, (RRnpc), rn),
21569  ToC("vlstm", ec200a00, 1, (RRnpc), rn),
21570 };
21571 #undef ARM_VARIANT
21572 #undef THUMB_VARIANT
21573 #undef TCE
21574 #undef TUE
21575 #undef TUF
21576 #undef TCC
21577 #undef cCE
21578 #undef cCL
21579 #undef C3E
21580 #undef CE
21581 #undef CM
21582 #undef UE
21583 #undef UF
21584 #undef UT
21585 #undef NUF
21586 #undef nUF
21587 #undef NCE
21588 #undef nCE
21589 #undef OPS0
21590 #undef OPS1
21591 #undef OPS2
21592 #undef OPS3
21593 #undef OPS4
21594 #undef OPS5
21595 #undef OPS6
21596 #undef do_0
21597 \f
21598 /* MD interface: bits in the object file.  */
21599
21600 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
21601    for use in the a.out file, and stores them in the array pointed to by buf.
21602    This knows about the endian-ness of the target machine and does
21603    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
21604    2 (short) and 4 (long)  Floating numbers are put out as a series of
21605    LITTLENUMS (shorts, here at least).  */
21606
21607 void
21608 md_number_to_chars (char * buf, valueT val, int n)
21609 {
21610   if (target_big_endian)
21611     number_to_chars_bigendian (buf, val, n);
21612   else
21613     number_to_chars_littleendian (buf, val, n);
21614 }
21615
21616 static valueT
21617 md_chars_to_number (char * buf, int n)
21618 {
21619   valueT result = 0;
21620   unsigned char * where = (unsigned char *) buf;
21621
21622   if (target_big_endian)
21623     {
21624       while (n--)
21625         {
21626           result <<= 8;
21627           result |= (*where++ & 255);
21628         }
21629     }
21630   else
21631     {
21632       while (n--)
21633         {
21634           result <<= 8;
21635           result |= (where[n] & 255);
21636         }
21637     }
21638
21639   return result;
21640 }
21641
21642 /* MD interface: Sections.  */
21643
21644 /* Calculate the maximum variable size (i.e., excluding fr_fix)
21645    that an rs_machine_dependent frag may reach.  */
21646
21647 unsigned int
21648 arm_frag_max_var (fragS *fragp)
21649 {
21650   /* We only use rs_machine_dependent for variable-size Thumb instructions,
21651      which are either THUMB_SIZE (2) or INSN_SIZE (4).
21652
21653      Note that we generate relaxable instructions even for cases that don't
21654      really need it, like an immediate that's a trivial constant.  So we're
21655      overestimating the instruction size for some of those cases.  Rather
21656      than putting more intelligence here, it would probably be better to
21657      avoid generating a relaxation frag in the first place when it can be
21658      determined up front that a short instruction will suffice.  */
21659
21660   gas_assert (fragp->fr_type == rs_machine_dependent);
21661   return INSN_SIZE;
21662 }
21663
21664 /* Estimate the size of a frag before relaxing.  Assume everything fits in
21665    2 bytes.  */
21666
21667 int
21668 md_estimate_size_before_relax (fragS * fragp,
21669                                segT    segtype ATTRIBUTE_UNUSED)
21670 {
21671   fragp->fr_var = 2;
21672   return 2;
21673 }
21674
21675 /* Convert a machine dependent frag.  */
21676
21677 void
21678 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
21679 {
21680   unsigned long insn;
21681   unsigned long old_op;
21682   char *buf;
21683   expressionS exp;
21684   fixS *fixp;
21685   int reloc_type;
21686   int pc_rel;
21687   int opcode;
21688
21689   buf = fragp->fr_literal + fragp->fr_fix;
21690
21691   old_op = bfd_get_16(abfd, buf);
21692   if (fragp->fr_symbol)
21693     {
21694       exp.X_op = O_symbol;
21695       exp.X_add_symbol = fragp->fr_symbol;
21696     }
21697   else
21698     {
21699       exp.X_op = O_constant;
21700     }
21701   exp.X_add_number = fragp->fr_offset;
21702   opcode = fragp->fr_subtype;
21703   switch (opcode)
21704     {
21705     case T_MNEM_ldr_pc:
21706     case T_MNEM_ldr_pc2:
21707     case T_MNEM_ldr_sp:
21708     case T_MNEM_str_sp:
21709     case T_MNEM_ldr:
21710     case T_MNEM_ldrb:
21711     case T_MNEM_ldrh:
21712     case T_MNEM_str:
21713     case T_MNEM_strb:
21714     case T_MNEM_strh:
21715       if (fragp->fr_var == 4)
21716         {
21717           insn = THUMB_OP32 (opcode);
21718           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
21719             {
21720               insn |= (old_op & 0x700) << 4;
21721             }
21722           else
21723             {
21724               insn |= (old_op & 7) << 12;
21725               insn |= (old_op & 0x38) << 13;
21726             }
21727           insn |= 0x00000c00;
21728           put_thumb32_insn (buf, insn);
21729           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
21730         }
21731       else
21732         {
21733           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
21734         }
21735       pc_rel = (opcode == T_MNEM_ldr_pc2);
21736       break;
21737     case T_MNEM_adr:
21738       if (fragp->fr_var == 4)
21739         {
21740           insn = THUMB_OP32 (opcode);
21741           insn |= (old_op & 0xf0) << 4;
21742           put_thumb32_insn (buf, insn);
21743           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
21744         }
21745       else
21746         {
21747           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21748           exp.X_add_number -= 4;
21749         }
21750       pc_rel = 1;
21751       break;
21752     case T_MNEM_mov:
21753     case T_MNEM_movs:
21754     case T_MNEM_cmp:
21755     case T_MNEM_cmn:
21756       if (fragp->fr_var == 4)
21757         {
21758           int r0off = (opcode == T_MNEM_mov
21759                        || opcode == T_MNEM_movs) ? 0 : 8;
21760           insn = THUMB_OP32 (opcode);
21761           insn = (insn & 0xe1ffffff) | 0x10000000;
21762           insn |= (old_op & 0x700) << r0off;
21763           put_thumb32_insn (buf, insn);
21764           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21765         }
21766       else
21767         {
21768           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
21769         }
21770       pc_rel = 0;
21771       break;
21772     case T_MNEM_b:
21773       if (fragp->fr_var == 4)
21774         {
21775           insn = THUMB_OP32(opcode);
21776           put_thumb32_insn (buf, insn);
21777           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
21778         }
21779       else
21780         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
21781       pc_rel = 1;
21782       break;
21783     case T_MNEM_bcond:
21784       if (fragp->fr_var == 4)
21785         {
21786           insn = THUMB_OP32(opcode);
21787           insn |= (old_op & 0xf00) << 14;
21788           put_thumb32_insn (buf, insn);
21789           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
21790         }
21791       else
21792         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
21793       pc_rel = 1;
21794       break;
21795     case T_MNEM_add_sp:
21796     case T_MNEM_add_pc:
21797     case T_MNEM_inc_sp:
21798     case T_MNEM_dec_sp:
21799       if (fragp->fr_var == 4)
21800         {
21801           /* ??? Choose between add and addw.  */
21802           insn = THUMB_OP32 (opcode);
21803           insn |= (old_op & 0xf0) << 4;
21804           put_thumb32_insn (buf, insn);
21805           if (opcode == T_MNEM_add_pc)
21806             reloc_type = BFD_RELOC_ARM_T32_IMM12;
21807           else
21808             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21809         }
21810       else
21811         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21812       pc_rel = 0;
21813       break;
21814
21815     case T_MNEM_addi:
21816     case T_MNEM_addis:
21817     case T_MNEM_subi:
21818     case T_MNEM_subis:
21819       if (fragp->fr_var == 4)
21820         {
21821           insn = THUMB_OP32 (opcode);
21822           insn |= (old_op & 0xf0) << 4;
21823           insn |= (old_op & 0xf) << 16;
21824           put_thumb32_insn (buf, insn);
21825           if (insn & (1 << 20))
21826             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21827           else
21828             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21829         }
21830       else
21831         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21832       pc_rel = 0;
21833       break;
21834     default:
21835       abort ();
21836     }
21837   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
21838                       (enum bfd_reloc_code_real) reloc_type);
21839   fixp->fx_file = fragp->fr_file;
21840   fixp->fx_line = fragp->fr_line;
21841   fragp->fr_fix += fragp->fr_var;
21842
21843   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
21844   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
21845       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
21846     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
21847 }
21848
21849 /* Return the size of a relaxable immediate operand instruction.
21850    SHIFT and SIZE specify the form of the allowable immediate.  */
21851 static int
21852 relax_immediate (fragS *fragp, int size, int shift)
21853 {
21854   offsetT offset;
21855   offsetT mask;
21856   offsetT low;
21857
21858   /* ??? Should be able to do better than this.  */
21859   if (fragp->fr_symbol)
21860     return 4;
21861
21862   low = (1 << shift) - 1;
21863   mask = (1 << (shift + size)) - (1 << shift);
21864   offset = fragp->fr_offset;
21865   /* Force misaligned offsets to 32-bit variant.  */
21866   if (offset & low)
21867     return 4;
21868   if (offset & ~mask)
21869     return 4;
21870   return 2;
21871 }
21872
21873 /* Get the address of a symbol during relaxation.  */
21874 static addressT
21875 relaxed_symbol_addr (fragS *fragp, long stretch)
21876 {
21877   fragS *sym_frag;
21878   addressT addr;
21879   symbolS *sym;
21880
21881   sym = fragp->fr_symbol;
21882   sym_frag = symbol_get_frag (sym);
21883   know (S_GET_SEGMENT (sym) != absolute_section
21884         || sym_frag == &zero_address_frag);
21885   addr = S_GET_VALUE (sym) + fragp->fr_offset;
21886
21887   /* If frag has yet to be reached on this pass, assume it will
21888      move by STRETCH just as we did.  If this is not so, it will
21889      be because some frag between grows, and that will force
21890      another pass.  */
21891
21892   if (stretch != 0
21893       && sym_frag->relax_marker != fragp->relax_marker)
21894     {
21895       fragS *f;
21896
21897       /* Adjust stretch for any alignment frag.  Note that if have
21898          been expanding the earlier code, the symbol may be
21899          defined in what appears to be an earlier frag.  FIXME:
21900          This doesn't handle the fr_subtype field, which specifies
21901          a maximum number of bytes to skip when doing an
21902          alignment.  */
21903       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
21904         {
21905           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
21906             {
21907               if (stretch < 0)
21908                 stretch = - ((- stretch)
21909                              & ~ ((1 << (int) f->fr_offset) - 1));
21910               else
21911                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
21912               if (stretch == 0)
21913                 break;
21914             }
21915         }
21916       if (f != NULL)
21917         addr += stretch;
21918     }
21919
21920   return addr;
21921 }
21922
21923 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
21924    load.  */
21925 static int
21926 relax_adr (fragS *fragp, asection *sec, long stretch)
21927 {
21928   addressT addr;
21929   offsetT val;
21930
21931   /* Assume worst case for symbols not known to be in the same section.  */
21932   if (fragp->fr_symbol == NULL
21933       || !S_IS_DEFINED (fragp->fr_symbol)
21934       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21935       || S_IS_WEAK (fragp->fr_symbol))
21936     return 4;
21937
21938   val = relaxed_symbol_addr (fragp, stretch);
21939   addr = fragp->fr_address + fragp->fr_fix;
21940   addr = (addr + 4) & ~3;
21941   /* Force misaligned targets to 32-bit variant.  */
21942   if (val & 3)
21943     return 4;
21944   val -= addr;
21945   if (val < 0 || val > 1020)
21946     return 4;
21947   return 2;
21948 }
21949
21950 /* Return the size of a relaxable add/sub immediate instruction.  */
21951 static int
21952 relax_addsub (fragS *fragp, asection *sec)
21953 {
21954   char *buf;
21955   int op;
21956
21957   buf = fragp->fr_literal + fragp->fr_fix;
21958   op = bfd_get_16(sec->owner, buf);
21959   if ((op & 0xf) == ((op >> 4) & 0xf))
21960     return relax_immediate (fragp, 8, 0);
21961   else
21962     return relax_immediate (fragp, 3, 0);
21963 }
21964
21965 /* Return TRUE iff the definition of symbol S could be pre-empted
21966    (overridden) at link or load time.  */
21967 static bfd_boolean
21968 symbol_preemptible (symbolS *s)
21969 {
21970   /* Weak symbols can always be pre-empted.  */
21971   if (S_IS_WEAK (s))
21972     return TRUE;
21973
21974   /* Non-global symbols cannot be pre-empted. */
21975   if (! S_IS_EXTERNAL (s))
21976     return FALSE;
21977
21978 #ifdef OBJ_ELF
21979   /* In ELF, a global symbol can be marked protected, or private.  In that
21980      case it can't be pre-empted (other definitions in the same link unit
21981      would violate the ODR).  */
21982   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
21983     return FALSE;
21984 #endif
21985
21986   /* Other global symbols might be pre-empted.  */
21987   return TRUE;
21988 }
21989
21990 /* Return the size of a relaxable branch instruction.  BITS is the
21991    size of the offset field in the narrow instruction.  */
21992
21993 static int
21994 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
21995 {
21996   addressT addr;
21997   offsetT val;
21998   offsetT limit;
21999
22000   /* Assume worst case for symbols not known to be in the same section.  */
22001   if (!S_IS_DEFINED (fragp->fr_symbol)
22002       || sec != S_GET_SEGMENT (fragp->fr_symbol)
22003       || S_IS_WEAK (fragp->fr_symbol))
22004     return 4;
22005
22006 #ifdef OBJ_ELF
22007   /* A branch to a function in ARM state will require interworking.  */
22008   if (S_IS_DEFINED (fragp->fr_symbol)
22009       && ARM_IS_FUNC (fragp->fr_symbol))
22010       return 4;
22011 #endif
22012
22013   if (symbol_preemptible (fragp->fr_symbol))
22014     return 4;
22015
22016   val = relaxed_symbol_addr (fragp, stretch);
22017   addr = fragp->fr_address + fragp->fr_fix + 4;
22018   val -= addr;
22019
22020   /* Offset is a signed value *2 */
22021   limit = 1 << bits;
22022   if (val >= limit || val < -limit)
22023     return 4;
22024   return 2;
22025 }
22026
22027
22028 /* Relax a machine dependent frag.  This returns the amount by which
22029    the current size of the frag should change.  */
22030
22031 int
22032 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
22033 {
22034   int oldsize;
22035   int newsize;
22036
22037   oldsize = fragp->fr_var;
22038   switch (fragp->fr_subtype)
22039     {
22040     case T_MNEM_ldr_pc2:
22041       newsize = relax_adr (fragp, sec, stretch);
22042       break;
22043     case T_MNEM_ldr_pc:
22044     case T_MNEM_ldr_sp:
22045     case T_MNEM_str_sp:
22046       newsize = relax_immediate (fragp, 8, 2);
22047       break;
22048     case T_MNEM_ldr:
22049     case T_MNEM_str:
22050       newsize = relax_immediate (fragp, 5, 2);
22051       break;
22052     case T_MNEM_ldrh:
22053     case T_MNEM_strh:
22054       newsize = relax_immediate (fragp, 5, 1);
22055       break;
22056     case T_MNEM_ldrb:
22057     case T_MNEM_strb:
22058       newsize = relax_immediate (fragp, 5, 0);
22059       break;
22060     case T_MNEM_adr:
22061       newsize = relax_adr (fragp, sec, stretch);
22062       break;
22063     case T_MNEM_mov:
22064     case T_MNEM_movs:
22065     case T_MNEM_cmp:
22066     case T_MNEM_cmn:
22067       newsize = relax_immediate (fragp, 8, 0);
22068       break;
22069     case T_MNEM_b:
22070       newsize = relax_branch (fragp, sec, 11, stretch);
22071       break;
22072     case T_MNEM_bcond:
22073       newsize = relax_branch (fragp, sec, 8, stretch);
22074       break;
22075     case T_MNEM_add_sp:
22076     case T_MNEM_add_pc:
22077       newsize = relax_immediate (fragp, 8, 2);
22078       break;
22079     case T_MNEM_inc_sp:
22080     case T_MNEM_dec_sp:
22081       newsize = relax_immediate (fragp, 7, 2);
22082       break;
22083     case T_MNEM_addi:
22084     case T_MNEM_addis:
22085     case T_MNEM_subi:
22086     case T_MNEM_subis:
22087       newsize = relax_addsub (fragp, sec);
22088       break;
22089     default:
22090       abort ();
22091     }
22092
22093   fragp->fr_var = newsize;
22094   /* Freeze wide instructions that are at or before the same location as
22095      in the previous pass.  This avoids infinite loops.
22096      Don't freeze them unconditionally because targets may be artificially
22097      misaligned by the expansion of preceding frags.  */
22098   if (stretch <= 0 && newsize > 2)
22099     {
22100       md_convert_frag (sec->owner, sec, fragp);
22101       frag_wane (fragp);
22102     }
22103
22104   return newsize - oldsize;
22105 }
22106
22107 /* Round up a section size to the appropriate boundary.  */
22108
22109 valueT
22110 md_section_align (segT   segment ATTRIBUTE_UNUSED,
22111                   valueT size)
22112 {
22113   return size;
22114 }
22115
22116 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
22117    of an rs_align_code fragment.  */
22118
22119 void
22120 arm_handle_align (fragS * fragP)
22121 {
22122   static unsigned char const arm_noop[2][2][4] =
22123     {
22124       {  /* ARMv1 */
22125         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
22126         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
22127       },
22128       {  /* ARMv6k */
22129         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
22130         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
22131       },
22132     };
22133   static unsigned char const thumb_noop[2][2][2] =
22134     {
22135       {  /* Thumb-1 */
22136         {0xc0, 0x46},  /* LE */
22137         {0x46, 0xc0},  /* BE */
22138       },
22139       {  /* Thumb-2 */
22140         {0x00, 0xbf},  /* LE */
22141         {0xbf, 0x00}   /* BE */
22142       }
22143     };
22144   static unsigned char const wide_thumb_noop[2][4] =
22145     {  /* Wide Thumb-2 */
22146       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
22147       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
22148     };
22149
22150   unsigned bytes, fix, noop_size;
22151   char * p;
22152   const unsigned char * noop;
22153   const unsigned char *narrow_noop = NULL;
22154 #ifdef OBJ_ELF
22155   enum mstate state;
22156 #endif
22157
22158   if (fragP->fr_type != rs_align_code)
22159     return;
22160
22161   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
22162   p = fragP->fr_literal + fragP->fr_fix;
22163   fix = 0;
22164
22165   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
22166     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
22167
22168   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
22169
22170   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
22171     {
22172       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
22173                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
22174         {
22175           narrow_noop = thumb_noop[1][target_big_endian];
22176           noop = wide_thumb_noop[target_big_endian];
22177         }
22178       else
22179         noop = thumb_noop[0][target_big_endian];
22180       noop_size = 2;
22181 #ifdef OBJ_ELF
22182       state = MAP_THUMB;
22183 #endif
22184     }
22185   else
22186     {
22187       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
22188                                            ? selected_cpu : arm_arch_none,
22189                                            arm_ext_v6k) != 0]
22190                      [target_big_endian];
22191       noop_size = 4;
22192 #ifdef OBJ_ELF
22193       state = MAP_ARM;
22194 #endif
22195     }
22196
22197   fragP->fr_var = noop_size;
22198
22199   if (bytes & (noop_size - 1))
22200     {
22201       fix = bytes & (noop_size - 1);
22202 #ifdef OBJ_ELF
22203       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
22204 #endif
22205       memset (p, 0, fix);
22206       p += fix;
22207       bytes -= fix;
22208     }
22209
22210   if (narrow_noop)
22211     {
22212       if (bytes & noop_size)
22213         {
22214           /* Insert a narrow noop.  */
22215           memcpy (p, narrow_noop, noop_size);
22216           p += noop_size;
22217           bytes -= noop_size;
22218           fix += noop_size;
22219         }
22220
22221       /* Use wide noops for the remainder */
22222       noop_size = 4;
22223     }
22224
22225   while (bytes >= noop_size)
22226     {
22227       memcpy (p, noop, noop_size);
22228       p += noop_size;
22229       bytes -= noop_size;
22230       fix += noop_size;
22231     }
22232
22233   fragP->fr_fix += fix;
22234 }
22235
22236 /* Called from md_do_align.  Used to create an alignment
22237    frag in a code section.  */
22238
22239 void
22240 arm_frag_align_code (int n, int max)
22241 {
22242   char * p;
22243
22244   /* We assume that there will never be a requirement
22245      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
22246   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
22247     {
22248       char err_msg[128];
22249
22250       sprintf (err_msg,
22251         _("alignments greater than %d bytes not supported in .text sections."),
22252         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
22253       as_fatal ("%s", err_msg);
22254     }
22255
22256   p = frag_var (rs_align_code,
22257                 MAX_MEM_FOR_RS_ALIGN_CODE,
22258                 1,
22259                 (relax_substateT) max,
22260                 (symbolS *) NULL,
22261                 (offsetT) n,
22262                 (char *) NULL);
22263   *p = 0;
22264 }
22265
22266 /* Perform target specific initialisation of a frag.
22267    Note - despite the name this initialisation is not done when the frag
22268    is created, but only when its type is assigned.  A frag can be created
22269    and used a long time before its type is set, so beware of assuming that
22270    this initialisation is performed first.  */
22271
22272 #ifndef OBJ_ELF
22273 void
22274 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
22275 {
22276   /* Record whether this frag is in an ARM or a THUMB area.  */
22277   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
22278 }
22279
22280 #else /* OBJ_ELF is defined.  */
22281 void
22282 arm_init_frag (fragS * fragP, int max_chars)
22283 {
22284   bfd_boolean frag_thumb_mode;
22285
22286   /* If the current ARM vs THUMB mode has not already
22287      been recorded into this frag then do so now.  */
22288   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
22289     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
22290
22291   /* PR 21809: Do not set a mapping state for debug sections
22292      - it just confuses other tools.  */
22293   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
22294     return;
22295
22296   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
22297
22298   /* Record a mapping symbol for alignment frags.  We will delete this
22299      later if the alignment ends up empty.  */
22300   switch (fragP->fr_type)
22301     {
22302     case rs_align:
22303     case rs_align_test:
22304     case rs_fill:
22305       mapping_state_2 (MAP_DATA, max_chars);
22306       break;
22307     case rs_align_code:
22308       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
22309       break;
22310     default:
22311       break;
22312     }
22313 }
22314
22315 /* When we change sections we need to issue a new mapping symbol.  */
22316
22317 void
22318 arm_elf_change_section (void)
22319 {
22320   /* Link an unlinked unwind index table section to the .text section.  */
22321   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
22322       && elf_linked_to_section (now_seg) == NULL)
22323     elf_linked_to_section (now_seg) = text_section;
22324 }
22325
22326 int
22327 arm_elf_section_type (const char * str, size_t len)
22328 {
22329   if (len == 5 && strncmp (str, "exidx", 5) == 0)
22330     return SHT_ARM_EXIDX;
22331
22332   return -1;
22333 }
22334 \f
22335 /* Code to deal with unwinding tables.  */
22336
22337 static void add_unwind_adjustsp (offsetT);
22338
22339 /* Generate any deferred unwind frame offset.  */
22340
22341 static void
22342 flush_pending_unwind (void)
22343 {
22344   offsetT offset;
22345
22346   offset = unwind.pending_offset;
22347   unwind.pending_offset = 0;
22348   if (offset != 0)
22349     add_unwind_adjustsp (offset);
22350 }
22351
22352 /* Add an opcode to this list for this function.  Two-byte opcodes should
22353    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
22354    order.  */
22355
22356 static void
22357 add_unwind_opcode (valueT op, int length)
22358 {
22359   /* Add any deferred stack adjustment.  */
22360   if (unwind.pending_offset)
22361     flush_pending_unwind ();
22362
22363   unwind.sp_restored = 0;
22364
22365   if (unwind.opcode_count + length > unwind.opcode_alloc)
22366     {
22367       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
22368       if (unwind.opcodes)
22369         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
22370                                      unwind.opcode_alloc);
22371       else
22372         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
22373     }
22374   while (length > 0)
22375     {
22376       length--;
22377       unwind.opcodes[unwind.opcode_count] = op & 0xff;
22378       op >>= 8;
22379       unwind.opcode_count++;
22380     }
22381 }
22382
22383 /* Add unwind opcodes to adjust the stack pointer.  */
22384
22385 static void
22386 add_unwind_adjustsp (offsetT offset)
22387 {
22388   valueT op;
22389
22390   if (offset > 0x200)
22391     {
22392       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
22393       char bytes[5];
22394       int n;
22395       valueT o;
22396
22397       /* Long form: 0xb2, uleb128.  */
22398       /* This might not fit in a word so add the individual bytes,
22399          remembering the list is built in reverse order.  */
22400       o = (valueT) ((offset - 0x204) >> 2);
22401       if (o == 0)
22402         add_unwind_opcode (0, 1);
22403
22404       /* Calculate the uleb128 encoding of the offset.  */
22405       n = 0;
22406       while (o)
22407         {
22408           bytes[n] = o & 0x7f;
22409           o >>= 7;
22410           if (o)
22411             bytes[n] |= 0x80;
22412           n++;
22413         }
22414       /* Add the insn.  */
22415       for (; n; n--)
22416         add_unwind_opcode (bytes[n - 1], 1);
22417       add_unwind_opcode (0xb2, 1);
22418     }
22419   else if (offset > 0x100)
22420     {
22421       /* Two short opcodes.  */
22422       add_unwind_opcode (0x3f, 1);
22423       op = (offset - 0x104) >> 2;
22424       add_unwind_opcode (op, 1);
22425     }
22426   else if (offset > 0)
22427     {
22428       /* Short opcode.  */
22429       op = (offset - 4) >> 2;
22430       add_unwind_opcode (op, 1);
22431     }
22432   else if (offset < 0)
22433     {
22434       offset = -offset;
22435       while (offset > 0x100)
22436         {
22437           add_unwind_opcode (0x7f, 1);
22438           offset -= 0x100;
22439         }
22440       op = ((offset - 4) >> 2) | 0x40;
22441       add_unwind_opcode (op, 1);
22442     }
22443 }
22444
22445 /* Finish the list of unwind opcodes for this function.  */
22446
22447 static void
22448 finish_unwind_opcodes (void)
22449 {
22450   valueT op;
22451
22452   if (unwind.fp_used)
22453     {
22454       /* Adjust sp as necessary.  */
22455       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
22456       flush_pending_unwind ();
22457
22458       /* After restoring sp from the frame pointer.  */
22459       op = 0x90 | unwind.fp_reg;
22460       add_unwind_opcode (op, 1);
22461     }
22462   else
22463     flush_pending_unwind ();
22464 }
22465
22466
22467 /* Start an exception table entry.  If idx is nonzero this is an index table
22468    entry.  */
22469
22470 static void
22471 start_unwind_section (const segT text_seg, int idx)
22472 {
22473   const char * text_name;
22474   const char * prefix;
22475   const char * prefix_once;
22476   const char * group_name;
22477   char * sec_name;
22478   int type;
22479   int flags;
22480   int linkonce;
22481
22482   if (idx)
22483     {
22484       prefix = ELF_STRING_ARM_unwind;
22485       prefix_once = ELF_STRING_ARM_unwind_once;
22486       type = SHT_ARM_EXIDX;
22487     }
22488   else
22489     {
22490       prefix = ELF_STRING_ARM_unwind_info;
22491       prefix_once = ELF_STRING_ARM_unwind_info_once;
22492       type = SHT_PROGBITS;
22493     }
22494
22495   text_name = segment_name (text_seg);
22496   if (streq (text_name, ".text"))
22497     text_name = "";
22498
22499   if (strncmp (text_name, ".gnu.linkonce.t.",
22500                strlen (".gnu.linkonce.t.")) == 0)
22501     {
22502       prefix = prefix_once;
22503       text_name += strlen (".gnu.linkonce.t.");
22504     }
22505
22506   sec_name = concat (prefix, text_name, (char *) NULL);
22507
22508   flags = SHF_ALLOC;
22509   linkonce = 0;
22510   group_name = 0;
22511
22512   /* Handle COMDAT group.  */
22513   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
22514     {
22515       group_name = elf_group_name (text_seg);
22516       if (group_name == NULL)
22517         {
22518           as_bad (_("Group section `%s' has no group signature"),
22519                   segment_name (text_seg));
22520           ignore_rest_of_line ();
22521           return;
22522         }
22523       flags |= SHF_GROUP;
22524       linkonce = 1;
22525     }
22526
22527   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
22528                           linkonce, 0);
22529
22530   /* Set the section link for index tables.  */
22531   if (idx)
22532     elf_linked_to_section (now_seg) = text_seg;
22533 }
22534
22535
22536 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
22537    personality routine data.  Returns zero, or the index table value for
22538    an inline entry.  */
22539
22540 static valueT
22541 create_unwind_entry (int have_data)
22542 {
22543   int size;
22544   addressT where;
22545   char *ptr;
22546   /* The current word of data.  */
22547   valueT data;
22548   /* The number of bytes left in this word.  */
22549   int n;
22550
22551   finish_unwind_opcodes ();
22552
22553   /* Remember the current text section.  */
22554   unwind.saved_seg = now_seg;
22555   unwind.saved_subseg = now_subseg;
22556
22557   start_unwind_section (now_seg, 0);
22558
22559   if (unwind.personality_routine == NULL)
22560     {
22561       if (unwind.personality_index == -2)
22562         {
22563           if (have_data)
22564             as_bad (_("handlerdata in cantunwind frame"));
22565           return 1; /* EXIDX_CANTUNWIND.  */
22566         }
22567
22568       /* Use a default personality routine if none is specified.  */
22569       if (unwind.personality_index == -1)
22570         {
22571           if (unwind.opcode_count > 3)
22572             unwind.personality_index = 1;
22573           else
22574             unwind.personality_index = 0;
22575         }
22576
22577       /* Space for the personality routine entry.  */
22578       if (unwind.personality_index == 0)
22579         {
22580           if (unwind.opcode_count > 3)
22581             as_bad (_("too many unwind opcodes for personality routine 0"));
22582
22583           if (!have_data)
22584             {
22585               /* All the data is inline in the index table.  */
22586               data = 0x80;
22587               n = 3;
22588               while (unwind.opcode_count > 0)
22589                 {
22590                   unwind.opcode_count--;
22591                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22592                   n--;
22593                 }
22594
22595               /* Pad with "finish" opcodes.  */
22596               while (n--)
22597                 data = (data << 8) | 0xb0;
22598
22599               return data;
22600             }
22601           size = 0;
22602         }
22603       else
22604         /* We get two opcodes "free" in the first word.  */
22605         size = unwind.opcode_count - 2;
22606     }
22607   else
22608     {
22609       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
22610       if (unwind.personality_index != -1)
22611         {
22612           as_bad (_("attempt to recreate an unwind entry"));
22613           return 1;
22614         }
22615
22616       /* An extra byte is required for the opcode count.        */
22617       size = unwind.opcode_count + 1;
22618     }
22619
22620   size = (size + 3) >> 2;
22621   if (size > 0xff)
22622     as_bad (_("too many unwind opcodes"));
22623
22624   frag_align (2, 0, 0);
22625   record_alignment (now_seg, 2);
22626   unwind.table_entry = expr_build_dot ();
22627
22628   /* Allocate the table entry.  */
22629   ptr = frag_more ((size << 2) + 4);
22630   /* PR 13449: Zero the table entries in case some of them are not used.  */
22631   memset (ptr, 0, (size << 2) + 4);
22632   where = frag_now_fix () - ((size << 2) + 4);
22633
22634   switch (unwind.personality_index)
22635     {
22636     case -1:
22637       /* ??? Should this be a PLT generating relocation?  */
22638       /* Custom personality routine.  */
22639       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
22640                BFD_RELOC_ARM_PREL31);
22641
22642       where += 4;
22643       ptr += 4;
22644
22645       /* Set the first byte to the number of additional words.  */
22646       data = size > 0 ? size - 1 : 0;
22647       n = 3;
22648       break;
22649
22650     /* ABI defined personality routines.  */
22651     case 0:
22652       /* Three opcodes bytes are packed into the first word.  */
22653       data = 0x80;
22654       n = 3;
22655       break;
22656
22657     case 1:
22658     case 2:
22659       /* The size and first two opcode bytes go in the first word.  */
22660       data = ((0x80 + unwind.personality_index) << 8) | size;
22661       n = 2;
22662       break;
22663
22664     default:
22665       /* Should never happen.  */
22666       abort ();
22667     }
22668
22669   /* Pack the opcodes into words (MSB first), reversing the list at the same
22670      time.  */
22671   while (unwind.opcode_count > 0)
22672     {
22673       if (n == 0)
22674         {
22675           md_number_to_chars (ptr, data, 4);
22676           ptr += 4;
22677           n = 4;
22678           data = 0;
22679         }
22680       unwind.opcode_count--;
22681       n--;
22682       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22683     }
22684
22685   /* Finish off the last word.  */
22686   if (n < 4)
22687     {
22688       /* Pad with "finish" opcodes.  */
22689       while (n--)
22690         data = (data << 8) | 0xb0;
22691
22692       md_number_to_chars (ptr, data, 4);
22693     }
22694
22695   if (!have_data)
22696     {
22697       /* Add an empty descriptor if there is no user-specified data.   */
22698       ptr = frag_more (4);
22699       md_number_to_chars (ptr, 0, 4);
22700     }
22701
22702   return 0;
22703 }
22704
22705
22706 /* Initialize the DWARF-2 unwind information for this procedure.  */
22707
22708 void
22709 tc_arm_frame_initial_instructions (void)
22710 {
22711   cfi_add_CFA_def_cfa (REG_SP, 0);
22712 }
22713 #endif /* OBJ_ELF */
22714
22715 /* Convert REGNAME to a DWARF-2 register number.  */
22716
22717 int
22718 tc_arm_regname_to_dw2regnum (char *regname)
22719 {
22720   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
22721   if (reg != FAIL)
22722     return reg;
22723
22724   /* PR 16694: Allow VFP registers as well.  */
22725   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
22726   if (reg != FAIL)
22727     return 64 + reg;
22728
22729   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
22730   if (reg != FAIL)
22731     return reg + 256;
22732
22733   return FAIL;
22734 }
22735
22736 #ifdef TE_PE
22737 void
22738 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
22739 {
22740   expressionS exp;
22741
22742   exp.X_op = O_secrel;
22743   exp.X_add_symbol = symbol;
22744   exp.X_add_number = 0;
22745   emit_expr (&exp, size);
22746 }
22747 #endif
22748
22749 /* MD interface: Symbol and relocation handling.  */
22750
22751 /* Return the address within the segment that a PC-relative fixup is
22752    relative to.  For ARM, PC-relative fixups applied to instructions
22753    are generally relative to the location of the fixup plus 8 bytes.
22754    Thumb branches are offset by 4, and Thumb loads relative to PC
22755    require special handling.  */
22756
22757 long
22758 md_pcrel_from_section (fixS * fixP, segT seg)
22759 {
22760   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
22761
22762   /* If this is pc-relative and we are going to emit a relocation
22763      then we just want to put out any pipeline compensation that the linker
22764      will need.  Otherwise we want to use the calculated base.
22765      For WinCE we skip the bias for externals as well, since this
22766      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
22767   if (fixP->fx_pcrel
22768       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
22769           || (arm_force_relocation (fixP)
22770 #ifdef TE_WINCE
22771               && !S_IS_EXTERNAL (fixP->fx_addsy)
22772 #endif
22773               )))
22774     base = 0;
22775
22776
22777   switch (fixP->fx_r_type)
22778     {
22779       /* PC relative addressing on the Thumb is slightly odd as the
22780          bottom two bits of the PC are forced to zero for the
22781          calculation.  This happens *after* application of the
22782          pipeline offset.  However, Thumb adrl already adjusts for
22783          this, so we need not do it again.  */
22784     case BFD_RELOC_ARM_THUMB_ADD:
22785       return base & ~3;
22786
22787     case BFD_RELOC_ARM_THUMB_OFFSET:
22788     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22789     case BFD_RELOC_ARM_T32_ADD_PC12:
22790     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22791       return (base + 4) & ~3;
22792
22793       /* Thumb branches are simply offset by +4.  */
22794     case BFD_RELOC_THUMB_PCREL_BRANCH7:
22795     case BFD_RELOC_THUMB_PCREL_BRANCH9:
22796     case BFD_RELOC_THUMB_PCREL_BRANCH12:
22797     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22798     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22799       return base + 4;
22800
22801     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22802       if (fixP->fx_addsy
22803           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22804           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22805           && ARM_IS_FUNC (fixP->fx_addsy)
22806           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22807         base = fixP->fx_where + fixP->fx_frag->fr_address;
22808        return base + 4;
22809
22810       /* BLX is like branches above, but forces the low two bits of PC to
22811          zero.  */
22812     case BFD_RELOC_THUMB_PCREL_BLX:
22813       if (fixP->fx_addsy
22814           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22815           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22816           && THUMB_IS_FUNC (fixP->fx_addsy)
22817           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22818         base = fixP->fx_where + fixP->fx_frag->fr_address;
22819       return (base + 4) & ~3;
22820
22821       /* ARM mode branches are offset by +8.  However, the Windows CE
22822          loader expects the relocation not to take this into account.  */
22823     case BFD_RELOC_ARM_PCREL_BLX:
22824       if (fixP->fx_addsy
22825           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22826           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22827           && ARM_IS_FUNC (fixP->fx_addsy)
22828           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22829         base = fixP->fx_where + fixP->fx_frag->fr_address;
22830       return base + 8;
22831
22832     case BFD_RELOC_ARM_PCREL_CALL:
22833       if (fixP->fx_addsy
22834           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22835           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22836           && THUMB_IS_FUNC (fixP->fx_addsy)
22837           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22838         base = fixP->fx_where + fixP->fx_frag->fr_address;
22839       return base + 8;
22840
22841     case BFD_RELOC_ARM_PCREL_BRANCH:
22842     case BFD_RELOC_ARM_PCREL_JUMP:
22843     case BFD_RELOC_ARM_PLT32:
22844 #ifdef TE_WINCE
22845       /* When handling fixups immediately, because we have already
22846          discovered the value of a symbol, or the address of the frag involved
22847          we must account for the offset by +8, as the OS loader will never see the reloc.
22848          see fixup_segment() in write.c
22849          The S_IS_EXTERNAL test handles the case of global symbols.
22850          Those need the calculated base, not just the pipe compensation the linker will need.  */
22851       if (fixP->fx_pcrel
22852           && fixP->fx_addsy != NULL
22853           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22854           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
22855         return base + 8;
22856       return base;
22857 #else
22858       return base + 8;
22859 #endif
22860
22861
22862       /* ARM mode loads relative to PC are also offset by +8.  Unlike
22863          branches, the Windows CE loader *does* expect the relocation
22864          to take this into account.  */
22865     case BFD_RELOC_ARM_OFFSET_IMM:
22866     case BFD_RELOC_ARM_OFFSET_IMM8:
22867     case BFD_RELOC_ARM_HWLITERAL:
22868     case BFD_RELOC_ARM_LITERAL:
22869     case BFD_RELOC_ARM_CP_OFF_IMM:
22870       return base + 8;
22871
22872
22873       /* Other PC-relative relocations are un-offset.  */
22874     default:
22875       return base;
22876     }
22877 }
22878
22879 static bfd_boolean flag_warn_syms = TRUE;
22880
22881 bfd_boolean
22882 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
22883 {
22884   /* PR 18347 - Warn if the user attempts to create a symbol with the same
22885      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
22886      does mean that the resulting code might be very confusing to the reader.
22887      Also this warning can be triggered if the user omits an operand before
22888      an immediate address, eg:
22889
22890        LDR =foo
22891
22892      GAS treats this as an assignment of the value of the symbol foo to a
22893      symbol LDR, and so (without this code) it will not issue any kind of
22894      warning or error message.
22895
22896      Note - ARM instructions are case-insensitive but the strings in the hash
22897      table are all stored in lower case, so we must first ensure that name is
22898      lower case too.  */
22899   if (flag_warn_syms && arm_ops_hsh)
22900     {
22901       char * nbuf = strdup (name);
22902       char * p;
22903
22904       for (p = nbuf; *p; p++)
22905         *p = TOLOWER (*p);
22906       if (hash_find (arm_ops_hsh, nbuf) != NULL)
22907         {
22908           static struct hash_control * already_warned = NULL;
22909
22910           if (already_warned == NULL)
22911             already_warned = hash_new ();
22912           /* Only warn about the symbol once.  To keep the code
22913              simple we let hash_insert do the lookup for us.  */
22914           if (hash_insert (already_warned, name, NULL) == NULL)
22915             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
22916         }
22917       else
22918         free (nbuf);
22919     }
22920
22921   return FALSE;
22922 }
22923
22924 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
22925    Otherwise we have no need to default values of symbols.  */
22926
22927 symbolS *
22928 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
22929 {
22930 #ifdef OBJ_ELF
22931   if (name[0] == '_' && name[1] == 'G'
22932       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
22933     {
22934       if (!GOT_symbol)
22935         {
22936           if (symbol_find (name))
22937             as_bad (_("GOT already in the symbol table"));
22938
22939           GOT_symbol = symbol_new (name, undefined_section,
22940                                    (valueT) 0, & zero_address_frag);
22941         }
22942
22943       return GOT_symbol;
22944     }
22945 #endif
22946
22947   return NULL;
22948 }
22949
22950 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
22951    computed as two separate immediate values, added together.  We
22952    already know that this value cannot be computed by just one ARM
22953    instruction.  */
22954
22955 static unsigned int
22956 validate_immediate_twopart (unsigned int   val,
22957                             unsigned int * highpart)
22958 {
22959   unsigned int a;
22960   unsigned int i;
22961
22962   for (i = 0; i < 32; i += 2)
22963     if (((a = rotate_left (val, i)) & 0xff) != 0)
22964       {
22965         if (a & 0xff00)
22966           {
22967             if (a & ~ 0xffff)
22968               continue;
22969             * highpart = (a  >> 8) | ((i + 24) << 7);
22970           }
22971         else if (a & 0xff0000)
22972           {
22973             if (a & 0xff000000)
22974               continue;
22975             * highpart = (a >> 16) | ((i + 16) << 7);
22976           }
22977         else
22978           {
22979             gas_assert (a & 0xff000000);
22980             * highpart = (a >> 24) | ((i + 8) << 7);
22981           }
22982
22983         return (a & 0xff) | (i << 7);
22984       }
22985
22986   return FAIL;
22987 }
22988
22989 static int
22990 validate_offset_imm (unsigned int val, int hwse)
22991 {
22992   if ((hwse && val > 255) || val > 4095)
22993     return FAIL;
22994   return val;
22995 }
22996
22997 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
22998    negative immediate constant by altering the instruction.  A bit of
22999    a hack really.
23000         MOV <-> MVN
23001         AND <-> BIC
23002         ADC <-> SBC
23003         by inverting the second operand, and
23004         ADD <-> SUB
23005         CMP <-> CMN
23006         by negating the second operand.  */
23007
23008 static int
23009 negate_data_op (unsigned long * instruction,
23010                 unsigned long   value)
23011 {
23012   int op, new_inst;
23013   unsigned long negated, inverted;
23014
23015   negated = encode_arm_immediate (-value);
23016   inverted = encode_arm_immediate (~value);
23017
23018   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
23019   switch (op)
23020     {
23021       /* First negates.  */
23022     case OPCODE_SUB:             /* ADD <-> SUB  */
23023       new_inst = OPCODE_ADD;
23024       value = negated;
23025       break;
23026
23027     case OPCODE_ADD:
23028       new_inst = OPCODE_SUB;
23029       value = negated;
23030       break;
23031
23032     case OPCODE_CMP:             /* CMP <-> CMN  */
23033       new_inst = OPCODE_CMN;
23034       value = negated;
23035       break;
23036
23037     case OPCODE_CMN:
23038       new_inst = OPCODE_CMP;
23039       value = negated;
23040       break;
23041
23042       /* Now Inverted ops.  */
23043     case OPCODE_MOV:             /* MOV <-> MVN  */
23044       new_inst = OPCODE_MVN;
23045       value = inverted;
23046       break;
23047
23048     case OPCODE_MVN:
23049       new_inst = OPCODE_MOV;
23050       value = inverted;
23051       break;
23052
23053     case OPCODE_AND:             /* AND <-> BIC  */
23054       new_inst = OPCODE_BIC;
23055       value = inverted;
23056       break;
23057
23058     case OPCODE_BIC:
23059       new_inst = OPCODE_AND;
23060       value = inverted;
23061       break;
23062
23063     case OPCODE_ADC:              /* ADC <-> SBC  */
23064       new_inst = OPCODE_SBC;
23065       value = inverted;
23066       break;
23067
23068     case OPCODE_SBC:
23069       new_inst = OPCODE_ADC;
23070       value = inverted;
23071       break;
23072
23073       /* We cannot do anything.  */
23074     default:
23075       return FAIL;
23076     }
23077
23078   if (value == (unsigned) FAIL)
23079     return FAIL;
23080
23081   *instruction &= OPCODE_MASK;
23082   *instruction |= new_inst << DATA_OP_SHIFT;
23083   return value;
23084 }
23085
23086 /* Like negate_data_op, but for Thumb-2.   */
23087
23088 static unsigned int
23089 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
23090 {
23091   int op, new_inst;
23092   int rd;
23093   unsigned int negated, inverted;
23094
23095   negated = encode_thumb32_immediate (-value);
23096   inverted = encode_thumb32_immediate (~value);
23097
23098   rd = (*instruction >> 8) & 0xf;
23099   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
23100   switch (op)
23101     {
23102       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
23103     case T2_OPCODE_SUB:
23104       new_inst = T2_OPCODE_ADD;
23105       value = negated;
23106       break;
23107
23108     case T2_OPCODE_ADD:
23109       new_inst = T2_OPCODE_SUB;
23110       value = negated;
23111       break;
23112
23113       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
23114     case T2_OPCODE_ORR:
23115       new_inst = T2_OPCODE_ORN;
23116       value = inverted;
23117       break;
23118
23119     case T2_OPCODE_ORN:
23120       new_inst = T2_OPCODE_ORR;
23121       value = inverted;
23122       break;
23123
23124       /* AND <-> BIC.  TST has no inverted equivalent.  */
23125     case T2_OPCODE_AND:
23126       new_inst = T2_OPCODE_BIC;
23127       if (rd == 15)
23128         value = FAIL;
23129       else
23130         value = inverted;
23131       break;
23132
23133     case T2_OPCODE_BIC:
23134       new_inst = T2_OPCODE_AND;
23135       value = inverted;
23136       break;
23137
23138       /* ADC <-> SBC  */
23139     case T2_OPCODE_ADC:
23140       new_inst = T2_OPCODE_SBC;
23141       value = inverted;
23142       break;
23143
23144     case T2_OPCODE_SBC:
23145       new_inst = T2_OPCODE_ADC;
23146       value = inverted;
23147       break;
23148
23149       /* We cannot do anything.  */
23150     default:
23151       return FAIL;
23152     }
23153
23154   if (value == (unsigned int)FAIL)
23155     return FAIL;
23156
23157   *instruction &= T2_OPCODE_MASK;
23158   *instruction |= new_inst << T2_DATA_OP_SHIFT;
23159   return value;
23160 }
23161
23162 /* Read a 32-bit thumb instruction from buf.  */
23163
23164 static unsigned long
23165 get_thumb32_insn (char * buf)
23166 {
23167   unsigned long insn;
23168   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
23169   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23170
23171   return insn;
23172 }
23173
23174 /* We usually want to set the low bit on the address of thumb function
23175    symbols.  In particular .word foo - . should have the low bit set.
23176    Generic code tries to fold the difference of two symbols to
23177    a constant.  Prevent this and force a relocation when the first symbols
23178    is a thumb function.  */
23179
23180 bfd_boolean
23181 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
23182 {
23183   if (op == O_subtract
23184       && l->X_op == O_symbol
23185       && r->X_op == O_symbol
23186       && THUMB_IS_FUNC (l->X_add_symbol))
23187     {
23188       l->X_op = O_subtract;
23189       l->X_op_symbol = r->X_add_symbol;
23190       l->X_add_number -= r->X_add_number;
23191       return TRUE;
23192     }
23193
23194   /* Process as normal.  */
23195   return FALSE;
23196 }
23197
23198 /* Encode Thumb2 unconditional branches and calls. The encoding
23199    for the 2 are identical for the immediate values.  */
23200
23201 static void
23202 encode_thumb2_b_bl_offset (char * buf, offsetT value)
23203 {
23204 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
23205   offsetT newval;
23206   offsetT newval2;
23207   addressT S, I1, I2, lo, hi;
23208
23209   S = (value >> 24) & 0x01;
23210   I1 = (value >> 23) & 0x01;
23211   I2 = (value >> 22) & 0x01;
23212   hi = (value >> 12) & 0x3ff;
23213   lo = (value >> 1) & 0x7ff;
23214   newval   = md_chars_to_number (buf, THUMB_SIZE);
23215   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23216   newval  |= (S << 10) | hi;
23217   newval2 &=  ~T2I1I2MASK;
23218   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
23219   md_number_to_chars (buf, newval, THUMB_SIZE);
23220   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23221 }
23222
23223 void
23224 md_apply_fix (fixS *    fixP,
23225                valueT * valP,
23226                segT     seg)
23227 {
23228   offsetT        value = * valP;
23229   offsetT        newval;
23230   unsigned int   newimm;
23231   unsigned long  temp;
23232   int            sign;
23233   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
23234
23235   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
23236
23237   /* Note whether this will delete the relocation.  */
23238
23239   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
23240     fixP->fx_done = 1;
23241
23242   /* On a 64-bit host, silently truncate 'value' to 32 bits for
23243      consistency with the behaviour on 32-bit hosts.  Remember value
23244      for emit_reloc.  */
23245   value &= 0xffffffff;
23246   value ^= 0x80000000;
23247   value -= 0x80000000;
23248
23249   *valP = value;
23250   fixP->fx_addnumber = value;
23251
23252   /* Same treatment for fixP->fx_offset.  */
23253   fixP->fx_offset &= 0xffffffff;
23254   fixP->fx_offset ^= 0x80000000;
23255   fixP->fx_offset -= 0x80000000;
23256
23257   switch (fixP->fx_r_type)
23258     {
23259     case BFD_RELOC_NONE:
23260       /* This will need to go in the object file.  */
23261       fixP->fx_done = 0;
23262       break;
23263
23264     case BFD_RELOC_ARM_IMMEDIATE:
23265       /* We claim that this fixup has been processed here,
23266          even if in fact we generate an error because we do
23267          not have a reloc for it, so tc_gen_reloc will reject it.  */
23268       fixP->fx_done = 1;
23269
23270       if (fixP->fx_addsy)
23271         {
23272           const char *msg = 0;
23273
23274           if (! S_IS_DEFINED (fixP->fx_addsy))
23275             msg = _("undefined symbol %s used as an immediate value");
23276           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
23277             msg = _("symbol %s is in a different section");
23278           else if (S_IS_WEAK (fixP->fx_addsy))
23279             msg = _("symbol %s is weak and may be overridden later");
23280
23281           if (msg)
23282             {
23283               as_bad_where (fixP->fx_file, fixP->fx_line,
23284                             msg, S_GET_NAME (fixP->fx_addsy));
23285               break;
23286             }
23287         }
23288
23289       temp = md_chars_to_number (buf, INSN_SIZE);
23290
23291       /* If the offset is negative, we should use encoding A2 for ADR.  */
23292       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
23293         newimm = negate_data_op (&temp, value);
23294       else
23295         {
23296           newimm = encode_arm_immediate (value);
23297
23298           /* If the instruction will fail, see if we can fix things up by
23299              changing the opcode.  */
23300           if (newimm == (unsigned int) FAIL)
23301             newimm = negate_data_op (&temp, value);
23302           /* MOV accepts both ARM modified immediate (A1 encoding) and
23303              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
23304              When disassembling, MOV is preferred when there is no encoding
23305              overlap.  */
23306           if (newimm == (unsigned int) FAIL
23307               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
23308               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
23309               && !((temp >> SBIT_SHIFT) & 0x1)
23310               && value >= 0 && value <= 0xffff)
23311             {
23312               /* Clear bits[23:20] to change encoding from A1 to A2.  */
23313               temp &= 0xff0fffff;
23314               /* Encoding high 4bits imm.  Code below will encode the remaining
23315                  low 12bits.  */
23316               temp |= (value & 0x0000f000) << 4;
23317               newimm = value & 0x00000fff;
23318             }
23319         }
23320
23321       if (newimm == (unsigned int) FAIL)
23322         {
23323           as_bad_where (fixP->fx_file, fixP->fx_line,
23324                         _("invalid constant (%lx) after fixup"),
23325                         (unsigned long) value);
23326           break;
23327         }
23328
23329       newimm |= (temp & 0xfffff000);
23330       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
23331       break;
23332
23333     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
23334       {
23335         unsigned int highpart = 0;
23336         unsigned int newinsn  = 0xe1a00000; /* nop.  */
23337
23338         if (fixP->fx_addsy)
23339           {
23340             const char *msg = 0;
23341
23342             if (! S_IS_DEFINED (fixP->fx_addsy))
23343               msg = _("undefined symbol %s used as an immediate value");
23344             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
23345               msg = _("symbol %s is in a different section");
23346             else if (S_IS_WEAK (fixP->fx_addsy))
23347               msg = _("symbol %s is weak and may be overridden later");
23348
23349             if (msg)
23350               {
23351                 as_bad_where (fixP->fx_file, fixP->fx_line,
23352                               msg, S_GET_NAME (fixP->fx_addsy));
23353                 break;
23354               }
23355           }
23356
23357         newimm = encode_arm_immediate (value);
23358         temp = md_chars_to_number (buf, INSN_SIZE);
23359
23360         /* If the instruction will fail, see if we can fix things up by
23361            changing the opcode.  */
23362         if (newimm == (unsigned int) FAIL
23363             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
23364           {
23365             /* No ?  OK - try using two ADD instructions to generate
23366                the value.  */
23367             newimm = validate_immediate_twopart (value, & highpart);
23368
23369             /* Yes - then make sure that the second instruction is
23370                also an add.  */
23371             if (newimm != (unsigned int) FAIL)
23372               newinsn = temp;
23373             /* Still No ?  Try using a negated value.  */
23374             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
23375               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
23376             /* Otherwise - give up.  */
23377             else
23378               {
23379                 as_bad_where (fixP->fx_file, fixP->fx_line,
23380                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
23381                               (long) value);
23382                 break;
23383               }
23384
23385             /* Replace the first operand in the 2nd instruction (which
23386                is the PC) with the destination register.  We have
23387                already added in the PC in the first instruction and we
23388                do not want to do it again.  */
23389             newinsn &= ~ 0xf0000;
23390             newinsn |= ((newinsn & 0x0f000) << 4);
23391           }
23392
23393         newimm |= (temp & 0xfffff000);
23394         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
23395
23396         highpart |= (newinsn & 0xfffff000);
23397         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
23398       }
23399       break;
23400
23401     case BFD_RELOC_ARM_OFFSET_IMM:
23402       if (!fixP->fx_done && seg->use_rela_p)
23403         value = 0;
23404       /* Fall through.  */
23405
23406     case BFD_RELOC_ARM_LITERAL:
23407       sign = value > 0;
23408
23409       if (value < 0)
23410         value = - value;
23411
23412       if (validate_offset_imm (value, 0) == FAIL)
23413         {
23414           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
23415             as_bad_where (fixP->fx_file, fixP->fx_line,
23416                           _("invalid literal constant: pool needs to be closer"));
23417           else
23418             as_bad_where (fixP->fx_file, fixP->fx_line,
23419                           _("bad immediate value for offset (%ld)"),
23420                           (long) value);
23421           break;
23422         }
23423
23424       newval = md_chars_to_number (buf, INSN_SIZE);
23425       if (value == 0)
23426         newval &= 0xfffff000;
23427       else
23428         {
23429           newval &= 0xff7ff000;
23430           newval |= value | (sign ? INDEX_UP : 0);
23431         }
23432       md_number_to_chars (buf, newval, INSN_SIZE);
23433       break;
23434
23435     case BFD_RELOC_ARM_OFFSET_IMM8:
23436     case BFD_RELOC_ARM_HWLITERAL:
23437       sign = value > 0;
23438
23439       if (value < 0)
23440         value = - value;
23441
23442       if (validate_offset_imm (value, 1) == FAIL)
23443         {
23444           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
23445             as_bad_where (fixP->fx_file, fixP->fx_line,
23446                           _("invalid literal constant: pool needs to be closer"));
23447           else
23448             as_bad_where (fixP->fx_file, fixP->fx_line,
23449                           _("bad immediate value for 8-bit offset (%ld)"),
23450                           (long) value);
23451           break;
23452         }
23453
23454       newval = md_chars_to_number (buf, INSN_SIZE);
23455       if (value == 0)
23456         newval &= 0xfffff0f0;
23457       else
23458         {
23459           newval &= 0xff7ff0f0;
23460           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
23461         }
23462       md_number_to_chars (buf, newval, INSN_SIZE);
23463       break;
23464
23465     case BFD_RELOC_ARM_T32_OFFSET_U8:
23466       if (value < 0 || value > 1020 || value % 4 != 0)
23467         as_bad_where (fixP->fx_file, fixP->fx_line,
23468                       _("bad immediate value for offset (%ld)"), (long) value);
23469       value /= 4;
23470
23471       newval = md_chars_to_number (buf+2, THUMB_SIZE);
23472       newval |= value;
23473       md_number_to_chars (buf+2, newval, THUMB_SIZE);
23474       break;
23475
23476     case BFD_RELOC_ARM_T32_OFFSET_IMM:
23477       /* This is a complicated relocation used for all varieties of Thumb32
23478          load/store instruction with immediate offset:
23479
23480          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
23481                                                    *4, optional writeback(W)
23482                                                    (doubleword load/store)
23483
23484          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
23485          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
23486          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
23487          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
23488          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
23489
23490          Uppercase letters indicate bits that are already encoded at
23491          this point.  Lowercase letters are our problem.  For the
23492          second block of instructions, the secondary opcode nybble
23493          (bits 8..11) is present, and bit 23 is zero, even if this is
23494          a PC-relative operation.  */
23495       newval = md_chars_to_number (buf, THUMB_SIZE);
23496       newval <<= 16;
23497       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
23498
23499       if ((newval & 0xf0000000) == 0xe0000000)
23500         {
23501           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
23502           if (value >= 0)
23503             newval |= (1 << 23);
23504           else
23505             value = -value;
23506           if (value % 4 != 0)
23507             {
23508               as_bad_where (fixP->fx_file, fixP->fx_line,
23509                             _("offset not a multiple of 4"));
23510               break;
23511             }
23512           value /= 4;
23513           if (value > 0xff)
23514             {
23515               as_bad_where (fixP->fx_file, fixP->fx_line,
23516                             _("offset out of range"));
23517               break;
23518             }
23519           newval &= ~0xff;
23520         }
23521       else if ((newval & 0x000f0000) == 0x000f0000)
23522         {
23523           /* PC-relative, 12-bit offset.  */
23524           if (value >= 0)
23525             newval |= (1 << 23);
23526           else
23527             value = -value;
23528           if (value > 0xfff)
23529             {
23530               as_bad_where (fixP->fx_file, fixP->fx_line,
23531                             _("offset out of range"));
23532               break;
23533             }
23534           newval &= ~0xfff;
23535         }
23536       else if ((newval & 0x00000100) == 0x00000100)
23537         {
23538           /* Writeback: 8-bit, +/- offset.  */
23539           if (value >= 0)
23540             newval |= (1 << 9);
23541           else
23542             value = -value;
23543           if (value > 0xff)
23544             {
23545               as_bad_where (fixP->fx_file, fixP->fx_line,
23546                             _("offset out of range"));
23547               break;
23548             }
23549           newval &= ~0xff;
23550         }
23551       else if ((newval & 0x00000f00) == 0x00000e00)
23552         {
23553           /* T-instruction: positive 8-bit offset.  */
23554           if (value < 0 || value > 0xff)
23555             {
23556               as_bad_where (fixP->fx_file, fixP->fx_line,
23557                             _("offset out of range"));
23558               break;
23559             }
23560           newval &= ~0xff;
23561           newval |= value;
23562         }
23563       else
23564         {
23565           /* Positive 12-bit or negative 8-bit offset.  */
23566           int limit;
23567           if (value >= 0)
23568             {
23569               newval |= (1 << 23);
23570               limit = 0xfff;
23571             }
23572           else
23573             {
23574               value = -value;
23575               limit = 0xff;
23576             }
23577           if (value > limit)
23578             {
23579               as_bad_where (fixP->fx_file, fixP->fx_line,
23580                             _("offset out of range"));
23581               break;
23582             }
23583           newval &= ~limit;
23584         }
23585
23586       newval |= value;
23587       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
23588       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
23589       break;
23590
23591     case BFD_RELOC_ARM_SHIFT_IMM:
23592       newval = md_chars_to_number (buf, INSN_SIZE);
23593       if (((unsigned long) value) > 32
23594           || (value == 32
23595               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
23596         {
23597           as_bad_where (fixP->fx_file, fixP->fx_line,
23598                         _("shift expression is too large"));
23599           break;
23600         }
23601
23602       if (value == 0)
23603         /* Shifts of zero must be done as lsl.  */
23604         newval &= ~0x60;
23605       else if (value == 32)
23606         value = 0;
23607       newval &= 0xfffff07f;
23608       newval |= (value & 0x1f) << 7;
23609       md_number_to_chars (buf, newval, INSN_SIZE);
23610       break;
23611
23612     case BFD_RELOC_ARM_T32_IMMEDIATE:
23613     case BFD_RELOC_ARM_T32_ADD_IMM:
23614     case BFD_RELOC_ARM_T32_IMM12:
23615     case BFD_RELOC_ARM_T32_ADD_PC12:
23616       /* We claim that this fixup has been processed here,
23617          even if in fact we generate an error because we do
23618          not have a reloc for it, so tc_gen_reloc will reject it.  */
23619       fixP->fx_done = 1;
23620
23621       if (fixP->fx_addsy
23622           && ! S_IS_DEFINED (fixP->fx_addsy))
23623         {
23624           as_bad_where (fixP->fx_file, fixP->fx_line,
23625                         _("undefined symbol %s used as an immediate value"),
23626                         S_GET_NAME (fixP->fx_addsy));
23627           break;
23628         }
23629
23630       newval = md_chars_to_number (buf, THUMB_SIZE);
23631       newval <<= 16;
23632       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
23633
23634       newimm = FAIL;
23635       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23636            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
23637               Thumb2 modified immediate encoding (T2).  */
23638            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
23639           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23640         {
23641           newimm = encode_thumb32_immediate (value);
23642           if (newimm == (unsigned int) FAIL)
23643             newimm = thumb32_negate_data_op (&newval, value);
23644         }
23645       if (newimm == (unsigned int) FAIL)
23646         {
23647           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
23648             {
23649               /* Turn add/sum into addw/subw.  */
23650               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23651                 newval = (newval & 0xfeffffff) | 0x02000000;
23652               /* No flat 12-bit imm encoding for addsw/subsw.  */
23653               if ((newval & 0x00100000) == 0)
23654                 {
23655                   /* 12 bit immediate for addw/subw.  */
23656                   if (value < 0)
23657                     {
23658                       value = -value;
23659                       newval ^= 0x00a00000;
23660                     }
23661                   if (value > 0xfff)
23662                     newimm = (unsigned int) FAIL;
23663                   else
23664                     newimm = value;
23665                 }
23666             }
23667           else
23668             {
23669               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
23670                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
23671                  disassembling, MOV is preferred when there is no encoding
23672                  overlap.  */
23673               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
23674                   /* NOTE: MOV uses the ORR opcode in Thumb 2 mode
23675                      but with the Rn field [19:16] set to 1111.  */
23676                   && (((newval >> 16) & 0xf) == 0xf)
23677                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
23678                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
23679                   && value >= 0 && value <= 0xffff)
23680                 {
23681                   /* Toggle bit[25] to change encoding from T2 to T3.  */
23682                   newval ^= 1 << 25;
23683                   /* Clear bits[19:16].  */
23684                   newval &= 0xfff0ffff;
23685                   /* Encoding high 4bits imm.  Code below will encode the
23686                      remaining low 12bits.  */
23687                   newval |= (value & 0x0000f000) << 4;
23688                   newimm = value & 0x00000fff;
23689                 }
23690             }
23691         }
23692
23693       if (newimm == (unsigned int)FAIL)
23694         {
23695           as_bad_where (fixP->fx_file, fixP->fx_line,
23696                         _("invalid constant (%lx) after fixup"),
23697                         (unsigned long) value);
23698           break;
23699         }
23700
23701       newval |= (newimm & 0x800) << 15;
23702       newval |= (newimm & 0x700) << 4;
23703       newval |= (newimm & 0x0ff);
23704
23705       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
23706       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
23707       break;
23708
23709     case BFD_RELOC_ARM_SMC:
23710       if (((unsigned long) value) > 0xffff)
23711         as_bad_where (fixP->fx_file, fixP->fx_line,
23712                       _("invalid smc expression"));
23713       newval = md_chars_to_number (buf, INSN_SIZE);
23714       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23715       md_number_to_chars (buf, newval, INSN_SIZE);
23716       break;
23717
23718     case BFD_RELOC_ARM_HVC:
23719       if (((unsigned long) value) > 0xffff)
23720         as_bad_where (fixP->fx_file, fixP->fx_line,
23721                       _("invalid hvc expression"));
23722       newval = md_chars_to_number (buf, INSN_SIZE);
23723       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23724       md_number_to_chars (buf, newval, INSN_SIZE);
23725       break;
23726
23727     case BFD_RELOC_ARM_SWI:
23728       if (fixP->tc_fix_data != 0)
23729         {
23730           if (((unsigned long) value) > 0xff)
23731             as_bad_where (fixP->fx_file, fixP->fx_line,
23732                           _("invalid swi expression"));
23733           newval = md_chars_to_number (buf, THUMB_SIZE);
23734           newval |= value;
23735           md_number_to_chars (buf, newval, THUMB_SIZE);
23736         }
23737       else
23738         {
23739           if (((unsigned long) value) > 0x00ffffff)
23740             as_bad_where (fixP->fx_file, fixP->fx_line,
23741                           _("invalid swi expression"));
23742           newval = md_chars_to_number (buf, INSN_SIZE);
23743           newval |= value;
23744           md_number_to_chars (buf, newval, INSN_SIZE);
23745         }
23746       break;
23747
23748     case BFD_RELOC_ARM_MULTI:
23749       if (((unsigned long) value) > 0xffff)
23750         as_bad_where (fixP->fx_file, fixP->fx_line,
23751                       _("invalid expression in load/store multiple"));
23752       newval = value | md_chars_to_number (buf, INSN_SIZE);
23753       md_number_to_chars (buf, newval, INSN_SIZE);
23754       break;
23755
23756 #ifdef OBJ_ELF
23757     case BFD_RELOC_ARM_PCREL_CALL:
23758
23759       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23760           && fixP->fx_addsy
23761           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23762           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23763           && THUMB_IS_FUNC (fixP->fx_addsy))
23764         /* Flip the bl to blx. This is a simple flip
23765            bit here because we generate PCREL_CALL for
23766            unconditional bls.  */
23767         {
23768           newval = md_chars_to_number (buf, INSN_SIZE);
23769           newval = newval | 0x10000000;
23770           md_number_to_chars (buf, newval, INSN_SIZE);
23771           temp = 1;
23772           fixP->fx_done = 1;
23773         }
23774       else
23775         temp = 3;
23776       goto arm_branch_common;
23777
23778     case BFD_RELOC_ARM_PCREL_JUMP:
23779       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23780           && fixP->fx_addsy
23781           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23782           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23783           && THUMB_IS_FUNC (fixP->fx_addsy))
23784         {
23785           /* This would map to a bl<cond>, b<cond>,
23786              b<always> to a Thumb function. We
23787              need to force a relocation for this particular
23788              case.  */
23789           newval = md_chars_to_number (buf, INSN_SIZE);
23790           fixP->fx_done = 0;
23791         }
23792       /* Fall through.  */
23793
23794     case BFD_RELOC_ARM_PLT32:
23795 #endif
23796     case BFD_RELOC_ARM_PCREL_BRANCH:
23797       temp = 3;
23798       goto arm_branch_common;
23799
23800     case BFD_RELOC_ARM_PCREL_BLX:
23801
23802       temp = 1;
23803       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23804           && fixP->fx_addsy
23805           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23806           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23807           && ARM_IS_FUNC (fixP->fx_addsy))
23808         {
23809           /* Flip the blx to a bl and warn.  */
23810           const char *name = S_GET_NAME (fixP->fx_addsy);
23811           newval = 0xeb000000;
23812           as_warn_where (fixP->fx_file, fixP->fx_line,
23813                          _("blx to '%s' an ARM ISA state function changed to bl"),
23814                           name);
23815           md_number_to_chars (buf, newval, INSN_SIZE);
23816           temp = 3;
23817           fixP->fx_done = 1;
23818         }
23819
23820 #ifdef OBJ_ELF
23821        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23822          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
23823 #endif
23824
23825     arm_branch_common:
23826       /* We are going to store value (shifted right by two) in the
23827          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
23828          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
23829          also be clear.  */
23830       if (value & temp)
23831         as_bad_where (fixP->fx_file, fixP->fx_line,
23832                       _("misaligned branch destination"));
23833       if ((value & (offsetT)0xfe000000) != (offsetT)0
23834           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
23835         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23836
23837       if (fixP->fx_done || !seg->use_rela_p)
23838         {
23839           newval = md_chars_to_number (buf, INSN_SIZE);
23840           newval |= (value >> 2) & 0x00ffffff;
23841           /* Set the H bit on BLX instructions.  */
23842           if (temp == 1)
23843             {
23844               if (value & 2)
23845                 newval |= 0x01000000;
23846               else
23847                 newval &= ~0x01000000;
23848             }
23849           md_number_to_chars (buf, newval, INSN_SIZE);
23850         }
23851       break;
23852
23853     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
23854       /* CBZ can only branch forward.  */
23855
23856       /* Attempts to use CBZ to branch to the next instruction
23857          (which, strictly speaking, are prohibited) will be turned into
23858          no-ops.
23859
23860          FIXME: It may be better to remove the instruction completely and
23861          perform relaxation.  */
23862       if (value == -2)
23863         {
23864           newval = md_chars_to_number (buf, THUMB_SIZE);
23865           newval = 0xbf00; /* NOP encoding T1 */
23866           md_number_to_chars (buf, newval, THUMB_SIZE);
23867         }
23868       else
23869         {
23870           if (value & ~0x7e)
23871             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23872
23873           if (fixP->fx_done || !seg->use_rela_p)
23874             {
23875               newval = md_chars_to_number (buf, THUMB_SIZE);
23876               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
23877               md_number_to_chars (buf, newval, THUMB_SIZE);
23878             }
23879         }
23880       break;
23881
23882     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
23883       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
23884         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23885
23886       if (fixP->fx_done || !seg->use_rela_p)
23887         {
23888           newval = md_chars_to_number (buf, THUMB_SIZE);
23889           newval |= (value & 0x1ff) >> 1;
23890           md_number_to_chars (buf, newval, THUMB_SIZE);
23891         }
23892       break;
23893
23894     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
23895       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
23896         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23897
23898       if (fixP->fx_done || !seg->use_rela_p)
23899         {
23900           newval = md_chars_to_number (buf, THUMB_SIZE);
23901           newval |= (value & 0xfff) >> 1;
23902           md_number_to_chars (buf, newval, THUMB_SIZE);
23903         }
23904       break;
23905
23906     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23907       if (fixP->fx_addsy
23908           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23909           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23910           && ARM_IS_FUNC (fixP->fx_addsy)
23911           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23912         {
23913           /* Force a relocation for a branch 20 bits wide.  */
23914           fixP->fx_done = 0;
23915         }
23916       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
23917         as_bad_where (fixP->fx_file, fixP->fx_line,
23918                       _("conditional branch out of range"));
23919
23920       if (fixP->fx_done || !seg->use_rela_p)
23921         {
23922           offsetT newval2;
23923           addressT S, J1, J2, lo, hi;
23924
23925           S  = (value & 0x00100000) >> 20;
23926           J2 = (value & 0x00080000) >> 19;
23927           J1 = (value & 0x00040000) >> 18;
23928           hi = (value & 0x0003f000) >> 12;
23929           lo = (value & 0x00000ffe) >> 1;
23930
23931           newval   = md_chars_to_number (buf, THUMB_SIZE);
23932           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23933           newval  |= (S << 10) | hi;
23934           newval2 |= (J1 << 13) | (J2 << 11) | lo;
23935           md_number_to_chars (buf, newval, THUMB_SIZE);
23936           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23937         }
23938       break;
23939
23940     case BFD_RELOC_THUMB_PCREL_BLX:
23941       /* If there is a blx from a thumb state function to
23942          another thumb function flip this to a bl and warn
23943          about it.  */
23944
23945       if (fixP->fx_addsy
23946           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23947           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23948           && THUMB_IS_FUNC (fixP->fx_addsy))
23949         {
23950           const char *name = S_GET_NAME (fixP->fx_addsy);
23951           as_warn_where (fixP->fx_file, fixP->fx_line,
23952                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
23953                          name);
23954           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23955           newval = newval | 0x1000;
23956           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23957           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23958           fixP->fx_done = 1;
23959         }
23960
23961
23962       goto thumb_bl_common;
23963
23964     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23965       /* A bl from Thumb state ISA to an internal ARM state function
23966          is converted to a blx.  */
23967       if (fixP->fx_addsy
23968           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23969           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23970           && ARM_IS_FUNC (fixP->fx_addsy)
23971           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23972         {
23973           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23974           newval = newval & ~0x1000;
23975           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23976           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
23977           fixP->fx_done = 1;
23978         }
23979
23980     thumb_bl_common:
23981
23982       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23983         /* For a BLX instruction, make sure that the relocation is rounded up
23984            to a word boundary.  This follows the semantics of the instruction
23985            which specifies that bit 1 of the target address will come from bit
23986            1 of the base address.  */
23987         value = (value + 3) & ~ 3;
23988
23989 #ifdef OBJ_ELF
23990        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
23991            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23992          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23993 #endif
23994
23995       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
23996         {
23997           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
23998             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23999           else if ((value & ~0x1ffffff)
24000                    && ((value & ~0x1ffffff) != ~0x1ffffff))
24001             as_bad_where (fixP->fx_file, fixP->fx_line,
24002                           _("Thumb2 branch out of range"));
24003         }
24004
24005       if (fixP->fx_done || !seg->use_rela_p)
24006         encode_thumb2_b_bl_offset (buf, value);
24007
24008       break;
24009
24010     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24011       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
24012         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
24013
24014       if (fixP->fx_done || !seg->use_rela_p)
24015           encode_thumb2_b_bl_offset (buf, value);
24016
24017       break;
24018
24019     case BFD_RELOC_8:
24020       if (fixP->fx_done || !seg->use_rela_p)
24021         *buf = value;
24022       break;
24023
24024     case BFD_RELOC_16:
24025       if (fixP->fx_done || !seg->use_rela_p)
24026         md_number_to_chars (buf, value, 2);
24027       break;
24028
24029 #ifdef OBJ_ELF
24030     case BFD_RELOC_ARM_TLS_CALL:
24031     case BFD_RELOC_ARM_THM_TLS_CALL:
24032     case BFD_RELOC_ARM_TLS_DESCSEQ:
24033     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
24034     case BFD_RELOC_ARM_TLS_GOTDESC:
24035     case BFD_RELOC_ARM_TLS_GD32:
24036     case BFD_RELOC_ARM_TLS_LE32:
24037     case BFD_RELOC_ARM_TLS_IE32:
24038     case BFD_RELOC_ARM_TLS_LDM32:
24039     case BFD_RELOC_ARM_TLS_LDO32:
24040       S_SET_THREAD_LOCAL (fixP->fx_addsy);
24041       break;
24042
24043       /* Same handling as above, but with the arm_fdpic guard.  */
24044     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
24045     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
24046     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
24047       if (arm_fdpic)
24048         {
24049           S_SET_THREAD_LOCAL (fixP->fx_addsy);
24050         }
24051       else
24052         {
24053           as_bad_where (fixP->fx_file, fixP->fx_line,
24054                         _("Relocation supported only in FDPIC mode"));
24055         }
24056       break;
24057
24058     case BFD_RELOC_ARM_GOT32:
24059     case BFD_RELOC_ARM_GOTOFF:
24060       break;
24061
24062     case BFD_RELOC_ARM_GOT_PREL:
24063       if (fixP->fx_done || !seg->use_rela_p)
24064         md_number_to_chars (buf, value, 4);
24065       break;
24066
24067     case BFD_RELOC_ARM_TARGET2:
24068       /* TARGET2 is not partial-inplace, so we need to write the
24069          addend here for REL targets, because it won't be written out
24070          during reloc processing later.  */
24071       if (fixP->fx_done || !seg->use_rela_p)
24072         md_number_to_chars (buf, fixP->fx_offset, 4);
24073       break;
24074
24075       /* Relocations for FDPIC.  */
24076     case BFD_RELOC_ARM_GOTFUNCDESC:
24077     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
24078     case BFD_RELOC_ARM_FUNCDESC:
24079       if (arm_fdpic)
24080         {
24081           if (fixP->fx_done || !seg->use_rela_p)
24082             md_number_to_chars (buf, 0, 4);
24083         }
24084       else
24085         {
24086           as_bad_where (fixP->fx_file, fixP->fx_line,
24087                         _("Relocation supported only in FDPIC mode"));
24088       }
24089       break;
24090 #endif
24091
24092     case BFD_RELOC_RVA:
24093     case BFD_RELOC_32:
24094     case BFD_RELOC_ARM_TARGET1:
24095     case BFD_RELOC_ARM_ROSEGREL32:
24096     case BFD_RELOC_ARM_SBREL32:
24097     case BFD_RELOC_32_PCREL:
24098 #ifdef TE_PE
24099     case BFD_RELOC_32_SECREL:
24100 #endif
24101       if (fixP->fx_done || !seg->use_rela_p)
24102 #ifdef TE_WINCE
24103         /* For WinCE we only do this for pcrel fixups.  */
24104         if (fixP->fx_done || fixP->fx_pcrel)
24105 #endif
24106           md_number_to_chars (buf, value, 4);
24107       break;
24108
24109 #ifdef OBJ_ELF
24110     case BFD_RELOC_ARM_PREL31:
24111       if (fixP->fx_done || !seg->use_rela_p)
24112         {
24113           newval = md_chars_to_number (buf, 4) & 0x80000000;
24114           if ((value ^ (value >> 1)) & 0x40000000)
24115             {
24116               as_bad_where (fixP->fx_file, fixP->fx_line,
24117                             _("rel31 relocation overflow"));
24118             }
24119           newval |= value & 0x7fffffff;
24120           md_number_to_chars (buf, newval, 4);
24121         }
24122       break;
24123 #endif
24124
24125     case BFD_RELOC_ARM_CP_OFF_IMM:
24126     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
24127       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
24128         newval = md_chars_to_number (buf, INSN_SIZE);
24129       else
24130         newval = get_thumb32_insn (buf);
24131       if ((newval & 0x0f200f00) == 0x0d000900)
24132         {
24133           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
24134              has permitted values that are multiples of 2, in the range 0
24135              to 510.  */
24136           if (value < -510 || value > 510 || (value & 1))
24137             as_bad_where (fixP->fx_file, fixP->fx_line,
24138                           _("co-processor offset out of range"));
24139         }
24140       else if (value < -1023 || value > 1023 || (value & 3))
24141         as_bad_where (fixP->fx_file, fixP->fx_line,
24142                       _("co-processor offset out of range"));
24143     cp_off_common:
24144       sign = value > 0;
24145       if (value < 0)
24146         value = -value;
24147       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24148           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
24149         newval = md_chars_to_number (buf, INSN_SIZE);
24150       else
24151         newval = get_thumb32_insn (buf);
24152       if (value == 0)
24153         newval &= 0xffffff00;
24154       else
24155         {
24156           newval &= 0xff7fff00;
24157           if ((newval & 0x0f200f00) == 0x0d000900)
24158             {
24159               /* This is a fp16 vstr/vldr.
24160
24161                  It requires the immediate offset in the instruction is shifted
24162                  left by 1 to be a half-word offset.
24163
24164                  Here, left shift by 1 first, and later right shift by 2
24165                  should get the right offset.  */
24166               value <<= 1;
24167             }
24168           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
24169         }
24170       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24171           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
24172         md_number_to_chars (buf, newval, INSN_SIZE);
24173       else
24174         put_thumb32_insn (buf, newval);
24175       break;
24176
24177     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
24178     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
24179       if (value < -255 || value > 255)
24180         as_bad_where (fixP->fx_file, fixP->fx_line,
24181                       _("co-processor offset out of range"));
24182       value *= 4;
24183       goto cp_off_common;
24184
24185     case BFD_RELOC_ARM_THUMB_OFFSET:
24186       newval = md_chars_to_number (buf, THUMB_SIZE);
24187       /* Exactly what ranges, and where the offset is inserted depends
24188          on the type of instruction, we can establish this from the
24189          top 4 bits.  */
24190       switch (newval >> 12)
24191         {
24192         case 4: /* PC load.  */
24193           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
24194              forced to zero for these loads; md_pcrel_from has already
24195              compensated for this.  */
24196           if (value & 3)
24197             as_bad_where (fixP->fx_file, fixP->fx_line,
24198                           _("invalid offset, target not word aligned (0x%08lX)"),
24199                           (((unsigned long) fixP->fx_frag->fr_address
24200                             + (unsigned long) fixP->fx_where) & ~3)
24201                           + (unsigned long) value);
24202
24203           if (value & ~0x3fc)
24204             as_bad_where (fixP->fx_file, fixP->fx_line,
24205                           _("invalid offset, value too big (0x%08lX)"),
24206                           (long) value);
24207
24208           newval |= value >> 2;
24209           break;
24210
24211         case 9: /* SP load/store.  */
24212           if (value & ~0x3fc)
24213             as_bad_where (fixP->fx_file, fixP->fx_line,
24214                           _("invalid offset, value too big (0x%08lX)"),
24215                           (long) value);
24216           newval |= value >> 2;
24217           break;
24218
24219         case 6: /* Word load/store.  */
24220           if (value & ~0x7c)
24221             as_bad_where (fixP->fx_file, fixP->fx_line,
24222                           _("invalid offset, value too big (0x%08lX)"),
24223                           (long) value);
24224           newval |= value << 4; /* 6 - 2.  */
24225           break;
24226
24227         case 7: /* Byte load/store.  */
24228           if (value & ~0x1f)
24229             as_bad_where (fixP->fx_file, fixP->fx_line,
24230                           _("invalid offset, value too big (0x%08lX)"),
24231                           (long) value);
24232           newval |= value << 6;
24233           break;
24234
24235         case 8: /* Halfword load/store.  */
24236           if (value & ~0x3e)
24237             as_bad_where (fixP->fx_file, fixP->fx_line,
24238                           _("invalid offset, value too big (0x%08lX)"),
24239                           (long) value);
24240           newval |= value << 5; /* 6 - 1.  */
24241           break;
24242
24243         default:
24244           as_bad_where (fixP->fx_file, fixP->fx_line,
24245                         "Unable to process relocation for thumb opcode: %lx",
24246                         (unsigned long) newval);
24247           break;
24248         }
24249       md_number_to_chars (buf, newval, THUMB_SIZE);
24250       break;
24251
24252     case BFD_RELOC_ARM_THUMB_ADD:
24253       /* This is a complicated relocation, since we use it for all of
24254          the following immediate relocations:
24255
24256             3bit ADD/SUB
24257             8bit ADD/SUB
24258             9bit ADD/SUB SP word-aligned
24259            10bit ADD PC/SP word-aligned
24260
24261          The type of instruction being processed is encoded in the
24262          instruction field:
24263
24264            0x8000  SUB
24265            0x00F0  Rd
24266            0x000F  Rs
24267       */
24268       newval = md_chars_to_number (buf, THUMB_SIZE);
24269       {
24270         int rd = (newval >> 4) & 0xf;
24271         int rs = newval & 0xf;
24272         int subtract = !!(newval & 0x8000);
24273
24274         /* Check for HI regs, only very restricted cases allowed:
24275            Adjusting SP, and using PC or SP to get an address.  */
24276         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
24277             || (rs > 7 && rs != REG_SP && rs != REG_PC))
24278           as_bad_where (fixP->fx_file, fixP->fx_line,
24279                         _("invalid Hi register with immediate"));
24280
24281         /* If value is negative, choose the opposite instruction.  */
24282         if (value < 0)
24283           {
24284             value = -value;
24285             subtract = !subtract;
24286             if (value < 0)
24287               as_bad_where (fixP->fx_file, fixP->fx_line,
24288                             _("immediate value out of range"));
24289           }
24290
24291         if (rd == REG_SP)
24292           {
24293             if (value & ~0x1fc)
24294               as_bad_where (fixP->fx_file, fixP->fx_line,
24295                             _("invalid immediate for stack address calculation"));
24296             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
24297             newval |= value >> 2;
24298           }
24299         else if (rs == REG_PC || rs == REG_SP)
24300           {
24301             /* PR gas/18541.  If the addition is for a defined symbol
24302                within range of an ADR instruction then accept it.  */
24303             if (subtract
24304                 && value == 4
24305                 && fixP->fx_addsy != NULL)
24306               {
24307                 subtract = 0;
24308
24309                 if (! S_IS_DEFINED (fixP->fx_addsy)
24310                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
24311                     || S_IS_WEAK (fixP->fx_addsy))
24312                   {
24313                     as_bad_where (fixP->fx_file, fixP->fx_line,
24314                                   _("address calculation needs a strongly defined nearby symbol"));
24315                   }
24316                 else
24317                   {
24318                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
24319
24320                     /* Round up to the next 4-byte boundary.  */
24321                     if (v & 3)
24322                       v = (v + 3) & ~ 3;
24323                     else
24324                       v += 4;
24325                     v = S_GET_VALUE (fixP->fx_addsy) - v;
24326
24327                     if (v & ~0x3fc)
24328                       {
24329                         as_bad_where (fixP->fx_file, fixP->fx_line,
24330                                       _("symbol too far away"));
24331                       }
24332                     else
24333                       {
24334                         fixP->fx_done = 1;
24335                         value = v;
24336                       }
24337                   }
24338               }
24339
24340             if (subtract || value & ~0x3fc)
24341               as_bad_where (fixP->fx_file, fixP->fx_line,
24342                             _("invalid immediate for address calculation (value = 0x%08lX)"),
24343                             (unsigned long) (subtract ? - value : value));
24344             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
24345             newval |= rd << 8;
24346             newval |= value >> 2;
24347           }
24348         else if (rs == rd)
24349           {
24350             if (value & ~0xff)
24351               as_bad_where (fixP->fx_file, fixP->fx_line,
24352                             _("immediate value out of range"));
24353             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
24354             newval |= (rd << 8) | value;
24355           }
24356         else
24357           {
24358             if (value & ~0x7)
24359               as_bad_where (fixP->fx_file, fixP->fx_line,
24360                             _("immediate value out of range"));
24361             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
24362             newval |= rd | (rs << 3) | (value << 6);
24363           }
24364       }
24365       md_number_to_chars (buf, newval, THUMB_SIZE);
24366       break;
24367
24368     case BFD_RELOC_ARM_THUMB_IMM:
24369       newval = md_chars_to_number (buf, THUMB_SIZE);
24370       if (value < 0 || value > 255)
24371         as_bad_where (fixP->fx_file, fixP->fx_line,
24372                       _("invalid immediate: %ld is out of range"),
24373                       (long) value);
24374       newval |= value;
24375       md_number_to_chars (buf, newval, THUMB_SIZE);
24376       break;
24377
24378     case BFD_RELOC_ARM_THUMB_SHIFT:
24379       /* 5bit shift value (0..32).  LSL cannot take 32.  */
24380       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
24381       temp = newval & 0xf800;
24382       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
24383         as_bad_where (fixP->fx_file, fixP->fx_line,
24384                       _("invalid shift value: %ld"), (long) value);
24385       /* Shifts of zero must be encoded as LSL.  */
24386       if (value == 0)
24387         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
24388       /* Shifts of 32 are encoded as zero.  */
24389       else if (value == 32)
24390         value = 0;
24391       newval |= value << 6;
24392       md_number_to_chars (buf, newval, THUMB_SIZE);
24393       break;
24394
24395     case BFD_RELOC_VTABLE_INHERIT:
24396     case BFD_RELOC_VTABLE_ENTRY:
24397       fixP->fx_done = 0;
24398       return;
24399
24400     case BFD_RELOC_ARM_MOVW:
24401     case BFD_RELOC_ARM_MOVT:
24402     case BFD_RELOC_ARM_THUMB_MOVW:
24403     case BFD_RELOC_ARM_THUMB_MOVT:
24404       if (fixP->fx_done || !seg->use_rela_p)
24405         {
24406           /* REL format relocations are limited to a 16-bit addend.  */
24407           if (!fixP->fx_done)
24408             {
24409               if (value < -0x8000 || value > 0x7fff)
24410                   as_bad_where (fixP->fx_file, fixP->fx_line,
24411                                 _("offset out of range"));
24412             }
24413           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
24414                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
24415             {
24416               value >>= 16;
24417             }
24418
24419           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
24420               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
24421             {
24422               newval = get_thumb32_insn (buf);
24423               newval &= 0xfbf08f00;
24424               newval |= (value & 0xf000) << 4;
24425               newval |= (value & 0x0800) << 15;
24426               newval |= (value & 0x0700) << 4;
24427               newval |= (value & 0x00ff);
24428               put_thumb32_insn (buf, newval);
24429             }
24430           else
24431             {
24432               newval = md_chars_to_number (buf, 4);
24433               newval &= 0xfff0f000;
24434               newval |= value & 0x0fff;
24435               newval |= (value & 0xf000) << 4;
24436               md_number_to_chars (buf, newval, 4);
24437             }
24438         }
24439       return;
24440
24441    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24442    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24443    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24444    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24445       gas_assert (!fixP->fx_done);
24446       {
24447         bfd_vma insn;
24448         bfd_boolean is_mov;
24449         bfd_vma encoded_addend = value;
24450
24451         /* Check that addend can be encoded in instruction.  */
24452         if (!seg->use_rela_p && (value < 0 || value > 255))
24453           as_bad_where (fixP->fx_file, fixP->fx_line,
24454                         _("the offset 0x%08lX is not representable"),
24455                         (unsigned long) encoded_addend);
24456
24457         /* Extract the instruction.  */
24458         insn = md_chars_to_number (buf, THUMB_SIZE);
24459         is_mov = (insn & 0xf800) == 0x2000;
24460
24461         /* Encode insn.  */
24462         if (is_mov)
24463           {
24464             if (!seg->use_rela_p)
24465               insn |= encoded_addend;
24466           }
24467         else
24468           {
24469             int rd, rs;
24470
24471             /* Extract the instruction.  */
24472              /* Encoding is the following
24473                 0x8000  SUB
24474                 0x00F0  Rd
24475                 0x000F  Rs
24476              */
24477              /* The following conditions must be true :
24478                 - ADD
24479                 - Rd == Rs
24480                 - Rd <= 7
24481              */
24482             rd = (insn >> 4) & 0xf;
24483             rs = insn & 0xf;
24484             if ((insn & 0x8000) || (rd != rs) || rd > 7)
24485               as_bad_where (fixP->fx_file, fixP->fx_line,
24486                         _("Unable to process relocation for thumb opcode: %lx"),
24487                         (unsigned long) insn);
24488
24489             /* Encode as ADD immediate8 thumb 1 code.  */
24490             insn = 0x3000 | (rd << 8);
24491
24492             /* Place the encoded addend into the first 8 bits of the
24493                instruction.  */
24494             if (!seg->use_rela_p)
24495               insn |= encoded_addend;
24496           }
24497
24498         /* Update the instruction.  */
24499         md_number_to_chars (buf, insn, THUMB_SIZE);
24500       }
24501       break;
24502
24503    case BFD_RELOC_ARM_ALU_PC_G0_NC:
24504    case BFD_RELOC_ARM_ALU_PC_G0:
24505    case BFD_RELOC_ARM_ALU_PC_G1_NC:
24506    case BFD_RELOC_ARM_ALU_PC_G1:
24507    case BFD_RELOC_ARM_ALU_PC_G2:
24508    case BFD_RELOC_ARM_ALU_SB_G0_NC:
24509    case BFD_RELOC_ARM_ALU_SB_G0:
24510    case BFD_RELOC_ARM_ALU_SB_G1_NC:
24511    case BFD_RELOC_ARM_ALU_SB_G1:
24512    case BFD_RELOC_ARM_ALU_SB_G2:
24513      gas_assert (!fixP->fx_done);
24514      if (!seg->use_rela_p)
24515        {
24516          bfd_vma insn;
24517          bfd_vma encoded_addend;
24518          bfd_vma addend_abs = llabs (value);
24519
24520          /* Check that the absolute value of the addend can be
24521             expressed as an 8-bit constant plus a rotation.  */
24522          encoded_addend = encode_arm_immediate (addend_abs);
24523          if (encoded_addend == (unsigned int) FAIL)
24524            as_bad_where (fixP->fx_file, fixP->fx_line,
24525                          _("the offset 0x%08lX is not representable"),
24526                          (unsigned long) addend_abs);
24527
24528          /* Extract the instruction.  */
24529          insn = md_chars_to_number (buf, INSN_SIZE);
24530
24531          /* If the addend is positive, use an ADD instruction.
24532             Otherwise use a SUB.  Take care not to destroy the S bit.  */
24533          insn &= 0xff1fffff;
24534          if (value < 0)
24535            insn |= 1 << 22;
24536          else
24537            insn |= 1 << 23;
24538
24539          /* Place the encoded addend into the first 12 bits of the
24540             instruction.  */
24541          insn &= 0xfffff000;
24542          insn |= encoded_addend;
24543
24544          /* Update the instruction.  */
24545          md_number_to_chars (buf, insn, INSN_SIZE);
24546        }
24547      break;
24548
24549     case BFD_RELOC_ARM_LDR_PC_G0:
24550     case BFD_RELOC_ARM_LDR_PC_G1:
24551     case BFD_RELOC_ARM_LDR_PC_G2:
24552     case BFD_RELOC_ARM_LDR_SB_G0:
24553     case BFD_RELOC_ARM_LDR_SB_G1:
24554     case BFD_RELOC_ARM_LDR_SB_G2:
24555       gas_assert (!fixP->fx_done);
24556       if (!seg->use_rela_p)
24557         {
24558           bfd_vma insn;
24559           bfd_vma addend_abs = llabs (value);
24560
24561           /* Check that the absolute value of the addend can be
24562              encoded in 12 bits.  */
24563           if (addend_abs >= 0x1000)
24564             as_bad_where (fixP->fx_file, fixP->fx_line,
24565                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
24566                           (unsigned long) addend_abs);
24567
24568           /* Extract the instruction.  */
24569           insn = md_chars_to_number (buf, INSN_SIZE);
24570
24571           /* If the addend is negative, clear bit 23 of the instruction.
24572              Otherwise set it.  */
24573           if (value < 0)
24574             insn &= ~(1 << 23);
24575           else
24576             insn |= 1 << 23;
24577
24578           /* Place the absolute value of the addend into the first 12 bits
24579              of the instruction.  */
24580           insn &= 0xfffff000;
24581           insn |= addend_abs;
24582
24583           /* Update the instruction.  */
24584           md_number_to_chars (buf, insn, INSN_SIZE);
24585         }
24586       break;
24587
24588     case BFD_RELOC_ARM_LDRS_PC_G0:
24589     case BFD_RELOC_ARM_LDRS_PC_G1:
24590     case BFD_RELOC_ARM_LDRS_PC_G2:
24591     case BFD_RELOC_ARM_LDRS_SB_G0:
24592     case BFD_RELOC_ARM_LDRS_SB_G1:
24593     case BFD_RELOC_ARM_LDRS_SB_G2:
24594       gas_assert (!fixP->fx_done);
24595       if (!seg->use_rela_p)
24596         {
24597           bfd_vma insn;
24598           bfd_vma addend_abs = llabs (value);
24599
24600           /* Check that the absolute value of the addend can be
24601              encoded in 8 bits.  */
24602           if (addend_abs >= 0x100)
24603             as_bad_where (fixP->fx_file, fixP->fx_line,
24604                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
24605                           (unsigned long) addend_abs);
24606
24607           /* Extract the instruction.  */
24608           insn = md_chars_to_number (buf, INSN_SIZE);
24609
24610           /* If the addend is negative, clear bit 23 of the instruction.
24611              Otherwise set it.  */
24612           if (value < 0)
24613             insn &= ~(1 << 23);
24614           else
24615             insn |= 1 << 23;
24616
24617           /* Place the first four bits of the absolute value of the addend
24618              into the first 4 bits of the instruction, and the remaining
24619              four into bits 8 .. 11.  */
24620           insn &= 0xfffff0f0;
24621           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
24622
24623           /* Update the instruction.  */
24624           md_number_to_chars (buf, insn, INSN_SIZE);
24625         }
24626       break;
24627
24628     case BFD_RELOC_ARM_LDC_PC_G0:
24629     case BFD_RELOC_ARM_LDC_PC_G1:
24630     case BFD_RELOC_ARM_LDC_PC_G2:
24631     case BFD_RELOC_ARM_LDC_SB_G0:
24632     case BFD_RELOC_ARM_LDC_SB_G1:
24633     case BFD_RELOC_ARM_LDC_SB_G2:
24634       gas_assert (!fixP->fx_done);
24635       if (!seg->use_rela_p)
24636         {
24637           bfd_vma insn;
24638           bfd_vma addend_abs = llabs (value);
24639
24640           /* Check that the absolute value of the addend is a multiple of
24641              four and, when divided by four, fits in 8 bits.  */
24642           if (addend_abs & 0x3)
24643             as_bad_where (fixP->fx_file, fixP->fx_line,
24644                           _("bad offset 0x%08lX (must be word-aligned)"),
24645                           (unsigned long) addend_abs);
24646
24647           if ((addend_abs >> 2) > 0xff)
24648             as_bad_where (fixP->fx_file, fixP->fx_line,
24649                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
24650                           (unsigned long) addend_abs);
24651
24652           /* Extract the instruction.  */
24653           insn = md_chars_to_number (buf, INSN_SIZE);
24654
24655           /* If the addend is negative, clear bit 23 of the instruction.
24656              Otherwise set it.  */
24657           if (value < 0)
24658             insn &= ~(1 << 23);
24659           else
24660             insn |= 1 << 23;
24661
24662           /* Place the addend (divided by four) into the first eight
24663              bits of the instruction.  */
24664           insn &= 0xfffffff0;
24665           insn |= addend_abs >> 2;
24666
24667           /* Update the instruction.  */
24668           md_number_to_chars (buf, insn, INSN_SIZE);
24669         }
24670       break;
24671
24672     case BFD_RELOC_ARM_V4BX:
24673       /* This will need to go in the object file.  */
24674       fixP->fx_done = 0;
24675       break;
24676
24677     case BFD_RELOC_UNUSED:
24678     default:
24679       as_bad_where (fixP->fx_file, fixP->fx_line,
24680                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
24681     }
24682 }
24683
24684 /* Translate internal representation of relocation info to BFD target
24685    format.  */
24686
24687 arelent *
24688 tc_gen_reloc (asection *section, fixS *fixp)
24689 {
24690   arelent * reloc;
24691   bfd_reloc_code_real_type code;
24692
24693   reloc = XNEW (arelent);
24694
24695   reloc->sym_ptr_ptr = XNEW (asymbol *);
24696   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
24697   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
24698
24699   if (fixp->fx_pcrel)
24700     {
24701       if (section->use_rela_p)
24702         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
24703       else
24704         fixp->fx_offset = reloc->address;
24705     }
24706   reloc->addend = fixp->fx_offset;
24707
24708   switch (fixp->fx_r_type)
24709     {
24710     case BFD_RELOC_8:
24711       if (fixp->fx_pcrel)
24712         {
24713           code = BFD_RELOC_8_PCREL;
24714           break;
24715         }
24716       /* Fall through.  */
24717
24718     case BFD_RELOC_16:
24719       if (fixp->fx_pcrel)
24720         {
24721           code = BFD_RELOC_16_PCREL;
24722           break;
24723         }
24724       /* Fall through.  */
24725
24726     case BFD_RELOC_32:
24727       if (fixp->fx_pcrel)
24728         {
24729           code = BFD_RELOC_32_PCREL;
24730           break;
24731         }
24732       /* Fall through.  */
24733
24734     case BFD_RELOC_ARM_MOVW:
24735       if (fixp->fx_pcrel)
24736         {
24737           code = BFD_RELOC_ARM_MOVW_PCREL;
24738           break;
24739         }
24740       /* Fall through.  */
24741
24742     case BFD_RELOC_ARM_MOVT:
24743       if (fixp->fx_pcrel)
24744         {
24745           code = BFD_RELOC_ARM_MOVT_PCREL;
24746           break;
24747         }
24748       /* Fall through.  */
24749
24750     case BFD_RELOC_ARM_THUMB_MOVW:
24751       if (fixp->fx_pcrel)
24752         {
24753           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
24754           break;
24755         }
24756       /* Fall through.  */
24757
24758     case BFD_RELOC_ARM_THUMB_MOVT:
24759       if (fixp->fx_pcrel)
24760         {
24761           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
24762           break;
24763         }
24764       /* Fall through.  */
24765
24766     case BFD_RELOC_NONE:
24767     case BFD_RELOC_ARM_PCREL_BRANCH:
24768     case BFD_RELOC_ARM_PCREL_BLX:
24769     case BFD_RELOC_RVA:
24770     case BFD_RELOC_THUMB_PCREL_BRANCH7:
24771     case BFD_RELOC_THUMB_PCREL_BRANCH9:
24772     case BFD_RELOC_THUMB_PCREL_BRANCH12:
24773     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24774     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24775     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24776     case BFD_RELOC_VTABLE_ENTRY:
24777     case BFD_RELOC_VTABLE_INHERIT:
24778 #ifdef TE_PE
24779     case BFD_RELOC_32_SECREL:
24780 #endif
24781       code = fixp->fx_r_type;
24782       break;
24783
24784     case BFD_RELOC_THUMB_PCREL_BLX:
24785 #ifdef OBJ_ELF
24786       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
24787         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
24788       else
24789 #endif
24790         code = BFD_RELOC_THUMB_PCREL_BLX;
24791       break;
24792
24793     case BFD_RELOC_ARM_LITERAL:
24794     case BFD_RELOC_ARM_HWLITERAL:
24795       /* If this is called then the a literal has
24796          been referenced across a section boundary.  */
24797       as_bad_where (fixp->fx_file, fixp->fx_line,
24798                     _("literal referenced across section boundary"));
24799       return NULL;
24800
24801 #ifdef OBJ_ELF
24802     case BFD_RELOC_ARM_TLS_CALL:
24803     case BFD_RELOC_ARM_THM_TLS_CALL:
24804     case BFD_RELOC_ARM_TLS_DESCSEQ:
24805     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
24806     case BFD_RELOC_ARM_GOT32:
24807     case BFD_RELOC_ARM_GOTOFF:
24808     case BFD_RELOC_ARM_GOT_PREL:
24809     case BFD_RELOC_ARM_PLT32:
24810     case BFD_RELOC_ARM_TARGET1:
24811     case BFD_RELOC_ARM_ROSEGREL32:
24812     case BFD_RELOC_ARM_SBREL32:
24813     case BFD_RELOC_ARM_PREL31:
24814     case BFD_RELOC_ARM_TARGET2:
24815     case BFD_RELOC_ARM_TLS_LDO32:
24816     case BFD_RELOC_ARM_PCREL_CALL:
24817     case BFD_RELOC_ARM_PCREL_JUMP:
24818     case BFD_RELOC_ARM_ALU_PC_G0_NC:
24819     case BFD_RELOC_ARM_ALU_PC_G0:
24820     case BFD_RELOC_ARM_ALU_PC_G1_NC:
24821     case BFD_RELOC_ARM_ALU_PC_G1:
24822     case BFD_RELOC_ARM_ALU_PC_G2:
24823     case BFD_RELOC_ARM_LDR_PC_G0:
24824     case BFD_RELOC_ARM_LDR_PC_G1:
24825     case BFD_RELOC_ARM_LDR_PC_G2:
24826     case BFD_RELOC_ARM_LDRS_PC_G0:
24827     case BFD_RELOC_ARM_LDRS_PC_G1:
24828     case BFD_RELOC_ARM_LDRS_PC_G2:
24829     case BFD_RELOC_ARM_LDC_PC_G0:
24830     case BFD_RELOC_ARM_LDC_PC_G1:
24831     case BFD_RELOC_ARM_LDC_PC_G2:
24832     case BFD_RELOC_ARM_ALU_SB_G0_NC:
24833     case BFD_RELOC_ARM_ALU_SB_G0:
24834     case BFD_RELOC_ARM_ALU_SB_G1_NC:
24835     case BFD_RELOC_ARM_ALU_SB_G1:
24836     case BFD_RELOC_ARM_ALU_SB_G2:
24837     case BFD_RELOC_ARM_LDR_SB_G0:
24838     case BFD_RELOC_ARM_LDR_SB_G1:
24839     case BFD_RELOC_ARM_LDR_SB_G2:
24840     case BFD_RELOC_ARM_LDRS_SB_G0:
24841     case BFD_RELOC_ARM_LDRS_SB_G1:
24842     case BFD_RELOC_ARM_LDRS_SB_G2:
24843     case BFD_RELOC_ARM_LDC_SB_G0:
24844     case BFD_RELOC_ARM_LDC_SB_G1:
24845     case BFD_RELOC_ARM_LDC_SB_G2:
24846     case BFD_RELOC_ARM_V4BX:
24847     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24848     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24849     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24850     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24851     case BFD_RELOC_ARM_GOTFUNCDESC:
24852     case BFD_RELOC_ARM_GOTOFFFUNCDESC:
24853     case BFD_RELOC_ARM_FUNCDESC:
24854       code = fixp->fx_r_type;
24855       break;
24856
24857     case BFD_RELOC_ARM_TLS_GOTDESC:
24858     case BFD_RELOC_ARM_TLS_GD32:
24859     case BFD_RELOC_ARM_TLS_GD32_FDPIC:
24860     case BFD_RELOC_ARM_TLS_LE32:
24861     case BFD_RELOC_ARM_TLS_IE32:
24862     case BFD_RELOC_ARM_TLS_IE32_FDPIC:
24863     case BFD_RELOC_ARM_TLS_LDM32:
24864     case BFD_RELOC_ARM_TLS_LDM32_FDPIC:
24865       /* BFD will include the symbol's address in the addend.
24866          But we don't want that, so subtract it out again here.  */
24867       if (!S_IS_COMMON (fixp->fx_addsy))
24868         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
24869       code = fixp->fx_r_type;
24870       break;
24871 #endif
24872
24873     case BFD_RELOC_ARM_IMMEDIATE:
24874       as_bad_where (fixp->fx_file, fixp->fx_line,
24875                     _("internal relocation (type: IMMEDIATE) not fixed up"));
24876       return NULL;
24877
24878     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
24879       as_bad_where (fixp->fx_file, fixp->fx_line,
24880                     _("ADRL used for a symbol not defined in the same file"));
24881       return NULL;
24882
24883     case BFD_RELOC_ARM_OFFSET_IMM:
24884       if (section->use_rela_p)
24885         {
24886           code = fixp->fx_r_type;
24887           break;
24888         }
24889
24890       if (fixp->fx_addsy != NULL
24891           && !S_IS_DEFINED (fixp->fx_addsy)
24892           && S_IS_LOCAL (fixp->fx_addsy))
24893         {
24894           as_bad_where (fixp->fx_file, fixp->fx_line,
24895                         _("undefined local label `%s'"),
24896                         S_GET_NAME (fixp->fx_addsy));
24897           return NULL;
24898         }
24899
24900       as_bad_where (fixp->fx_file, fixp->fx_line,
24901                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
24902       return NULL;
24903
24904     default:
24905       {
24906         const char * type;
24907
24908         switch (fixp->fx_r_type)
24909           {
24910           case BFD_RELOC_NONE:             type = "NONE";         break;
24911           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
24912           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
24913           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
24914           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
24915           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
24916           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
24917           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
24918           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
24919           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
24920           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
24921           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
24922           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
24923           default:                         type = _("<unknown>"); break;
24924           }
24925         as_bad_where (fixp->fx_file, fixp->fx_line,
24926                       _("cannot represent %s relocation in this object file format"),
24927                       type);
24928         return NULL;
24929       }
24930     }
24931
24932 #ifdef OBJ_ELF
24933   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
24934       && GOT_symbol
24935       && fixp->fx_addsy == GOT_symbol)
24936     {
24937       code = BFD_RELOC_ARM_GOTPC;
24938       reloc->addend = fixp->fx_offset = reloc->address;
24939     }
24940 #endif
24941
24942   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
24943
24944   if (reloc->howto == NULL)
24945     {
24946       as_bad_where (fixp->fx_file, fixp->fx_line,
24947                     _("cannot represent %s relocation in this object file format"),
24948                     bfd_get_reloc_code_name (code));
24949       return NULL;
24950     }
24951
24952   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
24953      vtable entry to be used in the relocation's section offset.  */
24954   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24955     reloc->address = fixp->fx_offset;
24956
24957   return reloc;
24958 }
24959
24960 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
24961
24962 void
24963 cons_fix_new_arm (fragS *       frag,
24964                   int           where,
24965                   int           size,
24966                   expressionS * exp,
24967                   bfd_reloc_code_real_type reloc)
24968 {
24969   int pcrel = 0;
24970
24971   /* Pick a reloc.
24972      FIXME: @@ Should look at CPU word size.  */
24973   switch (size)
24974     {
24975     case 1:
24976       reloc = BFD_RELOC_8;
24977       break;
24978     case 2:
24979       reloc = BFD_RELOC_16;
24980       break;
24981     case 4:
24982     default:
24983       reloc = BFD_RELOC_32;
24984       break;
24985     case 8:
24986       reloc = BFD_RELOC_64;
24987       break;
24988     }
24989
24990 #ifdef TE_PE
24991   if (exp->X_op == O_secrel)
24992   {
24993     exp->X_op = O_symbol;
24994     reloc = BFD_RELOC_32_SECREL;
24995   }
24996 #endif
24997
24998   fix_new_exp (frag, where, size, exp, pcrel, reloc);
24999 }
25000
25001 #if defined (OBJ_COFF)
25002 void
25003 arm_validate_fix (fixS * fixP)
25004 {
25005   /* If the destination of the branch is a defined symbol which does not have
25006      the THUMB_FUNC attribute, then we must be calling a function which has
25007      the (interfacearm) attribute.  We look for the Thumb entry point to that
25008      function and change the branch to refer to that function instead.  */
25009   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
25010       && fixP->fx_addsy != NULL
25011       && S_IS_DEFINED (fixP->fx_addsy)
25012       && ! THUMB_IS_FUNC (fixP->fx_addsy))
25013     {
25014       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
25015     }
25016 }
25017 #endif
25018
25019
25020 int
25021 arm_force_relocation (struct fix * fixp)
25022 {
25023 #if defined (OBJ_COFF) && defined (TE_PE)
25024   if (fixp->fx_r_type == BFD_RELOC_RVA)
25025     return 1;
25026 #endif
25027
25028   /* In case we have a call or a branch to a function in ARM ISA mode from
25029      a thumb function or vice-versa force the relocation. These relocations
25030      are cleared off for some cores that might have blx and simple transformations
25031      are possible.  */
25032
25033 #ifdef OBJ_ELF
25034   switch (fixp->fx_r_type)
25035     {
25036     case BFD_RELOC_ARM_PCREL_JUMP:
25037     case BFD_RELOC_ARM_PCREL_CALL:
25038     case BFD_RELOC_THUMB_PCREL_BLX:
25039       if (THUMB_IS_FUNC (fixp->fx_addsy))
25040         return 1;
25041       break;
25042
25043     case BFD_RELOC_ARM_PCREL_BLX:
25044     case BFD_RELOC_THUMB_PCREL_BRANCH25:
25045     case BFD_RELOC_THUMB_PCREL_BRANCH20:
25046     case BFD_RELOC_THUMB_PCREL_BRANCH23:
25047       if (ARM_IS_FUNC (fixp->fx_addsy))
25048         return 1;
25049       break;
25050
25051     default:
25052       break;
25053     }
25054 #endif
25055
25056   /* Resolve these relocations even if the symbol is extern or weak.
25057      Technically this is probably wrong due to symbol preemption.
25058      In practice these relocations do not have enough range to be useful
25059      at dynamic link time, and some code (e.g. in the Linux kernel)
25060      expects these references to be resolved.  */
25061   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
25062       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
25063       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
25064       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
25065       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
25066       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
25067       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
25068       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
25069       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
25070       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
25071       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
25072       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
25073       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
25074       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
25075     return 0;
25076
25077   /* Always leave these relocations for the linker.  */
25078   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
25079        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
25080       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
25081     return 1;
25082
25083   /* Always generate relocations against function symbols.  */
25084   if (fixp->fx_r_type == BFD_RELOC_32
25085       && fixp->fx_addsy
25086       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
25087     return 1;
25088
25089   return generic_force_reloc (fixp);
25090 }
25091
25092 #if defined (OBJ_ELF) || defined (OBJ_COFF)
25093 /* Relocations against function names must be left unadjusted,
25094    so that the linker can use this information to generate interworking
25095    stubs.  The MIPS version of this function
25096    also prevents relocations that are mips-16 specific, but I do not
25097    know why it does this.
25098
25099    FIXME:
25100    There is one other problem that ought to be addressed here, but
25101    which currently is not:  Taking the address of a label (rather
25102    than a function) and then later jumping to that address.  Such
25103    addresses also ought to have their bottom bit set (assuming that
25104    they reside in Thumb code), but at the moment they will not.  */
25105
25106 bfd_boolean
25107 arm_fix_adjustable (fixS * fixP)
25108 {
25109   if (fixP->fx_addsy == NULL)
25110     return 1;
25111
25112   /* Preserve relocations against symbols with function type.  */
25113   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
25114     return FALSE;
25115
25116   if (THUMB_IS_FUNC (fixP->fx_addsy)
25117       && fixP->fx_subsy == NULL)
25118     return FALSE;
25119
25120   /* We need the symbol name for the VTABLE entries.  */
25121   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
25122       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
25123     return FALSE;
25124
25125   /* Don't allow symbols to be discarded on GOT related relocs.  */
25126   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
25127       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
25128       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
25129       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
25130       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32_FDPIC
25131       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
25132       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
25133       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32_FDPIC
25134       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
25135       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32_FDPIC
25136       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
25137       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
25138       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
25139       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
25140       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
25141       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
25142       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
25143     return FALSE;
25144
25145   /* Similarly for group relocations.  */
25146   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
25147        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
25148       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
25149     return FALSE;
25150
25151   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
25152   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
25153       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
25154       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
25155       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
25156       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
25157       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
25158       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
25159       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
25160     return FALSE;
25161
25162   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
25163      offsets, so keep these symbols.  */
25164   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
25165       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
25166     return FALSE;
25167
25168   return TRUE;
25169 }
25170 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
25171
25172 #ifdef OBJ_ELF
25173 const char *
25174 elf32_arm_target_format (void)
25175 {
25176 #ifdef TE_SYMBIAN
25177   return (target_big_endian
25178           ? "elf32-bigarm-symbian"
25179           : "elf32-littlearm-symbian");
25180 #elif defined (TE_VXWORKS)
25181   return (target_big_endian
25182           ? "elf32-bigarm-vxworks"
25183           : "elf32-littlearm-vxworks");
25184 #elif defined (TE_NACL)
25185   return (target_big_endian
25186           ? "elf32-bigarm-nacl"
25187           : "elf32-littlearm-nacl");
25188 #else
25189   if (arm_fdpic)
25190     {
25191       if (target_big_endian)
25192         return "elf32-bigarm-fdpic";
25193       else
25194         return "elf32-littlearm-fdpic";
25195     }
25196   else
25197     {
25198       if (target_big_endian)
25199         return "elf32-bigarm";
25200       else
25201         return "elf32-littlearm";
25202     }
25203 #endif
25204 }
25205
25206 void
25207 armelf_frob_symbol (symbolS * symp,
25208                     int *     puntp)
25209 {
25210   elf_frob_symbol (symp, puntp);
25211 }
25212 #endif
25213
25214 /* MD interface: Finalization.  */
25215
25216 void
25217 arm_cleanup (void)
25218 {
25219   literal_pool * pool;
25220
25221   /* Ensure that all the IT blocks are properly closed.  */
25222   check_it_blocks_finished ();
25223
25224   for (pool = list_of_pools; pool; pool = pool->next)
25225     {
25226       /* Put it at the end of the relevant section.  */
25227       subseg_set (pool->section, pool->sub_section);
25228 #ifdef OBJ_ELF
25229       arm_elf_change_section ();
25230 #endif
25231       s_ltorg (0);
25232     }
25233 }
25234
25235 #ifdef OBJ_ELF
25236 /* Remove any excess mapping symbols generated for alignment frags in
25237    SEC.  We may have created a mapping symbol before a zero byte
25238    alignment; remove it if there's a mapping symbol after the
25239    alignment.  */
25240 static void
25241 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
25242                        void *dummy ATTRIBUTE_UNUSED)
25243 {
25244   segment_info_type *seginfo = seg_info (sec);
25245   fragS *fragp;
25246
25247   if (seginfo == NULL || seginfo->frchainP == NULL)
25248     return;
25249
25250   for (fragp = seginfo->frchainP->frch_root;
25251        fragp != NULL;
25252        fragp = fragp->fr_next)
25253     {
25254       symbolS *sym = fragp->tc_frag_data.last_map;
25255       fragS *next = fragp->fr_next;
25256
25257       /* Variable-sized frags have been converted to fixed size by
25258          this point.  But if this was variable-sized to start with,
25259          there will be a fixed-size frag after it.  So don't handle
25260          next == NULL.  */
25261       if (sym == NULL || next == NULL)
25262         continue;
25263
25264       if (S_GET_VALUE (sym) < next->fr_address)
25265         /* Not at the end of this frag.  */
25266         continue;
25267       know (S_GET_VALUE (sym) == next->fr_address);
25268
25269       do
25270         {
25271           if (next->tc_frag_data.first_map != NULL)
25272             {
25273               /* Next frag starts with a mapping symbol.  Discard this
25274                  one.  */
25275               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
25276               break;
25277             }
25278
25279           if (next->fr_next == NULL)
25280             {
25281               /* This mapping symbol is at the end of the section.  Discard
25282                  it.  */
25283               know (next->fr_fix == 0 && next->fr_var == 0);
25284               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
25285               break;
25286             }
25287
25288           /* As long as we have empty frags without any mapping symbols,
25289              keep looking.  */
25290           /* If the next frag is non-empty and does not start with a
25291              mapping symbol, then this mapping symbol is required.  */
25292           if (next->fr_address != next->fr_next->fr_address)
25293             break;
25294
25295           next = next->fr_next;
25296         }
25297       while (next != NULL);
25298     }
25299 }
25300 #endif
25301
25302 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
25303    ARM ones.  */
25304
25305 void
25306 arm_adjust_symtab (void)
25307 {
25308 #ifdef OBJ_COFF
25309   symbolS * sym;
25310
25311   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
25312     {
25313       if (ARM_IS_THUMB (sym))
25314         {
25315           if (THUMB_IS_FUNC (sym))
25316             {
25317               /* Mark the symbol as a Thumb function.  */
25318               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
25319                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
25320                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
25321
25322               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
25323                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
25324               else
25325                 as_bad (_("%s: unexpected function type: %d"),
25326                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
25327             }
25328           else switch (S_GET_STORAGE_CLASS (sym))
25329             {
25330             case C_EXT:
25331               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
25332               break;
25333             case C_STAT:
25334               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
25335               break;
25336             case C_LABEL:
25337               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
25338               break;
25339             default:
25340               /* Do nothing.  */
25341               break;
25342             }
25343         }
25344
25345       if (ARM_IS_INTERWORK (sym))
25346         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
25347     }
25348 #endif
25349 #ifdef OBJ_ELF
25350   symbolS * sym;
25351   char      bind;
25352
25353   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
25354     {
25355       if (ARM_IS_THUMB (sym))
25356         {
25357           elf_symbol_type * elf_sym;
25358
25359           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
25360           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
25361
25362           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
25363                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
25364             {
25365               /* If it's a .thumb_func, declare it as so,
25366                  otherwise tag label as .code 16.  */
25367               if (THUMB_IS_FUNC (sym))
25368                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
25369                                          ST_BRANCH_TO_THUMB);
25370               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
25371                 elf_sym->internal_elf_sym.st_info =
25372                   ELF_ST_INFO (bind, STT_ARM_16BIT);
25373             }
25374         }
25375     }
25376
25377   /* Remove any overlapping mapping symbols generated by alignment frags.  */
25378   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
25379   /* Now do generic ELF adjustments.  */
25380   elf_adjust_symtab ();
25381 #endif
25382 }
25383
25384 /* MD interface: Initialization.  */
25385
25386 static void
25387 set_constant_flonums (void)
25388 {
25389   int i;
25390
25391   for (i = 0; i < NUM_FLOAT_VALS; i++)
25392     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
25393       abort ();
25394 }
25395
25396 /* Auto-select Thumb mode if it's the only available instruction set for the
25397    given architecture.  */
25398
25399 static void
25400 autoselect_thumb_from_cpu_variant (void)
25401 {
25402   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
25403     opcode_select (16);
25404 }
25405
25406 void
25407 md_begin (void)
25408 {
25409   unsigned mach;
25410   unsigned int i;
25411
25412   if (   (arm_ops_hsh = hash_new ()) == NULL
25413       || (arm_cond_hsh = hash_new ()) == NULL
25414       || (arm_shift_hsh = hash_new ()) == NULL
25415       || (arm_psr_hsh = hash_new ()) == NULL
25416       || (arm_v7m_psr_hsh = hash_new ()) == NULL
25417       || (arm_reg_hsh = hash_new ()) == NULL
25418       || (arm_reloc_hsh = hash_new ()) == NULL
25419       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
25420     as_fatal (_("virtual memory exhausted"));
25421
25422   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
25423     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
25424   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
25425     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
25426   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
25427     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
25428   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
25429     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
25430   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
25431     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
25432                  (void *) (v7m_psrs + i));
25433   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
25434     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
25435   for (i = 0;
25436        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
25437        i++)
25438     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
25439                  (void *) (barrier_opt_names + i));
25440 #ifdef OBJ_ELF
25441   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
25442     {
25443       struct reloc_entry * entry = reloc_names + i;
25444
25445       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
25446         /* This makes encode_branch() use the EABI versions of this relocation.  */
25447         entry->reloc = BFD_RELOC_UNUSED;
25448
25449       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
25450     }
25451 #endif
25452
25453   set_constant_flonums ();
25454
25455   /* Set the cpu variant based on the command-line options.  We prefer
25456      -mcpu= over -march= if both are set (as for GCC); and we prefer
25457      -mfpu= over any other way of setting the floating point unit.
25458      Use of legacy options with new options are faulted.  */
25459   if (legacy_cpu)
25460     {
25461       if (mcpu_cpu_opt || march_cpu_opt)
25462         as_bad (_("use of old and new-style options to set CPU type"));
25463
25464       selected_arch = *legacy_cpu;
25465     }
25466   else if (mcpu_cpu_opt)
25467     {
25468       selected_arch = *mcpu_cpu_opt;
25469       selected_ext = *mcpu_ext_opt;
25470     }
25471   else if (march_cpu_opt)
25472     {
25473       selected_arch = *march_cpu_opt;
25474       selected_ext = *march_ext_opt;
25475     }
25476   ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
25477
25478   if (legacy_fpu)
25479     {
25480       if (mfpu_opt)
25481         as_bad (_("use of old and new-style options to set FPU type"));
25482
25483       selected_fpu = *legacy_fpu;
25484     }
25485   else if (mfpu_opt)
25486     selected_fpu = *mfpu_opt;
25487   else
25488     {
25489 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
25490         || defined (TE_NetBSD) || defined (TE_VXWORKS))
25491       /* Some environments specify a default FPU.  If they don't, infer it
25492          from the processor.  */
25493       if (mcpu_fpu_opt)
25494         selected_fpu = *mcpu_fpu_opt;
25495       else if (march_fpu_opt)
25496         selected_fpu = *march_fpu_opt;
25497 #else
25498       selected_fpu = fpu_default;
25499 #endif
25500     }
25501
25502   if (ARM_FEATURE_ZERO (selected_fpu))
25503     {
25504       if (!no_cpu_selected ())
25505         selected_fpu = fpu_default;
25506       else
25507         selected_fpu = fpu_arch_fpa;
25508     }
25509
25510 #ifdef CPU_DEFAULT
25511   if (ARM_FEATURE_ZERO (selected_arch))
25512     {
25513       selected_arch = cpu_default;
25514       selected_cpu = selected_arch;
25515     }
25516   ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
25517 #else
25518   /*  Autodection of feature mode: allow all features in cpu_variant but leave
25519       selected_cpu unset.  It will be set in aeabi_set_public_attributes ()
25520       after all instruction have been processed and we can decide what CPU
25521       should be selected.  */
25522   if (ARM_FEATURE_ZERO (selected_arch))
25523     ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
25524   else
25525     ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
25526 #endif
25527
25528   autoselect_thumb_from_cpu_variant ();
25529
25530   arm_arch_used = thumb_arch_used = arm_arch_none;
25531
25532 #if defined OBJ_COFF || defined OBJ_ELF
25533   {
25534     unsigned int flags = 0;
25535
25536 #if defined OBJ_ELF
25537     flags = meabi_flags;
25538
25539     switch (meabi_flags)
25540       {
25541       case EF_ARM_EABI_UNKNOWN:
25542 #endif
25543         /* Set the flags in the private structure.  */
25544         if (uses_apcs_26)      flags |= F_APCS26;
25545         if (support_interwork) flags |= F_INTERWORK;
25546         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
25547         if (pic_code)          flags |= F_PIC;
25548         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
25549           flags |= F_SOFT_FLOAT;
25550
25551         switch (mfloat_abi_opt)
25552           {
25553           case ARM_FLOAT_ABI_SOFT:
25554           case ARM_FLOAT_ABI_SOFTFP:
25555             flags |= F_SOFT_FLOAT;
25556             break;
25557
25558           case ARM_FLOAT_ABI_HARD:
25559             if (flags & F_SOFT_FLOAT)
25560               as_bad (_("hard-float conflicts with specified fpu"));
25561             break;
25562           }
25563
25564         /* Using pure-endian doubles (even if soft-float).      */
25565         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
25566           flags |= F_VFP_FLOAT;
25567
25568 #if defined OBJ_ELF
25569         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
25570             flags |= EF_ARM_MAVERICK_FLOAT;
25571         break;
25572
25573       case EF_ARM_EABI_VER4:
25574       case EF_ARM_EABI_VER5:
25575         /* No additional flags to set.  */
25576         break;
25577
25578       default:
25579         abort ();
25580       }
25581 #endif
25582     bfd_set_private_flags (stdoutput, flags);
25583
25584     /* We have run out flags in the COFF header to encode the
25585        status of ATPCS support, so instead we create a dummy,
25586        empty, debug section called .arm.atpcs.  */
25587     if (atpcs)
25588       {
25589         asection * sec;
25590
25591         sec = bfd_make_section (stdoutput, ".arm.atpcs");
25592
25593         if (sec != NULL)
25594           {
25595             bfd_set_section_flags
25596               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
25597             bfd_set_section_size (stdoutput, sec, 0);
25598             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
25599           }
25600       }
25601   }
25602 #endif
25603
25604   /* Record the CPU type as well.  */
25605   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
25606     mach = bfd_mach_arm_iWMMXt2;
25607   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
25608     mach = bfd_mach_arm_iWMMXt;
25609   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
25610     mach = bfd_mach_arm_XScale;
25611   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
25612     mach = bfd_mach_arm_ep9312;
25613   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
25614     mach = bfd_mach_arm_5TE;
25615   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
25616     {
25617       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25618         mach = bfd_mach_arm_5T;
25619       else
25620         mach = bfd_mach_arm_5;
25621     }
25622   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
25623     {
25624       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25625         mach = bfd_mach_arm_4T;
25626       else
25627         mach = bfd_mach_arm_4;
25628     }
25629   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
25630     mach = bfd_mach_arm_3M;
25631   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
25632     mach = bfd_mach_arm_3;
25633   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
25634     mach = bfd_mach_arm_2a;
25635   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
25636     mach = bfd_mach_arm_2;
25637   else
25638     mach = bfd_mach_arm_unknown;
25639
25640   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
25641 }
25642
25643 /* Command line processing.  */
25644
25645 /* md_parse_option
25646       Invocation line includes a switch not recognized by the base assembler.
25647       See if it's a processor-specific option.
25648
25649       This routine is somewhat complicated by the need for backwards
25650       compatibility (since older releases of gcc can't be changed).
25651       The new options try to make the interface as compatible as
25652       possible with GCC.
25653
25654       New options (supported) are:
25655
25656               -mcpu=<cpu name>           Assemble for selected processor
25657               -march=<architecture name> Assemble for selected architecture
25658               -mfpu=<fpu architecture>   Assemble for selected FPU.
25659               -EB/-mbig-endian           Big-endian
25660               -EL/-mlittle-endian        Little-endian
25661               -k                         Generate PIC code
25662               -mthumb                    Start in Thumb mode
25663               -mthumb-interwork          Code supports ARM/Thumb interworking
25664
25665               -m[no-]warn-deprecated     Warn about deprecated features
25666               -m[no-]warn-syms           Warn when symbols match instructions
25667
25668       For now we will also provide support for:
25669
25670               -mapcs-32                  32-bit Program counter
25671               -mapcs-26                  26-bit Program counter
25672               -macps-float               Floats passed in FP registers
25673               -mapcs-reentrant           Reentrant code
25674               -matpcs
25675       (sometime these will probably be replaced with -mapcs=<list of options>
25676       and -matpcs=<list of options>)
25677
25678       The remaining options are only supported for back-wards compatibility.
25679       Cpu variants, the arm part is optional:
25680               -m[arm]1                Currently not supported.
25681               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
25682               -m[arm]3                Arm 3 processor
25683               -m[arm]6[xx],           Arm 6 processors
25684               -m[arm]7[xx][t][[d]m]   Arm 7 processors
25685               -m[arm]8[10]            Arm 8 processors
25686               -m[arm]9[20][tdmi]      Arm 9 processors
25687               -mstrongarm[110[0]]     StrongARM processors
25688               -mxscale                XScale processors
25689               -m[arm]v[2345[t[e]]]    Arm architectures
25690               -mall                   All (except the ARM1)
25691       FP variants:
25692               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
25693               -mfpe-old               (No float load/store multiples)
25694               -mvfpxd                 VFP Single precision
25695               -mvfp                   All VFP
25696               -mno-fpu                Disable all floating point instructions
25697
25698       The following CPU names are recognized:
25699               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
25700               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
25701               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
25702               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
25703               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
25704               arm10t arm10e, arm1020t, arm1020e, arm10200e,
25705               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
25706
25707       */
25708
25709 const char * md_shortopts = "m:k";
25710
25711 #ifdef ARM_BI_ENDIAN
25712 #define OPTION_EB (OPTION_MD_BASE + 0)
25713 #define OPTION_EL (OPTION_MD_BASE + 1)
25714 #else
25715 #if TARGET_BYTES_BIG_ENDIAN
25716 #define OPTION_EB (OPTION_MD_BASE + 0)
25717 #else
25718 #define OPTION_EL (OPTION_MD_BASE + 1)
25719 #endif
25720 #endif
25721 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
25722 #define OPTION_FDPIC (OPTION_MD_BASE + 3)
25723
25724 struct option md_longopts[] =
25725 {
25726 #ifdef OPTION_EB
25727   {"EB", no_argument, NULL, OPTION_EB},
25728 #endif
25729 #ifdef OPTION_EL
25730   {"EL", no_argument, NULL, OPTION_EL},
25731 #endif
25732   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
25733 #ifdef OBJ_ELF
25734   {"fdpic", no_argument, NULL, OPTION_FDPIC},
25735 #endif
25736   {NULL, no_argument, NULL, 0}
25737 };
25738
25739 size_t md_longopts_size = sizeof (md_longopts);
25740
25741 struct arm_option_table
25742 {
25743   const char *  option;         /* Option name to match.  */
25744   const char *  help;           /* Help information.  */
25745   int *         var;            /* Variable to change.  */
25746   int           value;          /* What to change it to.  */
25747   const char *  deprecated;     /* If non-null, print this message.  */
25748 };
25749
25750 struct arm_option_table arm_opts[] =
25751 {
25752   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
25753   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
25754   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
25755    &support_interwork, 1, NULL},
25756   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
25757   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
25758   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
25759    1, NULL},
25760   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
25761   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
25762   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
25763   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
25764    NULL},
25765
25766   /* These are recognized by the assembler, but have no affect on code.  */
25767   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
25768   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
25769
25770   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
25771   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
25772    &warn_on_deprecated, 0, NULL},
25773   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
25774   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
25775   {NULL, NULL, NULL, 0, NULL}
25776 };
25777
25778 struct arm_legacy_option_table
25779 {
25780   const char *              option;             /* Option name to match.  */
25781   const arm_feature_set **  var;                /* Variable to change.  */
25782   const arm_feature_set     value;              /* What to change it to.  */
25783   const char *              deprecated;         /* If non-null, print this message.  */
25784 };
25785
25786 const struct arm_legacy_option_table arm_legacy_opts[] =
25787 {
25788   /* DON'T add any new processors to this list -- we want the whole list
25789      to go away...  Add them to the processors table instead.  */
25790   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25791   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25792   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25793   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25794   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25795   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25796   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25797   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25798   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25799   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25800   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25801   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25802   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25803   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25804   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25805   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25806   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25807   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25808   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25809   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25810   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25811   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25812   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25813   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25814   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25815   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25816   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25817   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25818   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25819   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25820   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25821   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25822   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25823   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25824   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25825   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25826   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25827   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25828   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25829   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25830   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25831   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25832   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25833   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25834   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25835   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25836   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25837   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25838   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25839   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25840   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25841   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25842   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25843   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25844   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25845   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25846   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25847   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25848   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25849   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25850   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25851   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25852   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25853   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25854   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25855   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25856   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25857   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25858   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
25859   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
25860    N_("use -mcpu=strongarm110")},
25861   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
25862    N_("use -mcpu=strongarm1100")},
25863   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
25864    N_("use -mcpu=strongarm1110")},
25865   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
25866   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
25867   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
25868
25869   /* Architecture variants -- don't add any more to this list either.  */
25870   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25871   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25872   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25873   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25874   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25875   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25876   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25877   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25878   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25879   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25880   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25881   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25882   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25883   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25884   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25885   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25886   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25887   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25888
25889   /* Floating point variants -- don't add any more to this list either.  */
25890   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
25891   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
25892   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
25893   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
25894    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
25895
25896   {NULL, NULL, ARM_ARCH_NONE, NULL}
25897 };
25898
25899 struct arm_cpu_option_table
25900 {
25901   const char *           name;
25902   size_t                 name_len;
25903   const arm_feature_set  value;
25904   const arm_feature_set  ext;
25905   /* For some CPUs we assume an FPU unless the user explicitly sets
25906      -mfpu=...  */
25907   const arm_feature_set  default_fpu;
25908   /* The canonical name of the CPU, or NULL to use NAME converted to upper
25909      case.  */
25910   const char *           canonical_name;
25911 };
25912
25913 /* This list should, at a minimum, contain all the cpu names
25914    recognized by GCC.  */
25915 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
25916
25917 static const struct arm_cpu_option_table arm_cpus[] =
25918 {
25919   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
25920                ARM_ARCH_NONE,
25921                FPU_ARCH_FPA),
25922   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
25923                ARM_ARCH_NONE,
25924                FPU_ARCH_FPA),
25925   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
25926                ARM_ARCH_NONE,
25927                FPU_ARCH_FPA),
25928   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
25929                ARM_ARCH_NONE,
25930                FPU_ARCH_FPA),
25931   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
25932                ARM_ARCH_NONE,
25933                FPU_ARCH_FPA),
25934   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
25935                ARM_ARCH_NONE,
25936                FPU_ARCH_FPA),
25937   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
25938                ARM_ARCH_NONE,
25939                FPU_ARCH_FPA),
25940   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
25941                ARM_ARCH_NONE,
25942                FPU_ARCH_FPA),
25943   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
25944                ARM_ARCH_NONE,
25945                FPU_ARCH_FPA),
25946   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
25947                ARM_ARCH_NONE,
25948                FPU_ARCH_FPA),
25949   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
25950                ARM_ARCH_NONE,
25951                FPU_ARCH_FPA),
25952   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
25953                ARM_ARCH_NONE,
25954                FPU_ARCH_FPA),
25955   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
25956                ARM_ARCH_NONE,
25957                FPU_ARCH_FPA),
25958   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
25959                ARM_ARCH_NONE,
25960                FPU_ARCH_FPA),
25961   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
25962                ARM_ARCH_NONE,
25963                FPU_ARCH_FPA),
25964   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
25965                ARM_ARCH_NONE,
25966                FPU_ARCH_FPA),
25967   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
25968                ARM_ARCH_NONE,
25969                FPU_ARCH_FPA),
25970   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
25971                ARM_ARCH_NONE,
25972                FPU_ARCH_FPA),
25973   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
25974                ARM_ARCH_NONE,
25975                FPU_ARCH_FPA),
25976   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
25977                ARM_ARCH_NONE,
25978                FPU_ARCH_FPA),
25979   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
25980                ARM_ARCH_NONE,
25981                FPU_ARCH_FPA),
25982   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
25983                ARM_ARCH_NONE,
25984                FPU_ARCH_FPA),
25985   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
25986                ARM_ARCH_NONE,
25987                FPU_ARCH_FPA),
25988   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
25989                ARM_ARCH_NONE,
25990                FPU_ARCH_FPA),
25991   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
25992                ARM_ARCH_NONE,
25993                FPU_ARCH_FPA),
25994   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
25995                ARM_ARCH_NONE,
25996                FPU_ARCH_FPA),
25997   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
25998                ARM_ARCH_NONE,
25999                FPU_ARCH_FPA),
26000   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
26001                ARM_ARCH_NONE,
26002                FPU_ARCH_FPA),
26003   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
26004                ARM_ARCH_NONE,
26005                FPU_ARCH_FPA),
26006   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
26007                ARM_ARCH_NONE,
26008                FPU_ARCH_FPA),
26009   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
26010                ARM_ARCH_NONE,
26011                FPU_ARCH_FPA),
26012   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
26013                ARM_ARCH_NONE,
26014                FPU_ARCH_FPA),
26015   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
26016                ARM_ARCH_NONE,
26017                FPU_ARCH_FPA),
26018   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
26019                ARM_ARCH_NONE,
26020                FPU_ARCH_FPA),
26021   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
26022                ARM_ARCH_NONE,
26023                FPU_ARCH_FPA),
26024   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
26025                ARM_ARCH_NONE,
26026                FPU_ARCH_FPA),
26027   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
26028                ARM_ARCH_NONE,
26029                FPU_ARCH_FPA),
26030   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
26031                ARM_ARCH_NONE,
26032                FPU_ARCH_FPA),
26033   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
26034                ARM_ARCH_NONE,
26035                FPU_ARCH_FPA),
26036   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
26037                ARM_ARCH_NONE,
26038                FPU_ARCH_FPA),
26039   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
26040                ARM_ARCH_NONE,
26041                FPU_ARCH_FPA),
26042   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
26043                ARM_ARCH_NONE,
26044                FPU_ARCH_FPA),
26045   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
26046                ARM_ARCH_NONE,
26047                FPU_ARCH_FPA),
26048   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
26049                ARM_ARCH_NONE,
26050                FPU_ARCH_FPA),
26051   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
26052                ARM_ARCH_NONE,
26053                FPU_ARCH_FPA),
26054   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
26055                ARM_ARCH_NONE,
26056                FPU_ARCH_FPA),
26057
26058   /* For V5 or later processors we default to using VFP; but the user
26059      should really set the FPU type explicitly.  */
26060   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
26061                ARM_ARCH_NONE,
26062                FPU_ARCH_VFP_V2),
26063   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
26064                ARM_ARCH_NONE,
26065                FPU_ARCH_VFP_V2),
26066   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
26067                ARM_ARCH_NONE,
26068                FPU_ARCH_VFP_V2),
26069   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
26070                ARM_ARCH_NONE,
26071                FPU_ARCH_VFP_V2),
26072   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
26073                ARM_ARCH_NONE,
26074                FPU_ARCH_VFP_V2),
26075   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
26076                ARM_ARCH_NONE,
26077                FPU_ARCH_VFP_V2),
26078   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
26079                ARM_ARCH_NONE,
26080                FPU_ARCH_VFP_V2),
26081   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
26082                ARM_ARCH_NONE,
26083                FPU_ARCH_VFP_V2),
26084   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
26085                ARM_ARCH_NONE,
26086                FPU_ARCH_VFP_V2),
26087   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
26088                ARM_ARCH_NONE,
26089                FPU_ARCH_VFP_V2),
26090   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
26091                ARM_ARCH_NONE,
26092                FPU_ARCH_VFP_V2),
26093   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
26094                ARM_ARCH_NONE,
26095                FPU_ARCH_VFP_V2),
26096   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
26097                ARM_ARCH_NONE,
26098                FPU_ARCH_VFP_V1),
26099   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
26100                ARM_ARCH_NONE,
26101                FPU_ARCH_VFP_V1),
26102   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
26103                ARM_ARCH_NONE,
26104                FPU_ARCH_VFP_V2),
26105   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
26106                ARM_ARCH_NONE,
26107                FPU_ARCH_VFP_V2),
26108   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
26109                ARM_ARCH_NONE,
26110                FPU_ARCH_VFP_V1),
26111   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
26112                ARM_ARCH_NONE,
26113                FPU_ARCH_VFP_V2),
26114   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
26115                ARM_ARCH_NONE,
26116                FPU_ARCH_VFP_V2),
26117   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
26118                ARM_ARCH_NONE,
26119                FPU_ARCH_VFP_V2),
26120   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
26121                ARM_ARCH_NONE,
26122                FPU_ARCH_VFP_V2),
26123   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
26124                ARM_ARCH_NONE,
26125                FPU_ARCH_VFP_V2),
26126   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
26127                ARM_ARCH_NONE,
26128                FPU_ARCH_VFP_V2),
26129   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
26130                ARM_ARCH_NONE,
26131                FPU_ARCH_VFP_V2),
26132   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
26133                ARM_ARCH_NONE,
26134                FPU_ARCH_VFP_V2),
26135   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
26136                ARM_ARCH_NONE,
26137                FPU_ARCH_VFP_V2),
26138   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
26139                ARM_ARCH_NONE,
26140                FPU_NONE),
26141   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
26142                ARM_ARCH_NONE,
26143                FPU_NONE),
26144   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
26145                ARM_ARCH_NONE,
26146                FPU_ARCH_VFP_V2),
26147   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
26148                ARM_ARCH_NONE,
26149                FPU_ARCH_VFP_V2),
26150   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
26151                ARM_ARCH_NONE,
26152                FPU_ARCH_VFP_V2),
26153   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
26154                ARM_ARCH_NONE,
26155                FPU_NONE),
26156   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
26157                ARM_ARCH_NONE,
26158                FPU_NONE),
26159   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
26160                ARM_ARCH_NONE,
26161                FPU_ARCH_VFP_V2),
26162   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
26163                ARM_ARCH_NONE,
26164                FPU_NONE),
26165   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
26166                ARM_ARCH_NONE,
26167                FPU_ARCH_VFP_V2),
26168   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
26169                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26170                FPU_NONE),
26171   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
26172                ARM_ARCH_NONE,
26173                FPU_ARCH_NEON_VFP_V4),
26174   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
26175                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26176                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
26177   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
26178                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26179                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
26180   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
26181                ARM_ARCH_NONE,
26182                FPU_ARCH_NEON_VFP_V4),
26183   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
26184                ARM_ARCH_NONE,
26185                FPU_ARCH_NEON_VFP_V4),
26186   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
26187                ARM_ARCH_NONE,
26188                FPU_ARCH_NEON_VFP_V4),
26189   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
26190                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26191                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26192   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
26193                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26194                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26195   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
26196                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26197                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26198   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
26199                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26200                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26201   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
26202                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26203                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26204   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
26205               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26206               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26207   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
26208               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26209               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26210   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
26211                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26212                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26213   ARM_CPU_OPT ("cortex-a76",    "Cortex-A76",          ARM_ARCH_V8_2A,
26214                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26215                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26216   ARM_CPU_OPT ("ares",    "Ares",              ARM_ARCH_V8_2A,
26217                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26218                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26219   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
26220                ARM_ARCH_NONE,
26221                FPU_NONE),
26222   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
26223                ARM_ARCH_NONE,
26224                FPU_ARCH_VFP_V3D16),
26225   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
26226                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26227                FPU_NONE),
26228   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
26229                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26230                FPU_ARCH_VFP_V3D16),
26231   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
26232                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26233                FPU_ARCH_VFP_V3D16),
26234   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
26235               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26236               FPU_ARCH_NEON_VFP_ARMV8),
26237   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
26238                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26239                FPU_NONE),
26240   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
26241                ARM_ARCH_NONE,
26242                FPU_NONE),
26243   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
26244                ARM_ARCH_NONE,
26245                FPU_NONE),
26246   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
26247                ARM_ARCH_NONE,
26248                FPU_NONE),
26249   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
26250                ARM_ARCH_NONE,
26251                FPU_NONE),
26252   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
26253                ARM_ARCH_NONE,
26254                FPU_NONE),
26255   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
26256                ARM_ARCH_NONE,
26257                FPU_NONE),
26258   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
26259                ARM_ARCH_NONE,
26260                FPU_NONE),
26261   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
26262                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26263                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26264   ARM_CPU_OPT ("neoverse-n1",    "Neoverse N1",        ARM_ARCH_V8_2A,
26265                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26266                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26267   /* ??? XSCALE is really an architecture.  */
26268   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
26269                ARM_ARCH_NONE,
26270                FPU_ARCH_VFP_V2),
26271
26272   /* ??? iwmmxt is not a processor.  */
26273   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
26274                ARM_ARCH_NONE,
26275                FPU_ARCH_VFP_V2),
26276   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
26277                ARM_ARCH_NONE,
26278                FPU_ARCH_VFP_V2),
26279   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
26280                ARM_ARCH_NONE,
26281                FPU_ARCH_VFP_V2),
26282
26283   /* Maverick.  */
26284   ARM_CPU_OPT ("ep9312",          "ARM920T",
26285                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
26286                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
26287
26288   /* Marvell processors.  */
26289   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
26290                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26291                FPU_ARCH_VFP_V3D16),
26292   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
26293                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26294                FPU_ARCH_NEON_VFP_V4),
26295
26296   /* APM X-Gene family.  */
26297   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
26298                ARM_ARCH_NONE,
26299                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26300   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
26301                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26302                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26303
26304   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
26305 };
26306 #undef ARM_CPU_OPT
26307
26308 struct arm_ext_table
26309 {
26310   const char *            name;
26311   size_t                  name_len;
26312   const arm_feature_set   merge;
26313   const arm_feature_set   clear;
26314 };
26315
26316 struct arm_arch_option_table
26317 {
26318   const char *                  name;
26319   size_t                        name_len;
26320   const arm_feature_set         value;
26321   const arm_feature_set         default_fpu;
26322   const struct arm_ext_table *  ext_table;
26323 };
26324
26325 /* Used to add support for +E and +noE extension.  */
26326 #define ARM_EXT(E, M, C) { E, sizeof (E) - 1, M, C }
26327 /* Used to add support for a +E extension.  */
26328 #define ARM_ADD(E, M) { E, sizeof(E) - 1, M, ARM_ARCH_NONE }
26329 /* Used to add support for a +noE extension.  */
26330 #define ARM_REMOVE(E, C) { E, sizeof(E) -1, ARM_ARCH_NONE, C }
26331
26332 #define ALL_FP ARM_FEATURE (0, ARM_EXT2_FP16_INST | ARM_EXT2_FP16_FML, \
26333                             ~0 & ~FPU_ENDIAN_PURE)
26334
26335 static const struct arm_ext_table armv5te_ext_table[] =
26336 {
26337   ARM_EXT ("fp", FPU_ARCH_VFP_V2, ALL_FP),
26338   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26339 };
26340
26341 static const struct arm_ext_table armv7_ext_table[] =
26342 {
26343   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
26344   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26345 };
26346
26347 static const struct arm_ext_table armv7ve_ext_table[] =
26348 {
26349   ARM_EXT ("fp", FPU_ARCH_VFP_V4D16, ALL_FP),
26350   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16),
26351   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
26352   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
26353   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
26354   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),  /* Alias for +fp.  */
26355   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
26356
26357   ARM_EXT ("simd", FPU_ARCH_NEON_VFP_V4,
26358            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
26359
26360   /* Aliases for +simd.  */
26361   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
26362
26363   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
26364   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
26365   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
26366
26367   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26368 };
26369
26370 static const struct arm_ext_table armv7a_ext_table[] =
26371 {
26372   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
26373   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
26374   ARM_ADD ("vfpv3", FPU_ARCH_VFP_V3),
26375   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
26376   ARM_ADD ("vfpv3-fp16", FPU_ARCH_VFP_V3_FP16),
26377   ARM_ADD ("vfpv4-d16", FPU_ARCH_VFP_V4D16),
26378   ARM_ADD ("vfpv4", FPU_ARCH_VFP_V4),
26379
26380   ARM_EXT ("simd", FPU_ARCH_VFP_V3_PLUS_NEON_V1,
26381            ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_NEON_EXT_FMA)),
26382
26383   /* Aliases for +simd.  */
26384   ARM_ADD ("neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
26385   ARM_ADD ("neon-vfpv3", FPU_ARCH_VFP_V3_PLUS_NEON_V1),
26386
26387   ARM_ADD ("neon-fp16", FPU_ARCH_NEON_FP16),
26388   ARM_ADD ("neon-vfpv4", FPU_ARCH_NEON_VFP_V4),
26389
26390   ARM_ADD ("mp", ARM_FEATURE_CORE_LOW (ARM_EXT_MP)),
26391   ARM_ADD ("sec", ARM_FEATURE_CORE_LOW (ARM_EXT_SEC)),
26392   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26393 };
26394
26395 static const struct arm_ext_table armv7r_ext_table[] =
26396 {
26397   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V3xD),
26398   ARM_ADD ("vfpv3xd", FPU_ARCH_VFP_V3xD), /* Alias for +fp.sp.  */
26399   ARM_EXT ("fp", FPU_ARCH_VFP_V3D16, ALL_FP),
26400   ARM_ADD ("vfpv3-d16", FPU_ARCH_VFP_V3D16), /* Alias for +fp.  */
26401   ARM_ADD ("vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16),
26402   ARM_ADD ("vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16),
26403   ARM_EXT ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
26404            ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV)),
26405   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26406 };
26407
26408 static const struct arm_ext_table armv7em_ext_table[] =
26409 {
26410   ARM_EXT ("fp", FPU_ARCH_VFP_V4_SP_D16, ALL_FP),
26411   /* Alias for +fp, used to be known as fpv4-sp-d16.  */
26412   ARM_ADD ("vfpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16),
26413   ARM_ADD ("fpv5", FPU_ARCH_VFP_V5_SP_D16),
26414   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
26415   ARM_ADD ("fpv5-d16", FPU_ARCH_VFP_V5D16),
26416   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26417 };
26418
26419 static const struct arm_ext_table armv8a_ext_table[] =
26420 {
26421   ARM_ADD ("crc", ARCH_CRC_ARMV8),
26422   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
26423   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
26424            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26425
26426   /* Armv8-a does not allow an FP implementation without SIMD, so the user
26427      should use the +simd option to turn on FP.  */
26428   ARM_REMOVE ("fp", ALL_FP),
26429   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
26430   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
26431   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26432 };
26433
26434
26435 static const struct arm_ext_table armv81a_ext_table[] =
26436 {
26437   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
26438   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
26439            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26440
26441   /* Armv8-a does not allow an FP implementation without SIMD, so the user
26442      should use the +simd option to turn on FP.  */
26443   ARM_REMOVE ("fp", ALL_FP),
26444   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
26445   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
26446   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26447 };
26448
26449 static const struct arm_ext_table armv82a_ext_table[] =
26450 {
26451   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8_1),
26452   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_2_FP16),
26453   ARM_ADD ("fp16fml", FPU_ARCH_NEON_VFP_ARMV8_2_FP16FML),
26454   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1,
26455            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26456   ARM_ADD ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
26457
26458   /* Armv8-a does not allow an FP implementation without SIMD, so the user
26459      should use the +simd option to turn on FP.  */
26460   ARM_REMOVE ("fp", ALL_FP),
26461   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
26462   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
26463   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26464 };
26465
26466 static const struct arm_ext_table armv84a_ext_table[] =
26467 {
26468   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
26469   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
26470   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
26471            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26472
26473   /* Armv8-a does not allow an FP implementation without SIMD, so the user
26474      should use the +simd option to turn on FP.  */
26475   ARM_REMOVE ("fp", ALL_FP),
26476   ARM_ADD ("sb", ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB)),
26477   ARM_ADD ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES)),
26478   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26479 };
26480
26481 static const struct arm_ext_table armv85a_ext_table[] =
26482 {
26483   ARM_ADD ("simd", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8),
26484   ARM_ADD ("fp16", FPU_ARCH_NEON_VFP_ARMV8_4_FP16FML),
26485   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_4,
26486            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26487
26488   /* Armv8-a does not allow an FP implementation without SIMD, so the user
26489      should use the +simd option to turn on FP.  */
26490   ARM_REMOVE ("fp", ALL_FP),
26491   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26492 };
26493
26494 static const struct arm_ext_table armv8m_main_ext_table[] =
26495 {
26496   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26497                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
26498   ARM_EXT ("fp", FPU_ARCH_VFP_V5_SP_D16, ALL_FP),
26499   ARM_ADD ("fp.dp", FPU_ARCH_VFP_V5D16),
26500   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26501 };
26502
26503 static const struct arm_ext_table armv8_1m_main_ext_table[] =
26504 {
26505   ARM_EXT ("dsp", ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26506                   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP)),
26507   ARM_EXT ("fp",
26508            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
26509                         FPU_VFP_V5_SP_D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA),
26510            ALL_FP),
26511   ARM_ADD ("fp.dp",
26512            ARM_FEATURE (0, ARM_EXT2_FP16_INST,
26513                         FPU_VFP_V5D16 | FPU_VFP_EXT_FP16 | FPU_VFP_EXT_FMA)),
26514   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26515 };
26516
26517 static const struct arm_ext_table armv8r_ext_table[] =
26518 {
26519   ARM_ADD ("crc", ARCH_CRC_ARMV8),
26520   ARM_ADD ("simd", FPU_ARCH_NEON_VFP_ARMV8),
26521   ARM_EXT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
26522            ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8)),
26523   ARM_REMOVE ("fp", ALL_FP),
26524   ARM_ADD ("fp.sp", FPU_ARCH_VFP_V5_SP_D16),
26525   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26526 };
26527
26528 /* This list should, at a minimum, contain all the architecture names
26529    recognized by GCC.  */
26530 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF, NULL }
26531 #define ARM_ARCH_OPT2(N, V, DF, ext) \
26532   { N, sizeof (N) - 1, V, DF, ext##_ext_table }
26533
26534 static const struct arm_arch_option_table arm_archs[] =
26535 {
26536   ARM_ARCH_OPT ("all",            ARM_ANY,              FPU_ARCH_FPA),
26537   ARM_ARCH_OPT ("armv1",          ARM_ARCH_V1,          FPU_ARCH_FPA),
26538   ARM_ARCH_OPT ("armv2",          ARM_ARCH_V2,          FPU_ARCH_FPA),
26539   ARM_ARCH_OPT ("armv2a",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
26540   ARM_ARCH_OPT ("armv2s",         ARM_ARCH_V2S,         FPU_ARCH_FPA),
26541   ARM_ARCH_OPT ("armv3",          ARM_ARCH_V3,          FPU_ARCH_FPA),
26542   ARM_ARCH_OPT ("armv3m",         ARM_ARCH_V3M,         FPU_ARCH_FPA),
26543   ARM_ARCH_OPT ("armv4",          ARM_ARCH_V4,          FPU_ARCH_FPA),
26544   ARM_ARCH_OPT ("armv4xm",        ARM_ARCH_V4xM,        FPU_ARCH_FPA),
26545   ARM_ARCH_OPT ("armv4t",         ARM_ARCH_V4T,         FPU_ARCH_FPA),
26546   ARM_ARCH_OPT ("armv4txm",       ARM_ARCH_V4TxM,       FPU_ARCH_FPA),
26547   ARM_ARCH_OPT ("armv5",          ARM_ARCH_V5,          FPU_ARCH_VFP),
26548   ARM_ARCH_OPT ("armv5t",         ARM_ARCH_V5T,         FPU_ARCH_VFP),
26549   ARM_ARCH_OPT ("armv5txm",       ARM_ARCH_V5TxM,       FPU_ARCH_VFP),
26550   ARM_ARCH_OPT2 ("armv5te",       ARM_ARCH_V5TE,        FPU_ARCH_VFP,   armv5te),
26551   ARM_ARCH_OPT2 ("armv5texp",     ARM_ARCH_V5TExP,      FPU_ARCH_VFP, armv5te),
26552   ARM_ARCH_OPT2 ("armv5tej",      ARM_ARCH_V5TEJ,       FPU_ARCH_VFP,   armv5te),
26553   ARM_ARCH_OPT2 ("armv6",         ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
26554   ARM_ARCH_OPT2 ("armv6j",        ARM_ARCH_V6,          FPU_ARCH_VFP,   armv5te),
26555   ARM_ARCH_OPT2 ("armv6k",        ARM_ARCH_V6K,         FPU_ARCH_VFP,   armv5te),
26556   ARM_ARCH_OPT2 ("armv6z",        ARM_ARCH_V6Z,         FPU_ARCH_VFP,   armv5te),
26557   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
26558      kept to preserve existing behaviour.  */
26559   ARM_ARCH_OPT2 ("armv6kz",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
26560   ARM_ARCH_OPT2 ("armv6zk",       ARM_ARCH_V6KZ,        FPU_ARCH_VFP,   armv5te),
26561   ARM_ARCH_OPT2 ("armv6t2",       ARM_ARCH_V6T2,        FPU_ARCH_VFP,   armv5te),
26562   ARM_ARCH_OPT2 ("armv6kt2",      ARM_ARCH_V6KT2,       FPU_ARCH_VFP,   armv5te),
26563   ARM_ARCH_OPT2 ("armv6zt2",      ARM_ARCH_V6ZT2,       FPU_ARCH_VFP,   armv5te),
26564   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
26565      kept to preserve existing behaviour.  */
26566   ARM_ARCH_OPT2 ("armv6kzt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
26567   ARM_ARCH_OPT2 ("armv6zkt2",     ARM_ARCH_V6KZT2,      FPU_ARCH_VFP,   armv5te),
26568   ARM_ARCH_OPT ("armv6-m",        ARM_ARCH_V6M,         FPU_ARCH_VFP),
26569   ARM_ARCH_OPT ("armv6s-m",       ARM_ARCH_V6SM,        FPU_ARCH_VFP),
26570   ARM_ARCH_OPT2 ("armv7",         ARM_ARCH_V7,          FPU_ARCH_VFP, armv7),
26571   /* The official spelling of the ARMv7 profile variants is the dashed form.
26572      Accept the non-dashed form for compatibility with old toolchains.  */
26573   ARM_ARCH_OPT2 ("armv7a",        ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
26574   ARM_ARCH_OPT2 ("armv7ve",       ARM_ARCH_V7VE,        FPU_ARCH_VFP, armv7ve),
26575   ARM_ARCH_OPT2 ("armv7r",        ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
26576   ARM_ARCH_OPT ("armv7m",         ARM_ARCH_V7M,         FPU_ARCH_VFP),
26577   ARM_ARCH_OPT2 ("armv7-a",       ARM_ARCH_V7A,         FPU_ARCH_VFP, armv7a),
26578   ARM_ARCH_OPT2 ("armv7-r",       ARM_ARCH_V7R,         FPU_ARCH_VFP, armv7r),
26579   ARM_ARCH_OPT ("armv7-m",        ARM_ARCH_V7M,         FPU_ARCH_VFP),
26580   ARM_ARCH_OPT2 ("armv7e-m",      ARM_ARCH_V7EM,        FPU_ARCH_VFP, armv7em),
26581   ARM_ARCH_OPT ("armv8-m.base",   ARM_ARCH_V8M_BASE,    FPU_ARCH_VFP),
26582   ARM_ARCH_OPT2 ("armv8-m.main",  ARM_ARCH_V8M_MAIN,    FPU_ARCH_VFP,
26583                  armv8m_main),
26584   ARM_ARCH_OPT2 ("armv8.1-m.main", ARM_ARCH_V8_1M_MAIN, FPU_ARCH_VFP,
26585                  armv8_1m_main),
26586   ARM_ARCH_OPT2 ("armv8-a",       ARM_ARCH_V8A,         FPU_ARCH_VFP, armv8a),
26587   ARM_ARCH_OPT2 ("armv8.1-a",     ARM_ARCH_V8_1A,       FPU_ARCH_VFP, armv81a),
26588   ARM_ARCH_OPT2 ("armv8.2-a",     ARM_ARCH_V8_2A,       FPU_ARCH_VFP, armv82a),
26589   ARM_ARCH_OPT2 ("armv8.3-a",     ARM_ARCH_V8_3A,       FPU_ARCH_VFP, armv82a),
26590   ARM_ARCH_OPT2 ("armv8-r",       ARM_ARCH_V8R,         FPU_ARCH_VFP, armv8r),
26591   ARM_ARCH_OPT2 ("armv8.4-a",     ARM_ARCH_V8_4A,       FPU_ARCH_VFP, armv84a),
26592   ARM_ARCH_OPT2 ("armv8.5-a",     ARM_ARCH_V8_5A,       FPU_ARCH_VFP, armv85a),
26593   ARM_ARCH_OPT ("xscale",         ARM_ARCH_XSCALE,      FPU_ARCH_VFP),
26594   ARM_ARCH_OPT ("iwmmxt",         ARM_ARCH_IWMMXT,      FPU_ARCH_VFP),
26595   ARM_ARCH_OPT ("iwmmxt2",        ARM_ARCH_IWMMXT2,     FPU_ARCH_VFP),
26596   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
26597 };
26598 #undef ARM_ARCH_OPT
26599
26600 /* ISA extensions in the co-processor and main instruction set space.  */
26601
26602 struct arm_option_extension_value_table
26603 {
26604   const char *           name;
26605   size_t                 name_len;
26606   const arm_feature_set  merge_value;
26607   const arm_feature_set  clear_value;
26608   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
26609      indicates that an extension is available for all architectures while
26610      ARM_ANY marks an empty entry.  */
26611   const arm_feature_set  allowed_archs[2];
26612 };
26613
26614 /* The following table must be in alphabetical order with a NULL last entry.  */
26615
26616 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
26617 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
26618
26619 /* DEPRECATED: Refrain from using this table to add any new extensions, instead
26620    use the context sensitive approach using arm_ext_table's.  */
26621 static const struct arm_option_extension_value_table arm_extensions[] =
26622 {
26623   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26624                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26625   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
26626                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
26627                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26628   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
26629                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
26630                           ARM_ARCH_V8_2A),
26631   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26632                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26633                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
26634   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
26635                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26636   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26637                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26638                         ARM_ARCH_V8_2A),
26639   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
26640                                                   | ARM_EXT2_FP16_FML),
26641                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
26642                                                   | ARM_EXT2_FP16_FML),
26643                            ARM_ARCH_V8_2A),
26644   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
26645                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
26646                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
26647                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
26648   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
26649      Thumb divide instruction.  Due to this having the same name as the
26650      previous entry, this will be ignored when doing command-line parsing and
26651      only considered by build attribute selection code.  */
26652   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
26653                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
26654                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
26655   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
26656                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
26657   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
26658                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
26659   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
26660                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
26661   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
26662                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
26663                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
26664                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
26665   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
26666                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
26667                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
26668   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
26669                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
26670                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26671   ARM_EXT_OPT ("predres", ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
26672                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_PREDRES),
26673                         ARM_ARCH_V8A),
26674   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
26675                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
26676                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26677   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
26678                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
26679                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26680   ARM_EXT_OPT ("sb",    ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
26681                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_SB),
26682                         ARM_ARCH_V8A),
26683   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26684                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26685                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
26686                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
26687   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
26688                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
26689                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26690   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
26691                                      | ARM_EXT_DIV),
26692                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
26693                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
26694   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
26695                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
26696   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
26697 };
26698 #undef ARM_EXT_OPT
26699
26700 /* ISA floating-point and Advanced SIMD extensions.  */
26701 struct arm_option_fpu_value_table
26702 {
26703   const char *           name;
26704   const arm_feature_set  value;
26705 };
26706
26707 /* This list should, at a minimum, contain all the fpu names
26708    recognized by GCC.  */
26709 static const struct arm_option_fpu_value_table arm_fpus[] =
26710 {
26711   {"softfpa",           FPU_NONE},
26712   {"fpe",               FPU_ARCH_FPE},
26713   {"fpe2",              FPU_ARCH_FPE},
26714   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
26715   {"fpa",               FPU_ARCH_FPA},
26716   {"fpa10",             FPU_ARCH_FPA},
26717   {"fpa11",             FPU_ARCH_FPA},
26718   {"arm7500fe",         FPU_ARCH_FPA},
26719   {"softvfp",           FPU_ARCH_VFP},
26720   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
26721   {"vfp",               FPU_ARCH_VFP_V2},
26722   {"vfp9",              FPU_ARCH_VFP_V2},
26723   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
26724   {"vfp10",             FPU_ARCH_VFP_V2},
26725   {"vfp10-r0",          FPU_ARCH_VFP_V1},
26726   {"vfpxd",             FPU_ARCH_VFP_V1xD},
26727   {"vfpv2",             FPU_ARCH_VFP_V2},
26728   {"vfpv3",             FPU_ARCH_VFP_V3},
26729   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
26730   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
26731   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
26732   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
26733   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
26734   {"arm1020t",          FPU_ARCH_VFP_V1},
26735   {"arm1020e",          FPU_ARCH_VFP_V2},
26736   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
26737   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
26738   {"maverick",          FPU_ARCH_MAVERICK},
26739   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
26740   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
26741   {"neon-fp16",         FPU_ARCH_NEON_FP16},
26742   {"vfpv4",             FPU_ARCH_VFP_V4},
26743   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
26744   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
26745   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
26746   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
26747   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
26748   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
26749   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
26750   {"crypto-neon-fp-armv8",
26751                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
26752   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
26753   {"crypto-neon-fp-armv8.1",
26754                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
26755   {NULL,                ARM_ARCH_NONE}
26756 };
26757
26758 struct arm_option_value_table
26759 {
26760   const char *name;
26761   long value;
26762 };
26763
26764 static const struct arm_option_value_table arm_float_abis[] =
26765 {
26766   {"hard",      ARM_FLOAT_ABI_HARD},
26767   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
26768   {"soft",      ARM_FLOAT_ABI_SOFT},
26769   {NULL,        0}
26770 };
26771
26772 #ifdef OBJ_ELF
26773 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
26774 static const struct arm_option_value_table arm_eabis[] =
26775 {
26776   {"gnu",       EF_ARM_EABI_UNKNOWN},
26777   {"4",         EF_ARM_EABI_VER4},
26778   {"5",         EF_ARM_EABI_VER5},
26779   {NULL,        0}
26780 };
26781 #endif
26782
26783 struct arm_long_option_table
26784 {
26785   const char * option;                  /* Substring to match.  */
26786   const char * help;                    /* Help information.  */
26787   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
26788   const char * deprecated;              /* If non-null, print this message.  */
26789 };
26790
26791 static bfd_boolean
26792 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
26793                      arm_feature_set *ext_set,
26794                      const struct arm_ext_table *ext_table)
26795 {
26796   /* We insist on extensions being specified in alphabetical order, and with
26797      extensions being added before being removed.  We achieve this by having
26798      the global ARM_EXTENSIONS table in alphabetical order, and using the
26799      ADDING_VALUE variable to indicate whether we are adding an extension (1)
26800      or removing it (0) and only allowing it to change in the order
26801      -1 -> 1 -> 0.  */
26802   const struct arm_option_extension_value_table * opt = NULL;
26803   const arm_feature_set arm_any = ARM_ANY;
26804   int adding_value = -1;
26805
26806   while (str != NULL && *str != 0)
26807     {
26808       const char *ext;
26809       size_t len;
26810
26811       if (*str != '+')
26812         {
26813           as_bad (_("invalid architectural extension"));
26814           return FALSE;
26815         }
26816
26817       str++;
26818       ext = strchr (str, '+');
26819
26820       if (ext != NULL)
26821         len = ext - str;
26822       else
26823         len = strlen (str);
26824
26825       if (len >= 2 && strncmp (str, "no", 2) == 0)
26826         {
26827           if (adding_value != 0)
26828             {
26829               adding_value = 0;
26830               opt = arm_extensions;
26831             }
26832
26833           len -= 2;
26834           str += 2;
26835         }
26836       else if (len > 0)
26837         {
26838           if (adding_value == -1)
26839             {
26840               adding_value = 1;
26841               opt = arm_extensions;
26842             }
26843           else if (adding_value != 1)
26844             {
26845               as_bad (_("must specify extensions to add before specifying "
26846                         "those to remove"));
26847               return FALSE;
26848             }
26849         }
26850
26851       if (len == 0)
26852         {
26853           as_bad (_("missing architectural extension"));
26854           return FALSE;
26855         }
26856
26857       gas_assert (adding_value != -1);
26858       gas_assert (opt != NULL);
26859
26860       if (ext_table != NULL)
26861         {
26862           const struct arm_ext_table * ext_opt = ext_table;
26863           bfd_boolean found = FALSE;
26864           for (; ext_opt->name != NULL; ext_opt++)
26865             if (ext_opt->name_len == len
26866                 && strncmp (ext_opt->name, str, len) == 0)
26867               {
26868                 if (adding_value)
26869                   {
26870                     if (ARM_FEATURE_ZERO (ext_opt->merge))
26871                         /* TODO: Option not supported.  When we remove the
26872                            legacy table this case should error out.  */
26873                         continue;
26874
26875                     ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, ext_opt->merge);
26876                   }
26877                 else
26878                   {
26879                     if (ARM_FEATURE_ZERO (ext_opt->clear))
26880                         /* TODO: Option not supported.  When we remove the
26881                            legacy table this case should error out.  */
26882                         continue;
26883                     ARM_CLEAR_FEATURE (*ext_set, *ext_set, ext_opt->clear);
26884                   }
26885                 found = TRUE;
26886                 break;
26887               }
26888           if (found)
26889             {
26890               str = ext;
26891               continue;
26892             }
26893         }
26894
26895       /* Scan over the options table trying to find an exact match. */
26896       for (; opt->name != NULL; opt++)
26897         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26898           {
26899             int i, nb_allowed_archs =
26900               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
26901             /* Check we can apply the extension to this architecture.  */
26902             for (i = 0; i < nb_allowed_archs; i++)
26903               {
26904                 /* Empty entry.  */
26905                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
26906                   continue;
26907                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
26908                   break;
26909               }
26910             if (i == nb_allowed_archs)
26911               {
26912                 as_bad (_("extension does not apply to the base architecture"));
26913                 return FALSE;
26914               }
26915
26916             /* Add or remove the extension.  */
26917             if (adding_value)
26918               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
26919             else
26920               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
26921
26922             /* Allowing Thumb division instructions for ARMv7 in autodetection
26923                rely on this break so that duplicate extensions (extensions
26924                with the same name as a previous extension in the list) are not
26925                considered for command-line parsing.  */
26926             break;
26927           }
26928
26929       if (opt->name == NULL)
26930         {
26931           /* Did we fail to find an extension because it wasn't specified in
26932              alphabetical order, or because it does not exist?  */
26933
26934           for (opt = arm_extensions; opt->name != NULL; opt++)
26935             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26936               break;
26937
26938           if (opt->name == NULL)
26939             as_bad (_("unknown architectural extension `%s'"), str);
26940           else
26941             as_bad (_("architectural extensions must be specified in "
26942                       "alphabetical order"));
26943
26944           return FALSE;
26945         }
26946       else
26947         {
26948           /* We should skip the extension we've just matched the next time
26949              round.  */
26950           opt++;
26951         }
26952
26953       str = ext;
26954     };
26955
26956   return TRUE;
26957 }
26958
26959 static bfd_boolean
26960 arm_parse_cpu (const char *str)
26961 {
26962   const struct arm_cpu_option_table *opt;
26963   const char *ext = strchr (str, '+');
26964   size_t len;
26965
26966   if (ext != NULL)
26967     len = ext - str;
26968   else
26969     len = strlen (str);
26970
26971   if (len == 0)
26972     {
26973       as_bad (_("missing cpu name `%s'"), str);
26974       return FALSE;
26975     }
26976
26977   for (opt = arm_cpus; opt->name != NULL; opt++)
26978     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26979       {
26980         mcpu_cpu_opt = &opt->value;
26981         if (mcpu_ext_opt == NULL)
26982           mcpu_ext_opt = XNEW (arm_feature_set);
26983         *mcpu_ext_opt = opt->ext;
26984         mcpu_fpu_opt = &opt->default_fpu;
26985         if (opt->canonical_name)
26986           {
26987             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
26988             strcpy (selected_cpu_name, opt->canonical_name);
26989           }
26990         else
26991           {
26992             size_t i;
26993
26994             if (len >= sizeof selected_cpu_name)
26995               len = (sizeof selected_cpu_name) - 1;
26996
26997             for (i = 0; i < len; i++)
26998               selected_cpu_name[i] = TOUPPER (opt->name[i]);
26999             selected_cpu_name[i] = 0;
27000           }
27001
27002         if (ext != NULL)
27003           return arm_parse_extension (ext, mcpu_cpu_opt, mcpu_ext_opt, NULL);
27004
27005         return TRUE;
27006       }
27007
27008   as_bad (_("unknown cpu `%s'"), str);
27009   return FALSE;
27010 }
27011
27012 static bfd_boolean
27013 arm_parse_arch (const char *str)
27014 {
27015   const struct arm_arch_option_table *opt;
27016   const char *ext = strchr (str, '+');
27017   size_t len;
27018
27019   if (ext != NULL)
27020     len = ext - str;
27021   else
27022     len = strlen (str);
27023
27024   if (len == 0)
27025     {
27026       as_bad (_("missing architecture name `%s'"), str);
27027       return FALSE;
27028     }
27029
27030   for (opt = arm_archs; opt->name != NULL; opt++)
27031     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
27032       {
27033         march_cpu_opt = &opt->value;
27034         if (march_ext_opt == NULL)
27035           march_ext_opt = XNEW (arm_feature_set);
27036         *march_ext_opt = arm_arch_none;
27037         march_fpu_opt = &opt->default_fpu;
27038         strcpy (selected_cpu_name, opt->name);
27039
27040         if (ext != NULL)
27041           return arm_parse_extension (ext, march_cpu_opt, march_ext_opt,
27042                                       opt->ext_table);
27043
27044         return TRUE;
27045       }
27046
27047   as_bad (_("unknown architecture `%s'\n"), str);
27048   return FALSE;
27049 }
27050
27051 static bfd_boolean
27052 arm_parse_fpu (const char * str)
27053 {
27054   const struct arm_option_fpu_value_table * opt;
27055
27056   for (opt = arm_fpus; opt->name != NULL; opt++)
27057     if (streq (opt->name, str))
27058       {
27059         mfpu_opt = &opt->value;
27060         return TRUE;
27061       }
27062
27063   as_bad (_("unknown floating point format `%s'\n"), str);
27064   return FALSE;
27065 }
27066
27067 static bfd_boolean
27068 arm_parse_float_abi (const char * str)
27069 {
27070   const struct arm_option_value_table * opt;
27071
27072   for (opt = arm_float_abis; opt->name != NULL; opt++)
27073     if (streq (opt->name, str))
27074       {
27075         mfloat_abi_opt = opt->value;
27076         return TRUE;
27077       }
27078
27079   as_bad (_("unknown floating point abi `%s'\n"), str);
27080   return FALSE;
27081 }
27082
27083 #ifdef OBJ_ELF
27084 static bfd_boolean
27085 arm_parse_eabi (const char * str)
27086 {
27087   const struct arm_option_value_table *opt;
27088
27089   for (opt = arm_eabis; opt->name != NULL; opt++)
27090     if (streq (opt->name, str))
27091       {
27092         meabi_flags = opt->value;
27093         return TRUE;
27094       }
27095   as_bad (_("unknown EABI `%s'\n"), str);
27096   return FALSE;
27097 }
27098 #endif
27099
27100 static bfd_boolean
27101 arm_parse_it_mode (const char * str)
27102 {
27103   bfd_boolean ret = TRUE;
27104
27105   if (streq ("arm", str))
27106     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
27107   else if (streq ("thumb", str))
27108     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
27109   else if (streq ("always", str))
27110     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
27111   else if (streq ("never", str))
27112     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
27113   else
27114     {
27115       as_bad (_("unknown implicit IT mode `%s', should be "\
27116                 "arm, thumb, always, or never."), str);
27117       ret = FALSE;
27118     }
27119
27120   return ret;
27121 }
27122
27123 static bfd_boolean
27124 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
27125 {
27126   codecomposer_syntax = TRUE;
27127   arm_comment_chars[0] = ';';
27128   arm_line_separator_chars[0] = 0;
27129   return TRUE;
27130 }
27131
27132 struct arm_long_option_table arm_long_opts[] =
27133 {
27134   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
27135    arm_parse_cpu, NULL},
27136   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
27137    arm_parse_arch, NULL},
27138   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
27139    arm_parse_fpu, NULL},
27140   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
27141    arm_parse_float_abi, NULL},
27142 #ifdef OBJ_ELF
27143   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
27144    arm_parse_eabi, NULL},
27145 #endif
27146   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
27147    arm_parse_it_mode, NULL},
27148   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
27149    arm_ccs_mode, NULL},
27150   {NULL, NULL, 0, NULL}
27151 };
27152
27153 int
27154 md_parse_option (int c, const char * arg)
27155 {
27156   struct arm_option_table *opt;
27157   const struct arm_legacy_option_table *fopt;
27158   struct arm_long_option_table *lopt;
27159
27160   switch (c)
27161     {
27162 #ifdef OPTION_EB
27163     case OPTION_EB:
27164       target_big_endian = 1;
27165       break;
27166 #endif
27167
27168 #ifdef OPTION_EL
27169     case OPTION_EL:
27170       target_big_endian = 0;
27171       break;
27172 #endif
27173
27174     case OPTION_FIX_V4BX:
27175       fix_v4bx = TRUE;
27176       break;
27177
27178 #ifdef OBJ_ELF
27179     case OPTION_FDPIC:
27180       arm_fdpic = TRUE;
27181       break;
27182 #endif /* OBJ_ELF */
27183
27184     case 'a':
27185       /* Listing option.  Just ignore these, we don't support additional
27186          ones.  */
27187       return 0;
27188
27189     default:
27190       for (opt = arm_opts; opt->option != NULL; opt++)
27191         {
27192           if (c == opt->option[0]
27193               && ((arg == NULL && opt->option[1] == 0)
27194                   || streq (arg, opt->option + 1)))
27195             {
27196               /* If the option is deprecated, tell the user.  */
27197               if (warn_on_deprecated && opt->deprecated != NULL)
27198                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
27199                            arg ? arg : "", _(opt->deprecated));
27200
27201               if (opt->var != NULL)
27202                 *opt->var = opt->value;
27203
27204               return 1;
27205             }
27206         }
27207
27208       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
27209         {
27210           if (c == fopt->option[0]
27211               && ((arg == NULL && fopt->option[1] == 0)
27212                   || streq (arg, fopt->option + 1)))
27213             {
27214               /* If the option is deprecated, tell the user.  */
27215               if (warn_on_deprecated && fopt->deprecated != NULL)
27216                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
27217                            arg ? arg : "", _(fopt->deprecated));
27218
27219               if (fopt->var != NULL)
27220                 *fopt->var = &fopt->value;
27221
27222               return 1;
27223             }
27224         }
27225
27226       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
27227         {
27228           /* These options are expected to have an argument.  */
27229           if (c == lopt->option[0]
27230               && arg != NULL
27231               && strncmp (arg, lopt->option + 1,
27232                           strlen (lopt->option + 1)) == 0)
27233             {
27234               /* If the option is deprecated, tell the user.  */
27235               if (warn_on_deprecated && lopt->deprecated != NULL)
27236                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
27237                            _(lopt->deprecated));
27238
27239               /* Call the sup-option parser.  */
27240               return lopt->func (arg + strlen (lopt->option) - 1);
27241             }
27242         }
27243
27244       return 0;
27245     }
27246
27247   return 1;
27248 }
27249
27250 void
27251 md_show_usage (FILE * fp)
27252 {
27253   struct arm_option_table *opt;
27254   struct arm_long_option_table *lopt;
27255
27256   fprintf (fp, _(" ARM-specific assembler options:\n"));
27257
27258   for (opt = arm_opts; opt->option != NULL; opt++)
27259     if (opt->help != NULL)
27260       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
27261
27262   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
27263     if (lopt->help != NULL)
27264       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
27265
27266 #ifdef OPTION_EB
27267   fprintf (fp, _("\
27268   -EB                     assemble code for a big-endian cpu\n"));
27269 #endif
27270
27271 #ifdef OPTION_EL
27272   fprintf (fp, _("\
27273   -EL                     assemble code for a little-endian cpu\n"));
27274 #endif
27275
27276   fprintf (fp, _("\
27277   --fix-v4bx              Allow BX in ARMv4 code\n"));
27278
27279 #ifdef OBJ_ELF
27280   fprintf (fp, _("\
27281   --fdpic                 generate an FDPIC object file\n"));
27282 #endif /* OBJ_ELF */
27283 }
27284
27285 #ifdef OBJ_ELF
27286
27287 typedef struct
27288 {
27289   int val;
27290   arm_feature_set flags;
27291 } cpu_arch_ver_table;
27292
27293 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
27294    chronologically for architectures, with an exception for ARMv6-M and
27295    ARMv6S-M due to legacy reasons.  No new architecture should have a
27296    special case.  This allows for build attribute selection results to be
27297    stable when new architectures are added.  */
27298 static const cpu_arch_ver_table cpu_arch_ver[] =
27299 {
27300     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V1},
27301     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2},
27302     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V2S},
27303     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3},
27304     {TAG_CPU_ARCH_PRE_V4,     ARM_ARCH_V3M},
27305     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4xM},
27306     {TAG_CPU_ARCH_V4,         ARM_ARCH_V4},
27307     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4TxM},
27308     {TAG_CPU_ARCH_V4T,        ARM_ARCH_V4T},
27309     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5xM},
27310     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5},
27311     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5TxM},
27312     {TAG_CPU_ARCH_V5T,        ARM_ARCH_V5T},
27313     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TExP},
27314     {TAG_CPU_ARCH_V5TE,       ARM_ARCH_V5TE},
27315     {TAG_CPU_ARCH_V5TEJ,      ARM_ARCH_V5TEJ},
27316     {TAG_CPU_ARCH_V6,         ARM_ARCH_V6},
27317     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6Z},
27318     {TAG_CPU_ARCH_V6KZ,       ARM_ARCH_V6KZ},
27319     {TAG_CPU_ARCH_V6K,        ARM_ARCH_V6K},
27320     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6T2},
27321     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KT2},
27322     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6ZT2},
27323     {TAG_CPU_ARCH_V6T2,       ARM_ARCH_V6KZT2},
27324
27325     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
27326        always selected build attributes to match those of ARMv6-M
27327        (resp. ARMv6S-M).  However, due to these architectures being a strict
27328        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
27329        would be selected when fully respecting chronology of architectures.
27330        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
27331        move them before ARMv7 architectures.  */
27332     {TAG_CPU_ARCH_V6_M,       ARM_ARCH_V6M},
27333     {TAG_CPU_ARCH_V6S_M,      ARM_ARCH_V6SM},
27334
27335     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7},
27336     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7A},
27337     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7R},
27338     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7M},
27339     {TAG_CPU_ARCH_V7,         ARM_ARCH_V7VE},
27340     {TAG_CPU_ARCH_V7E_M,      ARM_ARCH_V7EM},
27341     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8A},
27342     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_1A},
27343     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_2A},
27344     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_3A},
27345     {TAG_CPU_ARCH_V8M_BASE,   ARM_ARCH_V8M_BASE},
27346     {TAG_CPU_ARCH_V8M_MAIN,   ARM_ARCH_V8M_MAIN},
27347     {TAG_CPU_ARCH_V8R,        ARM_ARCH_V8R},
27348     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_4A},
27349     {TAG_CPU_ARCH_V8,         ARM_ARCH_V8_5A},
27350     {TAG_CPU_ARCH_V8_1M_MAIN, ARM_ARCH_V8_1M_MAIN},
27351     {-1,                      ARM_ARCH_NONE}
27352 };
27353
27354 /* Set an attribute if it has not already been set by the user.  */
27355
27356 static void
27357 aeabi_set_attribute_int (int tag, int value)
27358 {
27359   if (tag < 1
27360       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
27361       || !attributes_set_explicitly[tag])
27362     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
27363 }
27364
27365 static void
27366 aeabi_set_attribute_string (int tag, const char *value)
27367 {
27368   if (tag < 1
27369       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
27370       || !attributes_set_explicitly[tag])
27371     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
27372 }
27373
27374 /* Return whether features in the *NEEDED feature set are available via
27375    extensions for the architecture whose feature set is *ARCH_FSET.  */
27376
27377 static bfd_boolean
27378 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
27379                             const arm_feature_set *needed)
27380 {
27381   int i, nb_allowed_archs;
27382   arm_feature_set ext_fset;
27383   const struct arm_option_extension_value_table *opt;
27384
27385   ext_fset = arm_arch_none;
27386   for (opt = arm_extensions; opt->name != NULL; opt++)
27387     {
27388       /* Extension does not provide any feature we need.  */
27389       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
27390         continue;
27391
27392       nb_allowed_archs =
27393         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
27394       for (i = 0; i < nb_allowed_archs; i++)
27395         {
27396           /* Empty entry.  */
27397           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
27398             break;
27399
27400           /* Extension is available, add it.  */
27401           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
27402             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
27403         }
27404     }
27405
27406   /* Can we enable all features in *needed?  */
27407   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
27408 }
27409
27410 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
27411    a given architecture feature set *ARCH_EXT_FSET including extension feature
27412    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
27413    - if true, check for an exact match of the architecture modulo extensions;
27414    - otherwise, select build attribute value of the first superset
27415      architecture released so that results remains stable when new architectures
27416      are added.
27417    For -march/-mcpu=all the build attribute value of the most featureful
27418    architecture is returned.  Tag_CPU_arch_profile result is returned in
27419    PROFILE.  */
27420
27421 static int
27422 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
27423                               const arm_feature_set *ext_fset,
27424                               char *profile, int exact_match)
27425 {
27426   arm_feature_set arch_fset;
27427   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
27428
27429   /* Select most featureful architecture with all its extensions if building
27430      for -march=all as the feature sets used to set build attributes.  */
27431   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
27432     {
27433       /* Force revisiting of decision for each new architecture.  */
27434       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8_1M_MAIN);
27435       *profile = 'A';
27436       return TAG_CPU_ARCH_V8;
27437     }
27438
27439   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
27440
27441   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
27442     {
27443       arm_feature_set known_arch_fset;
27444
27445       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
27446       if (exact_match)
27447         {
27448           /* Base architecture match user-specified architecture and
27449              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
27450           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
27451             {
27452               p_ver_ret = p_ver;
27453               goto found;
27454             }
27455           /* Base architecture match user-specified architecture only
27456              (eg. ARMv6-M in the same case as above).  Record it in case we
27457              find a match with above condition.  */
27458           else if (p_ver_ret == NULL
27459                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
27460             p_ver_ret = p_ver;
27461         }
27462       else
27463         {
27464
27465           /* Architecture has all features wanted.  */
27466           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
27467             {
27468               arm_feature_set added_fset;
27469
27470               /* Compute features added by this architecture over the one
27471                  recorded in p_ver_ret.  */
27472               if (p_ver_ret != NULL)
27473                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
27474                                    p_ver_ret->flags);
27475               /* First architecture that match incl. with extensions, or the
27476                  only difference in features over the recorded match is
27477                  features that were optional and are now mandatory.  */
27478               if (p_ver_ret == NULL
27479                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
27480                 {
27481                   p_ver_ret = p_ver;
27482                   goto found;
27483                 }
27484             }
27485           else if (p_ver_ret == NULL)
27486             {
27487               arm_feature_set needed_ext_fset;
27488
27489               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
27490
27491               /* Architecture has all features needed when using some
27492                  extensions.  Record it and continue searching in case there
27493                  exist an architecture providing all needed features without
27494                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
27495                  OS extension).  */
27496               if (have_ext_for_needed_feat_p (&known_arch_fset,
27497                                               &needed_ext_fset))
27498                 p_ver_ret = p_ver;
27499             }
27500         }
27501     }
27502
27503   if (p_ver_ret == NULL)
27504     return -1;
27505
27506 found:
27507   /* Tag_CPU_arch_profile.  */
27508   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
27509       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
27510       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
27511           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
27512     *profile = 'A';
27513   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
27514     *profile = 'R';
27515   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
27516     *profile = 'M';
27517   else
27518     *profile = '\0';
27519   return p_ver_ret->val;
27520 }
27521
27522 /* Set the public EABI object attributes.  */
27523
27524 static void
27525 aeabi_set_public_attributes (void)
27526 {
27527   char profile = '\0';
27528   int arch = -1;
27529   int virt_sec = 0;
27530   int fp16_optional = 0;
27531   int skip_exact_match = 0;
27532   arm_feature_set flags, flags_arch, flags_ext;
27533
27534   /* Autodetection mode, choose the architecture based the instructions
27535      actually used.  */
27536   if (no_cpu_selected ())
27537     {
27538       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
27539
27540       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
27541         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
27542
27543       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
27544         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
27545
27546       /* Code run during relaxation relies on selected_cpu being set.  */
27547       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
27548       flags_ext = arm_arch_none;
27549       ARM_CLEAR_FEATURE (selected_arch, flags_arch, flags_ext);
27550       selected_ext = flags_ext;
27551       selected_cpu = flags;
27552     }
27553   /* Otherwise, choose the architecture based on the capabilities of the
27554      requested cpu.  */
27555   else
27556     {
27557       ARM_MERGE_FEATURE_SETS (flags_arch, selected_arch, selected_ext);
27558       ARM_CLEAR_FEATURE (flags_arch, flags_arch, fpu_any);
27559       flags_ext = selected_ext;
27560       flags = selected_cpu;
27561     }
27562   ARM_MERGE_FEATURE_SETS (flags, flags, selected_fpu);
27563
27564   /* Allow the user to override the reported architecture.  */
27565   if (!ARM_FEATURE_ZERO (selected_object_arch))
27566     {
27567       ARM_CLEAR_FEATURE (flags_arch, selected_object_arch, fpu_any);
27568       flags_ext = arm_arch_none;
27569     }
27570   else
27571     skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
27572
27573   /* When this function is run again after relaxation has happened there is no
27574      way to determine whether an architecture or CPU was specified by the user:
27575      - selected_cpu is set above for relaxation to work;
27576      - march_cpu_opt is not set if only -mcpu or .cpu is used;
27577      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
27578      Therefore, if not in -march=all case we first try an exact match and fall
27579      back to autodetection.  */
27580   if (!skip_exact_match)
27581     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
27582   if (arch == -1)
27583     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
27584   if (arch == -1)
27585     as_bad (_("no architecture contains all the instructions used\n"));
27586
27587   /* Tag_CPU_name.  */
27588   if (selected_cpu_name[0])
27589     {
27590       char *q;
27591
27592       q = selected_cpu_name;
27593       if (strncmp (q, "armv", 4) == 0)
27594         {
27595           int i;
27596
27597           q += 4;
27598           for (i = 0; q[i]; i++)
27599             q[i] = TOUPPER (q[i]);
27600         }
27601       aeabi_set_attribute_string (Tag_CPU_name, q);
27602     }
27603
27604   /* Tag_CPU_arch.  */
27605   aeabi_set_attribute_int (Tag_CPU_arch, arch);
27606
27607   /* Tag_CPU_arch_profile.  */
27608   if (profile != '\0')
27609     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
27610
27611   /* Tag_DSP_extension.  */
27612   if (ARM_CPU_HAS_FEATURE (selected_ext, arm_ext_dsp))
27613     aeabi_set_attribute_int (Tag_DSP_extension, 1);
27614
27615   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
27616   /* Tag_ARM_ISA_use.  */
27617   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
27618       || ARM_FEATURE_ZERO (flags_arch))
27619     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
27620
27621   /* Tag_THUMB_ISA_use.  */
27622   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
27623       || ARM_FEATURE_ZERO (flags_arch))
27624     {
27625       int thumb_isa_use;
27626
27627       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
27628           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
27629         thumb_isa_use = 3;
27630       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
27631         thumb_isa_use = 2;
27632       else
27633         thumb_isa_use = 1;
27634       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
27635     }
27636
27637   /* Tag_VFP_arch.  */
27638   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
27639     aeabi_set_attribute_int (Tag_VFP_arch,
27640                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
27641                              ? 7 : 8);
27642   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
27643     aeabi_set_attribute_int (Tag_VFP_arch,
27644                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
27645                              ? 5 : 6);
27646   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
27647     {
27648       fp16_optional = 1;
27649       aeabi_set_attribute_int (Tag_VFP_arch, 3);
27650     }
27651   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
27652     {
27653       aeabi_set_attribute_int (Tag_VFP_arch, 4);
27654       fp16_optional = 1;
27655     }
27656   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
27657     aeabi_set_attribute_int (Tag_VFP_arch, 2);
27658   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
27659            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
27660     aeabi_set_attribute_int (Tag_VFP_arch, 1);
27661
27662   /* Tag_ABI_HardFP_use.  */
27663   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
27664       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
27665     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
27666
27667   /* Tag_WMMX_arch.  */
27668   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
27669     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
27670   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
27671     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
27672
27673   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
27674   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
27675     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
27676   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
27677     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
27678   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
27679     {
27680       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
27681         {
27682           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
27683         }
27684       else
27685         {
27686           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
27687           fp16_optional = 1;
27688         }
27689     }
27690
27691   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
27692   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
27693     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
27694
27695   /* Tag_DIV_use.
27696
27697      We set Tag_DIV_use to two when integer divide instructions have been used
27698      in ARM state, or when Thumb integer divide instructions have been used,
27699      but we have no architecture profile set, nor have we any ARM instructions.
27700
27701      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
27702      by the base architecture.
27703
27704      For new architectures we will have to check these tests.  */
27705   gas_assert (arch <= TAG_CPU_ARCH_V8_1M_MAIN);
27706   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
27707       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
27708     aeabi_set_attribute_int (Tag_DIV_use, 0);
27709   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
27710            || (profile == '\0'
27711                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
27712                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
27713     aeabi_set_attribute_int (Tag_DIV_use, 2);
27714
27715   /* Tag_MP_extension_use.  */
27716   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
27717     aeabi_set_attribute_int (Tag_MPextension_use, 1);
27718
27719   /* Tag Virtualization_use.  */
27720   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
27721     virt_sec |= 1;
27722   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
27723     virt_sec |= 2;
27724   if (virt_sec != 0)
27725     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
27726 }
27727
27728 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
27729    finished and free extension feature bits which will not be used anymore.  */
27730
27731 void
27732 arm_md_post_relax (void)
27733 {
27734   aeabi_set_public_attributes ();
27735   XDELETE (mcpu_ext_opt);
27736   mcpu_ext_opt = NULL;
27737   XDELETE (march_ext_opt);
27738   march_ext_opt = NULL;
27739 }
27740
27741 /* Add the default contents for the .ARM.attributes section.  */
27742
27743 void
27744 arm_md_end (void)
27745 {
27746   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
27747     return;
27748
27749   aeabi_set_public_attributes ();
27750 }
27751 #endif /* OBJ_ELF */
27752
27753 /* Parse a .cpu directive.  */
27754
27755 static void
27756 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
27757 {
27758   const struct arm_cpu_option_table *opt;
27759   char *name;
27760   char saved_char;
27761
27762   name = input_line_pointer;
27763   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27764     input_line_pointer++;
27765   saved_char = *input_line_pointer;
27766   *input_line_pointer = 0;
27767
27768   /* Skip the first "all" entry.  */
27769   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
27770     if (streq (opt->name, name))
27771       {
27772         selected_arch = opt->value;
27773         selected_ext = opt->ext;
27774         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
27775         if (opt->canonical_name)
27776           strcpy (selected_cpu_name, opt->canonical_name);
27777         else
27778           {
27779             int i;
27780             for (i = 0; opt->name[i]; i++)
27781               selected_cpu_name[i] = TOUPPER (opt->name[i]);
27782
27783             selected_cpu_name[i] = 0;
27784           }
27785         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
27786
27787         *input_line_pointer = saved_char;
27788         demand_empty_rest_of_line ();
27789         return;
27790       }
27791   as_bad (_("unknown cpu `%s'"), name);
27792   *input_line_pointer = saved_char;
27793   ignore_rest_of_line ();
27794 }
27795
27796 /* Parse a .arch directive.  */
27797
27798 static void
27799 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
27800 {
27801   const struct arm_arch_option_table *opt;
27802   char saved_char;
27803   char *name;
27804
27805   name = input_line_pointer;
27806   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27807     input_line_pointer++;
27808   saved_char = *input_line_pointer;
27809   *input_line_pointer = 0;
27810
27811   /* Skip the first "all" entry.  */
27812   for (opt = arm_archs + 1; opt->name != NULL; opt++)
27813     if (streq (opt->name, name))
27814       {
27815         selected_arch = opt->value;
27816         selected_ext = arm_arch_none;
27817         selected_cpu = selected_arch;
27818         strcpy (selected_cpu_name, opt->name);
27819         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
27820         *input_line_pointer = saved_char;
27821         demand_empty_rest_of_line ();
27822         return;
27823       }
27824
27825   as_bad (_("unknown architecture `%s'\n"), name);
27826   *input_line_pointer = saved_char;
27827   ignore_rest_of_line ();
27828 }
27829
27830 /* Parse a .object_arch directive.  */
27831
27832 static void
27833 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
27834 {
27835   const struct arm_arch_option_table *opt;
27836   char saved_char;
27837   char *name;
27838
27839   name = input_line_pointer;
27840   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27841     input_line_pointer++;
27842   saved_char = *input_line_pointer;
27843   *input_line_pointer = 0;
27844
27845   /* Skip the first "all" entry.  */
27846   for (opt = arm_archs + 1; opt->name != NULL; opt++)
27847     if (streq (opt->name, name))
27848       {
27849         selected_object_arch = opt->value;
27850         *input_line_pointer = saved_char;
27851         demand_empty_rest_of_line ();
27852         return;
27853       }
27854
27855   as_bad (_("unknown architecture `%s'\n"), name);
27856   *input_line_pointer = saved_char;
27857   ignore_rest_of_line ();
27858 }
27859
27860 /* Parse a .arch_extension directive.  */
27861
27862 static void
27863 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
27864 {
27865   const struct arm_option_extension_value_table *opt;
27866   char saved_char;
27867   char *name;
27868   int adding_value = 1;
27869
27870   name = input_line_pointer;
27871   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27872     input_line_pointer++;
27873   saved_char = *input_line_pointer;
27874   *input_line_pointer = 0;
27875
27876   if (strlen (name) >= 2
27877       && strncmp (name, "no", 2) == 0)
27878     {
27879       adding_value = 0;
27880       name += 2;
27881     }
27882
27883   for (opt = arm_extensions; opt->name != NULL; opt++)
27884     if (streq (opt->name, name))
27885       {
27886         int i, nb_allowed_archs =
27887           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
27888         for (i = 0; i < nb_allowed_archs; i++)
27889           {
27890             /* Empty entry.  */
27891             if (ARM_CPU_IS_ANY (opt->allowed_archs[i]))
27892               continue;
27893             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], selected_arch))
27894               break;
27895           }
27896
27897         if (i == nb_allowed_archs)
27898           {
27899             as_bad (_("architectural extension `%s' is not allowed for the "
27900                       "current base architecture"), name);
27901             break;
27902           }
27903
27904         if (adding_value)
27905           ARM_MERGE_FEATURE_SETS (selected_ext, selected_ext,
27906                                   opt->merge_value);
27907         else
27908           ARM_CLEAR_FEATURE (selected_ext, selected_ext, opt->clear_value);
27909
27910         ARM_MERGE_FEATURE_SETS (selected_cpu, selected_arch, selected_ext);
27911         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
27912         *input_line_pointer = saved_char;
27913         demand_empty_rest_of_line ();
27914         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
27915            on this return so that duplicate extensions (extensions with the
27916            same name as a previous extension in the list) are not considered
27917            for command-line parsing.  */
27918         return;
27919       }
27920
27921   if (opt->name == NULL)
27922     as_bad (_("unknown architecture extension `%s'\n"), name);
27923
27924   *input_line_pointer = saved_char;
27925   ignore_rest_of_line ();
27926 }
27927
27928 /* Parse a .fpu directive.  */
27929
27930 static void
27931 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
27932 {
27933   const struct arm_option_fpu_value_table *opt;
27934   char saved_char;
27935   char *name;
27936
27937   name = input_line_pointer;
27938   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27939     input_line_pointer++;
27940   saved_char = *input_line_pointer;
27941   *input_line_pointer = 0;
27942
27943   for (opt = arm_fpus; opt->name != NULL; opt++)
27944     if (streq (opt->name, name))
27945       {
27946         selected_fpu = opt->value;
27947 #ifndef CPU_DEFAULT
27948         if (no_cpu_selected ())
27949           ARM_MERGE_FEATURE_SETS (cpu_variant, arm_arch_any, selected_fpu);
27950         else
27951 #endif
27952           ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, selected_fpu);
27953         *input_line_pointer = saved_char;
27954         demand_empty_rest_of_line ();
27955         return;
27956       }
27957
27958   as_bad (_("unknown floating point format `%s'\n"), name);
27959   *input_line_pointer = saved_char;
27960   ignore_rest_of_line ();
27961 }
27962
27963 /* Copy symbol information.  */
27964
27965 void
27966 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
27967 {
27968   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
27969 }
27970
27971 #ifdef OBJ_ELF
27972 /* Given a symbolic attribute NAME, return the proper integer value.
27973    Returns -1 if the attribute is not known.  */
27974
27975 int
27976 arm_convert_symbolic_attribute (const char *name)
27977 {
27978   static const struct
27979   {
27980     const char * name;
27981     const int    tag;
27982   }
27983   attribute_table[] =
27984     {
27985       /* When you modify this table you should
27986          also modify the list in doc/c-arm.texi.  */
27987 #define T(tag) {#tag, tag}
27988       T (Tag_CPU_raw_name),
27989       T (Tag_CPU_name),
27990       T (Tag_CPU_arch),
27991       T (Tag_CPU_arch_profile),
27992       T (Tag_ARM_ISA_use),
27993       T (Tag_THUMB_ISA_use),
27994       T (Tag_FP_arch),
27995       T (Tag_VFP_arch),
27996       T (Tag_WMMX_arch),
27997       T (Tag_Advanced_SIMD_arch),
27998       T (Tag_PCS_config),
27999       T (Tag_ABI_PCS_R9_use),
28000       T (Tag_ABI_PCS_RW_data),
28001       T (Tag_ABI_PCS_RO_data),
28002       T (Tag_ABI_PCS_GOT_use),
28003       T (Tag_ABI_PCS_wchar_t),
28004       T (Tag_ABI_FP_rounding),
28005       T (Tag_ABI_FP_denormal),
28006       T (Tag_ABI_FP_exceptions),
28007       T (Tag_ABI_FP_user_exceptions),
28008       T (Tag_ABI_FP_number_model),
28009       T (Tag_ABI_align_needed),
28010       T (Tag_ABI_align8_needed),
28011       T (Tag_ABI_align_preserved),
28012       T (Tag_ABI_align8_preserved),
28013       T (Tag_ABI_enum_size),
28014       T (Tag_ABI_HardFP_use),
28015       T (Tag_ABI_VFP_args),
28016       T (Tag_ABI_WMMX_args),
28017       T (Tag_ABI_optimization_goals),
28018       T (Tag_ABI_FP_optimization_goals),
28019       T (Tag_compatibility),
28020       T (Tag_CPU_unaligned_access),
28021       T (Tag_FP_HP_extension),
28022       T (Tag_VFP_HP_extension),
28023       T (Tag_ABI_FP_16bit_format),
28024       T (Tag_MPextension_use),
28025       T (Tag_DIV_use),
28026       T (Tag_nodefaults),
28027       T (Tag_also_compatible_with),
28028       T (Tag_conformance),
28029       T (Tag_T2EE_use),
28030       T (Tag_Virtualization_use),
28031       T (Tag_DSP_extension),
28032       /* We deliberately do not include Tag_MPextension_use_legacy.  */
28033 #undef T
28034     };
28035   unsigned int i;
28036
28037   if (name == NULL)
28038     return -1;
28039
28040   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
28041     if (streq (name, attribute_table[i].name))
28042       return attribute_table[i].tag;
28043
28044   return -1;
28045 }
28046
28047 /* Apply sym value for relocations only in the case that they are for
28048    local symbols in the same segment as the fixup and you have the
28049    respective architectural feature for blx and simple switches.  */
28050
28051 int
28052 arm_apply_sym_value (struct fix * fixP, segT this_seg)
28053 {
28054   if (fixP->fx_addsy
28055       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
28056       /* PR 17444: If the local symbol is in a different section then a reloc
28057          will always be generated for it, so applying the symbol value now
28058          will result in a double offset being stored in the relocation.  */
28059       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
28060       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
28061     {
28062       switch (fixP->fx_r_type)
28063         {
28064         case BFD_RELOC_ARM_PCREL_BLX:
28065         case BFD_RELOC_THUMB_PCREL_BRANCH23:
28066           if (ARM_IS_FUNC (fixP->fx_addsy))
28067             return 1;
28068           break;
28069
28070         case BFD_RELOC_ARM_PCREL_CALL:
28071         case BFD_RELOC_THUMB_PCREL_BLX:
28072           if (THUMB_IS_FUNC (fixP->fx_addsy))
28073             return 1;
28074           break;
28075
28076         default:
28077           break;
28078         }
28079
28080     }
28081   return 0;
28082 }
28083 #endif /* OBJ_ELF */