-Wimplicit-fallthrough warning fixes
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994-2016 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 #endif /* OBJ_ELF */
79
80 /* Results from operand parsing worker functions.  */
81
82 typedef enum
83 {
84   PARSE_OPERAND_SUCCESS,
85   PARSE_OPERAND_FAIL,
86   PARSE_OPERAND_FAIL_NO_BACKTRACK
87 } parse_operand_result;
88
89 enum arm_float_abi
90 {
91   ARM_FLOAT_ABI_HARD,
92   ARM_FLOAT_ABI_SOFTFP,
93   ARM_FLOAT_ABI_SOFT
94 };
95
96 /* Types of processor to assemble for.  */
97 #ifndef CPU_DEFAULT
98 /* The code that was here used to select a default CPU depending on compiler
99    pre-defines which were only present when doing native builds, thus
100    changing gas' default behaviour depending upon the build host.
101
102    If you have a target that requires a default CPU option then the you
103    should define CPU_DEFAULT here.  */
104 #endif
105
106 #ifndef FPU_DEFAULT
107 # ifdef TE_LINUX
108 #  define FPU_DEFAULT FPU_ARCH_FPA
109 # elif defined (TE_NetBSD)
110 #  ifdef OBJ_ELF
111 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
112 #  else
113     /* Legacy a.out format.  */
114 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
115 #  endif
116 # elif defined (TE_VXWORKS)
117 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
118 # else
119    /* For backwards compatibility, default to FPA.  */
120 #  define FPU_DEFAULT FPU_ARCH_FPA
121 # endif
122 #endif /* ifndef FPU_DEFAULT */
123
124 #define streq(a, b)           (strcmp (a, b) == 0)
125
126 static arm_feature_set cpu_variant;
127 static arm_feature_set arm_arch_used;
128 static arm_feature_set thumb_arch_used;
129
130 /* Flags stored in private area of BFD structure.  */
131 static int uses_apcs_26      = FALSE;
132 static int atpcs             = FALSE;
133 static int support_interwork = FALSE;
134 static int uses_apcs_float   = FALSE;
135 static int pic_code          = FALSE;
136 static int fix_v4bx          = FALSE;
137 /* Warn on using deprecated features.  */
138 static int warn_on_deprecated = TRUE;
139
140 /* Understand CodeComposer Studio assembly syntax.  */
141 bfd_boolean codecomposer_syntax = FALSE;
142
143 /* Variables that we set while parsing command-line options.  Once all
144    options have been read we re-process these values to set the real
145    assembly flags.  */
146 static const arm_feature_set *legacy_cpu = NULL;
147 static const arm_feature_set *legacy_fpu = NULL;
148
149 static const arm_feature_set *mcpu_cpu_opt = NULL;
150 static const arm_feature_set *mcpu_fpu_opt = NULL;
151 static const arm_feature_set *march_cpu_opt = NULL;
152 static const arm_feature_set *march_fpu_opt = NULL;
153 static const arm_feature_set *mfpu_opt = NULL;
154 static const arm_feature_set *object_arch = NULL;
155
156 /* Constants for known architecture features.  */
157 static const arm_feature_set fpu_default = FPU_DEFAULT;
158 static const arm_feature_set fpu_arch_vfp_v1 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V1;
159 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
160 static const arm_feature_set fpu_arch_vfp_v3 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V3;
161 static const arm_feature_set fpu_arch_neon_v1 ATTRIBUTE_UNUSED = FPU_ARCH_NEON_V1;
162 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
163 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
164 #ifdef OBJ_ELF
165 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
166 #endif
167 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
168
169 #ifdef CPU_DEFAULT
170 static const arm_feature_set cpu_default = CPU_DEFAULT;
171 #endif
172
173 static const arm_feature_set arm_ext_v1 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
174 static const arm_feature_set arm_ext_v2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
175 static const arm_feature_set arm_ext_v2s = ARM_FEATURE_CORE_LOW (ARM_EXT_V2S);
176 static const arm_feature_set arm_ext_v3 = ARM_FEATURE_CORE_LOW (ARM_EXT_V3);
177 static const arm_feature_set arm_ext_v3m = ARM_FEATURE_CORE_LOW (ARM_EXT_V3M);
178 static const arm_feature_set arm_ext_v4 = ARM_FEATURE_CORE_LOW (ARM_EXT_V4);
179 static const arm_feature_set arm_ext_v4t = ARM_FEATURE_CORE_LOW (ARM_EXT_V4T);
180 static const arm_feature_set arm_ext_v5 = ARM_FEATURE_CORE_LOW (ARM_EXT_V5);
181 static const arm_feature_set arm_ext_v4t_5 =
182   ARM_FEATURE_CORE_LOW (ARM_EXT_V4T | ARM_EXT_V5);
183 static const arm_feature_set arm_ext_v5t = ARM_FEATURE_CORE_LOW (ARM_EXT_V5T);
184 static const arm_feature_set arm_ext_v5e = ARM_FEATURE_CORE_LOW (ARM_EXT_V5E);
185 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP);
186 static const arm_feature_set arm_ext_v5j = ARM_FEATURE_CORE_LOW (ARM_EXT_V5J);
187 static const arm_feature_set arm_ext_v6 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6);
188 static const arm_feature_set arm_ext_v6k = ARM_FEATURE_CORE_LOW (ARM_EXT_V6K);
189 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6T2);
190 static const arm_feature_set arm_ext_v6m = ARM_FEATURE_CORE_LOW (ARM_EXT_V6M);
191 static const arm_feature_set arm_ext_v6_notm =
192   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_NOTM);
193 static const arm_feature_set arm_ext_v6_dsp =
194   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_DSP);
195 static const arm_feature_set arm_ext_barrier =
196   ARM_FEATURE_CORE_LOW (ARM_EXT_BARRIER);
197 static const arm_feature_set arm_ext_msr =
198   ARM_FEATURE_CORE_LOW (ARM_EXT_THUMB_MSR);
199 static const arm_feature_set arm_ext_div = ARM_FEATURE_CORE_LOW (ARM_EXT_DIV);
200 static const arm_feature_set arm_ext_v7 = ARM_FEATURE_CORE_LOW (ARM_EXT_V7);
201 static const arm_feature_set arm_ext_v7a = ARM_FEATURE_CORE_LOW (ARM_EXT_V7A);
202 static const arm_feature_set arm_ext_v7r = ARM_FEATURE_CORE_LOW (ARM_EXT_V7R);
203 #ifdef OBJ_ELF
204 static const arm_feature_set arm_ext_v7m = ARM_FEATURE_CORE_LOW (ARM_EXT_V7M);
205 #endif
206 static const arm_feature_set arm_ext_v8 = ARM_FEATURE_CORE_LOW (ARM_EXT_V8);
207 static const arm_feature_set arm_ext_m =
208   ARM_FEATURE_CORE (ARM_EXT_V6M | ARM_EXT_OS | ARM_EXT_V7M,
209                     ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
210 static const arm_feature_set arm_ext_mp = ARM_FEATURE_CORE_LOW (ARM_EXT_MP);
211 static const arm_feature_set arm_ext_sec = ARM_FEATURE_CORE_LOW (ARM_EXT_SEC);
212 static const arm_feature_set arm_ext_os = ARM_FEATURE_CORE_LOW (ARM_EXT_OS);
213 static const arm_feature_set arm_ext_adiv = ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV);
214 static const arm_feature_set arm_ext_virt = ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT);
215 static const arm_feature_set arm_ext_pan = ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN);
216 static const arm_feature_set arm_ext_v8m = ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M);
217 static const arm_feature_set arm_ext_v8m_main =
218   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M_MAIN);
219 /* Instructions in ARMv8-M only found in M profile architectures.  */
220 static const arm_feature_set arm_ext_v8m_m_only =
221   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
222 static const arm_feature_set arm_ext_v6t2_v8m =
223   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V6T2_V8M);
224 /* Instructions shared between ARMv8-A and ARMv8-M.  */
225 static const arm_feature_set arm_ext_atomics =
226   ARM_FEATURE_CORE_HIGH (ARM_EXT2_ATOMICS);
227 #ifdef OBJ_ELF
228 /* DSP instructions Tag_DSP_extension refers to.  */
229 static const arm_feature_set arm_ext_dsp =
230   ARM_FEATURE_CORE_LOW (ARM_EXT_V5E | ARM_EXT_V5ExP | ARM_EXT_V6_DSP);
231 #endif
232 static const arm_feature_set arm_ext_ras =
233   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS);
234 /* FP16 instructions.  */
235 static const arm_feature_set arm_ext_fp16 =
236   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST);
237
238 static const arm_feature_set arm_arch_any = ARM_ANY;
239 static const arm_feature_set arm_arch_full ATTRIBUTE_UNUSED = ARM_FEATURE (-1, -1, -1);
240 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
241 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
242 #ifdef OBJ_ELF
243 static const arm_feature_set arm_arch_v6m_only = ARM_ARCH_V6M_ONLY;
244 #endif
245
246 static const arm_feature_set arm_cext_iwmmxt2 =
247   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
248 static const arm_feature_set arm_cext_iwmmxt =
249   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
250 static const arm_feature_set arm_cext_xscale =
251   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
252 static const arm_feature_set arm_cext_maverick =
253   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
254 static const arm_feature_set fpu_fpa_ext_v1 =
255   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
256 static const arm_feature_set fpu_fpa_ext_v2 =
257   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
258 static const arm_feature_set fpu_vfp_ext_v1xd =
259   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
260 static const arm_feature_set fpu_vfp_ext_v1 =
261   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
262 static const arm_feature_set fpu_vfp_ext_v2 =
263   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
264 static const arm_feature_set fpu_vfp_ext_v3xd =
265   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
266 static const arm_feature_set fpu_vfp_ext_v3 =
267   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
268 static const arm_feature_set fpu_vfp_ext_d32 =
269   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
270 static const arm_feature_set fpu_neon_ext_v1 =
271   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
272 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
273   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
274 #ifdef OBJ_ELF
275 static const arm_feature_set fpu_vfp_fp16 =
276   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
277 static const arm_feature_set fpu_neon_ext_fma =
278   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
279 #endif
280 static const arm_feature_set fpu_vfp_ext_fma =
281   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
282 static const arm_feature_set fpu_vfp_ext_armv8 =
283   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
284 static const arm_feature_set fpu_vfp_ext_armv8xd =
285   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
286 static const arm_feature_set fpu_neon_ext_armv8 =
287   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
288 static const arm_feature_set fpu_crypto_ext_armv8 =
289   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
290 static const arm_feature_set crc_ext_armv8 =
291   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
292 static const arm_feature_set fpu_neon_ext_v8_1 =
293   ARM_FEATURE_COPROC (FPU_NEON_EXT_RDMA);
294
295 static int mfloat_abi_opt = -1;
296 /* Record user cpu selection for object attributes.  */
297 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
298 /* Must be long enough to hold any of the names in arm_cpus.  */
299 static char selected_cpu_name[20];
300
301 extern FLONUM_TYPE generic_floating_point_number;
302
303 /* Return if no cpu was selected on command-line.  */
304 static bfd_boolean
305 no_cpu_selected (void)
306 {
307   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
308 }
309
310 #ifdef OBJ_ELF
311 # ifdef EABI_DEFAULT
312 static int meabi_flags = EABI_DEFAULT;
313 # else
314 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
315 # endif
316
317 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
318
319 bfd_boolean
320 arm_is_eabi (void)
321 {
322   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
323 }
324 #endif
325
326 #ifdef OBJ_ELF
327 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
328 symbolS * GOT_symbol;
329 #endif
330
331 /* 0: assemble for ARM,
332    1: assemble for Thumb,
333    2: assemble for Thumb even though target CPU does not support thumb
334       instructions.  */
335 static int thumb_mode = 0;
336 /* A value distinct from the possible values for thumb_mode that we
337    can use to record whether thumb_mode has been copied into the
338    tc_frag_data field of a frag.  */
339 #define MODE_RECORDED (1 << 4)
340
341 /* Specifies the intrinsic IT insn behavior mode.  */
342 enum implicit_it_mode
343 {
344   IMPLICIT_IT_MODE_NEVER  = 0x00,
345   IMPLICIT_IT_MODE_ARM    = 0x01,
346   IMPLICIT_IT_MODE_THUMB  = 0x02,
347   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
348 };
349 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
350
351 /* If unified_syntax is true, we are processing the new unified
352    ARM/Thumb syntax.  Important differences from the old ARM mode:
353
354      - Immediate operands do not require a # prefix.
355      - Conditional affixes always appear at the end of the
356        instruction.  (For backward compatibility, those instructions
357        that formerly had them in the middle, continue to accept them
358        there.)
359      - The IT instruction may appear, and if it does is validated
360        against subsequent conditional affixes.  It does not generate
361        machine code.
362
363    Important differences from the old Thumb mode:
364
365      - Immediate operands do not require a # prefix.
366      - Most of the V6T2 instructions are only available in unified mode.
367      - The .N and .W suffixes are recognized and honored (it is an error
368        if they cannot be honored).
369      - All instructions set the flags if and only if they have an 's' affix.
370      - Conditional affixes may be used.  They are validated against
371        preceding IT instructions.  Unlike ARM mode, you cannot use a
372        conditional affix except in the scope of an IT instruction.  */
373
374 static bfd_boolean unified_syntax = FALSE;
375
376 /* An immediate operand can start with #, and ld*, st*, pld operands
377    can contain [ and ].  We need to tell APP not to elide whitespace
378    before a [, which can appear as the first operand for pld.
379    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
380 const char arm_symbol_chars[] = "#[]{}";
381
382 enum neon_el_type
383 {
384   NT_invtype,
385   NT_untyped,
386   NT_integer,
387   NT_float,
388   NT_poly,
389   NT_signed,
390   NT_unsigned
391 };
392
393 struct neon_type_el
394 {
395   enum neon_el_type type;
396   unsigned size;
397 };
398
399 #define NEON_MAX_TYPE_ELS 4
400
401 struct neon_type
402 {
403   struct neon_type_el el[NEON_MAX_TYPE_ELS];
404   unsigned elems;
405 };
406
407 enum it_instruction_type
408 {
409    OUTSIDE_IT_INSN,
410    INSIDE_IT_INSN,
411    INSIDE_IT_LAST_INSN,
412    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
413                               if inside, should be the last one.  */
414    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
415                               i.e. BKPT and NOP.  */
416    IT_INSN                 /* The IT insn has been parsed.  */
417 };
418
419 /* The maximum number of operands we need.  */
420 #define ARM_IT_MAX_OPERANDS 6
421
422 struct arm_it
423 {
424   const char *  error;
425   unsigned long instruction;
426   int           size;
427   int           size_req;
428   int           cond;
429   /* "uncond_value" is set to the value in place of the conditional field in
430      unconditional versions of the instruction, or -1 if nothing is
431      appropriate.  */
432   int           uncond_value;
433   struct neon_type vectype;
434   /* This does not indicate an actual NEON instruction, only that
435      the mnemonic accepts neon-style type suffixes.  */
436   int           is_neon;
437   /* Set to the opcode if the instruction needs relaxation.
438      Zero if the instruction is not relaxed.  */
439   unsigned long relax;
440   struct
441   {
442     bfd_reloc_code_real_type type;
443     expressionS              exp;
444     int                      pc_rel;
445   } reloc;
446
447   enum it_instruction_type it_insn_type;
448
449   struct
450   {
451     unsigned reg;
452     signed int imm;
453     struct neon_type_el vectype;
454     unsigned present    : 1;  /* Operand present.  */
455     unsigned isreg      : 1;  /* Operand was a register.  */
456     unsigned immisreg   : 1;  /* .imm field is a second register.  */
457     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
458     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
459     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
460     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
461        instructions. This allows us to disambiguate ARM <-> vector insns.  */
462     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
463     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
464     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
465     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
466     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
467     unsigned writeback  : 1;  /* Operand has trailing !  */
468     unsigned preind     : 1;  /* Preindexed address.  */
469     unsigned postind    : 1;  /* Postindexed address.  */
470     unsigned negative   : 1;  /* Index register was negated.  */
471     unsigned shifted    : 1;  /* Shift applied to operation.  */
472     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
473   } operands[ARM_IT_MAX_OPERANDS];
474 };
475
476 static struct arm_it inst;
477
478 #define NUM_FLOAT_VALS 8
479
480 const char * fp_const[] =
481 {
482   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
483 };
484
485 /* Number of littlenums required to hold an extended precision number.  */
486 #define MAX_LITTLENUMS 6
487
488 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
489
490 #define FAIL    (-1)
491 #define SUCCESS (0)
492
493 #define SUFF_S 1
494 #define SUFF_D 2
495 #define SUFF_E 3
496 #define SUFF_P 4
497
498 #define CP_T_X   0x00008000
499 #define CP_T_Y   0x00400000
500
501 #define CONDS_BIT        0x00100000
502 #define LOAD_BIT         0x00100000
503
504 #define DOUBLE_LOAD_FLAG 0x00000001
505
506 struct asm_cond
507 {
508   const char *   template_name;
509   unsigned long  value;
510 };
511
512 #define COND_ALWAYS 0xE
513
514 struct asm_psr
515 {
516   const char *   template_name;
517   unsigned long  field;
518 };
519
520 struct asm_barrier_opt
521 {
522   const char *    template_name;
523   unsigned long   value;
524   const arm_feature_set arch;
525 };
526
527 /* The bit that distinguishes CPSR and SPSR.  */
528 #define SPSR_BIT   (1 << 22)
529
530 /* The individual PSR flag bits.  */
531 #define PSR_c   (1 << 16)
532 #define PSR_x   (1 << 17)
533 #define PSR_s   (1 << 18)
534 #define PSR_f   (1 << 19)
535
536 struct reloc_entry
537 {
538   const char *                    name;
539   bfd_reloc_code_real_type  reloc;
540 };
541
542 enum vfp_reg_pos
543 {
544   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
545   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
546 };
547
548 enum vfp_ldstm_type
549 {
550   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
551 };
552
553 /* Bits for DEFINED field in neon_typed_alias.  */
554 #define NTA_HASTYPE  1
555 #define NTA_HASINDEX 2
556
557 struct neon_typed_alias
558 {
559   unsigned char        defined;
560   unsigned char        index;
561   struct neon_type_el  eltype;
562 };
563
564 /* ARM register categories.  This includes coprocessor numbers and various
565    architecture extensions' registers.  */
566 enum arm_reg_type
567 {
568   REG_TYPE_RN,
569   REG_TYPE_CP,
570   REG_TYPE_CN,
571   REG_TYPE_FN,
572   REG_TYPE_VFS,
573   REG_TYPE_VFD,
574   REG_TYPE_NQ,
575   REG_TYPE_VFSD,
576   REG_TYPE_NDQ,
577   REG_TYPE_NSDQ,
578   REG_TYPE_VFC,
579   REG_TYPE_MVF,
580   REG_TYPE_MVD,
581   REG_TYPE_MVFX,
582   REG_TYPE_MVDX,
583   REG_TYPE_MVAX,
584   REG_TYPE_DSPSC,
585   REG_TYPE_MMXWR,
586   REG_TYPE_MMXWC,
587   REG_TYPE_MMXWCG,
588   REG_TYPE_XSCALE,
589   REG_TYPE_RNB
590 };
591
592 /* Structure for a hash table entry for a register.
593    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
594    information which states whether a vector type or index is specified (for a
595    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
596 struct reg_entry
597 {
598   const char *               name;
599   unsigned int               number;
600   unsigned char              type;
601   unsigned char              builtin;
602   struct neon_typed_alias *  neon;
603 };
604
605 /* Diagnostics used when we don't get a register of the expected type.  */
606 const char * const reg_expected_msgs[] =
607 {
608   N_("ARM register expected"),
609   N_("bad or missing co-processor number"),
610   N_("co-processor register expected"),
611   N_("FPA register expected"),
612   N_("VFP single precision register expected"),
613   N_("VFP/Neon double precision register expected"),
614   N_("Neon quad precision register expected"),
615   N_("VFP single or double precision register expected"),
616   N_("Neon double or quad precision register expected"),
617   N_("VFP single, double or Neon quad precision register expected"),
618   N_("VFP system register expected"),
619   N_("Maverick MVF register expected"),
620   N_("Maverick MVD register expected"),
621   N_("Maverick MVFX register expected"),
622   N_("Maverick MVDX register expected"),
623   N_("Maverick MVAX register expected"),
624   N_("Maverick DSPSC register expected"),
625   N_("iWMMXt data register expected"),
626   N_("iWMMXt control register expected"),
627   N_("iWMMXt scalar register expected"),
628   N_("XScale accumulator register expected"),
629 };
630
631 /* Some well known registers that we refer to directly elsewhere.  */
632 #define REG_R12 12
633 #define REG_SP  13
634 #define REG_LR  14
635 #define REG_PC  15
636
637 /* ARM instructions take 4bytes in the object file, Thumb instructions
638    take 2:  */
639 #define INSN_SIZE       4
640
641 struct asm_opcode
642 {
643   /* Basic string to match.  */
644   const char * template_name;
645
646   /* Parameters to instruction.  */
647   unsigned int operands[8];
648
649   /* Conditional tag - see opcode_lookup.  */
650   unsigned int tag : 4;
651
652   /* Basic instruction code.  */
653   unsigned int avalue : 28;
654
655   /* Thumb-format instruction code.  */
656   unsigned int tvalue;
657
658   /* Which architecture variant provides this instruction.  */
659   const arm_feature_set * avariant;
660   const arm_feature_set * tvariant;
661
662   /* Function to call to encode instruction in ARM format.  */
663   void (* aencode) (void);
664
665   /* Function to call to encode instruction in Thumb format.  */
666   void (* tencode) (void);
667 };
668
669 /* Defines for various bits that we will want to toggle.  */
670 #define INST_IMMEDIATE  0x02000000
671 #define OFFSET_REG      0x02000000
672 #define HWOFFSET_IMM    0x00400000
673 #define SHIFT_BY_REG    0x00000010
674 #define PRE_INDEX       0x01000000
675 #define INDEX_UP        0x00800000
676 #define WRITE_BACK      0x00200000
677 #define LDM_TYPE_2_OR_3 0x00400000
678 #define CPSI_MMOD       0x00020000
679
680 #define LITERAL_MASK    0xf000f000
681 #define OPCODE_MASK     0xfe1fffff
682 #define V4_STR_BIT      0x00000020
683 #define VLDR_VMOV_SAME  0x0040f000
684
685 #define T2_SUBS_PC_LR   0xf3de8f00
686
687 #define DATA_OP_SHIFT   21
688
689 #define T2_OPCODE_MASK  0xfe1fffff
690 #define T2_DATA_OP_SHIFT 21
691
692 #define A_COND_MASK         0xf0000000
693 #define A_PUSH_POP_OP_MASK  0x0fff0000
694
695 /* Opcodes for pushing/poping registers to/from the stack.  */
696 #define A1_OPCODE_PUSH    0x092d0000
697 #define A2_OPCODE_PUSH    0x052d0004
698 #define A2_OPCODE_POP     0x049d0004
699
700 /* Codes to distinguish the arithmetic instructions.  */
701 #define OPCODE_AND      0
702 #define OPCODE_EOR      1
703 #define OPCODE_SUB      2
704 #define OPCODE_RSB      3
705 #define OPCODE_ADD      4
706 #define OPCODE_ADC      5
707 #define OPCODE_SBC      6
708 #define OPCODE_RSC      7
709 #define OPCODE_TST      8
710 #define OPCODE_TEQ      9
711 #define OPCODE_CMP      10
712 #define OPCODE_CMN      11
713 #define OPCODE_ORR      12
714 #define OPCODE_MOV      13
715 #define OPCODE_BIC      14
716 #define OPCODE_MVN      15
717
718 #define T2_OPCODE_AND   0
719 #define T2_OPCODE_BIC   1
720 #define T2_OPCODE_ORR   2
721 #define T2_OPCODE_ORN   3
722 #define T2_OPCODE_EOR   4
723 #define T2_OPCODE_ADD   8
724 #define T2_OPCODE_ADC   10
725 #define T2_OPCODE_SBC   11
726 #define T2_OPCODE_SUB   13
727 #define T2_OPCODE_RSB   14
728
729 #define T_OPCODE_MUL 0x4340
730 #define T_OPCODE_TST 0x4200
731 #define T_OPCODE_CMN 0x42c0
732 #define T_OPCODE_NEG 0x4240
733 #define T_OPCODE_MVN 0x43c0
734
735 #define T_OPCODE_ADD_R3 0x1800
736 #define T_OPCODE_SUB_R3 0x1a00
737 #define T_OPCODE_ADD_HI 0x4400
738 #define T_OPCODE_ADD_ST 0xb000
739 #define T_OPCODE_SUB_ST 0xb080
740 #define T_OPCODE_ADD_SP 0xa800
741 #define T_OPCODE_ADD_PC 0xa000
742 #define T_OPCODE_ADD_I8 0x3000
743 #define T_OPCODE_SUB_I8 0x3800
744 #define T_OPCODE_ADD_I3 0x1c00
745 #define T_OPCODE_SUB_I3 0x1e00
746
747 #define T_OPCODE_ASR_R  0x4100
748 #define T_OPCODE_LSL_R  0x4080
749 #define T_OPCODE_LSR_R  0x40c0
750 #define T_OPCODE_ROR_R  0x41c0
751 #define T_OPCODE_ASR_I  0x1000
752 #define T_OPCODE_LSL_I  0x0000
753 #define T_OPCODE_LSR_I  0x0800
754
755 #define T_OPCODE_MOV_I8 0x2000
756 #define T_OPCODE_CMP_I8 0x2800
757 #define T_OPCODE_CMP_LR 0x4280
758 #define T_OPCODE_MOV_HR 0x4600
759 #define T_OPCODE_CMP_HR 0x4500
760
761 #define T_OPCODE_LDR_PC 0x4800
762 #define T_OPCODE_LDR_SP 0x9800
763 #define T_OPCODE_STR_SP 0x9000
764 #define T_OPCODE_LDR_IW 0x6800
765 #define T_OPCODE_STR_IW 0x6000
766 #define T_OPCODE_LDR_IH 0x8800
767 #define T_OPCODE_STR_IH 0x8000
768 #define T_OPCODE_LDR_IB 0x7800
769 #define T_OPCODE_STR_IB 0x7000
770 #define T_OPCODE_LDR_RW 0x5800
771 #define T_OPCODE_STR_RW 0x5000
772 #define T_OPCODE_LDR_RH 0x5a00
773 #define T_OPCODE_STR_RH 0x5200
774 #define T_OPCODE_LDR_RB 0x5c00
775 #define T_OPCODE_STR_RB 0x5400
776
777 #define T_OPCODE_PUSH   0xb400
778 #define T_OPCODE_POP    0xbc00
779
780 #define T_OPCODE_BRANCH 0xe000
781
782 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
783 #define THUMB_PP_PC_LR 0x0100
784 #define THUMB_LOAD_BIT 0x0800
785 #define THUMB2_LOAD_BIT 0x00100000
786
787 #define BAD_ARGS        _("bad arguments to instruction")
788 #define BAD_SP          _("r13 not allowed here")
789 #define BAD_PC          _("r15 not allowed here")
790 #define BAD_COND        _("instruction cannot be conditional")
791 #define BAD_OVERLAP     _("registers may not be the same")
792 #define BAD_HIREG       _("lo register required")
793 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
794 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
795 #define BAD_BRANCH      _("branch must be last instruction in IT block")
796 #define BAD_NOT_IT      _("instruction not allowed in IT block")
797 #define BAD_FPU         _("selected FPU does not support instruction")
798 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
799 #define BAD_IT_COND     _("incorrect condition in IT block")
800 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
801 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
802 #define BAD_PC_ADDRESSING \
803         _("cannot use register index with PC-relative addressing")
804 #define BAD_PC_WRITEBACK \
805         _("cannot use writeback with PC-relative addressing")
806 #define BAD_RANGE       _("branch out of range")
807 #define BAD_FP16        _("selected processor does not support fp16 instruction")
808 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
809 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
810
811 static struct hash_control * arm_ops_hsh;
812 static struct hash_control * arm_cond_hsh;
813 static struct hash_control * arm_shift_hsh;
814 static struct hash_control * arm_psr_hsh;
815 static struct hash_control * arm_v7m_psr_hsh;
816 static struct hash_control * arm_reg_hsh;
817 static struct hash_control * arm_reloc_hsh;
818 static struct hash_control * arm_barrier_opt_hsh;
819
820 /* Stuff needed to resolve the label ambiguity
821    As:
822      ...
823      label:   <insn>
824    may differ from:
825      ...
826      label:
827               <insn>  */
828
829 symbolS *  last_label_seen;
830 static int label_is_thumb_function_name = FALSE;
831
832 /* Literal pool structure.  Held on a per-section
833    and per-sub-section basis.  */
834
835 #define MAX_LITERAL_POOL_SIZE 1024
836 typedef struct literal_pool
837 {
838   expressionS            literals [MAX_LITERAL_POOL_SIZE];
839   unsigned int           next_free_entry;
840   unsigned int           id;
841   symbolS *              symbol;
842   segT                   section;
843   subsegT                sub_section;
844 #ifdef OBJ_ELF
845   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
846 #endif
847   struct literal_pool *  next;
848   unsigned int           alignment;
849 } literal_pool;
850
851 /* Pointer to a linked list of literal pools.  */
852 literal_pool * list_of_pools = NULL;
853
854 typedef enum asmfunc_states
855 {
856   OUTSIDE_ASMFUNC,
857   WAITING_ASMFUNC_NAME,
858   WAITING_ENDASMFUNC
859 } asmfunc_states;
860
861 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
862
863 #ifdef OBJ_ELF
864 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
865 #else
866 static struct current_it now_it;
867 #endif
868
869 static inline int
870 now_it_compatible (int cond)
871 {
872   return (cond & ~1) == (now_it.cc & ~1);
873 }
874
875 static inline int
876 conditional_insn (void)
877 {
878   return inst.cond != COND_ALWAYS;
879 }
880
881 static int in_it_block (void);
882
883 static int handle_it_state (void);
884
885 static void force_automatic_it_block_close (void);
886
887 static void it_fsm_post_encode (void);
888
889 #define set_it_insn_type(type)                  \
890   do                                            \
891     {                                           \
892       inst.it_insn_type = type;                 \
893       if (handle_it_state () == FAIL)           \
894         return;                                 \
895     }                                           \
896   while (0)
897
898 #define set_it_insn_type_nonvoid(type, failret) \
899   do                                            \
900     {                                           \
901       inst.it_insn_type = type;                 \
902       if (handle_it_state () == FAIL)           \
903         return failret;                         \
904     }                                           \
905   while(0)
906
907 #define set_it_insn_type_last()                         \
908   do                                                    \
909     {                                                   \
910       if (inst.cond == COND_ALWAYS)                     \
911         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
912       else                                              \
913         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
914     }                                                   \
915   while (0)
916
917 /* Pure syntax.  */
918
919 /* This array holds the chars that always start a comment.  If the
920    pre-processor is disabled, these aren't very useful.  */
921 char arm_comment_chars[] = "@";
922
923 /* This array holds the chars that only start a comment at the beginning of
924    a line.  If the line seems to have the form '# 123 filename'
925    .line and .file directives will appear in the pre-processed output.  */
926 /* Note that input_file.c hand checks for '#' at the beginning of the
927    first line of the input file.  This is because the compiler outputs
928    #NO_APP at the beginning of its output.  */
929 /* Also note that comments like this one will always work.  */
930 const char line_comment_chars[] = "#";
931
932 char arm_line_separator_chars[] = ";";
933
934 /* Chars that can be used to separate mant
935    from exp in floating point numbers.  */
936 const char EXP_CHARS[] = "eE";
937
938 /* Chars that mean this number is a floating point constant.  */
939 /* As in 0f12.456  */
940 /* or    0d1.2345e12  */
941
942 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
943
944 /* Prefix characters that indicate the start of an immediate
945    value.  */
946 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
947
948 /* Separator character handling.  */
949
950 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
951
952 static inline int
953 skip_past_char (char ** str, char c)
954 {
955   /* PR gas/14987: Allow for whitespace before the expected character.  */
956   skip_whitespace (*str);
957
958   if (**str == c)
959     {
960       (*str)++;
961       return SUCCESS;
962     }
963   else
964     return FAIL;
965 }
966
967 #define skip_past_comma(str) skip_past_char (str, ',')
968
969 /* Arithmetic expressions (possibly involving symbols).  */
970
971 /* Return TRUE if anything in the expression is a bignum.  */
972
973 static int
974 walk_no_bignums (symbolS * sp)
975 {
976   if (symbol_get_value_expression (sp)->X_op == O_big)
977     return 1;
978
979   if (symbol_get_value_expression (sp)->X_add_symbol)
980     {
981       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
982               || (symbol_get_value_expression (sp)->X_op_symbol
983                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
984     }
985
986   return 0;
987 }
988
989 static int in_my_get_expression = 0;
990
991 /* Third argument to my_get_expression.  */
992 #define GE_NO_PREFIX 0
993 #define GE_IMM_PREFIX 1
994 #define GE_OPT_PREFIX 2
995 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
996    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
997 #define GE_OPT_PREFIX_BIG 3
998
999 static int
1000 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1001 {
1002   char * save_in;
1003   segT   seg;
1004
1005   /* In unified syntax, all prefixes are optional.  */
1006   if (unified_syntax)
1007     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1008                   : GE_OPT_PREFIX;
1009
1010   switch (prefix_mode)
1011     {
1012     case GE_NO_PREFIX: break;
1013     case GE_IMM_PREFIX:
1014       if (!is_immediate_prefix (**str))
1015         {
1016           inst.error = _("immediate expression requires a # prefix");
1017           return FAIL;
1018         }
1019       (*str)++;
1020       break;
1021     case GE_OPT_PREFIX:
1022     case GE_OPT_PREFIX_BIG:
1023       if (is_immediate_prefix (**str))
1024         (*str)++;
1025       break;
1026     default: abort ();
1027     }
1028
1029   memset (ep, 0, sizeof (expressionS));
1030
1031   save_in = input_line_pointer;
1032   input_line_pointer = *str;
1033   in_my_get_expression = 1;
1034   seg = expression (ep);
1035   in_my_get_expression = 0;
1036
1037   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1038     {
1039       /* We found a bad or missing expression in md_operand().  */
1040       *str = input_line_pointer;
1041       input_line_pointer = save_in;
1042       if (inst.error == NULL)
1043         inst.error = (ep->X_op == O_absent
1044                       ? _("missing expression") :_("bad expression"));
1045       return 1;
1046     }
1047
1048 #ifdef OBJ_AOUT
1049   if (seg != absolute_section
1050       && seg != text_section
1051       && seg != data_section
1052       && seg != bss_section
1053       && seg != undefined_section)
1054     {
1055       inst.error = _("bad segment");
1056       *str = input_line_pointer;
1057       input_line_pointer = save_in;
1058       return 1;
1059     }
1060 #else
1061   (void) seg;
1062 #endif
1063
1064   /* Get rid of any bignums now, so that we don't generate an error for which
1065      we can't establish a line number later on.  Big numbers are never valid
1066      in instructions, which is where this routine is always called.  */
1067   if (prefix_mode != GE_OPT_PREFIX_BIG
1068       && (ep->X_op == O_big
1069           || (ep->X_add_symbol
1070               && (walk_no_bignums (ep->X_add_symbol)
1071                   || (ep->X_op_symbol
1072                       && walk_no_bignums (ep->X_op_symbol))))))
1073     {
1074       inst.error = _("invalid constant");
1075       *str = input_line_pointer;
1076       input_line_pointer = save_in;
1077       return 1;
1078     }
1079
1080   *str = input_line_pointer;
1081   input_line_pointer = save_in;
1082   return 0;
1083 }
1084
1085 /* Turn a string in input_line_pointer into a floating point constant
1086    of type TYPE, and store the appropriate bytes in *LITP.  The number
1087    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1088    returned, or NULL on OK.
1089
1090    Note that fp constants aren't represent in the normal way on the ARM.
1091    In big endian mode, things are as expected.  However, in little endian
1092    mode fp constants are big-endian word-wise, and little-endian byte-wise
1093    within the words.  For example, (double) 1.1 in big endian mode is
1094    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1095    the byte sequence 99 99 f1 3f 9a 99 99 99.
1096
1097    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1098
1099 const char *
1100 md_atof (int type, char * litP, int * sizeP)
1101 {
1102   int prec;
1103   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1104   char *t;
1105   int i;
1106
1107   switch (type)
1108     {
1109     case 'f':
1110     case 'F':
1111     case 's':
1112     case 'S':
1113       prec = 2;
1114       break;
1115
1116     case 'd':
1117     case 'D':
1118     case 'r':
1119     case 'R':
1120       prec = 4;
1121       break;
1122
1123     case 'x':
1124     case 'X':
1125       prec = 5;
1126       break;
1127
1128     case 'p':
1129     case 'P':
1130       prec = 5;
1131       break;
1132
1133     default:
1134       *sizeP = 0;
1135       return _("Unrecognized or unsupported floating point constant");
1136     }
1137
1138   t = atof_ieee (input_line_pointer, type, words);
1139   if (t)
1140     input_line_pointer = t;
1141   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1142
1143   if (target_big_endian)
1144     {
1145       for (i = 0; i < prec; i++)
1146         {
1147           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1148           litP += sizeof (LITTLENUM_TYPE);
1149         }
1150     }
1151   else
1152     {
1153       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1154         for (i = prec - 1; i >= 0; i--)
1155           {
1156             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1157             litP += sizeof (LITTLENUM_TYPE);
1158           }
1159       else
1160         /* For a 4 byte float the order of elements in `words' is 1 0.
1161            For an 8 byte float the order is 1 0 3 2.  */
1162         for (i = 0; i < prec; i += 2)
1163           {
1164             md_number_to_chars (litP, (valueT) words[i + 1],
1165                                 sizeof (LITTLENUM_TYPE));
1166             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1167                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1168             litP += 2 * sizeof (LITTLENUM_TYPE);
1169           }
1170     }
1171
1172   return NULL;
1173 }
1174
1175 /* We handle all bad expressions here, so that we can report the faulty
1176    instruction in the error message.  */
1177 void
1178 md_operand (expressionS * exp)
1179 {
1180   if (in_my_get_expression)
1181     exp->X_op = O_illegal;
1182 }
1183
1184 /* Immediate values.  */
1185
1186 /* Generic immediate-value read function for use in directives.
1187    Accepts anything that 'expression' can fold to a constant.
1188    *val receives the number.  */
1189 #ifdef OBJ_ELF
1190 static int
1191 immediate_for_directive (int *val)
1192 {
1193   expressionS exp;
1194   exp.X_op = O_illegal;
1195
1196   if (is_immediate_prefix (*input_line_pointer))
1197     {
1198       input_line_pointer++;
1199       expression (&exp);
1200     }
1201
1202   if (exp.X_op != O_constant)
1203     {
1204       as_bad (_("expected #constant"));
1205       ignore_rest_of_line ();
1206       return FAIL;
1207     }
1208   *val = exp.X_add_number;
1209   return SUCCESS;
1210 }
1211 #endif
1212
1213 /* Register parsing.  */
1214
1215 /* Generic register parser.  CCP points to what should be the
1216    beginning of a register name.  If it is indeed a valid register
1217    name, advance CCP over it and return the reg_entry structure;
1218    otherwise return NULL.  Does not issue diagnostics.  */
1219
1220 static struct reg_entry *
1221 arm_reg_parse_multi (char **ccp)
1222 {
1223   char *start = *ccp;
1224   char *p;
1225   struct reg_entry *reg;
1226
1227   skip_whitespace (start);
1228
1229 #ifdef REGISTER_PREFIX
1230   if (*start != REGISTER_PREFIX)
1231     return NULL;
1232   start++;
1233 #endif
1234 #ifdef OPTIONAL_REGISTER_PREFIX
1235   if (*start == OPTIONAL_REGISTER_PREFIX)
1236     start++;
1237 #endif
1238
1239   p = start;
1240   if (!ISALPHA (*p) || !is_name_beginner (*p))
1241     return NULL;
1242
1243   do
1244     p++;
1245   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1246
1247   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1248
1249   if (!reg)
1250     return NULL;
1251
1252   *ccp = p;
1253   return reg;
1254 }
1255
1256 static int
1257 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1258                     enum arm_reg_type type)
1259 {
1260   /* Alternative syntaxes are accepted for a few register classes.  */
1261   switch (type)
1262     {
1263     case REG_TYPE_MVF:
1264     case REG_TYPE_MVD:
1265     case REG_TYPE_MVFX:
1266     case REG_TYPE_MVDX:
1267       /* Generic coprocessor register names are allowed for these.  */
1268       if (reg && reg->type == REG_TYPE_CN)
1269         return reg->number;
1270       break;
1271
1272     case REG_TYPE_CP:
1273       /* For backward compatibility, a bare number is valid here.  */
1274       {
1275         unsigned long processor = strtoul (start, ccp, 10);
1276         if (*ccp != start && processor <= 15)
1277           return processor;
1278       }
1279       /* Fall through.  */
1280
1281     case REG_TYPE_MMXWC:
1282       /* WC includes WCG.  ??? I'm not sure this is true for all
1283          instructions that take WC registers.  */
1284       if (reg && reg->type == REG_TYPE_MMXWCG)
1285         return reg->number;
1286       break;
1287
1288     default:
1289       break;
1290     }
1291
1292   return FAIL;
1293 }
1294
1295 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1296    return value is the register number or FAIL.  */
1297
1298 static int
1299 arm_reg_parse (char **ccp, enum arm_reg_type type)
1300 {
1301   char *start = *ccp;
1302   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1303   int ret;
1304
1305   /* Do not allow a scalar (reg+index) to parse as a register.  */
1306   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1307     return FAIL;
1308
1309   if (reg && reg->type == type)
1310     return reg->number;
1311
1312   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1313     return ret;
1314
1315   *ccp = start;
1316   return FAIL;
1317 }
1318
1319 /* Parse a Neon type specifier. *STR should point at the leading '.'
1320    character. Does no verification at this stage that the type fits the opcode
1321    properly. E.g.,
1322
1323      .i32.i32.s16
1324      .s32.f32
1325      .u16
1326
1327    Can all be legally parsed by this function.
1328
1329    Fills in neon_type struct pointer with parsed information, and updates STR
1330    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1331    type, FAIL if not.  */
1332
1333 static int
1334 parse_neon_type (struct neon_type *type, char **str)
1335 {
1336   char *ptr = *str;
1337
1338   if (type)
1339     type->elems = 0;
1340
1341   while (type->elems < NEON_MAX_TYPE_ELS)
1342     {
1343       enum neon_el_type thistype = NT_untyped;
1344       unsigned thissize = -1u;
1345
1346       if (*ptr != '.')
1347         break;
1348
1349       ptr++;
1350
1351       /* Just a size without an explicit type.  */
1352       if (ISDIGIT (*ptr))
1353         goto parsesize;
1354
1355       switch (TOLOWER (*ptr))
1356         {
1357         case 'i': thistype = NT_integer; break;
1358         case 'f': thistype = NT_float; break;
1359         case 'p': thistype = NT_poly; break;
1360         case 's': thistype = NT_signed; break;
1361         case 'u': thistype = NT_unsigned; break;
1362         case 'd':
1363           thistype = NT_float;
1364           thissize = 64;
1365           ptr++;
1366           goto done;
1367         default:
1368           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1369           return FAIL;
1370         }
1371
1372       ptr++;
1373
1374       /* .f is an abbreviation for .f32.  */
1375       if (thistype == NT_float && !ISDIGIT (*ptr))
1376         thissize = 32;
1377       else
1378         {
1379         parsesize:
1380           thissize = strtoul (ptr, &ptr, 10);
1381
1382           if (thissize != 8 && thissize != 16 && thissize != 32
1383               && thissize != 64)
1384             {
1385               as_bad (_("bad size %d in type specifier"), thissize);
1386               return FAIL;
1387             }
1388         }
1389
1390       done:
1391       if (type)
1392         {
1393           type->el[type->elems].type = thistype;
1394           type->el[type->elems].size = thissize;
1395           type->elems++;
1396         }
1397     }
1398
1399   /* Empty/missing type is not a successful parse.  */
1400   if (type->elems == 0)
1401     return FAIL;
1402
1403   *str = ptr;
1404
1405   return SUCCESS;
1406 }
1407
1408 /* Errors may be set multiple times during parsing or bit encoding
1409    (particularly in the Neon bits), but usually the earliest error which is set
1410    will be the most meaningful. Avoid overwriting it with later (cascading)
1411    errors by calling this function.  */
1412
1413 static void
1414 first_error (const char *err)
1415 {
1416   if (!inst.error)
1417     inst.error = err;
1418 }
1419
1420 /* Parse a single type, e.g. ".s32", leading period included.  */
1421 static int
1422 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1423 {
1424   char *str = *ccp;
1425   struct neon_type optype;
1426
1427   if (*str == '.')
1428     {
1429       if (parse_neon_type (&optype, &str) == SUCCESS)
1430         {
1431           if (optype.elems == 1)
1432             *vectype = optype.el[0];
1433           else
1434             {
1435               first_error (_("only one type should be specified for operand"));
1436               return FAIL;
1437             }
1438         }
1439       else
1440         {
1441           first_error (_("vector type expected"));
1442           return FAIL;
1443         }
1444     }
1445   else
1446     return FAIL;
1447
1448   *ccp = str;
1449
1450   return SUCCESS;
1451 }
1452
1453 /* Special meanings for indices (which have a range of 0-7), which will fit into
1454    a 4-bit integer.  */
1455
1456 #define NEON_ALL_LANES          15
1457 #define NEON_INTERLEAVE_LANES   14
1458
1459 /* Parse either a register or a scalar, with an optional type. Return the
1460    register number, and optionally fill in the actual type of the register
1461    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1462    type/index information in *TYPEINFO.  */
1463
1464 static int
1465 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1466                            enum arm_reg_type *rtype,
1467                            struct neon_typed_alias *typeinfo)
1468 {
1469   char *str = *ccp;
1470   struct reg_entry *reg = arm_reg_parse_multi (&str);
1471   struct neon_typed_alias atype;
1472   struct neon_type_el parsetype;
1473
1474   atype.defined = 0;
1475   atype.index = -1;
1476   atype.eltype.type = NT_invtype;
1477   atype.eltype.size = -1;
1478
1479   /* Try alternate syntax for some types of register. Note these are mutually
1480      exclusive with the Neon syntax extensions.  */
1481   if (reg == NULL)
1482     {
1483       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1484       if (altreg != FAIL)
1485         *ccp = str;
1486       if (typeinfo)
1487         *typeinfo = atype;
1488       return altreg;
1489     }
1490
1491   /* Undo polymorphism when a set of register types may be accepted.  */
1492   if ((type == REG_TYPE_NDQ
1493        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1494       || (type == REG_TYPE_VFSD
1495           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1496       || (type == REG_TYPE_NSDQ
1497           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1498               || reg->type == REG_TYPE_NQ))
1499       || (type == REG_TYPE_MMXWC
1500           && (reg->type == REG_TYPE_MMXWCG)))
1501     type = (enum arm_reg_type) reg->type;
1502
1503   if (type != reg->type)
1504     return FAIL;
1505
1506   if (reg->neon)
1507     atype = *reg->neon;
1508
1509   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1510     {
1511       if ((atype.defined & NTA_HASTYPE) != 0)
1512         {
1513           first_error (_("can't redefine type for operand"));
1514           return FAIL;
1515         }
1516       atype.defined |= NTA_HASTYPE;
1517       atype.eltype = parsetype;
1518     }
1519
1520   if (skip_past_char (&str, '[') == SUCCESS)
1521     {
1522       if (type != REG_TYPE_VFD)
1523         {
1524           first_error (_("only D registers may be indexed"));
1525           return FAIL;
1526         }
1527
1528       if ((atype.defined & NTA_HASINDEX) != 0)
1529         {
1530           first_error (_("can't change index for operand"));
1531           return FAIL;
1532         }
1533
1534       atype.defined |= NTA_HASINDEX;
1535
1536       if (skip_past_char (&str, ']') == SUCCESS)
1537         atype.index = NEON_ALL_LANES;
1538       else
1539         {
1540           expressionS exp;
1541
1542           my_get_expression (&exp, &str, GE_NO_PREFIX);
1543
1544           if (exp.X_op != O_constant)
1545             {
1546               first_error (_("constant expression required"));
1547               return FAIL;
1548             }
1549
1550           if (skip_past_char (&str, ']') == FAIL)
1551             return FAIL;
1552
1553           atype.index = exp.X_add_number;
1554         }
1555     }
1556
1557   if (typeinfo)
1558     *typeinfo = atype;
1559
1560   if (rtype)
1561     *rtype = type;
1562
1563   *ccp = str;
1564
1565   return reg->number;
1566 }
1567
1568 /* Like arm_reg_parse, but allow allow the following extra features:
1569     - If RTYPE is non-zero, return the (possibly restricted) type of the
1570       register (e.g. Neon double or quad reg when either has been requested).
1571     - If this is a Neon vector type with additional type information, fill
1572       in the struct pointed to by VECTYPE (if non-NULL).
1573    This function will fault on encountering a scalar.  */
1574
1575 static int
1576 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1577                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1578 {
1579   struct neon_typed_alias atype;
1580   char *str = *ccp;
1581   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1582
1583   if (reg == FAIL)
1584     return FAIL;
1585
1586   /* Do not allow regname(... to parse as a register.  */
1587   if (*str == '(')
1588     return FAIL;
1589
1590   /* Do not allow a scalar (reg+index) to parse as a register.  */
1591   if ((atype.defined & NTA_HASINDEX) != 0)
1592     {
1593       first_error (_("register operand expected, but got scalar"));
1594       return FAIL;
1595     }
1596
1597   if (vectype)
1598     *vectype = atype.eltype;
1599
1600   *ccp = str;
1601
1602   return reg;
1603 }
1604
1605 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1606 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1607
1608 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1609    have enough information to be able to do a good job bounds-checking. So, we
1610    just do easy checks here, and do further checks later.  */
1611
1612 static int
1613 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1614 {
1615   int reg;
1616   char *str = *ccp;
1617   struct neon_typed_alias atype;
1618
1619   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1620
1621   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1622     return FAIL;
1623
1624   if (atype.index == NEON_ALL_LANES)
1625     {
1626       first_error (_("scalar must have an index"));
1627       return FAIL;
1628     }
1629   else if (atype.index >= 64 / elsize)
1630     {
1631       first_error (_("scalar index out of range"));
1632       return FAIL;
1633     }
1634
1635   if (type)
1636     *type = atype.eltype;
1637
1638   *ccp = str;
1639
1640   return reg * 16 + atype.index;
1641 }
1642
1643 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1644
1645 static long
1646 parse_reg_list (char ** strp)
1647 {
1648   char * str = * strp;
1649   long   range = 0;
1650   int    another_range;
1651
1652   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1653   do
1654     {
1655       skip_whitespace (str);
1656
1657       another_range = 0;
1658
1659       if (*str == '{')
1660         {
1661           int in_range = 0;
1662           int cur_reg = -1;
1663
1664           str++;
1665           do
1666             {
1667               int reg;
1668
1669               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1670                 {
1671                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1672                   return FAIL;
1673                 }
1674
1675               if (in_range)
1676                 {
1677                   int i;
1678
1679                   if (reg <= cur_reg)
1680                     {
1681                       first_error (_("bad range in register list"));
1682                       return FAIL;
1683                     }
1684
1685                   for (i = cur_reg + 1; i < reg; i++)
1686                     {
1687                       if (range & (1 << i))
1688                         as_tsktsk
1689                           (_("Warning: duplicated register (r%d) in register list"),
1690                            i);
1691                       else
1692                         range |= 1 << i;
1693                     }
1694                   in_range = 0;
1695                 }
1696
1697               if (range & (1 << reg))
1698                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1699                            reg);
1700               else if (reg <= cur_reg)
1701                 as_tsktsk (_("Warning: register range not in ascending order"));
1702
1703               range |= 1 << reg;
1704               cur_reg = reg;
1705             }
1706           while (skip_past_comma (&str) != FAIL
1707                  || (in_range = 1, *str++ == '-'));
1708           str--;
1709
1710           if (skip_past_char (&str, '}') == FAIL)
1711             {
1712               first_error (_("missing `}'"));
1713               return FAIL;
1714             }
1715         }
1716       else
1717         {
1718           expressionS exp;
1719
1720           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1721             return FAIL;
1722
1723           if (exp.X_op == O_constant)
1724             {
1725               if (exp.X_add_number
1726                   != (exp.X_add_number & 0x0000ffff))
1727                 {
1728                   inst.error = _("invalid register mask");
1729                   return FAIL;
1730                 }
1731
1732               if ((range & exp.X_add_number) != 0)
1733                 {
1734                   int regno = range & exp.X_add_number;
1735
1736                   regno &= -regno;
1737                   regno = (1 << regno) - 1;
1738                   as_tsktsk
1739                     (_("Warning: duplicated register (r%d) in register list"),
1740                      regno);
1741                 }
1742
1743               range |= exp.X_add_number;
1744             }
1745           else
1746             {
1747               if (inst.reloc.type != 0)
1748                 {
1749                   inst.error = _("expression too complex");
1750                   return FAIL;
1751                 }
1752
1753               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1754               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1755               inst.reloc.pc_rel = 0;
1756             }
1757         }
1758
1759       if (*str == '|' || *str == '+')
1760         {
1761           str++;
1762           another_range = 1;
1763         }
1764     }
1765   while (another_range);
1766
1767   *strp = str;
1768   return range;
1769 }
1770
1771 /* Types of registers in a list.  */
1772
1773 enum reg_list_els
1774 {
1775   REGLIST_VFP_S,
1776   REGLIST_VFP_D,
1777   REGLIST_NEON_D
1778 };
1779
1780 /* Parse a VFP register list.  If the string is invalid return FAIL.
1781    Otherwise return the number of registers, and set PBASE to the first
1782    register.  Parses registers of type ETYPE.
1783    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1784      - Q registers can be used to specify pairs of D registers
1785      - { } can be omitted from around a singleton register list
1786          FIXME: This is not implemented, as it would require backtracking in
1787          some cases, e.g.:
1788            vtbl.8 d3,d4,d5
1789          This could be done (the meaning isn't really ambiguous), but doesn't
1790          fit in well with the current parsing framework.
1791      - 32 D registers may be used (also true for VFPv3).
1792    FIXME: Types are ignored in these register lists, which is probably a
1793    bug.  */
1794
1795 static int
1796 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1797 {
1798   char *str = *ccp;
1799   int base_reg;
1800   int new_base;
1801   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1802   int max_regs = 0;
1803   int count = 0;
1804   int warned = 0;
1805   unsigned long mask = 0;
1806   int i;
1807
1808   if (skip_past_char (&str, '{') == FAIL)
1809     {
1810       inst.error = _("expecting {");
1811       return FAIL;
1812     }
1813
1814   switch (etype)
1815     {
1816     case REGLIST_VFP_S:
1817       regtype = REG_TYPE_VFS;
1818       max_regs = 32;
1819       break;
1820
1821     case REGLIST_VFP_D:
1822       regtype = REG_TYPE_VFD;
1823       break;
1824
1825     case REGLIST_NEON_D:
1826       regtype = REG_TYPE_NDQ;
1827       break;
1828     }
1829
1830   if (etype != REGLIST_VFP_S)
1831     {
1832       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1833       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1834         {
1835           max_regs = 32;
1836           if (thumb_mode)
1837             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1838                                     fpu_vfp_ext_d32);
1839           else
1840             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1841                                     fpu_vfp_ext_d32);
1842         }
1843       else
1844         max_regs = 16;
1845     }
1846
1847   base_reg = max_regs;
1848
1849   do
1850     {
1851       int setmask = 1, addregs = 1;
1852
1853       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1854
1855       if (new_base == FAIL)
1856         {
1857           first_error (_(reg_expected_msgs[regtype]));
1858           return FAIL;
1859         }
1860
1861       if (new_base >= max_regs)
1862         {
1863           first_error (_("register out of range in list"));
1864           return FAIL;
1865         }
1866
1867       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1868       if (regtype == REG_TYPE_NQ)
1869         {
1870           setmask = 3;
1871           addregs = 2;
1872         }
1873
1874       if (new_base < base_reg)
1875         base_reg = new_base;
1876
1877       if (mask & (setmask << new_base))
1878         {
1879           first_error (_("invalid register list"));
1880           return FAIL;
1881         }
1882
1883       if ((mask >> new_base) != 0 && ! warned)
1884         {
1885           as_tsktsk (_("register list not in ascending order"));
1886           warned = 1;
1887         }
1888
1889       mask |= setmask << new_base;
1890       count += addregs;
1891
1892       if (*str == '-') /* We have the start of a range expression */
1893         {
1894           int high_range;
1895
1896           str++;
1897
1898           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1899               == FAIL)
1900             {
1901               inst.error = gettext (reg_expected_msgs[regtype]);
1902               return FAIL;
1903             }
1904
1905           if (high_range >= max_regs)
1906             {
1907               first_error (_("register out of range in list"));
1908               return FAIL;
1909             }
1910
1911           if (regtype == REG_TYPE_NQ)
1912             high_range = high_range + 1;
1913
1914           if (high_range <= new_base)
1915             {
1916               inst.error = _("register range not in ascending order");
1917               return FAIL;
1918             }
1919
1920           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1921             {
1922               if (mask & (setmask << new_base))
1923                 {
1924                   inst.error = _("invalid register list");
1925                   return FAIL;
1926                 }
1927
1928               mask |= setmask << new_base;
1929               count += addregs;
1930             }
1931         }
1932     }
1933   while (skip_past_comma (&str) != FAIL);
1934
1935   str++;
1936
1937   /* Sanity check -- should have raised a parse error above.  */
1938   if (count == 0 || count > max_regs)
1939     abort ();
1940
1941   *pbase = base_reg;
1942
1943   /* Final test -- the registers must be consecutive.  */
1944   mask >>= base_reg;
1945   for (i = 0; i < count; i++)
1946     {
1947       if ((mask & (1u << i)) == 0)
1948         {
1949           inst.error = _("non-contiguous register range");
1950           return FAIL;
1951         }
1952     }
1953
1954   *ccp = str;
1955
1956   return count;
1957 }
1958
1959 /* True if two alias types are the same.  */
1960
1961 static bfd_boolean
1962 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1963 {
1964   if (!a && !b)
1965     return TRUE;
1966
1967   if (!a || !b)
1968     return FALSE;
1969
1970   if (a->defined != b->defined)
1971     return FALSE;
1972
1973   if ((a->defined & NTA_HASTYPE) != 0
1974       && (a->eltype.type != b->eltype.type
1975           || a->eltype.size != b->eltype.size))
1976     return FALSE;
1977
1978   if ((a->defined & NTA_HASINDEX) != 0
1979       && (a->index != b->index))
1980     return FALSE;
1981
1982   return TRUE;
1983 }
1984
1985 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1986    The base register is put in *PBASE.
1987    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1988    the return value.
1989    The register stride (minus one) is put in bit 4 of the return value.
1990    Bits [6:5] encode the list length (minus one).
1991    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1992
1993 #define NEON_LANE(X)            ((X) & 0xf)
1994 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1995 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1996
1997 static int
1998 parse_neon_el_struct_list (char **str, unsigned *pbase,
1999                            struct neon_type_el *eltype)
2000 {
2001   char *ptr = *str;
2002   int base_reg = -1;
2003   int reg_incr = -1;
2004   int count = 0;
2005   int lane = -1;
2006   int leading_brace = 0;
2007   enum arm_reg_type rtype = REG_TYPE_NDQ;
2008   const char *const incr_error = _("register stride must be 1 or 2");
2009   const char *const type_error = _("mismatched element/structure types in list");
2010   struct neon_typed_alias firsttype;
2011   firsttype.defined = 0;
2012   firsttype.eltype.type = NT_invtype;
2013   firsttype.eltype.size = -1;
2014   firsttype.index = -1;
2015
2016   if (skip_past_char (&ptr, '{') == SUCCESS)
2017     leading_brace = 1;
2018
2019   do
2020     {
2021       struct neon_typed_alias atype;
2022       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2023
2024       if (getreg == FAIL)
2025         {
2026           first_error (_(reg_expected_msgs[rtype]));
2027           return FAIL;
2028         }
2029
2030       if (base_reg == -1)
2031         {
2032           base_reg = getreg;
2033           if (rtype == REG_TYPE_NQ)
2034             {
2035               reg_incr = 1;
2036             }
2037           firsttype = atype;
2038         }
2039       else if (reg_incr == -1)
2040         {
2041           reg_incr = getreg - base_reg;
2042           if (reg_incr < 1 || reg_incr > 2)
2043             {
2044               first_error (_(incr_error));
2045               return FAIL;
2046             }
2047         }
2048       else if (getreg != base_reg + reg_incr * count)
2049         {
2050           first_error (_(incr_error));
2051           return FAIL;
2052         }
2053
2054       if (! neon_alias_types_same (&atype, &firsttype))
2055         {
2056           first_error (_(type_error));
2057           return FAIL;
2058         }
2059
2060       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2061          modes.  */
2062       if (ptr[0] == '-')
2063         {
2064           struct neon_typed_alias htype;
2065           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2066           if (lane == -1)
2067             lane = NEON_INTERLEAVE_LANES;
2068           else if (lane != NEON_INTERLEAVE_LANES)
2069             {
2070               first_error (_(type_error));
2071               return FAIL;
2072             }
2073           if (reg_incr == -1)
2074             reg_incr = 1;
2075           else if (reg_incr != 1)
2076             {
2077               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2078               return FAIL;
2079             }
2080           ptr++;
2081           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2082           if (hireg == FAIL)
2083             {
2084               first_error (_(reg_expected_msgs[rtype]));
2085               return FAIL;
2086             }
2087           if (! neon_alias_types_same (&htype, &firsttype))
2088             {
2089               first_error (_(type_error));
2090               return FAIL;
2091             }
2092           count += hireg + dregs - getreg;
2093           continue;
2094         }
2095
2096       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2097       if (rtype == REG_TYPE_NQ)
2098         {
2099           count += 2;
2100           continue;
2101         }
2102
2103       if ((atype.defined & NTA_HASINDEX) != 0)
2104         {
2105           if (lane == -1)
2106             lane = atype.index;
2107           else if (lane != atype.index)
2108             {
2109               first_error (_(type_error));
2110               return FAIL;
2111             }
2112         }
2113       else if (lane == -1)
2114         lane = NEON_INTERLEAVE_LANES;
2115       else if (lane != NEON_INTERLEAVE_LANES)
2116         {
2117           first_error (_(type_error));
2118           return FAIL;
2119         }
2120       count++;
2121     }
2122   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2123
2124   /* No lane set by [x]. We must be interleaving structures.  */
2125   if (lane == -1)
2126     lane = NEON_INTERLEAVE_LANES;
2127
2128   /* Sanity check.  */
2129   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2130       || (count > 1 && reg_incr == -1))
2131     {
2132       first_error (_("error parsing element/structure list"));
2133       return FAIL;
2134     }
2135
2136   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2137     {
2138       first_error (_("expected }"));
2139       return FAIL;
2140     }
2141
2142   if (reg_incr == -1)
2143     reg_incr = 1;
2144
2145   if (eltype)
2146     *eltype = firsttype.eltype;
2147
2148   *pbase = base_reg;
2149   *str = ptr;
2150
2151   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2152 }
2153
2154 /* Parse an explicit relocation suffix on an expression.  This is
2155    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2156    arm_reloc_hsh contains no entries, so this function can only
2157    succeed if there is no () after the word.  Returns -1 on error,
2158    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2159
2160 static int
2161 parse_reloc (char **str)
2162 {
2163   struct reloc_entry *r;
2164   char *p, *q;
2165
2166   if (**str != '(')
2167     return BFD_RELOC_UNUSED;
2168
2169   p = *str + 1;
2170   q = p;
2171
2172   while (*q && *q != ')' && *q != ',')
2173     q++;
2174   if (*q != ')')
2175     return -1;
2176
2177   if ((r = (struct reloc_entry *)
2178        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2179     return -1;
2180
2181   *str = q + 1;
2182   return r->reloc;
2183 }
2184
2185 /* Directives: register aliases.  */
2186
2187 static struct reg_entry *
2188 insert_reg_alias (char *str, unsigned number, int type)
2189 {
2190   struct reg_entry *new_reg;
2191   const char *name;
2192
2193   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2194     {
2195       if (new_reg->builtin)
2196         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2197
2198       /* Only warn about a redefinition if it's not defined as the
2199          same register.  */
2200       else if (new_reg->number != number || new_reg->type != type)
2201         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2202
2203       return NULL;
2204     }
2205
2206   name = xstrdup (str);
2207   new_reg = XNEW (struct reg_entry);
2208
2209   new_reg->name = name;
2210   new_reg->number = number;
2211   new_reg->type = type;
2212   new_reg->builtin = FALSE;
2213   new_reg->neon = NULL;
2214
2215   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2216     abort ();
2217
2218   return new_reg;
2219 }
2220
2221 static void
2222 insert_neon_reg_alias (char *str, int number, int type,
2223                        struct neon_typed_alias *atype)
2224 {
2225   struct reg_entry *reg = insert_reg_alias (str, number, type);
2226
2227   if (!reg)
2228     {
2229       first_error (_("attempt to redefine typed alias"));
2230       return;
2231     }
2232
2233   if (atype)
2234     {
2235       reg->neon = XNEW (struct neon_typed_alias);
2236       *reg->neon = *atype;
2237     }
2238 }
2239
2240 /* Look for the .req directive.  This is of the form:
2241
2242         new_register_name .req existing_register_name
2243
2244    If we find one, or if it looks sufficiently like one that we want to
2245    handle any error here, return TRUE.  Otherwise return FALSE.  */
2246
2247 static bfd_boolean
2248 create_register_alias (char * newname, char *p)
2249 {
2250   struct reg_entry *old;
2251   char *oldname, *nbuf;
2252   size_t nlen;
2253
2254   /* The input scrubber ensures that whitespace after the mnemonic is
2255      collapsed to single spaces.  */
2256   oldname = p;
2257   if (strncmp (oldname, " .req ", 6) != 0)
2258     return FALSE;
2259
2260   oldname += 6;
2261   if (*oldname == '\0')
2262     return FALSE;
2263
2264   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2265   if (!old)
2266     {
2267       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2268       return TRUE;
2269     }
2270
2271   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2272      the desired alias name, and p points to its end.  If not, then
2273      the desired alias name is in the global original_case_string.  */
2274 #ifdef TC_CASE_SENSITIVE
2275   nlen = p - newname;
2276 #else
2277   newname = original_case_string;
2278   nlen = strlen (newname);
2279 #endif
2280
2281   nbuf = xmemdup0 (newname, nlen);
2282
2283   /* Create aliases under the new name as stated; an all-lowercase
2284      version of the new name; and an all-uppercase version of the new
2285      name.  */
2286   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2287     {
2288       for (p = nbuf; *p; p++)
2289         *p = TOUPPER (*p);
2290
2291       if (strncmp (nbuf, newname, nlen))
2292         {
2293           /* If this attempt to create an additional alias fails, do not bother
2294              trying to create the all-lower case alias.  We will fail and issue
2295              a second, duplicate error message.  This situation arises when the
2296              programmer does something like:
2297                foo .req r0
2298                Foo .req r1
2299              The second .req creates the "Foo" alias but then fails to create
2300              the artificial FOO alias because it has already been created by the
2301              first .req.  */
2302           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2303             {
2304               free (nbuf);
2305               return TRUE;
2306             }
2307         }
2308
2309       for (p = nbuf; *p; p++)
2310         *p = TOLOWER (*p);
2311
2312       if (strncmp (nbuf, newname, nlen))
2313         insert_reg_alias (nbuf, old->number, old->type);
2314     }
2315
2316   free (nbuf);
2317   return TRUE;
2318 }
2319
2320 /* Create a Neon typed/indexed register alias using directives, e.g.:
2321      X .dn d5.s32[1]
2322      Y .qn 6.s16
2323      Z .dn d7
2324      T .dn Z[0]
2325    These typed registers can be used instead of the types specified after the
2326    Neon mnemonic, so long as all operands given have types. Types can also be
2327    specified directly, e.g.:
2328      vadd d0.s32, d1.s32, d2.s32  */
2329
2330 static bfd_boolean
2331 create_neon_reg_alias (char *newname, char *p)
2332 {
2333   enum arm_reg_type basetype;
2334   struct reg_entry *basereg;
2335   struct reg_entry mybasereg;
2336   struct neon_type ntype;
2337   struct neon_typed_alias typeinfo;
2338   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2339   int namelen;
2340
2341   typeinfo.defined = 0;
2342   typeinfo.eltype.type = NT_invtype;
2343   typeinfo.eltype.size = -1;
2344   typeinfo.index = -1;
2345
2346   nameend = p;
2347
2348   if (strncmp (p, " .dn ", 5) == 0)
2349     basetype = REG_TYPE_VFD;
2350   else if (strncmp (p, " .qn ", 5) == 0)
2351     basetype = REG_TYPE_NQ;
2352   else
2353     return FALSE;
2354
2355   p += 5;
2356
2357   if (*p == '\0')
2358     return FALSE;
2359
2360   basereg = arm_reg_parse_multi (&p);
2361
2362   if (basereg && basereg->type != basetype)
2363     {
2364       as_bad (_("bad type for register"));
2365       return FALSE;
2366     }
2367
2368   if (basereg == NULL)
2369     {
2370       expressionS exp;
2371       /* Try parsing as an integer.  */
2372       my_get_expression (&exp, &p, GE_NO_PREFIX);
2373       if (exp.X_op != O_constant)
2374         {
2375           as_bad (_("expression must be constant"));
2376           return FALSE;
2377         }
2378       basereg = &mybasereg;
2379       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2380                                                   : exp.X_add_number;
2381       basereg->neon = 0;
2382     }
2383
2384   if (basereg->neon)
2385     typeinfo = *basereg->neon;
2386
2387   if (parse_neon_type (&ntype, &p) == SUCCESS)
2388     {
2389       /* We got a type.  */
2390       if (typeinfo.defined & NTA_HASTYPE)
2391         {
2392           as_bad (_("can't redefine the type of a register alias"));
2393           return FALSE;
2394         }
2395
2396       typeinfo.defined |= NTA_HASTYPE;
2397       if (ntype.elems != 1)
2398         {
2399           as_bad (_("you must specify a single type only"));
2400           return FALSE;
2401         }
2402       typeinfo.eltype = ntype.el[0];
2403     }
2404
2405   if (skip_past_char (&p, '[') == SUCCESS)
2406     {
2407       expressionS exp;
2408       /* We got a scalar index.  */
2409
2410       if (typeinfo.defined & NTA_HASINDEX)
2411         {
2412           as_bad (_("can't redefine the index of a scalar alias"));
2413           return FALSE;
2414         }
2415
2416       my_get_expression (&exp, &p, GE_NO_PREFIX);
2417
2418       if (exp.X_op != O_constant)
2419         {
2420           as_bad (_("scalar index must be constant"));
2421           return FALSE;
2422         }
2423
2424       typeinfo.defined |= NTA_HASINDEX;
2425       typeinfo.index = exp.X_add_number;
2426
2427       if (skip_past_char (&p, ']') == FAIL)
2428         {
2429           as_bad (_("expecting ]"));
2430           return FALSE;
2431         }
2432     }
2433
2434   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2435      the desired alias name, and p points to its end.  If not, then
2436      the desired alias name is in the global original_case_string.  */
2437 #ifdef TC_CASE_SENSITIVE
2438   namelen = nameend - newname;
2439 #else
2440   newname = original_case_string;
2441   namelen = strlen (newname);
2442 #endif
2443
2444   namebuf = xmemdup0 (newname, namelen);
2445
2446   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2447                          typeinfo.defined != 0 ? &typeinfo : NULL);
2448
2449   /* Insert name in all uppercase.  */
2450   for (p = namebuf; *p; p++)
2451     *p = TOUPPER (*p);
2452
2453   if (strncmp (namebuf, newname, namelen))
2454     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2455                            typeinfo.defined != 0 ? &typeinfo : NULL);
2456
2457   /* Insert name in all lowercase.  */
2458   for (p = namebuf; *p; p++)
2459     *p = TOLOWER (*p);
2460
2461   if (strncmp (namebuf, newname, namelen))
2462     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2463                            typeinfo.defined != 0 ? &typeinfo : NULL);
2464
2465   free (namebuf);
2466   return TRUE;
2467 }
2468
2469 /* Should never be called, as .req goes between the alias and the
2470    register name, not at the beginning of the line.  */
2471
2472 static void
2473 s_req (int a ATTRIBUTE_UNUSED)
2474 {
2475   as_bad (_("invalid syntax for .req directive"));
2476 }
2477
2478 static void
2479 s_dn (int a ATTRIBUTE_UNUSED)
2480 {
2481   as_bad (_("invalid syntax for .dn directive"));
2482 }
2483
2484 static void
2485 s_qn (int a ATTRIBUTE_UNUSED)
2486 {
2487   as_bad (_("invalid syntax for .qn directive"));
2488 }
2489
2490 /* The .unreq directive deletes an alias which was previously defined
2491    by .req.  For example:
2492
2493        my_alias .req r11
2494        .unreq my_alias    */
2495
2496 static void
2497 s_unreq (int a ATTRIBUTE_UNUSED)
2498 {
2499   char * name;
2500   char saved_char;
2501
2502   name = input_line_pointer;
2503
2504   while (*input_line_pointer != 0
2505          && *input_line_pointer != ' '
2506          && *input_line_pointer != '\n')
2507     ++input_line_pointer;
2508
2509   saved_char = *input_line_pointer;
2510   *input_line_pointer = 0;
2511
2512   if (!*name)
2513     as_bad (_("invalid syntax for .unreq directive"));
2514   else
2515     {
2516       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2517                                                               name);
2518
2519       if (!reg)
2520         as_bad (_("unknown register alias '%s'"), name);
2521       else if (reg->builtin)
2522         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2523                  name);
2524       else
2525         {
2526           char * p;
2527           char * nbuf;
2528
2529           hash_delete (arm_reg_hsh, name, FALSE);
2530           free ((char *) reg->name);
2531           if (reg->neon)
2532             free (reg->neon);
2533           free (reg);
2534
2535           /* Also locate the all upper case and all lower case versions.
2536              Do not complain if we cannot find one or the other as it
2537              was probably deleted above.  */
2538
2539           nbuf = strdup (name);
2540           for (p = nbuf; *p; p++)
2541             *p = TOUPPER (*p);
2542           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2543           if (reg)
2544             {
2545               hash_delete (arm_reg_hsh, nbuf, FALSE);
2546               free ((char *) reg->name);
2547               if (reg->neon)
2548                 free (reg->neon);
2549               free (reg);
2550             }
2551
2552           for (p = nbuf; *p; p++)
2553             *p = TOLOWER (*p);
2554           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2555           if (reg)
2556             {
2557               hash_delete (arm_reg_hsh, nbuf, FALSE);
2558               free ((char *) reg->name);
2559               if (reg->neon)
2560                 free (reg->neon);
2561               free (reg);
2562             }
2563
2564           free (nbuf);
2565         }
2566     }
2567
2568   *input_line_pointer = saved_char;
2569   demand_empty_rest_of_line ();
2570 }
2571
2572 /* Directives: Instruction set selection.  */
2573
2574 #ifdef OBJ_ELF
2575 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2576    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2577    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2578    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2579
2580 /* Create a new mapping symbol for the transition to STATE.  */
2581
2582 static void
2583 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2584 {
2585   symbolS * symbolP;
2586   const char * symname;
2587   int type;
2588
2589   switch (state)
2590     {
2591     case MAP_DATA:
2592       symname = "$d";
2593       type = BSF_NO_FLAGS;
2594       break;
2595     case MAP_ARM:
2596       symname = "$a";
2597       type = BSF_NO_FLAGS;
2598       break;
2599     case MAP_THUMB:
2600       symname = "$t";
2601       type = BSF_NO_FLAGS;
2602       break;
2603     default:
2604       abort ();
2605     }
2606
2607   symbolP = symbol_new (symname, now_seg, value, frag);
2608   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2609
2610   switch (state)
2611     {
2612     case MAP_ARM:
2613       THUMB_SET_FUNC (symbolP, 0);
2614       ARM_SET_THUMB (symbolP, 0);
2615       ARM_SET_INTERWORK (symbolP, support_interwork);
2616       break;
2617
2618     case MAP_THUMB:
2619       THUMB_SET_FUNC (symbolP, 1);
2620       ARM_SET_THUMB (symbolP, 1);
2621       ARM_SET_INTERWORK (symbolP, support_interwork);
2622       break;
2623
2624     case MAP_DATA:
2625     default:
2626       break;
2627     }
2628
2629   /* Save the mapping symbols for future reference.  Also check that
2630      we do not place two mapping symbols at the same offset within a
2631      frag.  We'll handle overlap between frags in
2632      check_mapping_symbols.
2633
2634      If .fill or other data filling directive generates zero sized data,
2635      the mapping symbol for the following code will have the same value
2636      as the one generated for the data filling directive.  In this case,
2637      we replace the old symbol with the new one at the same address.  */
2638   if (value == 0)
2639     {
2640       if (frag->tc_frag_data.first_map != NULL)
2641         {
2642           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2643           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2644         }
2645       frag->tc_frag_data.first_map = symbolP;
2646     }
2647   if (frag->tc_frag_data.last_map != NULL)
2648     {
2649       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2650       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2651         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2652     }
2653   frag->tc_frag_data.last_map = symbolP;
2654 }
2655
2656 /* We must sometimes convert a region marked as code to data during
2657    code alignment, if an odd number of bytes have to be padded.  The
2658    code mapping symbol is pushed to an aligned address.  */
2659
2660 static void
2661 insert_data_mapping_symbol (enum mstate state,
2662                             valueT value, fragS *frag, offsetT bytes)
2663 {
2664   /* If there was already a mapping symbol, remove it.  */
2665   if (frag->tc_frag_data.last_map != NULL
2666       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2667     {
2668       symbolS *symp = frag->tc_frag_data.last_map;
2669
2670       if (value == 0)
2671         {
2672           know (frag->tc_frag_data.first_map == symp);
2673           frag->tc_frag_data.first_map = NULL;
2674         }
2675       frag->tc_frag_data.last_map = NULL;
2676       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2677     }
2678
2679   make_mapping_symbol (MAP_DATA, value, frag);
2680   make_mapping_symbol (state, value + bytes, frag);
2681 }
2682
2683 static void mapping_state_2 (enum mstate state, int max_chars);
2684
2685 /* Set the mapping state to STATE.  Only call this when about to
2686    emit some STATE bytes to the file.  */
2687
2688 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2689 void
2690 mapping_state (enum mstate state)
2691 {
2692   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2693
2694   if (mapstate == state)
2695     /* The mapping symbol has already been emitted.
2696        There is nothing else to do.  */
2697     return;
2698
2699   if (state == MAP_ARM || state == MAP_THUMB)
2700     /*  PR gas/12931
2701         All ARM instructions require 4-byte alignment.
2702         (Almost) all Thumb instructions require 2-byte alignment.
2703
2704         When emitting instructions into any section, mark the section
2705         appropriately.
2706
2707         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2708         but themselves require 2-byte alignment; this applies to some
2709         PC- relative forms.  However, these cases will invovle implicit
2710         literal pool generation or an explicit .align >=2, both of
2711         which will cause the section to me marked with sufficient
2712         alignment.  Thus, we don't handle those cases here.  */
2713     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2714
2715   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2716     /* This case will be evaluated later.  */
2717     return;
2718
2719   mapping_state_2 (state, 0);
2720 }
2721
2722 /* Same as mapping_state, but MAX_CHARS bytes have already been
2723    allocated.  Put the mapping symbol that far back.  */
2724
2725 static void
2726 mapping_state_2 (enum mstate state, int max_chars)
2727 {
2728   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2729
2730   if (!SEG_NORMAL (now_seg))
2731     return;
2732
2733   if (mapstate == state)
2734     /* The mapping symbol has already been emitted.
2735        There is nothing else to do.  */
2736     return;
2737
2738   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2739           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2740     {
2741       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2742       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2743
2744       if (add_symbol)
2745         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2746     }
2747
2748   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2749   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2750 }
2751 #undef TRANSITION
2752 #else
2753 #define mapping_state(x) ((void)0)
2754 #define mapping_state_2(x, y) ((void)0)
2755 #endif
2756
2757 /* Find the real, Thumb encoded start of a Thumb function.  */
2758
2759 #ifdef OBJ_COFF
2760 static symbolS *
2761 find_real_start (symbolS * symbolP)
2762 {
2763   char *       real_start;
2764   const char * name = S_GET_NAME (symbolP);
2765   symbolS *    new_target;
2766
2767   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2768 #define STUB_NAME ".real_start_of"
2769
2770   if (name == NULL)
2771     abort ();
2772
2773   /* The compiler may generate BL instructions to local labels because
2774      it needs to perform a branch to a far away location. These labels
2775      do not have a corresponding ".real_start_of" label.  We check
2776      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2777      the ".real_start_of" convention for nonlocal branches.  */
2778   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2779     return symbolP;
2780
2781   real_start = concat (STUB_NAME, name, NULL);
2782   new_target = symbol_find (real_start);
2783   free (real_start);
2784
2785   if (new_target == NULL)
2786     {
2787       as_warn (_("Failed to find real start of function: %s\n"), name);
2788       new_target = symbolP;
2789     }
2790
2791   return new_target;
2792 }
2793 #endif
2794
2795 static void
2796 opcode_select (int width)
2797 {
2798   switch (width)
2799     {
2800     case 16:
2801       if (! thumb_mode)
2802         {
2803           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2804             as_bad (_("selected processor does not support THUMB opcodes"));
2805
2806           thumb_mode = 1;
2807           /* No need to force the alignment, since we will have been
2808              coming from ARM mode, which is word-aligned.  */
2809           record_alignment (now_seg, 1);
2810         }
2811       break;
2812
2813     case 32:
2814       if (thumb_mode)
2815         {
2816           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2817             as_bad (_("selected processor does not support ARM opcodes"));
2818
2819           thumb_mode = 0;
2820
2821           if (!need_pass_2)
2822             frag_align (2, 0, 0);
2823
2824           record_alignment (now_seg, 1);
2825         }
2826       break;
2827
2828     default:
2829       as_bad (_("invalid instruction size selected (%d)"), width);
2830     }
2831 }
2832
2833 static void
2834 s_arm (int ignore ATTRIBUTE_UNUSED)
2835 {
2836   opcode_select (32);
2837   demand_empty_rest_of_line ();
2838 }
2839
2840 static void
2841 s_thumb (int ignore ATTRIBUTE_UNUSED)
2842 {
2843   opcode_select (16);
2844   demand_empty_rest_of_line ();
2845 }
2846
2847 static void
2848 s_code (int unused ATTRIBUTE_UNUSED)
2849 {
2850   int temp;
2851
2852   temp = get_absolute_expression ();
2853   switch (temp)
2854     {
2855     case 16:
2856     case 32:
2857       opcode_select (temp);
2858       break;
2859
2860     default:
2861       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2862     }
2863 }
2864
2865 static void
2866 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2867 {
2868   /* If we are not already in thumb mode go into it, EVEN if
2869      the target processor does not support thumb instructions.
2870      This is used by gcc/config/arm/lib1funcs.asm for example
2871      to compile interworking support functions even if the
2872      target processor should not support interworking.  */
2873   if (! thumb_mode)
2874     {
2875       thumb_mode = 2;
2876       record_alignment (now_seg, 1);
2877     }
2878
2879   demand_empty_rest_of_line ();
2880 }
2881
2882 static void
2883 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2884 {
2885   s_thumb (0);
2886
2887   /* The following label is the name/address of the start of a Thumb function.
2888      We need to know this for the interworking support.  */
2889   label_is_thumb_function_name = TRUE;
2890 }
2891
2892 /* Perform a .set directive, but also mark the alias as
2893    being a thumb function.  */
2894
2895 static void
2896 s_thumb_set (int equiv)
2897 {
2898   /* XXX the following is a duplicate of the code for s_set() in read.c
2899      We cannot just call that code as we need to get at the symbol that
2900      is created.  */
2901   char *    name;
2902   char      delim;
2903   char *    end_name;
2904   symbolS * symbolP;
2905
2906   /* Especial apologies for the random logic:
2907      This just grew, and could be parsed much more simply!
2908      Dean - in haste.  */
2909   delim     = get_symbol_name (& name);
2910   end_name  = input_line_pointer;
2911   (void) restore_line_pointer (delim);
2912
2913   if (*input_line_pointer != ',')
2914     {
2915       *end_name = 0;
2916       as_bad (_("expected comma after name \"%s\""), name);
2917       *end_name = delim;
2918       ignore_rest_of_line ();
2919       return;
2920     }
2921
2922   input_line_pointer++;
2923   *end_name = 0;
2924
2925   if (name[0] == '.' && name[1] == '\0')
2926     {
2927       /* XXX - this should not happen to .thumb_set.  */
2928       abort ();
2929     }
2930
2931   if ((symbolP = symbol_find (name)) == NULL
2932       && (symbolP = md_undefined_symbol (name)) == NULL)
2933     {
2934 #ifndef NO_LISTING
2935       /* When doing symbol listings, play games with dummy fragments living
2936          outside the normal fragment chain to record the file and line info
2937          for this symbol.  */
2938       if (listing & LISTING_SYMBOLS)
2939         {
2940           extern struct list_info_struct * listing_tail;
2941           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2942
2943           memset (dummy_frag, 0, sizeof (fragS));
2944           dummy_frag->fr_type = rs_fill;
2945           dummy_frag->line = listing_tail;
2946           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2947           dummy_frag->fr_symbol = symbolP;
2948         }
2949       else
2950 #endif
2951         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2952
2953 #ifdef OBJ_COFF
2954       /* "set" symbols are local unless otherwise specified.  */
2955       SF_SET_LOCAL (symbolP);
2956 #endif /* OBJ_COFF  */
2957     }                           /* Make a new symbol.  */
2958
2959   symbol_table_insert (symbolP);
2960
2961   * end_name = delim;
2962
2963   if (equiv
2964       && S_IS_DEFINED (symbolP)
2965       && S_GET_SEGMENT (symbolP) != reg_section)
2966     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2967
2968   pseudo_set (symbolP);
2969
2970   demand_empty_rest_of_line ();
2971
2972   /* XXX Now we come to the Thumb specific bit of code.  */
2973
2974   THUMB_SET_FUNC (symbolP, 1);
2975   ARM_SET_THUMB (symbolP, 1);
2976 #if defined OBJ_ELF || defined OBJ_COFF
2977   ARM_SET_INTERWORK (symbolP, support_interwork);
2978 #endif
2979 }
2980
2981 /* Directives: Mode selection.  */
2982
2983 /* .syntax [unified|divided] - choose the new unified syntax
2984    (same for Arm and Thumb encoding, modulo slight differences in what
2985    can be represented) or the old divergent syntax for each mode.  */
2986 static void
2987 s_syntax (int unused ATTRIBUTE_UNUSED)
2988 {
2989   char *name, delim;
2990
2991   delim = get_symbol_name (& name);
2992
2993   if (!strcasecmp (name, "unified"))
2994     unified_syntax = TRUE;
2995   else if (!strcasecmp (name, "divided"))
2996     unified_syntax = FALSE;
2997   else
2998     {
2999       as_bad (_("unrecognized syntax mode \"%s\""), name);
3000       return;
3001     }
3002   (void) restore_line_pointer (delim);
3003   demand_empty_rest_of_line ();
3004 }
3005
3006 /* Directives: sectioning and alignment.  */
3007
3008 static void
3009 s_bss (int ignore ATTRIBUTE_UNUSED)
3010 {
3011   /* We don't support putting frags in the BSS segment, we fake it by
3012      marking in_bss, then looking at s_skip for clues.  */
3013   subseg_set (bss_section, 0);
3014   demand_empty_rest_of_line ();
3015
3016 #ifdef md_elf_section_change_hook
3017   md_elf_section_change_hook ();
3018 #endif
3019 }
3020
3021 static void
3022 s_even (int ignore ATTRIBUTE_UNUSED)
3023 {
3024   /* Never make frag if expect extra pass.  */
3025   if (!need_pass_2)
3026     frag_align (1, 0, 0);
3027
3028   record_alignment (now_seg, 1);
3029
3030   demand_empty_rest_of_line ();
3031 }
3032
3033 /* Directives: CodeComposer Studio.  */
3034
3035 /*  .ref  (for CodeComposer Studio syntax only).  */
3036 static void
3037 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3038 {
3039   if (codecomposer_syntax)
3040     ignore_rest_of_line ();
3041   else
3042     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3043 }
3044
3045 /*  If name is not NULL, then it is used for marking the beginning of a
3046     function, wherease if it is NULL then it means the function end.  */
3047 static void
3048 asmfunc_debug (const char * name)
3049 {
3050   static const char * last_name = NULL;
3051
3052   if (name != NULL)
3053     {
3054       gas_assert (last_name == NULL);
3055       last_name = name;
3056
3057       if (debug_type == DEBUG_STABS)
3058          stabs_generate_asm_func (name, name);
3059     }
3060   else
3061     {
3062       gas_assert (last_name != NULL);
3063
3064       if (debug_type == DEBUG_STABS)
3065         stabs_generate_asm_endfunc (last_name, last_name);
3066
3067       last_name = NULL;
3068     }
3069 }
3070
3071 static void
3072 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3073 {
3074   if (codecomposer_syntax)
3075     {
3076       switch (asmfunc_state)
3077         {
3078         case OUTSIDE_ASMFUNC:
3079           asmfunc_state = WAITING_ASMFUNC_NAME;
3080           break;
3081
3082         case WAITING_ASMFUNC_NAME:
3083           as_bad (_(".asmfunc repeated."));
3084           break;
3085
3086         case WAITING_ENDASMFUNC:
3087           as_bad (_(".asmfunc without function."));
3088           break;
3089         }
3090       demand_empty_rest_of_line ();
3091     }
3092   else
3093     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3094 }
3095
3096 static void
3097 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3098 {
3099   if (codecomposer_syntax)
3100     {
3101       switch (asmfunc_state)
3102         {
3103         case OUTSIDE_ASMFUNC:
3104           as_bad (_(".endasmfunc without a .asmfunc."));
3105           break;
3106
3107         case WAITING_ASMFUNC_NAME:
3108           as_bad (_(".endasmfunc without function."));
3109           break;
3110
3111         case WAITING_ENDASMFUNC:
3112           asmfunc_state = OUTSIDE_ASMFUNC;
3113           asmfunc_debug (NULL);
3114           break;
3115         }
3116       demand_empty_rest_of_line ();
3117     }
3118   else
3119     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3120 }
3121
3122 static void
3123 s_ccs_def (int name)
3124 {
3125   if (codecomposer_syntax)
3126     s_globl (name);
3127   else
3128     as_bad (_(".def pseudo-op only available with -mccs flag."));
3129 }
3130
3131 /* Directives: Literal pools.  */
3132
3133 static literal_pool *
3134 find_literal_pool (void)
3135 {
3136   literal_pool * pool;
3137
3138   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3139     {
3140       if (pool->section == now_seg
3141           && pool->sub_section == now_subseg)
3142         break;
3143     }
3144
3145   return pool;
3146 }
3147
3148 static literal_pool *
3149 find_or_make_literal_pool (void)
3150 {
3151   /* Next literal pool ID number.  */
3152   static unsigned int latest_pool_num = 1;
3153   literal_pool *      pool;
3154
3155   pool = find_literal_pool ();
3156
3157   if (pool == NULL)
3158     {
3159       /* Create a new pool.  */
3160       pool = XNEW (literal_pool);
3161       if (! pool)
3162         return NULL;
3163
3164       pool->next_free_entry = 0;
3165       pool->section         = now_seg;
3166       pool->sub_section     = now_subseg;
3167       pool->next            = list_of_pools;
3168       pool->symbol          = NULL;
3169       pool->alignment       = 2;
3170
3171       /* Add it to the list.  */
3172       list_of_pools = pool;
3173     }
3174
3175   /* New pools, and emptied pools, will have a NULL symbol.  */
3176   if (pool->symbol == NULL)
3177     {
3178       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3179                                     (valueT) 0, &zero_address_frag);
3180       pool->id = latest_pool_num ++;
3181     }
3182
3183   /* Done.  */
3184   return pool;
3185 }
3186
3187 /* Add the literal in the global 'inst'
3188    structure to the relevant literal pool.  */
3189
3190 static int
3191 add_to_lit_pool (unsigned int nbytes)
3192 {
3193 #define PADDING_SLOT 0x1
3194 #define LIT_ENTRY_SIZE_MASK 0xFF
3195   literal_pool * pool;
3196   unsigned int entry, pool_size = 0;
3197   bfd_boolean padding_slot_p = FALSE;
3198   unsigned imm1 = 0;
3199   unsigned imm2 = 0;
3200
3201   if (nbytes == 8)
3202     {
3203       imm1 = inst.operands[1].imm;
3204       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3205                : inst.reloc.exp.X_unsigned ? 0
3206                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3207       if (target_big_endian)
3208         {
3209           imm1 = imm2;
3210           imm2 = inst.operands[1].imm;
3211         }
3212     }
3213
3214   pool = find_or_make_literal_pool ();
3215
3216   /* Check if this literal value is already in the pool.  */
3217   for (entry = 0; entry < pool->next_free_entry; entry ++)
3218     {
3219       if (nbytes == 4)
3220         {
3221           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3222               && (inst.reloc.exp.X_op == O_constant)
3223               && (pool->literals[entry].X_add_number
3224                   == inst.reloc.exp.X_add_number)
3225               && (pool->literals[entry].X_md == nbytes)
3226               && (pool->literals[entry].X_unsigned
3227                   == inst.reloc.exp.X_unsigned))
3228             break;
3229
3230           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3231               && (inst.reloc.exp.X_op == O_symbol)
3232               && (pool->literals[entry].X_add_number
3233                   == inst.reloc.exp.X_add_number)
3234               && (pool->literals[entry].X_add_symbol
3235                   == inst.reloc.exp.X_add_symbol)
3236               && (pool->literals[entry].X_op_symbol
3237                   == inst.reloc.exp.X_op_symbol)
3238               && (pool->literals[entry].X_md == nbytes))
3239             break;
3240         }
3241       else if ((nbytes == 8)
3242                && !(pool_size & 0x7)
3243                && ((entry + 1) != pool->next_free_entry)
3244                && (pool->literals[entry].X_op == O_constant)
3245                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3246                && (pool->literals[entry].X_unsigned
3247                    == inst.reloc.exp.X_unsigned)
3248                && (pool->literals[entry + 1].X_op == O_constant)
3249                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3250                && (pool->literals[entry + 1].X_unsigned
3251                    == inst.reloc.exp.X_unsigned))
3252         break;
3253
3254       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3255       if (padding_slot_p && (nbytes == 4))
3256         break;
3257
3258       pool_size += 4;
3259     }
3260
3261   /* Do we need to create a new entry?  */
3262   if (entry == pool->next_free_entry)
3263     {
3264       if (entry >= MAX_LITERAL_POOL_SIZE)
3265         {
3266           inst.error = _("literal pool overflow");
3267           return FAIL;
3268         }
3269
3270       if (nbytes == 8)
3271         {
3272           /* For 8-byte entries, we align to an 8-byte boundary,
3273              and split it into two 4-byte entries, because on 32-bit
3274              host, 8-byte constants are treated as big num, thus
3275              saved in "generic_bignum" which will be overwritten
3276              by later assignments.
3277
3278              We also need to make sure there is enough space for
3279              the split.
3280
3281              We also check to make sure the literal operand is a
3282              constant number.  */
3283           if (!(inst.reloc.exp.X_op == O_constant
3284                 || inst.reloc.exp.X_op == O_big))
3285             {
3286               inst.error = _("invalid type for literal pool");
3287               return FAIL;
3288             }
3289           else if (pool_size & 0x7)
3290             {
3291               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3292                 {
3293                   inst.error = _("literal pool overflow");
3294                   return FAIL;
3295                 }
3296
3297               pool->literals[entry] = inst.reloc.exp;
3298               pool->literals[entry].X_op = O_constant;
3299               pool->literals[entry].X_add_number = 0;
3300               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3301               pool->next_free_entry += 1;
3302               pool_size += 4;
3303             }
3304           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3305             {
3306               inst.error = _("literal pool overflow");
3307               return FAIL;
3308             }
3309
3310           pool->literals[entry] = inst.reloc.exp;
3311           pool->literals[entry].X_op = O_constant;
3312           pool->literals[entry].X_add_number = imm1;
3313           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3314           pool->literals[entry++].X_md = 4;
3315           pool->literals[entry] = inst.reloc.exp;
3316           pool->literals[entry].X_op = O_constant;
3317           pool->literals[entry].X_add_number = imm2;
3318           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3319           pool->literals[entry].X_md = 4;
3320           pool->alignment = 3;
3321           pool->next_free_entry += 1;
3322         }
3323       else
3324         {
3325           pool->literals[entry] = inst.reloc.exp;
3326           pool->literals[entry].X_md = 4;
3327         }
3328
3329 #ifdef OBJ_ELF
3330       /* PR ld/12974: Record the location of the first source line to reference
3331          this entry in the literal pool.  If it turns out during linking that the
3332          symbol does not exist we will be able to give an accurate line number for
3333          the (first use of the) missing reference.  */
3334       if (debug_type == DEBUG_DWARF2)
3335         dwarf2_where (pool->locs + entry);
3336 #endif
3337       pool->next_free_entry += 1;
3338     }
3339   else if (padding_slot_p)
3340     {
3341       pool->literals[entry] = inst.reloc.exp;
3342       pool->literals[entry].X_md = nbytes;
3343     }
3344
3345   inst.reloc.exp.X_op         = O_symbol;
3346   inst.reloc.exp.X_add_number = pool_size;
3347   inst.reloc.exp.X_add_symbol = pool->symbol;
3348
3349   return SUCCESS;
3350 }
3351
3352 bfd_boolean
3353 tc_start_label_without_colon (void)
3354 {
3355   bfd_boolean ret = TRUE;
3356
3357   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3358     {
3359       const char *label = input_line_pointer;
3360
3361       while (!is_end_of_line[(int) label[-1]])
3362         --label;
3363
3364       if (*label == '.')
3365         {
3366           as_bad (_("Invalid label '%s'"), label);
3367           ret = FALSE;
3368         }
3369
3370       asmfunc_debug (label);
3371
3372       asmfunc_state = WAITING_ENDASMFUNC;
3373     }
3374
3375   return ret;
3376 }
3377
3378 /* Can't use symbol_new here, so have to create a symbol and then at
3379    a later date assign it a value. Thats what these functions do.  */
3380
3381 static void
3382 symbol_locate (symbolS *    symbolP,
3383                const char * name,       /* It is copied, the caller can modify.  */
3384                segT         segment,    /* Segment identifier (SEG_<something>).  */
3385                valueT       valu,       /* Symbol value.  */
3386                fragS *      frag)       /* Associated fragment.  */
3387 {
3388   size_t name_length;
3389   char * preserved_copy_of_name;
3390
3391   name_length = strlen (name) + 1;   /* +1 for \0.  */
3392   obstack_grow (&notes, name, name_length);
3393   preserved_copy_of_name = (char *) obstack_finish (&notes);
3394
3395 #ifdef tc_canonicalize_symbol_name
3396   preserved_copy_of_name =
3397     tc_canonicalize_symbol_name (preserved_copy_of_name);
3398 #endif
3399
3400   S_SET_NAME (symbolP, preserved_copy_of_name);
3401
3402   S_SET_SEGMENT (symbolP, segment);
3403   S_SET_VALUE (symbolP, valu);
3404   symbol_clear_list_pointers (symbolP);
3405
3406   symbol_set_frag (symbolP, frag);
3407
3408   /* Link to end of symbol chain.  */
3409   {
3410     extern int symbol_table_frozen;
3411
3412     if (symbol_table_frozen)
3413       abort ();
3414   }
3415
3416   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3417
3418   obj_symbol_new_hook (symbolP);
3419
3420 #ifdef tc_symbol_new_hook
3421   tc_symbol_new_hook (symbolP);
3422 #endif
3423
3424 #ifdef DEBUG_SYMS
3425   verify_symbol_chain (symbol_rootP, symbol_lastP);
3426 #endif /* DEBUG_SYMS  */
3427 }
3428
3429 static void
3430 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3431 {
3432   unsigned int entry;
3433   literal_pool * pool;
3434   char sym_name[20];
3435
3436   pool = find_literal_pool ();
3437   if (pool == NULL
3438       || pool->symbol == NULL
3439       || pool->next_free_entry == 0)
3440     return;
3441
3442   /* Align pool as you have word accesses.
3443      Only make a frag if we have to.  */
3444   if (!need_pass_2)
3445     frag_align (pool->alignment, 0, 0);
3446
3447   record_alignment (now_seg, 2);
3448
3449 #ifdef OBJ_ELF
3450   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3451   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3452 #endif
3453   sprintf (sym_name, "$$lit_\002%x", pool->id);
3454
3455   symbol_locate (pool->symbol, sym_name, now_seg,
3456                  (valueT) frag_now_fix (), frag_now);
3457   symbol_table_insert (pool->symbol);
3458
3459   ARM_SET_THUMB (pool->symbol, thumb_mode);
3460
3461 #if defined OBJ_COFF || defined OBJ_ELF
3462   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3463 #endif
3464
3465   for (entry = 0; entry < pool->next_free_entry; entry ++)
3466     {
3467 #ifdef OBJ_ELF
3468       if (debug_type == DEBUG_DWARF2)
3469         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3470 #endif
3471       /* First output the expression in the instruction to the pool.  */
3472       emit_expr (&(pool->literals[entry]),
3473                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3474     }
3475
3476   /* Mark the pool as empty.  */
3477   pool->next_free_entry = 0;
3478   pool->symbol = NULL;
3479 }
3480
3481 #ifdef OBJ_ELF
3482 /* Forward declarations for functions below, in the MD interface
3483    section.  */
3484 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3485 static valueT create_unwind_entry (int);
3486 static void start_unwind_section (const segT, int);
3487 static void add_unwind_opcode (valueT, int);
3488 static void flush_pending_unwind (void);
3489
3490 /* Directives: Data.  */
3491
3492 static void
3493 s_arm_elf_cons (int nbytes)
3494 {
3495   expressionS exp;
3496
3497 #ifdef md_flush_pending_output
3498   md_flush_pending_output ();
3499 #endif
3500
3501   if (is_it_end_of_statement ())
3502     {
3503       demand_empty_rest_of_line ();
3504       return;
3505     }
3506
3507 #ifdef md_cons_align
3508   md_cons_align (nbytes);
3509 #endif
3510
3511   mapping_state (MAP_DATA);
3512   do
3513     {
3514       int reloc;
3515       char *base = input_line_pointer;
3516
3517       expression (& exp);
3518
3519       if (exp.X_op != O_symbol)
3520         emit_expr (&exp, (unsigned int) nbytes);
3521       else
3522         {
3523           char *before_reloc = input_line_pointer;
3524           reloc = parse_reloc (&input_line_pointer);
3525           if (reloc == -1)
3526             {
3527               as_bad (_("unrecognized relocation suffix"));
3528               ignore_rest_of_line ();
3529               return;
3530             }
3531           else if (reloc == BFD_RELOC_UNUSED)
3532             emit_expr (&exp, (unsigned int) nbytes);
3533           else
3534             {
3535               reloc_howto_type *howto = (reloc_howto_type *)
3536                   bfd_reloc_type_lookup (stdoutput,
3537                                          (bfd_reloc_code_real_type) reloc);
3538               int size = bfd_get_reloc_size (howto);
3539
3540               if (reloc == BFD_RELOC_ARM_PLT32)
3541                 {
3542                   as_bad (_("(plt) is only valid on branch targets"));
3543                   reloc = BFD_RELOC_UNUSED;
3544                   size = 0;
3545                 }
3546
3547               if (size > nbytes)
3548                 as_bad (_("%s relocations do not fit in %d bytes"),
3549                         howto->name, nbytes);
3550               else
3551                 {
3552                   /* We've parsed an expression stopping at O_symbol.
3553                      But there may be more expression left now that we
3554                      have parsed the relocation marker.  Parse it again.
3555                      XXX Surely there is a cleaner way to do this.  */
3556                   char *p = input_line_pointer;
3557                   int offset;
3558                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3559
3560                   memcpy (save_buf, base, input_line_pointer - base);
3561                   memmove (base + (input_line_pointer - before_reloc),
3562                            base, before_reloc - base);
3563
3564                   input_line_pointer = base + (input_line_pointer-before_reloc);
3565                   expression (&exp);
3566                   memcpy (base, save_buf, p - base);
3567
3568                   offset = nbytes - size;
3569                   p = frag_more (nbytes);
3570                   memset (p, 0, nbytes);
3571                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3572                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3573                   free (save_buf);
3574                 }
3575             }
3576         }
3577     }
3578   while (*input_line_pointer++ == ',');
3579
3580   /* Put terminator back into stream.  */
3581   input_line_pointer --;
3582   demand_empty_rest_of_line ();
3583 }
3584
3585 /* Emit an expression containing a 32-bit thumb instruction.
3586    Implementation based on put_thumb32_insn.  */
3587
3588 static void
3589 emit_thumb32_expr (expressionS * exp)
3590 {
3591   expressionS exp_high = *exp;
3592
3593   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3594   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3595   exp->X_add_number &= 0xffff;
3596   emit_expr (exp, (unsigned int) THUMB_SIZE);
3597 }
3598
3599 /*  Guess the instruction size based on the opcode.  */
3600
3601 static int
3602 thumb_insn_size (int opcode)
3603 {
3604   if ((unsigned int) opcode < 0xe800u)
3605     return 2;
3606   else if ((unsigned int) opcode >= 0xe8000000u)
3607     return 4;
3608   else
3609     return 0;
3610 }
3611
3612 static bfd_boolean
3613 emit_insn (expressionS *exp, int nbytes)
3614 {
3615   int size = 0;
3616
3617   if (exp->X_op == O_constant)
3618     {
3619       size = nbytes;
3620
3621       if (size == 0)
3622         size = thumb_insn_size (exp->X_add_number);
3623
3624       if (size != 0)
3625         {
3626           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3627             {
3628               as_bad (_(".inst.n operand too big. "\
3629                         "Use .inst.w instead"));
3630               size = 0;
3631             }
3632           else
3633             {
3634               if (now_it.state == AUTOMATIC_IT_BLOCK)
3635                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3636               else
3637                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3638
3639               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3640                 emit_thumb32_expr (exp);
3641               else
3642                 emit_expr (exp, (unsigned int) size);
3643
3644               it_fsm_post_encode ();
3645             }
3646         }
3647       else
3648         as_bad (_("cannot determine Thumb instruction size. "   \
3649                   "Use .inst.n/.inst.w instead"));
3650     }
3651   else
3652     as_bad (_("constant expression required"));
3653
3654   return (size != 0);
3655 }
3656
3657 /* Like s_arm_elf_cons but do not use md_cons_align and
3658    set the mapping state to MAP_ARM/MAP_THUMB.  */
3659
3660 static void
3661 s_arm_elf_inst (int nbytes)
3662 {
3663   if (is_it_end_of_statement ())
3664     {
3665       demand_empty_rest_of_line ();
3666       return;
3667     }
3668
3669   /* Calling mapping_state () here will not change ARM/THUMB,
3670      but will ensure not to be in DATA state.  */
3671
3672   if (thumb_mode)
3673     mapping_state (MAP_THUMB);
3674   else
3675     {
3676       if (nbytes != 0)
3677         {
3678           as_bad (_("width suffixes are invalid in ARM mode"));
3679           ignore_rest_of_line ();
3680           return;
3681         }
3682
3683       nbytes = 4;
3684
3685       mapping_state (MAP_ARM);
3686     }
3687
3688   do
3689     {
3690       expressionS exp;
3691
3692       expression (& exp);
3693
3694       if (! emit_insn (& exp, nbytes))
3695         {
3696           ignore_rest_of_line ();
3697           return;
3698         }
3699     }
3700   while (*input_line_pointer++ == ',');
3701
3702   /* Put terminator back into stream.  */
3703   input_line_pointer --;
3704   demand_empty_rest_of_line ();
3705 }
3706
3707 /* Parse a .rel31 directive.  */
3708
3709 static void
3710 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3711 {
3712   expressionS exp;
3713   char *p;
3714   valueT highbit;
3715
3716   highbit = 0;
3717   if (*input_line_pointer == '1')
3718     highbit = 0x80000000;
3719   else if (*input_line_pointer != '0')
3720     as_bad (_("expected 0 or 1"));
3721
3722   input_line_pointer++;
3723   if (*input_line_pointer != ',')
3724     as_bad (_("missing comma"));
3725   input_line_pointer++;
3726
3727 #ifdef md_flush_pending_output
3728   md_flush_pending_output ();
3729 #endif
3730
3731 #ifdef md_cons_align
3732   md_cons_align (4);
3733 #endif
3734
3735   mapping_state (MAP_DATA);
3736
3737   expression (&exp);
3738
3739   p = frag_more (4);
3740   md_number_to_chars (p, highbit, 4);
3741   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3742                BFD_RELOC_ARM_PREL31);
3743
3744   demand_empty_rest_of_line ();
3745 }
3746
3747 /* Directives: AEABI stack-unwind tables.  */
3748
3749 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3750
3751 static void
3752 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3753 {
3754   demand_empty_rest_of_line ();
3755   if (unwind.proc_start)
3756     {
3757       as_bad (_("duplicate .fnstart directive"));
3758       return;
3759     }
3760
3761   /* Mark the start of the function.  */
3762   unwind.proc_start = expr_build_dot ();
3763
3764   /* Reset the rest of the unwind info.  */
3765   unwind.opcode_count = 0;
3766   unwind.table_entry = NULL;
3767   unwind.personality_routine = NULL;
3768   unwind.personality_index = -1;
3769   unwind.frame_size = 0;
3770   unwind.fp_offset = 0;
3771   unwind.fp_reg = REG_SP;
3772   unwind.fp_used = 0;
3773   unwind.sp_restored = 0;
3774 }
3775
3776
3777 /* Parse a handlerdata directive.  Creates the exception handling table entry
3778    for the function.  */
3779
3780 static void
3781 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3782 {
3783   demand_empty_rest_of_line ();
3784   if (!unwind.proc_start)
3785     as_bad (MISSING_FNSTART);
3786
3787   if (unwind.table_entry)
3788     as_bad (_("duplicate .handlerdata directive"));
3789
3790   create_unwind_entry (1);
3791 }
3792
3793 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3794
3795 static void
3796 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3797 {
3798   long where;
3799   char *ptr;
3800   valueT val;
3801   unsigned int marked_pr_dependency;
3802
3803   demand_empty_rest_of_line ();
3804
3805   if (!unwind.proc_start)
3806     {
3807       as_bad (_(".fnend directive without .fnstart"));
3808       return;
3809     }
3810
3811   /* Add eh table entry.  */
3812   if (unwind.table_entry == NULL)
3813     val = create_unwind_entry (0);
3814   else
3815     val = 0;
3816
3817   /* Add index table entry.  This is two words.  */
3818   start_unwind_section (unwind.saved_seg, 1);
3819   frag_align (2, 0, 0);
3820   record_alignment (now_seg, 2);
3821
3822   ptr = frag_more (8);
3823   memset (ptr, 0, 8);
3824   where = frag_now_fix () - 8;
3825
3826   /* Self relative offset of the function start.  */
3827   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3828            BFD_RELOC_ARM_PREL31);
3829
3830   /* Indicate dependency on EHABI-defined personality routines to the
3831      linker, if it hasn't been done already.  */
3832   marked_pr_dependency
3833     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3834   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3835       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3836     {
3837       static const char *const name[] =
3838         {
3839           "__aeabi_unwind_cpp_pr0",
3840           "__aeabi_unwind_cpp_pr1",
3841           "__aeabi_unwind_cpp_pr2"
3842         };
3843       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3844       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3845       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3846         |= 1 << unwind.personality_index;
3847     }
3848
3849   if (val)
3850     /* Inline exception table entry.  */
3851     md_number_to_chars (ptr + 4, val, 4);
3852   else
3853     /* Self relative offset of the table entry.  */
3854     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3855              BFD_RELOC_ARM_PREL31);
3856
3857   /* Restore the original section.  */
3858   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3859
3860   unwind.proc_start = NULL;
3861 }
3862
3863
3864 /* Parse an unwind_cantunwind directive.  */
3865
3866 static void
3867 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3868 {
3869   demand_empty_rest_of_line ();
3870   if (!unwind.proc_start)
3871     as_bad (MISSING_FNSTART);
3872
3873   if (unwind.personality_routine || unwind.personality_index != -1)
3874     as_bad (_("personality routine specified for cantunwind frame"));
3875
3876   unwind.personality_index = -2;
3877 }
3878
3879
3880 /* Parse a personalityindex directive.  */
3881
3882 static void
3883 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3884 {
3885   expressionS exp;
3886
3887   if (!unwind.proc_start)
3888     as_bad (MISSING_FNSTART);
3889
3890   if (unwind.personality_routine || unwind.personality_index != -1)
3891     as_bad (_("duplicate .personalityindex directive"));
3892
3893   expression (&exp);
3894
3895   if (exp.X_op != O_constant
3896       || exp.X_add_number < 0 || exp.X_add_number > 15)
3897     {
3898       as_bad (_("bad personality routine number"));
3899       ignore_rest_of_line ();
3900       return;
3901     }
3902
3903   unwind.personality_index = exp.X_add_number;
3904
3905   demand_empty_rest_of_line ();
3906 }
3907
3908
3909 /* Parse a personality directive.  */
3910
3911 static void
3912 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3913 {
3914   char *name, *p, c;
3915
3916   if (!unwind.proc_start)
3917     as_bad (MISSING_FNSTART);
3918
3919   if (unwind.personality_routine || unwind.personality_index != -1)
3920     as_bad (_("duplicate .personality directive"));
3921
3922   c = get_symbol_name (& name);
3923   p = input_line_pointer;
3924   if (c == '"')
3925     ++ input_line_pointer;
3926   unwind.personality_routine = symbol_find_or_make (name);
3927   *p = c;
3928   demand_empty_rest_of_line ();
3929 }
3930
3931
3932 /* Parse a directive saving core registers.  */
3933
3934 static void
3935 s_arm_unwind_save_core (void)
3936 {
3937   valueT op;
3938   long range;
3939   int n;
3940
3941   range = parse_reg_list (&input_line_pointer);
3942   if (range == FAIL)
3943     {
3944       as_bad (_("expected register list"));
3945       ignore_rest_of_line ();
3946       return;
3947     }
3948
3949   demand_empty_rest_of_line ();
3950
3951   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3952      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3953      ip because it is clobbered by calls.  */
3954   if (unwind.sp_restored && unwind.fp_reg == 12
3955       && (range & 0x3000) == 0x1000)
3956     {
3957       unwind.opcode_count--;
3958       unwind.sp_restored = 0;
3959       range = (range | 0x2000) & ~0x1000;
3960       unwind.pending_offset = 0;
3961     }
3962
3963   /* Pop r4-r15.  */
3964   if (range & 0xfff0)
3965     {
3966       /* See if we can use the short opcodes.  These pop a block of up to 8
3967          registers starting with r4, plus maybe r14.  */
3968       for (n = 0; n < 8; n++)
3969         {
3970           /* Break at the first non-saved register.      */
3971           if ((range & (1 << (n + 4))) == 0)
3972             break;
3973         }
3974       /* See if there are any other bits set.  */
3975       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3976         {
3977           /* Use the long form.  */
3978           op = 0x8000 | ((range >> 4) & 0xfff);
3979           add_unwind_opcode (op, 2);
3980         }
3981       else
3982         {
3983           /* Use the short form.  */
3984           if (range & 0x4000)
3985             op = 0xa8; /* Pop r14.      */
3986           else
3987             op = 0xa0; /* Do not pop r14.  */
3988           op |= (n - 1);
3989           add_unwind_opcode (op, 1);
3990         }
3991     }
3992
3993   /* Pop r0-r3.  */
3994   if (range & 0xf)
3995     {
3996       op = 0xb100 | (range & 0xf);
3997       add_unwind_opcode (op, 2);
3998     }
3999
4000   /* Record the number of bytes pushed.  */
4001   for (n = 0; n < 16; n++)
4002     {
4003       if (range & (1 << n))
4004         unwind.frame_size += 4;
4005     }
4006 }
4007
4008
4009 /* Parse a directive saving FPA registers.  */
4010
4011 static void
4012 s_arm_unwind_save_fpa (int reg)
4013 {
4014   expressionS exp;
4015   int num_regs;
4016   valueT op;
4017
4018   /* Get Number of registers to transfer.  */
4019   if (skip_past_comma (&input_line_pointer) != FAIL)
4020     expression (&exp);
4021   else
4022     exp.X_op = O_illegal;
4023
4024   if (exp.X_op != O_constant)
4025     {
4026       as_bad (_("expected , <constant>"));
4027       ignore_rest_of_line ();
4028       return;
4029     }
4030
4031   num_regs = exp.X_add_number;
4032
4033   if (num_regs < 1 || num_regs > 4)
4034     {
4035       as_bad (_("number of registers must be in the range [1:4]"));
4036       ignore_rest_of_line ();
4037       return;
4038     }
4039
4040   demand_empty_rest_of_line ();
4041
4042   if (reg == 4)
4043     {
4044       /* Short form.  */
4045       op = 0xb4 | (num_regs - 1);
4046       add_unwind_opcode (op, 1);
4047     }
4048   else
4049     {
4050       /* Long form.  */
4051       op = 0xc800 | (reg << 4) | (num_regs - 1);
4052       add_unwind_opcode (op, 2);
4053     }
4054   unwind.frame_size += num_regs * 12;
4055 }
4056
4057
4058 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4059
4060 static void
4061 s_arm_unwind_save_vfp_armv6 (void)
4062 {
4063   int count;
4064   unsigned int start;
4065   valueT op;
4066   int num_vfpv3_regs = 0;
4067   int num_regs_below_16;
4068
4069   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
4070   if (count == FAIL)
4071     {
4072       as_bad (_("expected register list"));
4073       ignore_rest_of_line ();
4074       return;
4075     }
4076
4077   demand_empty_rest_of_line ();
4078
4079   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4080      than FSTMX/FLDMX-style ones).  */
4081
4082   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4083   if (start >= 16)
4084     num_vfpv3_regs = count;
4085   else if (start + count > 16)
4086     num_vfpv3_regs = start + count - 16;
4087
4088   if (num_vfpv3_regs > 0)
4089     {
4090       int start_offset = start > 16 ? start - 16 : 0;
4091       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4092       add_unwind_opcode (op, 2);
4093     }
4094
4095   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4096   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4097   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4098   if (num_regs_below_16 > 0)
4099     {
4100       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4101       add_unwind_opcode (op, 2);
4102     }
4103
4104   unwind.frame_size += count * 8;
4105 }
4106
4107
4108 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4109
4110 static void
4111 s_arm_unwind_save_vfp (void)
4112 {
4113   int count;
4114   unsigned int reg;
4115   valueT op;
4116
4117   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4118   if (count == FAIL)
4119     {
4120       as_bad (_("expected register list"));
4121       ignore_rest_of_line ();
4122       return;
4123     }
4124
4125   demand_empty_rest_of_line ();
4126
4127   if (reg == 8)
4128     {
4129       /* Short form.  */
4130       op = 0xb8 | (count - 1);
4131       add_unwind_opcode (op, 1);
4132     }
4133   else
4134     {
4135       /* Long form.  */
4136       op = 0xb300 | (reg << 4) | (count - 1);
4137       add_unwind_opcode (op, 2);
4138     }
4139   unwind.frame_size += count * 8 + 4;
4140 }
4141
4142
4143 /* Parse a directive saving iWMMXt data registers.  */
4144
4145 static void
4146 s_arm_unwind_save_mmxwr (void)
4147 {
4148   int reg;
4149   int hi_reg;
4150   int i;
4151   unsigned mask = 0;
4152   valueT op;
4153
4154   if (*input_line_pointer == '{')
4155     input_line_pointer++;
4156
4157   do
4158     {
4159       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4160
4161       if (reg == FAIL)
4162         {
4163           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4164           goto error;
4165         }
4166
4167       if (mask >> reg)
4168         as_tsktsk (_("register list not in ascending order"));
4169       mask |= 1 << reg;
4170
4171       if (*input_line_pointer == '-')
4172         {
4173           input_line_pointer++;
4174           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4175           if (hi_reg == FAIL)
4176             {
4177               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4178               goto error;
4179             }
4180           else if (reg >= hi_reg)
4181             {
4182               as_bad (_("bad register range"));
4183               goto error;
4184             }
4185           for (; reg < hi_reg; reg++)
4186             mask |= 1 << reg;
4187         }
4188     }
4189   while (skip_past_comma (&input_line_pointer) != FAIL);
4190
4191   skip_past_char (&input_line_pointer, '}');
4192
4193   demand_empty_rest_of_line ();
4194
4195   /* Generate any deferred opcodes because we're going to be looking at
4196      the list.  */
4197   flush_pending_unwind ();
4198
4199   for (i = 0; i < 16; i++)
4200     {
4201       if (mask & (1 << i))
4202         unwind.frame_size += 8;
4203     }
4204
4205   /* Attempt to combine with a previous opcode.  We do this because gcc
4206      likes to output separate unwind directives for a single block of
4207      registers.  */
4208   if (unwind.opcode_count > 0)
4209     {
4210       i = unwind.opcodes[unwind.opcode_count - 1];
4211       if ((i & 0xf8) == 0xc0)
4212         {
4213           i &= 7;
4214           /* Only merge if the blocks are contiguous.  */
4215           if (i < 6)
4216             {
4217               if ((mask & 0xfe00) == (1 << 9))
4218                 {
4219                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4220                   unwind.opcode_count--;
4221                 }
4222             }
4223           else if (i == 6 && unwind.opcode_count >= 2)
4224             {
4225               i = unwind.opcodes[unwind.opcode_count - 2];
4226               reg = i >> 4;
4227               i &= 0xf;
4228
4229               op = 0xffff << (reg - 1);
4230               if (reg > 0
4231                   && ((mask & op) == (1u << (reg - 1))))
4232                 {
4233                   op = (1 << (reg + i + 1)) - 1;
4234                   op &= ~((1 << reg) - 1);
4235                   mask |= op;
4236                   unwind.opcode_count -= 2;
4237                 }
4238             }
4239         }
4240     }
4241
4242   hi_reg = 15;
4243   /* We want to generate opcodes in the order the registers have been
4244      saved, ie. descending order.  */
4245   for (reg = 15; reg >= -1; reg--)
4246     {
4247       /* Save registers in blocks.  */
4248       if (reg < 0
4249           || !(mask & (1 << reg)))
4250         {
4251           /* We found an unsaved reg.  Generate opcodes to save the
4252              preceding block.   */
4253           if (reg != hi_reg)
4254             {
4255               if (reg == 9)
4256                 {
4257                   /* Short form.  */
4258                   op = 0xc0 | (hi_reg - 10);
4259                   add_unwind_opcode (op, 1);
4260                 }
4261               else
4262                 {
4263                   /* Long form.  */
4264                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4265                   add_unwind_opcode (op, 2);
4266                 }
4267             }
4268           hi_reg = reg - 1;
4269         }
4270     }
4271
4272   return;
4273 error:
4274   ignore_rest_of_line ();
4275 }
4276
4277 static void
4278 s_arm_unwind_save_mmxwcg (void)
4279 {
4280   int reg;
4281   int hi_reg;
4282   unsigned mask = 0;
4283   valueT op;
4284
4285   if (*input_line_pointer == '{')
4286     input_line_pointer++;
4287
4288   skip_whitespace (input_line_pointer);
4289
4290   do
4291     {
4292       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4293
4294       if (reg == FAIL)
4295         {
4296           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4297           goto error;
4298         }
4299
4300       reg -= 8;
4301       if (mask >> reg)
4302         as_tsktsk (_("register list not in ascending order"));
4303       mask |= 1 << reg;
4304
4305       if (*input_line_pointer == '-')
4306         {
4307           input_line_pointer++;
4308           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4309           if (hi_reg == FAIL)
4310             {
4311               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4312               goto error;
4313             }
4314           else if (reg >= hi_reg)
4315             {
4316               as_bad (_("bad register range"));
4317               goto error;
4318             }
4319           for (; reg < hi_reg; reg++)
4320             mask |= 1 << reg;
4321         }
4322     }
4323   while (skip_past_comma (&input_line_pointer) != FAIL);
4324
4325   skip_past_char (&input_line_pointer, '}');
4326
4327   demand_empty_rest_of_line ();
4328
4329   /* Generate any deferred opcodes because we're going to be looking at
4330      the list.  */
4331   flush_pending_unwind ();
4332
4333   for (reg = 0; reg < 16; reg++)
4334     {
4335       if (mask & (1 << reg))
4336         unwind.frame_size += 4;
4337     }
4338   op = 0xc700 | mask;
4339   add_unwind_opcode (op, 2);
4340   return;
4341 error:
4342   ignore_rest_of_line ();
4343 }
4344
4345
4346 /* Parse an unwind_save directive.
4347    If the argument is non-zero, this is a .vsave directive.  */
4348
4349 static void
4350 s_arm_unwind_save (int arch_v6)
4351 {
4352   char *peek;
4353   struct reg_entry *reg;
4354   bfd_boolean had_brace = FALSE;
4355
4356   if (!unwind.proc_start)
4357     as_bad (MISSING_FNSTART);
4358
4359   /* Figure out what sort of save we have.  */
4360   peek = input_line_pointer;
4361
4362   if (*peek == '{')
4363     {
4364       had_brace = TRUE;
4365       peek++;
4366     }
4367
4368   reg = arm_reg_parse_multi (&peek);
4369
4370   if (!reg)
4371     {
4372       as_bad (_("register expected"));
4373       ignore_rest_of_line ();
4374       return;
4375     }
4376
4377   switch (reg->type)
4378     {
4379     case REG_TYPE_FN:
4380       if (had_brace)
4381         {
4382           as_bad (_("FPA .unwind_save does not take a register list"));
4383           ignore_rest_of_line ();
4384           return;
4385         }
4386       input_line_pointer = peek;
4387       s_arm_unwind_save_fpa (reg->number);
4388       return;
4389
4390     case REG_TYPE_RN:
4391       s_arm_unwind_save_core ();
4392       return;
4393
4394     case REG_TYPE_VFD:
4395       if (arch_v6)
4396         s_arm_unwind_save_vfp_armv6 ();
4397       else
4398         s_arm_unwind_save_vfp ();
4399       return;
4400
4401     case REG_TYPE_MMXWR:
4402       s_arm_unwind_save_mmxwr ();
4403       return;
4404
4405     case REG_TYPE_MMXWCG:
4406       s_arm_unwind_save_mmxwcg ();
4407       return;
4408
4409     default:
4410       as_bad (_(".unwind_save does not support this kind of register"));
4411       ignore_rest_of_line ();
4412     }
4413 }
4414
4415
4416 /* Parse an unwind_movsp directive.  */
4417
4418 static void
4419 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4420 {
4421   int reg;
4422   valueT op;
4423   int offset;
4424
4425   if (!unwind.proc_start)
4426     as_bad (MISSING_FNSTART);
4427
4428   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4429   if (reg == FAIL)
4430     {
4431       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4432       ignore_rest_of_line ();
4433       return;
4434     }
4435
4436   /* Optional constant.  */
4437   if (skip_past_comma (&input_line_pointer) != FAIL)
4438     {
4439       if (immediate_for_directive (&offset) == FAIL)
4440         return;
4441     }
4442   else
4443     offset = 0;
4444
4445   demand_empty_rest_of_line ();
4446
4447   if (reg == REG_SP || reg == REG_PC)
4448     {
4449       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4450       return;
4451     }
4452
4453   if (unwind.fp_reg != REG_SP)
4454     as_bad (_("unexpected .unwind_movsp directive"));
4455
4456   /* Generate opcode to restore the value.  */
4457   op = 0x90 | reg;
4458   add_unwind_opcode (op, 1);
4459
4460   /* Record the information for later.  */
4461   unwind.fp_reg = reg;
4462   unwind.fp_offset = unwind.frame_size - offset;
4463   unwind.sp_restored = 1;
4464 }
4465
4466 /* Parse an unwind_pad directive.  */
4467
4468 static void
4469 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4470 {
4471   int offset;
4472
4473   if (!unwind.proc_start)
4474     as_bad (MISSING_FNSTART);
4475
4476   if (immediate_for_directive (&offset) == FAIL)
4477     return;
4478
4479   if (offset & 3)
4480     {
4481       as_bad (_("stack increment must be multiple of 4"));
4482       ignore_rest_of_line ();
4483       return;
4484     }
4485
4486   /* Don't generate any opcodes, just record the details for later.  */
4487   unwind.frame_size += offset;
4488   unwind.pending_offset += offset;
4489
4490   demand_empty_rest_of_line ();
4491 }
4492
4493 /* Parse an unwind_setfp directive.  */
4494
4495 static void
4496 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4497 {
4498   int sp_reg;
4499   int fp_reg;
4500   int offset;
4501
4502   if (!unwind.proc_start)
4503     as_bad (MISSING_FNSTART);
4504
4505   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4506   if (skip_past_comma (&input_line_pointer) == FAIL)
4507     sp_reg = FAIL;
4508   else
4509     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4510
4511   if (fp_reg == FAIL || sp_reg == FAIL)
4512     {
4513       as_bad (_("expected <reg>, <reg>"));
4514       ignore_rest_of_line ();
4515       return;
4516     }
4517
4518   /* Optional constant.  */
4519   if (skip_past_comma (&input_line_pointer) != FAIL)
4520     {
4521       if (immediate_for_directive (&offset) == FAIL)
4522         return;
4523     }
4524   else
4525     offset = 0;
4526
4527   demand_empty_rest_of_line ();
4528
4529   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4530     {
4531       as_bad (_("register must be either sp or set by a previous"
4532                 "unwind_movsp directive"));
4533       return;
4534     }
4535
4536   /* Don't generate any opcodes, just record the information for later.  */
4537   unwind.fp_reg = fp_reg;
4538   unwind.fp_used = 1;
4539   if (sp_reg == REG_SP)
4540     unwind.fp_offset = unwind.frame_size - offset;
4541   else
4542     unwind.fp_offset -= offset;
4543 }
4544
4545 /* Parse an unwind_raw directive.  */
4546
4547 static void
4548 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4549 {
4550   expressionS exp;
4551   /* This is an arbitrary limit.         */
4552   unsigned char op[16];
4553   int count;
4554
4555   if (!unwind.proc_start)
4556     as_bad (MISSING_FNSTART);
4557
4558   expression (&exp);
4559   if (exp.X_op == O_constant
4560       && skip_past_comma (&input_line_pointer) != FAIL)
4561     {
4562       unwind.frame_size += exp.X_add_number;
4563       expression (&exp);
4564     }
4565   else
4566     exp.X_op = O_illegal;
4567
4568   if (exp.X_op != O_constant)
4569     {
4570       as_bad (_("expected <offset>, <opcode>"));
4571       ignore_rest_of_line ();
4572       return;
4573     }
4574
4575   count = 0;
4576
4577   /* Parse the opcode.  */
4578   for (;;)
4579     {
4580       if (count >= 16)
4581         {
4582           as_bad (_("unwind opcode too long"));
4583           ignore_rest_of_line ();
4584         }
4585       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4586         {
4587           as_bad (_("invalid unwind opcode"));
4588           ignore_rest_of_line ();
4589           return;
4590         }
4591       op[count++] = exp.X_add_number;
4592
4593       /* Parse the next byte.  */
4594       if (skip_past_comma (&input_line_pointer) == FAIL)
4595         break;
4596
4597       expression (&exp);
4598     }
4599
4600   /* Add the opcode bytes in reverse order.  */
4601   while (count--)
4602     add_unwind_opcode (op[count], 1);
4603
4604   demand_empty_rest_of_line ();
4605 }
4606
4607
4608 /* Parse a .eabi_attribute directive.  */
4609
4610 static void
4611 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4612 {
4613   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4614
4615   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4616     attributes_set_explicitly[tag] = 1;
4617 }
4618
4619 /* Emit a tls fix for the symbol.  */
4620
4621 static void
4622 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4623 {
4624   char *p;
4625   expressionS exp;
4626 #ifdef md_flush_pending_output
4627   md_flush_pending_output ();
4628 #endif
4629
4630 #ifdef md_cons_align
4631   md_cons_align (4);
4632 #endif
4633
4634   /* Since we're just labelling the code, there's no need to define a
4635      mapping symbol.  */
4636   expression (&exp);
4637   p = obstack_next_free (&frchain_now->frch_obstack);
4638   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4639                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4640                : BFD_RELOC_ARM_TLS_DESCSEQ);
4641 }
4642 #endif /* OBJ_ELF */
4643
4644 static void s_arm_arch (int);
4645 static void s_arm_object_arch (int);
4646 static void s_arm_cpu (int);
4647 static void s_arm_fpu (int);
4648 static void s_arm_arch_extension (int);
4649
4650 #ifdef TE_PE
4651
4652 static void
4653 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4654 {
4655   expressionS exp;
4656
4657   do
4658     {
4659       expression (&exp);
4660       if (exp.X_op == O_symbol)
4661         exp.X_op = O_secrel;
4662
4663       emit_expr (&exp, 4);
4664     }
4665   while (*input_line_pointer++ == ',');
4666
4667   input_line_pointer--;
4668   demand_empty_rest_of_line ();
4669 }
4670 #endif /* TE_PE */
4671
4672 /* This table describes all the machine specific pseudo-ops the assembler
4673    has to support.  The fields are:
4674      pseudo-op name without dot
4675      function to call to execute this pseudo-op
4676      Integer arg to pass to the function.  */
4677
4678 const pseudo_typeS md_pseudo_table[] =
4679 {
4680   /* Never called because '.req' does not start a line.  */
4681   { "req",         s_req,         0 },
4682   /* Following two are likewise never called.  */
4683   { "dn",          s_dn,          0 },
4684   { "qn",          s_qn,          0 },
4685   { "unreq",       s_unreq,       0 },
4686   { "bss",         s_bss,         0 },
4687   { "align",       s_align_ptwo,  2 },
4688   { "arm",         s_arm,         0 },
4689   { "thumb",       s_thumb,       0 },
4690   { "code",        s_code,        0 },
4691   { "force_thumb", s_force_thumb, 0 },
4692   { "thumb_func",  s_thumb_func,  0 },
4693   { "thumb_set",   s_thumb_set,   0 },
4694   { "even",        s_even,        0 },
4695   { "ltorg",       s_ltorg,       0 },
4696   { "pool",        s_ltorg,       0 },
4697   { "syntax",      s_syntax,      0 },
4698   { "cpu",         s_arm_cpu,     0 },
4699   { "arch",        s_arm_arch,    0 },
4700   { "object_arch", s_arm_object_arch,   0 },
4701   { "fpu",         s_arm_fpu,     0 },
4702   { "arch_extension", s_arm_arch_extension, 0 },
4703 #ifdef OBJ_ELF
4704   { "word",             s_arm_elf_cons, 4 },
4705   { "long",             s_arm_elf_cons, 4 },
4706   { "inst.n",           s_arm_elf_inst, 2 },
4707   { "inst.w",           s_arm_elf_inst, 4 },
4708   { "inst",             s_arm_elf_inst, 0 },
4709   { "rel31",            s_arm_rel31,      0 },
4710   { "fnstart",          s_arm_unwind_fnstart,   0 },
4711   { "fnend",            s_arm_unwind_fnend,     0 },
4712   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4713   { "personality",      s_arm_unwind_personality, 0 },
4714   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4715   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4716   { "save",             s_arm_unwind_save,      0 },
4717   { "vsave",            s_arm_unwind_save,      1 },
4718   { "movsp",            s_arm_unwind_movsp,     0 },
4719   { "pad",              s_arm_unwind_pad,       0 },
4720   { "setfp",            s_arm_unwind_setfp,     0 },
4721   { "unwind_raw",       s_arm_unwind_raw,       0 },
4722   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4723   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4724 #else
4725   { "word",        cons, 4},
4726
4727   /* These are used for dwarf.  */
4728   {"2byte", cons, 2},
4729   {"4byte", cons, 4},
4730   {"8byte", cons, 8},
4731   /* These are used for dwarf2.  */
4732   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4733   { "loc",  dwarf2_directive_loc,  0 },
4734   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4735 #endif
4736   { "extend",      float_cons, 'x' },
4737   { "ldouble",     float_cons, 'x' },
4738   { "packed",      float_cons, 'p' },
4739 #ifdef TE_PE
4740   {"secrel32", pe_directive_secrel, 0},
4741 #endif
4742
4743   /* These are for compatibility with CodeComposer Studio.  */
4744   {"ref",          s_ccs_ref,        0},
4745   {"def",          s_ccs_def,        0},
4746   {"asmfunc",      s_ccs_asmfunc,    0},
4747   {"endasmfunc",   s_ccs_endasmfunc, 0},
4748
4749   { 0, 0, 0 }
4750 };
4751 \f
4752 /* Parser functions used exclusively in instruction operands.  */
4753
4754 /* Generic immediate-value read function for use in insn parsing.
4755    STR points to the beginning of the immediate (the leading #);
4756    VAL receives the value; if the value is outside [MIN, MAX]
4757    issue an error.  PREFIX_OPT is true if the immediate prefix is
4758    optional.  */
4759
4760 static int
4761 parse_immediate (char **str, int *val, int min, int max,
4762                  bfd_boolean prefix_opt)
4763 {
4764   expressionS exp;
4765   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4766   if (exp.X_op != O_constant)
4767     {
4768       inst.error = _("constant expression required");
4769       return FAIL;
4770     }
4771
4772   if (exp.X_add_number < min || exp.X_add_number > max)
4773     {
4774       inst.error = _("immediate value out of range");
4775       return FAIL;
4776     }
4777
4778   *val = exp.X_add_number;
4779   return SUCCESS;
4780 }
4781
4782 /* Less-generic immediate-value read function with the possibility of loading a
4783    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4784    instructions. Puts the result directly in inst.operands[i].  */
4785
4786 static int
4787 parse_big_immediate (char **str, int i, expressionS *in_exp,
4788                      bfd_boolean allow_symbol_p)
4789 {
4790   expressionS exp;
4791   expressionS *exp_p = in_exp ? in_exp : &exp;
4792   char *ptr = *str;
4793
4794   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4795
4796   if (exp_p->X_op == O_constant)
4797     {
4798       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4799       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4800          O_constant.  We have to be careful not to break compilation for
4801          32-bit X_add_number, though.  */
4802       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4803         {
4804           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4805           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4806                                   & 0xffffffff);
4807           inst.operands[i].regisimm = 1;
4808         }
4809     }
4810   else if (exp_p->X_op == O_big
4811            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4812     {
4813       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4814
4815       /* Bignums have their least significant bits in
4816          generic_bignum[0]. Make sure we put 32 bits in imm and
4817          32 bits in reg,  in a (hopefully) portable way.  */
4818       gas_assert (parts != 0);
4819
4820       /* Make sure that the number is not too big.
4821          PR 11972: Bignums can now be sign-extended to the
4822          size of a .octa so check that the out of range bits
4823          are all zero or all one.  */
4824       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4825         {
4826           LITTLENUM_TYPE m = -1;
4827
4828           if (generic_bignum[parts * 2] != 0
4829               && generic_bignum[parts * 2] != m)
4830             return FAIL;
4831
4832           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4833             if (generic_bignum[j] != generic_bignum[j-1])
4834               return FAIL;
4835         }
4836
4837       inst.operands[i].imm = 0;
4838       for (j = 0; j < parts; j++, idx++)
4839         inst.operands[i].imm |= generic_bignum[idx]
4840                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4841       inst.operands[i].reg = 0;
4842       for (j = 0; j < parts; j++, idx++)
4843         inst.operands[i].reg |= generic_bignum[idx]
4844                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4845       inst.operands[i].regisimm = 1;
4846     }
4847   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4848     return FAIL;
4849
4850   *str = ptr;
4851
4852   return SUCCESS;
4853 }
4854
4855 /* Returns the pseudo-register number of an FPA immediate constant,
4856    or FAIL if there isn't a valid constant here.  */
4857
4858 static int
4859 parse_fpa_immediate (char ** str)
4860 {
4861   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4862   char *         save_in;
4863   expressionS    exp;
4864   int            i;
4865   int            j;
4866
4867   /* First try and match exact strings, this is to guarantee
4868      that some formats will work even for cross assembly.  */
4869
4870   for (i = 0; fp_const[i]; i++)
4871     {
4872       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4873         {
4874           char *start = *str;
4875
4876           *str += strlen (fp_const[i]);
4877           if (is_end_of_line[(unsigned char) **str])
4878             return i + 8;
4879           *str = start;
4880         }
4881     }
4882
4883   /* Just because we didn't get a match doesn't mean that the constant
4884      isn't valid, just that it is in a format that we don't
4885      automatically recognize.  Try parsing it with the standard
4886      expression routines.  */
4887
4888   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4889
4890   /* Look for a raw floating point number.  */
4891   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4892       && is_end_of_line[(unsigned char) *save_in])
4893     {
4894       for (i = 0; i < NUM_FLOAT_VALS; i++)
4895         {
4896           for (j = 0; j < MAX_LITTLENUMS; j++)
4897             {
4898               if (words[j] != fp_values[i][j])
4899                 break;
4900             }
4901
4902           if (j == MAX_LITTLENUMS)
4903             {
4904               *str = save_in;
4905               return i + 8;
4906             }
4907         }
4908     }
4909
4910   /* Try and parse a more complex expression, this will probably fail
4911      unless the code uses a floating point prefix (eg "0f").  */
4912   save_in = input_line_pointer;
4913   input_line_pointer = *str;
4914   if (expression (&exp) == absolute_section
4915       && exp.X_op == O_big
4916       && exp.X_add_number < 0)
4917     {
4918       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4919          Ditto for 15.  */
4920 #define X_PRECISION 5
4921 #define E_PRECISION 15L
4922       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
4923         {
4924           for (i = 0; i < NUM_FLOAT_VALS; i++)
4925             {
4926               for (j = 0; j < MAX_LITTLENUMS; j++)
4927                 {
4928                   if (words[j] != fp_values[i][j])
4929                     break;
4930                 }
4931
4932               if (j == MAX_LITTLENUMS)
4933                 {
4934                   *str = input_line_pointer;
4935                   input_line_pointer = save_in;
4936                   return i + 8;
4937                 }
4938             }
4939         }
4940     }
4941
4942   *str = input_line_pointer;
4943   input_line_pointer = save_in;
4944   inst.error = _("invalid FPA immediate expression");
4945   return FAIL;
4946 }
4947
4948 /* Returns 1 if a number has "quarter-precision" float format
4949    0baBbbbbbc defgh000 00000000 00000000.  */
4950
4951 static int
4952 is_quarter_float (unsigned imm)
4953 {
4954   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4955   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4956 }
4957
4958
4959 /* Detect the presence of a floating point or integer zero constant,
4960    i.e. #0.0 or #0.  */
4961
4962 static bfd_boolean
4963 parse_ifimm_zero (char **in)
4964 {
4965   int error_code;
4966
4967   if (!is_immediate_prefix (**in))
4968     return FALSE;
4969
4970   ++*in;
4971
4972   /* Accept #0x0 as a synonym for #0.  */
4973   if (strncmp (*in, "0x", 2) == 0)
4974     {
4975       int val;
4976       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
4977         return FALSE;
4978       return TRUE;
4979     }
4980
4981   error_code = atof_generic (in, ".", EXP_CHARS,
4982                              &generic_floating_point_number);
4983
4984   if (!error_code
4985       && generic_floating_point_number.sign == '+'
4986       && (generic_floating_point_number.low
4987           > generic_floating_point_number.leader))
4988     return TRUE;
4989
4990   return FALSE;
4991 }
4992
4993 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4994    0baBbbbbbc defgh000 00000000 00000000.
4995    The zero and minus-zero cases need special handling, since they can't be
4996    encoded in the "quarter-precision" float format, but can nonetheless be
4997    loaded as integer constants.  */
4998
4999 static unsigned
5000 parse_qfloat_immediate (char **ccp, int *immed)
5001 {
5002   char *str = *ccp;
5003   char *fpnum;
5004   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5005   int found_fpchar = 0;
5006
5007   skip_past_char (&str, '#');
5008
5009   /* We must not accidentally parse an integer as a floating-point number. Make
5010      sure that the value we parse is not an integer by checking for special
5011      characters '.' or 'e'.
5012      FIXME: This is a horrible hack, but doing better is tricky because type
5013      information isn't in a very usable state at parse time.  */
5014   fpnum = str;
5015   skip_whitespace (fpnum);
5016
5017   if (strncmp (fpnum, "0x", 2) == 0)
5018     return FAIL;
5019   else
5020     {
5021       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5022         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5023           {
5024             found_fpchar = 1;
5025             break;
5026           }
5027
5028       if (!found_fpchar)
5029         return FAIL;
5030     }
5031
5032   if ((str = atof_ieee (str, 's', words)) != NULL)
5033     {
5034       unsigned fpword = 0;
5035       int i;
5036
5037       /* Our FP word must be 32 bits (single-precision FP).  */
5038       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5039         {
5040           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5041           fpword |= words[i];
5042         }
5043
5044       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5045         *immed = fpword;
5046       else
5047         return FAIL;
5048
5049       *ccp = str;
5050
5051       return SUCCESS;
5052     }
5053
5054   return FAIL;
5055 }
5056
5057 /* Shift operands.  */
5058 enum shift_kind
5059 {
5060   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5061 };
5062
5063 struct asm_shift_name
5064 {
5065   const char      *name;
5066   enum shift_kind  kind;
5067 };
5068
5069 /* Third argument to parse_shift.  */
5070 enum parse_shift_mode
5071 {
5072   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5073   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5074   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5075   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5076   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5077 };
5078
5079 /* Parse a <shift> specifier on an ARM data processing instruction.
5080    This has three forms:
5081
5082      (LSL|LSR|ASL|ASR|ROR) Rs
5083      (LSL|LSR|ASL|ASR|ROR) #imm
5084      RRX
5085
5086    Note that ASL is assimilated to LSL in the instruction encoding, and
5087    RRX to ROR #0 (which cannot be written as such).  */
5088
5089 static int
5090 parse_shift (char **str, int i, enum parse_shift_mode mode)
5091 {
5092   const struct asm_shift_name *shift_name;
5093   enum shift_kind shift;
5094   char *s = *str;
5095   char *p = s;
5096   int reg;
5097
5098   for (p = *str; ISALPHA (*p); p++)
5099     ;
5100
5101   if (p == *str)
5102     {
5103       inst.error = _("shift expression expected");
5104       return FAIL;
5105     }
5106
5107   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5108                                                             p - *str);
5109
5110   if (shift_name == NULL)
5111     {
5112       inst.error = _("shift expression expected");
5113       return FAIL;
5114     }
5115
5116   shift = shift_name->kind;
5117
5118   switch (mode)
5119     {
5120     case NO_SHIFT_RESTRICT:
5121     case SHIFT_IMMEDIATE:   break;
5122
5123     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5124       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5125         {
5126           inst.error = _("'LSL' or 'ASR' required");
5127           return FAIL;
5128         }
5129       break;
5130
5131     case SHIFT_LSL_IMMEDIATE:
5132       if (shift != SHIFT_LSL)
5133         {
5134           inst.error = _("'LSL' required");
5135           return FAIL;
5136         }
5137       break;
5138
5139     case SHIFT_ASR_IMMEDIATE:
5140       if (shift != SHIFT_ASR)
5141         {
5142           inst.error = _("'ASR' required");
5143           return FAIL;
5144         }
5145       break;
5146
5147     default: abort ();
5148     }
5149
5150   if (shift != SHIFT_RRX)
5151     {
5152       /* Whitespace can appear here if the next thing is a bare digit.  */
5153       skip_whitespace (p);
5154
5155       if (mode == NO_SHIFT_RESTRICT
5156           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5157         {
5158           inst.operands[i].imm = reg;
5159           inst.operands[i].immisreg = 1;
5160         }
5161       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5162         return FAIL;
5163     }
5164   inst.operands[i].shift_kind = shift;
5165   inst.operands[i].shifted = 1;
5166   *str = p;
5167   return SUCCESS;
5168 }
5169
5170 /* Parse a <shifter_operand> for an ARM data processing instruction:
5171
5172       #<immediate>
5173       #<immediate>, <rotate>
5174       <Rm>
5175       <Rm>, <shift>
5176
5177    where <shift> is defined by parse_shift above, and <rotate> is a
5178    multiple of 2 between 0 and 30.  Validation of immediate operands
5179    is deferred to md_apply_fix.  */
5180
5181 static int
5182 parse_shifter_operand (char **str, int i)
5183 {
5184   int value;
5185   expressionS exp;
5186
5187   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5188     {
5189       inst.operands[i].reg = value;
5190       inst.operands[i].isreg = 1;
5191
5192       /* parse_shift will override this if appropriate */
5193       inst.reloc.exp.X_op = O_constant;
5194       inst.reloc.exp.X_add_number = 0;
5195
5196       if (skip_past_comma (str) == FAIL)
5197         return SUCCESS;
5198
5199       /* Shift operation on register.  */
5200       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5201     }
5202
5203   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5204     return FAIL;
5205
5206   if (skip_past_comma (str) == SUCCESS)
5207     {
5208       /* #x, y -- ie explicit rotation by Y.  */
5209       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5210         return FAIL;
5211
5212       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5213         {
5214           inst.error = _("constant expression expected");
5215           return FAIL;
5216         }
5217
5218       value = exp.X_add_number;
5219       if (value < 0 || value > 30 || value % 2 != 0)
5220         {
5221           inst.error = _("invalid rotation");
5222           return FAIL;
5223         }
5224       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5225         {
5226           inst.error = _("invalid constant");
5227           return FAIL;
5228         }
5229
5230       /* Encode as specified.  */
5231       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5232       return SUCCESS;
5233     }
5234
5235   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5236   inst.reloc.pc_rel = 0;
5237   return SUCCESS;
5238 }
5239
5240 /* Group relocation information.  Each entry in the table contains the
5241    textual name of the relocation as may appear in assembler source
5242    and must end with a colon.
5243    Along with this textual name are the relocation codes to be used if
5244    the corresponding instruction is an ALU instruction (ADD or SUB only),
5245    an LDR, an LDRS, or an LDC.  */
5246
5247 struct group_reloc_table_entry
5248 {
5249   const char *name;
5250   int alu_code;
5251   int ldr_code;
5252   int ldrs_code;
5253   int ldc_code;
5254 };
5255
5256 typedef enum
5257 {
5258   /* Varieties of non-ALU group relocation.  */
5259
5260   GROUP_LDR,
5261   GROUP_LDRS,
5262   GROUP_LDC
5263 } group_reloc_type;
5264
5265 static struct group_reloc_table_entry group_reloc_table[] =
5266   { /* Program counter relative: */
5267     { "pc_g0_nc",
5268       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5269       0,                                /* LDR */
5270       0,                                /* LDRS */
5271       0 },                              /* LDC */
5272     { "pc_g0",
5273       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5274       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5275       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5276       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5277     { "pc_g1_nc",
5278       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5279       0,                                /* LDR */
5280       0,                                /* LDRS */
5281       0 },                              /* LDC */
5282     { "pc_g1",
5283       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5284       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5285       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5286       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5287     { "pc_g2",
5288       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5289       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5290       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5291       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5292     /* Section base relative */
5293     { "sb_g0_nc",
5294       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5295       0,                                /* LDR */
5296       0,                                /* LDRS */
5297       0 },                              /* LDC */
5298     { "sb_g0",
5299       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5300       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5301       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5302       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5303     { "sb_g1_nc",
5304       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5305       0,                                /* LDR */
5306       0,                                /* LDRS */
5307       0 },                              /* LDC */
5308     { "sb_g1",
5309       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5310       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5311       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5312       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5313     { "sb_g2",
5314       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5315       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5316       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5317       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5318     /* Absolute thumb alu relocations.  */
5319     { "lower0_7",
5320       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5321       0,                                /* LDR.  */
5322       0,                                /* LDRS.  */
5323       0 },                              /* LDC.  */
5324     { "lower8_15",
5325       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5326       0,                                /* LDR.  */
5327       0,                                /* LDRS.  */
5328       0 },                              /* LDC.  */
5329     { "upper0_7",
5330       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5331       0,                                /* LDR.  */
5332       0,                                /* LDRS.  */
5333       0 },                              /* LDC.  */
5334     { "upper8_15",
5335       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5336       0,                                /* LDR.  */
5337       0,                                /* LDRS.  */
5338       0 } };                            /* LDC.  */
5339
5340 /* Given the address of a pointer pointing to the textual name of a group
5341    relocation as may appear in assembler source, attempt to find its details
5342    in group_reloc_table.  The pointer will be updated to the character after
5343    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5344    otherwise.  On success, *entry will be updated to point at the relevant
5345    group_reloc_table entry. */
5346
5347 static int
5348 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5349 {
5350   unsigned int i;
5351   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5352     {
5353       int length = strlen (group_reloc_table[i].name);
5354
5355       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5356           && (*str)[length] == ':')
5357         {
5358           *out = &group_reloc_table[i];
5359           *str += (length + 1);
5360           return SUCCESS;
5361         }
5362     }
5363
5364   return FAIL;
5365 }
5366
5367 /* Parse a <shifter_operand> for an ARM data processing instruction
5368    (as for parse_shifter_operand) where group relocations are allowed:
5369
5370       #<immediate>
5371       #<immediate>, <rotate>
5372       #:<group_reloc>:<expression>
5373       <Rm>
5374       <Rm>, <shift>
5375
5376    where <group_reloc> is one of the strings defined in group_reloc_table.
5377    The hashes are optional.
5378
5379    Everything else is as for parse_shifter_operand.  */
5380
5381 static parse_operand_result
5382 parse_shifter_operand_group_reloc (char **str, int i)
5383 {
5384   /* Determine if we have the sequence of characters #: or just :
5385      coming next.  If we do, then we check for a group relocation.
5386      If we don't, punt the whole lot to parse_shifter_operand.  */
5387
5388   if (((*str)[0] == '#' && (*str)[1] == ':')
5389       || (*str)[0] == ':')
5390     {
5391       struct group_reloc_table_entry *entry;
5392
5393       if ((*str)[0] == '#')
5394         (*str) += 2;
5395       else
5396         (*str)++;
5397
5398       /* Try to parse a group relocation.  Anything else is an error.  */
5399       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5400         {
5401           inst.error = _("unknown group relocation");
5402           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5403         }
5404
5405       /* We now have the group relocation table entry corresponding to
5406          the name in the assembler source.  Next, we parse the expression.  */
5407       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5408         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5409
5410       /* Record the relocation type (always the ALU variant here).  */
5411       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5412       gas_assert (inst.reloc.type != 0);
5413
5414       return PARSE_OPERAND_SUCCESS;
5415     }
5416   else
5417     return parse_shifter_operand (str, i) == SUCCESS
5418            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5419
5420   /* Never reached.  */
5421 }
5422
5423 /* Parse a Neon alignment expression.  Information is written to
5424    inst.operands[i].  We assume the initial ':' has been skipped.
5425
5426    align        .imm = align << 8, .immisalign=1, .preind=0  */
5427 static parse_operand_result
5428 parse_neon_alignment (char **str, int i)
5429 {
5430   char *p = *str;
5431   expressionS exp;
5432
5433   my_get_expression (&exp, &p, GE_NO_PREFIX);
5434
5435   if (exp.X_op != O_constant)
5436     {
5437       inst.error = _("alignment must be constant");
5438       return PARSE_OPERAND_FAIL;
5439     }
5440
5441   inst.operands[i].imm = exp.X_add_number << 8;
5442   inst.operands[i].immisalign = 1;
5443   /* Alignments are not pre-indexes.  */
5444   inst.operands[i].preind = 0;
5445
5446   *str = p;
5447   return PARSE_OPERAND_SUCCESS;
5448 }
5449
5450 /* Parse all forms of an ARM address expression.  Information is written
5451    to inst.operands[i] and/or inst.reloc.
5452
5453    Preindexed addressing (.preind=1):
5454
5455    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5456    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5457    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5458                        .shift_kind=shift .reloc.exp=shift_imm
5459
5460    These three may have a trailing ! which causes .writeback to be set also.
5461
5462    Postindexed addressing (.postind=1, .writeback=1):
5463
5464    [Rn], #offset       .reg=Rn .reloc.exp=offset
5465    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5466    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5467                        .shift_kind=shift .reloc.exp=shift_imm
5468
5469    Unindexed addressing (.preind=0, .postind=0):
5470
5471    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5472
5473    Other:
5474
5475    [Rn]{!}             shorthand for [Rn,#0]{!}
5476    =immediate          .isreg=0 .reloc.exp=immediate
5477    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5478
5479   It is the caller's responsibility to check for addressing modes not
5480   supported by the instruction, and to set inst.reloc.type.  */
5481
5482 static parse_operand_result
5483 parse_address_main (char **str, int i, int group_relocations,
5484                     group_reloc_type group_type)
5485 {
5486   char *p = *str;
5487   int reg;
5488
5489   if (skip_past_char (&p, '[') == FAIL)
5490     {
5491       if (skip_past_char (&p, '=') == FAIL)
5492         {
5493           /* Bare address - translate to PC-relative offset.  */
5494           inst.reloc.pc_rel = 1;
5495           inst.operands[i].reg = REG_PC;
5496           inst.operands[i].isreg = 1;
5497           inst.operands[i].preind = 1;
5498
5499           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5500             return PARSE_OPERAND_FAIL;
5501         }
5502       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5503                                     /*allow_symbol_p=*/TRUE))
5504         return PARSE_OPERAND_FAIL;
5505
5506       *str = p;
5507       return PARSE_OPERAND_SUCCESS;
5508     }
5509
5510   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5511   skip_whitespace (p);
5512
5513   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5514     {
5515       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5516       return PARSE_OPERAND_FAIL;
5517     }
5518   inst.operands[i].reg = reg;
5519   inst.operands[i].isreg = 1;
5520
5521   if (skip_past_comma (&p) == SUCCESS)
5522     {
5523       inst.operands[i].preind = 1;
5524
5525       if (*p == '+') p++;
5526       else if (*p == '-') p++, inst.operands[i].negative = 1;
5527
5528       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5529         {
5530           inst.operands[i].imm = reg;
5531           inst.operands[i].immisreg = 1;
5532
5533           if (skip_past_comma (&p) == SUCCESS)
5534             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5535               return PARSE_OPERAND_FAIL;
5536         }
5537       else if (skip_past_char (&p, ':') == SUCCESS)
5538         {
5539           /* FIXME: '@' should be used here, but it's filtered out by generic
5540              code before we get to see it here. This may be subject to
5541              change.  */
5542           parse_operand_result result = parse_neon_alignment (&p, i);
5543
5544           if (result != PARSE_OPERAND_SUCCESS)
5545             return result;
5546         }
5547       else
5548         {
5549           if (inst.operands[i].negative)
5550             {
5551               inst.operands[i].negative = 0;
5552               p--;
5553             }
5554
5555           if (group_relocations
5556               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5557             {
5558               struct group_reloc_table_entry *entry;
5559
5560               /* Skip over the #: or : sequence.  */
5561               if (*p == '#')
5562                 p += 2;
5563               else
5564                 p++;
5565
5566               /* Try to parse a group relocation.  Anything else is an
5567                  error.  */
5568               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5569                 {
5570                   inst.error = _("unknown group relocation");
5571                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5572                 }
5573
5574               /* We now have the group relocation table entry corresponding to
5575                  the name in the assembler source.  Next, we parse the
5576                  expression.  */
5577               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5578                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5579
5580               /* Record the relocation type.  */
5581               switch (group_type)
5582                 {
5583                   case GROUP_LDR:
5584                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5585                     break;
5586
5587                   case GROUP_LDRS:
5588                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5589                     break;
5590
5591                   case GROUP_LDC:
5592                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5593                     break;
5594
5595                   default:
5596                     gas_assert (0);
5597                 }
5598
5599               if (inst.reloc.type == 0)
5600                 {
5601                   inst.error = _("this group relocation is not allowed on this instruction");
5602                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5603                 }
5604             }
5605           else
5606             {
5607               char *q = p;
5608               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5609                 return PARSE_OPERAND_FAIL;
5610               /* If the offset is 0, find out if it's a +0 or -0.  */
5611               if (inst.reloc.exp.X_op == O_constant
5612                   && inst.reloc.exp.X_add_number == 0)
5613                 {
5614                   skip_whitespace (q);
5615                   if (*q == '#')
5616                     {
5617                       q++;
5618                       skip_whitespace (q);
5619                     }
5620                   if (*q == '-')
5621                     inst.operands[i].negative = 1;
5622                 }
5623             }
5624         }
5625     }
5626   else if (skip_past_char (&p, ':') == SUCCESS)
5627     {
5628       /* FIXME: '@' should be used here, but it's filtered out by generic code
5629          before we get to see it here. This may be subject to change.  */
5630       parse_operand_result result = parse_neon_alignment (&p, i);
5631
5632       if (result != PARSE_OPERAND_SUCCESS)
5633         return result;
5634     }
5635
5636   if (skip_past_char (&p, ']') == FAIL)
5637     {
5638       inst.error = _("']' expected");
5639       return PARSE_OPERAND_FAIL;
5640     }
5641
5642   if (skip_past_char (&p, '!') == SUCCESS)
5643     inst.operands[i].writeback = 1;
5644
5645   else if (skip_past_comma (&p) == SUCCESS)
5646     {
5647       if (skip_past_char (&p, '{') == SUCCESS)
5648         {
5649           /* [Rn], {expr} - unindexed, with option */
5650           if (parse_immediate (&p, &inst.operands[i].imm,
5651                                0, 255, TRUE) == FAIL)
5652             return PARSE_OPERAND_FAIL;
5653
5654           if (skip_past_char (&p, '}') == FAIL)
5655             {
5656               inst.error = _("'}' expected at end of 'option' field");
5657               return PARSE_OPERAND_FAIL;
5658             }
5659           if (inst.operands[i].preind)
5660             {
5661               inst.error = _("cannot combine index with option");
5662               return PARSE_OPERAND_FAIL;
5663             }
5664           *str = p;
5665           return PARSE_OPERAND_SUCCESS;
5666         }
5667       else
5668         {
5669           inst.operands[i].postind = 1;
5670           inst.operands[i].writeback = 1;
5671
5672           if (inst.operands[i].preind)
5673             {
5674               inst.error = _("cannot combine pre- and post-indexing");
5675               return PARSE_OPERAND_FAIL;
5676             }
5677
5678           if (*p == '+') p++;
5679           else if (*p == '-') p++, inst.operands[i].negative = 1;
5680
5681           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5682             {
5683               /* We might be using the immediate for alignment already. If we
5684                  are, OR the register number into the low-order bits.  */
5685               if (inst.operands[i].immisalign)
5686                 inst.operands[i].imm |= reg;
5687               else
5688                 inst.operands[i].imm = reg;
5689               inst.operands[i].immisreg = 1;
5690
5691               if (skip_past_comma (&p) == SUCCESS)
5692                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5693                   return PARSE_OPERAND_FAIL;
5694             }
5695           else
5696             {
5697               char *q = p;
5698               if (inst.operands[i].negative)
5699                 {
5700                   inst.operands[i].negative = 0;
5701                   p--;
5702                 }
5703               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5704                 return PARSE_OPERAND_FAIL;
5705               /* If the offset is 0, find out if it's a +0 or -0.  */
5706               if (inst.reloc.exp.X_op == O_constant
5707                   && inst.reloc.exp.X_add_number == 0)
5708                 {
5709                   skip_whitespace (q);
5710                   if (*q == '#')
5711                     {
5712                       q++;
5713                       skip_whitespace (q);
5714                     }
5715                   if (*q == '-')
5716                     inst.operands[i].negative = 1;
5717                 }
5718             }
5719         }
5720     }
5721
5722   /* If at this point neither .preind nor .postind is set, we have a
5723      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5724   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5725     {
5726       inst.operands[i].preind = 1;
5727       inst.reloc.exp.X_op = O_constant;
5728       inst.reloc.exp.X_add_number = 0;
5729     }
5730   *str = p;
5731   return PARSE_OPERAND_SUCCESS;
5732 }
5733
5734 static int
5735 parse_address (char **str, int i)
5736 {
5737   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5738          ? SUCCESS : FAIL;
5739 }
5740
5741 static parse_operand_result
5742 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5743 {
5744   return parse_address_main (str, i, 1, type);
5745 }
5746
5747 /* Parse an operand for a MOVW or MOVT instruction.  */
5748 static int
5749 parse_half (char **str)
5750 {
5751   char * p;
5752
5753   p = *str;
5754   skip_past_char (&p, '#');
5755   if (strncasecmp (p, ":lower16:", 9) == 0)
5756     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5757   else if (strncasecmp (p, ":upper16:", 9) == 0)
5758     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5759
5760   if (inst.reloc.type != BFD_RELOC_UNUSED)
5761     {
5762       p += 9;
5763       skip_whitespace (p);
5764     }
5765
5766   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5767     return FAIL;
5768
5769   if (inst.reloc.type == BFD_RELOC_UNUSED)
5770     {
5771       if (inst.reloc.exp.X_op != O_constant)
5772         {
5773           inst.error = _("constant expression expected");
5774           return FAIL;
5775         }
5776       if (inst.reloc.exp.X_add_number < 0
5777           || inst.reloc.exp.X_add_number > 0xffff)
5778         {
5779           inst.error = _("immediate value out of range");
5780           return FAIL;
5781         }
5782     }
5783   *str = p;
5784   return SUCCESS;
5785 }
5786
5787 /* Miscellaneous. */
5788
5789 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5790    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5791 static int
5792 parse_psr (char **str, bfd_boolean lhs)
5793 {
5794   char *p;
5795   unsigned long psr_field;
5796   const struct asm_psr *psr;
5797   char *start;
5798   bfd_boolean is_apsr = FALSE;
5799   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5800
5801   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5802      be TRUE, but we want to ignore it in this case as we are building for any
5803      CPU type, including non-m variants.  */
5804   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5805     m_profile = FALSE;
5806
5807   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5808      feature for ease of use and backwards compatibility.  */
5809   p = *str;
5810   if (strncasecmp (p, "SPSR", 4) == 0)
5811     {
5812       if (m_profile)
5813         goto unsupported_psr;
5814
5815       psr_field = SPSR_BIT;
5816     }
5817   else if (strncasecmp (p, "CPSR", 4) == 0)
5818     {
5819       if (m_profile)
5820         goto unsupported_psr;
5821
5822       psr_field = 0;
5823     }
5824   else if (strncasecmp (p, "APSR", 4) == 0)
5825     {
5826       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5827          and ARMv7-R architecture CPUs.  */
5828       is_apsr = TRUE;
5829       psr_field = 0;
5830     }
5831   else if (m_profile)
5832     {
5833       start = p;
5834       do
5835         p++;
5836       while (ISALNUM (*p) || *p == '_');
5837
5838       if (strncasecmp (start, "iapsr", 5) == 0
5839           || strncasecmp (start, "eapsr", 5) == 0
5840           || strncasecmp (start, "xpsr", 4) == 0
5841           || strncasecmp (start, "psr", 3) == 0)
5842         p = start + strcspn (start, "rR") + 1;
5843
5844       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5845                                                   p - start);
5846
5847       if (!psr)
5848         return FAIL;
5849
5850       /* If APSR is being written, a bitfield may be specified.  Note that
5851          APSR itself is handled above.  */
5852       if (psr->field <= 3)
5853         {
5854           psr_field = psr->field;
5855           is_apsr = TRUE;
5856           goto check_suffix;
5857         }
5858
5859       *str = p;
5860       /* M-profile MSR instructions have the mask field set to "10", except
5861          *PSR variants which modify APSR, which may use a different mask (and
5862          have been handled already).  Do that by setting the PSR_f field
5863          here.  */
5864       return psr->field | (lhs ? PSR_f : 0);
5865     }
5866   else
5867     goto unsupported_psr;
5868
5869   p += 4;
5870 check_suffix:
5871   if (*p == '_')
5872     {
5873       /* A suffix follows.  */
5874       p++;
5875       start = p;
5876
5877       do
5878         p++;
5879       while (ISALNUM (*p) || *p == '_');
5880
5881       if (is_apsr)
5882         {
5883           /* APSR uses a notation for bits, rather than fields.  */
5884           unsigned int nzcvq_bits = 0;
5885           unsigned int g_bit = 0;
5886           char *bit;
5887
5888           for (bit = start; bit != p; bit++)
5889             {
5890               switch (TOLOWER (*bit))
5891                 {
5892                 case 'n':
5893                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5894                   break;
5895
5896                 case 'z':
5897                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5898                   break;
5899
5900                 case 'c':
5901                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5902                   break;
5903
5904                 case 'v':
5905                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5906                   break;
5907
5908                 case 'q':
5909                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5910                   break;
5911
5912                 case 'g':
5913                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5914                   break;
5915
5916                 default:
5917                   inst.error = _("unexpected bit specified after APSR");
5918                   return FAIL;
5919                 }
5920             }
5921
5922           if (nzcvq_bits == 0x1f)
5923             psr_field |= PSR_f;
5924
5925           if (g_bit == 0x1)
5926             {
5927               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5928                 {
5929                   inst.error = _("selected processor does not "
5930                                  "support DSP extension");
5931                   return FAIL;
5932                 }
5933
5934               psr_field |= PSR_s;
5935             }
5936
5937           if ((nzcvq_bits & 0x20) != 0
5938               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5939               || (g_bit & 0x2) != 0)
5940             {
5941               inst.error = _("bad bitmask specified after APSR");
5942               return FAIL;
5943             }
5944         }
5945       else
5946         {
5947           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5948                                                       p - start);
5949           if (!psr)
5950             goto error;
5951
5952           psr_field |= psr->field;
5953         }
5954     }
5955   else
5956     {
5957       if (ISALNUM (*p))
5958         goto error;    /* Garbage after "[CS]PSR".  */
5959
5960       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5961          is deprecated, but allow it anyway.  */
5962       if (is_apsr && lhs)
5963         {
5964           psr_field |= PSR_f;
5965           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5966                        "deprecated"));
5967         }
5968       else if (!m_profile)
5969         /* These bits are never right for M-profile devices: don't set them
5970            (only code paths which read/write APSR reach here).  */
5971         psr_field |= (PSR_c | PSR_f);
5972     }
5973   *str = p;
5974   return psr_field;
5975
5976  unsupported_psr:
5977   inst.error = _("selected processor does not support requested special "
5978                  "purpose register");
5979   return FAIL;
5980
5981  error:
5982   inst.error = _("flag for {c}psr instruction expected");
5983   return FAIL;
5984 }
5985
5986 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5987    value suitable for splatting into the AIF field of the instruction.  */
5988
5989 static int
5990 parse_cps_flags (char **str)
5991 {
5992   int val = 0;
5993   int saw_a_flag = 0;
5994   char *s = *str;
5995
5996   for (;;)
5997     switch (*s++)
5998       {
5999       case '\0': case ',':
6000         goto done;
6001
6002       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6003       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6004       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6005
6006       default:
6007         inst.error = _("unrecognized CPS flag");
6008         return FAIL;
6009       }
6010
6011  done:
6012   if (saw_a_flag == 0)
6013     {
6014       inst.error = _("missing CPS flags");
6015       return FAIL;
6016     }
6017
6018   *str = s - 1;
6019   return val;
6020 }
6021
6022 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6023    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6024
6025 static int
6026 parse_endian_specifier (char **str)
6027 {
6028   int little_endian;
6029   char *s = *str;
6030
6031   if (strncasecmp (s, "BE", 2))
6032     little_endian = 0;
6033   else if (strncasecmp (s, "LE", 2))
6034     little_endian = 1;
6035   else
6036     {
6037       inst.error = _("valid endian specifiers are be or le");
6038       return FAIL;
6039     }
6040
6041   if (ISALNUM (s[2]) || s[2] == '_')
6042     {
6043       inst.error = _("valid endian specifiers are be or le");
6044       return FAIL;
6045     }
6046
6047   *str = s + 2;
6048   return little_endian;
6049 }
6050
6051 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6052    value suitable for poking into the rotate field of an sxt or sxta
6053    instruction, or FAIL on error.  */
6054
6055 static int
6056 parse_ror (char **str)
6057 {
6058   int rot;
6059   char *s = *str;
6060
6061   if (strncasecmp (s, "ROR", 3) == 0)
6062     s += 3;
6063   else
6064     {
6065       inst.error = _("missing rotation field after comma");
6066       return FAIL;
6067     }
6068
6069   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6070     return FAIL;
6071
6072   switch (rot)
6073     {
6074     case  0: *str = s; return 0x0;
6075     case  8: *str = s; return 0x1;
6076     case 16: *str = s; return 0x2;
6077     case 24: *str = s; return 0x3;
6078
6079     default:
6080       inst.error = _("rotation can only be 0, 8, 16, or 24");
6081       return FAIL;
6082     }
6083 }
6084
6085 /* Parse a conditional code (from conds[] below).  The value returned is in the
6086    range 0 .. 14, or FAIL.  */
6087 static int
6088 parse_cond (char **str)
6089 {
6090   char *q;
6091   const struct asm_cond *c;
6092   int n;
6093   /* Condition codes are always 2 characters, so matching up to
6094      3 characters is sufficient.  */
6095   char cond[3];
6096
6097   q = *str;
6098   n = 0;
6099   while (ISALPHA (*q) && n < 3)
6100     {
6101       cond[n] = TOLOWER (*q);
6102       q++;
6103       n++;
6104     }
6105
6106   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6107   if (!c)
6108     {
6109       inst.error = _("condition required");
6110       return FAIL;
6111     }
6112
6113   *str = q;
6114   return c->value;
6115 }
6116
6117 /* Record a use of the given feature.  */
6118 static void
6119 record_feature_use (const arm_feature_set *feature)
6120 {
6121   if (thumb_mode)
6122     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6123   else
6124     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6125 }
6126
6127 /* If the given feature available in the selected CPU, mark it as used.
6128    Returns TRUE iff feature is available.  */
6129 static bfd_boolean
6130 mark_feature_used (const arm_feature_set *feature)
6131 {
6132   /* Ensure the option is valid on the current architecture.  */
6133   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6134     return FALSE;
6135
6136   /* Add the appropriate architecture feature for the barrier option used.
6137      */
6138   record_feature_use (feature);
6139
6140   return TRUE;
6141 }
6142
6143 /* Parse an option for a barrier instruction.  Returns the encoding for the
6144    option, or FAIL.  */
6145 static int
6146 parse_barrier (char **str)
6147 {
6148   char *p, *q;
6149   const struct asm_barrier_opt *o;
6150
6151   p = q = *str;
6152   while (ISALPHA (*q))
6153     q++;
6154
6155   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6156                                                     q - p);
6157   if (!o)
6158     return FAIL;
6159
6160   if (!mark_feature_used (&o->arch))
6161     return FAIL;
6162
6163   *str = q;
6164   return o->value;
6165 }
6166
6167 /* Parse the operands of a table branch instruction.  Similar to a memory
6168    operand.  */
6169 static int
6170 parse_tb (char **str)
6171 {
6172   char * p = *str;
6173   int reg;
6174
6175   if (skip_past_char (&p, '[') == FAIL)
6176     {
6177       inst.error = _("'[' expected");
6178       return FAIL;
6179     }
6180
6181   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6182     {
6183       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6184       return FAIL;
6185     }
6186   inst.operands[0].reg = reg;
6187
6188   if (skip_past_comma (&p) == FAIL)
6189     {
6190       inst.error = _("',' expected");
6191       return FAIL;
6192     }
6193
6194   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6195     {
6196       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6197       return FAIL;
6198     }
6199   inst.operands[0].imm = reg;
6200
6201   if (skip_past_comma (&p) == SUCCESS)
6202     {
6203       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6204         return FAIL;
6205       if (inst.reloc.exp.X_add_number != 1)
6206         {
6207           inst.error = _("invalid shift");
6208           return FAIL;
6209         }
6210       inst.operands[0].shifted = 1;
6211     }
6212
6213   if (skip_past_char (&p, ']') == FAIL)
6214     {
6215       inst.error = _("']' expected");
6216       return FAIL;
6217     }
6218   *str = p;
6219   return SUCCESS;
6220 }
6221
6222 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6223    information on the types the operands can take and how they are encoded.
6224    Up to four operands may be read; this function handles setting the
6225    ".present" field for each read operand itself.
6226    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6227    else returns FAIL.  */
6228
6229 static int
6230 parse_neon_mov (char **str, int *which_operand)
6231 {
6232   int i = *which_operand, val;
6233   enum arm_reg_type rtype;
6234   char *ptr = *str;
6235   struct neon_type_el optype;
6236
6237   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6238     {
6239       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6240       inst.operands[i].reg = val;
6241       inst.operands[i].isscalar = 1;
6242       inst.operands[i].vectype = optype;
6243       inst.operands[i++].present = 1;
6244
6245       if (skip_past_comma (&ptr) == FAIL)
6246         goto wanted_comma;
6247
6248       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6249         goto wanted_arm;
6250
6251       inst.operands[i].reg = val;
6252       inst.operands[i].isreg = 1;
6253       inst.operands[i].present = 1;
6254     }
6255   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6256            != FAIL)
6257     {
6258       /* Cases 0, 1, 2, 3, 5 (D only).  */
6259       if (skip_past_comma (&ptr) == FAIL)
6260         goto wanted_comma;
6261
6262       inst.operands[i].reg = val;
6263       inst.operands[i].isreg = 1;
6264       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6265       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6266       inst.operands[i].isvec = 1;
6267       inst.operands[i].vectype = optype;
6268       inst.operands[i++].present = 1;
6269
6270       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6271         {
6272           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6273              Case 13: VMOV <Sd>, <Rm>  */
6274           inst.operands[i].reg = val;
6275           inst.operands[i].isreg = 1;
6276           inst.operands[i].present = 1;
6277
6278           if (rtype == REG_TYPE_NQ)
6279             {
6280               first_error (_("can't use Neon quad register here"));
6281               return FAIL;
6282             }
6283           else if (rtype != REG_TYPE_VFS)
6284             {
6285               i++;
6286               if (skip_past_comma (&ptr) == FAIL)
6287                 goto wanted_comma;
6288               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6289                 goto wanted_arm;
6290               inst.operands[i].reg = val;
6291               inst.operands[i].isreg = 1;
6292               inst.operands[i].present = 1;
6293             }
6294         }
6295       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6296                                            &optype)) != FAIL)
6297         {
6298           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6299              Case 1: VMOV<c><q> <Dd>, <Dm>
6300              Case 8: VMOV.F32 <Sd>, <Sm>
6301              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6302
6303           inst.operands[i].reg = val;
6304           inst.operands[i].isreg = 1;
6305           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6306           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6307           inst.operands[i].isvec = 1;
6308           inst.operands[i].vectype = optype;
6309           inst.operands[i].present = 1;
6310
6311           if (skip_past_comma (&ptr) == SUCCESS)
6312             {
6313               /* Case 15.  */
6314               i++;
6315
6316               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6317                 goto wanted_arm;
6318
6319               inst.operands[i].reg = val;
6320               inst.operands[i].isreg = 1;
6321               inst.operands[i++].present = 1;
6322
6323               if (skip_past_comma (&ptr) == FAIL)
6324                 goto wanted_comma;
6325
6326               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6327                 goto wanted_arm;
6328
6329               inst.operands[i].reg = val;
6330               inst.operands[i].isreg = 1;
6331               inst.operands[i].present = 1;
6332             }
6333         }
6334       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6335           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6336              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6337              Case 10: VMOV.F32 <Sd>, #<imm>
6338              Case 11: VMOV.F64 <Dd>, #<imm>  */
6339         inst.operands[i].immisfloat = 1;
6340       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6341                == SUCCESS)
6342           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6343              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6344         ;
6345       else
6346         {
6347           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6348           return FAIL;
6349         }
6350     }
6351   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6352     {
6353       /* Cases 6, 7.  */
6354       inst.operands[i].reg = val;
6355       inst.operands[i].isreg = 1;
6356       inst.operands[i++].present = 1;
6357
6358       if (skip_past_comma (&ptr) == FAIL)
6359         goto wanted_comma;
6360
6361       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6362         {
6363           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6364           inst.operands[i].reg = val;
6365           inst.operands[i].isscalar = 1;
6366           inst.operands[i].present = 1;
6367           inst.operands[i].vectype = optype;
6368         }
6369       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6370         {
6371           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6372           inst.operands[i].reg = val;
6373           inst.operands[i].isreg = 1;
6374           inst.operands[i++].present = 1;
6375
6376           if (skip_past_comma (&ptr) == FAIL)
6377             goto wanted_comma;
6378
6379           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6380               == FAIL)
6381             {
6382               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6383               return FAIL;
6384             }
6385
6386           inst.operands[i].reg = val;
6387           inst.operands[i].isreg = 1;
6388           inst.operands[i].isvec = 1;
6389           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6390           inst.operands[i].vectype = optype;
6391           inst.operands[i].present = 1;
6392
6393           if (rtype == REG_TYPE_VFS)
6394             {
6395               /* Case 14.  */
6396               i++;
6397               if (skip_past_comma (&ptr) == FAIL)
6398                 goto wanted_comma;
6399               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6400                                               &optype)) == FAIL)
6401                 {
6402                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6403                   return FAIL;
6404                 }
6405               inst.operands[i].reg = val;
6406               inst.operands[i].isreg = 1;
6407               inst.operands[i].isvec = 1;
6408               inst.operands[i].issingle = 1;
6409               inst.operands[i].vectype = optype;
6410               inst.operands[i].present = 1;
6411             }
6412         }
6413       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6414                != FAIL)
6415         {
6416           /* Case 13.  */
6417           inst.operands[i].reg = val;
6418           inst.operands[i].isreg = 1;
6419           inst.operands[i].isvec = 1;
6420           inst.operands[i].issingle = 1;
6421           inst.operands[i].vectype = optype;
6422           inst.operands[i].present = 1;
6423         }
6424     }
6425   else
6426     {
6427       first_error (_("parse error"));
6428       return FAIL;
6429     }
6430
6431   /* Successfully parsed the operands. Update args.  */
6432   *which_operand = i;
6433   *str = ptr;
6434   return SUCCESS;
6435
6436  wanted_comma:
6437   first_error (_("expected comma"));
6438   return FAIL;
6439
6440  wanted_arm:
6441   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6442   return FAIL;
6443 }
6444
6445 /* Use this macro when the operand constraints are different
6446    for ARM and THUMB (e.g. ldrd).  */
6447 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6448         ((arm_operand) | ((thumb_operand) << 16))
6449
6450 /* Matcher codes for parse_operands.  */
6451 enum operand_parse_code
6452 {
6453   OP_stop,      /* end of line */
6454
6455   OP_RR,        /* ARM register */
6456   OP_RRnpc,     /* ARM register, not r15 */
6457   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6458   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6459   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6460                    optional trailing ! */
6461   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6462   OP_RCP,       /* Coprocessor number */
6463   OP_RCN,       /* Coprocessor register */
6464   OP_RF,        /* FPA register */
6465   OP_RVS,       /* VFP single precision register */
6466   OP_RVD,       /* VFP double precision register (0..15) */
6467   OP_RND,       /* Neon double precision register (0..31) */
6468   OP_RNQ,       /* Neon quad precision register */
6469   OP_RVSD,      /* VFP single or double precision register */
6470   OP_RNDQ,      /* Neon double or quad precision register */
6471   OP_RNSDQ,     /* Neon single, double or quad precision register */
6472   OP_RNSC,      /* Neon scalar D[X] */
6473   OP_RVC,       /* VFP control register */
6474   OP_RMF,       /* Maverick F register */
6475   OP_RMD,       /* Maverick D register */
6476   OP_RMFX,      /* Maverick FX register */
6477   OP_RMDX,      /* Maverick DX register */
6478   OP_RMAX,      /* Maverick AX register */
6479   OP_RMDS,      /* Maverick DSPSC register */
6480   OP_RIWR,      /* iWMMXt wR register */
6481   OP_RIWC,      /* iWMMXt wC register */
6482   OP_RIWG,      /* iWMMXt wCG register */
6483   OP_RXA,       /* XScale accumulator register */
6484
6485   OP_REGLST,    /* ARM register list */
6486   OP_VRSLST,    /* VFP single-precision register list */
6487   OP_VRDLST,    /* VFP double-precision register list */
6488   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6489   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6490   OP_NSTRLST,   /* Neon element/structure list */
6491
6492   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6493   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6494   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6495   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6496   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6497   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6498   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6499   OP_VMOV,      /* Neon VMOV operands.  */
6500   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6501   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6502   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6503
6504   OP_I0,        /* immediate zero */
6505   OP_I7,        /* immediate value 0 .. 7 */
6506   OP_I15,       /*                 0 .. 15 */
6507   OP_I16,       /*                 1 .. 16 */
6508   OP_I16z,      /*                 0 .. 16 */
6509   OP_I31,       /*                 0 .. 31 */
6510   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6511   OP_I32,       /*                 1 .. 32 */
6512   OP_I32z,      /*                 0 .. 32 */
6513   OP_I63,       /*                 0 .. 63 */
6514   OP_I63s,      /*               -64 .. 63 */
6515   OP_I64,       /*                 1 .. 64 */
6516   OP_I64z,      /*                 0 .. 64 */
6517   OP_I255,      /*                 0 .. 255 */
6518
6519   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6520   OP_I7b,       /*                             0 .. 7 */
6521   OP_I15b,      /*                             0 .. 15 */
6522   OP_I31b,      /*                             0 .. 31 */
6523
6524   OP_SH,        /* shifter operand */
6525   OP_SHG,       /* shifter operand with possible group relocation */
6526   OP_ADDR,      /* Memory address expression (any mode) */
6527   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6528   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6529   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6530   OP_EXP,       /* arbitrary expression */
6531   OP_EXPi,      /* same, with optional immediate prefix */
6532   OP_EXPr,      /* same, with optional relocation suffix */
6533   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6534
6535   OP_CPSF,      /* CPS flags */
6536   OP_ENDI,      /* Endianness specifier */
6537   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6538   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6539   OP_COND,      /* conditional code */
6540   OP_TB,        /* Table branch.  */
6541
6542   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6543
6544   OP_RRnpc_I0,  /* ARM register or literal 0 */
6545   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6546   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6547   OP_RF_IF,     /* FPA register or immediate */
6548   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6549   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6550
6551   /* Optional operands.  */
6552   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6553   OP_oI31b,      /*                             0 .. 31 */
6554   OP_oI32b,      /*                             1 .. 32 */
6555   OP_oI32z,      /*                             0 .. 32 */
6556   OP_oIffffb,    /*                             0 .. 65535 */
6557   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6558
6559   OP_oRR,        /* ARM register */
6560   OP_oRRnpc,     /* ARM register, not the PC */
6561   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6562   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6563   OP_oRND,       /* Optional Neon double precision register */
6564   OP_oRNQ,       /* Optional Neon quad precision register */
6565   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6566   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6567   OP_oSHll,      /* LSL immediate */
6568   OP_oSHar,      /* ASR immediate */
6569   OP_oSHllar,    /* LSL or ASR immediate */
6570   OP_oROR,       /* ROR 0/8/16/24 */
6571   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6572
6573   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6574   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6575   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6576   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6577
6578   OP_FIRST_OPTIONAL = OP_oI7b
6579 };
6580
6581 /* Generic instruction operand parser.  This does no encoding and no
6582    semantic validation; it merely squirrels values away in the inst
6583    structure.  Returns SUCCESS or FAIL depending on whether the
6584    specified grammar matched.  */
6585 static int
6586 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6587 {
6588   unsigned const int *upat = pattern;
6589   char *backtrack_pos = 0;
6590   const char *backtrack_error = 0;
6591   int i, val = 0, backtrack_index = 0;
6592   enum arm_reg_type rtype;
6593   parse_operand_result result;
6594   unsigned int op_parse_code;
6595
6596 #define po_char_or_fail(chr)                    \
6597   do                                            \
6598     {                                           \
6599       if (skip_past_char (&str, chr) == FAIL)   \
6600         goto bad_args;                          \
6601     }                                           \
6602   while (0)
6603
6604 #define po_reg_or_fail(regtype)                                 \
6605   do                                                            \
6606     {                                                           \
6607       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6608                                  & inst.operands[i].vectype);   \
6609       if (val == FAIL)                                          \
6610         {                                                       \
6611           first_error (_(reg_expected_msgs[regtype]));          \
6612           goto failure;                                         \
6613         }                                                       \
6614       inst.operands[i].reg = val;                               \
6615       inst.operands[i].isreg = 1;                               \
6616       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6617       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6618       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6619                              || rtype == REG_TYPE_VFD           \
6620                              || rtype == REG_TYPE_NQ);          \
6621     }                                                           \
6622   while (0)
6623
6624 #define po_reg_or_goto(regtype, label)                          \
6625   do                                                            \
6626     {                                                           \
6627       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6628                                  & inst.operands[i].vectype);   \
6629       if (val == FAIL)                                          \
6630         goto label;                                             \
6631                                                                 \
6632       inst.operands[i].reg = val;                               \
6633       inst.operands[i].isreg = 1;                               \
6634       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6635       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6636       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6637                              || rtype == REG_TYPE_VFD           \
6638                              || rtype == REG_TYPE_NQ);          \
6639     }                                                           \
6640   while (0)
6641
6642 #define po_imm_or_fail(min, max, popt)                          \
6643   do                                                            \
6644     {                                                           \
6645       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6646         goto failure;                                           \
6647       inst.operands[i].imm = val;                               \
6648     }                                                           \
6649   while (0)
6650
6651 #define po_scalar_or_goto(elsz, label)                                  \
6652   do                                                                    \
6653     {                                                                   \
6654       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6655       if (val == FAIL)                                                  \
6656         goto label;                                                     \
6657       inst.operands[i].reg = val;                                       \
6658       inst.operands[i].isscalar = 1;                                    \
6659     }                                                                   \
6660   while (0)
6661
6662 #define po_misc_or_fail(expr)                   \
6663   do                                            \
6664     {                                           \
6665       if (expr)                                 \
6666         goto failure;                           \
6667     }                                           \
6668   while (0)
6669
6670 #define po_misc_or_fail_no_backtrack(expr)              \
6671   do                                                    \
6672     {                                                   \
6673       result = expr;                                    \
6674       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6675         backtrack_pos = 0;                              \
6676       if (result != PARSE_OPERAND_SUCCESS)              \
6677         goto failure;                                   \
6678     }                                                   \
6679   while (0)
6680
6681 #define po_barrier_or_imm(str)                             \
6682   do                                                       \
6683     {                                                      \
6684       val = parse_barrier (&str);                          \
6685       if (val == FAIL && ! ISALPHA (*str))                 \
6686         goto immediate;                                    \
6687       if (val == FAIL                                      \
6688           /* ISB can only take SY as an option.  */        \
6689           || ((inst.instruction & 0xf0) == 0x60            \
6690                && val != 0xf))                             \
6691         {                                                  \
6692            inst.error = _("invalid barrier type");         \
6693            backtrack_pos = 0;                              \
6694            goto failure;                                   \
6695         }                                                  \
6696     }                                                      \
6697   while (0)
6698
6699   skip_whitespace (str);
6700
6701   for (i = 0; upat[i] != OP_stop; i++)
6702     {
6703       op_parse_code = upat[i];
6704       if (op_parse_code >= 1<<16)
6705         op_parse_code = thumb ? (op_parse_code >> 16)
6706                                 : (op_parse_code & ((1<<16)-1));
6707
6708       if (op_parse_code >= OP_FIRST_OPTIONAL)
6709         {
6710           /* Remember where we are in case we need to backtrack.  */
6711           gas_assert (!backtrack_pos);
6712           backtrack_pos = str;
6713           backtrack_error = inst.error;
6714           backtrack_index = i;
6715         }
6716
6717       if (i > 0 && (i > 1 || inst.operands[0].present))
6718         po_char_or_fail (',');
6719
6720       switch (op_parse_code)
6721         {
6722           /* Registers */
6723         case OP_oRRnpc:
6724         case OP_oRRnpcsp:
6725         case OP_RRnpc:
6726         case OP_RRnpcsp:
6727         case OP_oRR:
6728         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6729         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6730         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6731         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6732         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6733         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6734         case OP_oRND:
6735         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6736         case OP_RVC:
6737           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6738           break;
6739           /* Also accept generic coprocessor regs for unknown registers.  */
6740           coproc_reg:
6741           po_reg_or_fail (REG_TYPE_CN);
6742           break;
6743         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6744         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6745         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6746         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6747         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6748         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6749         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6750         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6751         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6752         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6753         case OP_oRNQ:
6754         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6755         case OP_oRNDQ:
6756         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6757         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6758         case OP_oRNSDQ:
6759         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6760
6761         /* Neon scalar. Using an element size of 8 means that some invalid
6762            scalars are accepted here, so deal with those in later code.  */
6763         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6764
6765         case OP_RNDQ_I0:
6766           {
6767             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6768             break;
6769             try_imm0:
6770             po_imm_or_fail (0, 0, TRUE);
6771           }
6772           break;
6773
6774         case OP_RVSD_I0:
6775           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6776           break;
6777
6778         case OP_RSVD_FI0:
6779           {
6780             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6781             break;
6782             try_ifimm0:
6783             if (parse_ifimm_zero (&str))
6784               inst.operands[i].imm = 0;
6785             else
6786             {
6787               inst.error
6788                 = _("only floating point zero is allowed as immediate value");
6789               goto failure;
6790             }
6791           }
6792           break;
6793
6794         case OP_RR_RNSC:
6795           {
6796             po_scalar_or_goto (8, try_rr);
6797             break;
6798             try_rr:
6799             po_reg_or_fail (REG_TYPE_RN);
6800           }
6801           break;
6802
6803         case OP_RNSDQ_RNSC:
6804           {
6805             po_scalar_or_goto (8, try_nsdq);
6806             break;
6807             try_nsdq:
6808             po_reg_or_fail (REG_TYPE_NSDQ);
6809           }
6810           break;
6811
6812         case OP_RNDQ_RNSC:
6813           {
6814             po_scalar_or_goto (8, try_ndq);
6815             break;
6816             try_ndq:
6817             po_reg_or_fail (REG_TYPE_NDQ);
6818           }
6819           break;
6820
6821         case OP_RND_RNSC:
6822           {
6823             po_scalar_or_goto (8, try_vfd);
6824             break;
6825             try_vfd:
6826             po_reg_or_fail (REG_TYPE_VFD);
6827           }
6828           break;
6829
6830         case OP_VMOV:
6831           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6832              not careful then bad things might happen.  */
6833           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6834           break;
6835
6836         case OP_RNDQ_Ibig:
6837           {
6838             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6839             break;
6840             try_immbig:
6841             /* There's a possibility of getting a 64-bit immediate here, so
6842                we need special handling.  */
6843             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6844                 == FAIL)
6845               {
6846                 inst.error = _("immediate value is out of range");
6847                 goto failure;
6848               }
6849           }
6850           break;
6851
6852         case OP_RNDQ_I63b:
6853           {
6854             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6855             break;
6856             try_shimm:
6857             po_imm_or_fail (0, 63, TRUE);
6858           }
6859           break;
6860
6861         case OP_RRnpcb:
6862           po_char_or_fail ('[');
6863           po_reg_or_fail  (REG_TYPE_RN);
6864           po_char_or_fail (']');
6865           break;
6866
6867         case OP_RRnpctw:
6868         case OP_RRw:
6869         case OP_oRRw:
6870           po_reg_or_fail (REG_TYPE_RN);
6871           if (skip_past_char (&str, '!') == SUCCESS)
6872             inst.operands[i].writeback = 1;
6873           break;
6874
6875           /* Immediates */
6876         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6877         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6878         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6879         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6880         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6881         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6882         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6883         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6884         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6885         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6886         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6887         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6888
6889         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6890         case OP_oI7b:
6891         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6892         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6893         case OP_oI31b:
6894         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6895         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6896         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6897         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6898
6899           /* Immediate variants */
6900         case OP_oI255c:
6901           po_char_or_fail ('{');
6902           po_imm_or_fail (0, 255, TRUE);
6903           po_char_or_fail ('}');
6904           break;
6905
6906         case OP_I31w:
6907           /* The expression parser chokes on a trailing !, so we have
6908              to find it first and zap it.  */
6909           {
6910             char *s = str;
6911             while (*s && *s != ',')
6912               s++;
6913             if (s[-1] == '!')
6914               {
6915                 s[-1] = '\0';
6916                 inst.operands[i].writeback = 1;
6917               }
6918             po_imm_or_fail (0, 31, TRUE);
6919             if (str == s - 1)
6920               str = s;
6921           }
6922           break;
6923
6924           /* Expressions */
6925         case OP_EXPi:   EXPi:
6926           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6927                                               GE_OPT_PREFIX));
6928           break;
6929
6930         case OP_EXP:
6931           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6932                                               GE_NO_PREFIX));
6933           break;
6934
6935         case OP_EXPr:   EXPr:
6936           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6937                                               GE_NO_PREFIX));
6938           if (inst.reloc.exp.X_op == O_symbol)
6939             {
6940               val = parse_reloc (&str);
6941               if (val == -1)
6942                 {
6943                   inst.error = _("unrecognized relocation suffix");
6944                   goto failure;
6945                 }
6946               else if (val != BFD_RELOC_UNUSED)
6947                 {
6948                   inst.operands[i].imm = val;
6949                   inst.operands[i].hasreloc = 1;
6950                 }
6951             }
6952           break;
6953
6954           /* Operand for MOVW or MOVT.  */
6955         case OP_HALF:
6956           po_misc_or_fail (parse_half (&str));
6957           break;
6958
6959           /* Register or expression.  */
6960         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6961         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6962
6963           /* Register or immediate.  */
6964         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6965         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6966
6967         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6968         IF:
6969           if (!is_immediate_prefix (*str))
6970             goto bad_args;
6971           str++;
6972           val = parse_fpa_immediate (&str);
6973           if (val == FAIL)
6974             goto failure;
6975           /* FPA immediates are encoded as registers 8-15.
6976              parse_fpa_immediate has already applied the offset.  */
6977           inst.operands[i].reg = val;
6978           inst.operands[i].isreg = 1;
6979           break;
6980
6981         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6982         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6983
6984           /* Two kinds of register.  */
6985         case OP_RIWR_RIWC:
6986           {
6987             struct reg_entry *rege = arm_reg_parse_multi (&str);
6988             if (!rege
6989                 || (rege->type != REG_TYPE_MMXWR
6990                     && rege->type != REG_TYPE_MMXWC
6991                     && rege->type != REG_TYPE_MMXWCG))
6992               {
6993                 inst.error = _("iWMMXt data or control register expected");
6994                 goto failure;
6995               }
6996             inst.operands[i].reg = rege->number;
6997             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6998           }
6999           break;
7000
7001         case OP_RIWC_RIWG:
7002           {
7003             struct reg_entry *rege = arm_reg_parse_multi (&str);
7004             if (!rege
7005                 || (rege->type != REG_TYPE_MMXWC
7006                     && rege->type != REG_TYPE_MMXWCG))
7007               {
7008                 inst.error = _("iWMMXt control register expected");
7009                 goto failure;
7010               }
7011             inst.operands[i].reg = rege->number;
7012             inst.operands[i].isreg = 1;
7013           }
7014           break;
7015
7016           /* Misc */
7017         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7018         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7019         case OP_oROR:    val = parse_ror (&str);                break;
7020         case OP_COND:    val = parse_cond (&str);               break;
7021         case OP_oBARRIER_I15:
7022           po_barrier_or_imm (str); break;
7023           immediate:
7024           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7025             goto failure;
7026           break;
7027
7028         case OP_wPSR:
7029         case OP_rPSR:
7030           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7031           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7032             {
7033               inst.error = _("Banked registers are not available with this "
7034                              "architecture.");
7035               goto failure;
7036             }
7037           break;
7038           try_psr:
7039           val = parse_psr (&str, op_parse_code == OP_wPSR);
7040           break;
7041
7042         case OP_APSR_RR:
7043           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7044           break;
7045           try_apsr:
7046           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7047              instruction).  */
7048           if (strncasecmp (str, "APSR_", 5) == 0)
7049             {
7050               unsigned found = 0;
7051               str += 5;
7052               while (found < 15)
7053                 switch (*str++)
7054                   {
7055                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7056                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7057                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7058                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7059                   default: found = 16;
7060                   }
7061               if (found != 15)
7062                 goto failure;
7063               inst.operands[i].isvec = 1;
7064               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7065               inst.operands[i].reg = REG_PC;
7066             }
7067           else
7068             goto failure;
7069           break;
7070
7071         case OP_TB:
7072           po_misc_or_fail (parse_tb (&str));
7073           break;
7074
7075           /* Register lists.  */
7076         case OP_REGLST:
7077           val = parse_reg_list (&str);
7078           if (*str == '^')
7079             {
7080               inst.operands[i].writeback = 1;
7081               str++;
7082             }
7083           break;
7084
7085         case OP_VRSLST:
7086           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7087           break;
7088
7089         case OP_VRDLST:
7090           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7091           break;
7092
7093         case OP_VRSDLST:
7094           /* Allow Q registers too.  */
7095           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7096                                     REGLIST_NEON_D);
7097           if (val == FAIL)
7098             {
7099               inst.error = NULL;
7100               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7101                                         REGLIST_VFP_S);
7102               inst.operands[i].issingle = 1;
7103             }
7104           break;
7105
7106         case OP_NRDLST:
7107           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7108                                     REGLIST_NEON_D);
7109           break;
7110
7111         case OP_NSTRLST:
7112           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7113                                            &inst.operands[i].vectype);
7114           break;
7115
7116           /* Addressing modes */
7117         case OP_ADDR:
7118           po_misc_or_fail (parse_address (&str, i));
7119           break;
7120
7121         case OP_ADDRGLDR:
7122           po_misc_or_fail_no_backtrack (
7123             parse_address_group_reloc (&str, i, GROUP_LDR));
7124           break;
7125
7126         case OP_ADDRGLDRS:
7127           po_misc_or_fail_no_backtrack (
7128             parse_address_group_reloc (&str, i, GROUP_LDRS));
7129           break;
7130
7131         case OP_ADDRGLDC:
7132           po_misc_or_fail_no_backtrack (
7133             parse_address_group_reloc (&str, i, GROUP_LDC));
7134           break;
7135
7136         case OP_SH:
7137           po_misc_or_fail (parse_shifter_operand (&str, i));
7138           break;
7139
7140         case OP_SHG:
7141           po_misc_or_fail_no_backtrack (
7142             parse_shifter_operand_group_reloc (&str, i));
7143           break;
7144
7145         case OP_oSHll:
7146           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7147           break;
7148
7149         case OP_oSHar:
7150           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7151           break;
7152
7153         case OP_oSHllar:
7154           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7155           break;
7156
7157         default:
7158           as_fatal (_("unhandled operand code %d"), op_parse_code);
7159         }
7160
7161       /* Various value-based sanity checks and shared operations.  We
7162          do not signal immediate failures for the register constraints;
7163          this allows a syntax error to take precedence.  */
7164       switch (op_parse_code)
7165         {
7166         case OP_oRRnpc:
7167         case OP_RRnpc:
7168         case OP_RRnpcb:
7169         case OP_RRw:
7170         case OP_oRRw:
7171         case OP_RRnpc_I0:
7172           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7173             inst.error = BAD_PC;
7174           break;
7175
7176         case OP_oRRnpcsp:
7177         case OP_RRnpcsp:
7178           if (inst.operands[i].isreg)
7179             {
7180               if (inst.operands[i].reg == REG_PC)
7181                 inst.error = BAD_PC;
7182               else if (inst.operands[i].reg == REG_SP)
7183                 inst.error = BAD_SP;
7184             }
7185           break;
7186
7187         case OP_RRnpctw:
7188           if (inst.operands[i].isreg
7189               && inst.operands[i].reg == REG_PC
7190               && (inst.operands[i].writeback || thumb))
7191             inst.error = BAD_PC;
7192           break;
7193
7194         case OP_CPSF:
7195         case OP_ENDI:
7196         case OP_oROR:
7197         case OP_wPSR:
7198         case OP_rPSR:
7199         case OP_COND:
7200         case OP_oBARRIER_I15:
7201         case OP_REGLST:
7202         case OP_VRSLST:
7203         case OP_VRDLST:
7204         case OP_VRSDLST:
7205         case OP_NRDLST:
7206         case OP_NSTRLST:
7207           if (val == FAIL)
7208             goto failure;
7209           inst.operands[i].imm = val;
7210           break;
7211
7212         default:
7213           break;
7214         }
7215
7216       /* If we get here, this operand was successfully parsed.  */
7217       inst.operands[i].present = 1;
7218       continue;
7219
7220     bad_args:
7221       inst.error = BAD_ARGS;
7222
7223     failure:
7224       if (!backtrack_pos)
7225         {
7226           /* The parse routine should already have set inst.error, but set a
7227              default here just in case.  */
7228           if (!inst.error)
7229             inst.error = _("syntax error");
7230           return FAIL;
7231         }
7232
7233       /* Do not backtrack over a trailing optional argument that
7234          absorbed some text.  We will only fail again, with the
7235          'garbage following instruction' error message, which is
7236          probably less helpful than the current one.  */
7237       if (backtrack_index == i && backtrack_pos != str
7238           && upat[i+1] == OP_stop)
7239         {
7240           if (!inst.error)
7241             inst.error = _("syntax error");
7242           return FAIL;
7243         }
7244
7245       /* Try again, skipping the optional argument at backtrack_pos.  */
7246       str = backtrack_pos;
7247       inst.error = backtrack_error;
7248       inst.operands[backtrack_index].present = 0;
7249       i = backtrack_index;
7250       backtrack_pos = 0;
7251     }
7252
7253   /* Check that we have parsed all the arguments.  */
7254   if (*str != '\0' && !inst.error)
7255     inst.error = _("garbage following instruction");
7256
7257   return inst.error ? FAIL : SUCCESS;
7258 }
7259
7260 #undef po_char_or_fail
7261 #undef po_reg_or_fail
7262 #undef po_reg_or_goto
7263 #undef po_imm_or_fail
7264 #undef po_scalar_or_fail
7265 #undef po_barrier_or_imm
7266
7267 /* Shorthand macro for instruction encoding functions issuing errors.  */
7268 #define constraint(expr, err)                   \
7269   do                                            \
7270     {                                           \
7271       if (expr)                                 \
7272         {                                       \
7273           inst.error = err;                     \
7274           return;                               \
7275         }                                       \
7276     }                                           \
7277   while (0)
7278
7279 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7280    instructions are unpredictable if these registers are used.  This
7281    is the BadReg predicate in ARM's Thumb-2 documentation.  */
7282 #define reject_bad_reg(reg)                             \
7283   do                                                    \
7284    if (reg == REG_SP || reg == REG_PC)                  \
7285      {                                                  \
7286        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
7287        return;                                          \
7288      }                                                  \
7289   while (0)
7290
7291 /* If REG is R13 (the stack pointer), warn that its use is
7292    deprecated.  */
7293 #define warn_deprecated_sp(reg)                 \
7294   do                                            \
7295     if (warn_on_deprecated && reg == REG_SP)    \
7296        as_tsktsk (_("use of r13 is deprecated"));       \
7297   while (0)
7298
7299 /* Functions for operand encoding.  ARM, then Thumb.  */
7300
7301 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7302
7303 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7304
7305    The only binary encoding difference is the Coprocessor number.  Coprocessor
7306    9 is used for half-precision calculations or conversions.  The format of the
7307    instruction is the same as the equivalent Coprocessor 10 instuction that
7308    exists for Single-Precision operation.  */
7309
7310 static void
7311 do_scalar_fp16_v82_encode (void)
7312 {
7313   if (inst.cond != COND_ALWAYS)
7314     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7315                " the behaviour is UNPREDICTABLE"));
7316   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7317               _(BAD_FP16));
7318
7319   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7320   mark_feature_used (&arm_ext_fp16);
7321 }
7322
7323 /* If VAL can be encoded in the immediate field of an ARM instruction,
7324    return the encoded form.  Otherwise, return FAIL.  */
7325
7326 static unsigned int
7327 encode_arm_immediate (unsigned int val)
7328 {
7329   unsigned int a, i;
7330
7331   if (val <= 0xff)
7332     return val;
7333
7334   for (i = 2; i < 32; i += 2)
7335     if ((a = rotate_left (val, i)) <= 0xff)
7336       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7337
7338   return FAIL;
7339 }
7340
7341 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7342    return the encoded form.  Otherwise, return FAIL.  */
7343 static unsigned int
7344 encode_thumb32_immediate (unsigned int val)
7345 {
7346   unsigned int a, i;
7347
7348   if (val <= 0xff)
7349     return val;
7350
7351   for (i = 1; i <= 24; i++)
7352     {
7353       a = val >> i;
7354       if ((val & ~(0xff << i)) == 0)
7355         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7356     }
7357
7358   a = val & 0xff;
7359   if (val == ((a << 16) | a))
7360     return 0x100 | a;
7361   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7362     return 0x300 | a;
7363
7364   a = val & 0xff00;
7365   if (val == ((a << 16) | a))
7366     return 0x200 | (a >> 8);
7367
7368   return FAIL;
7369 }
7370 /* Encode a VFP SP or DP register number into inst.instruction.  */
7371
7372 static void
7373 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7374 {
7375   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7376       && reg > 15)
7377     {
7378       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7379         {
7380           if (thumb_mode)
7381             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7382                                     fpu_vfp_ext_d32);
7383           else
7384             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7385                                     fpu_vfp_ext_d32);
7386         }
7387       else
7388         {
7389           first_error (_("D register out of range for selected VFP version"));
7390           return;
7391         }
7392     }
7393
7394   switch (pos)
7395     {
7396     case VFP_REG_Sd:
7397       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7398       break;
7399
7400     case VFP_REG_Sn:
7401       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7402       break;
7403
7404     case VFP_REG_Sm:
7405       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7406       break;
7407
7408     case VFP_REG_Dd:
7409       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7410       break;
7411
7412     case VFP_REG_Dn:
7413       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7414       break;
7415
7416     case VFP_REG_Dm:
7417       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7418       break;
7419
7420     default:
7421       abort ();
7422     }
7423 }
7424
7425 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7426    if any, is handled by md_apply_fix.   */
7427 static void
7428 encode_arm_shift (int i)
7429 {
7430   if (inst.operands[i].shift_kind == SHIFT_RRX)
7431     inst.instruction |= SHIFT_ROR << 5;
7432   else
7433     {
7434       inst.instruction |= inst.operands[i].shift_kind << 5;
7435       if (inst.operands[i].immisreg)
7436         {
7437           inst.instruction |= SHIFT_BY_REG;
7438           inst.instruction |= inst.operands[i].imm << 8;
7439         }
7440       else
7441         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7442     }
7443 }
7444
7445 static void
7446 encode_arm_shifter_operand (int i)
7447 {
7448   if (inst.operands[i].isreg)
7449     {
7450       inst.instruction |= inst.operands[i].reg;
7451       encode_arm_shift (i);
7452     }
7453   else
7454     {
7455       inst.instruction |= INST_IMMEDIATE;
7456       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7457         inst.instruction |= inst.operands[i].imm;
7458     }
7459 }
7460
7461 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7462 static void
7463 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7464 {
7465   /* PR 14260:
7466      Generate an error if the operand is not a register.  */
7467   constraint (!inst.operands[i].isreg,
7468               _("Instruction does not support =N addresses"));
7469
7470   inst.instruction |= inst.operands[i].reg << 16;
7471
7472   if (inst.operands[i].preind)
7473     {
7474       if (is_t)
7475         {
7476           inst.error = _("instruction does not accept preindexed addressing");
7477           return;
7478         }
7479       inst.instruction |= PRE_INDEX;
7480       if (inst.operands[i].writeback)
7481         inst.instruction |= WRITE_BACK;
7482
7483     }
7484   else if (inst.operands[i].postind)
7485     {
7486       gas_assert (inst.operands[i].writeback);
7487       if (is_t)
7488         inst.instruction |= WRITE_BACK;
7489     }
7490   else /* unindexed - only for coprocessor */
7491     {
7492       inst.error = _("instruction does not accept unindexed addressing");
7493       return;
7494     }
7495
7496   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7497       && (((inst.instruction & 0x000f0000) >> 16)
7498           == ((inst.instruction & 0x0000f000) >> 12)))
7499     as_warn ((inst.instruction & LOAD_BIT)
7500              ? _("destination register same as write-back base")
7501              : _("source register same as write-back base"));
7502 }
7503
7504 /* inst.operands[i] was set up by parse_address.  Encode it into an
7505    ARM-format mode 2 load or store instruction.  If is_t is true,
7506    reject forms that cannot be used with a T instruction (i.e. not
7507    post-indexed).  */
7508 static void
7509 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7510 {
7511   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7512
7513   encode_arm_addr_mode_common (i, is_t);
7514
7515   if (inst.operands[i].immisreg)
7516     {
7517       constraint ((inst.operands[i].imm == REG_PC
7518                    || (is_pc && inst.operands[i].writeback)),
7519                   BAD_PC_ADDRESSING);
7520       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7521       inst.instruction |= inst.operands[i].imm;
7522       if (!inst.operands[i].negative)
7523         inst.instruction |= INDEX_UP;
7524       if (inst.operands[i].shifted)
7525         {
7526           if (inst.operands[i].shift_kind == SHIFT_RRX)
7527             inst.instruction |= SHIFT_ROR << 5;
7528           else
7529             {
7530               inst.instruction |= inst.operands[i].shift_kind << 5;
7531               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7532             }
7533         }
7534     }
7535   else /* immediate offset in inst.reloc */
7536     {
7537       if (is_pc && !inst.reloc.pc_rel)
7538         {
7539           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7540
7541           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7542              cannot use PC in addressing.
7543              PC cannot be used in writeback addressing, either.  */
7544           constraint ((is_t || inst.operands[i].writeback),
7545                       BAD_PC_ADDRESSING);
7546
7547           /* Use of PC in str is deprecated for ARMv7.  */
7548           if (warn_on_deprecated
7549               && !is_load
7550               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7551             as_tsktsk (_("use of PC in this instruction is deprecated"));
7552         }
7553
7554       if (inst.reloc.type == BFD_RELOC_UNUSED)
7555         {
7556           /* Prefer + for zero encoded value.  */
7557           if (!inst.operands[i].negative)
7558             inst.instruction |= INDEX_UP;
7559           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7560         }
7561     }
7562 }
7563
7564 /* inst.operands[i] was set up by parse_address.  Encode it into an
7565    ARM-format mode 3 load or store instruction.  Reject forms that
7566    cannot be used with such instructions.  If is_t is true, reject
7567    forms that cannot be used with a T instruction (i.e. not
7568    post-indexed).  */
7569 static void
7570 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7571 {
7572   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7573     {
7574       inst.error = _("instruction does not accept scaled register index");
7575       return;
7576     }
7577
7578   encode_arm_addr_mode_common (i, is_t);
7579
7580   if (inst.operands[i].immisreg)
7581     {
7582       constraint ((inst.operands[i].imm == REG_PC
7583                    || (is_t && inst.operands[i].reg == REG_PC)),
7584                   BAD_PC_ADDRESSING);
7585       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7586                   BAD_PC_WRITEBACK);
7587       inst.instruction |= inst.operands[i].imm;
7588       if (!inst.operands[i].negative)
7589         inst.instruction |= INDEX_UP;
7590     }
7591   else /* immediate offset in inst.reloc */
7592     {
7593       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7594                    && inst.operands[i].writeback),
7595                   BAD_PC_WRITEBACK);
7596       inst.instruction |= HWOFFSET_IMM;
7597       if (inst.reloc.type == BFD_RELOC_UNUSED)
7598         {
7599           /* Prefer + for zero encoded value.  */
7600           if (!inst.operands[i].negative)
7601             inst.instruction |= INDEX_UP;
7602
7603           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7604         }
7605     }
7606 }
7607
7608 /* Write immediate bits [7:0] to the following locations:
7609
7610   |28/24|23     19|18 16|15                    4|3     0|
7611   |  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|
7612
7613   This function is used by VMOV/VMVN/VORR/VBIC.  */
7614
7615 static void
7616 neon_write_immbits (unsigned immbits)
7617 {
7618   inst.instruction |= immbits & 0xf;
7619   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7620   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7621 }
7622
7623 /* Invert low-order SIZE bits of XHI:XLO.  */
7624
7625 static void
7626 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7627 {
7628   unsigned immlo = xlo ? *xlo : 0;
7629   unsigned immhi = xhi ? *xhi : 0;
7630
7631   switch (size)
7632     {
7633     case 8:
7634       immlo = (~immlo) & 0xff;
7635       break;
7636
7637     case 16:
7638       immlo = (~immlo) & 0xffff;
7639       break;
7640
7641     case 64:
7642       immhi = (~immhi) & 0xffffffff;
7643       /* fall through.  */
7644
7645     case 32:
7646       immlo = (~immlo) & 0xffffffff;
7647       break;
7648
7649     default:
7650       abort ();
7651     }
7652
7653   if (xlo)
7654     *xlo = immlo;
7655
7656   if (xhi)
7657     *xhi = immhi;
7658 }
7659
7660 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7661    A, B, C, D.  */
7662
7663 static int
7664 neon_bits_same_in_bytes (unsigned imm)
7665 {
7666   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7667          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7668          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7669          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7670 }
7671
7672 /* For immediate of above form, return 0bABCD.  */
7673
7674 static unsigned
7675 neon_squash_bits (unsigned imm)
7676 {
7677   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7678          | ((imm & 0x01000000) >> 21);
7679 }
7680
7681 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7682
7683 static unsigned
7684 neon_qfloat_bits (unsigned imm)
7685 {
7686   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7687 }
7688
7689 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7690    the instruction. *OP is passed as the initial value of the op field, and
7691    may be set to a different value depending on the constant (i.e.
7692    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7693    MVN).  If the immediate looks like a repeated pattern then also
7694    try smaller element sizes.  */
7695
7696 static int
7697 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7698                          unsigned *immbits, int *op, int size,
7699                          enum neon_el_type type)
7700 {
7701   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7702      float.  */
7703   if (type == NT_float && !float_p)
7704     return FAIL;
7705
7706   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7707     {
7708       if (size != 32 || *op == 1)
7709         return FAIL;
7710       *immbits = neon_qfloat_bits (immlo);
7711       return 0xf;
7712     }
7713
7714   if (size == 64)
7715     {
7716       if (neon_bits_same_in_bytes (immhi)
7717           && neon_bits_same_in_bytes (immlo))
7718         {
7719           if (*op == 1)
7720             return FAIL;
7721           *immbits = (neon_squash_bits (immhi) << 4)
7722                      | neon_squash_bits (immlo);
7723           *op = 1;
7724           return 0xe;
7725         }
7726
7727       if (immhi != immlo)
7728         return FAIL;
7729     }
7730
7731   if (size >= 32)
7732     {
7733       if (immlo == (immlo & 0x000000ff))
7734         {
7735           *immbits = immlo;
7736           return 0x0;
7737         }
7738       else if (immlo == (immlo & 0x0000ff00))
7739         {
7740           *immbits = immlo >> 8;
7741           return 0x2;
7742         }
7743       else if (immlo == (immlo & 0x00ff0000))
7744         {
7745           *immbits = immlo >> 16;
7746           return 0x4;
7747         }
7748       else if (immlo == (immlo & 0xff000000))
7749         {
7750           *immbits = immlo >> 24;
7751           return 0x6;
7752         }
7753       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7754         {
7755           *immbits = (immlo >> 8) & 0xff;
7756           return 0xc;
7757         }
7758       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7759         {
7760           *immbits = (immlo >> 16) & 0xff;
7761           return 0xd;
7762         }
7763
7764       if ((immlo & 0xffff) != (immlo >> 16))
7765         return FAIL;
7766       immlo &= 0xffff;
7767     }
7768
7769   if (size >= 16)
7770     {
7771       if (immlo == (immlo & 0x000000ff))
7772         {
7773           *immbits = immlo;
7774           return 0x8;
7775         }
7776       else if (immlo == (immlo & 0x0000ff00))
7777         {
7778           *immbits = immlo >> 8;
7779           return 0xa;
7780         }
7781
7782       if ((immlo & 0xff) != (immlo >> 8))
7783         return FAIL;
7784       immlo &= 0xff;
7785     }
7786
7787   if (immlo == (immlo & 0x000000ff))
7788     {
7789       /* Don't allow MVN with 8-bit immediate.  */
7790       if (*op == 1)
7791         return FAIL;
7792       *immbits = immlo;
7793       return 0xe;
7794     }
7795
7796   return FAIL;
7797 }
7798
7799 #if defined BFD_HOST_64_BIT
7800 /* Returns TRUE if double precision value V may be cast
7801    to single precision without loss of accuracy.  */
7802
7803 static bfd_boolean
7804 is_double_a_single (bfd_int64_t v)
7805 {
7806   int exp = (int)((v >> 52) & 0x7FF);
7807   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7808
7809   return (exp == 0 || exp == 0x7FF
7810           || (exp >= 1023 - 126 && exp <= 1023 + 127))
7811     && (mantissa & 0x1FFFFFFFl) == 0;
7812 }
7813
7814 /* Returns a double precision value casted to single precision
7815    (ignoring the least significant bits in exponent and mantissa).  */
7816
7817 static int
7818 double_to_single (bfd_int64_t v)
7819 {
7820   int sign = (int) ((v >> 63) & 1l);
7821   int exp = (int) ((v >> 52) & 0x7FF);
7822   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7823
7824   if (exp == 0x7FF)
7825     exp = 0xFF;
7826   else
7827     {
7828       exp = exp - 1023 + 127;
7829       if (exp >= 0xFF)
7830         {
7831           /* Infinity.  */
7832           exp = 0x7F;
7833           mantissa = 0;
7834         }
7835       else if (exp < 0)
7836         {
7837           /* No denormalized numbers.  */
7838           exp = 0;
7839           mantissa = 0;
7840         }
7841     }
7842   mantissa >>= 29;
7843   return (sign << 31) | (exp << 23) | mantissa;
7844 }
7845 #endif /* BFD_HOST_64_BIT */
7846
7847 enum lit_type
7848 {
7849   CONST_THUMB,
7850   CONST_ARM,
7851   CONST_VEC
7852 };
7853
7854 static void do_vfp_nsyn_opcode (const char *);
7855
7856 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7857    Determine whether it can be performed with a move instruction; if
7858    it can, convert inst.instruction to that move instruction and
7859    return TRUE; if it can't, convert inst.instruction to a literal-pool
7860    load and return FALSE.  If this is not a valid thing to do in the
7861    current context, set inst.error and return TRUE.
7862
7863    inst.operands[i] describes the destination register.  */
7864
7865 static bfd_boolean
7866 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7867 {
7868   unsigned long tbit;
7869   bfd_boolean thumb_p = (t == CONST_THUMB);
7870   bfd_boolean arm_p   = (t == CONST_ARM);
7871
7872   if (thumb_p)
7873     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7874   else
7875     tbit = LOAD_BIT;
7876
7877   if ((inst.instruction & tbit) == 0)
7878     {
7879       inst.error = _("invalid pseudo operation");
7880       return TRUE;
7881     }
7882
7883   if (inst.reloc.exp.X_op != O_constant
7884       && inst.reloc.exp.X_op != O_symbol
7885       && inst.reloc.exp.X_op != O_big)
7886     {
7887       inst.error = _("constant expression expected");
7888       return TRUE;
7889     }
7890
7891   if (inst.reloc.exp.X_op == O_constant
7892       || inst.reloc.exp.X_op == O_big)
7893     {
7894 #if defined BFD_HOST_64_BIT
7895       bfd_int64_t v;
7896 #else
7897       offsetT v;
7898 #endif
7899       if (inst.reloc.exp.X_op == O_big)
7900         {
7901           LITTLENUM_TYPE w[X_PRECISION];
7902           LITTLENUM_TYPE * l;
7903
7904           if (inst.reloc.exp.X_add_number == -1)
7905             {
7906               gen_to_words (w, X_PRECISION, E_PRECISION);
7907               l = w;
7908               /* FIXME: Should we check words w[2..5] ?  */
7909             }
7910           else
7911             l = generic_bignum;
7912
7913 #if defined BFD_HOST_64_BIT
7914           v =
7915             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
7916                   << LITTLENUM_NUMBER_OF_BITS)
7917                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
7918                 << LITTLENUM_NUMBER_OF_BITS)
7919                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
7920               << LITTLENUM_NUMBER_OF_BITS)
7921              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
7922 #else
7923           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
7924             |  (l[0] & LITTLENUM_MASK);
7925 #endif
7926         }
7927       else
7928         v = inst.reloc.exp.X_add_number;
7929
7930       if (!inst.operands[i].issingle)
7931         {
7932           if (thumb_p)
7933             {
7934               /* This can be encoded only for a low register.  */
7935               if ((v & ~0xFF) == 0 && (inst.operands[i].reg < 8))
7936                 {
7937                   /* This can be done with a mov(1) instruction.  */
7938                   inst.instruction = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7939                   inst.instruction |= v;
7940                   return TRUE;
7941                 }
7942
7943               if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
7944                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
7945                 {
7946                   /* Check if on thumb2 it can be done with a mov.w, mvn or
7947                      movw instruction.  */
7948                   unsigned int newimm;
7949                   bfd_boolean isNegated;
7950
7951                   newimm = encode_thumb32_immediate (v);
7952                   if (newimm != (unsigned int) FAIL)
7953                     isNegated = FALSE;
7954                   else
7955                     {
7956                       newimm = encode_thumb32_immediate (~v);
7957                       if (newimm != (unsigned int) FAIL)
7958                         isNegated = TRUE;
7959                     }
7960
7961                   /* The number can be loaded with a mov.w or mvn
7962                      instruction.  */
7963                   if (newimm != (unsigned int) FAIL
7964                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
7965                     {
7966                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
7967                                           | (inst.operands[i].reg << 8));
7968                       /* Change to MOVN.  */
7969                       inst.instruction |= (isNegated ? 0x200000 : 0);
7970                       inst.instruction |= (newimm & 0x800) << 15;
7971                       inst.instruction |= (newimm & 0x700) << 4;
7972                       inst.instruction |= (newimm & 0x0ff);
7973                       return TRUE;
7974                     }
7975                   /* The number can be loaded with a movw instruction.  */
7976                   else if ((v & ~0xFFFF) == 0
7977                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
7978                     {
7979                       int imm = v & 0xFFFF;
7980
7981                       inst.instruction = 0xf2400000;  /* MOVW.  */
7982                       inst.instruction |= (inst.operands[i].reg << 8);
7983                       inst.instruction |= (imm & 0xf000) << 4;
7984                       inst.instruction |= (imm & 0x0800) << 15;
7985                       inst.instruction |= (imm & 0x0700) << 4;
7986                       inst.instruction |= (imm & 0x00ff);
7987                       return TRUE;
7988                     }
7989                 }
7990             }
7991           else if (arm_p)
7992             {
7993               int value = encode_arm_immediate (v);
7994
7995               if (value != FAIL)
7996                 {
7997                   /* This can be done with a mov instruction.  */
7998                   inst.instruction &= LITERAL_MASK;
7999                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8000                   inst.instruction |= value & 0xfff;
8001                   return TRUE;
8002                 }
8003
8004               value = encode_arm_immediate (~ v);
8005               if (value != FAIL)
8006                 {
8007                   /* This can be done with a mvn instruction.  */
8008                   inst.instruction &= LITERAL_MASK;
8009                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8010                   inst.instruction |= value & 0xfff;
8011                   return TRUE;
8012                 }
8013             }
8014           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8015             {
8016               int op = 0;
8017               unsigned immbits = 0;
8018               unsigned immlo = inst.operands[1].imm;
8019               unsigned immhi = inst.operands[1].regisimm
8020                 ? inst.operands[1].reg
8021                 : inst.reloc.exp.X_unsigned
8022                 ? 0
8023                 : ((bfd_int64_t)((int) immlo)) >> 32;
8024               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8025                                                    &op, 64, NT_invtype);
8026
8027               if (cmode == FAIL)
8028                 {
8029                   neon_invert_size (&immlo, &immhi, 64);
8030                   op = !op;
8031                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8032                                                    &op, 64, NT_invtype);
8033                 }
8034
8035               if (cmode != FAIL)
8036                 {
8037                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8038                     | (1 << 23)
8039                     | (cmode << 8)
8040                     | (op << 5)
8041                     | (1 << 4);
8042
8043                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8044                   if (thumb_mode)
8045                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8046                   else
8047                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8048                   neon_write_immbits (immbits);
8049                   return TRUE;
8050                 }
8051             }
8052         }
8053
8054       if (t == CONST_VEC)
8055         {
8056           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8057           if (inst.operands[i].issingle
8058               && is_quarter_float (inst.operands[1].imm)
8059               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8060             {
8061               inst.operands[1].imm =
8062                 neon_qfloat_bits (v);
8063               do_vfp_nsyn_opcode ("fconsts");
8064               return TRUE;
8065             }
8066
8067           /* If our host does not support a 64-bit type then we cannot perform
8068              the following optimization.  This mean that there will be a
8069              discrepancy between the output produced by an assembler built for
8070              a 32-bit-only host and the output produced from a 64-bit host, but
8071              this cannot be helped.  */
8072 #if defined BFD_HOST_64_BIT
8073           else if (!inst.operands[1].issingle
8074                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8075             {
8076               if (is_double_a_single (v)
8077                   && is_quarter_float (double_to_single (v)))
8078                 {
8079                   inst.operands[1].imm =
8080                     neon_qfloat_bits (double_to_single (v));
8081                   do_vfp_nsyn_opcode ("fconstd");
8082                   return TRUE;
8083                 }
8084             }
8085 #endif
8086         }
8087     }
8088
8089   if (add_to_lit_pool ((!inst.operands[i].isvec
8090                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8091     return TRUE;
8092
8093   inst.operands[1].reg = REG_PC;
8094   inst.operands[1].isreg = 1;
8095   inst.operands[1].preind = 1;
8096   inst.reloc.pc_rel = 1;
8097   inst.reloc.type = (thumb_p
8098                      ? BFD_RELOC_ARM_THUMB_OFFSET
8099                      : (mode_3
8100                         ? BFD_RELOC_ARM_HWLITERAL
8101                         : BFD_RELOC_ARM_LITERAL));
8102   return FALSE;
8103 }
8104
8105 /* inst.operands[i] was set up by parse_address.  Encode it into an
8106    ARM-format instruction.  Reject all forms which cannot be encoded
8107    into a coprocessor load/store instruction.  If wb_ok is false,
8108    reject use of writeback; if unind_ok is false, reject use of
8109    unindexed addressing.  If reloc_override is not 0, use it instead
8110    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8111    (in which case it is preserved).  */
8112
8113 static int
8114 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8115 {
8116   if (!inst.operands[i].isreg)
8117     {
8118       /* PR 18256 */
8119       if (! inst.operands[0].isvec)
8120         {
8121           inst.error = _("invalid co-processor operand");
8122           return FAIL;
8123         }
8124       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8125         return SUCCESS;
8126     }
8127
8128   inst.instruction |= inst.operands[i].reg << 16;
8129
8130   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8131
8132   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8133     {
8134       gas_assert (!inst.operands[i].writeback);
8135       if (!unind_ok)
8136         {
8137           inst.error = _("instruction does not support unindexed addressing");
8138           return FAIL;
8139         }
8140       inst.instruction |= inst.operands[i].imm;
8141       inst.instruction |= INDEX_UP;
8142       return SUCCESS;
8143     }
8144
8145   if (inst.operands[i].preind)
8146     inst.instruction |= PRE_INDEX;
8147
8148   if (inst.operands[i].writeback)
8149     {
8150       if (inst.operands[i].reg == REG_PC)
8151         {
8152           inst.error = _("pc may not be used with write-back");
8153           return FAIL;
8154         }
8155       if (!wb_ok)
8156         {
8157           inst.error = _("instruction does not support writeback");
8158           return FAIL;
8159         }
8160       inst.instruction |= WRITE_BACK;
8161     }
8162
8163   if (reloc_override)
8164     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
8165   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
8166             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
8167            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
8168     {
8169       if (thumb_mode)
8170         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8171       else
8172         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
8173     }
8174
8175   /* Prefer + for zero encoded value.  */
8176   if (!inst.operands[i].negative)
8177     inst.instruction |= INDEX_UP;
8178
8179   return SUCCESS;
8180 }
8181
8182 /* Functions for instruction encoding, sorted by sub-architecture.
8183    First some generics; their names are taken from the conventional
8184    bit positions for register arguments in ARM format instructions.  */
8185
8186 static void
8187 do_noargs (void)
8188 {
8189 }
8190
8191 static void
8192 do_rd (void)
8193 {
8194   inst.instruction |= inst.operands[0].reg << 12;
8195 }
8196
8197 static void
8198 do_rn (void)
8199 {
8200   inst.instruction |= inst.operands[0].reg << 16;
8201 }
8202
8203 static void
8204 do_rd_rm (void)
8205 {
8206   inst.instruction |= inst.operands[0].reg << 12;
8207   inst.instruction |= inst.operands[1].reg;
8208 }
8209
8210 static void
8211 do_rm_rn (void)
8212 {
8213   inst.instruction |= inst.operands[0].reg;
8214   inst.instruction |= inst.operands[1].reg << 16;
8215 }
8216
8217 static void
8218 do_rd_rn (void)
8219 {
8220   inst.instruction |= inst.operands[0].reg << 12;
8221   inst.instruction |= inst.operands[1].reg << 16;
8222 }
8223
8224 static void
8225 do_rn_rd (void)
8226 {
8227   inst.instruction |= inst.operands[0].reg << 16;
8228   inst.instruction |= inst.operands[1].reg << 12;
8229 }
8230
8231 static void
8232 do_tt (void)
8233 {
8234   inst.instruction |= inst.operands[0].reg << 8;
8235   inst.instruction |= inst.operands[1].reg << 16;
8236 }
8237
8238 static bfd_boolean
8239 check_obsolete (const arm_feature_set *feature, const char *msg)
8240 {
8241   if (ARM_CPU_IS_ANY (cpu_variant))
8242     {
8243       as_tsktsk ("%s", msg);
8244       return TRUE;
8245     }
8246   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8247     {
8248       as_bad ("%s", msg);
8249       return TRUE;
8250     }
8251
8252   return FALSE;
8253 }
8254
8255 static void
8256 do_rd_rm_rn (void)
8257 {
8258   unsigned Rn = inst.operands[2].reg;
8259   /* Enforce restrictions on SWP instruction.  */
8260   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8261     {
8262       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8263                   _("Rn must not overlap other operands"));
8264
8265       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8266        */
8267       if (!check_obsolete (&arm_ext_v8,
8268                            _("swp{b} use is obsoleted for ARMv8 and later"))
8269           && warn_on_deprecated
8270           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8271         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8272     }
8273
8274   inst.instruction |= inst.operands[0].reg << 12;
8275   inst.instruction |= inst.operands[1].reg;
8276   inst.instruction |= Rn << 16;
8277 }
8278
8279 static void
8280 do_rd_rn_rm (void)
8281 {
8282   inst.instruction |= inst.operands[0].reg << 12;
8283   inst.instruction |= inst.operands[1].reg << 16;
8284   inst.instruction |= inst.operands[2].reg;
8285 }
8286
8287 static void
8288 do_rm_rd_rn (void)
8289 {
8290   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8291   constraint (((inst.reloc.exp.X_op != O_constant
8292                 && inst.reloc.exp.X_op != O_illegal)
8293                || inst.reloc.exp.X_add_number != 0),
8294               BAD_ADDR_MODE);
8295   inst.instruction |= inst.operands[0].reg;
8296   inst.instruction |= inst.operands[1].reg << 12;
8297   inst.instruction |= inst.operands[2].reg << 16;
8298 }
8299
8300 static void
8301 do_imm0 (void)
8302 {
8303   inst.instruction |= inst.operands[0].imm;
8304 }
8305
8306 static void
8307 do_rd_cpaddr (void)
8308 {
8309   inst.instruction |= inst.operands[0].reg << 12;
8310   encode_arm_cp_address (1, TRUE, TRUE, 0);
8311 }
8312
8313 /* ARM instructions, in alphabetical order by function name (except
8314    that wrapper functions appear immediately after the function they
8315    wrap).  */
8316
8317 /* This is a pseudo-op of the form "adr rd, label" to be converted
8318    into a relative address of the form "add rd, pc, #label-.-8".  */
8319
8320 static void
8321 do_adr (void)
8322 {
8323   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8324
8325   /* Frag hacking will turn this into a sub instruction if the offset turns
8326      out to be negative.  */
8327   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8328   inst.reloc.pc_rel = 1;
8329   inst.reloc.exp.X_add_number -= 8;
8330 }
8331
8332 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8333    into a relative address of the form:
8334    add rd, pc, #low(label-.-8)"
8335    add rd, rd, #high(label-.-8)"  */
8336
8337 static void
8338 do_adrl (void)
8339 {
8340   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8341
8342   /* Frag hacking will turn this into a sub instruction if the offset turns
8343      out to be negative.  */
8344   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8345   inst.reloc.pc_rel            = 1;
8346   inst.size                    = INSN_SIZE * 2;
8347   inst.reloc.exp.X_add_number -= 8;
8348 }
8349
8350 static void
8351 do_arit (void)
8352 {
8353   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
8354               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
8355               THUMB1_RELOC_ONLY);
8356   if (!inst.operands[1].present)
8357     inst.operands[1].reg = inst.operands[0].reg;
8358   inst.instruction |= inst.operands[0].reg << 12;
8359   inst.instruction |= inst.operands[1].reg << 16;
8360   encode_arm_shifter_operand (2);
8361 }
8362
8363 static void
8364 do_barrier (void)
8365 {
8366   if (inst.operands[0].present)
8367     inst.instruction |= inst.operands[0].imm;
8368   else
8369     inst.instruction |= 0xf;
8370 }
8371
8372 static void
8373 do_bfc (void)
8374 {
8375   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8376   constraint (msb > 32, _("bit-field extends past end of register"));
8377   /* The instruction encoding stores the LSB and MSB,
8378      not the LSB and width.  */
8379   inst.instruction |= inst.operands[0].reg << 12;
8380   inst.instruction |= inst.operands[1].imm << 7;
8381   inst.instruction |= (msb - 1) << 16;
8382 }
8383
8384 static void
8385 do_bfi (void)
8386 {
8387   unsigned int msb;
8388
8389   /* #0 in second position is alternative syntax for bfc, which is
8390      the same instruction but with REG_PC in the Rm field.  */
8391   if (!inst.operands[1].isreg)
8392     inst.operands[1].reg = REG_PC;
8393
8394   msb = inst.operands[2].imm + inst.operands[3].imm;
8395   constraint (msb > 32, _("bit-field extends past end of register"));
8396   /* The instruction encoding stores the LSB and MSB,
8397      not the LSB and width.  */
8398   inst.instruction |= inst.operands[0].reg << 12;
8399   inst.instruction |= inst.operands[1].reg;
8400   inst.instruction |= inst.operands[2].imm << 7;
8401   inst.instruction |= (msb - 1) << 16;
8402 }
8403
8404 static void
8405 do_bfx (void)
8406 {
8407   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8408               _("bit-field extends past end of register"));
8409   inst.instruction |= inst.operands[0].reg << 12;
8410   inst.instruction |= inst.operands[1].reg;
8411   inst.instruction |= inst.operands[2].imm << 7;
8412   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8413 }
8414
8415 /* ARM V5 breakpoint instruction (argument parse)
8416      BKPT <16 bit unsigned immediate>
8417      Instruction is not conditional.
8418         The bit pattern given in insns[] has the COND_ALWAYS condition,
8419         and it is an error if the caller tried to override that.  */
8420
8421 static void
8422 do_bkpt (void)
8423 {
8424   /* Top 12 of 16 bits to bits 19:8.  */
8425   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8426
8427   /* Bottom 4 of 16 bits to bits 3:0.  */
8428   inst.instruction |= inst.operands[0].imm & 0xf;
8429 }
8430
8431 static void
8432 encode_branch (int default_reloc)
8433 {
8434   if (inst.operands[0].hasreloc)
8435     {
8436       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8437                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8438                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8439       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8440         ? BFD_RELOC_ARM_PLT32
8441         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8442     }
8443   else
8444     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8445   inst.reloc.pc_rel = 1;
8446 }
8447
8448 static void
8449 do_branch (void)
8450 {
8451 #ifdef OBJ_ELF
8452   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8453     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8454   else
8455 #endif
8456     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8457 }
8458
8459 static void
8460 do_bl (void)
8461 {
8462 #ifdef OBJ_ELF
8463   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8464     {
8465       if (inst.cond == COND_ALWAYS)
8466         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8467       else
8468         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8469     }
8470   else
8471 #endif
8472     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8473 }
8474
8475 /* ARM V5 branch-link-exchange instruction (argument parse)
8476      BLX <target_addr>          ie BLX(1)
8477      BLX{<condition>} <Rm>      ie BLX(2)
8478    Unfortunately, there are two different opcodes for this mnemonic.
8479    So, the insns[].value is not used, and the code here zaps values
8480         into inst.instruction.
8481    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8482
8483 static void
8484 do_blx (void)
8485 {
8486   if (inst.operands[0].isreg)
8487     {
8488       /* Arg is a register; the opcode provided by insns[] is correct.
8489          It is not illegal to do "blx pc", just useless.  */
8490       if (inst.operands[0].reg == REG_PC)
8491         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8492
8493       inst.instruction |= inst.operands[0].reg;
8494     }
8495   else
8496     {
8497       /* Arg is an address; this instruction cannot be executed
8498          conditionally, and the opcode must be adjusted.
8499          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8500          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8501       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8502       inst.instruction = 0xfa000000;
8503       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8504     }
8505 }
8506
8507 static void
8508 do_bx (void)
8509 {
8510   bfd_boolean want_reloc;
8511
8512   if (inst.operands[0].reg == REG_PC)
8513     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8514
8515   inst.instruction |= inst.operands[0].reg;
8516   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8517      it is for ARMv4t or earlier.  */
8518   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8519   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
8520       want_reloc = TRUE;
8521
8522 #ifdef OBJ_ELF
8523   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8524 #endif
8525     want_reloc = FALSE;
8526
8527   if (want_reloc)
8528     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8529 }
8530
8531
8532 /* ARM v5TEJ.  Jump to Jazelle code.  */
8533
8534 static void
8535 do_bxj (void)
8536 {
8537   if (inst.operands[0].reg == REG_PC)
8538     as_tsktsk (_("use of r15 in bxj is not really useful"));
8539
8540   inst.instruction |= inst.operands[0].reg;
8541 }
8542
8543 /* Co-processor data operation:
8544       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8545       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8546 static void
8547 do_cdp (void)
8548 {
8549   inst.instruction |= inst.operands[0].reg << 8;
8550   inst.instruction |= inst.operands[1].imm << 20;
8551   inst.instruction |= inst.operands[2].reg << 12;
8552   inst.instruction |= inst.operands[3].reg << 16;
8553   inst.instruction |= inst.operands[4].reg;
8554   inst.instruction |= inst.operands[5].imm << 5;
8555 }
8556
8557 static void
8558 do_cmp (void)
8559 {
8560   inst.instruction |= inst.operands[0].reg << 16;
8561   encode_arm_shifter_operand (1);
8562 }
8563
8564 /* Transfer between coprocessor and ARM registers.
8565    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8566    MRC2
8567    MCR{cond}
8568    MCR2
8569
8570    No special properties.  */
8571
8572 struct deprecated_coproc_regs_s
8573 {
8574   unsigned cp;
8575   int opc1;
8576   unsigned crn;
8577   unsigned crm;
8578   int opc2;
8579   arm_feature_set deprecated;
8580   arm_feature_set obsoleted;
8581   const char *dep_msg;
8582   const char *obs_msg;
8583 };
8584
8585 #define DEPR_ACCESS_V8 \
8586   N_("This coprocessor register access is deprecated in ARMv8")
8587
8588 /* Table of all deprecated coprocessor registers.  */
8589 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8590 {
8591     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8592      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8593      DEPR_ACCESS_V8, NULL},
8594     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8595      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8596      DEPR_ACCESS_V8, NULL},
8597     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8598      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8599      DEPR_ACCESS_V8, NULL},
8600     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8601      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8602      DEPR_ACCESS_V8, NULL},
8603     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8604      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8605      DEPR_ACCESS_V8, NULL},
8606 };
8607
8608 #undef DEPR_ACCESS_V8
8609
8610 static const size_t deprecated_coproc_reg_count =
8611   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8612
8613 static void
8614 do_co_reg (void)
8615 {
8616   unsigned Rd;
8617   size_t i;
8618
8619   Rd = inst.operands[2].reg;
8620   if (thumb_mode)
8621     {
8622       if (inst.instruction == 0xee000010
8623           || inst.instruction == 0xfe000010)
8624         /* MCR, MCR2  */
8625         reject_bad_reg (Rd);
8626       else
8627         /* MRC, MRC2  */
8628         constraint (Rd == REG_SP, BAD_SP);
8629     }
8630   else
8631     {
8632       /* MCR */
8633       if (inst.instruction == 0xe000010)
8634         constraint (Rd == REG_PC, BAD_PC);
8635     }
8636
8637     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8638       {
8639         const struct deprecated_coproc_regs_s *r =
8640           deprecated_coproc_regs + i;
8641
8642         if (inst.operands[0].reg == r->cp
8643             && inst.operands[1].imm == r->opc1
8644             && inst.operands[3].reg == r->crn
8645             && inst.operands[4].reg == r->crm
8646             && inst.operands[5].imm == r->opc2)
8647           {
8648             if (! ARM_CPU_IS_ANY (cpu_variant)
8649                 && warn_on_deprecated
8650                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8651               as_tsktsk ("%s", r->dep_msg);
8652           }
8653       }
8654
8655   inst.instruction |= inst.operands[0].reg << 8;
8656   inst.instruction |= inst.operands[1].imm << 21;
8657   inst.instruction |= Rd << 12;
8658   inst.instruction |= inst.operands[3].reg << 16;
8659   inst.instruction |= inst.operands[4].reg;
8660   inst.instruction |= inst.operands[5].imm << 5;
8661 }
8662
8663 /* Transfer between coprocessor register and pair of ARM registers.
8664    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8665    MCRR2
8666    MRRC{cond}
8667    MRRC2
8668
8669    Two XScale instructions are special cases of these:
8670
8671      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8672      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8673
8674    Result unpredictable if Rd or Rn is R15.  */
8675
8676 static void
8677 do_co_reg2c (void)
8678 {
8679   unsigned Rd, Rn;
8680
8681   Rd = inst.operands[2].reg;
8682   Rn = inst.operands[3].reg;
8683
8684   if (thumb_mode)
8685     {
8686       reject_bad_reg (Rd);
8687       reject_bad_reg (Rn);
8688     }
8689   else
8690     {
8691       constraint (Rd == REG_PC, BAD_PC);
8692       constraint (Rn == REG_PC, BAD_PC);
8693     }
8694
8695   /* Only check the MRRC{2} variants.  */
8696   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
8697     {
8698        /* If Rd == Rn, error that the operation is
8699           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
8700        constraint (Rd == Rn, BAD_OVERLAP);
8701     }
8702
8703   inst.instruction |= inst.operands[0].reg << 8;
8704   inst.instruction |= inst.operands[1].imm << 4;
8705   inst.instruction |= Rd << 12;
8706   inst.instruction |= Rn << 16;
8707   inst.instruction |= inst.operands[4].reg;
8708 }
8709
8710 static void
8711 do_cpsi (void)
8712 {
8713   inst.instruction |= inst.operands[0].imm << 6;
8714   if (inst.operands[1].present)
8715     {
8716       inst.instruction |= CPSI_MMOD;
8717       inst.instruction |= inst.operands[1].imm;
8718     }
8719 }
8720
8721 static void
8722 do_dbg (void)
8723 {
8724   inst.instruction |= inst.operands[0].imm;
8725 }
8726
8727 static void
8728 do_div (void)
8729 {
8730   unsigned Rd, Rn, Rm;
8731
8732   Rd = inst.operands[0].reg;
8733   Rn = (inst.operands[1].present
8734         ? inst.operands[1].reg : Rd);
8735   Rm = inst.operands[2].reg;
8736
8737   constraint ((Rd == REG_PC), BAD_PC);
8738   constraint ((Rn == REG_PC), BAD_PC);
8739   constraint ((Rm == REG_PC), BAD_PC);
8740
8741   inst.instruction |= Rd << 16;
8742   inst.instruction |= Rn << 0;
8743   inst.instruction |= Rm << 8;
8744 }
8745
8746 static void
8747 do_it (void)
8748 {
8749   /* There is no IT instruction in ARM mode.  We
8750      process it to do the validation as if in
8751      thumb mode, just in case the code gets
8752      assembled for thumb using the unified syntax.  */
8753
8754   inst.size = 0;
8755   if (unified_syntax)
8756     {
8757       set_it_insn_type (IT_INSN);
8758       now_it.mask = (inst.instruction & 0xf) | 0x10;
8759       now_it.cc = inst.operands[0].imm;
8760     }
8761 }
8762
8763 /* If there is only one register in the register list,
8764    then return its register number.  Otherwise return -1.  */
8765 static int
8766 only_one_reg_in_list (int range)
8767 {
8768   int i = ffs (range) - 1;
8769   return (i > 15 || range != (1 << i)) ? -1 : i;
8770 }
8771
8772 static void
8773 encode_ldmstm(int from_push_pop_mnem)
8774 {
8775   int base_reg = inst.operands[0].reg;
8776   int range = inst.operands[1].imm;
8777   int one_reg;
8778
8779   inst.instruction |= base_reg << 16;
8780   inst.instruction |= range;
8781
8782   if (inst.operands[1].writeback)
8783     inst.instruction |= LDM_TYPE_2_OR_3;
8784
8785   if (inst.operands[0].writeback)
8786     {
8787       inst.instruction |= WRITE_BACK;
8788       /* Check for unpredictable uses of writeback.  */
8789       if (inst.instruction & LOAD_BIT)
8790         {
8791           /* Not allowed in LDM type 2.  */
8792           if ((inst.instruction & LDM_TYPE_2_OR_3)
8793               && ((range & (1 << REG_PC)) == 0))
8794             as_warn (_("writeback of base register is UNPREDICTABLE"));
8795           /* Only allowed if base reg not in list for other types.  */
8796           else if (range & (1 << base_reg))
8797             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8798         }
8799       else /* STM.  */
8800         {
8801           /* Not allowed for type 2.  */
8802           if (inst.instruction & LDM_TYPE_2_OR_3)
8803             as_warn (_("writeback of base register is UNPREDICTABLE"));
8804           /* Only allowed if base reg not in list, or first in list.  */
8805           else if ((range & (1 << base_reg))
8806                    && (range & ((1 << base_reg) - 1)))
8807             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8808         }
8809     }
8810
8811   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8812   one_reg = only_one_reg_in_list (range);
8813   if (from_push_pop_mnem && one_reg >= 0)
8814     {
8815       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8816
8817       inst.instruction &= A_COND_MASK;
8818       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8819       inst.instruction |= one_reg << 12;
8820     }
8821 }
8822
8823 static void
8824 do_ldmstm (void)
8825 {
8826   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8827 }
8828
8829 /* ARMv5TE load-consecutive (argument parse)
8830    Mode is like LDRH.
8831
8832      LDRccD R, mode
8833      STRccD R, mode.  */
8834
8835 static void
8836 do_ldrd (void)
8837 {
8838   constraint (inst.operands[0].reg % 2 != 0,
8839               _("first transfer register must be even"));
8840   constraint (inst.operands[1].present
8841               && inst.operands[1].reg != inst.operands[0].reg + 1,
8842               _("can only transfer two consecutive registers"));
8843   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8844   constraint (!inst.operands[2].isreg, _("'[' expected"));
8845
8846   if (!inst.operands[1].present)
8847     inst.operands[1].reg = inst.operands[0].reg + 1;
8848
8849   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8850      register and the first register written; we have to diagnose
8851      overlap between the base and the second register written here.  */
8852
8853   if (inst.operands[2].reg == inst.operands[1].reg
8854       && (inst.operands[2].writeback || inst.operands[2].postind))
8855     as_warn (_("base register written back, and overlaps "
8856                "second transfer register"));
8857
8858   if (!(inst.instruction & V4_STR_BIT))
8859     {
8860       /* For an index-register load, the index register must not overlap the
8861         destination (even if not write-back).  */
8862       if (inst.operands[2].immisreg
8863               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8864               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8865         as_warn (_("index register overlaps transfer register"));
8866     }
8867   inst.instruction |= inst.operands[0].reg << 12;
8868   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8869 }
8870
8871 static void
8872 do_ldrex (void)
8873 {
8874   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8875               || inst.operands[1].postind || inst.operands[1].writeback
8876               || inst.operands[1].immisreg || inst.operands[1].shifted
8877               || inst.operands[1].negative
8878               /* This can arise if the programmer has written
8879                    strex rN, rM, foo
8880                  or if they have mistakenly used a register name as the last
8881                  operand,  eg:
8882                    strex rN, rM, rX
8883                  It is very difficult to distinguish between these two cases
8884                  because "rX" might actually be a label. ie the register
8885                  name has been occluded by a symbol of the same name. So we
8886                  just generate a general 'bad addressing mode' type error
8887                  message and leave it up to the programmer to discover the
8888                  true cause and fix their mistake.  */
8889               || (inst.operands[1].reg == REG_PC),
8890               BAD_ADDR_MODE);
8891
8892   constraint (inst.reloc.exp.X_op != O_constant
8893               || inst.reloc.exp.X_add_number != 0,
8894               _("offset must be zero in ARM encoding"));
8895
8896   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8897
8898   inst.instruction |= inst.operands[0].reg << 12;
8899   inst.instruction |= inst.operands[1].reg << 16;
8900   inst.reloc.type = BFD_RELOC_UNUSED;
8901 }
8902
8903 static void
8904 do_ldrexd (void)
8905 {
8906   constraint (inst.operands[0].reg % 2 != 0,
8907               _("even register required"));
8908   constraint (inst.operands[1].present
8909               && inst.operands[1].reg != inst.operands[0].reg + 1,
8910               _("can only load two consecutive registers"));
8911   /* If op 1 were present and equal to PC, this function wouldn't
8912      have been called in the first place.  */
8913   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8914
8915   inst.instruction |= inst.operands[0].reg << 12;
8916   inst.instruction |= inst.operands[2].reg << 16;
8917 }
8918
8919 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
8920    which is not a multiple of four is UNPREDICTABLE.  */
8921 static void
8922 check_ldr_r15_aligned (void)
8923 {
8924   constraint (!(inst.operands[1].immisreg)
8925               && (inst.operands[0].reg == REG_PC
8926               && inst.operands[1].reg == REG_PC
8927               && (inst.reloc.exp.X_add_number & 0x3)),
8928               _("ldr to register 15 must be 4-byte alligned"));
8929 }
8930
8931 static void
8932 do_ldst (void)
8933 {
8934   inst.instruction |= inst.operands[0].reg << 12;
8935   if (!inst.operands[1].isreg)
8936     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
8937       return;
8938   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
8939   check_ldr_r15_aligned ();
8940 }
8941
8942 static void
8943 do_ldstt (void)
8944 {
8945   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8946      reject [Rn,...].  */
8947   if (inst.operands[1].preind)
8948     {
8949       constraint (inst.reloc.exp.X_op != O_constant
8950                   || inst.reloc.exp.X_add_number != 0,
8951                   _("this instruction requires a post-indexed address"));
8952
8953       inst.operands[1].preind = 0;
8954       inst.operands[1].postind = 1;
8955       inst.operands[1].writeback = 1;
8956     }
8957   inst.instruction |= inst.operands[0].reg << 12;
8958   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8959 }
8960
8961 /* Halfword and signed-byte load/store operations.  */
8962
8963 static void
8964 do_ldstv4 (void)
8965 {
8966   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8967   inst.instruction |= inst.operands[0].reg << 12;
8968   if (!inst.operands[1].isreg)
8969     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
8970       return;
8971   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8972 }
8973
8974 static void
8975 do_ldsttv4 (void)
8976 {
8977   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8978      reject [Rn,...].  */
8979   if (inst.operands[1].preind)
8980     {
8981       constraint (inst.reloc.exp.X_op != O_constant
8982                   || inst.reloc.exp.X_add_number != 0,
8983                   _("this instruction requires a post-indexed address"));
8984
8985       inst.operands[1].preind = 0;
8986       inst.operands[1].postind = 1;
8987       inst.operands[1].writeback = 1;
8988     }
8989   inst.instruction |= inst.operands[0].reg << 12;
8990   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
8991 }
8992
8993 /* Co-processor register load/store.
8994    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
8995 static void
8996 do_lstc (void)
8997 {
8998   inst.instruction |= inst.operands[0].reg << 8;
8999   inst.instruction |= inst.operands[1].reg << 12;
9000   encode_arm_cp_address (2, TRUE, TRUE, 0);
9001 }
9002
9003 static void
9004 do_mlas (void)
9005 {
9006   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9007   if (inst.operands[0].reg == inst.operands[1].reg
9008       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9009       && !(inst.instruction & 0x00400000))
9010     as_tsktsk (_("Rd and Rm should be different in mla"));
9011
9012   inst.instruction |= inst.operands[0].reg << 16;
9013   inst.instruction |= inst.operands[1].reg;
9014   inst.instruction |= inst.operands[2].reg << 8;
9015   inst.instruction |= inst.operands[3].reg << 12;
9016 }
9017
9018 static void
9019 do_mov (void)
9020 {
9021   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9022               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9023               THUMB1_RELOC_ONLY);
9024   inst.instruction |= inst.operands[0].reg << 12;
9025   encode_arm_shifter_operand (1);
9026 }
9027
9028 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9029 static void
9030 do_mov16 (void)
9031 {
9032   bfd_vma imm;
9033   bfd_boolean top;
9034
9035   top = (inst.instruction & 0x00400000) != 0;
9036   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
9037               _(":lower16: not allowed this instruction"));
9038   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
9039               _(":upper16: not allowed instruction"));
9040   inst.instruction |= inst.operands[0].reg << 12;
9041   if (inst.reloc.type == BFD_RELOC_UNUSED)
9042     {
9043       imm = inst.reloc.exp.X_add_number;
9044       /* The value is in two pieces: 0:11, 16:19.  */
9045       inst.instruction |= (imm & 0x00000fff);
9046       inst.instruction |= (imm & 0x0000f000) << 4;
9047     }
9048 }
9049
9050 static int
9051 do_vfp_nsyn_mrs (void)
9052 {
9053   if (inst.operands[0].isvec)
9054     {
9055       if (inst.operands[1].reg != 1)
9056         first_error (_("operand 1 must be FPSCR"));
9057       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9058       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9059       do_vfp_nsyn_opcode ("fmstat");
9060     }
9061   else if (inst.operands[1].isvec)
9062     do_vfp_nsyn_opcode ("fmrx");
9063   else
9064     return FAIL;
9065
9066   return SUCCESS;
9067 }
9068
9069 static int
9070 do_vfp_nsyn_msr (void)
9071 {
9072   if (inst.operands[0].isvec)
9073     do_vfp_nsyn_opcode ("fmxr");
9074   else
9075     return FAIL;
9076
9077   return SUCCESS;
9078 }
9079
9080 static void
9081 do_vmrs (void)
9082 {
9083   unsigned Rt = inst.operands[0].reg;
9084
9085   if (thumb_mode && Rt == REG_SP)
9086     {
9087       inst.error = BAD_SP;
9088       return;
9089     }
9090
9091   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9092   if (!inst.operands[0].isvec && Rt == REG_PC)
9093     {
9094       inst.error = BAD_PC;
9095       return;
9096     }
9097
9098   /* If we get through parsing the register name, we just insert the number
9099      generated into the instruction without further validation.  */
9100   inst.instruction |= (inst.operands[1].reg << 16);
9101   inst.instruction |= (Rt << 12);
9102 }
9103
9104 static void
9105 do_vmsr (void)
9106 {
9107   unsigned Rt = inst.operands[1].reg;
9108
9109   if (thumb_mode)
9110     reject_bad_reg (Rt);
9111   else if (Rt == REG_PC)
9112     {
9113       inst.error = BAD_PC;
9114       return;
9115     }
9116
9117   /* If we get through parsing the register name, we just insert the number
9118      generated into the instruction without further validation.  */
9119   inst.instruction |= (inst.operands[0].reg << 16);
9120   inst.instruction |= (Rt << 12);
9121 }
9122
9123 static void
9124 do_mrs (void)
9125 {
9126   unsigned br;
9127
9128   if (do_vfp_nsyn_mrs () == SUCCESS)
9129     return;
9130
9131   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9132   inst.instruction |= inst.operands[0].reg << 12;
9133
9134   if (inst.operands[1].isreg)
9135     {
9136       br = inst.operands[1].reg;
9137       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
9138         as_bad (_("bad register for mrs"));
9139     }
9140   else
9141     {
9142       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9143       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9144                   != (PSR_c|PSR_f),
9145                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9146       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9147     }
9148
9149   inst.instruction |= br;
9150 }
9151
9152 /* Two possible forms:
9153       "{C|S}PSR_<field>, Rm",
9154       "{C|S}PSR_f, #expression".  */
9155
9156 static void
9157 do_msr (void)
9158 {
9159   if (do_vfp_nsyn_msr () == SUCCESS)
9160     return;
9161
9162   inst.instruction |= inst.operands[0].imm;
9163   if (inst.operands[1].isreg)
9164     inst.instruction |= inst.operands[1].reg;
9165   else
9166     {
9167       inst.instruction |= INST_IMMEDIATE;
9168       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
9169       inst.reloc.pc_rel = 0;
9170     }
9171 }
9172
9173 static void
9174 do_mul (void)
9175 {
9176   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9177
9178   if (!inst.operands[2].present)
9179     inst.operands[2].reg = inst.operands[0].reg;
9180   inst.instruction |= inst.operands[0].reg << 16;
9181   inst.instruction |= inst.operands[1].reg;
9182   inst.instruction |= inst.operands[2].reg << 8;
9183
9184   if (inst.operands[0].reg == inst.operands[1].reg
9185       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9186     as_tsktsk (_("Rd and Rm should be different in mul"));
9187 }
9188
9189 /* Long Multiply Parser
9190    UMULL RdLo, RdHi, Rm, Rs
9191    SMULL RdLo, RdHi, Rm, Rs
9192    UMLAL RdLo, RdHi, Rm, Rs
9193    SMLAL RdLo, RdHi, Rm, Rs.  */
9194
9195 static void
9196 do_mull (void)
9197 {
9198   inst.instruction |= inst.operands[0].reg << 12;
9199   inst.instruction |= inst.operands[1].reg << 16;
9200   inst.instruction |= inst.operands[2].reg;
9201   inst.instruction |= inst.operands[3].reg << 8;
9202
9203   /* rdhi and rdlo must be different.  */
9204   if (inst.operands[0].reg == inst.operands[1].reg)
9205     as_tsktsk (_("rdhi and rdlo must be different"));
9206
9207   /* rdhi, rdlo and rm must all be different before armv6.  */
9208   if ((inst.operands[0].reg == inst.operands[2].reg
9209       || inst.operands[1].reg == inst.operands[2].reg)
9210       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9211     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9212 }
9213
9214 static void
9215 do_nop (void)
9216 {
9217   if (inst.operands[0].present
9218       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9219     {
9220       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9221       inst.instruction &= 0xf0000000;
9222       inst.instruction |= 0x0320f000;
9223       if (inst.operands[0].present)
9224         inst.instruction |= inst.operands[0].imm;
9225     }
9226 }
9227
9228 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9229    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9230    Condition defaults to COND_ALWAYS.
9231    Error if Rd, Rn or Rm are R15.  */
9232
9233 static void
9234 do_pkhbt (void)
9235 {
9236   inst.instruction |= inst.operands[0].reg << 12;
9237   inst.instruction |= inst.operands[1].reg << 16;
9238   inst.instruction |= inst.operands[2].reg;
9239   if (inst.operands[3].present)
9240     encode_arm_shift (3);
9241 }
9242
9243 /* ARM V6 PKHTB (Argument Parse).  */
9244
9245 static void
9246 do_pkhtb (void)
9247 {
9248   if (!inst.operands[3].present)
9249     {
9250       /* If the shift specifier is omitted, turn the instruction
9251          into pkhbt rd, rm, rn. */
9252       inst.instruction &= 0xfff00010;
9253       inst.instruction |= inst.operands[0].reg << 12;
9254       inst.instruction |= inst.operands[1].reg;
9255       inst.instruction |= inst.operands[2].reg << 16;
9256     }
9257   else
9258     {
9259       inst.instruction |= inst.operands[0].reg << 12;
9260       inst.instruction |= inst.operands[1].reg << 16;
9261       inst.instruction |= inst.operands[2].reg;
9262       encode_arm_shift (3);
9263     }
9264 }
9265
9266 /* ARMv5TE: Preload-Cache
9267    MP Extensions: Preload for write
9268
9269     PLD(W) <addr_mode>
9270
9271   Syntactically, like LDR with B=1, W=0, L=1.  */
9272
9273 static void
9274 do_pld (void)
9275 {
9276   constraint (!inst.operands[0].isreg,
9277               _("'[' expected after PLD mnemonic"));
9278   constraint (inst.operands[0].postind,
9279               _("post-indexed expression used in preload instruction"));
9280   constraint (inst.operands[0].writeback,
9281               _("writeback used in preload instruction"));
9282   constraint (!inst.operands[0].preind,
9283               _("unindexed addressing used in preload instruction"));
9284   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9285 }
9286
9287 /* ARMv7: PLI <addr_mode>  */
9288 static void
9289 do_pli (void)
9290 {
9291   constraint (!inst.operands[0].isreg,
9292               _("'[' expected after PLI mnemonic"));
9293   constraint (inst.operands[0].postind,
9294               _("post-indexed expression used in preload instruction"));
9295   constraint (inst.operands[0].writeback,
9296               _("writeback used in preload instruction"));
9297   constraint (!inst.operands[0].preind,
9298               _("unindexed addressing used in preload instruction"));
9299   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9300   inst.instruction &= ~PRE_INDEX;
9301 }
9302
9303 static void
9304 do_push_pop (void)
9305 {
9306   constraint (inst.operands[0].writeback,
9307               _("push/pop do not support {reglist}^"));
9308   inst.operands[1] = inst.operands[0];
9309   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9310   inst.operands[0].isreg = 1;
9311   inst.operands[0].writeback = 1;
9312   inst.operands[0].reg = REG_SP;
9313   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9314 }
9315
9316 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9317    word at the specified address and the following word
9318    respectively.
9319    Unconditionally executed.
9320    Error if Rn is R15.  */
9321
9322 static void
9323 do_rfe (void)
9324 {
9325   inst.instruction |= inst.operands[0].reg << 16;
9326   if (inst.operands[0].writeback)
9327     inst.instruction |= WRITE_BACK;
9328 }
9329
9330 /* ARM V6 ssat (argument parse).  */
9331
9332 static void
9333 do_ssat (void)
9334 {
9335   inst.instruction |= inst.operands[0].reg << 12;
9336   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9337   inst.instruction |= inst.operands[2].reg;
9338
9339   if (inst.operands[3].present)
9340     encode_arm_shift (3);
9341 }
9342
9343 /* ARM V6 usat (argument parse).  */
9344
9345 static void
9346 do_usat (void)
9347 {
9348   inst.instruction |= inst.operands[0].reg << 12;
9349   inst.instruction |= inst.operands[1].imm << 16;
9350   inst.instruction |= inst.operands[2].reg;
9351
9352   if (inst.operands[3].present)
9353     encode_arm_shift (3);
9354 }
9355
9356 /* ARM V6 ssat16 (argument parse).  */
9357
9358 static void
9359 do_ssat16 (void)
9360 {
9361   inst.instruction |= inst.operands[0].reg << 12;
9362   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9363   inst.instruction |= inst.operands[2].reg;
9364 }
9365
9366 static void
9367 do_usat16 (void)
9368 {
9369   inst.instruction |= inst.operands[0].reg << 12;
9370   inst.instruction |= inst.operands[1].imm << 16;
9371   inst.instruction |= inst.operands[2].reg;
9372 }
9373
9374 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9375    preserving the other bits.
9376
9377    setend <endian_specifier>, where <endian_specifier> is either
9378    BE or LE.  */
9379
9380 static void
9381 do_setend (void)
9382 {
9383   if (warn_on_deprecated
9384       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9385       as_tsktsk (_("setend use is deprecated for ARMv8"));
9386
9387   if (inst.operands[0].imm)
9388     inst.instruction |= 0x200;
9389 }
9390
9391 static void
9392 do_shift (void)
9393 {
9394   unsigned int Rm = (inst.operands[1].present
9395                      ? inst.operands[1].reg
9396                      : inst.operands[0].reg);
9397
9398   inst.instruction |= inst.operands[0].reg << 12;
9399   inst.instruction |= Rm;
9400   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9401     {
9402       inst.instruction |= inst.operands[2].reg << 8;
9403       inst.instruction |= SHIFT_BY_REG;
9404       /* PR 12854: Error on extraneous shifts.  */
9405       constraint (inst.operands[2].shifted,
9406                   _("extraneous shift as part of operand to shift insn"));
9407     }
9408   else
9409     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9410 }
9411
9412 static void
9413 do_smc (void)
9414 {
9415   inst.reloc.type = BFD_RELOC_ARM_SMC;
9416   inst.reloc.pc_rel = 0;
9417 }
9418
9419 static void
9420 do_hvc (void)
9421 {
9422   inst.reloc.type = BFD_RELOC_ARM_HVC;
9423   inst.reloc.pc_rel = 0;
9424 }
9425
9426 static void
9427 do_swi (void)
9428 {
9429   inst.reloc.type = BFD_RELOC_ARM_SWI;
9430   inst.reloc.pc_rel = 0;
9431 }
9432
9433 static void
9434 do_setpan (void)
9435 {
9436   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9437               _("selected processor does not support SETPAN instruction"));
9438
9439   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9440 }
9441
9442 static void
9443 do_t_setpan (void)
9444 {
9445   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9446               _("selected processor does not support SETPAN instruction"));
9447
9448   inst.instruction |= (inst.operands[0].imm << 3);
9449 }
9450
9451 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9452    SMLAxy{cond} Rd,Rm,Rs,Rn
9453    SMLAWy{cond} Rd,Rm,Rs,Rn
9454    Error if any register is R15.  */
9455
9456 static void
9457 do_smla (void)
9458 {
9459   inst.instruction |= inst.operands[0].reg << 16;
9460   inst.instruction |= inst.operands[1].reg;
9461   inst.instruction |= inst.operands[2].reg << 8;
9462   inst.instruction |= inst.operands[3].reg << 12;
9463 }
9464
9465 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9466    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9467    Error if any register is R15.
9468    Warning if Rdlo == Rdhi.  */
9469
9470 static void
9471 do_smlal (void)
9472 {
9473   inst.instruction |= inst.operands[0].reg << 12;
9474   inst.instruction |= inst.operands[1].reg << 16;
9475   inst.instruction |= inst.operands[2].reg;
9476   inst.instruction |= inst.operands[3].reg << 8;
9477
9478   if (inst.operands[0].reg == inst.operands[1].reg)
9479     as_tsktsk (_("rdhi and rdlo must be different"));
9480 }
9481
9482 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9483    SMULxy{cond} Rd,Rm,Rs
9484    Error if any register is R15.  */
9485
9486 static void
9487 do_smul (void)
9488 {
9489   inst.instruction |= inst.operands[0].reg << 16;
9490   inst.instruction |= inst.operands[1].reg;
9491   inst.instruction |= inst.operands[2].reg << 8;
9492 }
9493
9494 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9495    the same for both ARM and Thumb-2.  */
9496
9497 static void
9498 do_srs (void)
9499 {
9500   int reg;
9501
9502   if (inst.operands[0].present)
9503     {
9504       reg = inst.operands[0].reg;
9505       constraint (reg != REG_SP, _("SRS base register must be r13"));
9506     }
9507   else
9508     reg = REG_SP;
9509
9510   inst.instruction |= reg << 16;
9511   inst.instruction |= inst.operands[1].imm;
9512   if (inst.operands[0].writeback || inst.operands[1].writeback)
9513     inst.instruction |= WRITE_BACK;
9514 }
9515
9516 /* ARM V6 strex (argument parse).  */
9517
9518 static void
9519 do_strex (void)
9520 {
9521   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9522               || inst.operands[2].postind || inst.operands[2].writeback
9523               || inst.operands[2].immisreg || inst.operands[2].shifted
9524               || inst.operands[2].negative
9525               /* See comment in do_ldrex().  */
9526               || (inst.operands[2].reg == REG_PC),
9527               BAD_ADDR_MODE);
9528
9529   constraint (inst.operands[0].reg == inst.operands[1].reg
9530               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9531
9532   constraint (inst.reloc.exp.X_op != O_constant
9533               || inst.reloc.exp.X_add_number != 0,
9534               _("offset must be zero in ARM encoding"));
9535
9536   inst.instruction |= inst.operands[0].reg << 12;
9537   inst.instruction |= inst.operands[1].reg;
9538   inst.instruction |= inst.operands[2].reg << 16;
9539   inst.reloc.type = BFD_RELOC_UNUSED;
9540 }
9541
9542 static void
9543 do_t_strexbh (void)
9544 {
9545   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9546               || inst.operands[2].postind || inst.operands[2].writeback
9547               || inst.operands[2].immisreg || inst.operands[2].shifted
9548               || inst.operands[2].negative,
9549               BAD_ADDR_MODE);
9550
9551   constraint (inst.operands[0].reg == inst.operands[1].reg
9552               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9553
9554   do_rm_rd_rn ();
9555 }
9556
9557 static void
9558 do_strexd (void)
9559 {
9560   constraint (inst.operands[1].reg % 2 != 0,
9561               _("even register required"));
9562   constraint (inst.operands[2].present
9563               && inst.operands[2].reg != inst.operands[1].reg + 1,
9564               _("can only store two consecutive registers"));
9565   /* If op 2 were present and equal to PC, this function wouldn't
9566      have been called in the first place.  */
9567   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9568
9569   constraint (inst.operands[0].reg == inst.operands[1].reg
9570               || inst.operands[0].reg == inst.operands[1].reg + 1
9571               || inst.operands[0].reg == inst.operands[3].reg,
9572               BAD_OVERLAP);
9573
9574   inst.instruction |= inst.operands[0].reg << 12;
9575   inst.instruction |= inst.operands[1].reg;
9576   inst.instruction |= inst.operands[3].reg << 16;
9577 }
9578
9579 /* ARM V8 STRL.  */
9580 static void
9581 do_stlex (void)
9582 {
9583   constraint (inst.operands[0].reg == inst.operands[1].reg
9584               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9585
9586   do_rd_rm_rn ();
9587 }
9588
9589 static void
9590 do_t_stlex (void)
9591 {
9592   constraint (inst.operands[0].reg == inst.operands[1].reg
9593               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9594
9595   do_rm_rd_rn ();
9596 }
9597
9598 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9599    extends it to 32-bits, and adds the result to a value in another
9600    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9601    before extracting the 16-bit value.
9602    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9603    Condition defaults to COND_ALWAYS.
9604    Error if any register uses R15.  */
9605
9606 static void
9607 do_sxtah (void)
9608 {
9609   inst.instruction |= inst.operands[0].reg << 12;
9610   inst.instruction |= inst.operands[1].reg << 16;
9611   inst.instruction |= inst.operands[2].reg;
9612   inst.instruction |= inst.operands[3].imm << 10;
9613 }
9614
9615 /* ARM V6 SXTH.
9616
9617    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9618    Condition defaults to COND_ALWAYS.
9619    Error if any register uses R15.  */
9620
9621 static void
9622 do_sxth (void)
9623 {
9624   inst.instruction |= inst.operands[0].reg << 12;
9625   inst.instruction |= inst.operands[1].reg;
9626   inst.instruction |= inst.operands[2].imm << 10;
9627 }
9628 \f
9629 /* VFP instructions.  In a logical order: SP variant first, monad
9630    before dyad, arithmetic then move then load/store.  */
9631
9632 static void
9633 do_vfp_sp_monadic (void)
9634 {
9635   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9636   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9637 }
9638
9639 static void
9640 do_vfp_sp_dyadic (void)
9641 {
9642   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9643   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9644   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9645 }
9646
9647 static void
9648 do_vfp_sp_compare_z (void)
9649 {
9650   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9651 }
9652
9653 static void
9654 do_vfp_dp_sp_cvt (void)
9655 {
9656   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9657   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9658 }
9659
9660 static void
9661 do_vfp_sp_dp_cvt (void)
9662 {
9663   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9664   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9665 }
9666
9667 static void
9668 do_vfp_reg_from_sp (void)
9669 {
9670   inst.instruction |= inst.operands[0].reg << 12;
9671   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9672 }
9673
9674 static void
9675 do_vfp_reg2_from_sp2 (void)
9676 {
9677   constraint (inst.operands[2].imm != 2,
9678               _("only two consecutive VFP SP registers allowed here"));
9679   inst.instruction |= inst.operands[0].reg << 12;
9680   inst.instruction |= inst.operands[1].reg << 16;
9681   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9682 }
9683
9684 static void
9685 do_vfp_sp_from_reg (void)
9686 {
9687   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9688   inst.instruction |= inst.operands[1].reg << 12;
9689 }
9690
9691 static void
9692 do_vfp_sp2_from_reg2 (void)
9693 {
9694   constraint (inst.operands[0].imm != 2,
9695               _("only two consecutive VFP SP registers allowed here"));
9696   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9697   inst.instruction |= inst.operands[1].reg << 12;
9698   inst.instruction |= inst.operands[2].reg << 16;
9699 }
9700
9701 static void
9702 do_vfp_sp_ldst (void)
9703 {
9704   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9705   encode_arm_cp_address (1, FALSE, TRUE, 0);
9706 }
9707
9708 static void
9709 do_vfp_dp_ldst (void)
9710 {
9711   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9712   encode_arm_cp_address (1, FALSE, TRUE, 0);
9713 }
9714
9715
9716 static void
9717 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9718 {
9719   if (inst.operands[0].writeback)
9720     inst.instruction |= WRITE_BACK;
9721   else
9722     constraint (ldstm_type != VFP_LDSTMIA,
9723                 _("this addressing mode requires base-register writeback"));
9724   inst.instruction |= inst.operands[0].reg << 16;
9725   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9726   inst.instruction |= inst.operands[1].imm;
9727 }
9728
9729 static void
9730 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9731 {
9732   int count;
9733
9734   if (inst.operands[0].writeback)
9735     inst.instruction |= WRITE_BACK;
9736   else
9737     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9738                 _("this addressing mode requires base-register writeback"));
9739
9740   inst.instruction |= inst.operands[0].reg << 16;
9741   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9742
9743   count = inst.operands[1].imm << 1;
9744   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9745     count += 1;
9746
9747   inst.instruction |= count;
9748 }
9749
9750 static void
9751 do_vfp_sp_ldstmia (void)
9752 {
9753   vfp_sp_ldstm (VFP_LDSTMIA);
9754 }
9755
9756 static void
9757 do_vfp_sp_ldstmdb (void)
9758 {
9759   vfp_sp_ldstm (VFP_LDSTMDB);
9760 }
9761
9762 static void
9763 do_vfp_dp_ldstmia (void)
9764 {
9765   vfp_dp_ldstm (VFP_LDSTMIA);
9766 }
9767
9768 static void
9769 do_vfp_dp_ldstmdb (void)
9770 {
9771   vfp_dp_ldstm (VFP_LDSTMDB);
9772 }
9773
9774 static void
9775 do_vfp_xp_ldstmia (void)
9776 {
9777   vfp_dp_ldstm (VFP_LDSTMIAX);
9778 }
9779
9780 static void
9781 do_vfp_xp_ldstmdb (void)
9782 {
9783   vfp_dp_ldstm (VFP_LDSTMDBX);
9784 }
9785
9786 static void
9787 do_vfp_dp_rd_rm (void)
9788 {
9789   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9790   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9791 }
9792
9793 static void
9794 do_vfp_dp_rn_rd (void)
9795 {
9796   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9797   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9798 }
9799
9800 static void
9801 do_vfp_dp_rd_rn (void)
9802 {
9803   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9804   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9805 }
9806
9807 static void
9808 do_vfp_dp_rd_rn_rm (void)
9809 {
9810   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9811   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9812   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9813 }
9814
9815 static void
9816 do_vfp_dp_rd (void)
9817 {
9818   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9819 }
9820
9821 static void
9822 do_vfp_dp_rm_rd_rn (void)
9823 {
9824   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9825   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9826   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9827 }
9828
9829 /* VFPv3 instructions.  */
9830 static void
9831 do_vfp_sp_const (void)
9832 {
9833   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9834   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9835   inst.instruction |= (inst.operands[1].imm & 0x0f);
9836 }
9837
9838 static void
9839 do_vfp_dp_const (void)
9840 {
9841   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9842   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9843   inst.instruction |= (inst.operands[1].imm & 0x0f);
9844 }
9845
9846 static void
9847 vfp_conv (int srcsize)
9848 {
9849   int immbits = srcsize - inst.operands[1].imm;
9850
9851   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9852     {
9853       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9854          i.e. immbits must be in range 0 - 16.  */
9855       inst.error = _("immediate value out of range, expected range [0, 16]");
9856       return;
9857     }
9858   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9859     {
9860       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9861          i.e. immbits must be in range 0 - 31.  */
9862       inst.error = _("immediate value out of range, expected range [1, 32]");
9863       return;
9864     }
9865
9866   inst.instruction |= (immbits & 1) << 5;
9867   inst.instruction |= (immbits >> 1);
9868 }
9869
9870 static void
9871 do_vfp_sp_conv_16 (void)
9872 {
9873   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9874   vfp_conv (16);
9875 }
9876
9877 static void
9878 do_vfp_dp_conv_16 (void)
9879 {
9880   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9881   vfp_conv (16);
9882 }
9883
9884 static void
9885 do_vfp_sp_conv_32 (void)
9886 {
9887   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9888   vfp_conv (32);
9889 }
9890
9891 static void
9892 do_vfp_dp_conv_32 (void)
9893 {
9894   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9895   vfp_conv (32);
9896 }
9897 \f
9898 /* FPA instructions.  Also in a logical order.  */
9899
9900 static void
9901 do_fpa_cmp (void)
9902 {
9903   inst.instruction |= inst.operands[0].reg << 16;
9904   inst.instruction |= inst.operands[1].reg;
9905 }
9906
9907 static void
9908 do_fpa_ldmstm (void)
9909 {
9910   inst.instruction |= inst.operands[0].reg << 12;
9911   switch (inst.operands[1].imm)
9912     {
9913     case 1: inst.instruction |= CP_T_X;          break;
9914     case 2: inst.instruction |= CP_T_Y;          break;
9915     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
9916     case 4:                                      break;
9917     default: abort ();
9918     }
9919
9920   if (inst.instruction & (PRE_INDEX | INDEX_UP))
9921     {
9922       /* The instruction specified "ea" or "fd", so we can only accept
9923          [Rn]{!}.  The instruction does not really support stacking or
9924          unstacking, so we have to emulate these by setting appropriate
9925          bits and offsets.  */
9926       constraint (inst.reloc.exp.X_op != O_constant
9927                   || inst.reloc.exp.X_add_number != 0,
9928                   _("this instruction does not support indexing"));
9929
9930       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
9931         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
9932
9933       if (!(inst.instruction & INDEX_UP))
9934         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
9935
9936       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
9937         {
9938           inst.operands[2].preind = 0;
9939           inst.operands[2].postind = 1;
9940         }
9941     }
9942
9943   encode_arm_cp_address (2, TRUE, TRUE, 0);
9944 }
9945 \f
9946 /* iWMMXt instructions: strictly in alphabetical order.  */
9947
9948 static void
9949 do_iwmmxt_tandorc (void)
9950 {
9951   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
9952 }
9953
9954 static void
9955 do_iwmmxt_textrc (void)
9956 {
9957   inst.instruction |= inst.operands[0].reg << 12;
9958   inst.instruction |= inst.operands[1].imm;
9959 }
9960
9961 static void
9962 do_iwmmxt_textrm (void)
9963 {
9964   inst.instruction |= inst.operands[0].reg << 12;
9965   inst.instruction |= inst.operands[1].reg << 16;
9966   inst.instruction |= inst.operands[2].imm;
9967 }
9968
9969 static void
9970 do_iwmmxt_tinsr (void)
9971 {
9972   inst.instruction |= inst.operands[0].reg << 16;
9973   inst.instruction |= inst.operands[1].reg << 12;
9974   inst.instruction |= inst.operands[2].imm;
9975 }
9976
9977 static void
9978 do_iwmmxt_tmia (void)
9979 {
9980   inst.instruction |= inst.operands[0].reg << 5;
9981   inst.instruction |= inst.operands[1].reg;
9982   inst.instruction |= inst.operands[2].reg << 12;
9983 }
9984
9985 static void
9986 do_iwmmxt_waligni (void)
9987 {
9988   inst.instruction |= inst.operands[0].reg << 12;
9989   inst.instruction |= inst.operands[1].reg << 16;
9990   inst.instruction |= inst.operands[2].reg;
9991   inst.instruction |= inst.operands[3].imm << 20;
9992 }
9993
9994 static void
9995 do_iwmmxt_wmerge (void)
9996 {
9997   inst.instruction |= inst.operands[0].reg << 12;
9998   inst.instruction |= inst.operands[1].reg << 16;
9999   inst.instruction |= inst.operands[2].reg;
10000   inst.instruction |= inst.operands[3].imm << 21;
10001 }
10002
10003 static void
10004 do_iwmmxt_wmov (void)
10005 {
10006   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10007   inst.instruction |= inst.operands[0].reg << 12;
10008   inst.instruction |= inst.operands[1].reg << 16;
10009   inst.instruction |= inst.operands[1].reg;
10010 }
10011
10012 static void
10013 do_iwmmxt_wldstbh (void)
10014 {
10015   int reloc;
10016   inst.instruction |= inst.operands[0].reg << 12;
10017   if (thumb_mode)
10018     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10019   else
10020     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10021   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10022 }
10023
10024 static void
10025 do_iwmmxt_wldstw (void)
10026 {
10027   /* RIWR_RIWC clears .isreg for a control register.  */
10028   if (!inst.operands[0].isreg)
10029     {
10030       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10031       inst.instruction |= 0xf0000000;
10032     }
10033
10034   inst.instruction |= inst.operands[0].reg << 12;
10035   encode_arm_cp_address (1, TRUE, TRUE, 0);
10036 }
10037
10038 static void
10039 do_iwmmxt_wldstd (void)
10040 {
10041   inst.instruction |= inst.operands[0].reg << 12;
10042   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10043       && inst.operands[1].immisreg)
10044     {
10045       inst.instruction &= ~0x1a000ff;
10046       inst.instruction |= (0xfU << 28);
10047       if (inst.operands[1].preind)
10048         inst.instruction |= PRE_INDEX;
10049       if (!inst.operands[1].negative)
10050         inst.instruction |= INDEX_UP;
10051       if (inst.operands[1].writeback)
10052         inst.instruction |= WRITE_BACK;
10053       inst.instruction |= inst.operands[1].reg << 16;
10054       inst.instruction |= inst.reloc.exp.X_add_number << 4;
10055       inst.instruction |= inst.operands[1].imm;
10056     }
10057   else
10058     encode_arm_cp_address (1, TRUE, FALSE, 0);
10059 }
10060
10061 static void
10062 do_iwmmxt_wshufh (void)
10063 {
10064   inst.instruction |= inst.operands[0].reg << 12;
10065   inst.instruction |= inst.operands[1].reg << 16;
10066   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10067   inst.instruction |= (inst.operands[2].imm & 0x0f);
10068 }
10069
10070 static void
10071 do_iwmmxt_wzero (void)
10072 {
10073   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10074   inst.instruction |= inst.operands[0].reg;
10075   inst.instruction |= inst.operands[0].reg << 12;
10076   inst.instruction |= inst.operands[0].reg << 16;
10077 }
10078
10079 static void
10080 do_iwmmxt_wrwrwr_or_imm5 (void)
10081 {
10082   if (inst.operands[2].isreg)
10083     do_rd_rn_rm ();
10084   else {
10085     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10086                 _("immediate operand requires iWMMXt2"));
10087     do_rd_rn ();
10088     if (inst.operands[2].imm == 0)
10089       {
10090         switch ((inst.instruction >> 20) & 0xf)
10091           {
10092           case 4:
10093           case 5:
10094           case 6:
10095           case 7:
10096             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10097             inst.operands[2].imm = 16;
10098             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10099             break;
10100           case 8:
10101           case 9:
10102           case 10:
10103           case 11:
10104             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10105             inst.operands[2].imm = 32;
10106             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10107             break;
10108           case 12:
10109           case 13:
10110           case 14:
10111           case 15:
10112             {
10113               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10114               unsigned long wrn;
10115               wrn = (inst.instruction >> 16) & 0xf;
10116               inst.instruction &= 0xff0fff0f;
10117               inst.instruction |= wrn;
10118               /* Bail out here; the instruction is now assembled.  */
10119               return;
10120             }
10121           }
10122       }
10123     /* Map 32 -> 0, etc.  */
10124     inst.operands[2].imm &= 0x1f;
10125     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10126   }
10127 }
10128 \f
10129 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10130    operations first, then control, shift, and load/store.  */
10131
10132 /* Insns like "foo X,Y,Z".  */
10133
10134 static void
10135 do_mav_triple (void)
10136 {
10137   inst.instruction |= inst.operands[0].reg << 16;
10138   inst.instruction |= inst.operands[1].reg;
10139   inst.instruction |= inst.operands[2].reg << 12;
10140 }
10141
10142 /* Insns like "foo W,X,Y,Z".
10143     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10144
10145 static void
10146 do_mav_quad (void)
10147 {
10148   inst.instruction |= inst.operands[0].reg << 5;
10149   inst.instruction |= inst.operands[1].reg << 12;
10150   inst.instruction |= inst.operands[2].reg << 16;
10151   inst.instruction |= inst.operands[3].reg;
10152 }
10153
10154 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10155 static void
10156 do_mav_dspsc (void)
10157 {
10158   inst.instruction |= inst.operands[1].reg << 12;
10159 }
10160
10161 /* Maverick shift immediate instructions.
10162    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10163    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10164
10165 static void
10166 do_mav_shift (void)
10167 {
10168   int imm = inst.operands[2].imm;
10169
10170   inst.instruction |= inst.operands[0].reg << 12;
10171   inst.instruction |= inst.operands[1].reg << 16;
10172
10173   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10174      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10175      Bit 4 should be 0.  */
10176   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10177
10178   inst.instruction |= imm;
10179 }
10180 \f
10181 /* XScale instructions.  Also sorted arithmetic before move.  */
10182
10183 /* Xscale multiply-accumulate (argument parse)
10184      MIAcc   acc0,Rm,Rs
10185      MIAPHcc acc0,Rm,Rs
10186      MIAxycc acc0,Rm,Rs.  */
10187
10188 static void
10189 do_xsc_mia (void)
10190 {
10191   inst.instruction |= inst.operands[1].reg;
10192   inst.instruction |= inst.operands[2].reg << 12;
10193 }
10194
10195 /* Xscale move-accumulator-register (argument parse)
10196
10197      MARcc   acc0,RdLo,RdHi.  */
10198
10199 static void
10200 do_xsc_mar (void)
10201 {
10202   inst.instruction |= inst.operands[1].reg << 12;
10203   inst.instruction |= inst.operands[2].reg << 16;
10204 }
10205
10206 /* Xscale move-register-accumulator (argument parse)
10207
10208      MRAcc   RdLo,RdHi,acc0.  */
10209
10210 static void
10211 do_xsc_mra (void)
10212 {
10213   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10214   inst.instruction |= inst.operands[0].reg << 12;
10215   inst.instruction |= inst.operands[1].reg << 16;
10216 }
10217 \f
10218 /* Encoding functions relevant only to Thumb.  */
10219
10220 /* inst.operands[i] is a shifted-register operand; encode
10221    it into inst.instruction in the format used by Thumb32.  */
10222
10223 static void
10224 encode_thumb32_shifted_operand (int i)
10225 {
10226   unsigned int value = inst.reloc.exp.X_add_number;
10227   unsigned int shift = inst.operands[i].shift_kind;
10228
10229   constraint (inst.operands[i].immisreg,
10230               _("shift by register not allowed in thumb mode"));
10231   inst.instruction |= inst.operands[i].reg;
10232   if (shift == SHIFT_RRX)
10233     inst.instruction |= SHIFT_ROR << 4;
10234   else
10235     {
10236       constraint (inst.reloc.exp.X_op != O_constant,
10237                   _("expression too complex"));
10238
10239       constraint (value > 32
10240                   || (value == 32 && (shift == SHIFT_LSL
10241                                       || shift == SHIFT_ROR)),
10242                   _("shift expression is too large"));
10243
10244       if (value == 0)
10245         shift = SHIFT_LSL;
10246       else if (value == 32)
10247         value = 0;
10248
10249       inst.instruction |= shift << 4;
10250       inst.instruction |= (value & 0x1c) << 10;
10251       inst.instruction |= (value & 0x03) << 6;
10252     }
10253 }
10254
10255
10256 /* inst.operands[i] was set up by parse_address.  Encode it into a
10257    Thumb32 format load or store instruction.  Reject forms that cannot
10258    be used with such instructions.  If is_t is true, reject forms that
10259    cannot be used with a T instruction; if is_d is true, reject forms
10260    that cannot be used with a D instruction.  If it is a store insn,
10261    reject PC in Rn.  */
10262
10263 static void
10264 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10265 {
10266   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10267
10268   constraint (!inst.operands[i].isreg,
10269               _("Instruction does not support =N addresses"));
10270
10271   inst.instruction |= inst.operands[i].reg << 16;
10272   if (inst.operands[i].immisreg)
10273     {
10274       constraint (is_pc, BAD_PC_ADDRESSING);
10275       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10276       constraint (inst.operands[i].negative,
10277                   _("Thumb does not support negative register indexing"));
10278       constraint (inst.operands[i].postind,
10279                   _("Thumb does not support register post-indexing"));
10280       constraint (inst.operands[i].writeback,
10281                   _("Thumb does not support register indexing with writeback"));
10282       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10283                   _("Thumb supports only LSL in shifted register indexing"));
10284
10285       inst.instruction |= inst.operands[i].imm;
10286       if (inst.operands[i].shifted)
10287         {
10288           constraint (inst.reloc.exp.X_op != O_constant,
10289                       _("expression too complex"));
10290           constraint (inst.reloc.exp.X_add_number < 0
10291                       || inst.reloc.exp.X_add_number > 3,
10292                       _("shift out of range"));
10293           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10294         }
10295       inst.reloc.type = BFD_RELOC_UNUSED;
10296     }
10297   else if (inst.operands[i].preind)
10298     {
10299       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10300       constraint (is_t && inst.operands[i].writeback,
10301                   _("cannot use writeback with this instruction"));
10302       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10303                   BAD_PC_ADDRESSING);
10304
10305       if (is_d)
10306         {
10307           inst.instruction |= 0x01000000;
10308           if (inst.operands[i].writeback)
10309             inst.instruction |= 0x00200000;
10310         }
10311       else
10312         {
10313           inst.instruction |= 0x00000c00;
10314           if (inst.operands[i].writeback)
10315             inst.instruction |= 0x00000100;
10316         }
10317       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10318     }
10319   else if (inst.operands[i].postind)
10320     {
10321       gas_assert (inst.operands[i].writeback);
10322       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10323       constraint (is_t, _("cannot use post-indexing with this instruction"));
10324
10325       if (is_d)
10326         inst.instruction |= 0x00200000;
10327       else
10328         inst.instruction |= 0x00000900;
10329       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10330     }
10331   else /* unindexed - only for coprocessor */
10332     inst.error = _("instruction does not accept unindexed addressing");
10333 }
10334
10335 /* Table of Thumb instructions which exist in both 16- and 32-bit
10336    encodings (the latter only in post-V6T2 cores).  The index is the
10337    value used in the insns table below.  When there is more than one
10338    possible 16-bit encoding for the instruction, this table always
10339    holds variant (1).
10340    Also contains several pseudo-instructions used during relaxation.  */
10341 #define T16_32_TAB                              \
10342   X(_adc,   4140, eb400000),                    \
10343   X(_adcs,  4140, eb500000),                    \
10344   X(_add,   1c00, eb000000),                    \
10345   X(_adds,  1c00, eb100000),                    \
10346   X(_addi,  0000, f1000000),                    \
10347   X(_addis, 0000, f1100000),                    \
10348   X(_add_pc,000f, f20f0000),                    \
10349   X(_add_sp,000d, f10d0000),                    \
10350   X(_adr,   000f, f20f0000),                    \
10351   X(_and,   4000, ea000000),                    \
10352   X(_ands,  4000, ea100000),                    \
10353   X(_asr,   1000, fa40f000),                    \
10354   X(_asrs,  1000, fa50f000),                    \
10355   X(_b,     e000, f000b000),                    \
10356   X(_bcond, d000, f0008000),                    \
10357   X(_bic,   4380, ea200000),                    \
10358   X(_bics,  4380, ea300000),                    \
10359   X(_cmn,   42c0, eb100f00),                    \
10360   X(_cmp,   2800, ebb00f00),                    \
10361   X(_cpsie, b660, f3af8400),                    \
10362   X(_cpsid, b670, f3af8600),                    \
10363   X(_cpy,   4600, ea4f0000),                    \
10364   X(_dec_sp,80dd, f1ad0d00),                    \
10365   X(_eor,   4040, ea800000),                    \
10366   X(_eors,  4040, ea900000),                    \
10367   X(_inc_sp,00dd, f10d0d00),                    \
10368   X(_ldmia, c800, e8900000),                    \
10369   X(_ldr,   6800, f8500000),                    \
10370   X(_ldrb,  7800, f8100000),                    \
10371   X(_ldrh,  8800, f8300000),                    \
10372   X(_ldrsb, 5600, f9100000),                    \
10373   X(_ldrsh, 5e00, f9300000),                    \
10374   X(_ldr_pc,4800, f85f0000),                    \
10375   X(_ldr_pc2,4800, f85f0000),                   \
10376   X(_ldr_sp,9800, f85d0000),                    \
10377   X(_lsl,   0000, fa00f000),                    \
10378   X(_lsls,  0000, fa10f000),                    \
10379   X(_lsr,   0800, fa20f000),                    \
10380   X(_lsrs,  0800, fa30f000),                    \
10381   X(_mov,   2000, ea4f0000),                    \
10382   X(_movs,  2000, ea5f0000),                    \
10383   X(_mul,   4340, fb00f000),                     \
10384   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10385   X(_mvn,   43c0, ea6f0000),                    \
10386   X(_mvns,  43c0, ea7f0000),                    \
10387   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10388   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10389   X(_orr,   4300, ea400000),                    \
10390   X(_orrs,  4300, ea500000),                    \
10391   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10392   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10393   X(_rev,   ba00, fa90f080),                    \
10394   X(_rev16, ba40, fa90f090),                    \
10395   X(_revsh, bac0, fa90f0b0),                    \
10396   X(_ror,   41c0, fa60f000),                    \
10397   X(_rors,  41c0, fa70f000),                    \
10398   X(_sbc,   4180, eb600000),                    \
10399   X(_sbcs,  4180, eb700000),                    \
10400   X(_stmia, c000, e8800000),                    \
10401   X(_str,   6000, f8400000),                    \
10402   X(_strb,  7000, f8000000),                    \
10403   X(_strh,  8000, f8200000),                    \
10404   X(_str_sp,9000, f84d0000),                    \
10405   X(_sub,   1e00, eba00000),                    \
10406   X(_subs,  1e00, ebb00000),                    \
10407   X(_subi,  8000, f1a00000),                    \
10408   X(_subis, 8000, f1b00000),                    \
10409   X(_sxtb,  b240, fa4ff080),                    \
10410   X(_sxth,  b200, fa0ff080),                    \
10411   X(_tst,   4200, ea100f00),                    \
10412   X(_uxtb,  b2c0, fa5ff080),                    \
10413   X(_uxth,  b280, fa1ff080),                    \
10414   X(_nop,   bf00, f3af8000),                    \
10415   X(_yield, bf10, f3af8001),                    \
10416   X(_wfe,   bf20, f3af8002),                    \
10417   X(_wfi,   bf30, f3af8003),                    \
10418   X(_sev,   bf40, f3af8004),                    \
10419   X(_sevl,  bf50, f3af8005),                    \
10420   X(_udf,   de00, f7f0a000)
10421
10422 /* To catch errors in encoding functions, the codes are all offset by
10423    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10424    as 16-bit instructions.  */
10425 #define X(a,b,c) T_MNEM##a
10426 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10427 #undef X
10428
10429 #define X(a,b,c) 0x##b
10430 static const unsigned short thumb_op16[] = { T16_32_TAB };
10431 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10432 #undef X
10433
10434 #define X(a,b,c) 0x##c
10435 static const unsigned int thumb_op32[] = { T16_32_TAB };
10436 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10437 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10438 #undef X
10439 #undef T16_32_TAB
10440
10441 /* Thumb instruction encoders, in alphabetical order.  */
10442
10443 /* ADDW or SUBW.  */
10444
10445 static void
10446 do_t_add_sub_w (void)
10447 {
10448   int Rd, Rn;
10449
10450   Rd = inst.operands[0].reg;
10451   Rn = inst.operands[1].reg;
10452
10453   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10454      is the SP-{plus,minus}-immediate form of the instruction.  */
10455   if (Rn == REG_SP)
10456     constraint (Rd == REG_PC, BAD_PC);
10457   else
10458     reject_bad_reg (Rd);
10459
10460   inst.instruction |= (Rn << 16) | (Rd << 8);
10461   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10462 }
10463
10464 /* Parse an add or subtract instruction.  We get here with inst.instruction
10465    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10466
10467 static void
10468 do_t_add_sub (void)
10469 {
10470   int Rd, Rs, Rn;
10471
10472   Rd = inst.operands[0].reg;
10473   Rs = (inst.operands[1].present
10474         ? inst.operands[1].reg    /* Rd, Rs, foo */
10475         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10476
10477   if (Rd == REG_PC)
10478     set_it_insn_type_last ();
10479
10480   if (unified_syntax)
10481     {
10482       bfd_boolean flags;
10483       bfd_boolean narrow;
10484       int opcode;
10485
10486       flags = (inst.instruction == T_MNEM_adds
10487                || inst.instruction == T_MNEM_subs);
10488       if (flags)
10489         narrow = !in_it_block ();
10490       else
10491         narrow = in_it_block ();
10492       if (!inst.operands[2].isreg)
10493         {
10494           int add;
10495
10496           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10497
10498           add = (inst.instruction == T_MNEM_add
10499                  || inst.instruction == T_MNEM_adds);
10500           opcode = 0;
10501           if (inst.size_req != 4)
10502             {
10503               /* Attempt to use a narrow opcode, with relaxation if
10504                  appropriate.  */
10505               if (Rd == REG_SP && Rs == REG_SP && !flags)
10506                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10507               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10508                 opcode = T_MNEM_add_sp;
10509               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10510                 opcode = T_MNEM_add_pc;
10511               else if (Rd <= 7 && Rs <= 7 && narrow)
10512                 {
10513                   if (flags)
10514                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10515                   else
10516                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10517                 }
10518               if (opcode)
10519                 {
10520                   inst.instruction = THUMB_OP16(opcode);
10521                   inst.instruction |= (Rd << 4) | Rs;
10522                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10523                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
10524                   {
10525                     if (inst.size_req == 2)
10526                       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10527                     else
10528                       inst.relax = opcode;
10529                   }
10530                 }
10531               else
10532                 constraint (inst.size_req == 2, BAD_HIREG);
10533             }
10534           if (inst.size_req == 4
10535               || (inst.size_req != 2 && !opcode))
10536             {
10537               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10538                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
10539                           THUMB1_RELOC_ONLY);
10540               if (Rd == REG_PC)
10541                 {
10542                   constraint (add, BAD_PC);
10543                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10544                              _("only SUBS PC, LR, #const allowed"));
10545                   constraint (inst.reloc.exp.X_op != O_constant,
10546                               _("expression too complex"));
10547                   constraint (inst.reloc.exp.X_add_number < 0
10548                               || inst.reloc.exp.X_add_number > 0xff,
10549                              _("immediate value out of range"));
10550                   inst.instruction = T2_SUBS_PC_LR
10551                                      | inst.reloc.exp.X_add_number;
10552                   inst.reloc.type = BFD_RELOC_UNUSED;
10553                   return;
10554                 }
10555               else if (Rs == REG_PC)
10556                 {
10557                   /* Always use addw/subw.  */
10558                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10559                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10560                 }
10561               else
10562                 {
10563                   inst.instruction = THUMB_OP32 (inst.instruction);
10564                   inst.instruction = (inst.instruction & 0xe1ffffff)
10565                                      | 0x10000000;
10566                   if (flags)
10567                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10568                   else
10569                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10570                 }
10571               inst.instruction |= Rd << 8;
10572               inst.instruction |= Rs << 16;
10573             }
10574         }
10575       else
10576         {
10577           unsigned int value = inst.reloc.exp.X_add_number;
10578           unsigned int shift = inst.operands[2].shift_kind;
10579
10580           Rn = inst.operands[2].reg;
10581           /* See if we can do this with a 16-bit instruction.  */
10582           if (!inst.operands[2].shifted && inst.size_req != 4)
10583             {
10584               if (Rd > 7 || Rs > 7 || Rn > 7)
10585                 narrow = FALSE;
10586
10587               if (narrow)
10588                 {
10589                   inst.instruction = ((inst.instruction == T_MNEM_adds
10590                                        || inst.instruction == T_MNEM_add)
10591                                       ? T_OPCODE_ADD_R3
10592                                       : T_OPCODE_SUB_R3);
10593                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10594                   return;
10595                 }
10596
10597               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10598                 {
10599                   /* Thumb-1 cores (except v6-M) require at least one high
10600                      register in a narrow non flag setting add.  */
10601                   if (Rd > 7 || Rn > 7
10602                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10603                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10604                     {
10605                       if (Rd == Rn)
10606                         {
10607                           Rn = Rs;
10608                           Rs = Rd;
10609                         }
10610                       inst.instruction = T_OPCODE_ADD_HI;
10611                       inst.instruction |= (Rd & 8) << 4;
10612                       inst.instruction |= (Rd & 7);
10613                       inst.instruction |= Rn << 3;
10614                       return;
10615                     }
10616                 }
10617             }
10618
10619           constraint (Rd == REG_PC, BAD_PC);
10620           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10621           constraint (Rs == REG_PC, BAD_PC);
10622           reject_bad_reg (Rn);
10623
10624           /* If we get here, it can't be done in 16 bits.  */
10625           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10626                       _("shift must be constant"));
10627           inst.instruction = THUMB_OP32 (inst.instruction);
10628           inst.instruction |= Rd << 8;
10629           inst.instruction |= Rs << 16;
10630           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10631                       _("shift value over 3 not allowed in thumb mode"));
10632           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10633                       _("only LSL shift allowed in thumb mode"));
10634           encode_thumb32_shifted_operand (2);
10635         }
10636     }
10637   else
10638     {
10639       constraint (inst.instruction == T_MNEM_adds
10640                   || inst.instruction == T_MNEM_subs,
10641                   BAD_THUMB32);
10642
10643       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10644         {
10645           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10646                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10647                       BAD_HIREG);
10648
10649           inst.instruction = (inst.instruction == T_MNEM_add
10650                               ? 0x0000 : 0x8000);
10651           inst.instruction |= (Rd << 4) | Rs;
10652           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10653           return;
10654         }
10655
10656       Rn = inst.operands[2].reg;
10657       constraint (inst.operands[2].shifted, _("unshifted register required"));
10658
10659       /* We now have Rd, Rs, and Rn set to registers.  */
10660       if (Rd > 7 || Rs > 7 || Rn > 7)
10661         {
10662           /* Can't do this for SUB.      */
10663           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10664           inst.instruction = T_OPCODE_ADD_HI;
10665           inst.instruction |= (Rd & 8) << 4;
10666           inst.instruction |= (Rd & 7);
10667           if (Rs == Rd)
10668             inst.instruction |= Rn << 3;
10669           else if (Rn == Rd)
10670             inst.instruction |= Rs << 3;
10671           else
10672             constraint (1, _("dest must overlap one source register"));
10673         }
10674       else
10675         {
10676           inst.instruction = (inst.instruction == T_MNEM_add
10677                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10678           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10679         }
10680     }
10681 }
10682
10683 static void
10684 do_t_adr (void)
10685 {
10686   unsigned Rd;
10687
10688   Rd = inst.operands[0].reg;
10689   reject_bad_reg (Rd);
10690
10691   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10692     {
10693       /* Defer to section relaxation.  */
10694       inst.relax = inst.instruction;
10695       inst.instruction = THUMB_OP16 (inst.instruction);
10696       inst.instruction |= Rd << 4;
10697     }
10698   else if (unified_syntax && inst.size_req != 2)
10699     {
10700       /* Generate a 32-bit opcode.  */
10701       inst.instruction = THUMB_OP32 (inst.instruction);
10702       inst.instruction |= Rd << 8;
10703       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10704       inst.reloc.pc_rel = 1;
10705     }
10706   else
10707     {
10708       /* Generate a 16-bit opcode.  */
10709       inst.instruction = THUMB_OP16 (inst.instruction);
10710       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10711       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10712       inst.reloc.pc_rel = 1;
10713
10714       inst.instruction |= Rd << 4;
10715     }
10716 }
10717
10718 /* Arithmetic instructions for which there is just one 16-bit
10719    instruction encoding, and it allows only two low registers.
10720    For maximal compatibility with ARM syntax, we allow three register
10721    operands even when Thumb-32 instructions are not available, as long
10722    as the first two are identical.  For instance, both "sbc r0,r1" and
10723    "sbc r0,r0,r1" are allowed.  */
10724 static void
10725 do_t_arit3 (void)
10726 {
10727   int Rd, Rs, Rn;
10728
10729   Rd = inst.operands[0].reg;
10730   Rs = (inst.operands[1].present
10731         ? inst.operands[1].reg    /* Rd, Rs, foo */
10732         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10733   Rn = inst.operands[2].reg;
10734
10735   reject_bad_reg (Rd);
10736   reject_bad_reg (Rs);
10737   if (inst.operands[2].isreg)
10738     reject_bad_reg (Rn);
10739
10740   if (unified_syntax)
10741     {
10742       if (!inst.operands[2].isreg)
10743         {
10744           /* For an immediate, we always generate a 32-bit opcode;
10745              section relaxation will shrink it later if possible.  */
10746           inst.instruction = THUMB_OP32 (inst.instruction);
10747           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10748           inst.instruction |= Rd << 8;
10749           inst.instruction |= Rs << 16;
10750           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10751         }
10752       else
10753         {
10754           bfd_boolean narrow;
10755
10756           /* See if we can do this with a 16-bit instruction.  */
10757           if (THUMB_SETS_FLAGS (inst.instruction))
10758             narrow = !in_it_block ();
10759           else
10760             narrow = in_it_block ();
10761
10762           if (Rd > 7 || Rn > 7 || Rs > 7)
10763             narrow = FALSE;
10764           if (inst.operands[2].shifted)
10765             narrow = FALSE;
10766           if (inst.size_req == 4)
10767             narrow = FALSE;
10768
10769           if (narrow
10770               && Rd == Rs)
10771             {
10772               inst.instruction = THUMB_OP16 (inst.instruction);
10773               inst.instruction |= Rd;
10774               inst.instruction |= Rn << 3;
10775               return;
10776             }
10777
10778           /* If we get here, it can't be done in 16 bits.  */
10779           constraint (inst.operands[2].shifted
10780                       && inst.operands[2].immisreg,
10781                       _("shift must be constant"));
10782           inst.instruction = THUMB_OP32 (inst.instruction);
10783           inst.instruction |= Rd << 8;
10784           inst.instruction |= Rs << 16;
10785           encode_thumb32_shifted_operand (2);
10786         }
10787     }
10788   else
10789     {
10790       /* On its face this is a lie - the instruction does set the
10791          flags.  However, the only supported mnemonic in this mode
10792          says it doesn't.  */
10793       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10794
10795       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10796                   _("unshifted register required"));
10797       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10798       constraint (Rd != Rs,
10799                   _("dest and source1 must be the same register"));
10800
10801       inst.instruction = THUMB_OP16 (inst.instruction);
10802       inst.instruction |= Rd;
10803       inst.instruction |= Rn << 3;
10804     }
10805 }
10806
10807 /* Similarly, but for instructions where the arithmetic operation is
10808    commutative, so we can allow either of them to be different from
10809    the destination operand in a 16-bit instruction.  For instance, all
10810    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10811    accepted.  */
10812 static void
10813 do_t_arit3c (void)
10814 {
10815   int Rd, Rs, Rn;
10816
10817   Rd = inst.operands[0].reg;
10818   Rs = (inst.operands[1].present
10819         ? inst.operands[1].reg    /* Rd, Rs, foo */
10820         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10821   Rn = inst.operands[2].reg;
10822
10823   reject_bad_reg (Rd);
10824   reject_bad_reg (Rs);
10825   if (inst.operands[2].isreg)
10826     reject_bad_reg (Rn);
10827
10828   if (unified_syntax)
10829     {
10830       if (!inst.operands[2].isreg)
10831         {
10832           /* For an immediate, we always generate a 32-bit opcode;
10833              section relaxation will shrink it later if possible.  */
10834           inst.instruction = THUMB_OP32 (inst.instruction);
10835           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10836           inst.instruction |= Rd << 8;
10837           inst.instruction |= Rs << 16;
10838           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10839         }
10840       else
10841         {
10842           bfd_boolean narrow;
10843
10844           /* See if we can do this with a 16-bit instruction.  */
10845           if (THUMB_SETS_FLAGS (inst.instruction))
10846             narrow = !in_it_block ();
10847           else
10848             narrow = in_it_block ();
10849
10850           if (Rd > 7 || Rn > 7 || Rs > 7)
10851             narrow = FALSE;
10852           if (inst.operands[2].shifted)
10853             narrow = FALSE;
10854           if (inst.size_req == 4)
10855             narrow = FALSE;
10856
10857           if (narrow)
10858             {
10859               if (Rd == Rs)
10860                 {
10861                   inst.instruction = THUMB_OP16 (inst.instruction);
10862                   inst.instruction |= Rd;
10863                   inst.instruction |= Rn << 3;
10864                   return;
10865                 }
10866               if (Rd == Rn)
10867                 {
10868                   inst.instruction = THUMB_OP16 (inst.instruction);
10869                   inst.instruction |= Rd;
10870                   inst.instruction |= Rs << 3;
10871                   return;
10872                 }
10873             }
10874
10875           /* If we get here, it can't be done in 16 bits.  */
10876           constraint (inst.operands[2].shifted
10877                       && inst.operands[2].immisreg,
10878                       _("shift must be constant"));
10879           inst.instruction = THUMB_OP32 (inst.instruction);
10880           inst.instruction |= Rd << 8;
10881           inst.instruction |= Rs << 16;
10882           encode_thumb32_shifted_operand (2);
10883         }
10884     }
10885   else
10886     {
10887       /* On its face this is a lie - the instruction does set the
10888          flags.  However, the only supported mnemonic in this mode
10889          says it doesn't.  */
10890       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10891
10892       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10893                   _("unshifted register required"));
10894       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10895
10896       inst.instruction = THUMB_OP16 (inst.instruction);
10897       inst.instruction |= Rd;
10898
10899       if (Rd == Rs)
10900         inst.instruction |= Rn << 3;
10901       else if (Rd == Rn)
10902         inst.instruction |= Rs << 3;
10903       else
10904         constraint (1, _("dest must overlap one source register"));
10905     }
10906 }
10907
10908 static void
10909 do_t_bfc (void)
10910 {
10911   unsigned Rd;
10912   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
10913   constraint (msb > 32, _("bit-field extends past end of register"));
10914   /* The instruction encoding stores the LSB and MSB,
10915      not the LSB and width.  */
10916   Rd = inst.operands[0].reg;
10917   reject_bad_reg (Rd);
10918   inst.instruction |= Rd << 8;
10919   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
10920   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
10921   inst.instruction |= msb - 1;
10922 }
10923
10924 static void
10925 do_t_bfi (void)
10926 {
10927   int Rd, Rn;
10928   unsigned int msb;
10929
10930   Rd = inst.operands[0].reg;
10931   reject_bad_reg (Rd);
10932
10933   /* #0 in second position is alternative syntax for bfc, which is
10934      the same instruction but with REG_PC in the Rm field.  */
10935   if (!inst.operands[1].isreg)
10936     Rn = REG_PC;
10937   else
10938     {
10939       Rn = inst.operands[1].reg;
10940       reject_bad_reg (Rn);
10941     }
10942
10943   msb = inst.operands[2].imm + inst.operands[3].imm;
10944   constraint (msb > 32, _("bit-field extends past end of register"));
10945   /* The instruction encoding stores the LSB and MSB,
10946      not the LSB and width.  */
10947   inst.instruction |= Rd << 8;
10948   inst.instruction |= Rn << 16;
10949   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10950   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10951   inst.instruction |= msb - 1;
10952 }
10953
10954 static void
10955 do_t_bfx (void)
10956 {
10957   unsigned Rd, Rn;
10958
10959   Rd = inst.operands[0].reg;
10960   Rn = inst.operands[1].reg;
10961
10962   reject_bad_reg (Rd);
10963   reject_bad_reg (Rn);
10964
10965   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
10966               _("bit-field extends past end of register"));
10967   inst.instruction |= Rd << 8;
10968   inst.instruction |= Rn << 16;
10969   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10970   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10971   inst.instruction |= inst.operands[3].imm - 1;
10972 }
10973
10974 /* ARM V5 Thumb BLX (argument parse)
10975         BLX <target_addr>       which is BLX(1)
10976         BLX <Rm>                which is BLX(2)
10977    Unfortunately, there are two different opcodes for this mnemonic.
10978    So, the insns[].value is not used, and the code here zaps values
10979         into inst.instruction.
10980
10981    ??? How to take advantage of the additional two bits of displacement
10982    available in Thumb32 mode?  Need new relocation?  */
10983
10984 static void
10985 do_t_blx (void)
10986 {
10987   set_it_insn_type_last ();
10988
10989   if (inst.operands[0].isreg)
10990     {
10991       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10992       /* We have a register, so this is BLX(2).  */
10993       inst.instruction |= inst.operands[0].reg << 3;
10994     }
10995   else
10996     {
10997       /* No register.  This must be BLX(1).  */
10998       inst.instruction = 0xf000e800;
10999       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11000     }
11001 }
11002
11003 static void
11004 do_t_branch (void)
11005 {
11006   int opcode;
11007   int cond;
11008   bfd_reloc_code_real_type reloc;
11009
11010   cond = inst.cond;
11011   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
11012
11013   if (in_it_block ())
11014     {
11015       /* Conditional branches inside IT blocks are encoded as unconditional
11016          branches.  */
11017       cond = COND_ALWAYS;
11018     }
11019   else
11020     cond = inst.cond;
11021
11022   if (cond != COND_ALWAYS)
11023     opcode = T_MNEM_bcond;
11024   else
11025     opcode = inst.instruction;
11026
11027   if (unified_syntax
11028       && (inst.size_req == 4
11029           || (inst.size_req != 2
11030               && (inst.operands[0].hasreloc
11031                   || inst.reloc.exp.X_op == O_constant))))
11032     {
11033       inst.instruction = THUMB_OP32(opcode);
11034       if (cond == COND_ALWAYS)
11035         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11036       else
11037         {
11038           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11039                       _("selected architecture does not support "
11040                         "wide conditional branch instruction"));
11041
11042           gas_assert (cond != 0xF);
11043           inst.instruction |= cond << 22;
11044           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11045         }
11046     }
11047   else
11048     {
11049       inst.instruction = THUMB_OP16(opcode);
11050       if (cond == COND_ALWAYS)
11051         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11052       else
11053         {
11054           inst.instruction |= cond << 8;
11055           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11056         }
11057       /* Allow section relaxation.  */
11058       if (unified_syntax && inst.size_req != 2)
11059         inst.relax = opcode;
11060     }
11061   inst.reloc.type = reloc;
11062   inst.reloc.pc_rel = 1;
11063 }
11064
11065 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11066    between the two is the maximum immediate allowed - which is passed in
11067    RANGE.  */
11068 static void
11069 do_t_bkpt_hlt1 (int range)
11070 {
11071   constraint (inst.cond != COND_ALWAYS,
11072               _("instruction is always unconditional"));
11073   if (inst.operands[0].present)
11074     {
11075       constraint (inst.operands[0].imm > range,
11076                   _("immediate value out of range"));
11077       inst.instruction |= inst.operands[0].imm;
11078     }
11079
11080   set_it_insn_type (NEUTRAL_IT_INSN);
11081 }
11082
11083 static void
11084 do_t_hlt (void)
11085 {
11086   do_t_bkpt_hlt1 (63);
11087 }
11088
11089 static void
11090 do_t_bkpt (void)
11091 {
11092   do_t_bkpt_hlt1 (255);
11093 }
11094
11095 static void
11096 do_t_branch23 (void)
11097 {
11098   set_it_insn_type_last ();
11099   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11100
11101   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11102      this file.  We used to simply ignore the PLT reloc type here --
11103      the branch encoding is now needed to deal with TLSCALL relocs.
11104      So if we see a PLT reloc now, put it back to how it used to be to
11105      keep the preexisting behaviour.  */
11106   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
11107     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11108
11109 #if defined(OBJ_COFF)
11110   /* If the destination of the branch is a defined symbol which does not have
11111      the THUMB_FUNC attribute, then we must be calling a function which has
11112      the (interfacearm) attribute.  We look for the Thumb entry point to that
11113      function and change the branch to refer to that function instead.  */
11114   if (   inst.reloc.exp.X_op == O_symbol
11115       && inst.reloc.exp.X_add_symbol != NULL
11116       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
11117       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
11118     inst.reloc.exp.X_add_symbol =
11119       find_real_start (inst.reloc.exp.X_add_symbol);
11120 #endif
11121 }
11122
11123 static void
11124 do_t_bx (void)
11125 {
11126   set_it_insn_type_last ();
11127   inst.instruction |= inst.operands[0].reg << 3;
11128   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11129      should cause the alignment to be checked once it is known.  This is
11130      because BX PC only works if the instruction is word aligned.  */
11131 }
11132
11133 static void
11134 do_t_bxj (void)
11135 {
11136   int Rm;
11137
11138   set_it_insn_type_last ();
11139   Rm = inst.operands[0].reg;
11140   reject_bad_reg (Rm);
11141   inst.instruction |= Rm << 16;
11142 }
11143
11144 static void
11145 do_t_clz (void)
11146 {
11147   unsigned Rd;
11148   unsigned Rm;
11149
11150   Rd = inst.operands[0].reg;
11151   Rm = inst.operands[1].reg;
11152
11153   reject_bad_reg (Rd);
11154   reject_bad_reg (Rm);
11155
11156   inst.instruction |= Rd << 8;
11157   inst.instruction |= Rm << 16;
11158   inst.instruction |= Rm;
11159 }
11160
11161 static void
11162 do_t_cps (void)
11163 {
11164   set_it_insn_type (OUTSIDE_IT_INSN);
11165   inst.instruction |= inst.operands[0].imm;
11166 }
11167
11168 static void
11169 do_t_cpsi (void)
11170 {
11171   set_it_insn_type (OUTSIDE_IT_INSN);
11172   if (unified_syntax
11173       && (inst.operands[1].present || inst.size_req == 4)
11174       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11175     {
11176       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11177       inst.instruction = 0xf3af8000;
11178       inst.instruction |= imod << 9;
11179       inst.instruction |= inst.operands[0].imm << 5;
11180       if (inst.operands[1].present)
11181         inst.instruction |= 0x100 | inst.operands[1].imm;
11182     }
11183   else
11184     {
11185       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11186                   && (inst.operands[0].imm & 4),
11187                   _("selected processor does not support 'A' form "
11188                     "of this instruction"));
11189       constraint (inst.operands[1].present || inst.size_req == 4,
11190                   _("Thumb does not support the 2-argument "
11191                     "form of this instruction"));
11192       inst.instruction |= inst.operands[0].imm;
11193     }
11194 }
11195
11196 /* THUMB CPY instruction (argument parse).  */
11197
11198 static void
11199 do_t_cpy (void)
11200 {
11201   if (inst.size_req == 4)
11202     {
11203       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11204       inst.instruction |= inst.operands[0].reg << 8;
11205       inst.instruction |= inst.operands[1].reg;
11206     }
11207   else
11208     {
11209       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11210       inst.instruction |= (inst.operands[0].reg & 0x7);
11211       inst.instruction |= inst.operands[1].reg << 3;
11212     }
11213 }
11214
11215 static void
11216 do_t_cbz (void)
11217 {
11218   set_it_insn_type (OUTSIDE_IT_INSN);
11219   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11220   inst.instruction |= inst.operands[0].reg;
11221   inst.reloc.pc_rel = 1;
11222   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11223 }
11224
11225 static void
11226 do_t_dbg (void)
11227 {
11228   inst.instruction |= inst.operands[0].imm;
11229 }
11230
11231 static void
11232 do_t_div (void)
11233 {
11234   unsigned Rd, Rn, Rm;
11235
11236   Rd = inst.operands[0].reg;
11237   Rn = (inst.operands[1].present
11238         ? inst.operands[1].reg : Rd);
11239   Rm = inst.operands[2].reg;
11240
11241   reject_bad_reg (Rd);
11242   reject_bad_reg (Rn);
11243   reject_bad_reg (Rm);
11244
11245   inst.instruction |= Rd << 8;
11246   inst.instruction |= Rn << 16;
11247   inst.instruction |= Rm;
11248 }
11249
11250 static void
11251 do_t_hint (void)
11252 {
11253   if (unified_syntax && inst.size_req == 4)
11254     inst.instruction = THUMB_OP32 (inst.instruction);
11255   else
11256     inst.instruction = THUMB_OP16 (inst.instruction);
11257 }
11258
11259 static void
11260 do_t_it (void)
11261 {
11262   unsigned int cond = inst.operands[0].imm;
11263
11264   set_it_insn_type (IT_INSN);
11265   now_it.mask = (inst.instruction & 0xf) | 0x10;
11266   now_it.cc = cond;
11267   now_it.warn_deprecated = FALSE;
11268
11269   /* If the condition is a negative condition, invert the mask.  */
11270   if ((cond & 0x1) == 0x0)
11271     {
11272       unsigned int mask = inst.instruction & 0x000f;
11273
11274       if ((mask & 0x7) == 0)
11275         {
11276           /* No conversion needed.  */
11277           now_it.block_length = 1;
11278         }
11279       else if ((mask & 0x3) == 0)
11280         {
11281           mask ^= 0x8;
11282           now_it.block_length = 2;
11283         }
11284       else if ((mask & 0x1) == 0)
11285         {
11286           mask ^= 0xC;
11287           now_it.block_length = 3;
11288         }
11289       else
11290         {
11291           mask ^= 0xE;
11292           now_it.block_length = 4;
11293         }
11294
11295       inst.instruction &= 0xfff0;
11296       inst.instruction |= mask;
11297     }
11298
11299   inst.instruction |= cond << 4;
11300 }
11301
11302 /* Helper function used for both push/pop and ldm/stm.  */
11303 static void
11304 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11305 {
11306   bfd_boolean load;
11307
11308   load = (inst.instruction & (1 << 20)) != 0;
11309
11310   if (mask & (1 << 13))
11311     inst.error =  _("SP not allowed in register list");
11312
11313   if ((mask & (1 << base)) != 0
11314       && writeback)
11315     inst.error = _("having the base register in the register list when "
11316                    "using write back is UNPREDICTABLE");
11317
11318   if (load)
11319     {
11320       if (mask & (1 << 15))
11321         {
11322           if (mask & (1 << 14))
11323             inst.error = _("LR and PC should not both be in register list");
11324           else
11325             set_it_insn_type_last ();
11326         }
11327     }
11328   else
11329     {
11330       if (mask & (1 << 15))
11331         inst.error = _("PC not allowed in register list");
11332     }
11333
11334   if ((mask & (mask - 1)) == 0)
11335     {
11336       /* Single register transfers implemented as str/ldr.  */
11337       if (writeback)
11338         {
11339           if (inst.instruction & (1 << 23))
11340             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11341           else
11342             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11343         }
11344       else
11345         {
11346           if (inst.instruction & (1 << 23))
11347             inst.instruction = 0x00800000; /* ia -> [base] */
11348           else
11349             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11350         }
11351
11352       inst.instruction |= 0xf8400000;
11353       if (load)
11354         inst.instruction |= 0x00100000;
11355
11356       mask = ffs (mask) - 1;
11357       mask <<= 12;
11358     }
11359   else if (writeback)
11360     inst.instruction |= WRITE_BACK;
11361
11362   inst.instruction |= mask;
11363   inst.instruction |= base << 16;
11364 }
11365
11366 static void
11367 do_t_ldmstm (void)
11368 {
11369   /* This really doesn't seem worth it.  */
11370   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11371               _("expression too complex"));
11372   constraint (inst.operands[1].writeback,
11373               _("Thumb load/store multiple does not support {reglist}^"));
11374
11375   if (unified_syntax)
11376     {
11377       bfd_boolean narrow;
11378       unsigned mask;
11379
11380       narrow = FALSE;
11381       /* See if we can use a 16-bit instruction.  */
11382       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11383           && inst.size_req != 4
11384           && !(inst.operands[1].imm & ~0xff))
11385         {
11386           mask = 1 << inst.operands[0].reg;
11387
11388           if (inst.operands[0].reg <= 7)
11389             {
11390               if (inst.instruction == T_MNEM_stmia
11391                   ? inst.operands[0].writeback
11392                   : (inst.operands[0].writeback
11393                      == !(inst.operands[1].imm & mask)))
11394                 {
11395                   if (inst.instruction == T_MNEM_stmia
11396                       && (inst.operands[1].imm & mask)
11397                       && (inst.operands[1].imm & (mask - 1)))
11398                     as_warn (_("value stored for r%d is UNKNOWN"),
11399                              inst.operands[0].reg);
11400
11401                   inst.instruction = THUMB_OP16 (inst.instruction);
11402                   inst.instruction |= inst.operands[0].reg << 8;
11403                   inst.instruction |= inst.operands[1].imm;
11404                   narrow = TRUE;
11405                 }
11406               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11407                 {
11408                   /* This means 1 register in reg list one of 3 situations:
11409                      1. Instruction is stmia, but without writeback.
11410                      2. lmdia without writeback, but with Rn not in
11411                         reglist.
11412                      3. ldmia with writeback, but with Rn in reglist.
11413                      Case 3 is UNPREDICTABLE behaviour, so we handle
11414                      case 1 and 2 which can be converted into a 16-bit
11415                      str or ldr. The SP cases are handled below.  */
11416                   unsigned long opcode;
11417                   /* First, record an error for Case 3.  */
11418                   if (inst.operands[1].imm & mask
11419                       && inst.operands[0].writeback)
11420                     inst.error =
11421                         _("having the base register in the register list when "
11422                           "using write back is UNPREDICTABLE");
11423
11424                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11425                                                              : T_MNEM_ldr);
11426                   inst.instruction = THUMB_OP16 (opcode);
11427                   inst.instruction |= inst.operands[0].reg << 3;
11428                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11429                   narrow = TRUE;
11430                 }
11431             }
11432           else if (inst.operands[0] .reg == REG_SP)
11433             {
11434               if (inst.operands[0].writeback)
11435                 {
11436                   inst.instruction =
11437                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11438                                     ? T_MNEM_push : T_MNEM_pop);
11439                   inst.instruction |= inst.operands[1].imm;
11440                   narrow = TRUE;
11441                 }
11442               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11443                 {
11444                   inst.instruction =
11445                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11446                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11447                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11448                   narrow = TRUE;
11449                 }
11450             }
11451         }
11452
11453       if (!narrow)
11454         {
11455           if (inst.instruction < 0xffff)
11456             inst.instruction = THUMB_OP32 (inst.instruction);
11457
11458           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11459                                 inst.operands[0].writeback);
11460         }
11461     }
11462   else
11463     {
11464       constraint (inst.operands[0].reg > 7
11465                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11466       constraint (inst.instruction != T_MNEM_ldmia
11467                   && inst.instruction != T_MNEM_stmia,
11468                   _("Thumb-2 instruction only valid in unified syntax"));
11469       if (inst.instruction == T_MNEM_stmia)
11470         {
11471           if (!inst.operands[0].writeback)
11472             as_warn (_("this instruction will write back the base register"));
11473           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11474               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11475             as_warn (_("value stored for r%d is UNKNOWN"),
11476                      inst.operands[0].reg);
11477         }
11478       else
11479         {
11480           if (!inst.operands[0].writeback
11481               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11482             as_warn (_("this instruction will write back the base register"));
11483           else if (inst.operands[0].writeback
11484                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11485             as_warn (_("this instruction will not write back the base register"));
11486         }
11487
11488       inst.instruction = THUMB_OP16 (inst.instruction);
11489       inst.instruction |= inst.operands[0].reg << 8;
11490       inst.instruction |= inst.operands[1].imm;
11491     }
11492 }
11493
11494 static void
11495 do_t_ldrex (void)
11496 {
11497   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11498               || inst.operands[1].postind || inst.operands[1].writeback
11499               || inst.operands[1].immisreg || inst.operands[1].shifted
11500               || inst.operands[1].negative,
11501               BAD_ADDR_MODE);
11502
11503   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11504
11505   inst.instruction |= inst.operands[0].reg << 12;
11506   inst.instruction |= inst.operands[1].reg << 16;
11507   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11508 }
11509
11510 static void
11511 do_t_ldrexd (void)
11512 {
11513   if (!inst.operands[1].present)
11514     {
11515       constraint (inst.operands[0].reg == REG_LR,
11516                   _("r14 not allowed as first register "
11517                     "when second register is omitted"));
11518       inst.operands[1].reg = inst.operands[0].reg + 1;
11519     }
11520   constraint (inst.operands[0].reg == inst.operands[1].reg,
11521               BAD_OVERLAP);
11522
11523   inst.instruction |= inst.operands[0].reg << 12;
11524   inst.instruction |= inst.operands[1].reg << 8;
11525   inst.instruction |= inst.operands[2].reg << 16;
11526 }
11527
11528 static void
11529 do_t_ldst (void)
11530 {
11531   unsigned long opcode;
11532   int Rn;
11533
11534   if (inst.operands[0].isreg
11535       && !inst.operands[0].preind
11536       && inst.operands[0].reg == REG_PC)
11537     set_it_insn_type_last ();
11538
11539   opcode = inst.instruction;
11540   if (unified_syntax)
11541     {
11542       if (!inst.operands[1].isreg)
11543         {
11544           if (opcode <= 0xffff)
11545             inst.instruction = THUMB_OP32 (opcode);
11546           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11547             return;
11548         }
11549       if (inst.operands[1].isreg
11550           && !inst.operands[1].writeback
11551           && !inst.operands[1].shifted && !inst.operands[1].postind
11552           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11553           && opcode <= 0xffff
11554           && inst.size_req != 4)
11555         {
11556           /* Insn may have a 16-bit form.  */
11557           Rn = inst.operands[1].reg;
11558           if (inst.operands[1].immisreg)
11559             {
11560               inst.instruction = THUMB_OP16 (opcode);
11561               /* [Rn, Rik] */
11562               if (Rn <= 7 && inst.operands[1].imm <= 7)
11563                 goto op16;
11564               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11565                 reject_bad_reg (inst.operands[1].imm);
11566             }
11567           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11568                     && opcode != T_MNEM_ldrsb)
11569                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11570                    || (Rn == REG_SP && opcode == T_MNEM_str))
11571             {
11572               /* [Rn, #const] */
11573               if (Rn > 7)
11574                 {
11575                   if (Rn == REG_PC)
11576                     {
11577                       if (inst.reloc.pc_rel)
11578                         opcode = T_MNEM_ldr_pc2;
11579                       else
11580                         opcode = T_MNEM_ldr_pc;
11581                     }
11582                   else
11583                     {
11584                       if (opcode == T_MNEM_ldr)
11585                         opcode = T_MNEM_ldr_sp;
11586                       else
11587                         opcode = T_MNEM_str_sp;
11588                     }
11589                   inst.instruction = inst.operands[0].reg << 8;
11590                 }
11591               else
11592                 {
11593                   inst.instruction = inst.operands[0].reg;
11594                   inst.instruction |= inst.operands[1].reg << 3;
11595                 }
11596               inst.instruction |= THUMB_OP16 (opcode);
11597               if (inst.size_req == 2)
11598                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11599               else
11600                 inst.relax = opcode;
11601               return;
11602             }
11603         }
11604       /* Definitely a 32-bit variant.  */
11605
11606       /* Warning for Erratum 752419.  */
11607       if (opcode == T_MNEM_ldr
11608           && inst.operands[0].reg == REG_SP
11609           && inst.operands[1].writeback == 1
11610           && !inst.operands[1].immisreg)
11611         {
11612           if (no_cpu_selected ()
11613               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11614                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11615                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11616             as_warn (_("This instruction may be unpredictable "
11617                        "if executed on M-profile cores "
11618                        "with interrupts enabled."));
11619         }
11620
11621       /* Do some validations regarding addressing modes.  */
11622       if (inst.operands[1].immisreg)
11623         reject_bad_reg (inst.operands[1].imm);
11624
11625       constraint (inst.operands[1].writeback == 1
11626                   && inst.operands[0].reg == inst.operands[1].reg,
11627                   BAD_OVERLAP);
11628
11629       inst.instruction = THUMB_OP32 (opcode);
11630       inst.instruction |= inst.operands[0].reg << 12;
11631       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11632       check_ldr_r15_aligned ();
11633       return;
11634     }
11635
11636   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11637
11638   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11639     {
11640       /* Only [Rn,Rm] is acceptable.  */
11641       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11642       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11643                   || inst.operands[1].postind || inst.operands[1].shifted
11644                   || inst.operands[1].negative,
11645                   _("Thumb does not support this addressing mode"));
11646       inst.instruction = THUMB_OP16 (inst.instruction);
11647       goto op16;
11648     }
11649
11650   inst.instruction = THUMB_OP16 (inst.instruction);
11651   if (!inst.operands[1].isreg)
11652     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11653       return;
11654
11655   constraint (!inst.operands[1].preind
11656               || inst.operands[1].shifted
11657               || inst.operands[1].writeback,
11658               _("Thumb does not support this addressing mode"));
11659   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11660     {
11661       constraint (inst.instruction & 0x0600,
11662                   _("byte or halfword not valid for base register"));
11663       constraint (inst.operands[1].reg == REG_PC
11664                   && !(inst.instruction & THUMB_LOAD_BIT),
11665                   _("r15 based store not allowed"));
11666       constraint (inst.operands[1].immisreg,
11667                   _("invalid base register for register offset"));
11668
11669       if (inst.operands[1].reg == REG_PC)
11670         inst.instruction = T_OPCODE_LDR_PC;
11671       else if (inst.instruction & THUMB_LOAD_BIT)
11672         inst.instruction = T_OPCODE_LDR_SP;
11673       else
11674         inst.instruction = T_OPCODE_STR_SP;
11675
11676       inst.instruction |= inst.operands[0].reg << 8;
11677       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11678       return;
11679     }
11680
11681   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11682   if (!inst.operands[1].immisreg)
11683     {
11684       /* Immediate offset.  */
11685       inst.instruction |= inst.operands[0].reg;
11686       inst.instruction |= inst.operands[1].reg << 3;
11687       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11688       return;
11689     }
11690
11691   /* Register offset.  */
11692   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11693   constraint (inst.operands[1].negative,
11694               _("Thumb does not support this addressing mode"));
11695
11696  op16:
11697   switch (inst.instruction)
11698     {
11699     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11700     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11701     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11702     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11703     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11704     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11705     case 0x5600 /* ldrsb */:
11706     case 0x5e00 /* ldrsh */: break;
11707     default: abort ();
11708     }
11709
11710   inst.instruction |= inst.operands[0].reg;
11711   inst.instruction |= inst.operands[1].reg << 3;
11712   inst.instruction |= inst.operands[1].imm << 6;
11713 }
11714
11715 static void
11716 do_t_ldstd (void)
11717 {
11718   if (!inst.operands[1].present)
11719     {
11720       inst.operands[1].reg = inst.operands[0].reg + 1;
11721       constraint (inst.operands[0].reg == REG_LR,
11722                   _("r14 not allowed here"));
11723       constraint (inst.operands[0].reg == REG_R12,
11724                   _("r12 not allowed here"));
11725     }
11726
11727   if (inst.operands[2].writeback
11728       && (inst.operands[0].reg == inst.operands[2].reg
11729       || inst.operands[1].reg == inst.operands[2].reg))
11730     as_warn (_("base register written back, and overlaps "
11731                "one of transfer registers"));
11732
11733   inst.instruction |= inst.operands[0].reg << 12;
11734   inst.instruction |= inst.operands[1].reg << 8;
11735   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11736 }
11737
11738 static void
11739 do_t_ldstt (void)
11740 {
11741   inst.instruction |= inst.operands[0].reg << 12;
11742   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11743 }
11744
11745 static void
11746 do_t_mla (void)
11747 {
11748   unsigned Rd, Rn, Rm, Ra;
11749
11750   Rd = inst.operands[0].reg;
11751   Rn = inst.operands[1].reg;
11752   Rm = inst.operands[2].reg;
11753   Ra = inst.operands[3].reg;
11754
11755   reject_bad_reg (Rd);
11756   reject_bad_reg (Rn);
11757   reject_bad_reg (Rm);
11758   reject_bad_reg (Ra);
11759
11760   inst.instruction |= Rd << 8;
11761   inst.instruction |= Rn << 16;
11762   inst.instruction |= Rm;
11763   inst.instruction |= Ra << 12;
11764 }
11765
11766 static void
11767 do_t_mlal (void)
11768 {
11769   unsigned RdLo, RdHi, Rn, Rm;
11770
11771   RdLo = inst.operands[0].reg;
11772   RdHi = inst.operands[1].reg;
11773   Rn = inst.operands[2].reg;
11774   Rm = inst.operands[3].reg;
11775
11776   reject_bad_reg (RdLo);
11777   reject_bad_reg (RdHi);
11778   reject_bad_reg (Rn);
11779   reject_bad_reg (Rm);
11780
11781   inst.instruction |= RdLo << 12;
11782   inst.instruction |= RdHi << 8;
11783   inst.instruction |= Rn << 16;
11784   inst.instruction |= Rm;
11785 }
11786
11787 static void
11788 do_t_mov_cmp (void)
11789 {
11790   unsigned Rn, Rm;
11791
11792   Rn = inst.operands[0].reg;
11793   Rm = inst.operands[1].reg;
11794
11795   if (Rn == REG_PC)
11796     set_it_insn_type_last ();
11797
11798   if (unified_syntax)
11799     {
11800       int r0off = (inst.instruction == T_MNEM_mov
11801                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11802       unsigned long opcode;
11803       bfd_boolean narrow;
11804       bfd_boolean low_regs;
11805
11806       low_regs = (Rn <= 7 && Rm <= 7);
11807       opcode = inst.instruction;
11808       if (in_it_block ())
11809         narrow = opcode != T_MNEM_movs;
11810       else
11811         narrow = opcode != T_MNEM_movs || low_regs;
11812       if (inst.size_req == 4
11813           || inst.operands[1].shifted)
11814         narrow = FALSE;
11815
11816       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11817       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11818           && !inst.operands[1].shifted
11819           && Rn == REG_PC
11820           && Rm == REG_LR)
11821         {
11822           inst.instruction = T2_SUBS_PC_LR;
11823           return;
11824         }
11825
11826       if (opcode == T_MNEM_cmp)
11827         {
11828           constraint (Rn == REG_PC, BAD_PC);
11829           if (narrow)
11830             {
11831               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11832                  but valid.  */
11833               warn_deprecated_sp (Rm);
11834               /* R15 was documented as a valid choice for Rm in ARMv6,
11835                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11836                  tools reject R15, so we do too.  */
11837               constraint (Rm == REG_PC, BAD_PC);
11838             }
11839           else
11840             reject_bad_reg (Rm);
11841         }
11842       else if (opcode == T_MNEM_mov
11843                || opcode == T_MNEM_movs)
11844         {
11845           if (inst.operands[1].isreg)
11846             {
11847               if (opcode == T_MNEM_movs)
11848                 {
11849                   reject_bad_reg (Rn);
11850                   reject_bad_reg (Rm);
11851                 }
11852               else if (narrow)
11853                 {
11854                   /* This is mov.n.  */
11855                   if ((Rn == REG_SP || Rn == REG_PC)
11856                       && (Rm == REG_SP || Rm == REG_PC))
11857                     {
11858                       as_tsktsk (_("Use of r%u as a source register is "
11859                                  "deprecated when r%u is the destination "
11860                                  "register."), Rm, Rn);
11861                     }
11862                 }
11863               else
11864                 {
11865                   /* This is mov.w.  */
11866                   constraint (Rn == REG_PC, BAD_PC);
11867                   constraint (Rm == REG_PC, BAD_PC);
11868                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11869                 }
11870             }
11871           else
11872             reject_bad_reg (Rn);
11873         }
11874
11875       if (!inst.operands[1].isreg)
11876         {
11877           /* Immediate operand.  */
11878           if (!in_it_block () && opcode == T_MNEM_mov)
11879             narrow = 0;
11880           if (low_regs && narrow)
11881             {
11882               inst.instruction = THUMB_OP16 (opcode);
11883               inst.instruction |= Rn << 8;
11884               if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11885                   || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
11886                 {
11887                   if (inst.size_req == 2)
11888                     inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11889                   else
11890                     inst.relax = opcode;
11891                 }
11892             }
11893           else
11894             {
11895               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11896                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
11897                           THUMB1_RELOC_ONLY);
11898
11899               inst.instruction = THUMB_OP32 (inst.instruction);
11900               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11901               inst.instruction |= Rn << r0off;
11902               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11903             }
11904         }
11905       else if (inst.operands[1].shifted && inst.operands[1].immisreg
11906                && (inst.instruction == T_MNEM_mov
11907                    || inst.instruction == T_MNEM_movs))
11908         {
11909           /* Register shifts are encoded as separate shift instructions.  */
11910           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
11911
11912           if (in_it_block ())
11913             narrow = !flags;
11914           else
11915             narrow = flags;
11916
11917           if (inst.size_req == 4)
11918             narrow = FALSE;
11919
11920           if (!low_regs || inst.operands[1].imm > 7)
11921             narrow = FALSE;
11922
11923           if (Rn != Rm)
11924             narrow = FALSE;
11925
11926           switch (inst.operands[1].shift_kind)
11927             {
11928             case SHIFT_LSL:
11929               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
11930               break;
11931             case SHIFT_ASR:
11932               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
11933               break;
11934             case SHIFT_LSR:
11935               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
11936               break;
11937             case SHIFT_ROR:
11938               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
11939               break;
11940             default:
11941               abort ();
11942             }
11943
11944           inst.instruction = opcode;
11945           if (narrow)
11946             {
11947               inst.instruction |= Rn;
11948               inst.instruction |= inst.operands[1].imm << 3;
11949             }
11950           else
11951             {
11952               if (flags)
11953                 inst.instruction |= CONDS_BIT;
11954
11955               inst.instruction |= Rn << 8;
11956               inst.instruction |= Rm << 16;
11957               inst.instruction |= inst.operands[1].imm;
11958             }
11959         }
11960       else if (!narrow)
11961         {
11962           /* Some mov with immediate shift have narrow variants.
11963              Register shifts are handled above.  */
11964           if (low_regs && inst.operands[1].shifted
11965               && (inst.instruction == T_MNEM_mov
11966                   || inst.instruction == T_MNEM_movs))
11967             {
11968               if (in_it_block ())
11969                 narrow = (inst.instruction == T_MNEM_mov);
11970               else
11971                 narrow = (inst.instruction == T_MNEM_movs);
11972             }
11973
11974           if (narrow)
11975             {
11976               switch (inst.operands[1].shift_kind)
11977                 {
11978                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11979                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11980                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11981                 default: narrow = FALSE; break;
11982                 }
11983             }
11984
11985           if (narrow)
11986             {
11987               inst.instruction |= Rn;
11988               inst.instruction |= Rm << 3;
11989               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11990             }
11991           else
11992             {
11993               inst.instruction = THUMB_OP32 (inst.instruction);
11994               inst.instruction |= Rn << r0off;
11995               encode_thumb32_shifted_operand (1);
11996             }
11997         }
11998       else
11999         switch (inst.instruction)
12000           {
12001           case T_MNEM_mov:
12002             /* In v4t or v5t a move of two lowregs produces unpredictable
12003                results. Don't allow this.  */
12004             if (low_regs)
12005               {
12006                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12007                             "MOV Rd, Rs with two low registers is not "
12008                             "permitted on this architecture");
12009                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12010                                         arm_ext_v6);
12011               }
12012
12013             inst.instruction = T_OPCODE_MOV_HR;
12014             inst.instruction |= (Rn & 0x8) << 4;
12015             inst.instruction |= (Rn & 0x7);
12016             inst.instruction |= Rm << 3;
12017             break;
12018
12019           case T_MNEM_movs:
12020             /* We know we have low registers at this point.
12021                Generate LSLS Rd, Rs, #0.  */
12022             inst.instruction = T_OPCODE_LSL_I;
12023             inst.instruction |= Rn;
12024             inst.instruction |= Rm << 3;
12025             break;
12026
12027           case T_MNEM_cmp:
12028             if (low_regs)
12029               {
12030                 inst.instruction = T_OPCODE_CMP_LR;
12031                 inst.instruction |= Rn;
12032                 inst.instruction |= Rm << 3;
12033               }
12034             else
12035               {
12036                 inst.instruction = T_OPCODE_CMP_HR;
12037                 inst.instruction |= (Rn & 0x8) << 4;
12038                 inst.instruction |= (Rn & 0x7);
12039                 inst.instruction |= Rm << 3;
12040               }
12041             break;
12042           }
12043       return;
12044     }
12045
12046   inst.instruction = THUMB_OP16 (inst.instruction);
12047
12048   /* PR 10443: Do not silently ignore shifted operands.  */
12049   constraint (inst.operands[1].shifted,
12050               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12051
12052   if (inst.operands[1].isreg)
12053     {
12054       if (Rn < 8 && Rm < 8)
12055         {
12056           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12057              since a MOV instruction produces unpredictable results.  */
12058           if (inst.instruction == T_OPCODE_MOV_I8)
12059             inst.instruction = T_OPCODE_ADD_I3;
12060           else
12061             inst.instruction = T_OPCODE_CMP_LR;
12062
12063           inst.instruction |= Rn;
12064           inst.instruction |= Rm << 3;
12065         }
12066       else
12067         {
12068           if (inst.instruction == T_OPCODE_MOV_I8)
12069             inst.instruction = T_OPCODE_MOV_HR;
12070           else
12071             inst.instruction = T_OPCODE_CMP_HR;
12072           do_t_cpy ();
12073         }
12074     }
12075   else
12076     {
12077       constraint (Rn > 7,
12078                   _("only lo regs allowed with immediate"));
12079       inst.instruction |= Rn << 8;
12080       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12081     }
12082 }
12083
12084 static void
12085 do_t_mov16 (void)
12086 {
12087   unsigned Rd;
12088   bfd_vma imm;
12089   bfd_boolean top;
12090
12091   top = (inst.instruction & 0x00800000) != 0;
12092   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
12093     {
12094       constraint (top, _(":lower16: not allowed this instruction"));
12095       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
12096     }
12097   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
12098     {
12099       constraint (!top, _(":upper16: not allowed this instruction"));
12100       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
12101     }
12102
12103   Rd = inst.operands[0].reg;
12104   reject_bad_reg (Rd);
12105
12106   inst.instruction |= Rd << 8;
12107   if (inst.reloc.type == BFD_RELOC_UNUSED)
12108     {
12109       imm = inst.reloc.exp.X_add_number;
12110       inst.instruction |= (imm & 0xf000) << 4;
12111       inst.instruction |= (imm & 0x0800) << 15;
12112       inst.instruction |= (imm & 0x0700) << 4;
12113       inst.instruction |= (imm & 0x00ff);
12114     }
12115 }
12116
12117 static void
12118 do_t_mvn_tst (void)
12119 {
12120   unsigned Rn, Rm;
12121
12122   Rn = inst.operands[0].reg;
12123   Rm = inst.operands[1].reg;
12124
12125   if (inst.instruction == T_MNEM_cmp
12126       || inst.instruction == T_MNEM_cmn)
12127     constraint (Rn == REG_PC, BAD_PC);
12128   else
12129     reject_bad_reg (Rn);
12130   reject_bad_reg (Rm);
12131
12132   if (unified_syntax)
12133     {
12134       int r0off = (inst.instruction == T_MNEM_mvn
12135                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12136       bfd_boolean narrow;
12137
12138       if (inst.size_req == 4
12139           || inst.instruction > 0xffff
12140           || inst.operands[1].shifted
12141           || Rn > 7 || Rm > 7)
12142         narrow = FALSE;
12143       else if (inst.instruction == T_MNEM_cmn
12144                || inst.instruction == T_MNEM_tst)
12145         narrow = TRUE;
12146       else if (THUMB_SETS_FLAGS (inst.instruction))
12147         narrow = !in_it_block ();
12148       else
12149         narrow = in_it_block ();
12150
12151       if (!inst.operands[1].isreg)
12152         {
12153           /* For an immediate, we always generate a 32-bit opcode;
12154              section relaxation will shrink it later if possible.  */
12155           if (inst.instruction < 0xffff)
12156             inst.instruction = THUMB_OP32 (inst.instruction);
12157           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12158           inst.instruction |= Rn << r0off;
12159           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12160         }
12161       else
12162         {
12163           /* See if we can do this with a 16-bit instruction.  */
12164           if (narrow)
12165             {
12166               inst.instruction = THUMB_OP16 (inst.instruction);
12167               inst.instruction |= Rn;
12168               inst.instruction |= Rm << 3;
12169             }
12170           else
12171             {
12172               constraint (inst.operands[1].shifted
12173                           && inst.operands[1].immisreg,
12174                           _("shift must be constant"));
12175               if (inst.instruction < 0xffff)
12176                 inst.instruction = THUMB_OP32 (inst.instruction);
12177               inst.instruction |= Rn << r0off;
12178               encode_thumb32_shifted_operand (1);
12179             }
12180         }
12181     }
12182   else
12183     {
12184       constraint (inst.instruction > 0xffff
12185                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12186       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12187                   _("unshifted register required"));
12188       constraint (Rn > 7 || Rm > 7,
12189                   BAD_HIREG);
12190
12191       inst.instruction = THUMB_OP16 (inst.instruction);
12192       inst.instruction |= Rn;
12193       inst.instruction |= Rm << 3;
12194     }
12195 }
12196
12197 static void
12198 do_t_mrs (void)
12199 {
12200   unsigned Rd;
12201
12202   if (do_vfp_nsyn_mrs () == SUCCESS)
12203     return;
12204
12205   Rd = inst.operands[0].reg;
12206   reject_bad_reg (Rd);
12207   inst.instruction |= Rd << 8;
12208
12209   if (inst.operands[1].isreg)
12210     {
12211       unsigned br = inst.operands[1].reg;
12212       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12213         as_bad (_("bad register for mrs"));
12214
12215       inst.instruction |= br & (0xf << 16);
12216       inst.instruction |= (br & 0x300) >> 4;
12217       inst.instruction |= (br & SPSR_BIT) >> 2;
12218     }
12219   else
12220     {
12221       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12222
12223       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12224         {
12225           /* PR gas/12698:  The constraint is only applied for m_profile.
12226              If the user has specified -march=all, we want to ignore it as
12227              we are building for any CPU type, including non-m variants.  */
12228           bfd_boolean m_profile =
12229             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12230           constraint ((flags != 0) && m_profile, _("selected processor does "
12231                                                    "not support requested special purpose register"));
12232         }
12233       else
12234         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12235            devices).  */
12236         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12237                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12238
12239       inst.instruction |= (flags & SPSR_BIT) >> 2;
12240       inst.instruction |= inst.operands[1].imm & 0xff;
12241       inst.instruction |= 0xf0000;
12242     }
12243 }
12244
12245 static void
12246 do_t_msr (void)
12247 {
12248   int flags;
12249   unsigned Rn;
12250
12251   if (do_vfp_nsyn_msr () == SUCCESS)
12252     return;
12253
12254   constraint (!inst.operands[1].isreg,
12255               _("Thumb encoding does not support an immediate here"));
12256
12257   if (inst.operands[0].isreg)
12258     flags = (int)(inst.operands[0].reg);
12259   else
12260     flags = inst.operands[0].imm;
12261
12262   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12263     {
12264       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12265
12266       /* PR gas/12698:  The constraint is only applied for m_profile.
12267          If the user has specified -march=all, we want to ignore it as
12268          we are building for any CPU type, including non-m variants.  */
12269       bfd_boolean m_profile =
12270         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12271       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12272            && (bits & ~(PSR_s | PSR_f)) != 0)
12273           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12274               && bits != PSR_f)) && m_profile,
12275           _("selected processor does not support requested special "
12276             "purpose register"));
12277     }
12278   else
12279      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12280                  "requested special purpose register"));
12281
12282   Rn = inst.operands[1].reg;
12283   reject_bad_reg (Rn);
12284
12285   inst.instruction |= (flags & SPSR_BIT) >> 2;
12286   inst.instruction |= (flags & 0xf0000) >> 8;
12287   inst.instruction |= (flags & 0x300) >> 4;
12288   inst.instruction |= (flags & 0xff);
12289   inst.instruction |= Rn << 16;
12290 }
12291
12292 static void
12293 do_t_mul (void)
12294 {
12295   bfd_boolean narrow;
12296   unsigned Rd, Rn, Rm;
12297
12298   if (!inst.operands[2].present)
12299     inst.operands[2].reg = inst.operands[0].reg;
12300
12301   Rd = inst.operands[0].reg;
12302   Rn = inst.operands[1].reg;
12303   Rm = inst.operands[2].reg;
12304
12305   if (unified_syntax)
12306     {
12307       if (inst.size_req == 4
12308           || (Rd != Rn
12309               && Rd != Rm)
12310           || Rn > 7
12311           || Rm > 7)
12312         narrow = FALSE;
12313       else if (inst.instruction == T_MNEM_muls)
12314         narrow = !in_it_block ();
12315       else
12316         narrow = in_it_block ();
12317     }
12318   else
12319     {
12320       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12321       constraint (Rn > 7 || Rm > 7,
12322                   BAD_HIREG);
12323       narrow = TRUE;
12324     }
12325
12326   if (narrow)
12327     {
12328       /* 16-bit MULS/Conditional MUL.  */
12329       inst.instruction = THUMB_OP16 (inst.instruction);
12330       inst.instruction |= Rd;
12331
12332       if (Rd == Rn)
12333         inst.instruction |= Rm << 3;
12334       else if (Rd == Rm)
12335         inst.instruction |= Rn << 3;
12336       else
12337         constraint (1, _("dest must overlap one source register"));
12338     }
12339   else
12340     {
12341       constraint (inst.instruction != T_MNEM_mul,
12342                   _("Thumb-2 MUL must not set flags"));
12343       /* 32-bit MUL.  */
12344       inst.instruction = THUMB_OP32 (inst.instruction);
12345       inst.instruction |= Rd << 8;
12346       inst.instruction |= Rn << 16;
12347       inst.instruction |= Rm << 0;
12348
12349       reject_bad_reg (Rd);
12350       reject_bad_reg (Rn);
12351       reject_bad_reg (Rm);
12352     }
12353 }
12354
12355 static void
12356 do_t_mull (void)
12357 {
12358   unsigned RdLo, RdHi, Rn, Rm;
12359
12360   RdLo = inst.operands[0].reg;
12361   RdHi = inst.operands[1].reg;
12362   Rn = inst.operands[2].reg;
12363   Rm = inst.operands[3].reg;
12364
12365   reject_bad_reg (RdLo);
12366   reject_bad_reg (RdHi);
12367   reject_bad_reg (Rn);
12368   reject_bad_reg (Rm);
12369
12370   inst.instruction |= RdLo << 12;
12371   inst.instruction |= RdHi << 8;
12372   inst.instruction |= Rn << 16;
12373   inst.instruction |= Rm;
12374
12375  if (RdLo == RdHi)
12376     as_tsktsk (_("rdhi and rdlo must be different"));
12377 }
12378
12379 static void
12380 do_t_nop (void)
12381 {
12382   set_it_insn_type (NEUTRAL_IT_INSN);
12383
12384   if (unified_syntax)
12385     {
12386       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12387         {
12388           inst.instruction = THUMB_OP32 (inst.instruction);
12389           inst.instruction |= inst.operands[0].imm;
12390         }
12391       else
12392         {
12393           /* PR9722: Check for Thumb2 availability before
12394              generating a thumb2 nop instruction.  */
12395           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12396             {
12397               inst.instruction = THUMB_OP16 (inst.instruction);
12398               inst.instruction |= inst.operands[0].imm << 4;
12399             }
12400           else
12401             inst.instruction = 0x46c0;
12402         }
12403     }
12404   else
12405     {
12406       constraint (inst.operands[0].present,
12407                   _("Thumb does not support NOP with hints"));
12408       inst.instruction = 0x46c0;
12409     }
12410 }
12411
12412 static void
12413 do_t_neg (void)
12414 {
12415   if (unified_syntax)
12416     {
12417       bfd_boolean narrow;
12418
12419       if (THUMB_SETS_FLAGS (inst.instruction))
12420         narrow = !in_it_block ();
12421       else
12422         narrow = in_it_block ();
12423       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12424         narrow = FALSE;
12425       if (inst.size_req == 4)
12426         narrow = FALSE;
12427
12428       if (!narrow)
12429         {
12430           inst.instruction = THUMB_OP32 (inst.instruction);
12431           inst.instruction |= inst.operands[0].reg << 8;
12432           inst.instruction |= inst.operands[1].reg << 16;
12433         }
12434       else
12435         {
12436           inst.instruction = THUMB_OP16 (inst.instruction);
12437           inst.instruction |= inst.operands[0].reg;
12438           inst.instruction |= inst.operands[1].reg << 3;
12439         }
12440     }
12441   else
12442     {
12443       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12444                   BAD_HIREG);
12445       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12446
12447       inst.instruction = THUMB_OP16 (inst.instruction);
12448       inst.instruction |= inst.operands[0].reg;
12449       inst.instruction |= inst.operands[1].reg << 3;
12450     }
12451 }
12452
12453 static void
12454 do_t_orn (void)
12455 {
12456   unsigned Rd, Rn;
12457
12458   Rd = inst.operands[0].reg;
12459   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12460
12461   reject_bad_reg (Rd);
12462   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12463   reject_bad_reg (Rn);
12464
12465   inst.instruction |= Rd << 8;
12466   inst.instruction |= Rn << 16;
12467
12468   if (!inst.operands[2].isreg)
12469     {
12470       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12471       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12472     }
12473   else
12474     {
12475       unsigned Rm;
12476
12477       Rm = inst.operands[2].reg;
12478       reject_bad_reg (Rm);
12479
12480       constraint (inst.operands[2].shifted
12481                   && inst.operands[2].immisreg,
12482                   _("shift must be constant"));
12483       encode_thumb32_shifted_operand (2);
12484     }
12485 }
12486
12487 static void
12488 do_t_pkhbt (void)
12489 {
12490   unsigned Rd, Rn, Rm;
12491
12492   Rd = inst.operands[0].reg;
12493   Rn = inst.operands[1].reg;
12494   Rm = inst.operands[2].reg;
12495
12496   reject_bad_reg (Rd);
12497   reject_bad_reg (Rn);
12498   reject_bad_reg (Rm);
12499
12500   inst.instruction |= Rd << 8;
12501   inst.instruction |= Rn << 16;
12502   inst.instruction |= Rm;
12503   if (inst.operands[3].present)
12504     {
12505       unsigned int val = inst.reloc.exp.X_add_number;
12506       constraint (inst.reloc.exp.X_op != O_constant,
12507                   _("expression too complex"));
12508       inst.instruction |= (val & 0x1c) << 10;
12509       inst.instruction |= (val & 0x03) << 6;
12510     }
12511 }
12512
12513 static void
12514 do_t_pkhtb (void)
12515 {
12516   if (!inst.operands[3].present)
12517     {
12518       unsigned Rtmp;
12519
12520       inst.instruction &= ~0x00000020;
12521
12522       /* PR 10168.  Swap the Rm and Rn registers.  */
12523       Rtmp = inst.operands[1].reg;
12524       inst.operands[1].reg = inst.operands[2].reg;
12525       inst.operands[2].reg = Rtmp;
12526     }
12527   do_t_pkhbt ();
12528 }
12529
12530 static void
12531 do_t_pld (void)
12532 {
12533   if (inst.operands[0].immisreg)
12534     reject_bad_reg (inst.operands[0].imm);
12535
12536   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12537 }
12538
12539 static void
12540 do_t_push_pop (void)
12541 {
12542   unsigned mask;
12543
12544   constraint (inst.operands[0].writeback,
12545               _("push/pop do not support {reglist}^"));
12546   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12547               _("expression too complex"));
12548
12549   mask = inst.operands[0].imm;
12550   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12551     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12552   else if (inst.size_req != 4
12553            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
12554                                        ? REG_LR : REG_PC)))
12555     {
12556       inst.instruction = THUMB_OP16 (inst.instruction);
12557       inst.instruction |= THUMB_PP_PC_LR;
12558       inst.instruction |= mask & 0xff;
12559     }
12560   else if (unified_syntax)
12561     {
12562       inst.instruction = THUMB_OP32 (inst.instruction);
12563       encode_thumb2_ldmstm (13, mask, TRUE);
12564     }
12565   else
12566     {
12567       inst.error = _("invalid register list to push/pop instruction");
12568       return;
12569     }
12570 }
12571
12572 static void
12573 do_t_rbit (void)
12574 {
12575   unsigned Rd, Rm;
12576
12577   Rd = inst.operands[0].reg;
12578   Rm = inst.operands[1].reg;
12579
12580   reject_bad_reg (Rd);
12581   reject_bad_reg (Rm);
12582
12583   inst.instruction |= Rd << 8;
12584   inst.instruction |= Rm << 16;
12585   inst.instruction |= Rm;
12586 }
12587
12588 static void
12589 do_t_rev (void)
12590 {
12591   unsigned Rd, Rm;
12592
12593   Rd = inst.operands[0].reg;
12594   Rm = inst.operands[1].reg;
12595
12596   reject_bad_reg (Rd);
12597   reject_bad_reg (Rm);
12598
12599   if (Rd <= 7 && Rm <= 7
12600       && inst.size_req != 4)
12601     {
12602       inst.instruction = THUMB_OP16 (inst.instruction);
12603       inst.instruction |= Rd;
12604       inst.instruction |= Rm << 3;
12605     }
12606   else if (unified_syntax)
12607     {
12608       inst.instruction = THUMB_OP32 (inst.instruction);
12609       inst.instruction |= Rd << 8;
12610       inst.instruction |= Rm << 16;
12611       inst.instruction |= Rm;
12612     }
12613   else
12614     inst.error = BAD_HIREG;
12615 }
12616
12617 static void
12618 do_t_rrx (void)
12619 {
12620   unsigned Rd, Rm;
12621
12622   Rd = inst.operands[0].reg;
12623   Rm = inst.operands[1].reg;
12624
12625   reject_bad_reg (Rd);
12626   reject_bad_reg (Rm);
12627
12628   inst.instruction |= Rd << 8;
12629   inst.instruction |= Rm;
12630 }
12631
12632 static void
12633 do_t_rsb (void)
12634 {
12635   unsigned Rd, Rs;
12636
12637   Rd = inst.operands[0].reg;
12638   Rs = (inst.operands[1].present
12639         ? inst.operands[1].reg    /* Rd, Rs, foo */
12640         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12641
12642   reject_bad_reg (Rd);
12643   reject_bad_reg (Rs);
12644   if (inst.operands[2].isreg)
12645     reject_bad_reg (inst.operands[2].reg);
12646
12647   inst.instruction |= Rd << 8;
12648   inst.instruction |= Rs << 16;
12649   if (!inst.operands[2].isreg)
12650     {
12651       bfd_boolean narrow;
12652
12653       if ((inst.instruction & 0x00100000) != 0)
12654         narrow = !in_it_block ();
12655       else
12656         narrow = in_it_block ();
12657
12658       if (Rd > 7 || Rs > 7)
12659         narrow = FALSE;
12660
12661       if (inst.size_req == 4 || !unified_syntax)
12662         narrow = FALSE;
12663
12664       if (inst.reloc.exp.X_op != O_constant
12665           || inst.reloc.exp.X_add_number != 0)
12666         narrow = FALSE;
12667
12668       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12669          relaxation, but it doesn't seem worth the hassle.  */
12670       if (narrow)
12671         {
12672           inst.reloc.type = BFD_RELOC_UNUSED;
12673           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12674           inst.instruction |= Rs << 3;
12675           inst.instruction |= Rd;
12676         }
12677       else
12678         {
12679           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12680           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12681         }
12682     }
12683   else
12684     encode_thumb32_shifted_operand (2);
12685 }
12686
12687 static void
12688 do_t_setend (void)
12689 {
12690   if (warn_on_deprecated
12691       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12692       as_tsktsk (_("setend use is deprecated for ARMv8"));
12693
12694   set_it_insn_type (OUTSIDE_IT_INSN);
12695   if (inst.operands[0].imm)
12696     inst.instruction |= 0x8;
12697 }
12698
12699 static void
12700 do_t_shift (void)
12701 {
12702   if (!inst.operands[1].present)
12703     inst.operands[1].reg = inst.operands[0].reg;
12704
12705   if (unified_syntax)
12706     {
12707       bfd_boolean narrow;
12708       int shift_kind;
12709
12710       switch (inst.instruction)
12711         {
12712         case T_MNEM_asr:
12713         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12714         case T_MNEM_lsl:
12715         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12716         case T_MNEM_lsr:
12717         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12718         case T_MNEM_ror:
12719         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12720         default: abort ();
12721         }
12722
12723       if (THUMB_SETS_FLAGS (inst.instruction))
12724         narrow = !in_it_block ();
12725       else
12726         narrow = in_it_block ();
12727       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12728         narrow = FALSE;
12729       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12730         narrow = FALSE;
12731       if (inst.operands[2].isreg
12732           && (inst.operands[1].reg != inst.operands[0].reg
12733               || inst.operands[2].reg > 7))
12734         narrow = FALSE;
12735       if (inst.size_req == 4)
12736         narrow = FALSE;
12737
12738       reject_bad_reg (inst.operands[0].reg);
12739       reject_bad_reg (inst.operands[1].reg);
12740
12741       if (!narrow)
12742         {
12743           if (inst.operands[2].isreg)
12744             {
12745               reject_bad_reg (inst.operands[2].reg);
12746               inst.instruction = THUMB_OP32 (inst.instruction);
12747               inst.instruction |= inst.operands[0].reg << 8;
12748               inst.instruction |= inst.operands[1].reg << 16;
12749               inst.instruction |= inst.operands[2].reg;
12750
12751               /* PR 12854: Error on extraneous shifts.  */
12752               constraint (inst.operands[2].shifted,
12753                           _("extraneous shift as part of operand to shift insn"));
12754             }
12755           else
12756             {
12757               inst.operands[1].shifted = 1;
12758               inst.operands[1].shift_kind = shift_kind;
12759               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12760                                              ? T_MNEM_movs : T_MNEM_mov);
12761               inst.instruction |= inst.operands[0].reg << 8;
12762               encode_thumb32_shifted_operand (1);
12763               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12764               inst.reloc.type = BFD_RELOC_UNUSED;
12765             }
12766         }
12767       else
12768         {
12769           if (inst.operands[2].isreg)
12770             {
12771               switch (shift_kind)
12772                 {
12773                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12774                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12775                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12776                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12777                 default: abort ();
12778                 }
12779
12780               inst.instruction |= inst.operands[0].reg;
12781               inst.instruction |= inst.operands[2].reg << 3;
12782
12783               /* PR 12854: Error on extraneous shifts.  */
12784               constraint (inst.operands[2].shifted,
12785                           _("extraneous shift as part of operand to shift insn"));
12786             }
12787           else
12788             {
12789               switch (shift_kind)
12790                 {
12791                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12792                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12793                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12794                 default: abort ();
12795                 }
12796               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12797               inst.instruction |= inst.operands[0].reg;
12798               inst.instruction |= inst.operands[1].reg << 3;
12799             }
12800         }
12801     }
12802   else
12803     {
12804       constraint (inst.operands[0].reg > 7
12805                   || inst.operands[1].reg > 7, BAD_HIREG);
12806       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12807
12808       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12809         {
12810           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12811           constraint (inst.operands[0].reg != inst.operands[1].reg,
12812                       _("source1 and dest must be same register"));
12813
12814           switch (inst.instruction)
12815             {
12816             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12817             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12818             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12819             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12820             default: abort ();
12821             }
12822
12823           inst.instruction |= inst.operands[0].reg;
12824           inst.instruction |= inst.operands[2].reg << 3;
12825
12826           /* PR 12854: Error on extraneous shifts.  */
12827           constraint (inst.operands[2].shifted,
12828                       _("extraneous shift as part of operand to shift insn"));
12829         }
12830       else
12831         {
12832           switch (inst.instruction)
12833             {
12834             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12835             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12836             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12837             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12838             default: abort ();
12839             }
12840           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12841           inst.instruction |= inst.operands[0].reg;
12842           inst.instruction |= inst.operands[1].reg << 3;
12843         }
12844     }
12845 }
12846
12847 static void
12848 do_t_simd (void)
12849 {
12850   unsigned Rd, Rn, Rm;
12851
12852   Rd = inst.operands[0].reg;
12853   Rn = inst.operands[1].reg;
12854   Rm = inst.operands[2].reg;
12855
12856   reject_bad_reg (Rd);
12857   reject_bad_reg (Rn);
12858   reject_bad_reg (Rm);
12859
12860   inst.instruction |= Rd << 8;
12861   inst.instruction |= Rn << 16;
12862   inst.instruction |= Rm;
12863 }
12864
12865 static void
12866 do_t_simd2 (void)
12867 {
12868   unsigned Rd, Rn, Rm;
12869
12870   Rd = inst.operands[0].reg;
12871   Rm = inst.operands[1].reg;
12872   Rn = inst.operands[2].reg;
12873
12874   reject_bad_reg (Rd);
12875   reject_bad_reg (Rn);
12876   reject_bad_reg (Rm);
12877
12878   inst.instruction |= Rd << 8;
12879   inst.instruction |= Rn << 16;
12880   inst.instruction |= Rm;
12881 }
12882
12883 static void
12884 do_t_smc (void)
12885 {
12886   unsigned int value = inst.reloc.exp.X_add_number;
12887   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
12888               _("SMC is not permitted on this architecture"));
12889   constraint (inst.reloc.exp.X_op != O_constant,
12890               _("expression too complex"));
12891   inst.reloc.type = BFD_RELOC_UNUSED;
12892   inst.instruction |= (value & 0xf000) >> 12;
12893   inst.instruction |= (value & 0x0ff0);
12894   inst.instruction |= (value & 0x000f) << 16;
12895   /* PR gas/15623: SMC instructions must be last in an IT block.  */
12896   set_it_insn_type_last ();
12897 }
12898
12899 static void
12900 do_t_hvc (void)
12901 {
12902   unsigned int value = inst.reloc.exp.X_add_number;
12903
12904   inst.reloc.type = BFD_RELOC_UNUSED;
12905   inst.instruction |= (value & 0x0fff);
12906   inst.instruction |= (value & 0xf000) << 4;
12907 }
12908
12909 static void
12910 do_t_ssat_usat (int bias)
12911 {
12912   unsigned Rd, Rn;
12913
12914   Rd = inst.operands[0].reg;
12915   Rn = inst.operands[2].reg;
12916
12917   reject_bad_reg (Rd);
12918   reject_bad_reg (Rn);
12919
12920   inst.instruction |= Rd << 8;
12921   inst.instruction |= inst.operands[1].imm - bias;
12922   inst.instruction |= Rn << 16;
12923
12924   if (inst.operands[3].present)
12925     {
12926       offsetT shift_amount = inst.reloc.exp.X_add_number;
12927
12928       inst.reloc.type = BFD_RELOC_UNUSED;
12929
12930       constraint (inst.reloc.exp.X_op != O_constant,
12931                   _("expression too complex"));
12932
12933       if (shift_amount != 0)
12934         {
12935           constraint (shift_amount > 31,
12936                       _("shift expression is too large"));
12937
12938           if (inst.operands[3].shift_kind == SHIFT_ASR)
12939             inst.instruction |= 0x00200000;  /* sh bit.  */
12940
12941           inst.instruction |= (shift_amount & 0x1c) << 10;
12942           inst.instruction |= (shift_amount & 0x03) << 6;
12943         }
12944     }
12945 }
12946
12947 static void
12948 do_t_ssat (void)
12949 {
12950   do_t_ssat_usat (1);
12951 }
12952
12953 static void
12954 do_t_ssat16 (void)
12955 {
12956   unsigned Rd, Rn;
12957
12958   Rd = inst.operands[0].reg;
12959   Rn = inst.operands[2].reg;
12960
12961   reject_bad_reg (Rd);
12962   reject_bad_reg (Rn);
12963
12964   inst.instruction |= Rd << 8;
12965   inst.instruction |= inst.operands[1].imm - 1;
12966   inst.instruction |= Rn << 16;
12967 }
12968
12969 static void
12970 do_t_strex (void)
12971 {
12972   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
12973               || inst.operands[2].postind || inst.operands[2].writeback
12974               || inst.operands[2].immisreg || inst.operands[2].shifted
12975               || inst.operands[2].negative,
12976               BAD_ADDR_MODE);
12977
12978   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
12979
12980   inst.instruction |= inst.operands[0].reg << 8;
12981   inst.instruction |= inst.operands[1].reg << 12;
12982   inst.instruction |= inst.operands[2].reg << 16;
12983   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
12984 }
12985
12986 static void
12987 do_t_strexd (void)
12988 {
12989   if (!inst.operands[2].present)
12990     inst.operands[2].reg = inst.operands[1].reg + 1;
12991
12992   constraint (inst.operands[0].reg == inst.operands[1].reg
12993               || inst.operands[0].reg == inst.operands[2].reg
12994               || inst.operands[0].reg == inst.operands[3].reg,
12995               BAD_OVERLAP);
12996
12997   inst.instruction |= inst.operands[0].reg;
12998   inst.instruction |= inst.operands[1].reg << 12;
12999   inst.instruction |= inst.operands[2].reg << 8;
13000   inst.instruction |= inst.operands[3].reg << 16;
13001 }
13002
13003 static void
13004 do_t_sxtah (void)
13005 {
13006   unsigned Rd, Rn, Rm;
13007
13008   Rd = inst.operands[0].reg;
13009   Rn = inst.operands[1].reg;
13010   Rm = inst.operands[2].reg;
13011
13012   reject_bad_reg (Rd);
13013   reject_bad_reg (Rn);
13014   reject_bad_reg (Rm);
13015
13016   inst.instruction |= Rd << 8;
13017   inst.instruction |= Rn << 16;
13018   inst.instruction |= Rm;
13019   inst.instruction |= inst.operands[3].imm << 4;
13020 }
13021
13022 static void
13023 do_t_sxth (void)
13024 {
13025   unsigned Rd, Rm;
13026
13027   Rd = inst.operands[0].reg;
13028   Rm = inst.operands[1].reg;
13029
13030   reject_bad_reg (Rd);
13031   reject_bad_reg (Rm);
13032
13033   if (inst.instruction <= 0xffff
13034       && inst.size_req != 4
13035       && Rd <= 7 && Rm <= 7
13036       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13037     {
13038       inst.instruction = THUMB_OP16 (inst.instruction);
13039       inst.instruction |= Rd;
13040       inst.instruction |= Rm << 3;
13041     }
13042   else if (unified_syntax)
13043     {
13044       if (inst.instruction <= 0xffff)
13045         inst.instruction = THUMB_OP32 (inst.instruction);
13046       inst.instruction |= Rd << 8;
13047       inst.instruction |= Rm;
13048       inst.instruction |= inst.operands[2].imm << 4;
13049     }
13050   else
13051     {
13052       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13053                   _("Thumb encoding does not support rotation"));
13054       constraint (1, BAD_HIREG);
13055     }
13056 }
13057
13058 static void
13059 do_t_swi (void)
13060 {
13061   /* We have to do the following check manually as ARM_EXT_OS only applies
13062      to ARM_EXT_V6M.  */
13063   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
13064     {
13065       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
13066           /* This only applies to the v6m howver, not later architectures.  */
13067           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
13068         as_bad (_("SVC is not permitted on this architecture"));
13069       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
13070     }
13071
13072   inst.reloc.type = BFD_RELOC_ARM_SWI;
13073 }
13074
13075 static void
13076 do_t_tb (void)
13077 {
13078   unsigned Rn, Rm;
13079   int half;
13080
13081   half = (inst.instruction & 0x10) != 0;
13082   set_it_insn_type_last ();
13083   constraint (inst.operands[0].immisreg,
13084               _("instruction requires register index"));
13085
13086   Rn = inst.operands[0].reg;
13087   Rm = inst.operands[0].imm;
13088
13089   constraint (Rn == REG_SP, BAD_SP);
13090   reject_bad_reg (Rm);
13091
13092   constraint (!half && inst.operands[0].shifted,
13093               _("instruction does not allow shifted index"));
13094   inst.instruction |= (Rn << 16) | Rm;
13095 }
13096
13097 static void
13098 do_t_udf (void)
13099 {
13100   if (!inst.operands[0].present)
13101     inst.operands[0].imm = 0;
13102
13103   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13104     {
13105       constraint (inst.size_req == 2,
13106                   _("immediate value out of range"));
13107       inst.instruction = THUMB_OP32 (inst.instruction);
13108       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13109       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13110     }
13111   else
13112     {
13113       inst.instruction = THUMB_OP16 (inst.instruction);
13114       inst.instruction |= inst.operands[0].imm;
13115     }
13116
13117   set_it_insn_type (NEUTRAL_IT_INSN);
13118 }
13119
13120
13121 static void
13122 do_t_usat (void)
13123 {
13124   do_t_ssat_usat (0);
13125 }
13126
13127 static void
13128 do_t_usat16 (void)
13129 {
13130   unsigned Rd, Rn;
13131
13132   Rd = inst.operands[0].reg;
13133   Rn = inst.operands[2].reg;
13134
13135   reject_bad_reg (Rd);
13136   reject_bad_reg (Rn);
13137
13138   inst.instruction |= Rd << 8;
13139   inst.instruction |= inst.operands[1].imm;
13140   inst.instruction |= Rn << 16;
13141 }
13142
13143 /* Neon instruction encoder helpers.  */
13144
13145 /* Encodings for the different types for various Neon opcodes.  */
13146
13147 /* An "invalid" code for the following tables.  */
13148 #define N_INV -1u
13149
13150 struct neon_tab_entry
13151 {
13152   unsigned integer;
13153   unsigned float_or_poly;
13154   unsigned scalar_or_imm;
13155 };
13156
13157 /* Map overloaded Neon opcodes to their respective encodings.  */
13158 #define NEON_ENC_TAB                                    \
13159   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13160   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13161   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13162   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13163   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13164   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13165   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13166   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13167   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13168   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13169   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13170   /* Register variants of the following two instructions are encoded as
13171      vcge / vcgt with the operands reversed.  */        \
13172   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13173   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13174   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13175   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13176   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13177   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13178   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13179   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13180   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13181   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13182   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13183   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13184   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13185   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13186   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13187   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13188   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13189   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13190   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13191   X(vand,       0x0000110, N_INV,     0x0800030),       \
13192   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13193   X(veor,       0x1000110, N_INV,     N_INV),           \
13194   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13195   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13196   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13197   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13198   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13199   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13200   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13201   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13202   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13203   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13204   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13205   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13206   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13207   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13208   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13209   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13210   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13211   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13212   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13213   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13214   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13215   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13216   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13217   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13218   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13219   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13220   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13221   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13222   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13223   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13224   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13225   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13226   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13227   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13228   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13229   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13230   X(aes,        0x3b00300, N_INV,     N_INV),           \
13231   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13232   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13233   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13234
13235 enum neon_opc
13236 {
13237 #define X(OPC,I,F,S) N_MNEM_##OPC
13238 NEON_ENC_TAB
13239 #undef X
13240 };
13241
13242 static const struct neon_tab_entry neon_enc_tab[] =
13243 {
13244 #define X(OPC,I,F,S) { (I), (F), (S) }
13245 NEON_ENC_TAB
13246 #undef X
13247 };
13248
13249 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13250 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13251 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13252 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13253 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13254 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13255 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13256 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13257 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13258 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13259 #define NEON_ENC_SINGLE_(X) \
13260   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13261 #define NEON_ENC_DOUBLE_(X) \
13262   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13263 #define NEON_ENC_FPV8_(X) \
13264   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13265
13266 #define NEON_ENCODE(type, inst)                                 \
13267   do                                                            \
13268     {                                                           \
13269       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13270       inst.is_neon = 1;                                         \
13271     }                                                           \
13272   while (0)
13273
13274 #define check_neon_suffixes                                             \
13275   do                                                                    \
13276     {                                                                   \
13277       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13278         {                                                               \
13279           as_bad (_("invalid neon suffix for non neon instruction"));   \
13280           return;                                                       \
13281         }                                                               \
13282     }                                                                   \
13283   while (0)
13284
13285 /* Define shapes for instruction operands. The following mnemonic characters
13286    are used in this table:
13287
13288      F - VFP S<n> register
13289      D - Neon D<n> register
13290      Q - Neon Q<n> register
13291      I - Immediate
13292      S - Scalar
13293      R - ARM register
13294      L - D<n> register list
13295
13296    This table is used to generate various data:
13297      - enumerations of the form NS_DDR to be used as arguments to
13298        neon_select_shape.
13299      - a table classifying shapes into single, double, quad, mixed.
13300      - a table used to drive neon_select_shape.  */
13301
13302 #define NEON_SHAPE_DEF                  \
13303   X(3, (D, D, D), DOUBLE),              \
13304   X(3, (Q, Q, Q), QUAD),                \
13305   X(3, (D, D, I), DOUBLE),              \
13306   X(3, (Q, Q, I), QUAD),                \
13307   X(3, (D, D, S), DOUBLE),              \
13308   X(3, (Q, Q, S), QUAD),                \
13309   X(2, (D, D), DOUBLE),                 \
13310   X(2, (Q, Q), QUAD),                   \
13311   X(2, (D, S), DOUBLE),                 \
13312   X(2, (Q, S), QUAD),                   \
13313   X(2, (D, R), DOUBLE),                 \
13314   X(2, (Q, R), QUAD),                   \
13315   X(2, (D, I), DOUBLE),                 \
13316   X(2, (Q, I), QUAD),                   \
13317   X(3, (D, L, D), DOUBLE),              \
13318   X(2, (D, Q), MIXED),                  \
13319   X(2, (Q, D), MIXED),                  \
13320   X(3, (D, Q, I), MIXED),               \
13321   X(3, (Q, D, I), MIXED),               \
13322   X(3, (Q, D, D), MIXED),               \
13323   X(3, (D, Q, Q), MIXED),               \
13324   X(3, (Q, Q, D), MIXED),               \
13325   X(3, (Q, D, S), MIXED),               \
13326   X(3, (D, Q, S), MIXED),               \
13327   X(4, (D, D, D, I), DOUBLE),           \
13328   X(4, (Q, Q, Q, I), QUAD),             \
13329   X(2, (F, F), SINGLE),                 \
13330   X(3, (F, F, F), SINGLE),              \
13331   X(2, (F, I), SINGLE),                 \
13332   X(2, (F, D), MIXED),                  \
13333   X(2, (D, F), MIXED),                  \
13334   X(3, (F, F, I), MIXED),               \
13335   X(4, (R, R, F, F), SINGLE),           \
13336   X(4, (F, F, R, R), SINGLE),           \
13337   X(3, (D, R, R), DOUBLE),              \
13338   X(3, (R, R, D), DOUBLE),              \
13339   X(2, (S, R), SINGLE),                 \
13340   X(2, (R, S), SINGLE),                 \
13341   X(2, (F, R), SINGLE),                 \
13342   X(2, (R, F), SINGLE),                 \
13343 /* Half float shape supported so far.  */\
13344   X (2, (H, D), MIXED),                 \
13345   X (2, (D, H), MIXED),                 \
13346   X (2, (H, F), MIXED),                 \
13347   X (2, (F, H), MIXED),                 \
13348   X (2, (H, H), HALF),                  \
13349   X (2, (H, R), HALF),                  \
13350   X (2, (R, H), HALF),                  \
13351   X (2, (H, I), HALF),                  \
13352   X (3, (H, H, H), HALF),               \
13353   X (3, (H, F, I), MIXED),              \
13354   X (3, (F, H, I), MIXED)
13355
13356 #define S2(A,B)         NS_##A##B
13357 #define S3(A,B,C)       NS_##A##B##C
13358 #define S4(A,B,C,D)     NS_##A##B##C##D
13359
13360 #define X(N, L, C) S##N L
13361
13362 enum neon_shape
13363 {
13364   NEON_SHAPE_DEF,
13365   NS_NULL
13366 };
13367
13368 #undef X
13369 #undef S2
13370 #undef S3
13371 #undef S4
13372
13373 enum neon_shape_class
13374 {
13375   SC_HALF,
13376   SC_SINGLE,
13377   SC_DOUBLE,
13378   SC_QUAD,
13379   SC_MIXED
13380 };
13381
13382 #define X(N, L, C) SC_##C
13383
13384 static enum neon_shape_class neon_shape_class[] =
13385 {
13386   NEON_SHAPE_DEF
13387 };
13388
13389 #undef X
13390
13391 enum neon_shape_el
13392 {
13393   SE_H,
13394   SE_F,
13395   SE_D,
13396   SE_Q,
13397   SE_I,
13398   SE_S,
13399   SE_R,
13400   SE_L
13401 };
13402
13403 /* Register widths of above.  */
13404 static unsigned neon_shape_el_size[] =
13405 {
13406   16,
13407   32,
13408   64,
13409   128,
13410   0,
13411   32,
13412   32,
13413   0
13414 };
13415
13416 struct neon_shape_info
13417 {
13418   unsigned els;
13419   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13420 };
13421
13422 #define S2(A,B)         { SE_##A, SE_##B }
13423 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13424 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13425
13426 #define X(N, L, C) { N, S##N L }
13427
13428 static struct neon_shape_info neon_shape_tab[] =
13429 {
13430   NEON_SHAPE_DEF
13431 };
13432
13433 #undef X
13434 #undef S2
13435 #undef S3
13436 #undef S4
13437
13438 /* Bit masks used in type checking given instructions.
13439   'N_EQK' means the type must be the same as (or based on in some way) the key
13440    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13441    set, various other bits can be set as well in order to modify the meaning of
13442    the type constraint.  */
13443
13444 enum neon_type_mask
13445 {
13446   N_S8   = 0x0000001,
13447   N_S16  = 0x0000002,
13448   N_S32  = 0x0000004,
13449   N_S64  = 0x0000008,
13450   N_U8   = 0x0000010,
13451   N_U16  = 0x0000020,
13452   N_U32  = 0x0000040,
13453   N_U64  = 0x0000080,
13454   N_I8   = 0x0000100,
13455   N_I16  = 0x0000200,
13456   N_I32  = 0x0000400,
13457   N_I64  = 0x0000800,
13458   N_8    = 0x0001000,
13459   N_16   = 0x0002000,
13460   N_32   = 0x0004000,
13461   N_64   = 0x0008000,
13462   N_P8   = 0x0010000,
13463   N_P16  = 0x0020000,
13464   N_F16  = 0x0040000,
13465   N_F32  = 0x0080000,
13466   N_F64  = 0x0100000,
13467   N_P64  = 0x0200000,
13468   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13469   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13470   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13471   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13472   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13473   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13474   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13475   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13476   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13477   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13478   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13479   N_UTYP = 0,
13480   N_MAX_NONSPECIAL = N_P64
13481 };
13482
13483 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13484
13485 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13486 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13487 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13488 #define N_S_32     (N_S8 | N_S16 | N_S32)
13489 #define N_F_16_32  (N_F16 | N_F32)
13490 #define N_SUF_32   (N_SU_32 | N_F_16_32)
13491 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13492 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
13493 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
13494
13495 /* Pass this as the first type argument to neon_check_type to ignore types
13496    altogether.  */
13497 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13498
13499 /* Select a "shape" for the current instruction (describing register types or
13500    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13501    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13502    function of operand parsing, so this function doesn't need to be called.
13503    Shapes should be listed in order of decreasing length.  */
13504
13505 static enum neon_shape
13506 neon_select_shape (enum neon_shape shape, ...)
13507 {
13508   va_list ap;
13509   enum neon_shape first_shape = shape;
13510
13511   /* Fix missing optional operands. FIXME: we don't know at this point how
13512      many arguments we should have, so this makes the assumption that we have
13513      > 1. This is true of all current Neon opcodes, I think, but may not be
13514      true in the future.  */
13515   if (!inst.operands[1].present)
13516     inst.operands[1] = inst.operands[0];
13517
13518   va_start (ap, shape);
13519
13520   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13521     {
13522       unsigned j;
13523       int matches = 1;
13524
13525       for (j = 0; j < neon_shape_tab[shape].els; j++)
13526         {
13527           if (!inst.operands[j].present)
13528             {
13529               matches = 0;
13530               break;
13531             }
13532
13533           switch (neon_shape_tab[shape].el[j])
13534             {
13535               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
13536                  a VFP single precision register operand, it's essentially
13537                  means only half of the register is used.
13538
13539                  If the type specifier is given after the mnemonics, the
13540                  information is stored in inst.vectype.  If the type specifier
13541                  is given after register operand, the information is stored
13542                  in inst.operands[].vectype.
13543
13544                  When there is only one type specifier, and all the register
13545                  operands are the same type of hardware register, the type
13546                  specifier applies to all register operands.
13547
13548                  If no type specifier is given, the shape is inferred from
13549                  operand information.
13550
13551                  for example:
13552                  vadd.f16 s0, s1, s2:           NS_HHH
13553                  vabs.f16 s0, s1:               NS_HH
13554                  vmov.f16 s0, r1:               NS_HR
13555                  vmov.f16 r0, s1:               NS_RH
13556                  vcvt.f16 r0, s1:               NS_RH
13557                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
13558                  vcvt.f16.s32   s2, s2:         NS_HF
13559               */
13560             case SE_H:
13561               if (!(inst.operands[j].isreg
13562                     && inst.operands[j].isvec
13563                     && inst.operands[j].issingle
13564                     && !inst.operands[j].isquad
13565                     && ((inst.vectype.elems == 1
13566                          && inst.vectype.el[0].size == 16)
13567                         || (inst.vectype.elems > 1
13568                             && inst.vectype.el[j].size == 16)
13569                         || (inst.vectype.elems == 0
13570                             && inst.operands[j].vectype.type != NT_invtype
13571                             && inst.operands[j].vectype.size == 16))))
13572                 matches = 0;
13573               break;
13574
13575             case SE_F:
13576               if (!(inst.operands[j].isreg
13577                     && inst.operands[j].isvec
13578                     && inst.operands[j].issingle
13579                     && !inst.operands[j].isquad
13580                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
13581                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
13582                         || (inst.vectype.elems == 0
13583                             && (inst.operands[j].vectype.size == 32
13584                                 || inst.operands[j].vectype.type == NT_invtype)))))
13585                 matches = 0;
13586               break;
13587
13588             case SE_D:
13589               if (!(inst.operands[j].isreg
13590                     && inst.operands[j].isvec
13591                     && !inst.operands[j].isquad
13592                     && !inst.operands[j].issingle))
13593                 matches = 0;
13594               break;
13595
13596             case SE_R:
13597               if (!(inst.operands[j].isreg
13598                     && !inst.operands[j].isvec))
13599                 matches = 0;
13600               break;
13601
13602             case SE_Q:
13603               if (!(inst.operands[j].isreg
13604                     && inst.operands[j].isvec
13605                     && inst.operands[j].isquad
13606                     && !inst.operands[j].issingle))
13607                 matches = 0;
13608               break;
13609
13610             case SE_I:
13611               if (!(!inst.operands[j].isreg
13612                     && !inst.operands[j].isscalar))
13613                 matches = 0;
13614               break;
13615
13616             case SE_S:
13617               if (!(!inst.operands[j].isreg
13618                     && inst.operands[j].isscalar))
13619                 matches = 0;
13620               break;
13621
13622             case SE_L:
13623               break;
13624             }
13625           if (!matches)
13626             break;
13627         }
13628       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13629         /* We've matched all the entries in the shape table, and we don't
13630            have any left over operands which have not been matched.  */
13631         break;
13632     }
13633
13634   va_end (ap);
13635
13636   if (shape == NS_NULL && first_shape != NS_NULL)
13637     first_error (_("invalid instruction shape"));
13638
13639   return shape;
13640 }
13641
13642 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13643    means the Q bit should be set).  */
13644
13645 static int
13646 neon_quad (enum neon_shape shape)
13647 {
13648   return neon_shape_class[shape] == SC_QUAD;
13649 }
13650
13651 static void
13652 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13653                        unsigned *g_size)
13654 {
13655   /* Allow modification to be made to types which are constrained to be
13656      based on the key element, based on bits set alongside N_EQK.  */
13657   if ((typebits & N_EQK) != 0)
13658     {
13659       if ((typebits & N_HLF) != 0)
13660         *g_size /= 2;
13661       else if ((typebits & N_DBL) != 0)
13662         *g_size *= 2;
13663       if ((typebits & N_SGN) != 0)
13664         *g_type = NT_signed;
13665       else if ((typebits & N_UNS) != 0)
13666         *g_type = NT_unsigned;
13667       else if ((typebits & N_INT) != 0)
13668         *g_type = NT_integer;
13669       else if ((typebits & N_FLT) != 0)
13670         *g_type = NT_float;
13671       else if ((typebits & N_SIZ) != 0)
13672         *g_type = NT_untyped;
13673     }
13674 }
13675
13676 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13677    operand type, i.e. the single type specified in a Neon instruction when it
13678    is the only one given.  */
13679
13680 static struct neon_type_el
13681 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13682 {
13683   struct neon_type_el dest = *key;
13684
13685   gas_assert ((thisarg & N_EQK) != 0);
13686
13687   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13688
13689   return dest;
13690 }
13691
13692 /* Convert Neon type and size into compact bitmask representation.  */
13693
13694 static enum neon_type_mask
13695 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13696 {
13697   switch (type)
13698     {
13699     case NT_untyped:
13700       switch (size)
13701         {
13702         case 8:  return N_8;
13703         case 16: return N_16;
13704         case 32: return N_32;
13705         case 64: return N_64;
13706         default: ;
13707         }
13708       break;
13709
13710     case NT_integer:
13711       switch (size)
13712         {
13713         case 8:  return N_I8;
13714         case 16: return N_I16;
13715         case 32: return N_I32;
13716         case 64: return N_I64;
13717         default: ;
13718         }
13719       break;
13720
13721     case NT_float:
13722       switch (size)
13723         {
13724         case 16: return N_F16;
13725         case 32: return N_F32;
13726         case 64: return N_F64;
13727         default: ;
13728         }
13729       break;
13730
13731     case NT_poly:
13732       switch (size)
13733         {
13734         case 8:  return N_P8;
13735         case 16: return N_P16;
13736         case 64: return N_P64;
13737         default: ;
13738         }
13739       break;
13740
13741     case NT_signed:
13742       switch (size)
13743         {
13744         case 8:  return N_S8;
13745         case 16: return N_S16;
13746         case 32: return N_S32;
13747         case 64: return N_S64;
13748         default: ;
13749         }
13750       break;
13751
13752     case NT_unsigned:
13753       switch (size)
13754         {
13755         case 8:  return N_U8;
13756         case 16: return N_U16;
13757         case 32: return N_U32;
13758         case 64: return N_U64;
13759         default: ;
13760         }
13761       break;
13762
13763     default: ;
13764     }
13765
13766   return N_UTYP;
13767 }
13768
13769 /* Convert compact Neon bitmask type representation to a type and size. Only
13770    handles the case where a single bit is set in the mask.  */
13771
13772 static int
13773 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13774                      enum neon_type_mask mask)
13775 {
13776   if ((mask & N_EQK) != 0)
13777     return FAIL;
13778
13779   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13780     *size = 8;
13781   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13782     *size = 16;
13783   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13784     *size = 32;
13785   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13786     *size = 64;
13787   else
13788     return FAIL;
13789
13790   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13791     *type = NT_signed;
13792   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13793     *type = NT_unsigned;
13794   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13795     *type = NT_integer;
13796   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13797     *type = NT_untyped;
13798   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13799     *type = NT_poly;
13800   else if ((mask & (N_F_ALL)) != 0)
13801     *type = NT_float;
13802   else
13803     return FAIL;
13804
13805   return SUCCESS;
13806 }
13807
13808 /* Modify a bitmask of allowed types. This is only needed for type
13809    relaxation.  */
13810
13811 static unsigned
13812 modify_types_allowed (unsigned allowed, unsigned mods)
13813 {
13814   unsigned size;
13815   enum neon_el_type type;
13816   unsigned destmask;
13817   int i;
13818
13819   destmask = 0;
13820
13821   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13822     {
13823       if (el_type_of_type_chk (&type, &size,
13824                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13825         {
13826           neon_modify_type_size (mods, &type, &size);
13827           destmask |= type_chk_of_el_type (type, size);
13828         }
13829     }
13830
13831   return destmask;
13832 }
13833
13834 /* Check type and return type classification.
13835    The manual states (paraphrase): If one datatype is given, it indicates the
13836    type given in:
13837     - the second operand, if there is one
13838     - the operand, if there is no second operand
13839     - the result, if there are no operands.
13840    This isn't quite good enough though, so we use a concept of a "key" datatype
13841    which is set on a per-instruction basis, which is the one which matters when
13842    only one data type is written.
13843    Note: this function has side-effects (e.g. filling in missing operands). All
13844    Neon instructions should call it before performing bit encoding.  */
13845
13846 static struct neon_type_el
13847 neon_check_type (unsigned els, enum neon_shape ns, ...)
13848 {
13849   va_list ap;
13850   unsigned i, pass, key_el = 0;
13851   unsigned types[NEON_MAX_TYPE_ELS];
13852   enum neon_el_type k_type = NT_invtype;
13853   unsigned k_size = -1u;
13854   struct neon_type_el badtype = {NT_invtype, -1};
13855   unsigned key_allowed = 0;
13856
13857   /* Optional registers in Neon instructions are always (not) in operand 1.
13858      Fill in the missing operand here, if it was omitted.  */
13859   if (els > 1 && !inst.operands[1].present)
13860     inst.operands[1] = inst.operands[0];
13861
13862   /* Suck up all the varargs.  */
13863   va_start (ap, ns);
13864   for (i = 0; i < els; i++)
13865     {
13866       unsigned thisarg = va_arg (ap, unsigned);
13867       if (thisarg == N_IGNORE_TYPE)
13868         {
13869           va_end (ap);
13870           return badtype;
13871         }
13872       types[i] = thisarg;
13873       if ((thisarg & N_KEY) != 0)
13874         key_el = i;
13875     }
13876   va_end (ap);
13877
13878   if (inst.vectype.elems > 0)
13879     for (i = 0; i < els; i++)
13880       if (inst.operands[i].vectype.type != NT_invtype)
13881         {
13882           first_error (_("types specified in both the mnemonic and operands"));
13883           return badtype;
13884         }
13885
13886   /* Duplicate inst.vectype elements here as necessary.
13887      FIXME: No idea if this is exactly the same as the ARM assembler,
13888      particularly when an insn takes one register and one non-register
13889      operand. */
13890   if (inst.vectype.elems == 1 && els > 1)
13891     {
13892       unsigned j;
13893       inst.vectype.elems = els;
13894       inst.vectype.el[key_el] = inst.vectype.el[0];
13895       for (j = 0; j < els; j++)
13896         if (j != key_el)
13897           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13898                                                   types[j]);
13899     }
13900   else if (inst.vectype.elems == 0 && els > 0)
13901     {
13902       unsigned j;
13903       /* No types were given after the mnemonic, so look for types specified
13904          after each operand. We allow some flexibility here; as long as the
13905          "key" operand has a type, we can infer the others.  */
13906       for (j = 0; j < els; j++)
13907         if (inst.operands[j].vectype.type != NT_invtype)
13908           inst.vectype.el[j] = inst.operands[j].vectype;
13909
13910       if (inst.operands[key_el].vectype.type != NT_invtype)
13911         {
13912           for (j = 0; j < els; j++)
13913             if (inst.operands[j].vectype.type == NT_invtype)
13914               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13915                                                       types[j]);
13916         }
13917       else
13918         {
13919           first_error (_("operand types can't be inferred"));
13920           return badtype;
13921         }
13922     }
13923   else if (inst.vectype.elems != els)
13924     {
13925       first_error (_("type specifier has the wrong number of parts"));
13926       return badtype;
13927     }
13928
13929   for (pass = 0; pass < 2; pass++)
13930     {
13931       for (i = 0; i < els; i++)
13932         {
13933           unsigned thisarg = types[i];
13934           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
13935             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
13936           enum neon_el_type g_type = inst.vectype.el[i].type;
13937           unsigned g_size = inst.vectype.el[i].size;
13938
13939           /* Decay more-specific signed & unsigned types to sign-insensitive
13940              integer types if sign-specific variants are unavailable.  */
13941           if ((g_type == NT_signed || g_type == NT_unsigned)
13942               && (types_allowed & N_SU_ALL) == 0)
13943             g_type = NT_integer;
13944
13945           /* If only untyped args are allowed, decay any more specific types to
13946              them. Some instructions only care about signs for some element
13947              sizes, so handle that properly.  */
13948           if (((types_allowed & N_UNT) == 0)
13949               && ((g_size == 8 && (types_allowed & N_8) != 0)
13950                   || (g_size == 16 && (types_allowed & N_16) != 0)
13951                   || (g_size == 32 && (types_allowed & N_32) != 0)
13952                   || (g_size == 64 && (types_allowed & N_64) != 0)))
13953             g_type = NT_untyped;
13954
13955           if (pass == 0)
13956             {
13957               if ((thisarg & N_KEY) != 0)
13958                 {
13959                   k_type = g_type;
13960                   k_size = g_size;
13961                   key_allowed = thisarg & ~N_KEY;
13962
13963                   /* Check architecture constraint on FP16 extension.  */
13964                   if (k_size == 16
13965                       && k_type == NT_float
13966                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
13967                     {
13968                       inst.error = _(BAD_FP16);
13969                       return badtype;
13970                     }
13971                 }
13972             }
13973           else
13974             {
13975               if ((thisarg & N_VFP) != 0)
13976                 {
13977                   enum neon_shape_el regshape;
13978                   unsigned regwidth, match;
13979
13980                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
13981                   if (ns == NS_NULL)
13982                     {
13983                       first_error (_("invalid instruction shape"));
13984                       return badtype;
13985                     }
13986                   regshape = neon_shape_tab[ns].el[i];
13987                   regwidth = neon_shape_el_size[regshape];
13988
13989                   /* In VFP mode, operands must match register widths. If we
13990                      have a key operand, use its width, else use the width of
13991                      the current operand.  */
13992                   if (k_size != -1u)
13993                     match = k_size;
13994                   else
13995                     match = g_size;
13996
13997                   /* FP16 will use a single precision register.  */
13998                   if (regwidth == 32 && match == 16)
13999                     {
14000                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14001                         match = regwidth;
14002                       else
14003                         {
14004                           inst.error = _(BAD_FP16);
14005                           return badtype;
14006                         }
14007                     }
14008
14009                   if (regwidth != match)
14010                     {
14011                       first_error (_("operand size must match register width"));
14012                       return badtype;
14013                     }
14014                 }
14015
14016               if ((thisarg & N_EQK) == 0)
14017                 {
14018                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
14019
14020                   if ((given_type & types_allowed) == 0)
14021                     {
14022                       first_error (_("bad type in Neon instruction"));
14023                       return badtype;
14024                     }
14025                 }
14026               else
14027                 {
14028                   enum neon_el_type mod_k_type = k_type;
14029                   unsigned mod_k_size = k_size;
14030                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
14031                   if (g_type != mod_k_type || g_size != mod_k_size)
14032                     {
14033                       first_error (_("inconsistent types in Neon instruction"));
14034                       return badtype;
14035                     }
14036                 }
14037             }
14038         }
14039     }
14040
14041   return inst.vectype.el[key_el];
14042 }
14043
14044 /* Neon-style VFP instruction forwarding.  */
14045
14046 /* Thumb VFP instructions have 0xE in the condition field.  */
14047
14048 static void
14049 do_vfp_cond_or_thumb (void)
14050 {
14051   inst.is_neon = 1;
14052
14053   if (thumb_mode)
14054     inst.instruction |= 0xe0000000;
14055   else
14056     inst.instruction |= inst.cond << 28;
14057 }
14058
14059 /* Look up and encode a simple mnemonic, for use as a helper function for the
14060    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
14061    etc.  It is assumed that operand parsing has already been done, and that the
14062    operands are in the form expected by the given opcode (this isn't necessarily
14063    the same as the form in which they were parsed, hence some massaging must
14064    take place before this function is called).
14065    Checks current arch version against that in the looked-up opcode.  */
14066
14067 static void
14068 do_vfp_nsyn_opcode (const char *opname)
14069 {
14070   const struct asm_opcode *opcode;
14071
14072   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
14073
14074   if (!opcode)
14075     abort ();
14076
14077   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
14078                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
14079               _(BAD_FPU));
14080
14081   inst.is_neon = 1;
14082
14083   if (thumb_mode)
14084     {
14085       inst.instruction = opcode->tvalue;
14086       opcode->tencode ();
14087     }
14088   else
14089     {
14090       inst.instruction = (inst.cond << 28) | opcode->avalue;
14091       opcode->aencode ();
14092     }
14093 }
14094
14095 static void
14096 do_vfp_nsyn_add_sub (enum neon_shape rs)
14097 {
14098   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
14099
14100   if (rs == NS_FFF || rs == NS_HHH)
14101     {
14102       if (is_add)
14103         do_vfp_nsyn_opcode ("fadds");
14104       else
14105         do_vfp_nsyn_opcode ("fsubs");
14106
14107       /* ARMv8.2 fp16 instruction.  */
14108       if (rs == NS_HHH)
14109         do_scalar_fp16_v82_encode ();
14110     }
14111   else
14112     {
14113       if (is_add)
14114         do_vfp_nsyn_opcode ("faddd");
14115       else
14116         do_vfp_nsyn_opcode ("fsubd");
14117     }
14118 }
14119
14120 /* Check operand types to see if this is a VFP instruction, and if so call
14121    PFN ().  */
14122
14123 static int
14124 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
14125 {
14126   enum neon_shape rs;
14127   struct neon_type_el et;
14128
14129   switch (args)
14130     {
14131     case 2:
14132       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14133       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14134       break;
14135
14136     case 3:
14137       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14138       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14139                             N_F_ALL | N_KEY | N_VFP);
14140       break;
14141
14142     default:
14143       abort ();
14144     }
14145
14146   if (et.type != NT_invtype)
14147     {
14148       pfn (rs);
14149       return SUCCESS;
14150     }
14151
14152   inst.error = NULL;
14153   return FAIL;
14154 }
14155
14156 static void
14157 do_vfp_nsyn_mla_mls (enum neon_shape rs)
14158 {
14159   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
14160
14161   if (rs == NS_FFF || rs == NS_HHH)
14162     {
14163       if (is_mla)
14164         do_vfp_nsyn_opcode ("fmacs");
14165       else
14166         do_vfp_nsyn_opcode ("fnmacs");
14167
14168       /* ARMv8.2 fp16 instruction.  */
14169       if (rs == NS_HHH)
14170         do_scalar_fp16_v82_encode ();
14171     }
14172   else
14173     {
14174       if (is_mla)
14175         do_vfp_nsyn_opcode ("fmacd");
14176       else
14177         do_vfp_nsyn_opcode ("fnmacd");
14178     }
14179 }
14180
14181 static void
14182 do_vfp_nsyn_fma_fms (enum neon_shape rs)
14183 {
14184   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
14185
14186   if (rs == NS_FFF || rs == NS_HHH)
14187     {
14188       if (is_fma)
14189         do_vfp_nsyn_opcode ("ffmas");
14190       else
14191         do_vfp_nsyn_opcode ("ffnmas");
14192
14193       /* ARMv8.2 fp16 instruction.  */
14194       if (rs == NS_HHH)
14195         do_scalar_fp16_v82_encode ();
14196     }
14197   else
14198     {
14199       if (is_fma)
14200         do_vfp_nsyn_opcode ("ffmad");
14201       else
14202         do_vfp_nsyn_opcode ("ffnmad");
14203     }
14204 }
14205
14206 static void
14207 do_vfp_nsyn_mul (enum neon_shape rs)
14208 {
14209   if (rs == NS_FFF || rs == NS_HHH)
14210     {
14211       do_vfp_nsyn_opcode ("fmuls");
14212
14213       /* ARMv8.2 fp16 instruction.  */
14214       if (rs == NS_HHH)
14215         do_scalar_fp16_v82_encode ();
14216     }
14217   else
14218     do_vfp_nsyn_opcode ("fmuld");
14219 }
14220
14221 static void
14222 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14223 {
14224   int is_neg = (inst.instruction & 0x80) != 0;
14225   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
14226
14227   if (rs == NS_FF || rs == NS_HH)
14228     {
14229       if (is_neg)
14230         do_vfp_nsyn_opcode ("fnegs");
14231       else
14232         do_vfp_nsyn_opcode ("fabss");
14233
14234       /* ARMv8.2 fp16 instruction.  */
14235       if (rs == NS_HH)
14236         do_scalar_fp16_v82_encode ();
14237     }
14238   else
14239     {
14240       if (is_neg)
14241         do_vfp_nsyn_opcode ("fnegd");
14242       else
14243         do_vfp_nsyn_opcode ("fabsd");
14244     }
14245 }
14246
14247 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14248    insns belong to Neon, and are handled elsewhere.  */
14249
14250 static void
14251 do_vfp_nsyn_ldm_stm (int is_dbmode)
14252 {
14253   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14254   if (is_ldm)
14255     {
14256       if (is_dbmode)
14257         do_vfp_nsyn_opcode ("fldmdbs");
14258       else
14259         do_vfp_nsyn_opcode ("fldmias");
14260     }
14261   else
14262     {
14263       if (is_dbmode)
14264         do_vfp_nsyn_opcode ("fstmdbs");
14265       else
14266         do_vfp_nsyn_opcode ("fstmias");
14267     }
14268 }
14269
14270 static void
14271 do_vfp_nsyn_sqrt (void)
14272 {
14273   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14274   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14275
14276   if (rs == NS_FF || rs == NS_HH)
14277     {
14278       do_vfp_nsyn_opcode ("fsqrts");
14279
14280       /* ARMv8.2 fp16 instruction.  */
14281       if (rs == NS_HH)
14282         do_scalar_fp16_v82_encode ();
14283     }
14284   else
14285     do_vfp_nsyn_opcode ("fsqrtd");
14286 }
14287
14288 static void
14289 do_vfp_nsyn_div (void)
14290 {
14291   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14292   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14293                    N_F_ALL | N_KEY | N_VFP);
14294
14295   if (rs == NS_FFF || rs == NS_HHH)
14296     {
14297       do_vfp_nsyn_opcode ("fdivs");
14298
14299       /* ARMv8.2 fp16 instruction.  */
14300       if (rs == NS_HHH)
14301         do_scalar_fp16_v82_encode ();
14302     }
14303   else
14304     do_vfp_nsyn_opcode ("fdivd");
14305 }
14306
14307 static void
14308 do_vfp_nsyn_nmul (void)
14309 {
14310   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14311   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14312                    N_F_ALL | N_KEY | N_VFP);
14313
14314   if (rs == NS_FFF || rs == NS_HHH)
14315     {
14316       NEON_ENCODE (SINGLE, inst);
14317       do_vfp_sp_dyadic ();
14318
14319       /* ARMv8.2 fp16 instruction.  */
14320       if (rs == NS_HHH)
14321         do_scalar_fp16_v82_encode ();
14322     }
14323   else
14324     {
14325       NEON_ENCODE (DOUBLE, inst);
14326       do_vfp_dp_rd_rn_rm ();
14327     }
14328   do_vfp_cond_or_thumb ();
14329
14330 }
14331
14332 static void
14333 do_vfp_nsyn_cmp (void)
14334 {
14335   enum neon_shape rs;
14336   if (inst.operands[1].isreg)
14337     {
14338       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14339       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14340
14341       if (rs == NS_FF || rs == NS_HH)
14342         {
14343           NEON_ENCODE (SINGLE, inst);
14344           do_vfp_sp_monadic ();
14345         }
14346       else
14347         {
14348           NEON_ENCODE (DOUBLE, inst);
14349           do_vfp_dp_rd_rm ();
14350         }
14351     }
14352   else
14353     {
14354       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
14355       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
14356
14357       switch (inst.instruction & 0x0fffffff)
14358         {
14359         case N_MNEM_vcmp:
14360           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
14361           break;
14362         case N_MNEM_vcmpe:
14363           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
14364           break;
14365         default:
14366           abort ();
14367         }
14368
14369       if (rs == NS_FI || rs == NS_HI)
14370         {
14371           NEON_ENCODE (SINGLE, inst);
14372           do_vfp_sp_compare_z ();
14373         }
14374       else
14375         {
14376           NEON_ENCODE (DOUBLE, inst);
14377           do_vfp_dp_rd ();
14378         }
14379     }
14380   do_vfp_cond_or_thumb ();
14381
14382   /* ARMv8.2 fp16 instruction.  */
14383   if (rs == NS_HI || rs == NS_HH)
14384     do_scalar_fp16_v82_encode ();
14385 }
14386
14387 static void
14388 nsyn_insert_sp (void)
14389 {
14390   inst.operands[1] = inst.operands[0];
14391   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
14392   inst.operands[0].reg = REG_SP;
14393   inst.operands[0].isreg = 1;
14394   inst.operands[0].writeback = 1;
14395   inst.operands[0].present = 1;
14396 }
14397
14398 static void
14399 do_vfp_nsyn_push (void)
14400 {
14401   nsyn_insert_sp ();
14402
14403   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14404               _("register list must contain at least 1 and at most 16 "
14405                 "registers"));
14406
14407   if (inst.operands[1].issingle)
14408     do_vfp_nsyn_opcode ("fstmdbs");
14409   else
14410     do_vfp_nsyn_opcode ("fstmdbd");
14411 }
14412
14413 static void
14414 do_vfp_nsyn_pop (void)
14415 {
14416   nsyn_insert_sp ();
14417
14418   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14419               _("register list must contain at least 1 and at most 16 "
14420                 "registers"));
14421
14422   if (inst.operands[1].issingle)
14423     do_vfp_nsyn_opcode ("fldmias");
14424   else
14425     do_vfp_nsyn_opcode ("fldmiad");
14426 }
14427
14428 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14429    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14430
14431 static void
14432 neon_dp_fixup (struct arm_it* insn)
14433 {
14434   unsigned int i = insn->instruction;
14435   insn->is_neon = 1;
14436
14437   if (thumb_mode)
14438     {
14439       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14440       if (i & (1 << 24))
14441         i |= 1 << 28;
14442
14443       i &= ~(1 << 24);
14444
14445       i |= 0xef000000;
14446     }
14447   else
14448     i |= 0xf2000000;
14449
14450   insn->instruction = i;
14451 }
14452
14453 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14454    (0, 1, 2, 3).  */
14455
14456 static unsigned
14457 neon_logbits (unsigned x)
14458 {
14459   return ffs (x) - 4;
14460 }
14461
14462 #define LOW4(R) ((R) & 0xf)
14463 #define HI1(R) (((R) >> 4) & 1)
14464
14465 /* Encode insns with bit pattern:
14466
14467   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14468   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14469
14470   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14471   different meaning for some instruction.  */
14472
14473 static void
14474 neon_three_same (int isquad, int ubit, int size)
14475 {
14476   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14477   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14478   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14479   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14480   inst.instruction |= LOW4 (inst.operands[2].reg);
14481   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14482   inst.instruction |= (isquad != 0) << 6;
14483   inst.instruction |= (ubit != 0) << 24;
14484   if (size != -1)
14485     inst.instruction |= neon_logbits (size) << 20;
14486
14487   neon_dp_fixup (&inst);
14488 }
14489
14490 /* Encode instructions of the form:
14491
14492   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14493   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14494
14495   Don't write size if SIZE == -1.  */
14496
14497 static void
14498 neon_two_same (int qbit, int ubit, int size)
14499 {
14500   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14501   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14502   inst.instruction |= LOW4 (inst.operands[1].reg);
14503   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14504   inst.instruction |= (qbit != 0) << 6;
14505   inst.instruction |= (ubit != 0) << 24;
14506
14507   if (size != -1)
14508     inst.instruction |= neon_logbits (size) << 18;
14509
14510   neon_dp_fixup (&inst);
14511 }
14512
14513 /* Neon instruction encoders, in approximate order of appearance.  */
14514
14515 static void
14516 do_neon_dyadic_i_su (void)
14517 {
14518   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14519   struct neon_type_el et = neon_check_type (3, rs,
14520     N_EQK, N_EQK, N_SU_32 | N_KEY);
14521   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14522 }
14523
14524 static void
14525 do_neon_dyadic_i64_su (void)
14526 {
14527   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14528   struct neon_type_el et = neon_check_type (3, rs,
14529     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14530   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14531 }
14532
14533 static void
14534 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14535                 unsigned immbits)
14536 {
14537   unsigned size = et.size >> 3;
14538   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14539   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14540   inst.instruction |= LOW4 (inst.operands[1].reg);
14541   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14542   inst.instruction |= (isquad != 0) << 6;
14543   inst.instruction |= immbits << 16;
14544   inst.instruction |= (size >> 3) << 7;
14545   inst.instruction |= (size & 0x7) << 19;
14546   if (write_ubit)
14547     inst.instruction |= (uval != 0) << 24;
14548
14549   neon_dp_fixup (&inst);
14550 }
14551
14552 static void
14553 do_neon_shl_imm (void)
14554 {
14555   if (!inst.operands[2].isreg)
14556     {
14557       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14558       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14559       int imm = inst.operands[2].imm;
14560
14561       constraint (imm < 0 || (unsigned)imm >= et.size,
14562                   _("immediate out of range for shift"));
14563       NEON_ENCODE (IMMED, inst);
14564       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14565     }
14566   else
14567     {
14568       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14569       struct neon_type_el et = neon_check_type (3, rs,
14570         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14571       unsigned int tmp;
14572
14573       /* VSHL/VQSHL 3-register variants have syntax such as:
14574            vshl.xx Dd, Dm, Dn
14575          whereas other 3-register operations encoded by neon_three_same have
14576          syntax like:
14577            vadd.xx Dd, Dn, Dm
14578          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14579          here.  */
14580       tmp = inst.operands[2].reg;
14581       inst.operands[2].reg = inst.operands[1].reg;
14582       inst.operands[1].reg = tmp;
14583       NEON_ENCODE (INTEGER, inst);
14584       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14585     }
14586 }
14587
14588 static void
14589 do_neon_qshl_imm (void)
14590 {
14591   if (!inst.operands[2].isreg)
14592     {
14593       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14594       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14595       int imm = inst.operands[2].imm;
14596
14597       constraint (imm < 0 || (unsigned)imm >= et.size,
14598                   _("immediate out of range for shift"));
14599       NEON_ENCODE (IMMED, inst);
14600       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14601     }
14602   else
14603     {
14604       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14605       struct neon_type_el et = neon_check_type (3, rs,
14606         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14607       unsigned int tmp;
14608
14609       /* See note in do_neon_shl_imm.  */
14610       tmp = inst.operands[2].reg;
14611       inst.operands[2].reg = inst.operands[1].reg;
14612       inst.operands[1].reg = tmp;
14613       NEON_ENCODE (INTEGER, inst);
14614       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14615     }
14616 }
14617
14618 static void
14619 do_neon_rshl (void)
14620 {
14621   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14622   struct neon_type_el et = neon_check_type (3, rs,
14623     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14624   unsigned int tmp;
14625
14626   tmp = inst.operands[2].reg;
14627   inst.operands[2].reg = inst.operands[1].reg;
14628   inst.operands[1].reg = tmp;
14629   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14630 }
14631
14632 static int
14633 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14634 {
14635   /* Handle .I8 pseudo-instructions.  */
14636   if (size == 8)
14637     {
14638       /* Unfortunately, this will make everything apart from zero out-of-range.
14639          FIXME is this the intended semantics? There doesn't seem much point in
14640          accepting .I8 if so.  */
14641       immediate |= immediate << 8;
14642       size = 16;
14643     }
14644
14645   if (size >= 32)
14646     {
14647       if (immediate == (immediate & 0x000000ff))
14648         {
14649           *immbits = immediate;
14650           return 0x1;
14651         }
14652       else if (immediate == (immediate & 0x0000ff00))
14653         {
14654           *immbits = immediate >> 8;
14655           return 0x3;
14656         }
14657       else if (immediate == (immediate & 0x00ff0000))
14658         {
14659           *immbits = immediate >> 16;
14660           return 0x5;
14661         }
14662       else if (immediate == (immediate & 0xff000000))
14663         {
14664           *immbits = immediate >> 24;
14665           return 0x7;
14666         }
14667       if ((immediate & 0xffff) != (immediate >> 16))
14668         goto bad_immediate;
14669       immediate &= 0xffff;
14670     }
14671
14672   if (immediate == (immediate & 0x000000ff))
14673     {
14674       *immbits = immediate;
14675       return 0x9;
14676     }
14677   else if (immediate == (immediate & 0x0000ff00))
14678     {
14679       *immbits = immediate >> 8;
14680       return 0xb;
14681     }
14682
14683   bad_immediate:
14684   first_error (_("immediate value out of range"));
14685   return FAIL;
14686 }
14687
14688 static void
14689 do_neon_logic (void)
14690 {
14691   if (inst.operands[2].present && inst.operands[2].isreg)
14692     {
14693       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14694       neon_check_type (3, rs, N_IGNORE_TYPE);
14695       /* U bit and size field were set as part of the bitmask.  */
14696       NEON_ENCODE (INTEGER, inst);
14697       neon_three_same (neon_quad (rs), 0, -1);
14698     }
14699   else
14700     {
14701       const int three_ops_form = (inst.operands[2].present
14702                                   && !inst.operands[2].isreg);
14703       const int immoperand = (three_ops_form ? 2 : 1);
14704       enum neon_shape rs = (three_ops_form
14705                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14706                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14707       struct neon_type_el et = neon_check_type (2, rs,
14708         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14709       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14710       unsigned immbits;
14711       int cmode;
14712
14713       if (et.type == NT_invtype)
14714         return;
14715
14716       if (three_ops_form)
14717         constraint (inst.operands[0].reg != inst.operands[1].reg,
14718                     _("first and second operands shall be the same register"));
14719
14720       NEON_ENCODE (IMMED, inst);
14721
14722       immbits = inst.operands[immoperand].imm;
14723       if (et.size == 64)
14724         {
14725           /* .i64 is a pseudo-op, so the immediate must be a repeating
14726              pattern.  */
14727           if (immbits != (inst.operands[immoperand].regisimm ?
14728                           inst.operands[immoperand].reg : 0))
14729             {
14730               /* Set immbits to an invalid constant.  */
14731               immbits = 0xdeadbeef;
14732             }
14733         }
14734
14735       switch (opcode)
14736         {
14737         case N_MNEM_vbic:
14738           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14739           break;
14740
14741         case N_MNEM_vorr:
14742           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14743           break;
14744
14745         case N_MNEM_vand:
14746           /* Pseudo-instruction for VBIC.  */
14747           neon_invert_size (&immbits, 0, et.size);
14748           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14749           break;
14750
14751         case N_MNEM_vorn:
14752           /* Pseudo-instruction for VORR.  */
14753           neon_invert_size (&immbits, 0, et.size);
14754           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14755           break;
14756
14757         default:
14758           abort ();
14759         }
14760
14761       if (cmode == FAIL)
14762         return;
14763
14764       inst.instruction |= neon_quad (rs) << 6;
14765       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14766       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14767       inst.instruction |= cmode << 8;
14768       neon_write_immbits (immbits);
14769
14770       neon_dp_fixup (&inst);
14771     }
14772 }
14773
14774 static void
14775 do_neon_bitfield (void)
14776 {
14777   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14778   neon_check_type (3, rs, N_IGNORE_TYPE);
14779   neon_three_same (neon_quad (rs), 0, -1);
14780 }
14781
14782 static void
14783 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14784                   unsigned destbits)
14785 {
14786   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14787   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14788                                             types | N_KEY);
14789   if (et.type == NT_float)
14790     {
14791       NEON_ENCODE (FLOAT, inst);
14792       neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
14793     }
14794   else
14795     {
14796       NEON_ENCODE (INTEGER, inst);
14797       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14798     }
14799 }
14800
14801 static void
14802 do_neon_dyadic_if_su (void)
14803 {
14804   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14805 }
14806
14807 static void
14808 do_neon_dyadic_if_su_d (void)
14809 {
14810   /* This version only allow D registers, but that constraint is enforced during
14811      operand parsing so we don't need to do anything extra here.  */
14812   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14813 }
14814
14815 static void
14816 do_neon_dyadic_if_i_d (void)
14817 {
14818   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14819      affected if we specify unsigned args.  */
14820   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14821 }
14822
14823 enum vfp_or_neon_is_neon_bits
14824 {
14825   NEON_CHECK_CC = 1,
14826   NEON_CHECK_ARCH = 2,
14827   NEON_CHECK_ARCH8 = 4
14828 };
14829
14830 /* Call this function if an instruction which may have belonged to the VFP or
14831    Neon instruction sets, but turned out to be a Neon instruction (due to the
14832    operand types involved, etc.). We have to check and/or fix-up a couple of
14833    things:
14834
14835      - Make sure the user hasn't attempted to make a Neon instruction
14836        conditional.
14837      - Alter the value in the condition code field if necessary.
14838      - Make sure that the arch supports Neon instructions.
14839
14840    Which of these operations take place depends on bits from enum
14841    vfp_or_neon_is_neon_bits.
14842
14843    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14844    current instruction's condition is COND_ALWAYS, the condition field is
14845    changed to inst.uncond_value. This is necessary because instructions shared
14846    between VFP and Neon may be conditional for the VFP variants only, and the
14847    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14848
14849 static int
14850 vfp_or_neon_is_neon (unsigned check)
14851 {
14852   /* Conditions are always legal in Thumb mode (IT blocks).  */
14853   if (!thumb_mode && (check & NEON_CHECK_CC))
14854     {
14855       if (inst.cond != COND_ALWAYS)
14856         {
14857           first_error (_(BAD_COND));
14858           return FAIL;
14859         }
14860       if (inst.uncond_value != -1)
14861         inst.instruction |= inst.uncond_value << 28;
14862     }
14863
14864   if ((check & NEON_CHECK_ARCH)
14865       && !mark_feature_used (&fpu_neon_ext_v1))
14866     {
14867       first_error (_(BAD_FPU));
14868       return FAIL;
14869     }
14870
14871   if ((check & NEON_CHECK_ARCH8)
14872       && !mark_feature_used (&fpu_neon_ext_armv8))
14873     {
14874       first_error (_(BAD_FPU));
14875       return FAIL;
14876     }
14877
14878   return SUCCESS;
14879 }
14880
14881 static void
14882 do_neon_addsub_if_i (void)
14883 {
14884   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
14885     return;
14886
14887   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14888     return;
14889
14890   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14891      affected if we specify unsigned args.  */
14892   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
14893 }
14894
14895 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
14896    result to be:
14897      V<op> A,B     (A is operand 0, B is operand 2)
14898    to mean:
14899      V<op> A,B,A
14900    not:
14901      V<op> A,B,B
14902    so handle that case specially.  */
14903
14904 static void
14905 neon_exchange_operands (void)
14906 {
14907   if (inst.operands[1].present)
14908     {
14909       void *scratch = xmalloc (sizeof (inst.operands[0]));
14910
14911       /* Swap operands[1] and operands[2].  */
14912       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
14913       inst.operands[1] = inst.operands[2];
14914       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
14915       free (scratch);
14916     }
14917   else
14918     {
14919       inst.operands[1] = inst.operands[2];
14920       inst.operands[2] = inst.operands[0];
14921     }
14922 }
14923
14924 static void
14925 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
14926 {
14927   if (inst.operands[2].isreg)
14928     {
14929       if (invert)
14930         neon_exchange_operands ();
14931       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
14932     }
14933   else
14934     {
14935       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14936       struct neon_type_el et = neon_check_type (2, rs,
14937         N_EQK | N_SIZ, immtypes | N_KEY);
14938
14939       NEON_ENCODE (IMMED, inst);
14940       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14941       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14942       inst.instruction |= LOW4 (inst.operands[1].reg);
14943       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14944       inst.instruction |= neon_quad (rs) << 6;
14945       inst.instruction |= (et.type == NT_float) << 10;
14946       inst.instruction |= neon_logbits (et.size) << 18;
14947
14948       neon_dp_fixup (&inst);
14949     }
14950 }
14951
14952 static void
14953 do_neon_cmp (void)
14954 {
14955   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
14956 }
14957
14958 static void
14959 do_neon_cmp_inv (void)
14960 {
14961   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
14962 }
14963
14964 static void
14965 do_neon_ceq (void)
14966 {
14967   neon_compare (N_IF_32, N_IF_32, FALSE);
14968 }
14969
14970 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
14971    scalars, which are encoded in 5 bits, M : Rm.
14972    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
14973    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
14974    index in M.  */
14975
14976 static unsigned
14977 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
14978 {
14979   unsigned regno = NEON_SCALAR_REG (scalar);
14980   unsigned elno = NEON_SCALAR_INDEX (scalar);
14981
14982   switch (elsize)
14983     {
14984     case 16:
14985       if (regno > 7 || elno > 3)
14986         goto bad_scalar;
14987       return regno | (elno << 3);
14988
14989     case 32:
14990       if (regno > 15 || elno > 1)
14991         goto bad_scalar;
14992       return regno | (elno << 4);
14993
14994     default:
14995     bad_scalar:
14996       first_error (_("scalar out of range for multiply instruction"));
14997     }
14998
14999   return 0;
15000 }
15001
15002 /* Encode multiply / multiply-accumulate scalar instructions.  */
15003
15004 static void
15005 neon_mul_mac (struct neon_type_el et, int ubit)
15006 {
15007   unsigned scalar;
15008
15009   /* Give a more helpful error message if we have an invalid type.  */
15010   if (et.type == NT_invtype)
15011     return;
15012
15013   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
15014   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15015   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15016   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15017   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15018   inst.instruction |= LOW4 (scalar);
15019   inst.instruction |= HI1 (scalar) << 5;
15020   inst.instruction |= (et.type == NT_float) << 8;
15021   inst.instruction |= neon_logbits (et.size) << 20;
15022   inst.instruction |= (ubit != 0) << 24;
15023
15024   neon_dp_fixup (&inst);
15025 }
15026
15027 static void
15028 do_neon_mac_maybe_scalar (void)
15029 {
15030   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
15031     return;
15032
15033   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15034     return;
15035
15036   if (inst.operands[2].isscalar)
15037     {
15038       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15039       struct neon_type_el et = neon_check_type (3, rs,
15040         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
15041       NEON_ENCODE (SCALAR, inst);
15042       neon_mul_mac (et, neon_quad (rs));
15043     }
15044   else
15045     {
15046       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
15047          affected if we specify unsigned args.  */
15048       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15049     }
15050 }
15051
15052 static void
15053 do_neon_fmac (void)
15054 {
15055   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
15056     return;
15057
15058   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15059     return;
15060
15061   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15062 }
15063
15064 static void
15065 do_neon_tst (void)
15066 {
15067   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15068   struct neon_type_el et = neon_check_type (3, rs,
15069     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
15070   neon_three_same (neon_quad (rs), 0, et.size);
15071 }
15072
15073 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
15074    same types as the MAC equivalents. The polynomial type for this instruction
15075    is encoded the same as the integer type.  */
15076
15077 static void
15078 do_neon_mul (void)
15079 {
15080   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
15081     return;
15082
15083   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15084     return;
15085
15086   if (inst.operands[2].isscalar)
15087     do_neon_mac_maybe_scalar ();
15088   else
15089     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
15090 }
15091
15092 static void
15093 do_neon_qdmulh (void)
15094 {
15095   if (inst.operands[2].isscalar)
15096     {
15097       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15098       struct neon_type_el et = neon_check_type (3, rs,
15099         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15100       NEON_ENCODE (SCALAR, inst);
15101       neon_mul_mac (et, neon_quad (rs));
15102     }
15103   else
15104     {
15105       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15106       struct neon_type_el et = neon_check_type (3, rs,
15107         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15108       NEON_ENCODE (INTEGER, inst);
15109       /* The U bit (rounding) comes from bit mask.  */
15110       neon_three_same (neon_quad (rs), 0, et.size);
15111     }
15112 }
15113
15114 static void
15115 do_neon_qrdmlah (void)
15116 {
15117   /* Check we're on the correct architecture.  */
15118   if (!mark_feature_used (&fpu_neon_ext_armv8))
15119     inst.error =
15120       _("instruction form not available on this architecture.");
15121   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
15122     {
15123       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
15124       record_feature_use (&fpu_neon_ext_v8_1);
15125     }
15126
15127   if (inst.operands[2].isscalar)
15128     {
15129       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15130       struct neon_type_el et = neon_check_type (3, rs,
15131         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15132       NEON_ENCODE (SCALAR, inst);
15133       neon_mul_mac (et, neon_quad (rs));
15134     }
15135   else
15136     {
15137       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15138       struct neon_type_el et = neon_check_type (3, rs,
15139         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15140       NEON_ENCODE (INTEGER, inst);
15141       /* The U bit (rounding) comes from bit mask.  */
15142       neon_three_same (neon_quad (rs), 0, et.size);
15143     }
15144 }
15145
15146 static void
15147 do_neon_fcmp_absolute (void)
15148 {
15149   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15150   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15151                                             N_F_16_32 | N_KEY);
15152   /* Size field comes from bit mask.  */
15153   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
15154 }
15155
15156 static void
15157 do_neon_fcmp_absolute_inv (void)
15158 {
15159   neon_exchange_operands ();
15160   do_neon_fcmp_absolute ();
15161 }
15162
15163 static void
15164 do_neon_step (void)
15165 {
15166   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15167   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15168                                             N_F_16_32 | N_KEY);
15169   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15170 }
15171
15172 static void
15173 do_neon_abs_neg (void)
15174 {
15175   enum neon_shape rs;
15176   struct neon_type_el et;
15177
15178   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
15179     return;
15180
15181   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15182     return;
15183
15184   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15185   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
15186
15187   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15188   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15189   inst.instruction |= LOW4 (inst.operands[1].reg);
15190   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15191   inst.instruction |= neon_quad (rs) << 6;
15192   inst.instruction |= (et.type == NT_float) << 10;
15193   inst.instruction |= neon_logbits (et.size) << 18;
15194
15195   neon_dp_fixup (&inst);
15196 }
15197
15198 static void
15199 do_neon_sli (void)
15200 {
15201   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15202   struct neon_type_el et = neon_check_type (2, rs,
15203     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15204   int imm = inst.operands[2].imm;
15205   constraint (imm < 0 || (unsigned)imm >= et.size,
15206               _("immediate out of range for insert"));
15207   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15208 }
15209
15210 static void
15211 do_neon_sri (void)
15212 {
15213   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15214   struct neon_type_el et = neon_check_type (2, rs,
15215     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15216   int imm = inst.operands[2].imm;
15217   constraint (imm < 1 || (unsigned)imm > et.size,
15218               _("immediate out of range for insert"));
15219   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
15220 }
15221
15222 static void
15223 do_neon_qshlu_imm (void)
15224 {
15225   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15226   struct neon_type_el et = neon_check_type (2, rs,
15227     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
15228   int imm = inst.operands[2].imm;
15229   constraint (imm < 0 || (unsigned)imm >= et.size,
15230               _("immediate out of range for shift"));
15231   /* Only encodes the 'U present' variant of the instruction.
15232      In this case, signed types have OP (bit 8) set to 0.
15233      Unsigned types have OP set to 1.  */
15234   inst.instruction |= (et.type == NT_unsigned) << 8;
15235   /* The rest of the bits are the same as other immediate shifts.  */
15236   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15237 }
15238
15239 static void
15240 do_neon_qmovn (void)
15241 {
15242   struct neon_type_el et = neon_check_type (2, NS_DQ,
15243     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15244   /* Saturating move where operands can be signed or unsigned, and the
15245      destination has the same signedness.  */
15246   NEON_ENCODE (INTEGER, inst);
15247   if (et.type == NT_unsigned)
15248     inst.instruction |= 0xc0;
15249   else
15250     inst.instruction |= 0x80;
15251   neon_two_same (0, 1, et.size / 2);
15252 }
15253
15254 static void
15255 do_neon_qmovun (void)
15256 {
15257   struct neon_type_el et = neon_check_type (2, NS_DQ,
15258     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15259   /* Saturating move with unsigned results. Operands must be signed.  */
15260   NEON_ENCODE (INTEGER, inst);
15261   neon_two_same (0, 1, et.size / 2);
15262 }
15263
15264 static void
15265 do_neon_rshift_sat_narrow (void)
15266 {
15267   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15268      or unsigned. If operands are unsigned, results must also be unsigned.  */
15269   struct neon_type_el et = neon_check_type (2, NS_DQI,
15270     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15271   int imm = inst.operands[2].imm;
15272   /* This gets the bounds check, size encoding and immediate bits calculation
15273      right.  */
15274   et.size /= 2;
15275
15276   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
15277      VQMOVN.I<size> <Dd>, <Qm>.  */
15278   if (imm == 0)
15279     {
15280       inst.operands[2].present = 0;
15281       inst.instruction = N_MNEM_vqmovn;
15282       do_neon_qmovn ();
15283       return;
15284     }
15285
15286   constraint (imm < 1 || (unsigned)imm > et.size,
15287               _("immediate out of range"));
15288   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
15289 }
15290
15291 static void
15292 do_neon_rshift_sat_narrow_u (void)
15293 {
15294   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15295      or unsigned. If operands are unsigned, results must also be unsigned.  */
15296   struct neon_type_el et = neon_check_type (2, NS_DQI,
15297     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15298   int imm = inst.operands[2].imm;
15299   /* This gets the bounds check, size encoding and immediate bits calculation
15300      right.  */
15301   et.size /= 2;
15302
15303   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
15304      VQMOVUN.I<size> <Dd>, <Qm>.  */
15305   if (imm == 0)
15306     {
15307       inst.operands[2].present = 0;
15308       inst.instruction = N_MNEM_vqmovun;
15309       do_neon_qmovun ();
15310       return;
15311     }
15312
15313   constraint (imm < 1 || (unsigned)imm > et.size,
15314               _("immediate out of range"));
15315   /* FIXME: The manual is kind of unclear about what value U should have in
15316      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
15317      must be 1.  */
15318   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
15319 }
15320
15321 static void
15322 do_neon_movn (void)
15323 {
15324   struct neon_type_el et = neon_check_type (2, NS_DQ,
15325     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15326   NEON_ENCODE (INTEGER, inst);
15327   neon_two_same (0, 1, et.size / 2);
15328 }
15329
15330 static void
15331 do_neon_rshift_narrow (void)
15332 {
15333   struct neon_type_el et = neon_check_type (2, NS_DQI,
15334     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15335   int imm = inst.operands[2].imm;
15336   /* This gets the bounds check, size encoding and immediate bits calculation
15337      right.  */
15338   et.size /= 2;
15339
15340   /* If immediate is zero then we are a pseudo-instruction for
15341      VMOVN.I<size> <Dd>, <Qm>  */
15342   if (imm == 0)
15343     {
15344       inst.operands[2].present = 0;
15345       inst.instruction = N_MNEM_vmovn;
15346       do_neon_movn ();
15347       return;
15348     }
15349
15350   constraint (imm < 1 || (unsigned)imm > et.size,
15351               _("immediate out of range for narrowing operation"));
15352   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
15353 }
15354
15355 static void
15356 do_neon_shll (void)
15357 {
15358   /* FIXME: Type checking when lengthening.  */
15359   struct neon_type_el et = neon_check_type (2, NS_QDI,
15360     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
15361   unsigned imm = inst.operands[2].imm;
15362
15363   if (imm == et.size)
15364     {
15365       /* Maximum shift variant.  */
15366       NEON_ENCODE (INTEGER, inst);
15367       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15368       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15369       inst.instruction |= LOW4 (inst.operands[1].reg);
15370       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15371       inst.instruction |= neon_logbits (et.size) << 18;
15372
15373       neon_dp_fixup (&inst);
15374     }
15375   else
15376     {
15377       /* A more-specific type check for non-max versions.  */
15378       et = neon_check_type (2, NS_QDI,
15379         N_EQK | N_DBL, N_SU_32 | N_KEY);
15380       NEON_ENCODE (IMMED, inst);
15381       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
15382     }
15383 }
15384
15385 /* Check the various types for the VCVT instruction, and return which version
15386    the current instruction is.  */
15387
15388 #define CVT_FLAVOUR_VAR                                                       \
15389   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
15390   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
15391   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
15392   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
15393   /* Half-precision conversions.  */                                          \
15394   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15395   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15396   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
15397   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
15398   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
15399   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
15400   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
15401      Compared with single/double precision variants, only the co-processor    \
15402      field is different, so the encoding flow is reused here.  */             \
15403   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
15404   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
15405   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
15406   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
15407   /* VFP instructions.  */                                                    \
15408   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
15409   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
15410   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
15411   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
15412   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
15413   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
15414   /* VFP instructions with bitshift.  */                                      \
15415   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
15416   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
15417   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
15418   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
15419   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
15420   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
15421   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
15422   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
15423
15424 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
15425   neon_cvt_flavour_##C,
15426
15427 /* The different types of conversions we can do.  */
15428 enum neon_cvt_flavour
15429 {
15430   CVT_FLAVOUR_VAR
15431   neon_cvt_flavour_invalid,
15432   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
15433 };
15434
15435 #undef CVT_VAR
15436
15437 static enum neon_cvt_flavour
15438 get_neon_cvt_flavour (enum neon_shape rs)
15439 {
15440 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
15441   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
15442   if (et.type != NT_invtype)                            \
15443     {                                                   \
15444       inst.error = NULL;                                \
15445       return (neon_cvt_flavour_##C);                    \
15446     }
15447
15448   struct neon_type_el et;
15449   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15450                         || rs == NS_FF) ? N_VFP : 0;
15451   /* The instruction versions which take an immediate take one register
15452      argument, which is extended to the width of the full register. Thus the
15453      "source" and "destination" registers must have the same width.  Hack that
15454      here by making the size equal to the key (wider, in this case) operand.  */
15455   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15456
15457   CVT_FLAVOUR_VAR;
15458
15459   return neon_cvt_flavour_invalid;
15460 #undef CVT_VAR
15461 }
15462
15463 enum neon_cvt_mode
15464 {
15465   neon_cvt_mode_a,
15466   neon_cvt_mode_n,
15467   neon_cvt_mode_p,
15468   neon_cvt_mode_m,
15469   neon_cvt_mode_z,
15470   neon_cvt_mode_x,
15471   neon_cvt_mode_r
15472 };
15473
15474 /* Neon-syntax VFP conversions.  */
15475
15476 static void
15477 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15478 {
15479   const char *opname = 0;
15480
15481   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
15482       || rs == NS_FHI || rs == NS_HFI)
15483     {
15484       /* Conversions with immediate bitshift.  */
15485       const char *enc[] =
15486         {
15487 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15488           CVT_FLAVOUR_VAR
15489           NULL
15490 #undef CVT_VAR
15491         };
15492
15493       if (flavour < (int) ARRAY_SIZE (enc))
15494         {
15495           opname = enc[flavour];
15496           constraint (inst.operands[0].reg != inst.operands[1].reg,
15497                       _("operands 0 and 1 must be the same register"));
15498           inst.operands[1] = inst.operands[2];
15499           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15500         }
15501     }
15502   else
15503     {
15504       /* Conversions without bitshift.  */
15505       const char *enc[] =
15506         {
15507 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15508           CVT_FLAVOUR_VAR
15509           NULL
15510 #undef CVT_VAR
15511         };
15512
15513       if (flavour < (int) ARRAY_SIZE (enc))
15514         opname = enc[flavour];
15515     }
15516
15517   if (opname)
15518     do_vfp_nsyn_opcode (opname);
15519
15520   /* ARMv8.2 fp16 VCVT instruction.  */
15521   if (flavour == neon_cvt_flavour_s32_f16
15522       || flavour == neon_cvt_flavour_u32_f16
15523       || flavour == neon_cvt_flavour_f16_u32
15524       || flavour == neon_cvt_flavour_f16_s32)
15525     do_scalar_fp16_v82_encode ();
15526 }
15527
15528 static void
15529 do_vfp_nsyn_cvtz (void)
15530 {
15531   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
15532   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15533   const char *enc[] =
15534     {
15535 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15536       CVT_FLAVOUR_VAR
15537       NULL
15538 #undef CVT_VAR
15539     };
15540
15541   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15542     do_vfp_nsyn_opcode (enc[flavour]);
15543 }
15544
15545 static void
15546 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15547                       enum neon_cvt_mode mode)
15548 {
15549   int sz, op;
15550   int rm;
15551
15552   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15553      D register operands.  */
15554   if (flavour == neon_cvt_flavour_s32_f64
15555       || flavour == neon_cvt_flavour_u32_f64)
15556     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15557                 _(BAD_FPU));
15558
15559   if (flavour == neon_cvt_flavour_s32_f16
15560       || flavour == neon_cvt_flavour_u32_f16)
15561     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
15562                 _(BAD_FP16));
15563
15564   set_it_insn_type (OUTSIDE_IT_INSN);
15565
15566   switch (flavour)
15567     {
15568     case neon_cvt_flavour_s32_f64:
15569       sz = 1;
15570       op = 1;
15571       break;
15572     case neon_cvt_flavour_s32_f32:
15573       sz = 0;
15574       op = 1;
15575       break;
15576     case neon_cvt_flavour_s32_f16:
15577       sz = 0;
15578       op = 1;
15579       break;
15580     case neon_cvt_flavour_u32_f64:
15581       sz = 1;
15582       op = 0;
15583       break;
15584     case neon_cvt_flavour_u32_f32:
15585       sz = 0;
15586       op = 0;
15587       break;
15588     case neon_cvt_flavour_u32_f16:
15589       sz = 0;
15590       op = 0;
15591       break;
15592     default:
15593       first_error (_("invalid instruction shape"));
15594       return;
15595     }
15596
15597   switch (mode)
15598     {
15599     case neon_cvt_mode_a: rm = 0; break;
15600     case neon_cvt_mode_n: rm = 1; break;
15601     case neon_cvt_mode_p: rm = 2; break;
15602     case neon_cvt_mode_m: rm = 3; break;
15603     default: first_error (_("invalid rounding mode")); return;
15604     }
15605
15606   NEON_ENCODE (FPV8, inst);
15607   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15608   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15609   inst.instruction |= sz << 8;
15610
15611   /* ARMv8.2 fp16 VCVT instruction.  */
15612   if (flavour == neon_cvt_flavour_s32_f16
15613       ||flavour == neon_cvt_flavour_u32_f16)
15614     do_scalar_fp16_v82_encode ();
15615   inst.instruction |= op << 7;
15616   inst.instruction |= rm << 16;
15617   inst.instruction |= 0xf0000000;
15618   inst.is_neon = TRUE;
15619 }
15620
15621 static void
15622 do_neon_cvt_1 (enum neon_cvt_mode mode)
15623 {
15624   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15625                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
15626                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
15627                                           NS_NULL);
15628   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15629
15630   if (flavour == neon_cvt_flavour_invalid)
15631     return;
15632
15633   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15634   if (mode == neon_cvt_mode_z
15635       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15636       && (flavour == neon_cvt_flavour_s16_f16
15637           || flavour == neon_cvt_flavour_u16_f16
15638           || flavour == neon_cvt_flavour_s32_f32
15639           || flavour == neon_cvt_flavour_u32_f32
15640           || flavour == neon_cvt_flavour_s32_f64
15641           || flavour == neon_cvt_flavour_u32_f64)
15642       && (rs == NS_FD || rs == NS_FF))
15643     {
15644       do_vfp_nsyn_cvtz ();
15645       return;
15646     }
15647
15648   /* ARMv8.2 fp16 VCVT conversions.  */
15649   if (mode == neon_cvt_mode_z
15650       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
15651       && (flavour == neon_cvt_flavour_s32_f16
15652           || flavour == neon_cvt_flavour_u32_f16)
15653       && (rs == NS_FH))
15654     {
15655       do_vfp_nsyn_cvtz ();
15656       do_scalar_fp16_v82_encode ();
15657       return;
15658     }
15659
15660   /* VFP rather than Neon conversions.  */
15661   if (flavour >= neon_cvt_flavour_first_fp)
15662     {
15663       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15664         do_vfp_nsyn_cvt (rs, flavour);
15665       else
15666         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15667
15668       return;
15669     }
15670
15671   switch (rs)
15672     {
15673     case NS_DDI:
15674     case NS_QQI:
15675       {
15676         unsigned immbits;
15677         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
15678                              0x0000100, 0x1000100, 0x0, 0x1000000};
15679
15680         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15681           return;
15682
15683         /* Fixed-point conversion with #0 immediate is encoded as an
15684            integer conversion.  */
15685         if (inst.operands[2].present && inst.operands[2].imm == 0)
15686           goto int_encode;
15687         NEON_ENCODE (IMMED, inst);
15688         if (flavour != neon_cvt_flavour_invalid)
15689           inst.instruction |= enctab[flavour];
15690         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15691         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15692         inst.instruction |= LOW4 (inst.operands[1].reg);
15693         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15694         inst.instruction |= neon_quad (rs) << 6;
15695         inst.instruction |= 1 << 21;
15696         if (flavour < neon_cvt_flavour_s16_f16)
15697           {
15698             inst.instruction |= 1 << 21;
15699             immbits = 32 - inst.operands[2].imm;
15700             inst.instruction |= immbits << 16;
15701           }
15702         else
15703           {
15704             inst.instruction |= 3 << 20;
15705             immbits = 16 - inst.operands[2].imm;
15706             inst.instruction |= immbits << 16;
15707             inst.instruction &= ~(1 << 9);
15708           }
15709
15710         neon_dp_fixup (&inst);
15711       }
15712       break;
15713
15714     case NS_DD:
15715     case NS_QQ:
15716       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15717         {
15718           NEON_ENCODE (FLOAT, inst);
15719           set_it_insn_type (OUTSIDE_IT_INSN);
15720
15721           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15722             return;
15723
15724           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15725           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15726           inst.instruction |= LOW4 (inst.operands[1].reg);
15727           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15728           inst.instruction |= neon_quad (rs) << 6;
15729           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
15730                                || flavour == neon_cvt_flavour_u32_f32) << 7;
15731           inst.instruction |= mode << 8;
15732           if (flavour == neon_cvt_flavour_u16_f16
15733               || flavour == neon_cvt_flavour_s16_f16)
15734             /* Mask off the original size bits and reencode them.  */
15735             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
15736
15737           if (thumb_mode)
15738             inst.instruction |= 0xfc000000;
15739           else
15740             inst.instruction |= 0xf0000000;
15741         }
15742       else
15743         {
15744     int_encode:
15745           {
15746             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
15747                                   0x100, 0x180, 0x0, 0x080};
15748
15749             NEON_ENCODE (INTEGER, inst);
15750
15751             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15752               return;
15753
15754             if (flavour != neon_cvt_flavour_invalid)
15755               inst.instruction |= enctab[flavour];
15756
15757             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15758             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15759             inst.instruction |= LOW4 (inst.operands[1].reg);
15760             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15761             inst.instruction |= neon_quad (rs) << 6;
15762             if (flavour >= neon_cvt_flavour_s16_f16
15763                 && flavour <= neon_cvt_flavour_f16_u16)
15764               /* Half precision.  */
15765               inst.instruction |= 1 << 18;
15766             else
15767               inst.instruction |= 2 << 18;
15768
15769             neon_dp_fixup (&inst);
15770           }
15771         }
15772       break;
15773
15774     /* Half-precision conversions for Advanced SIMD -- neon.  */
15775     case NS_QD:
15776     case NS_DQ:
15777
15778       if ((rs == NS_DQ)
15779           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15780           {
15781             as_bad (_("operand size must match register width"));
15782             break;
15783           }
15784
15785       if ((rs == NS_QD)
15786           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15787           {
15788             as_bad (_("operand size must match register width"));
15789             break;
15790           }
15791
15792       if (rs == NS_DQ)
15793         inst.instruction = 0x3b60600;
15794       else
15795         inst.instruction = 0x3b60700;
15796
15797       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15798       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15799       inst.instruction |= LOW4 (inst.operands[1].reg);
15800       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15801       neon_dp_fixup (&inst);
15802       break;
15803
15804     default:
15805       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
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 }
15812
15813 static void
15814 do_neon_cvtr (void)
15815 {
15816   do_neon_cvt_1 (neon_cvt_mode_x);
15817 }
15818
15819 static void
15820 do_neon_cvt (void)
15821 {
15822   do_neon_cvt_1 (neon_cvt_mode_z);
15823 }
15824
15825 static void
15826 do_neon_cvta (void)
15827 {
15828   do_neon_cvt_1 (neon_cvt_mode_a);
15829 }
15830
15831 static void
15832 do_neon_cvtn (void)
15833 {
15834   do_neon_cvt_1 (neon_cvt_mode_n);
15835 }
15836
15837 static void
15838 do_neon_cvtp (void)
15839 {
15840   do_neon_cvt_1 (neon_cvt_mode_p);
15841 }
15842
15843 static void
15844 do_neon_cvtm (void)
15845 {
15846   do_neon_cvt_1 (neon_cvt_mode_m);
15847 }
15848
15849 static void
15850 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15851 {
15852   if (is_double)
15853     mark_feature_used (&fpu_vfp_ext_armv8);
15854
15855   encode_arm_vfp_reg (inst.operands[0].reg,
15856                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
15857   encode_arm_vfp_reg (inst.operands[1].reg,
15858                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
15859   inst.instruction |= to ? 0x10000 : 0;
15860   inst.instruction |= t ? 0x80 : 0;
15861   inst.instruction |= is_double ? 0x100 : 0;
15862   do_vfp_cond_or_thumb ();
15863 }
15864
15865 static void
15866 do_neon_cvttb_1 (bfd_boolean t)
15867 {
15868   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
15869                                           NS_DF, NS_DH, NS_NULL);
15870
15871   if (rs == NS_NULL)
15872     return;
15873   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
15874     {
15875       inst.error = NULL;
15876       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
15877     }
15878   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
15879     {
15880       inst.error = NULL;
15881       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
15882     }
15883   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
15884     {
15885       /* The VCVTB and VCVTT instructions with D-register operands
15886          don't work for SP only targets.  */
15887       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15888                   _(BAD_FPU));
15889
15890       inst.error = NULL;
15891       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
15892     }
15893   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
15894     {
15895       /* The VCVTB and VCVTT instructions with D-register operands
15896          don't work for SP only targets.  */
15897       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15898                   _(BAD_FPU));
15899
15900       inst.error = NULL;
15901       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
15902     }
15903   else
15904     return;
15905 }
15906
15907 static void
15908 do_neon_cvtb (void)
15909 {
15910   do_neon_cvttb_1 (FALSE);
15911 }
15912
15913
15914 static void
15915 do_neon_cvtt (void)
15916 {
15917   do_neon_cvttb_1 (TRUE);
15918 }
15919
15920 static void
15921 neon_move_immediate (void)
15922 {
15923   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
15924   struct neon_type_el et = neon_check_type (2, rs,
15925     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
15926   unsigned immlo, immhi = 0, immbits;
15927   int op, cmode, float_p;
15928
15929   constraint (et.type == NT_invtype,
15930               _("operand size must be specified for immediate VMOV"));
15931
15932   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
15933   op = (inst.instruction & (1 << 5)) != 0;
15934
15935   immlo = inst.operands[1].imm;
15936   if (inst.operands[1].regisimm)
15937     immhi = inst.operands[1].reg;
15938
15939   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
15940               _("immediate has bits set outside the operand size"));
15941
15942   float_p = inst.operands[1].immisfloat;
15943
15944   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
15945                                         et.size, et.type)) == FAIL)
15946     {
15947       /* Invert relevant bits only.  */
15948       neon_invert_size (&immlo, &immhi, et.size);
15949       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
15950          with one or the other; those cases are caught by
15951          neon_cmode_for_move_imm.  */
15952       op = !op;
15953       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
15954                                             &op, et.size, et.type)) == FAIL)
15955         {
15956           first_error (_("immediate out of range"));
15957           return;
15958         }
15959     }
15960
15961   inst.instruction &= ~(1 << 5);
15962   inst.instruction |= op << 5;
15963
15964   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15965   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15966   inst.instruction |= neon_quad (rs) << 6;
15967   inst.instruction |= cmode << 8;
15968
15969   neon_write_immbits (immbits);
15970 }
15971
15972 static void
15973 do_neon_mvn (void)
15974 {
15975   if (inst.operands[1].isreg)
15976     {
15977       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15978
15979       NEON_ENCODE (INTEGER, inst);
15980       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15981       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15982       inst.instruction |= LOW4 (inst.operands[1].reg);
15983       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15984       inst.instruction |= neon_quad (rs) << 6;
15985     }
15986   else
15987     {
15988       NEON_ENCODE (IMMED, inst);
15989       neon_move_immediate ();
15990     }
15991
15992   neon_dp_fixup (&inst);
15993 }
15994
15995 /* Encode instructions of form:
15996
15997   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15998   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
15999
16000 static void
16001 neon_mixed_length (struct neon_type_el et, unsigned size)
16002 {
16003   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16004   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16005   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16006   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16007   inst.instruction |= LOW4 (inst.operands[2].reg);
16008   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16009   inst.instruction |= (et.type == NT_unsigned) << 24;
16010   inst.instruction |= neon_logbits (size) << 20;
16011
16012   neon_dp_fixup (&inst);
16013 }
16014
16015 static void
16016 do_neon_dyadic_long (void)
16017 {
16018   /* FIXME: Type checking for lengthening op.  */
16019   struct neon_type_el et = neon_check_type (3, NS_QDD,
16020     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
16021   neon_mixed_length (et, et.size);
16022 }
16023
16024 static void
16025 do_neon_abal (void)
16026 {
16027   struct neon_type_el et = neon_check_type (3, NS_QDD,
16028     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
16029   neon_mixed_length (et, et.size);
16030 }
16031
16032 static void
16033 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
16034 {
16035   if (inst.operands[2].isscalar)
16036     {
16037       struct neon_type_el et = neon_check_type (3, NS_QDS,
16038         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
16039       NEON_ENCODE (SCALAR, inst);
16040       neon_mul_mac (et, et.type == NT_unsigned);
16041     }
16042   else
16043     {
16044       struct neon_type_el et = neon_check_type (3, NS_QDD,
16045         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
16046       NEON_ENCODE (INTEGER, inst);
16047       neon_mixed_length (et, et.size);
16048     }
16049 }
16050
16051 static void
16052 do_neon_mac_maybe_scalar_long (void)
16053 {
16054   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
16055 }
16056
16057 static void
16058 do_neon_dyadic_wide (void)
16059 {
16060   struct neon_type_el et = neon_check_type (3, NS_QQD,
16061     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
16062   neon_mixed_length (et, et.size);
16063 }
16064
16065 static void
16066 do_neon_dyadic_narrow (void)
16067 {
16068   struct neon_type_el et = neon_check_type (3, NS_QDD,
16069     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
16070   /* Operand sign is unimportant, and the U bit is part of the opcode,
16071      so force the operand type to integer.  */
16072   et.type = NT_integer;
16073   neon_mixed_length (et, et.size / 2);
16074 }
16075
16076 static void
16077 do_neon_mul_sat_scalar_long (void)
16078 {
16079   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
16080 }
16081
16082 static void
16083 do_neon_vmull (void)
16084 {
16085   if (inst.operands[2].isscalar)
16086     do_neon_mac_maybe_scalar_long ();
16087   else
16088     {
16089       struct neon_type_el et = neon_check_type (3, NS_QDD,
16090         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
16091
16092       if (et.type == NT_poly)
16093         NEON_ENCODE (POLY, inst);
16094       else
16095         NEON_ENCODE (INTEGER, inst);
16096
16097       /* For polynomial encoding the U bit must be zero, and the size must
16098          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
16099          obviously, as 0b10).  */
16100       if (et.size == 64)
16101         {
16102           /* Check we're on the correct architecture.  */
16103           if (!mark_feature_used (&fpu_crypto_ext_armv8))
16104             inst.error =
16105               _("Instruction form not available on this architecture.");
16106
16107           et.size = 32;
16108         }
16109
16110       neon_mixed_length (et, et.size);
16111     }
16112 }
16113
16114 static void
16115 do_neon_ext (void)
16116 {
16117   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
16118   struct neon_type_el et = neon_check_type (3, rs,
16119     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16120   unsigned imm = (inst.operands[3].imm * et.size) / 8;
16121
16122   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
16123               _("shift out of range"));
16124   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16125   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16126   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16127   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16128   inst.instruction |= LOW4 (inst.operands[2].reg);
16129   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16130   inst.instruction |= neon_quad (rs) << 6;
16131   inst.instruction |= imm << 8;
16132
16133   neon_dp_fixup (&inst);
16134 }
16135
16136 static void
16137 do_neon_rev (void)
16138 {
16139   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16140   struct neon_type_el et = neon_check_type (2, rs,
16141     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16142   unsigned op = (inst.instruction >> 7) & 3;
16143   /* N (width of reversed regions) is encoded as part of the bitmask. We
16144      extract it here to check the elements to be reversed are smaller.
16145      Otherwise we'd get a reserved instruction.  */
16146   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
16147   gas_assert (elsize != 0);
16148   constraint (et.size >= elsize,
16149               _("elements must be smaller than reversal region"));
16150   neon_two_same (neon_quad (rs), 1, et.size);
16151 }
16152
16153 static void
16154 do_neon_dup (void)
16155 {
16156   if (inst.operands[1].isscalar)
16157     {
16158       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
16159       struct neon_type_el et = neon_check_type (2, rs,
16160         N_EQK, N_8 | N_16 | N_32 | N_KEY);
16161       unsigned sizebits = et.size >> 3;
16162       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
16163       int logsize = neon_logbits (et.size);
16164       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
16165
16166       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
16167         return;
16168
16169       NEON_ENCODE (SCALAR, inst);
16170       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16171       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16172       inst.instruction |= LOW4 (dm);
16173       inst.instruction |= HI1 (dm) << 5;
16174       inst.instruction |= neon_quad (rs) << 6;
16175       inst.instruction |= x << 17;
16176       inst.instruction |= sizebits << 16;
16177
16178       neon_dp_fixup (&inst);
16179     }
16180   else
16181     {
16182       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
16183       struct neon_type_el et = neon_check_type (2, rs,
16184         N_8 | N_16 | N_32 | N_KEY, N_EQK);
16185       /* Duplicate ARM register to lanes of vector.  */
16186       NEON_ENCODE (ARMREG, inst);
16187       switch (et.size)
16188         {
16189         case 8:  inst.instruction |= 0x400000; break;
16190         case 16: inst.instruction |= 0x000020; break;
16191         case 32: inst.instruction |= 0x000000; break;
16192         default: break;
16193         }
16194       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16195       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
16196       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
16197       inst.instruction |= neon_quad (rs) << 21;
16198       /* The encoding for this instruction is identical for the ARM and Thumb
16199          variants, except for the condition field.  */
16200       do_vfp_cond_or_thumb ();
16201     }
16202 }
16203
16204 /* VMOV has particularly many variations. It can be one of:
16205      0. VMOV<c><q> <Qd>, <Qm>
16206      1. VMOV<c><q> <Dd>, <Dm>
16207    (Register operations, which are VORR with Rm = Rn.)
16208      2. VMOV<c><q>.<dt> <Qd>, #<imm>
16209      3. VMOV<c><q>.<dt> <Dd>, #<imm>
16210    (Immediate loads.)
16211      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
16212    (ARM register to scalar.)
16213      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
16214    (Two ARM registers to vector.)
16215      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
16216    (Scalar to ARM register.)
16217      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
16218    (Vector to two ARM registers.)
16219      8. VMOV.F32 <Sd>, <Sm>
16220      9. VMOV.F64 <Dd>, <Dm>
16221    (VFP register moves.)
16222     10. VMOV.F32 <Sd>, #imm
16223     11. VMOV.F64 <Dd>, #imm
16224    (VFP float immediate load.)
16225     12. VMOV <Rd>, <Sm>
16226    (VFP single to ARM reg.)
16227     13. VMOV <Sd>, <Rm>
16228    (ARM reg to VFP single.)
16229     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
16230    (Two ARM regs to two VFP singles.)
16231     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
16232    (Two VFP singles to two ARM regs.)
16233
16234    These cases can be disambiguated using neon_select_shape, except cases 1/9
16235    and 3/11 which depend on the operand type too.
16236
16237    All the encoded bits are hardcoded by this function.
16238
16239    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
16240    Cases 5, 7 may be used with VFPv2 and above.
16241
16242    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
16243    can specify a type where it doesn't make sense to, and is ignored).  */
16244
16245 static void
16246 do_neon_mov (void)
16247 {
16248   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
16249                                           NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR,
16250                                           NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
16251                                           NS_HR, NS_RH, NS_HI, NS_NULL);
16252   struct neon_type_el et;
16253   const char *ldconst = 0;
16254
16255   switch (rs)
16256     {
16257     case NS_DD:  /* case 1/9.  */
16258       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16259       /* It is not an error here if no type is given.  */
16260       inst.error = NULL;
16261       if (et.type == NT_float && et.size == 64)
16262         {
16263           do_vfp_nsyn_opcode ("fcpyd");
16264           break;
16265         }
16266       /* fall through.  */
16267
16268     case NS_QQ:  /* case 0/1.  */
16269       {
16270         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16271           return;
16272         /* The architecture manual I have doesn't explicitly state which
16273            value the U bit should have for register->register moves, but
16274            the equivalent VORR instruction has U = 0, so do that.  */
16275         inst.instruction = 0x0200110;
16276         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16277         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16278         inst.instruction |= LOW4 (inst.operands[1].reg);
16279         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16280         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16281         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16282         inst.instruction |= neon_quad (rs) << 6;
16283
16284         neon_dp_fixup (&inst);
16285       }
16286       break;
16287
16288     case NS_DI:  /* case 3/11.  */
16289       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16290       inst.error = NULL;
16291       if (et.type == NT_float && et.size == 64)
16292         {
16293           /* case 11 (fconstd).  */
16294           ldconst = "fconstd";
16295           goto encode_fconstd;
16296         }
16297       /* fall through.  */
16298
16299     case NS_QI:  /* case 2/3.  */
16300       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16301         return;
16302       inst.instruction = 0x0800010;
16303       neon_move_immediate ();
16304       neon_dp_fixup (&inst);
16305       break;
16306
16307     case NS_SR:  /* case 4.  */
16308       {
16309         unsigned bcdebits = 0;
16310         int logsize;
16311         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
16312         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
16313
16314         /* .<size> is optional here, defaulting to .32. */
16315         if (inst.vectype.elems == 0
16316             && inst.operands[0].vectype.type == NT_invtype
16317             && inst.operands[1].vectype.type == NT_invtype)
16318           {
16319             inst.vectype.el[0].type = NT_untyped;
16320             inst.vectype.el[0].size = 32;
16321             inst.vectype.elems = 1;
16322           }
16323
16324         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
16325         logsize = neon_logbits (et.size);
16326
16327         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16328                     _(BAD_FPU));
16329         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16330                     && et.size != 32, _(BAD_FPU));
16331         constraint (et.type == NT_invtype, _("bad type for scalar"));
16332         constraint (x >= 64 / et.size, _("scalar index out of range"));
16333
16334         switch (et.size)
16335           {
16336           case 8:  bcdebits = 0x8; break;
16337           case 16: bcdebits = 0x1; break;
16338           case 32: bcdebits = 0x0; break;
16339           default: ;
16340           }
16341
16342         bcdebits |= x << logsize;
16343
16344         inst.instruction = 0xe000b10;
16345         do_vfp_cond_or_thumb ();
16346         inst.instruction |= LOW4 (dn) << 16;
16347         inst.instruction |= HI1 (dn) << 7;
16348         inst.instruction |= inst.operands[1].reg << 12;
16349         inst.instruction |= (bcdebits & 3) << 5;
16350         inst.instruction |= (bcdebits >> 2) << 21;
16351       }
16352       break;
16353
16354     case NS_DRR:  /* case 5 (fmdrr).  */
16355       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16356                   _(BAD_FPU));
16357
16358       inst.instruction = 0xc400b10;
16359       do_vfp_cond_or_thumb ();
16360       inst.instruction |= LOW4 (inst.operands[0].reg);
16361       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
16362       inst.instruction |= inst.operands[1].reg << 12;
16363       inst.instruction |= inst.operands[2].reg << 16;
16364       break;
16365
16366     case NS_RS:  /* case 6.  */
16367       {
16368         unsigned logsize;
16369         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
16370         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
16371         unsigned abcdebits = 0;
16372
16373         /* .<dt> is optional here, defaulting to .32. */
16374         if (inst.vectype.elems == 0
16375             && inst.operands[0].vectype.type == NT_invtype
16376             && inst.operands[1].vectype.type == NT_invtype)
16377           {
16378             inst.vectype.el[0].type = NT_untyped;
16379             inst.vectype.el[0].size = 32;
16380             inst.vectype.elems = 1;
16381           }
16382
16383         et = neon_check_type (2, NS_NULL,
16384                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
16385         logsize = neon_logbits (et.size);
16386
16387         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16388                     _(BAD_FPU));
16389         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16390                     && et.size != 32, _(BAD_FPU));
16391         constraint (et.type == NT_invtype, _("bad type for scalar"));
16392         constraint (x >= 64 / et.size, _("scalar index out of range"));
16393
16394         switch (et.size)
16395           {
16396           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
16397           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
16398           case 32: abcdebits = 0x00; break;
16399           default: ;
16400           }
16401
16402         abcdebits |= x << logsize;
16403         inst.instruction = 0xe100b10;
16404         do_vfp_cond_or_thumb ();
16405         inst.instruction |= LOW4 (dn) << 16;
16406         inst.instruction |= HI1 (dn) << 7;
16407         inst.instruction |= inst.operands[0].reg << 12;
16408         inst.instruction |= (abcdebits & 3) << 5;
16409         inst.instruction |= (abcdebits >> 2) << 21;
16410       }
16411       break;
16412
16413     case NS_RRD:  /* case 7 (fmrrd).  */
16414       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16415                   _(BAD_FPU));
16416
16417       inst.instruction = 0xc500b10;
16418       do_vfp_cond_or_thumb ();
16419       inst.instruction |= inst.operands[0].reg << 12;
16420       inst.instruction |= inst.operands[1].reg << 16;
16421       inst.instruction |= LOW4 (inst.operands[2].reg);
16422       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16423       break;
16424
16425     case NS_FF:  /* case 8 (fcpys).  */
16426       do_vfp_nsyn_opcode ("fcpys");
16427       break;
16428
16429     case NS_HI:
16430     case NS_FI:  /* case 10 (fconsts).  */
16431       ldconst = "fconsts";
16432       encode_fconstd:
16433       if (is_quarter_float (inst.operands[1].imm))
16434         {
16435           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
16436           do_vfp_nsyn_opcode (ldconst);
16437
16438           /* ARMv8.2 fp16 vmov.f16 instruction.  */
16439           if (rs == NS_HI)
16440             do_scalar_fp16_v82_encode ();
16441         }
16442       else
16443         first_error (_("immediate out of range"));
16444       break;
16445
16446     case NS_RH:
16447     case NS_RF:  /* case 12 (fmrs).  */
16448       do_vfp_nsyn_opcode ("fmrs");
16449       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16450       if (rs == NS_RH)
16451         do_scalar_fp16_v82_encode ();
16452       break;
16453
16454     case NS_HR:
16455     case NS_FR:  /* case 13 (fmsr).  */
16456       do_vfp_nsyn_opcode ("fmsr");
16457       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16458       if (rs == NS_HR)
16459         do_scalar_fp16_v82_encode ();
16460       break;
16461
16462     /* The encoders for the fmrrs and fmsrr instructions expect three operands
16463        (one of which is a list), but we have parsed four.  Do some fiddling to
16464        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
16465        expect.  */
16466     case NS_RRFF:  /* case 14 (fmrrs).  */
16467       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
16468                   _("VFP registers must be adjacent"));
16469       inst.operands[2].imm = 2;
16470       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16471       do_vfp_nsyn_opcode ("fmrrs");
16472       break;
16473
16474     case NS_FFRR:  /* case 15 (fmsrr).  */
16475       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
16476                   _("VFP registers must be adjacent"));
16477       inst.operands[1] = inst.operands[2];
16478       inst.operands[2] = inst.operands[3];
16479       inst.operands[0].imm = 2;
16480       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16481       do_vfp_nsyn_opcode ("fmsrr");
16482       break;
16483
16484     case NS_NULL:
16485       /* neon_select_shape has determined that the instruction
16486          shape is wrong and has already set the error message.  */
16487       break;
16488
16489     default:
16490       abort ();
16491     }
16492 }
16493
16494 static void
16495 do_neon_rshift_round_imm (void)
16496 {
16497   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16498   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16499   int imm = inst.operands[2].imm;
16500
16501   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
16502   if (imm == 0)
16503     {
16504       inst.operands[2].present = 0;
16505       do_neon_mov ();
16506       return;
16507     }
16508
16509   constraint (imm < 1 || (unsigned)imm > et.size,
16510               _("immediate out of range for shift"));
16511   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
16512                   et.size - imm);
16513 }
16514
16515 static void
16516 do_neon_movhf (void)
16517 {
16518   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
16519   constraint (rs != NS_HH, _("invalid suffix"));
16520
16521   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16522               _(BAD_FPU));
16523
16524   do_vfp_sp_monadic ();
16525
16526   inst.is_neon = 1;
16527   inst.instruction |= 0xf0000000;
16528 }
16529
16530 static void
16531 do_neon_movl (void)
16532 {
16533   struct neon_type_el et = neon_check_type (2, NS_QD,
16534     N_EQK | N_DBL, N_SU_32 | N_KEY);
16535   unsigned sizebits = et.size >> 3;
16536   inst.instruction |= sizebits << 19;
16537   neon_two_same (0, et.type == NT_unsigned, -1);
16538 }
16539
16540 static void
16541 do_neon_trn (void)
16542 {
16543   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16544   struct neon_type_el et = neon_check_type (2, rs,
16545     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16546   NEON_ENCODE (INTEGER, inst);
16547   neon_two_same (neon_quad (rs), 1, et.size);
16548 }
16549
16550 static void
16551 do_neon_zip_uzp (void)
16552 {
16553   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16554   struct neon_type_el et = neon_check_type (2, rs,
16555     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16556   if (rs == NS_DD && et.size == 32)
16557     {
16558       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16559       inst.instruction = N_MNEM_vtrn;
16560       do_neon_trn ();
16561       return;
16562     }
16563   neon_two_same (neon_quad (rs), 1, et.size);
16564 }
16565
16566 static void
16567 do_neon_sat_abs_neg (void)
16568 {
16569   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16570   struct neon_type_el et = neon_check_type (2, rs,
16571     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16572   neon_two_same (neon_quad (rs), 1, et.size);
16573 }
16574
16575 static void
16576 do_neon_pair_long (void)
16577 {
16578   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16579   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16580   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16581   inst.instruction |= (et.type == NT_unsigned) << 7;
16582   neon_two_same (neon_quad (rs), 1, et.size);
16583 }
16584
16585 static void
16586 do_neon_recip_est (void)
16587 {
16588   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16589   struct neon_type_el et = neon_check_type (2, rs,
16590     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
16591   inst.instruction |= (et.type == NT_float) << 8;
16592   neon_two_same (neon_quad (rs), 1, et.size);
16593 }
16594
16595 static void
16596 do_neon_cls (void)
16597 {
16598   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16599   struct neon_type_el et = neon_check_type (2, rs,
16600     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16601   neon_two_same (neon_quad (rs), 1, et.size);
16602 }
16603
16604 static void
16605 do_neon_clz (void)
16606 {
16607   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16608   struct neon_type_el et = neon_check_type (2, rs,
16609     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16610   neon_two_same (neon_quad (rs), 1, et.size);
16611 }
16612
16613 static void
16614 do_neon_cnt (void)
16615 {
16616   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16617   struct neon_type_el et = neon_check_type (2, rs,
16618     N_EQK | N_INT, N_8 | N_KEY);
16619   neon_two_same (neon_quad (rs), 1, et.size);
16620 }
16621
16622 static void
16623 do_neon_swp (void)
16624 {
16625   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16626   neon_two_same (neon_quad (rs), 1, -1);
16627 }
16628
16629 static void
16630 do_neon_tbl_tbx (void)
16631 {
16632   unsigned listlenbits;
16633   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16634
16635   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16636     {
16637       first_error (_("bad list length for table lookup"));
16638       return;
16639     }
16640
16641   listlenbits = inst.operands[1].imm - 1;
16642   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16643   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16644   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16645   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16646   inst.instruction |= LOW4 (inst.operands[2].reg);
16647   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16648   inst.instruction |= listlenbits << 8;
16649
16650   neon_dp_fixup (&inst);
16651 }
16652
16653 static void
16654 do_neon_ldm_stm (void)
16655 {
16656   /* P, U and L bits are part of bitmask.  */
16657   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16658   unsigned offsetbits = inst.operands[1].imm * 2;
16659
16660   if (inst.operands[1].issingle)
16661     {
16662       do_vfp_nsyn_ldm_stm (is_dbmode);
16663       return;
16664     }
16665
16666   constraint (is_dbmode && !inst.operands[0].writeback,
16667               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16668
16669   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16670               _("register list must contain at least 1 and at most 16 "
16671                 "registers"));
16672
16673   inst.instruction |= inst.operands[0].reg << 16;
16674   inst.instruction |= inst.operands[0].writeback << 21;
16675   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16676   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16677
16678   inst.instruction |= offsetbits;
16679
16680   do_vfp_cond_or_thumb ();
16681 }
16682
16683 static void
16684 do_neon_ldr_str (void)
16685 {
16686   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16687
16688   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16689      And is UNPREDICTABLE in thumb mode.  */
16690   if (!is_ldr
16691       && inst.operands[1].reg == REG_PC
16692       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16693     {
16694       if (thumb_mode)
16695         inst.error = _("Use of PC here is UNPREDICTABLE");
16696       else if (warn_on_deprecated)
16697         as_tsktsk (_("Use of PC here is deprecated"));
16698     }
16699
16700   if (inst.operands[0].issingle)
16701     {
16702       if (is_ldr)
16703         do_vfp_nsyn_opcode ("flds");
16704       else
16705         do_vfp_nsyn_opcode ("fsts");
16706
16707       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
16708       if (inst.vectype.el[0].size == 16)
16709         do_scalar_fp16_v82_encode ();
16710     }
16711   else
16712     {
16713       if (is_ldr)
16714         do_vfp_nsyn_opcode ("fldd");
16715       else
16716         do_vfp_nsyn_opcode ("fstd");
16717     }
16718 }
16719
16720 /* "interleave" version also handles non-interleaving register VLD1/VST1
16721    instructions.  */
16722
16723 static void
16724 do_neon_ld_st_interleave (void)
16725 {
16726   struct neon_type_el et = neon_check_type (1, NS_NULL,
16727                                             N_8 | N_16 | N_32 | N_64);
16728   unsigned alignbits = 0;
16729   unsigned idx;
16730   /* The bits in this table go:
16731      0: register stride of one (0) or two (1)
16732      1,2: register list length, minus one (1, 2, 3, 4).
16733      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
16734      We use -1 for invalid entries.  */
16735   const int typetable[] =
16736     {
16737       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
16738        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
16739        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
16740        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
16741     };
16742   int typebits;
16743
16744   if (et.type == NT_invtype)
16745     return;
16746
16747   if (inst.operands[1].immisalign)
16748     switch (inst.operands[1].imm >> 8)
16749       {
16750       case 64: alignbits = 1; break;
16751       case 128:
16752         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
16753             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16754           goto bad_alignment;
16755         alignbits = 2;
16756         break;
16757       case 256:
16758         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16759           goto bad_alignment;
16760         alignbits = 3;
16761         break;
16762       default:
16763       bad_alignment:
16764         first_error (_("bad alignment"));
16765         return;
16766       }
16767
16768   inst.instruction |= alignbits << 4;
16769   inst.instruction |= neon_logbits (et.size) << 6;
16770
16771   /* Bits [4:6] of the immediate in a list specifier encode register stride
16772      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
16773      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
16774      up the right value for "type" in a table based on this value and the given
16775      list style, then stick it back.  */
16776   idx = ((inst.operands[0].imm >> 4) & 7)
16777         | (((inst.instruction >> 8) & 3) << 3);
16778
16779   typebits = typetable[idx];
16780
16781   constraint (typebits == -1, _("bad list type for instruction"));
16782   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
16783               _("bad element type for instruction"));
16784
16785   inst.instruction &= ~0xf00;
16786   inst.instruction |= typebits << 8;
16787 }
16788
16789 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
16790    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
16791    otherwise. The variable arguments are a list of pairs of legal (size, align)
16792    values, terminated with -1.  */
16793
16794 static int
16795 neon_alignment_bit (int size, int align, int *do_alignment, ...)
16796 {
16797   va_list ap;
16798   int result = FAIL, thissize, thisalign;
16799
16800   if (!inst.operands[1].immisalign)
16801     {
16802       *do_alignment = 0;
16803       return SUCCESS;
16804     }
16805
16806   va_start (ap, do_alignment);
16807
16808   do
16809     {
16810       thissize = va_arg (ap, int);
16811       if (thissize == -1)
16812         break;
16813       thisalign = va_arg (ap, int);
16814
16815       if (size == thissize && align == thisalign)
16816         result = SUCCESS;
16817     }
16818   while (result != SUCCESS);
16819
16820   va_end (ap);
16821
16822   if (result == SUCCESS)
16823     *do_alignment = 1;
16824   else
16825     first_error (_("unsupported alignment for instruction"));
16826
16827   return result;
16828 }
16829
16830 static void
16831 do_neon_ld_st_lane (void)
16832 {
16833   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16834   int align_good, do_alignment = 0;
16835   int logsize = neon_logbits (et.size);
16836   int align = inst.operands[1].imm >> 8;
16837   int n = (inst.instruction >> 8) & 3;
16838   int max_el = 64 / et.size;
16839
16840   if (et.type == NT_invtype)
16841     return;
16842
16843   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
16844               _("bad list length"));
16845   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
16846               _("scalar index out of range"));
16847   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
16848               && et.size == 8,
16849               _("stride of 2 unavailable when element size is 8"));
16850
16851   switch (n)
16852     {
16853     case 0:  /* VLD1 / VST1.  */
16854       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
16855                                        32, 32, -1);
16856       if (align_good == FAIL)
16857         return;
16858       if (do_alignment)
16859         {
16860           unsigned alignbits = 0;
16861           switch (et.size)
16862             {
16863             case 16: alignbits = 0x1; break;
16864             case 32: alignbits = 0x3; break;
16865             default: ;
16866             }
16867           inst.instruction |= alignbits << 4;
16868         }
16869       break;
16870
16871     case 1:  /* VLD2 / VST2.  */
16872       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
16873                       16, 32, 32, 64, -1);
16874       if (align_good == FAIL)
16875         return;
16876       if (do_alignment)
16877         inst.instruction |= 1 << 4;
16878       break;
16879
16880     case 2:  /* VLD3 / VST3.  */
16881       constraint (inst.operands[1].immisalign,
16882                   _("can't use alignment with this instruction"));
16883       break;
16884
16885     case 3:  /* VLD4 / VST4.  */
16886       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
16887                                        16, 64, 32, 64, 32, 128, -1);
16888       if (align_good == FAIL)
16889         return;
16890       if (do_alignment)
16891         {
16892           unsigned alignbits = 0;
16893           switch (et.size)
16894             {
16895             case 8:  alignbits = 0x1; break;
16896             case 16: alignbits = 0x1; break;
16897             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
16898             default: ;
16899             }
16900           inst.instruction |= alignbits << 4;
16901         }
16902       break;
16903
16904     default: ;
16905     }
16906
16907   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
16908   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16909     inst.instruction |= 1 << (4 + logsize);
16910
16911   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
16912   inst.instruction |= logsize << 10;
16913 }
16914
16915 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
16916
16917 static void
16918 do_neon_ld_dup (void)
16919 {
16920   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16921   int align_good, do_alignment = 0;
16922
16923   if (et.type == NT_invtype)
16924     return;
16925
16926   switch ((inst.instruction >> 8) & 3)
16927     {
16928     case 0:  /* VLD1.  */
16929       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
16930       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16931                                        &do_alignment, 16, 16, 32, 32, -1);
16932       if (align_good == FAIL)
16933         return;
16934       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
16935         {
16936         case 1: break;
16937         case 2: inst.instruction |= 1 << 5; break;
16938         default: first_error (_("bad list length")); return;
16939         }
16940       inst.instruction |= neon_logbits (et.size) << 6;
16941       break;
16942
16943     case 1:  /* VLD2.  */
16944       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16945                                        &do_alignment, 8, 16, 16, 32, 32, 64,
16946                                        -1);
16947       if (align_good == FAIL)
16948         return;
16949       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
16950                   _("bad list length"));
16951       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16952         inst.instruction |= 1 << 5;
16953       inst.instruction |= neon_logbits (et.size) << 6;
16954       break;
16955
16956     case 2:  /* VLD3.  */
16957       constraint (inst.operands[1].immisalign,
16958                   _("can't use alignment with this instruction"));
16959       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
16960                   _("bad list length"));
16961       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16962         inst.instruction |= 1 << 5;
16963       inst.instruction |= neon_logbits (et.size) << 6;
16964       break;
16965
16966     case 3:  /* VLD4.  */
16967       {
16968         int align = inst.operands[1].imm >> 8;
16969         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
16970                                          16, 64, 32, 64, 32, 128, -1);
16971         if (align_good == FAIL)
16972           return;
16973         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
16974                     _("bad list length"));
16975         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16976           inst.instruction |= 1 << 5;
16977         if (et.size == 32 && align == 128)
16978           inst.instruction |= 0x3 << 6;
16979         else
16980           inst.instruction |= neon_logbits (et.size) << 6;
16981       }
16982       break;
16983
16984     default: ;
16985     }
16986
16987   inst.instruction |= do_alignment << 4;
16988 }
16989
16990 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
16991    apart from bits [11:4].  */
16992
16993 static void
16994 do_neon_ldx_stx (void)
16995 {
16996   if (inst.operands[1].isreg)
16997     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16998
16999   switch (NEON_LANE (inst.operands[0].imm))
17000     {
17001     case NEON_INTERLEAVE_LANES:
17002       NEON_ENCODE (INTERLV, inst);
17003       do_neon_ld_st_interleave ();
17004       break;
17005
17006     case NEON_ALL_LANES:
17007       NEON_ENCODE (DUP, inst);
17008       if (inst.instruction == N_INV)
17009         {
17010           first_error ("only loads support such operands");
17011           break;
17012         }
17013       do_neon_ld_dup ();
17014       break;
17015
17016     default:
17017       NEON_ENCODE (LANE, inst);
17018       do_neon_ld_st_lane ();
17019     }
17020
17021   /* L bit comes from bit mask.  */
17022   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17023   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17024   inst.instruction |= inst.operands[1].reg << 16;
17025
17026   if (inst.operands[1].postind)
17027     {
17028       int postreg = inst.operands[1].imm & 0xf;
17029       constraint (!inst.operands[1].immisreg,
17030                   _("post-index must be a register"));
17031       constraint (postreg == 0xd || postreg == 0xf,
17032                   _("bad register for post-index"));
17033       inst.instruction |= postreg;
17034     }
17035   else
17036     {
17037       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17038       constraint (inst.reloc.exp.X_op != O_constant
17039                   || inst.reloc.exp.X_add_number != 0,
17040                   BAD_ADDR_MODE);
17041
17042       if (inst.operands[1].writeback)
17043         {
17044           inst.instruction |= 0xd;
17045         }
17046       else
17047         inst.instruction |= 0xf;
17048     }
17049
17050   if (thumb_mode)
17051     inst.instruction |= 0xf9000000;
17052   else
17053     inst.instruction |= 0xf4000000;
17054 }
17055
17056 /* FP v8.  */
17057 static void
17058 do_vfp_nsyn_fpv8 (enum neon_shape rs)
17059 {
17060   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17061      D register operands.  */
17062   if (neon_shape_class[rs] == SC_DOUBLE)
17063     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17064                 _(BAD_FPU));
17065
17066   NEON_ENCODE (FPV8, inst);
17067
17068   if (rs == NS_FFF || rs == NS_HHH)
17069     {
17070       do_vfp_sp_dyadic ();
17071
17072       /* ARMv8.2 fp16 instruction.  */
17073       if (rs == NS_HHH)
17074         do_scalar_fp16_v82_encode ();
17075     }
17076   else
17077     do_vfp_dp_rd_rn_rm ();
17078
17079   if (rs == NS_DDD)
17080     inst.instruction |= 0x100;
17081
17082   inst.instruction |= 0xf0000000;
17083 }
17084
17085 static void
17086 do_vsel (void)
17087 {
17088   set_it_insn_type (OUTSIDE_IT_INSN);
17089
17090   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
17091     first_error (_("invalid instruction shape"));
17092 }
17093
17094 static void
17095 do_vmaxnm (void)
17096 {
17097   set_it_insn_type (OUTSIDE_IT_INSN);
17098
17099   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
17100     return;
17101
17102   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17103     return;
17104
17105   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
17106 }
17107
17108 static void
17109 do_vrint_1 (enum neon_cvt_mode mode)
17110 {
17111   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
17112   struct neon_type_el et;
17113
17114   if (rs == NS_NULL)
17115     return;
17116
17117   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17118      D register operands.  */
17119   if (neon_shape_class[rs] == SC_DOUBLE)
17120     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17121                 _(BAD_FPU));
17122
17123   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
17124                         | N_VFP);
17125   if (et.type != NT_invtype)
17126     {
17127       /* VFP encodings.  */
17128       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
17129           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
17130         set_it_insn_type (OUTSIDE_IT_INSN);
17131
17132       NEON_ENCODE (FPV8, inst);
17133       if (rs == NS_FF || rs == NS_HH)
17134         do_vfp_sp_monadic ();
17135       else
17136         do_vfp_dp_rd_rm ();
17137
17138       switch (mode)
17139         {
17140         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
17141         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
17142         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
17143         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
17144         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
17145         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
17146         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
17147         default: abort ();
17148         }
17149
17150       inst.instruction |= (rs == NS_DD) << 8;
17151       do_vfp_cond_or_thumb ();
17152
17153       /* ARMv8.2 fp16 vrint instruction.  */
17154       if (rs == NS_HH)
17155       do_scalar_fp16_v82_encode ();
17156     }
17157   else
17158     {
17159       /* Neon encodings (or something broken...).  */
17160       inst.error = NULL;
17161       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
17162
17163       if (et.type == NT_invtype)
17164         return;
17165
17166       set_it_insn_type (OUTSIDE_IT_INSN);
17167       NEON_ENCODE (FLOAT, inst);
17168
17169       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17170         return;
17171
17172       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17173       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17174       inst.instruction |= LOW4 (inst.operands[1].reg);
17175       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17176       inst.instruction |= neon_quad (rs) << 6;
17177       /* Mask off the original size bits and reencode them.  */
17178       inst.instruction = ((inst.instruction & 0xfff3ffff)
17179                           | neon_logbits (et.size) << 18);
17180
17181       switch (mode)
17182         {
17183         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
17184         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
17185         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
17186         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
17187         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
17188         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
17189         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
17190         default: abort ();
17191         }
17192
17193       if (thumb_mode)
17194         inst.instruction |= 0xfc000000;
17195       else
17196         inst.instruction |= 0xf0000000;
17197     }
17198 }
17199
17200 static void
17201 do_vrintx (void)
17202 {
17203   do_vrint_1 (neon_cvt_mode_x);
17204 }
17205
17206 static void
17207 do_vrintz (void)
17208 {
17209   do_vrint_1 (neon_cvt_mode_z);
17210 }
17211
17212 static void
17213 do_vrintr (void)
17214 {
17215   do_vrint_1 (neon_cvt_mode_r);
17216 }
17217
17218 static void
17219 do_vrinta (void)
17220 {
17221   do_vrint_1 (neon_cvt_mode_a);
17222 }
17223
17224 static void
17225 do_vrintn (void)
17226 {
17227   do_vrint_1 (neon_cvt_mode_n);
17228 }
17229
17230 static void
17231 do_vrintp (void)
17232 {
17233   do_vrint_1 (neon_cvt_mode_p);
17234 }
17235
17236 static void
17237 do_vrintm (void)
17238 {
17239   do_vrint_1 (neon_cvt_mode_m);
17240 }
17241
17242 /* Crypto v1 instructions.  */
17243 static void
17244 do_crypto_2op_1 (unsigned elttype, int op)
17245 {
17246   set_it_insn_type (OUTSIDE_IT_INSN);
17247
17248   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
17249       == NT_invtype)
17250     return;
17251
17252   inst.error = NULL;
17253
17254   NEON_ENCODE (INTEGER, inst);
17255   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17256   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17257   inst.instruction |= LOW4 (inst.operands[1].reg);
17258   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17259   if (op != -1)
17260     inst.instruction |= op << 6;
17261
17262   if (thumb_mode)
17263     inst.instruction |= 0xfc000000;
17264   else
17265     inst.instruction |= 0xf0000000;
17266 }
17267
17268 static void
17269 do_crypto_3op_1 (int u, int op)
17270 {
17271   set_it_insn_type (OUTSIDE_IT_INSN);
17272
17273   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
17274                        N_32 | N_UNT | N_KEY).type == NT_invtype)
17275     return;
17276
17277   inst.error = NULL;
17278
17279   NEON_ENCODE (INTEGER, inst);
17280   neon_three_same (1, u, 8 << op);
17281 }
17282
17283 static void
17284 do_aese (void)
17285 {
17286   do_crypto_2op_1 (N_8, 0);
17287 }
17288
17289 static void
17290 do_aesd (void)
17291 {
17292   do_crypto_2op_1 (N_8, 1);
17293 }
17294
17295 static void
17296 do_aesmc (void)
17297 {
17298   do_crypto_2op_1 (N_8, 2);
17299 }
17300
17301 static void
17302 do_aesimc (void)
17303 {
17304   do_crypto_2op_1 (N_8, 3);
17305 }
17306
17307 static void
17308 do_sha1c (void)
17309 {
17310   do_crypto_3op_1 (0, 0);
17311 }
17312
17313 static void
17314 do_sha1p (void)
17315 {
17316   do_crypto_3op_1 (0, 1);
17317 }
17318
17319 static void
17320 do_sha1m (void)
17321 {
17322   do_crypto_3op_1 (0, 2);
17323 }
17324
17325 static void
17326 do_sha1su0 (void)
17327 {
17328   do_crypto_3op_1 (0, 3);
17329 }
17330
17331 static void
17332 do_sha256h (void)
17333 {
17334   do_crypto_3op_1 (1, 0);
17335 }
17336
17337 static void
17338 do_sha256h2 (void)
17339 {
17340   do_crypto_3op_1 (1, 1);
17341 }
17342
17343 static void
17344 do_sha256su1 (void)
17345 {
17346   do_crypto_3op_1 (1, 2);
17347 }
17348
17349 static void
17350 do_sha1h (void)
17351 {
17352   do_crypto_2op_1 (N_32, -1);
17353 }
17354
17355 static void
17356 do_sha1su1 (void)
17357 {
17358   do_crypto_2op_1 (N_32, 0);
17359 }
17360
17361 static void
17362 do_sha256su0 (void)
17363 {
17364   do_crypto_2op_1 (N_32, 1);
17365 }
17366
17367 static void
17368 do_crc32_1 (unsigned int poly, unsigned int sz)
17369 {
17370   unsigned int Rd = inst.operands[0].reg;
17371   unsigned int Rn = inst.operands[1].reg;
17372   unsigned int Rm = inst.operands[2].reg;
17373
17374   set_it_insn_type (OUTSIDE_IT_INSN);
17375   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
17376   inst.instruction |= LOW4 (Rn) << 16;
17377   inst.instruction |= LOW4 (Rm);
17378   inst.instruction |= sz << (thumb_mode ? 4 : 21);
17379   inst.instruction |= poly << (thumb_mode ? 20 : 9);
17380
17381   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
17382     as_warn (UNPRED_REG ("r15"));
17383   if (thumb_mode && (Rd == REG_SP || Rn == REG_SP || Rm == REG_SP))
17384     as_warn (UNPRED_REG ("r13"));
17385 }
17386
17387 static void
17388 do_crc32b (void)
17389 {
17390   do_crc32_1 (0, 0);
17391 }
17392
17393 static void
17394 do_crc32h (void)
17395 {
17396   do_crc32_1 (0, 1);
17397 }
17398
17399 static void
17400 do_crc32w (void)
17401 {
17402   do_crc32_1 (0, 2);
17403 }
17404
17405 static void
17406 do_crc32cb (void)
17407 {
17408   do_crc32_1 (1, 0);
17409 }
17410
17411 static void
17412 do_crc32ch (void)
17413 {
17414   do_crc32_1 (1, 1);
17415 }
17416
17417 static void
17418 do_crc32cw (void)
17419 {
17420   do_crc32_1 (1, 2);
17421 }
17422
17423 \f
17424 /* Overall per-instruction processing.  */
17425
17426 /* We need to be able to fix up arbitrary expressions in some statements.
17427    This is so that we can handle symbols that are an arbitrary distance from
17428    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
17429    which returns part of an address in a form which will be valid for
17430    a data instruction.  We do this by pushing the expression into a symbol
17431    in the expr_section, and creating a fix for that.  */
17432
17433 static void
17434 fix_new_arm (fragS *       frag,
17435              int           where,
17436              short int     size,
17437              expressionS * exp,
17438              int           pc_rel,
17439              int           reloc)
17440 {
17441   fixS *           new_fix;
17442
17443   switch (exp->X_op)
17444     {
17445     case O_constant:
17446       if (pc_rel)
17447         {
17448           /* Create an absolute valued symbol, so we have something to
17449              refer to in the object file.  Unfortunately for us, gas's
17450              generic expression parsing will already have folded out
17451              any use of .set foo/.type foo %function that may have
17452              been used to set type information of the target location,
17453              that's being specified symbolically.  We have to presume
17454              the user knows what they are doing.  */
17455           char name[16 + 8];
17456           symbolS *symbol;
17457
17458           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
17459
17460           symbol = symbol_find_or_make (name);
17461           S_SET_SEGMENT (symbol, absolute_section);
17462           symbol_set_frag (symbol, &zero_address_frag);
17463           S_SET_VALUE (symbol, exp->X_add_number);
17464           exp->X_op = O_symbol;
17465           exp->X_add_symbol = symbol;
17466           exp->X_add_number = 0;
17467         }
17468       /* FALLTHROUGH */
17469     case O_symbol:
17470     case O_add:
17471     case O_subtract:
17472       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
17473                              (enum bfd_reloc_code_real) reloc);
17474       break;
17475
17476     default:
17477       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
17478                                   pc_rel, (enum bfd_reloc_code_real) reloc);
17479       break;
17480     }
17481
17482   /* Mark whether the fix is to a THUMB instruction, or an ARM
17483      instruction.  */
17484   new_fix->tc_fix_data = thumb_mode;
17485 }
17486
17487 /* Create a frg for an instruction requiring relaxation.  */
17488 static void
17489 output_relax_insn (void)
17490 {
17491   char * to;
17492   symbolS *sym;
17493   int offset;
17494
17495   /* The size of the instruction is unknown, so tie the debug info to the
17496      start of the instruction.  */
17497   dwarf2_emit_insn (0);
17498
17499   switch (inst.reloc.exp.X_op)
17500     {
17501     case O_symbol:
17502       sym = inst.reloc.exp.X_add_symbol;
17503       offset = inst.reloc.exp.X_add_number;
17504       break;
17505     case O_constant:
17506       sym = NULL;
17507       offset = inst.reloc.exp.X_add_number;
17508       break;
17509     default:
17510       sym = make_expr_symbol (&inst.reloc.exp);
17511       offset = 0;
17512       break;
17513   }
17514   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
17515                  inst.relax, sym, offset, NULL/*offset, opcode*/);
17516   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
17517 }
17518
17519 /* Write a 32-bit thumb instruction to buf.  */
17520 static void
17521 put_thumb32_insn (char * buf, unsigned long insn)
17522 {
17523   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
17524   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
17525 }
17526
17527 static void
17528 output_inst (const char * str)
17529 {
17530   char * to = NULL;
17531
17532   if (inst.error)
17533     {
17534       as_bad ("%s -- `%s'", inst.error, str);
17535       return;
17536     }
17537   if (inst.relax)
17538     {
17539       output_relax_insn ();
17540       return;
17541     }
17542   if (inst.size == 0)
17543     return;
17544
17545   to = frag_more (inst.size);
17546   /* PR 9814: Record the thumb mode into the current frag so that we know
17547      what type of NOP padding to use, if necessary.  We override any previous
17548      setting so that if the mode has changed then the NOPS that we use will
17549      match the encoding of the last instruction in the frag.  */
17550   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
17551
17552   if (thumb_mode && (inst.size > THUMB_SIZE))
17553     {
17554       gas_assert (inst.size == (2 * THUMB_SIZE));
17555       put_thumb32_insn (to, inst.instruction);
17556     }
17557   else if (inst.size > INSN_SIZE)
17558     {
17559       gas_assert (inst.size == (2 * INSN_SIZE));
17560       md_number_to_chars (to, inst.instruction, INSN_SIZE);
17561       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
17562     }
17563   else
17564     md_number_to_chars (to, inst.instruction, inst.size);
17565
17566   if (inst.reloc.type != BFD_RELOC_UNUSED)
17567     fix_new_arm (frag_now, to - frag_now->fr_literal,
17568                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
17569                  inst.reloc.type);
17570
17571   dwarf2_emit_insn (inst.size);
17572 }
17573
17574 static char *
17575 output_it_inst (int cond, int mask, char * to)
17576 {
17577   unsigned long instruction = 0xbf00;
17578
17579   mask &= 0xf;
17580   instruction |= mask;
17581   instruction |= cond << 4;
17582
17583   if (to == NULL)
17584     {
17585       to = frag_more (2);
17586 #ifdef OBJ_ELF
17587       dwarf2_emit_insn (2);
17588 #endif
17589     }
17590
17591   md_number_to_chars (to, instruction, 2);
17592
17593   return to;
17594 }
17595
17596 /* Tag values used in struct asm_opcode's tag field.  */
17597 enum opcode_tag
17598 {
17599   OT_unconditional,     /* Instruction cannot be conditionalized.
17600                            The ARM condition field is still 0xE.  */
17601   OT_unconditionalF,    /* Instruction cannot be conditionalized
17602                            and carries 0xF in its ARM condition field.  */
17603   OT_csuffix,           /* Instruction takes a conditional suffix.  */
17604   OT_csuffixF,          /* Some forms of the instruction take a conditional
17605                            suffix, others place 0xF where the condition field
17606                            would be.  */
17607   OT_cinfix3,           /* Instruction takes a conditional infix,
17608                            beginning at character index 3.  (In
17609                            unified mode, it becomes a suffix.)  */
17610   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
17611                             tsts, cmps, cmns, and teqs. */
17612   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
17613                            character index 3, even in unified mode.  Used for
17614                            legacy instructions where suffix and infix forms
17615                            may be ambiguous.  */
17616   OT_csuf_or_in3,       /* Instruction takes either a conditional
17617                            suffix or an infix at character index 3.  */
17618   OT_odd_infix_unc,     /* This is the unconditional variant of an
17619                            instruction that takes a conditional infix
17620                            at an unusual position.  In unified mode,
17621                            this variant will accept a suffix.  */
17622   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
17623                            are the conditional variants of instructions that
17624                            take conditional infixes in unusual positions.
17625                            The infix appears at character index
17626                            (tag - OT_odd_infix_0).  These are not accepted
17627                            in unified mode.  */
17628 };
17629
17630 /* Subroutine of md_assemble, responsible for looking up the primary
17631    opcode from the mnemonic the user wrote.  STR points to the
17632    beginning of the mnemonic.
17633
17634    This is not simply a hash table lookup, because of conditional
17635    variants.  Most instructions have conditional variants, which are
17636    expressed with a _conditional affix_ to the mnemonic.  If we were
17637    to encode each conditional variant as a literal string in the opcode
17638    table, it would have approximately 20,000 entries.
17639
17640    Most mnemonics take this affix as a suffix, and in unified syntax,
17641    'most' is upgraded to 'all'.  However, in the divided syntax, some
17642    instructions take the affix as an infix, notably the s-variants of
17643    the arithmetic instructions.  Of those instructions, all but six
17644    have the infix appear after the third character of the mnemonic.
17645
17646    Accordingly, the algorithm for looking up primary opcodes given
17647    an identifier is:
17648
17649    1. Look up the identifier in the opcode table.
17650       If we find a match, go to step U.
17651
17652    2. Look up the last two characters of the identifier in the
17653       conditions table.  If we find a match, look up the first N-2
17654       characters of the identifier in the opcode table.  If we
17655       find a match, go to step CE.
17656
17657    3. Look up the fourth and fifth characters of the identifier in
17658       the conditions table.  If we find a match, extract those
17659       characters from the identifier, and look up the remaining
17660       characters in the opcode table.  If we find a match, go
17661       to step CM.
17662
17663    4. Fail.
17664
17665    U. Examine the tag field of the opcode structure, in case this is
17666       one of the six instructions with its conditional infix in an
17667       unusual place.  If it is, the tag tells us where to find the
17668       infix; look it up in the conditions table and set inst.cond
17669       accordingly.  Otherwise, this is an unconditional instruction.
17670       Again set inst.cond accordingly.  Return the opcode structure.
17671
17672   CE. Examine the tag field to make sure this is an instruction that
17673       should receive a conditional suffix.  If it is not, fail.
17674       Otherwise, set inst.cond from the suffix we already looked up,
17675       and return the opcode structure.
17676
17677   CM. Examine the tag field to make sure this is an instruction that
17678       should receive a conditional infix after the third character.
17679       If it is not, fail.  Otherwise, undo the edits to the current
17680       line of input and proceed as for case CE.  */
17681
17682 static const struct asm_opcode *
17683 opcode_lookup (char **str)
17684 {
17685   char *end, *base;
17686   char *affix;
17687   const struct asm_opcode *opcode;
17688   const struct asm_cond *cond;
17689   char save[2];
17690
17691   /* Scan up to the end of the mnemonic, which must end in white space,
17692      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
17693   for (base = end = *str; *end != '\0'; end++)
17694     if (*end == ' ' || *end == '.')
17695       break;
17696
17697   if (end == base)
17698     return NULL;
17699
17700   /* Handle a possible width suffix and/or Neon type suffix.  */
17701   if (end[0] == '.')
17702     {
17703       int offset = 2;
17704
17705       /* The .w and .n suffixes are only valid if the unified syntax is in
17706          use.  */
17707       if (unified_syntax && end[1] == 'w')
17708         inst.size_req = 4;
17709       else if (unified_syntax && end[1] == 'n')
17710         inst.size_req = 2;
17711       else
17712         offset = 0;
17713
17714       inst.vectype.elems = 0;
17715
17716       *str = end + offset;
17717
17718       if (end[offset] == '.')
17719         {
17720           /* See if we have a Neon type suffix (possible in either unified or
17721              non-unified ARM syntax mode).  */
17722           if (parse_neon_type (&inst.vectype, str) == FAIL)
17723             return NULL;
17724         }
17725       else if (end[offset] != '\0' && end[offset] != ' ')
17726         return NULL;
17727     }
17728   else
17729     *str = end;
17730
17731   /* Look for unaffixed or special-case affixed mnemonic.  */
17732   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17733                                                     end - base);
17734   if (opcode)
17735     {
17736       /* step U */
17737       if (opcode->tag < OT_odd_infix_0)
17738         {
17739           inst.cond = COND_ALWAYS;
17740           return opcode;
17741         }
17742
17743       if (warn_on_deprecated && unified_syntax)
17744         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17745       affix = base + (opcode->tag - OT_odd_infix_0);
17746       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17747       gas_assert (cond);
17748
17749       inst.cond = cond->value;
17750       return opcode;
17751     }
17752
17753   /* Cannot have a conditional suffix on a mnemonic of less than two
17754      characters.  */
17755   if (end - base < 3)
17756     return NULL;
17757
17758   /* Look for suffixed mnemonic.  */
17759   affix = end - 2;
17760   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17761   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17762                                                     affix - base);
17763   if (opcode && cond)
17764     {
17765       /* step CE */
17766       switch (opcode->tag)
17767         {
17768         case OT_cinfix3_legacy:
17769           /* Ignore conditional suffixes matched on infix only mnemonics.  */
17770           break;
17771
17772         case OT_cinfix3:
17773         case OT_cinfix3_deprecated:
17774         case OT_odd_infix_unc:
17775           if (!unified_syntax)
17776             return 0;
17777           /* Fall through.  */
17778
17779         case OT_csuffix:
17780         case OT_csuffixF:
17781         case OT_csuf_or_in3:
17782           inst.cond = cond->value;
17783           return opcode;
17784
17785         case OT_unconditional:
17786         case OT_unconditionalF:
17787           if (thumb_mode)
17788             inst.cond = cond->value;
17789           else
17790             {
17791               /* Delayed diagnostic.  */
17792               inst.error = BAD_COND;
17793               inst.cond = COND_ALWAYS;
17794             }
17795           return opcode;
17796
17797         default:
17798           return NULL;
17799         }
17800     }
17801
17802   /* Cannot have a usual-position infix on a mnemonic of less than
17803      six characters (five would be a suffix).  */
17804   if (end - base < 6)
17805     return NULL;
17806
17807   /* Look for infixed mnemonic in the usual position.  */
17808   affix = base + 3;
17809   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17810   if (!cond)
17811     return NULL;
17812
17813   memcpy (save, affix, 2);
17814   memmove (affix, affix + 2, (end - affix) - 2);
17815   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17816                                                     (end - base) - 2);
17817   memmove (affix + 2, affix, (end - affix) - 2);
17818   memcpy (affix, save, 2);
17819
17820   if (opcode
17821       && (opcode->tag == OT_cinfix3
17822           || opcode->tag == OT_cinfix3_deprecated
17823           || opcode->tag == OT_csuf_or_in3
17824           || opcode->tag == OT_cinfix3_legacy))
17825     {
17826       /* Step CM.  */
17827       if (warn_on_deprecated && unified_syntax
17828           && (opcode->tag == OT_cinfix3
17829               || opcode->tag == OT_cinfix3_deprecated))
17830         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17831
17832       inst.cond = cond->value;
17833       return opcode;
17834     }
17835
17836   return NULL;
17837 }
17838
17839 /* This function generates an initial IT instruction, leaving its block
17840    virtually open for the new instructions. Eventually,
17841    the mask will be updated by now_it_add_mask () each time
17842    a new instruction needs to be included in the IT block.
17843    Finally, the block is closed with close_automatic_it_block ().
17844    The block closure can be requested either from md_assemble (),
17845    a tencode (), or due to a label hook.  */
17846
17847 static void
17848 new_automatic_it_block (int cond)
17849 {
17850   now_it.state = AUTOMATIC_IT_BLOCK;
17851   now_it.mask = 0x18;
17852   now_it.cc = cond;
17853   now_it.block_length = 1;
17854   mapping_state (MAP_THUMB);
17855   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
17856   now_it.warn_deprecated = FALSE;
17857   now_it.insn_cond = TRUE;
17858 }
17859
17860 /* Close an automatic IT block.
17861    See comments in new_automatic_it_block ().  */
17862
17863 static void
17864 close_automatic_it_block (void)
17865 {
17866   now_it.mask = 0x10;
17867   now_it.block_length = 0;
17868 }
17869
17870 /* Update the mask of the current automatically-generated IT
17871    instruction. See comments in new_automatic_it_block ().  */
17872
17873 static void
17874 now_it_add_mask (int cond)
17875 {
17876 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
17877 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
17878                                               | ((bitvalue) << (nbit)))
17879   const int resulting_bit = (cond & 1);
17880
17881   now_it.mask &= 0xf;
17882   now_it.mask = SET_BIT_VALUE (now_it.mask,
17883                                    resulting_bit,
17884                                   (5 - now_it.block_length));
17885   now_it.mask = SET_BIT_VALUE (now_it.mask,
17886                                    1,
17887                                    ((5 - now_it.block_length) - 1) );
17888   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
17889
17890 #undef CLEAR_BIT
17891 #undef SET_BIT_VALUE
17892 }
17893
17894 /* The IT blocks handling machinery is accessed through the these functions:
17895      it_fsm_pre_encode ()               from md_assemble ()
17896      set_it_insn_type ()                optional, from the tencode functions
17897      set_it_insn_type_last ()           ditto
17898      in_it_block ()                     ditto
17899      it_fsm_post_encode ()              from md_assemble ()
17900      force_automatic_it_block_close ()  from label habdling functions
17901
17902    Rationale:
17903      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
17904         initializing the IT insn type with a generic initial value depending
17905         on the inst.condition.
17906      2) During the tencode function, two things may happen:
17907         a) The tencode function overrides the IT insn type by
17908            calling either set_it_insn_type (type) or set_it_insn_type_last ().
17909         b) The tencode function queries the IT block state by
17910            calling in_it_block () (i.e. to determine narrow/not narrow mode).
17911
17912         Both set_it_insn_type and in_it_block run the internal FSM state
17913         handling function (handle_it_state), because: a) setting the IT insn
17914         type may incur in an invalid state (exiting the function),
17915         and b) querying the state requires the FSM to be updated.
17916         Specifically we want to avoid creating an IT block for conditional
17917         branches, so it_fsm_pre_encode is actually a guess and we can't
17918         determine whether an IT block is required until the tencode () routine
17919         has decided what type of instruction this actually it.
17920         Because of this, if set_it_insn_type and in_it_block have to be used,
17921         set_it_insn_type has to be called first.
17922
17923         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
17924         determines the insn IT type depending on the inst.cond code.
17925         When a tencode () routine encodes an instruction that can be
17926         either outside an IT block, or, in the case of being inside, has to be
17927         the last one, set_it_insn_type_last () will determine the proper
17928         IT instruction type based on the inst.cond code. Otherwise,
17929         set_it_insn_type can be called for overriding that logic or
17930         for covering other cases.
17931
17932         Calling handle_it_state () may not transition the IT block state to
17933         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
17934         still queried. Instead, if the FSM determines that the state should
17935         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
17936         after the tencode () function: that's what it_fsm_post_encode () does.
17937
17938         Since in_it_block () calls the state handling function to get an
17939         updated state, an error may occur (due to invalid insns combination).
17940         In that case, inst.error is set.
17941         Therefore, inst.error has to be checked after the execution of
17942         the tencode () routine.
17943
17944      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
17945         any pending state change (if any) that didn't take place in
17946         handle_it_state () as explained above.  */
17947
17948 static void
17949 it_fsm_pre_encode (void)
17950 {
17951   if (inst.cond != COND_ALWAYS)
17952     inst.it_insn_type = INSIDE_IT_INSN;
17953   else
17954     inst.it_insn_type = OUTSIDE_IT_INSN;
17955
17956   now_it.state_handled = 0;
17957 }
17958
17959 /* IT state FSM handling function.  */
17960
17961 static int
17962 handle_it_state (void)
17963 {
17964   now_it.state_handled = 1;
17965   now_it.insn_cond = FALSE;
17966
17967   switch (now_it.state)
17968     {
17969     case OUTSIDE_IT_BLOCK:
17970       switch (inst.it_insn_type)
17971         {
17972         case OUTSIDE_IT_INSN:
17973           break;
17974
17975         case INSIDE_IT_INSN:
17976         case INSIDE_IT_LAST_INSN:
17977           if (thumb_mode == 0)
17978             {
17979               if (unified_syntax
17980                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
17981                 as_tsktsk (_("Warning: conditional outside an IT block"\
17982                              " for Thumb."));
17983             }
17984           else
17985             {
17986               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
17987                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
17988                 {
17989                   /* Automatically generate the IT instruction.  */
17990                   new_automatic_it_block (inst.cond);
17991                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
17992                     close_automatic_it_block ();
17993                 }
17994               else
17995                 {
17996                   inst.error = BAD_OUT_IT;
17997                   return FAIL;
17998                 }
17999             }
18000           break;
18001
18002         case IF_INSIDE_IT_LAST_INSN:
18003         case NEUTRAL_IT_INSN:
18004           break;
18005
18006         case IT_INSN:
18007           now_it.state = MANUAL_IT_BLOCK;
18008           now_it.block_length = 0;
18009           break;
18010         }
18011       break;
18012
18013     case AUTOMATIC_IT_BLOCK:
18014       /* Three things may happen now:
18015          a) We should increment current it block size;
18016          b) We should close current it block (closing insn or 4 insns);
18017          c) We should close current it block and start a new one (due
18018          to incompatible conditions or
18019          4 insns-length block reached).  */
18020
18021       switch (inst.it_insn_type)
18022         {
18023         case OUTSIDE_IT_INSN:
18024           /* The closure of the block shall happen immediatelly,
18025              so any in_it_block () call reports the block as closed.  */
18026           force_automatic_it_block_close ();
18027           break;
18028
18029         case INSIDE_IT_INSN:
18030         case INSIDE_IT_LAST_INSN:
18031         case IF_INSIDE_IT_LAST_INSN:
18032           now_it.block_length++;
18033
18034           if (now_it.block_length > 4
18035               || !now_it_compatible (inst.cond))
18036             {
18037               force_automatic_it_block_close ();
18038               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
18039                 new_automatic_it_block (inst.cond);
18040             }
18041           else
18042             {
18043               now_it.insn_cond = TRUE;
18044               now_it_add_mask (inst.cond);
18045             }
18046
18047           if (now_it.state == AUTOMATIC_IT_BLOCK
18048               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
18049                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
18050             close_automatic_it_block ();
18051           break;
18052
18053         case NEUTRAL_IT_INSN:
18054           now_it.block_length++;
18055           now_it.insn_cond = TRUE;
18056
18057           if (now_it.block_length > 4)
18058             force_automatic_it_block_close ();
18059           else
18060             now_it_add_mask (now_it.cc & 1);
18061           break;
18062
18063         case IT_INSN:
18064           close_automatic_it_block ();
18065           now_it.state = MANUAL_IT_BLOCK;
18066           break;
18067         }
18068       break;
18069
18070     case MANUAL_IT_BLOCK:
18071       {
18072         /* Check conditional suffixes.  */
18073         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
18074         int is_last;
18075         now_it.mask <<= 1;
18076         now_it.mask &= 0x1f;
18077         is_last = (now_it.mask == 0x10);
18078         now_it.insn_cond = TRUE;
18079
18080         switch (inst.it_insn_type)
18081           {
18082           case OUTSIDE_IT_INSN:
18083             inst.error = BAD_NOT_IT;
18084             return FAIL;
18085
18086           case INSIDE_IT_INSN:
18087             if (cond != inst.cond)
18088               {
18089                 inst.error = BAD_IT_COND;
18090                 return FAIL;
18091               }
18092             break;
18093
18094           case INSIDE_IT_LAST_INSN:
18095           case IF_INSIDE_IT_LAST_INSN:
18096             if (cond != inst.cond)
18097               {
18098                 inst.error = BAD_IT_COND;
18099                 return FAIL;
18100               }
18101             if (!is_last)
18102               {
18103                 inst.error = BAD_BRANCH;
18104                 return FAIL;
18105               }
18106             break;
18107
18108           case NEUTRAL_IT_INSN:
18109             /* The BKPT instruction is unconditional even in an IT block.  */
18110             break;
18111
18112           case IT_INSN:
18113             inst.error = BAD_IT_IT;
18114             return FAIL;
18115           }
18116       }
18117       break;
18118     }
18119
18120   return SUCCESS;
18121 }
18122
18123 struct depr_insn_mask
18124 {
18125   unsigned long pattern;
18126   unsigned long mask;
18127   const char* description;
18128 };
18129
18130 /* List of 16-bit instruction patterns deprecated in an IT block in
18131    ARMv8.  */
18132 static const struct depr_insn_mask depr_it_insns[] = {
18133   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
18134   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
18135   { 0xa000, 0xb800, N_("ADR") },
18136   { 0x4800, 0xf800, N_("Literal loads") },
18137   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
18138   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
18139   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
18140      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
18141   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
18142   { 0, 0, NULL }
18143 };
18144
18145 static void
18146 it_fsm_post_encode (void)
18147 {
18148   int is_last;
18149
18150   if (!now_it.state_handled)
18151     handle_it_state ();
18152
18153   if (now_it.insn_cond
18154       && !now_it.warn_deprecated
18155       && warn_on_deprecated
18156       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
18157     {
18158       if (inst.instruction >= 0x10000)
18159         {
18160           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
18161                      "deprecated in ARMv8"));
18162           now_it.warn_deprecated = TRUE;
18163         }
18164       else
18165         {
18166           const struct depr_insn_mask *p = depr_it_insns;
18167
18168           while (p->mask != 0)
18169             {
18170               if ((inst.instruction & p->mask) == p->pattern)
18171                 {
18172                   as_tsktsk (_("IT blocks containing 16-bit Thumb instructions "
18173                              "of the following class are deprecated in ARMv8: "
18174                              "%s"), p->description);
18175                   now_it.warn_deprecated = TRUE;
18176                   break;
18177                 }
18178
18179               ++p;
18180             }
18181         }
18182
18183       if (now_it.block_length > 1)
18184         {
18185           as_tsktsk (_("IT blocks containing more than one conditional "
18186                      "instruction are deprecated in ARMv8"));
18187           now_it.warn_deprecated = TRUE;
18188         }
18189     }
18190
18191   is_last = (now_it.mask == 0x10);
18192   if (is_last)
18193     {
18194       now_it.state = OUTSIDE_IT_BLOCK;
18195       now_it.mask = 0;
18196     }
18197 }
18198
18199 static void
18200 force_automatic_it_block_close (void)
18201 {
18202   if (now_it.state == AUTOMATIC_IT_BLOCK)
18203     {
18204       close_automatic_it_block ();
18205       now_it.state = OUTSIDE_IT_BLOCK;
18206       now_it.mask = 0;
18207     }
18208 }
18209
18210 static int
18211 in_it_block (void)
18212 {
18213   if (!now_it.state_handled)
18214     handle_it_state ();
18215
18216   return now_it.state != OUTSIDE_IT_BLOCK;
18217 }
18218
18219 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
18220    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
18221    here, hence the "known" in the function name.  */
18222
18223 static bfd_boolean
18224 known_t32_only_insn (const struct asm_opcode *opcode)
18225 {
18226   /* Original Thumb-1 wide instruction.  */
18227   if (opcode->tencode == do_t_blx
18228       || opcode->tencode == do_t_branch23
18229       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
18230       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
18231     return TRUE;
18232
18233   /* Wide-only instruction added to ARMv8-M Baseline.  */
18234   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
18235       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
18236       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
18237       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
18238     return TRUE;
18239
18240   return FALSE;
18241 }
18242
18243 /* Whether wide instruction variant can be used if available for a valid OPCODE
18244    in ARCH.  */
18245
18246 static bfd_boolean
18247 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
18248 {
18249   if (known_t32_only_insn (opcode))
18250     return TRUE;
18251
18252   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
18253      of variant T3 of B.W is checked in do_t_branch.  */
18254   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18255       && opcode->tencode == do_t_branch)
18256     return TRUE;
18257
18258   /* Wide instruction variants of all instructions with narrow *and* wide
18259      variants become available with ARMv6t2.  Other opcodes are either
18260      narrow-only or wide-only and are thus available if OPCODE is valid.  */
18261   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
18262     return TRUE;
18263
18264   /* OPCODE with narrow only instruction variant or wide variant not
18265      available.  */
18266   return FALSE;
18267 }
18268
18269 void
18270 md_assemble (char *str)
18271 {
18272   char *p = str;
18273   const struct asm_opcode * opcode;
18274
18275   /* Align the previous label if needed.  */
18276   if (last_label_seen != NULL)
18277     {
18278       symbol_set_frag (last_label_seen, frag_now);
18279       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
18280       S_SET_SEGMENT (last_label_seen, now_seg);
18281     }
18282
18283   memset (&inst, '\0', sizeof (inst));
18284   inst.reloc.type = BFD_RELOC_UNUSED;
18285
18286   opcode = opcode_lookup (&p);
18287   if (!opcode)
18288     {
18289       /* It wasn't an instruction, but it might be a register alias of
18290          the form alias .req reg, or a Neon .dn/.qn directive.  */
18291       if (! create_register_alias (str, p)
18292           && ! create_neon_reg_alias (str, p))
18293         as_bad (_("bad instruction `%s'"), str);
18294
18295       return;
18296     }
18297
18298   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
18299     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
18300
18301   /* The value which unconditional instructions should have in place of the
18302      condition field.  */
18303   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
18304
18305   if (thumb_mode)
18306     {
18307       arm_feature_set variant;
18308
18309       variant = cpu_variant;
18310       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
18311       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
18312         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
18313       /* Check that this instruction is supported for this CPU.  */
18314       if (!opcode->tvariant
18315           || (thumb_mode == 1
18316               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
18317         {
18318           as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
18319           return;
18320         }
18321       if (inst.cond != COND_ALWAYS && !unified_syntax
18322           && opcode->tencode != do_t_branch)
18323         {
18324           as_bad (_("Thumb does not support conditional execution"));
18325           return;
18326         }
18327
18328       /* Two things are addressed here:
18329          1) Implicit require narrow instructions on Thumb-1.
18330             This avoids relaxation accidentally introducing Thumb-2
18331             instructions.
18332          2) Reject wide instructions in non Thumb-2 cores.
18333
18334          Only instructions with narrow and wide variants need to be handled
18335          but selecting all non wide-only instructions is easier.  */
18336       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
18337           && !t32_insn_ok (variant, opcode))
18338         {
18339           if (inst.size_req == 0)
18340             inst.size_req = 2;
18341           else if (inst.size_req == 4)
18342             {
18343               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
18344                 as_bad (_("selected processor does not support 32bit wide "
18345                           "variant of instruction `%s'"), str);
18346               else
18347                 as_bad (_("selected processor does not support `%s' in "
18348                           "Thumb-2 mode"), str);
18349               return;
18350             }
18351         }
18352
18353       inst.instruction = opcode->tvalue;
18354
18355       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
18356         {
18357           /* Prepare the it_insn_type for those encodings that don't set
18358              it.  */
18359           it_fsm_pre_encode ();
18360
18361           opcode->tencode ();
18362
18363           it_fsm_post_encode ();
18364         }
18365
18366       if (!(inst.error || inst.relax))
18367         {
18368           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
18369           inst.size = (inst.instruction > 0xffff ? 4 : 2);
18370           if (inst.size_req && inst.size_req != inst.size)
18371             {
18372               as_bad (_("cannot honor width suffix -- `%s'"), str);
18373               return;
18374             }
18375         }
18376
18377       /* Something has gone badly wrong if we try to relax a fixed size
18378          instruction.  */
18379       gas_assert (inst.size_req == 0 || !inst.relax);
18380
18381       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18382                               *opcode->tvariant);
18383       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
18384          set those bits when Thumb-2 32-bit instructions are seen.  The impact
18385          of relaxable instructions will be considered later after we finish all
18386          relaxation.  */
18387       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
18388         variant = arm_arch_none;
18389       else
18390         variant = cpu_variant;
18391       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
18392         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18393                                 arm_ext_v6t2);
18394
18395       check_neon_suffixes;
18396
18397       if (!inst.error)
18398         {
18399           mapping_state (MAP_THUMB);
18400         }
18401     }
18402   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
18403     {
18404       bfd_boolean is_bx;
18405
18406       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
18407       is_bx = (opcode->aencode == do_bx);
18408
18409       /* Check that this instruction is supported for this CPU.  */
18410       if (!(is_bx && fix_v4bx)
18411           && !(opcode->avariant &&
18412                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
18413         {
18414           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
18415           return;
18416         }
18417       if (inst.size_req)
18418         {
18419           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
18420           return;
18421         }
18422
18423       inst.instruction = opcode->avalue;
18424       if (opcode->tag == OT_unconditionalF)
18425         inst.instruction |= 0xFU << 28;
18426       else
18427         inst.instruction |= inst.cond << 28;
18428       inst.size = INSN_SIZE;
18429       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
18430         {
18431           it_fsm_pre_encode ();
18432           opcode->aencode ();
18433           it_fsm_post_encode ();
18434         }
18435       /* Arm mode bx is marked as both v4T and v5 because it's still required
18436          on a hypothetical non-thumb v5 core.  */
18437       if (is_bx)
18438         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
18439       else
18440         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
18441                                 *opcode->avariant);
18442
18443       check_neon_suffixes;
18444
18445       if (!inst.error)
18446         {
18447           mapping_state (MAP_ARM);
18448         }
18449     }
18450   else
18451     {
18452       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
18453                 "-- `%s'"), str);
18454       return;
18455     }
18456   output_inst (str);
18457 }
18458
18459 static void
18460 check_it_blocks_finished (void)
18461 {
18462 #ifdef OBJ_ELF
18463   asection *sect;
18464
18465   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
18466     if (seg_info (sect)->tc_segment_info_data.current_it.state
18467         == MANUAL_IT_BLOCK)
18468       {
18469         as_warn (_("section '%s' finished with an open IT block."),
18470                  sect->name);
18471       }
18472 #else
18473   if (now_it.state == MANUAL_IT_BLOCK)
18474     as_warn (_("file finished with an open IT block."));
18475 #endif
18476 }
18477
18478 /* Various frobbings of labels and their addresses.  */
18479
18480 void
18481 arm_start_line_hook (void)
18482 {
18483   last_label_seen = NULL;
18484 }
18485
18486 void
18487 arm_frob_label (symbolS * sym)
18488 {
18489   last_label_seen = sym;
18490
18491   ARM_SET_THUMB (sym, thumb_mode);
18492
18493 #if defined OBJ_COFF || defined OBJ_ELF
18494   ARM_SET_INTERWORK (sym, support_interwork);
18495 #endif
18496
18497   force_automatic_it_block_close ();
18498
18499   /* Note - do not allow local symbols (.Lxxx) to be labelled
18500      as Thumb functions.  This is because these labels, whilst
18501      they exist inside Thumb code, are not the entry points for
18502      possible ARM->Thumb calls.  Also, these labels can be used
18503      as part of a computed goto or switch statement.  eg gcc
18504      can generate code that looks like this:
18505
18506                 ldr  r2, [pc, .Laaa]
18507                 lsl  r3, r3, #2
18508                 ldr  r2, [r3, r2]
18509                 mov  pc, r2
18510
18511        .Lbbb:  .word .Lxxx
18512        .Lccc:  .word .Lyyy
18513        ..etc...
18514        .Laaa:   .word Lbbb
18515
18516      The first instruction loads the address of the jump table.
18517      The second instruction converts a table index into a byte offset.
18518      The third instruction gets the jump address out of the table.
18519      The fourth instruction performs the jump.
18520
18521      If the address stored at .Laaa is that of a symbol which has the
18522      Thumb_Func bit set, then the linker will arrange for this address
18523      to have the bottom bit set, which in turn would mean that the
18524      address computation performed by the third instruction would end
18525      up with the bottom bit set.  Since the ARM is capable of unaligned
18526      word loads, the instruction would then load the incorrect address
18527      out of the jump table, and chaos would ensue.  */
18528   if (label_is_thumb_function_name
18529       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
18530       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
18531     {
18532       /* When the address of a Thumb function is taken the bottom
18533          bit of that address should be set.  This will allow
18534          interworking between Arm and Thumb functions to work
18535          correctly.  */
18536
18537       THUMB_SET_FUNC (sym, 1);
18538
18539       label_is_thumb_function_name = FALSE;
18540     }
18541
18542   dwarf2_emit_label (sym);
18543 }
18544
18545 bfd_boolean
18546 arm_data_in_code (void)
18547 {
18548   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
18549     {
18550       *input_line_pointer = '/';
18551       input_line_pointer += 5;
18552       *input_line_pointer = 0;
18553       return TRUE;
18554     }
18555
18556   return FALSE;
18557 }
18558
18559 char *
18560 arm_canonicalize_symbol_name (char * name)
18561 {
18562   int len;
18563
18564   if (thumb_mode && (len = strlen (name)) > 5
18565       && streq (name + len - 5, "/data"))
18566     *(name + len - 5) = 0;
18567
18568   return name;
18569 }
18570 \f
18571 /* Table of all register names defined by default.  The user can
18572    define additional names with .req.  Note that all register names
18573    should appear in both upper and lowercase variants.  Some registers
18574    also have mixed-case names.  */
18575
18576 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
18577 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
18578 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
18579 #define REGSET(p,t) \
18580   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
18581   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
18582   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
18583   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
18584 #define REGSETH(p,t) \
18585   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
18586   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
18587   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
18588   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
18589 #define REGSET2(p,t) \
18590   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
18591   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
18592   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
18593   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
18594 #define SPLRBANK(base,bank,t) \
18595   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
18596   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
18597   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
18598   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
18599   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
18600   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
18601
18602 static const struct reg_entry reg_names[] =
18603 {
18604   /* ARM integer registers.  */
18605   REGSET(r, RN), REGSET(R, RN),
18606
18607   /* ATPCS synonyms.  */
18608   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
18609   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
18610   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
18611
18612   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
18613   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
18614   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
18615
18616   /* Well-known aliases.  */
18617   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
18618   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
18619
18620   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
18621   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
18622
18623   /* Coprocessor numbers.  */
18624   REGSET(p, CP), REGSET(P, CP),
18625
18626   /* Coprocessor register numbers.  The "cr" variants are for backward
18627      compatibility.  */
18628   REGSET(c,  CN), REGSET(C, CN),
18629   REGSET(cr, CN), REGSET(CR, CN),
18630
18631   /* ARM banked registers.  */
18632   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
18633   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
18634   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
18635   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
18636   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
18637   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
18638   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
18639
18640   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
18641   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
18642   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
18643   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
18644   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
18645   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
18646   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
18647   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
18648
18649   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
18650   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
18651   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
18652   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
18653   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
18654   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
18655   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
18656   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
18657   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
18658
18659   /* FPA registers.  */
18660   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
18661   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
18662
18663   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
18664   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
18665
18666   /* VFP SP registers.  */
18667   REGSET(s,VFS),  REGSET(S,VFS),
18668   REGSETH(s,VFS), REGSETH(S,VFS),
18669
18670   /* VFP DP Registers.  */
18671   REGSET(d,VFD),  REGSET(D,VFD),
18672   /* Extra Neon DP registers.  */
18673   REGSETH(d,VFD), REGSETH(D,VFD),
18674
18675   /* Neon QP registers.  */
18676   REGSET2(q,NQ),  REGSET2(Q,NQ),
18677
18678   /* VFP control registers.  */
18679   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
18680   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
18681   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
18682   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
18683   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
18684   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
18685
18686   /* Maverick DSP coprocessor registers.  */
18687   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
18688   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
18689
18690   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
18691   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
18692   REGDEF(dspsc,0,DSPSC),
18693
18694   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
18695   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
18696   REGDEF(DSPSC,0,DSPSC),
18697
18698   /* iWMMXt data registers - p0, c0-15.  */
18699   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
18700
18701   /* iWMMXt control registers - p1, c0-3.  */
18702   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
18703   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
18704   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
18705   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
18706
18707   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
18708   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
18709   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
18710   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
18711   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
18712
18713   /* XScale accumulator registers.  */
18714   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
18715 };
18716 #undef REGDEF
18717 #undef REGNUM
18718 #undef REGSET
18719
18720 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
18721    within psr_required_here.  */
18722 static const struct asm_psr psrs[] =
18723 {
18724   /* Backward compatibility notation.  Note that "all" is no longer
18725      truly all possible PSR bits.  */
18726   {"all",  PSR_c | PSR_f},
18727   {"flg",  PSR_f},
18728   {"ctl",  PSR_c},
18729
18730   /* Individual flags.  */
18731   {"f",    PSR_f},
18732   {"c",    PSR_c},
18733   {"x",    PSR_x},
18734   {"s",    PSR_s},
18735
18736   /* Combinations of flags.  */
18737   {"fs",   PSR_f | PSR_s},
18738   {"fx",   PSR_f | PSR_x},
18739   {"fc",   PSR_f | PSR_c},
18740   {"sf",   PSR_s | PSR_f},
18741   {"sx",   PSR_s | PSR_x},
18742   {"sc",   PSR_s | PSR_c},
18743   {"xf",   PSR_x | PSR_f},
18744   {"xs",   PSR_x | PSR_s},
18745   {"xc",   PSR_x | PSR_c},
18746   {"cf",   PSR_c | PSR_f},
18747   {"cs",   PSR_c | PSR_s},
18748   {"cx",   PSR_c | PSR_x},
18749   {"fsx",  PSR_f | PSR_s | PSR_x},
18750   {"fsc",  PSR_f | PSR_s | PSR_c},
18751   {"fxs",  PSR_f | PSR_x | PSR_s},
18752   {"fxc",  PSR_f | PSR_x | PSR_c},
18753   {"fcs",  PSR_f | PSR_c | PSR_s},
18754   {"fcx",  PSR_f | PSR_c | PSR_x},
18755   {"sfx",  PSR_s | PSR_f | PSR_x},
18756   {"sfc",  PSR_s | PSR_f | PSR_c},
18757   {"sxf",  PSR_s | PSR_x | PSR_f},
18758   {"sxc",  PSR_s | PSR_x | PSR_c},
18759   {"scf",  PSR_s | PSR_c | PSR_f},
18760   {"scx",  PSR_s | PSR_c | PSR_x},
18761   {"xfs",  PSR_x | PSR_f | PSR_s},
18762   {"xfc",  PSR_x | PSR_f | PSR_c},
18763   {"xsf",  PSR_x | PSR_s | PSR_f},
18764   {"xsc",  PSR_x | PSR_s | PSR_c},
18765   {"xcf",  PSR_x | PSR_c | PSR_f},
18766   {"xcs",  PSR_x | PSR_c | PSR_s},
18767   {"cfs",  PSR_c | PSR_f | PSR_s},
18768   {"cfx",  PSR_c | PSR_f | PSR_x},
18769   {"csf",  PSR_c | PSR_s | PSR_f},
18770   {"csx",  PSR_c | PSR_s | PSR_x},
18771   {"cxf",  PSR_c | PSR_x | PSR_f},
18772   {"cxs",  PSR_c | PSR_x | PSR_s},
18773   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
18774   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
18775   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
18776   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
18777   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
18778   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
18779   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
18780   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
18781   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
18782   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
18783   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
18784   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
18785   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
18786   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
18787   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
18788   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
18789   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
18790   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
18791   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
18792   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
18793   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
18794   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
18795   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
18796   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
18797 };
18798
18799 /* Table of V7M psr names.  */
18800 static const struct asm_psr v7m_psrs[] =
18801 {
18802   {"apsr",         0x0 }, {"APSR",         0x0 },
18803   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
18804   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
18805   {"psr",          0x3 }, {"PSR",          0x3 },
18806   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
18807   {"ipsr",         0x5 }, {"IPSR",         0x5 },
18808   {"epsr",         0x6 }, {"EPSR",         0x6 },
18809   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
18810   {"msp",          0x8 }, {"MSP",          0x8 },
18811   {"psp",          0x9 }, {"PSP",          0x9 },
18812   {"msplim",       0xa }, {"MSPLIM",       0xa },
18813   {"psplim",       0xb }, {"PSPLIM",       0xb },
18814   {"primask",      0x10}, {"PRIMASK",      0x10},
18815   {"basepri",      0x11}, {"BASEPRI",      0x11},
18816   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
18817   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
18818   {"control",      0x14}, {"CONTROL",      0x14},
18819   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
18820   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
18821   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
18822   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
18823   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
18824   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
18825   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
18826   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
18827   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
18828 };
18829
18830 /* Table of all shift-in-operand names.  */
18831 static const struct asm_shift_name shift_names [] =
18832 {
18833   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
18834   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
18835   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
18836   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
18837   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
18838   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
18839 };
18840
18841 /* Table of all explicit relocation names.  */
18842 #ifdef OBJ_ELF
18843 static struct reloc_entry reloc_names[] =
18844 {
18845   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
18846   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
18847   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
18848   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
18849   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
18850   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
18851   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
18852   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
18853   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
18854   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
18855   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
18856   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
18857   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
18858         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
18859   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
18860         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
18861   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
18862         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
18863 };
18864 #endif
18865
18866 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
18867 static const struct asm_cond conds[] =
18868 {
18869   {"eq", 0x0},
18870   {"ne", 0x1},
18871   {"cs", 0x2}, {"hs", 0x2},
18872   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
18873   {"mi", 0x4},
18874   {"pl", 0x5},
18875   {"vs", 0x6},
18876   {"vc", 0x7},
18877   {"hi", 0x8},
18878   {"ls", 0x9},
18879   {"ge", 0xa},
18880   {"lt", 0xb},
18881   {"gt", 0xc},
18882   {"le", 0xd},
18883   {"al", 0xe}
18884 };
18885
18886 #define UL_BARRIER(L,U,CODE,FEAT) \
18887   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
18888   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
18889
18890 static struct asm_barrier_opt barrier_opt_names[] =
18891 {
18892   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
18893   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
18894   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
18895   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
18896   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
18897   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
18898   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
18899   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
18900   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
18901   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
18902   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
18903   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
18904   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
18905   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
18906   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
18907   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
18908 };
18909
18910 #undef UL_BARRIER
18911
18912 /* Table of ARM-format instructions.    */
18913
18914 /* Macros for gluing together operand strings.  N.B. In all cases
18915    other than OPS0, the trailing OP_stop comes from default
18916    zero-initialization of the unspecified elements of the array.  */
18917 #define OPS0()            { OP_stop, }
18918 #define OPS1(a)           { OP_##a, }
18919 #define OPS2(a,b)         { OP_##a,OP_##b, }
18920 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
18921 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
18922 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
18923 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
18924
18925 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
18926    This is useful when mixing operands for ARM and THUMB, i.e. using the
18927    MIX_ARM_THUMB_OPERANDS macro.
18928    In order to use these macros, prefix the number of operands with _
18929    e.g. _3.  */
18930 #define OPS_1(a)           { a, }
18931 #define OPS_2(a,b)         { a,b, }
18932 #define OPS_3(a,b,c)       { a,b,c, }
18933 #define OPS_4(a,b,c,d)     { a,b,c,d, }
18934 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
18935 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
18936
18937 /* These macros abstract out the exact format of the mnemonic table and
18938    save some repeated characters.  */
18939
18940 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
18941 #define TxCE(mnem, op, top, nops, ops, ae, te) \
18942   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
18943     THUMB_VARIANT, do_##ae, do_##te }
18944
18945 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
18946    a T_MNEM_xyz enumerator.  */
18947 #define TCE(mnem, aop, top, nops, ops, ae, te) \
18948       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
18949 #define tCE(mnem, aop, top, nops, ops, ae, te) \
18950       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18951
18952 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
18953    infix after the third character.  */
18954 #define TxC3(mnem, op, top, nops, ops, ae, te) \
18955   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
18956     THUMB_VARIANT, do_##ae, do_##te }
18957 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
18958   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
18959     THUMB_VARIANT, do_##ae, do_##te }
18960 #define TC3(mnem, aop, top, nops, ops, ae, te) \
18961       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
18962 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
18963       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
18964 #define tC3(mnem, aop, top, nops, ops, ae, te) \
18965       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18966 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
18967       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18968
18969 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
18970    field is still 0xE.  Many of the Thumb variants can be executed
18971    conditionally, so this is checked separately.  */
18972 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
18973   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18974     THUMB_VARIANT, do_##ae, do_##te }
18975
18976 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
18977    Used by mnemonics that have very minimal differences in the encoding for
18978    ARM and Thumb variants and can be handled in a common function.  */
18979 #define TUEc(mnem, op, top, nops, ops, en) \
18980   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18981     THUMB_VARIANT, do_##en, do_##en }
18982
18983 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
18984    condition code field.  */
18985 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
18986   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
18987     THUMB_VARIANT, do_##ae, do_##te }
18988
18989 /* ARM-only variants of all the above.  */
18990 #define CE(mnem,  op, nops, ops, ae)    \
18991   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18992
18993 #define C3(mnem, op, nops, ops, ae)     \
18994   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18995
18996 /* Legacy mnemonics that always have conditional infix after the third
18997    character.  */
18998 #define CL(mnem, op, nops, ops, ae)     \
18999   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19000     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19001
19002 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
19003 #define cCE(mnem,  op, nops, ops, ae)   \
19004   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19005
19006 /* Legacy coprocessor instructions where conditional infix and conditional
19007    suffix are ambiguous.  For consistency this includes all FPA instructions,
19008    not just the potentially ambiguous ones.  */
19009 #define cCL(mnem, op, nops, ops, ae)    \
19010   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19011     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19012
19013 /* Coprocessor, takes either a suffix or a position-3 infix
19014    (for an FPA corner case). */
19015 #define C3E(mnem, op, nops, ops, ae) \
19016   { mnem, OPS##nops ops, OT_csuf_or_in3, \
19017     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19018
19019 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
19020   { m1 #m2 m3, OPS##nops ops, \
19021     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
19022     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19023
19024 #define CM(m1, m2, op, nops, ops, ae)   \
19025   xCM_ (m1,   , m2, op, nops, ops, ae), \
19026   xCM_ (m1, eq, m2, op, nops, ops, ae), \
19027   xCM_ (m1, ne, m2, op, nops, ops, ae), \
19028   xCM_ (m1, cs, m2, op, nops, ops, ae), \
19029   xCM_ (m1, hs, m2, op, nops, ops, ae), \
19030   xCM_ (m1, cc, m2, op, nops, ops, ae), \
19031   xCM_ (m1, ul, m2, op, nops, ops, ae), \
19032   xCM_ (m1, lo, m2, op, nops, ops, ae), \
19033   xCM_ (m1, mi, m2, op, nops, ops, ae), \
19034   xCM_ (m1, pl, m2, op, nops, ops, ae), \
19035   xCM_ (m1, vs, m2, op, nops, ops, ae), \
19036   xCM_ (m1, vc, m2, op, nops, ops, ae), \
19037   xCM_ (m1, hi, m2, op, nops, ops, ae), \
19038   xCM_ (m1, ls, m2, op, nops, ops, ae), \
19039   xCM_ (m1, ge, m2, op, nops, ops, ae), \
19040   xCM_ (m1, lt, m2, op, nops, ops, ae), \
19041   xCM_ (m1, gt, m2, op, nops, ops, ae), \
19042   xCM_ (m1, le, m2, op, nops, ops, ae), \
19043   xCM_ (m1, al, m2, op, nops, ops, ae)
19044
19045 #define UE(mnem, op, nops, ops, ae)     \
19046   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19047
19048 #define UF(mnem, op, nops, ops, ae)     \
19049   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19050
19051 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
19052    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
19053    use the same encoding function for each.  */
19054 #define NUF(mnem, op, nops, ops, enc)                                   \
19055   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
19056     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19057
19058 /* Neon data processing, version which indirects through neon_enc_tab for
19059    the various overloaded versions of opcodes.  */
19060 #define nUF(mnem, op, nops, ops, enc)                                   \
19061   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
19062     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19063
19064 /* Neon insn with conditional suffix for the ARM version, non-overloaded
19065    version.  */
19066 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
19067   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
19068     THUMB_VARIANT, do_##enc, do_##enc }
19069
19070 #define NCE(mnem, op, nops, ops, enc)                                   \
19071    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19072
19073 #define NCEF(mnem, op, nops, ops, enc)                                  \
19074     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19075
19076 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
19077 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
19078   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
19079     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19080
19081 #define nCE(mnem, op, nops, ops, enc)                                   \
19082    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19083
19084 #define nCEF(mnem, op, nops, ops, enc)                                  \
19085     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19086
19087 #define do_0 0
19088
19089 static const struct asm_opcode insns[] =
19090 {
19091 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
19092 #define THUMB_VARIANT  & arm_ext_v4t
19093  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
19094  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
19095  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
19096  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
19097  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
19098  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
19099  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
19100  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
19101  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
19102  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
19103  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
19104  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
19105  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
19106  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
19107  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
19108  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
19109
19110  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
19111     for setting PSR flag bits.  They are obsolete in V6 and do not
19112     have Thumb equivalents. */
19113  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19114  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19115   CL("tstp",    110f000,           2, (RR, SH),      cmp),
19116  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19117  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19118   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
19119  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19120  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19121   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
19122
19123  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
19124  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
19125  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
19126  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
19127
19128  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
19129  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19130  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
19131                                                                 OP_RRnpc),
19132                                         OP_ADDRGLDR),ldst, t_ldst),
19133  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19134
19135  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19136  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19137  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19138  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19139  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19140  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19141
19142  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
19143  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
19144  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
19145  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
19146
19147   /* Pseudo ops.  */
19148  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
19149   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
19150  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
19151  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
19152
19153   /* Thumb-compatibility pseudo ops.  */
19154  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
19155  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
19156  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
19157  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
19158  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
19159  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
19160  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
19161  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
19162  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
19163  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
19164  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
19165  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
19166
19167  /* These may simplify to neg.  */
19168  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
19169  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
19170
19171 #undef  THUMB_VARIANT
19172 #define THUMB_VARIANT  & arm_ext_v6
19173
19174  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
19175
19176  /* V1 instructions with no Thumb analogue prior to V6T2.  */
19177 #undef  THUMB_VARIANT
19178 #define THUMB_VARIANT  & arm_ext_v6t2
19179
19180  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19181  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19182   CL("teqp",    130f000,           2, (RR, SH),      cmp),
19183
19184  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19185  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19186  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
19187  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19188
19189  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19190  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19191
19192  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19193  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19194
19195  /* V1 instructions with no Thumb analogue at all.  */
19196   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
19197   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
19198
19199   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
19200   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
19201   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
19202   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
19203   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
19204   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
19205   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
19206   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
19207
19208 #undef  ARM_VARIANT
19209 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
19210 #undef  THUMB_VARIANT
19211 #define THUMB_VARIANT  & arm_ext_v4t
19212
19213  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
19214  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
19215
19216 #undef  THUMB_VARIANT
19217 #define THUMB_VARIANT  & arm_ext_v6t2
19218
19219  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19220   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
19221
19222   /* Generic coprocessor instructions.  */
19223  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19224  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19225  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19226  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19227  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19228  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19229  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
19230
19231 #undef  ARM_VARIANT
19232 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
19233
19234   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19235   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19236
19237 #undef  ARM_VARIANT
19238 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
19239 #undef  THUMB_VARIANT
19240 #define THUMB_VARIANT  & arm_ext_msr
19241
19242  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
19243  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
19244
19245 #undef  ARM_VARIANT
19246 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
19247 #undef  THUMB_VARIANT
19248 #define THUMB_VARIANT  & arm_ext_v6t2
19249
19250  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19251   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19252  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19253   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19254  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19255   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19256  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19257   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19258
19259 #undef  ARM_VARIANT
19260 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
19261 #undef  THUMB_VARIANT
19262 #define THUMB_VARIANT  & arm_ext_v4t
19263
19264  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19265  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19266  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19267  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19268  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19269  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19270
19271 #undef  ARM_VARIANT
19272 #define ARM_VARIANT  & arm_ext_v4t_5
19273
19274   /* ARM Architecture 4T.  */
19275   /* Note: bx (and blx) are required on V5, even if the processor does
19276      not support Thumb.  */
19277  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
19278
19279 #undef  ARM_VARIANT
19280 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
19281 #undef  THUMB_VARIANT
19282 #define THUMB_VARIANT  & arm_ext_v5t
19283
19284   /* Note: blx has 2 variants; the .value coded here is for
19285      BLX(2).  Only this variant has conditional execution.  */
19286  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
19287  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
19288
19289 #undef  THUMB_VARIANT
19290 #define THUMB_VARIANT  & arm_ext_v6t2
19291
19292  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
19293  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19294  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19295  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19296  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19297  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19298  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19299  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19300
19301 #undef  ARM_VARIANT
19302 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
19303 #undef  THUMB_VARIANT
19304 #define THUMB_VARIANT  & arm_ext_v5exp
19305
19306  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19307  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19308  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19309  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19310
19311  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19312  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19313
19314  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19315  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19316  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19317  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19318
19319  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19320  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19321  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19322  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19323
19324  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19325  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19326
19327  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19328  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19329  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19330  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19331
19332 #undef  ARM_VARIANT
19333 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
19334 #undef  THUMB_VARIANT
19335 #define THUMB_VARIANT  & arm_ext_v6t2
19336
19337  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
19338  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
19339      ldrd, t_ldstd),
19340  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
19341                                        ADDRGLDRS), ldrd, t_ldstd),
19342
19343  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19344  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19345
19346 #undef  ARM_VARIANT
19347 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
19348
19349  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
19350
19351 #undef  ARM_VARIANT
19352 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
19353 #undef  THUMB_VARIANT
19354 #define THUMB_VARIANT  & arm_ext_v6
19355
19356  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19357  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19358  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19359  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19360  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19361  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19362  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19363  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19364  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19365  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
19366
19367 #undef  THUMB_VARIANT
19368 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19369
19370  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
19371  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19372                                       strex,  t_strex),
19373 #undef  THUMB_VARIANT
19374 #define THUMB_VARIANT  & arm_ext_v6t2
19375
19376  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19377  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19378
19379  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
19380  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
19381
19382 /*  ARM V6 not included in V7M.  */
19383 #undef  THUMB_VARIANT
19384 #define THUMB_VARIANT  & arm_ext_v6_notm
19385  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19386  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19387   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
19388   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
19389  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19390  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19391   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
19392  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19393   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
19394  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19395  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19396  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19397   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
19398   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
19399   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
19400   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
19401  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19402  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19403  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
19404
19405 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
19406 #undef  THUMB_VARIANT
19407 #define THUMB_VARIANT  & arm_ext_v6_dsp
19408  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
19409  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
19410  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19411  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19412  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19413  /* Old name for QASX.  */
19414  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19415  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19416  /* Old name for QSAX.  */
19417  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19418  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19419  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19420  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19421  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19422  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19423  /* Old name for SASX.  */
19424  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19425  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19426  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19427  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19428  /* Old name for SHASX.  */
19429  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19430  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19431  /* Old name for SHSAX.  */
19432  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19433  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19434  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19435  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19436  /* Old name for SSAX.  */
19437  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19438  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19439  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19440  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19441  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19442  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19443  /* Old name for UASX.  */
19444  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19445  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19446  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19447  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19448  /* Old name for UHASX.  */
19449  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19450  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19451  /* Old name for UHSAX.  */
19452  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19453  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19454  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19455  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19456  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19457  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19458  /* Old name for UQASX.  */
19459  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19460  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19461  /* Old name for UQSAX.  */
19462  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19463  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19464  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19465  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19466  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19467  /* Old name for USAX.  */
19468  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19469  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19470  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19471  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19472  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19473  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19474  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19475  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19476  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19477  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19478  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19479  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19480  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19481  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19482  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19483  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19484  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19485  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19486  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19487  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19488  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19489  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19490  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19491  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19492  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19493  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19494  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19495  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19496  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19497  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
19498  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
19499  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
19500  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
19501  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
19502
19503 #undef  ARM_VARIANT
19504 #define ARM_VARIANT   & arm_ext_v6k
19505 #undef  THUMB_VARIANT
19506 #define THUMB_VARIANT & arm_ext_v6k
19507
19508  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
19509  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
19510  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
19511  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
19512
19513 #undef  THUMB_VARIANT
19514 #define THUMB_VARIANT  & arm_ext_v6_notm
19515  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
19516                                       ldrexd, t_ldrexd),
19517  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
19518                                        RRnpcb), strexd, t_strexd),
19519
19520 #undef  THUMB_VARIANT
19521 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19522  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
19523      rd_rn,  rd_rn),
19524  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
19525      rd_rn,  rd_rn),
19526  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19527      strex, t_strexbh),
19528  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19529      strex, t_strexbh),
19530  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
19531
19532 #undef  ARM_VARIANT
19533 #define ARM_VARIANT    & arm_ext_sec
19534 #undef  THUMB_VARIANT
19535 #define THUMB_VARIANT  & arm_ext_sec
19536
19537  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
19538
19539 #undef  ARM_VARIANT
19540 #define ARM_VARIANT    & arm_ext_virt
19541 #undef  THUMB_VARIANT
19542 #define THUMB_VARIANT    & arm_ext_virt
19543
19544  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
19545  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
19546
19547 #undef  ARM_VARIANT
19548 #define ARM_VARIANT    & arm_ext_pan
19549 #undef  THUMB_VARIANT
19550 #define THUMB_VARIANT  & arm_ext_pan
19551
19552  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
19553
19554 #undef  ARM_VARIANT
19555 #define ARM_VARIANT    & arm_ext_v6t2
19556 #undef  THUMB_VARIANT
19557 #define THUMB_VARIANT  & arm_ext_v6t2
19558
19559  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
19560  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
19561  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19562  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19563
19564  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19565  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
19566
19567  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19568  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19569  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19570  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19571
19572 #undef  THUMB_VARIANT
19573 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19574  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
19575  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
19576
19577  /* Thumb-only instructions.  */
19578 #undef  ARM_VARIANT
19579 #define ARM_VARIANT NULL
19580   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
19581   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
19582
19583  /* ARM does not really have an IT instruction, so always allow it.
19584     The opcode is copied from Thumb in order to allow warnings in
19585     -mimplicit-it=[never | arm] modes.  */
19586 #undef  ARM_VARIANT
19587 #define ARM_VARIANT  & arm_ext_v1
19588 #undef  THUMB_VARIANT
19589 #define THUMB_VARIANT  & arm_ext_v6t2
19590
19591  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
19592  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
19593  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
19594  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
19595  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
19596  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
19597  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
19598  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
19599  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
19600  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
19601  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
19602  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
19603  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
19604  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
19605  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
19606  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
19607  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
19608  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
19609
19610  /* Thumb2 only instructions.  */
19611 #undef  ARM_VARIANT
19612 #define ARM_VARIANT  NULL
19613
19614  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19615  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19616  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
19617  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
19618  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
19619  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
19620
19621  /* Hardware division instructions.  */
19622 #undef  ARM_VARIANT
19623 #define ARM_VARIANT    & arm_ext_adiv
19624 #undef  THUMB_VARIANT
19625 #define THUMB_VARIANT  & arm_ext_div
19626
19627  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
19628  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
19629
19630  /* ARM V6M/V7 instructions.  */
19631 #undef  ARM_VARIANT
19632 #define ARM_VARIANT    & arm_ext_barrier
19633 #undef  THUMB_VARIANT
19634 #define THUMB_VARIANT  & arm_ext_barrier
19635
19636  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
19637  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
19638  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
19639
19640  /* ARM V7 instructions.  */
19641 #undef  ARM_VARIANT
19642 #define ARM_VARIANT    & arm_ext_v7
19643 #undef  THUMB_VARIANT
19644 #define THUMB_VARIANT  & arm_ext_v7
19645
19646  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
19647  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
19648
19649 #undef  ARM_VARIANT
19650 #define ARM_VARIANT    & arm_ext_mp
19651 #undef  THUMB_VARIANT
19652 #define THUMB_VARIANT  & arm_ext_mp
19653
19654  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
19655
19656  /* AArchv8 instructions.  */
19657 #undef  ARM_VARIANT
19658 #define ARM_VARIANT   & arm_ext_v8
19659
19660 /* Instructions shared between armv8-a and armv8-m.  */
19661 #undef  THUMB_VARIANT
19662 #define THUMB_VARIANT & arm_ext_atomics
19663
19664  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19665  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19666  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19667  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19668  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19669  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19670  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19671  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
19672  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19673  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
19674                                                         stlex,  t_stlex),
19675  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
19676                                                         stlex, t_stlex),
19677  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
19678                                                         stlex, t_stlex),
19679 #undef  THUMB_VARIANT
19680 #define THUMB_VARIANT & arm_ext_v8
19681
19682  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
19683  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
19684  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
19685                                                         ldrexd, t_ldrexd),
19686  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
19687                                                         strexd, t_strexd),
19688  /* ARMv8 T32 only.  */
19689 #undef  ARM_VARIANT
19690 #define ARM_VARIANT  NULL
19691  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
19692  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
19693  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
19694
19695   /* FP for ARMv8.  */
19696 #undef  ARM_VARIANT
19697 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
19698 #undef  THUMB_VARIANT
19699 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
19700
19701   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
19702   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
19703   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
19704   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
19705   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19706   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19707   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
19708   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
19709   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
19710   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
19711   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
19712   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
19713   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
19714   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
19715   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
19716   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
19717   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
19718
19719   /* Crypto v1 extensions.  */
19720 #undef  ARM_VARIANT
19721 #define ARM_VARIANT & fpu_crypto_ext_armv8
19722 #undef  THUMB_VARIANT
19723 #define THUMB_VARIANT & fpu_crypto_ext_armv8
19724
19725   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
19726   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
19727   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
19728   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
19729   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
19730   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
19731   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
19732   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
19733   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
19734   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
19735   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
19736   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
19737   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
19738   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
19739
19740 #undef  ARM_VARIANT
19741 #define ARM_VARIANT   & crc_ext_armv8
19742 #undef  THUMB_VARIANT
19743 #define THUMB_VARIANT & crc_ext_armv8
19744   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
19745   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
19746   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
19747   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
19748   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
19749   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
19750
19751  /* ARMv8.2 RAS extension.  */
19752 #undef  ARM_VARIANT
19753 #define ARM_VARIANT   & arm_ext_ras
19754 #undef  THUMB_VARIANT
19755 #define THUMB_VARIANT & arm_ext_ras
19756  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
19757
19758 #undef  ARM_VARIANT
19759 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
19760 #undef  THUMB_VARIANT
19761 #define THUMB_VARIANT NULL
19762
19763  cCE("wfs",     e200110, 1, (RR),            rd),
19764  cCE("rfs",     e300110, 1, (RR),            rd),
19765  cCE("wfc",     e400110, 1, (RR),            rd),
19766  cCE("rfc",     e500110, 1, (RR),            rd),
19767
19768  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19769  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19770  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19771  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19772
19773  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19774  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19775  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19776  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19777
19778  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
19779  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
19780  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
19781  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
19782  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
19783  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
19784  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
19785  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
19786  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
19787  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
19788  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
19789  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
19790
19791  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
19792  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
19793  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
19794  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
19795  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
19796  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
19797  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
19798  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
19799  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
19800  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
19801  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
19802  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
19803
19804  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
19805  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
19806  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
19807  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
19808  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
19809  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
19810  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
19811  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
19812  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
19813  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
19814  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
19815  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
19816
19817  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
19818  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
19819  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
19820  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
19821  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
19822  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
19823  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
19824  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
19825  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
19826  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
19827  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
19828  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
19829
19830  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
19831  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
19832  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
19833  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
19834  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
19835  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
19836  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
19837  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
19838  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
19839  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
19840  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
19841  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
19842
19843  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
19844  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
19845  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
19846  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
19847  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
19848  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
19849  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
19850  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
19851  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
19852  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
19853  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
19854  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
19855
19856  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
19857  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
19858  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
19859  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
19860  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
19861  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
19862  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
19863  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
19864  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
19865  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
19866  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
19867  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
19868
19869  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
19870  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
19871  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
19872  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
19873  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
19874  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
19875  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
19876  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
19877  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
19878  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
19879  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
19880  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
19881
19882  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
19883  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
19884  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
19885  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
19886  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
19887  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
19888  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
19889  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
19890  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
19891  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
19892  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
19893  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
19894
19895  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
19896  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
19897  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
19898  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
19899  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
19900  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
19901  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
19902  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
19903  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
19904  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
19905  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
19906  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
19907
19908  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
19909  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
19910  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
19911  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
19912  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
19913  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
19914  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
19915  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
19916  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
19917  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
19918  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
19919  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
19920
19921  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
19922  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
19923  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
19924  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
19925  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
19926  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
19927  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
19928  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
19929  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
19930  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
19931  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
19932  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
19933
19934  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
19935  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
19936  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
19937  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
19938  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
19939  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
19940  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
19941  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
19942  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
19943  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
19944  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
19945  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
19946
19947  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
19948  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
19949  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
19950  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
19951  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
19952  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
19953  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
19954  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
19955  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
19956  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
19957  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
19958  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
19959
19960  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
19961  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
19962  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
19963  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
19964  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
19965  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
19966  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
19967  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
19968  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
19969  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
19970  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
19971  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
19972
19973  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
19974  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
19975  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
19976  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
19977  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
19978  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
19979  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
19980  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
19981  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
19982  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
19983  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
19984  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
19985
19986  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
19987  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
19988  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
19989  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
19990  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
19991  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19992  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19993  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19994  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
19995  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
19996  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
19997  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
19998
19999  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
20000  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
20001  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
20002  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
20003  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
20004  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20005  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20006  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20007  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
20008  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
20009  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
20010  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
20011
20012  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
20013  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
20014  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
20015  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
20016  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
20017  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20018  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20019  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20020  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
20021  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
20022  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
20023  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
20024
20025  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
20026  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
20027  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
20028  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
20029  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
20030  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20031  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20032  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20033  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
20034  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
20035  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
20036  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
20037
20038  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
20039  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
20040  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
20041  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
20042  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
20043  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20044  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20045  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20046  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
20047  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
20048  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
20049  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
20050
20051  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
20052  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
20053  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
20054  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
20055  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
20056  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20057  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20058  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20059  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
20060  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
20061  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
20062  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
20063
20064  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
20065  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
20066  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
20067  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
20068  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
20069  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20070  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20071  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20072  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
20073  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
20074  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
20075  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
20076
20077  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
20078  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
20079  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
20080  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
20081  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
20082  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20083  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20084  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20085  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
20086  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
20087  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
20088  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
20089
20090  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
20091  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
20092  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
20093  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
20094  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
20095  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20096  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20097  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20098  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
20099  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
20100  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
20101  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
20102
20103  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
20104  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
20105  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
20106  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
20107  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
20108  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20109  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20110  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20111  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
20112  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
20113  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
20114  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
20115
20116  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20117  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20118  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20119  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20120  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20121  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20122  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20123  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20124  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20125  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20126  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20127  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20128
20129  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20130  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20131  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20132  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20133  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20134  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20135  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20136  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20137  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20138  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20139  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20140  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20141
20142  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20143  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20144  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20145  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20146  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20147  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20148  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20149  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20150  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20151  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20152  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20153  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20154
20155  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
20156  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
20157  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
20158  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
20159
20160  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
20161  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
20162  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
20163  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
20164  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
20165  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
20166  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
20167  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
20168  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
20169  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
20170  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
20171  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
20172
20173   /* The implementation of the FIX instruction is broken on some
20174      assemblers, in that it accepts a precision specifier as well as a
20175      rounding specifier, despite the fact that this is meaningless.
20176      To be more compatible, we accept it as well, though of course it
20177      does not set any bits.  */
20178  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
20179  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
20180  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
20181  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
20182  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
20183  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
20184  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
20185  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
20186  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
20187  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
20188  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
20189  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
20190  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
20191
20192   /* Instructions that were new with the real FPA, call them V2.  */
20193 #undef  ARM_VARIANT
20194 #define ARM_VARIANT  & fpu_fpa_ext_v2
20195
20196  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20197  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20198  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20199  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20200  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20201  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20202
20203 #undef  ARM_VARIANT
20204 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
20205
20206   /* Moves and type conversions.  */
20207  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
20208  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
20209  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
20210  cCE("fmstat",  ef1fa10, 0, (),               noargs),
20211  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
20212  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
20213  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20214  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
20215  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20216  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20217  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20218  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20219  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
20220  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
20221
20222   /* Memory operations.  */
20223  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20224  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20225  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20226  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20227  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20228  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20229  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20230  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20231  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20232  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20233  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20234  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20235  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20236  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20237  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20238  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20239  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20240  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20241
20242   /* Monadic operations.  */
20243  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20244  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
20245  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20246
20247   /* Dyadic operations.  */
20248  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20249  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20250  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20251  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20252  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20253  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20254  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20255  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20256  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20257
20258   /* Comparisons.  */
20259  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
20260  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
20261  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20262  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
20263
20264  /* Double precision load/store are still present on single precision
20265     implementations.  */
20266  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20267  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20268  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20269  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20270  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20271  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20272  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20273  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20274  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20275  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20276
20277 #undef  ARM_VARIANT
20278 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
20279
20280   /* Moves and type conversions.  */
20281  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20282  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20283  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20284  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20285  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20286  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20287  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20288  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20289  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20290  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20291  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20292  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20293  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20294
20295   /* Monadic operations.  */
20296  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20297  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20298  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20299
20300   /* Dyadic operations.  */
20301  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20302  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20303  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20304  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20305  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20306  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20307  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20308  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20309  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20310
20311   /* Comparisons.  */
20312  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20313  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
20314  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20315  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
20316
20317 #undef  ARM_VARIANT
20318 #define ARM_VARIANT  & fpu_vfp_ext_v2
20319
20320  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
20321  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
20322  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
20323  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
20324
20325 /* Instructions which may belong to either the Neon or VFP instruction sets.
20326    Individual encoder functions perform additional architecture checks.  */
20327 #undef  ARM_VARIANT
20328 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
20329 #undef  THUMB_VARIANT
20330 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
20331
20332   /* These mnemonics are unique to VFP.  */
20333  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
20334  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
20335  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20336  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20337  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20338  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20339  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20340  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
20341  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
20342  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
20343
20344   /* Mnemonics shared by Neon and VFP.  */
20345  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
20346  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20347  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20348
20349  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20350  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20351
20352  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20353  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20354
20355  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20356  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20357  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20358  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20359  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20360  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20361  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20362  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20363
20364  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
20365  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
20366  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
20367  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
20368
20369
20370   /* NOTE: All VMOV encoding is special-cased!  */
20371  NCE(vmov,      0,       1, (VMOV), neon_mov),
20372  NCE(vmovq,     0,       1, (VMOV), neon_mov),
20373
20374 #undef  ARM_VARIANT
20375 #define ARM_VARIANT    & arm_ext_fp16
20376 #undef  THUMB_VARIANT
20377 #define THUMB_VARIANT  & arm_ext_fp16
20378  /* New instructions added from v8.2, allowing the extraction and insertion of
20379     the upper 16 bits of a 32-bit vector register.  */
20380  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
20381  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
20382
20383 #undef  THUMB_VARIANT
20384 #define THUMB_VARIANT  & fpu_neon_ext_v1
20385 #undef  ARM_VARIANT
20386 #define ARM_VARIANT    & fpu_neon_ext_v1
20387
20388   /* Data processing with three registers of the same length.  */
20389   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
20390  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
20391  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
20392  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20393  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20394  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20395  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20396  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20397  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20398   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
20399  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20400  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20401  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20402  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20403  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20404  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20405  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20406  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20407   /* If not immediate, fall back to neon_dyadic_i64_su.
20408      shl_imm should accept I8 I16 I32 I64,
20409      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
20410  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
20411  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
20412  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
20413  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
20414   /* Logic ops, types optional & ignored.  */
20415  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20416  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20417  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20418  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20419  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20420  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20421  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20422  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20423  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
20424  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
20425   /* Bitfield ops, untyped.  */
20426  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20427  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20428  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20429  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20430  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20431  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20432   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
20433  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20434  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20435  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20436  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20437  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20438  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20439   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
20440      back to neon_dyadic_if_su.  */
20441  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20442  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20443  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20444  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20445  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20446  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20447  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20448  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20449   /* Comparison. Type I8 I16 I32 F32.  */
20450  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
20451  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
20452   /* As above, D registers only.  */
20453  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20454  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20455   /* Int and float variants, signedness unimportant.  */
20456  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20457  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20458  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
20459   /* Add/sub take types I8 I16 I32 I64 F32.  */
20460  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20461  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20462   /* vtst takes sizes 8, 16, 32.  */
20463  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
20464  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
20465   /* VMUL takes I8 I16 I32 F32 P8.  */
20466  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
20467   /* VQD{R}MULH takes S16 S32.  */
20468  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20469  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20470  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20471  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20472  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20473  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20474  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20475  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20476  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20477  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20478  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20479  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20480  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20481  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20482  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20483  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20484  /* ARM v8.1 extension.  */
20485  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20486  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20487  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20488  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20489
20490   /* Two address, int/float. Types S8 S16 S32 F32.  */
20491  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
20492  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
20493
20494   /* Data processing with two registers and a shift amount.  */
20495   /* Right shifts, and variants with rounding.
20496      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
20497  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20498  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20499  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20500  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20501  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20502  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20503  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20504  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20505   /* Shift and insert. Sizes accepted 8 16 32 64.  */
20506  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
20507  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
20508  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
20509  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
20510   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
20511  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
20512  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
20513   /* Right shift immediate, saturating & narrowing, with rounding variants.
20514      Types accepted S16 S32 S64 U16 U32 U64.  */
20515  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20516  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20517   /* As above, unsigned. Types accepted S16 S32 S64.  */
20518  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20519  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20520   /* Right shift narrowing. Types accepted I16 I32 I64.  */
20521  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20522  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20523   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
20524  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
20525   /* CVT with optional immediate for fixed-point variant.  */
20526  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
20527
20528  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
20529  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
20530
20531   /* Data processing, three registers of different lengths.  */
20532   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
20533  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
20534  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
20535  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
20536  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
20537   /* If not scalar, fall back to neon_dyadic_long.
20538      Vector types as above, scalar types S16 S32 U16 U32.  */
20539  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20540  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20541   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
20542  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20543  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20544   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
20545  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20546  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20547  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20548  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20549   /* Saturating doubling multiplies. Types S16 S32.  */
20550  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20551  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20552  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20553   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
20554      S16 S32 U16 U32.  */
20555  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
20556
20557   /* Extract. Size 8.  */
20558  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
20559  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
20560
20561   /* Two registers, miscellaneous.  */
20562   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
20563  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
20564  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
20565  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
20566  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
20567  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
20568  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
20569   /* Vector replicate. Sizes 8 16 32.  */
20570  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
20571  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
20572   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
20573  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
20574   /* VMOVN. Types I16 I32 I64.  */
20575  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
20576   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
20577  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
20578   /* VQMOVUN. Types S16 S32 S64.  */
20579  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
20580   /* VZIP / VUZP. Sizes 8 16 32.  */
20581  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20582  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
20583  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20584  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
20585   /* VQABS / VQNEG. Types S8 S16 S32.  */
20586  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20587  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20588  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20589  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20590   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
20591  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
20592  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
20593  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
20594  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
20595   /* Reciprocal estimates.  Types U32 F16 F32.  */
20596  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
20597  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
20598  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
20599  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
20600   /* VCLS. Types S8 S16 S32.  */
20601  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
20602  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
20603   /* VCLZ. Types I8 I16 I32.  */
20604  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
20605  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
20606   /* VCNT. Size 8.  */
20607  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
20608  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
20609   /* Two address, untyped.  */
20610  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
20611  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
20612   /* VTRN. Sizes 8 16 32.  */
20613  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
20614  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
20615
20616   /* Table lookup. Size 8.  */
20617  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20618  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20619
20620 #undef  THUMB_VARIANT
20621 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
20622 #undef  ARM_VARIANT
20623 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
20624
20625   /* Neon element/structure load/store.  */
20626  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20627  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20628  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20629  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20630  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20631  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20632  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20633  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20634
20635 #undef  THUMB_VARIANT
20636 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
20637 #undef  ARM_VARIANT
20638 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
20639  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
20640  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20641  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20642  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20643  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20644  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20645  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20646  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20647  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20648
20649 #undef  THUMB_VARIANT
20650 #define THUMB_VARIANT  & fpu_vfp_ext_v3
20651 #undef  ARM_VARIANT
20652 #define ARM_VARIANT    & fpu_vfp_ext_v3
20653
20654  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
20655  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20656  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20657  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20658  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20659  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20660  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20661  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20662  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20663
20664 #undef  ARM_VARIANT
20665 #define ARM_VARIANT    & fpu_vfp_ext_fma
20666 #undef  THUMB_VARIANT
20667 #define THUMB_VARIANT  & fpu_vfp_ext_fma
20668  /* Mnemonics shared by Neon and VFP.  These are included in the
20669     VFP FMA variant; NEON and VFP FMA always includes the NEON
20670     FMA instructions.  */
20671  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20672  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20673  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
20674     the v form should always be used.  */
20675  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20676  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20677  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20678  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20679  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20680  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20681
20682 #undef THUMB_VARIANT
20683 #undef  ARM_VARIANT
20684 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
20685
20686  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20687  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20688  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20689  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20690  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20691  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20692  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
20693  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
20694
20695 #undef  ARM_VARIANT
20696 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
20697
20698  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
20699  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
20700  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
20701  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
20702  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
20703  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
20704  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
20705  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
20706  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
20707  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20708  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20709  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20710  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20711  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20712  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20713  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20714  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20715  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20716  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
20717  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
20718  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20719  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20720  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20721  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20722  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20723  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20724  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
20725  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
20726  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
20727  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
20728  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
20729  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
20730  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
20731  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
20732  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
20733  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
20734  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
20735  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20736  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20737  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20738  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20739  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20740  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20741  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20742  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20743  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20744  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
20745  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20746  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20747  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20748  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20749  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20750  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20751  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20752  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20753  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20754  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20755  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20756  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20757  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20758  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20759  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20760  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20761  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20762  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20763  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20764  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20765  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20766  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20767  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20768  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20769  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20770  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20771  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20772  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20773  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20774  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20775  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20776  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20777  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20778  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20779  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20780  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20781  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20782  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20783  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20784  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20785  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20786  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
20787  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20788  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20789  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20790  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20791  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20792  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20793  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20794  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20795  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20796  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20797  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20798  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20799  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20800  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20801  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20802  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20803  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20804  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20805  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20806  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20807  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20808  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
20809  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20810  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20811  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20812  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20813  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20814  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20815  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20816  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20817  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20818  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20819  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20820  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20821  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20822  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20823  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20824  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20825  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20826  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20827  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20828  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20829  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20830  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20831  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20832  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20833  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20834  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20835  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20836  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20837  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20838  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20839  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20840  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
20841  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
20842  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
20843  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
20844  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
20845  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
20846  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20847  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20848  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20849  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
20850  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
20851  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
20852  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
20853  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
20854  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
20855  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20856  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20857  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20858  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20859  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
20860
20861 #undef  ARM_VARIANT
20862 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
20863
20864  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
20865  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
20866  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
20867  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
20868  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
20869  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
20870  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20871  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20872  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20873  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20874  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20875  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20876  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20877  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20878  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20879  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20880  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20881  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20882  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20883  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20884  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
20885  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20886  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20887  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20888  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20889  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20890  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20891  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20892  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20893  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20894  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20895  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20896  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20897  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20898  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20899  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20900  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20901  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20902  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20903  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20904  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20905  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20906  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20907  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20908  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20909  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20910  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20911  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20912  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20913  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20914  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20915  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20916  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20917  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20918  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20919  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20920  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20921
20922 #undef  ARM_VARIANT
20923 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
20924
20925  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20926  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20927  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20928  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20929  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20930  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20931  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20932  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20933  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
20934  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
20935  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
20936  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
20937  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
20938  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
20939  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
20940  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
20941  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
20942  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
20943  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
20944  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
20945  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
20946  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
20947  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
20948  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
20949  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
20950  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
20951  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
20952  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
20953  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
20954  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
20955  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
20956  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
20957  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
20958  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
20959  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
20960  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
20961  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
20962  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
20963  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
20964  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
20965  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
20966  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
20967  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
20968  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
20969  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
20970  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
20971  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
20972  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
20973  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
20974  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
20975  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
20976  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
20977  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
20978  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
20979  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
20980  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20981  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
20982  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20983  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
20984  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
20985  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
20986  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
20987  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
20988  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
20989  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20990  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20991  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20992  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20993  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20994  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20995  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20996  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20997  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20998  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20999  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21000  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21001
21002  /* ARMv8-M instructions.  */
21003 #undef  ARM_VARIANT
21004 #define ARM_VARIANT NULL
21005 #undef  THUMB_VARIANT
21006 #define THUMB_VARIANT & arm_ext_v8m
21007  TUE("sg", 0, e97fe97f, 0, (), 0, noargs),
21008  TUE("blxns", 0, 4784, 1, (RRnpc), 0, t_blx),
21009  TUE("bxns", 0, 4704, 1, (RRnpc), 0, t_bx),
21010  TUE("tt", 0, e840f000, 2, (RRnpc, RRnpc), 0, tt),
21011  TUE("ttt", 0, e840f040, 2, (RRnpc, RRnpc), 0, tt),
21012  TUE("tta", 0, e840f080, 2, (RRnpc, RRnpc), 0, tt),
21013  TUE("ttat", 0, e840f0c0, 2, (RRnpc, RRnpc), 0, tt),
21014
21015  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
21016     instructions behave as nop if no VFP is present.  */
21017 #undef  THUMB_VARIANT
21018 #define THUMB_VARIANT & arm_ext_v8m_main
21019  TUEc("vlldm",  0,       ec300a00, 1, (RRnpc),  rn),
21020  TUEc("vlstm",  0,       ec200a00, 1, (RRnpc),  rn),
21021 };
21022 #undef ARM_VARIANT
21023 #undef THUMB_VARIANT
21024 #undef TCE
21025 #undef TUE
21026 #undef TUF
21027 #undef TCC
21028 #undef cCE
21029 #undef cCL
21030 #undef C3E
21031 #undef CE
21032 #undef CM
21033 #undef UE
21034 #undef UF
21035 #undef UT
21036 #undef NUF
21037 #undef nUF
21038 #undef NCE
21039 #undef nCE
21040 #undef OPS0
21041 #undef OPS1
21042 #undef OPS2
21043 #undef OPS3
21044 #undef OPS4
21045 #undef OPS5
21046 #undef OPS6
21047 #undef do_0
21048 \f
21049 /* MD interface: bits in the object file.  */
21050
21051 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
21052    for use in the a.out file, and stores them in the array pointed to by buf.
21053    This knows about the endian-ness of the target machine and does
21054    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
21055    2 (short) and 4 (long)  Floating numbers are put out as a series of
21056    LITTLENUMS (shorts, here at least).  */
21057
21058 void
21059 md_number_to_chars (char * buf, valueT val, int n)
21060 {
21061   if (target_big_endian)
21062     number_to_chars_bigendian (buf, val, n);
21063   else
21064     number_to_chars_littleendian (buf, val, n);
21065 }
21066
21067 static valueT
21068 md_chars_to_number (char * buf, int n)
21069 {
21070   valueT result = 0;
21071   unsigned char * where = (unsigned char *) buf;
21072
21073   if (target_big_endian)
21074     {
21075       while (n--)
21076         {
21077           result <<= 8;
21078           result |= (*where++ & 255);
21079         }
21080     }
21081   else
21082     {
21083       while (n--)
21084         {
21085           result <<= 8;
21086           result |= (where[n] & 255);
21087         }
21088     }
21089
21090   return result;
21091 }
21092
21093 /* MD interface: Sections.  */
21094
21095 /* Calculate the maximum variable size (i.e., excluding fr_fix)
21096    that an rs_machine_dependent frag may reach.  */
21097
21098 unsigned int
21099 arm_frag_max_var (fragS *fragp)
21100 {
21101   /* We only use rs_machine_dependent for variable-size Thumb instructions,
21102      which are either THUMB_SIZE (2) or INSN_SIZE (4).
21103
21104      Note that we generate relaxable instructions even for cases that don't
21105      really need it, like an immediate that's a trivial constant.  So we're
21106      overestimating the instruction size for some of those cases.  Rather
21107      than putting more intelligence here, it would probably be better to
21108      avoid generating a relaxation frag in the first place when it can be
21109      determined up front that a short instruction will suffice.  */
21110
21111   gas_assert (fragp->fr_type == rs_machine_dependent);
21112   return INSN_SIZE;
21113 }
21114
21115 /* Estimate the size of a frag before relaxing.  Assume everything fits in
21116    2 bytes.  */
21117
21118 int
21119 md_estimate_size_before_relax (fragS * fragp,
21120                                segT    segtype ATTRIBUTE_UNUSED)
21121 {
21122   fragp->fr_var = 2;
21123   return 2;
21124 }
21125
21126 /* Convert a machine dependent frag.  */
21127
21128 void
21129 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
21130 {
21131   unsigned long insn;
21132   unsigned long old_op;
21133   char *buf;
21134   expressionS exp;
21135   fixS *fixp;
21136   int reloc_type;
21137   int pc_rel;
21138   int opcode;
21139
21140   buf = fragp->fr_literal + fragp->fr_fix;
21141
21142   old_op = bfd_get_16(abfd, buf);
21143   if (fragp->fr_symbol)
21144     {
21145       exp.X_op = O_symbol;
21146       exp.X_add_symbol = fragp->fr_symbol;
21147     }
21148   else
21149     {
21150       exp.X_op = O_constant;
21151     }
21152   exp.X_add_number = fragp->fr_offset;
21153   opcode = fragp->fr_subtype;
21154   switch (opcode)
21155     {
21156     case T_MNEM_ldr_pc:
21157     case T_MNEM_ldr_pc2:
21158     case T_MNEM_ldr_sp:
21159     case T_MNEM_str_sp:
21160     case T_MNEM_ldr:
21161     case T_MNEM_ldrb:
21162     case T_MNEM_ldrh:
21163     case T_MNEM_str:
21164     case T_MNEM_strb:
21165     case T_MNEM_strh:
21166       if (fragp->fr_var == 4)
21167         {
21168           insn = THUMB_OP32 (opcode);
21169           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
21170             {
21171               insn |= (old_op & 0x700) << 4;
21172             }
21173           else
21174             {
21175               insn |= (old_op & 7) << 12;
21176               insn |= (old_op & 0x38) << 13;
21177             }
21178           insn |= 0x00000c00;
21179           put_thumb32_insn (buf, insn);
21180           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
21181         }
21182       else
21183         {
21184           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
21185         }
21186       pc_rel = (opcode == T_MNEM_ldr_pc2);
21187       break;
21188     case T_MNEM_adr:
21189       if (fragp->fr_var == 4)
21190         {
21191           insn = THUMB_OP32 (opcode);
21192           insn |= (old_op & 0xf0) << 4;
21193           put_thumb32_insn (buf, insn);
21194           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
21195         }
21196       else
21197         {
21198           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21199           exp.X_add_number -= 4;
21200         }
21201       pc_rel = 1;
21202       break;
21203     case T_MNEM_mov:
21204     case T_MNEM_movs:
21205     case T_MNEM_cmp:
21206     case T_MNEM_cmn:
21207       if (fragp->fr_var == 4)
21208         {
21209           int r0off = (opcode == T_MNEM_mov
21210                        || opcode == T_MNEM_movs) ? 0 : 8;
21211           insn = THUMB_OP32 (opcode);
21212           insn = (insn & 0xe1ffffff) | 0x10000000;
21213           insn |= (old_op & 0x700) << r0off;
21214           put_thumb32_insn (buf, insn);
21215           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21216         }
21217       else
21218         {
21219           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
21220         }
21221       pc_rel = 0;
21222       break;
21223     case T_MNEM_b:
21224       if (fragp->fr_var == 4)
21225         {
21226           insn = THUMB_OP32(opcode);
21227           put_thumb32_insn (buf, insn);
21228           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
21229         }
21230       else
21231         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
21232       pc_rel = 1;
21233       break;
21234     case T_MNEM_bcond:
21235       if (fragp->fr_var == 4)
21236         {
21237           insn = THUMB_OP32(opcode);
21238           insn |= (old_op & 0xf00) << 14;
21239           put_thumb32_insn (buf, insn);
21240           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
21241         }
21242       else
21243         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
21244       pc_rel = 1;
21245       break;
21246     case T_MNEM_add_sp:
21247     case T_MNEM_add_pc:
21248     case T_MNEM_inc_sp:
21249     case T_MNEM_dec_sp:
21250       if (fragp->fr_var == 4)
21251         {
21252           /* ??? Choose between add and addw.  */
21253           insn = THUMB_OP32 (opcode);
21254           insn |= (old_op & 0xf0) << 4;
21255           put_thumb32_insn (buf, insn);
21256           if (opcode == T_MNEM_add_pc)
21257             reloc_type = BFD_RELOC_ARM_T32_IMM12;
21258           else
21259             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21260         }
21261       else
21262         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21263       pc_rel = 0;
21264       break;
21265
21266     case T_MNEM_addi:
21267     case T_MNEM_addis:
21268     case T_MNEM_subi:
21269     case T_MNEM_subis:
21270       if (fragp->fr_var == 4)
21271         {
21272           insn = THUMB_OP32 (opcode);
21273           insn |= (old_op & 0xf0) << 4;
21274           insn |= (old_op & 0xf) << 16;
21275           put_thumb32_insn (buf, insn);
21276           if (insn & (1 << 20))
21277             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21278           else
21279             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21280         }
21281       else
21282         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21283       pc_rel = 0;
21284       break;
21285     default:
21286       abort ();
21287     }
21288   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
21289                       (enum bfd_reloc_code_real) reloc_type);
21290   fixp->fx_file = fragp->fr_file;
21291   fixp->fx_line = fragp->fr_line;
21292   fragp->fr_fix += fragp->fr_var;
21293
21294   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
21295   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
21296       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
21297     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
21298 }
21299
21300 /* Return the size of a relaxable immediate operand instruction.
21301    SHIFT and SIZE specify the form of the allowable immediate.  */
21302 static int
21303 relax_immediate (fragS *fragp, int size, int shift)
21304 {
21305   offsetT offset;
21306   offsetT mask;
21307   offsetT low;
21308
21309   /* ??? Should be able to do better than this.  */
21310   if (fragp->fr_symbol)
21311     return 4;
21312
21313   low = (1 << shift) - 1;
21314   mask = (1 << (shift + size)) - (1 << shift);
21315   offset = fragp->fr_offset;
21316   /* Force misaligned offsets to 32-bit variant.  */
21317   if (offset & low)
21318     return 4;
21319   if (offset & ~mask)
21320     return 4;
21321   return 2;
21322 }
21323
21324 /* Get the address of a symbol during relaxation.  */
21325 static addressT
21326 relaxed_symbol_addr (fragS *fragp, long stretch)
21327 {
21328   fragS *sym_frag;
21329   addressT addr;
21330   symbolS *sym;
21331
21332   sym = fragp->fr_symbol;
21333   sym_frag = symbol_get_frag (sym);
21334   know (S_GET_SEGMENT (sym) != absolute_section
21335         || sym_frag == &zero_address_frag);
21336   addr = S_GET_VALUE (sym) + fragp->fr_offset;
21337
21338   /* If frag has yet to be reached on this pass, assume it will
21339      move by STRETCH just as we did.  If this is not so, it will
21340      be because some frag between grows, and that will force
21341      another pass.  */
21342
21343   if (stretch != 0
21344       && sym_frag->relax_marker != fragp->relax_marker)
21345     {
21346       fragS *f;
21347
21348       /* Adjust stretch for any alignment frag.  Note that if have
21349          been expanding the earlier code, the symbol may be
21350          defined in what appears to be an earlier frag.  FIXME:
21351          This doesn't handle the fr_subtype field, which specifies
21352          a maximum number of bytes to skip when doing an
21353          alignment.  */
21354       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
21355         {
21356           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
21357             {
21358               if (stretch < 0)
21359                 stretch = - ((- stretch)
21360                              & ~ ((1 << (int) f->fr_offset) - 1));
21361               else
21362                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
21363               if (stretch == 0)
21364                 break;
21365             }
21366         }
21367       if (f != NULL)
21368         addr += stretch;
21369     }
21370
21371   return addr;
21372 }
21373
21374 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
21375    load.  */
21376 static int
21377 relax_adr (fragS *fragp, asection *sec, long stretch)
21378 {
21379   addressT addr;
21380   offsetT val;
21381
21382   /* Assume worst case for symbols not known to be in the same section.  */
21383   if (fragp->fr_symbol == NULL
21384       || !S_IS_DEFINED (fragp->fr_symbol)
21385       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21386       || S_IS_WEAK (fragp->fr_symbol))
21387     return 4;
21388
21389   val = relaxed_symbol_addr (fragp, stretch);
21390   addr = fragp->fr_address + fragp->fr_fix;
21391   addr = (addr + 4) & ~3;
21392   /* Force misaligned targets to 32-bit variant.  */
21393   if (val & 3)
21394     return 4;
21395   val -= addr;
21396   if (val < 0 || val > 1020)
21397     return 4;
21398   return 2;
21399 }
21400
21401 /* Return the size of a relaxable add/sub immediate instruction.  */
21402 static int
21403 relax_addsub (fragS *fragp, asection *sec)
21404 {
21405   char *buf;
21406   int op;
21407
21408   buf = fragp->fr_literal + fragp->fr_fix;
21409   op = bfd_get_16(sec->owner, buf);
21410   if ((op & 0xf) == ((op >> 4) & 0xf))
21411     return relax_immediate (fragp, 8, 0);
21412   else
21413     return relax_immediate (fragp, 3, 0);
21414 }
21415
21416 /* Return TRUE iff the definition of symbol S could be pre-empted
21417    (overridden) at link or load time.  */
21418 static bfd_boolean
21419 symbol_preemptible (symbolS *s)
21420 {
21421   /* Weak symbols can always be pre-empted.  */
21422   if (S_IS_WEAK (s))
21423     return TRUE;
21424
21425   /* Non-global symbols cannot be pre-empted. */
21426   if (! S_IS_EXTERNAL (s))
21427     return FALSE;
21428
21429 #ifdef OBJ_ELF
21430   /* In ELF, a global symbol can be marked protected, or private.  In that
21431      case it can't be pre-empted (other definitions in the same link unit
21432      would violate the ODR).  */
21433   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
21434     return FALSE;
21435 #endif
21436
21437   /* Other global symbols might be pre-empted.  */
21438   return TRUE;
21439 }
21440
21441 /* Return the size of a relaxable branch instruction.  BITS is the
21442    size of the offset field in the narrow instruction.  */
21443
21444 static int
21445 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
21446 {
21447   addressT addr;
21448   offsetT val;
21449   offsetT limit;
21450
21451   /* Assume worst case for symbols not known to be in the same section.  */
21452   if (!S_IS_DEFINED (fragp->fr_symbol)
21453       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21454       || S_IS_WEAK (fragp->fr_symbol))
21455     return 4;
21456
21457 #ifdef OBJ_ELF
21458   /* A branch to a function in ARM state will require interworking.  */
21459   if (S_IS_DEFINED (fragp->fr_symbol)
21460       && ARM_IS_FUNC (fragp->fr_symbol))
21461       return 4;
21462 #endif
21463
21464   if (symbol_preemptible (fragp->fr_symbol))
21465     return 4;
21466
21467   val = relaxed_symbol_addr (fragp, stretch);
21468   addr = fragp->fr_address + fragp->fr_fix + 4;
21469   val -= addr;
21470
21471   /* Offset is a signed value *2 */
21472   limit = 1 << bits;
21473   if (val >= limit || val < -limit)
21474     return 4;
21475   return 2;
21476 }
21477
21478
21479 /* Relax a machine dependent frag.  This returns the amount by which
21480    the current size of the frag should change.  */
21481
21482 int
21483 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
21484 {
21485   int oldsize;
21486   int newsize;
21487
21488   oldsize = fragp->fr_var;
21489   switch (fragp->fr_subtype)
21490     {
21491     case T_MNEM_ldr_pc2:
21492       newsize = relax_adr (fragp, sec, stretch);
21493       break;
21494     case T_MNEM_ldr_pc:
21495     case T_MNEM_ldr_sp:
21496     case T_MNEM_str_sp:
21497       newsize = relax_immediate (fragp, 8, 2);
21498       break;
21499     case T_MNEM_ldr:
21500     case T_MNEM_str:
21501       newsize = relax_immediate (fragp, 5, 2);
21502       break;
21503     case T_MNEM_ldrh:
21504     case T_MNEM_strh:
21505       newsize = relax_immediate (fragp, 5, 1);
21506       break;
21507     case T_MNEM_ldrb:
21508     case T_MNEM_strb:
21509       newsize = relax_immediate (fragp, 5, 0);
21510       break;
21511     case T_MNEM_adr:
21512       newsize = relax_adr (fragp, sec, stretch);
21513       break;
21514     case T_MNEM_mov:
21515     case T_MNEM_movs:
21516     case T_MNEM_cmp:
21517     case T_MNEM_cmn:
21518       newsize = relax_immediate (fragp, 8, 0);
21519       break;
21520     case T_MNEM_b:
21521       newsize = relax_branch (fragp, sec, 11, stretch);
21522       break;
21523     case T_MNEM_bcond:
21524       newsize = relax_branch (fragp, sec, 8, stretch);
21525       break;
21526     case T_MNEM_add_sp:
21527     case T_MNEM_add_pc:
21528       newsize = relax_immediate (fragp, 8, 2);
21529       break;
21530     case T_MNEM_inc_sp:
21531     case T_MNEM_dec_sp:
21532       newsize = relax_immediate (fragp, 7, 2);
21533       break;
21534     case T_MNEM_addi:
21535     case T_MNEM_addis:
21536     case T_MNEM_subi:
21537     case T_MNEM_subis:
21538       newsize = relax_addsub (fragp, sec);
21539       break;
21540     default:
21541       abort ();
21542     }
21543
21544   fragp->fr_var = newsize;
21545   /* Freeze wide instructions that are at or before the same location as
21546      in the previous pass.  This avoids infinite loops.
21547      Don't freeze them unconditionally because targets may be artificially
21548      misaligned by the expansion of preceding frags.  */
21549   if (stretch <= 0 && newsize > 2)
21550     {
21551       md_convert_frag (sec->owner, sec, fragp);
21552       frag_wane (fragp);
21553     }
21554
21555   return newsize - oldsize;
21556 }
21557
21558 /* Round up a section size to the appropriate boundary.  */
21559
21560 valueT
21561 md_section_align (segT   segment ATTRIBUTE_UNUSED,
21562                   valueT size)
21563 {
21564 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
21565   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
21566     {
21567       /* For a.out, force the section size to be aligned.  If we don't do
21568          this, BFD will align it for us, but it will not write out the
21569          final bytes of the section.  This may be a bug in BFD, but it is
21570          easier to fix it here since that is how the other a.out targets
21571          work.  */
21572       int align;
21573
21574       align = bfd_get_section_alignment (stdoutput, segment);
21575       size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
21576     }
21577 #endif
21578
21579   return size;
21580 }
21581
21582 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
21583    of an rs_align_code fragment.  */
21584
21585 void
21586 arm_handle_align (fragS * fragP)
21587 {
21588   static unsigned char const arm_noop[2][2][4] =
21589     {
21590       {  /* ARMv1 */
21591         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
21592         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
21593       },
21594       {  /* ARMv6k */
21595         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
21596         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
21597       },
21598     };
21599   static unsigned char const thumb_noop[2][2][2] =
21600     {
21601       {  /* Thumb-1 */
21602         {0xc0, 0x46},  /* LE */
21603         {0x46, 0xc0},  /* BE */
21604       },
21605       {  /* Thumb-2 */
21606         {0x00, 0xbf},  /* LE */
21607         {0xbf, 0x00}   /* BE */
21608       }
21609     };
21610   static unsigned char const wide_thumb_noop[2][4] =
21611     {  /* Wide Thumb-2 */
21612       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
21613       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
21614     };
21615
21616   unsigned bytes, fix, noop_size;
21617   char * p;
21618   const unsigned char * noop;
21619   const unsigned char *narrow_noop = NULL;
21620 #ifdef OBJ_ELF
21621   enum mstate state;
21622 #endif
21623
21624   if (fragP->fr_type != rs_align_code)
21625     return;
21626
21627   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
21628   p = fragP->fr_literal + fragP->fr_fix;
21629   fix = 0;
21630
21631   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
21632     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
21633
21634   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
21635
21636   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
21637     {
21638       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21639                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
21640         {
21641           narrow_noop = thumb_noop[1][target_big_endian];
21642           noop = wide_thumb_noop[target_big_endian];
21643         }
21644       else
21645         noop = thumb_noop[0][target_big_endian];
21646       noop_size = 2;
21647 #ifdef OBJ_ELF
21648       state = MAP_THUMB;
21649 #endif
21650     }
21651   else
21652     {
21653       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21654                                            ? selected_cpu : arm_arch_none,
21655                                            arm_ext_v6k) != 0]
21656                      [target_big_endian];
21657       noop_size = 4;
21658 #ifdef OBJ_ELF
21659       state = MAP_ARM;
21660 #endif
21661     }
21662
21663   fragP->fr_var = noop_size;
21664
21665   if (bytes & (noop_size - 1))
21666     {
21667       fix = bytes & (noop_size - 1);
21668 #ifdef OBJ_ELF
21669       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
21670 #endif
21671       memset (p, 0, fix);
21672       p += fix;
21673       bytes -= fix;
21674     }
21675
21676   if (narrow_noop)
21677     {
21678       if (bytes & noop_size)
21679         {
21680           /* Insert a narrow noop.  */
21681           memcpy (p, narrow_noop, noop_size);
21682           p += noop_size;
21683           bytes -= noop_size;
21684           fix += noop_size;
21685         }
21686
21687       /* Use wide noops for the remainder */
21688       noop_size = 4;
21689     }
21690
21691   while (bytes >= noop_size)
21692     {
21693       memcpy (p, noop, noop_size);
21694       p += noop_size;
21695       bytes -= noop_size;
21696       fix += noop_size;
21697     }
21698
21699   fragP->fr_fix += fix;
21700 }
21701
21702 /* Called from md_do_align.  Used to create an alignment
21703    frag in a code section.  */
21704
21705 void
21706 arm_frag_align_code (int n, int max)
21707 {
21708   char * p;
21709
21710   /* We assume that there will never be a requirement
21711      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
21712   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
21713     {
21714       char err_msg[128];
21715
21716       sprintf (err_msg,
21717         _("alignments greater than %d bytes not supported in .text sections."),
21718         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
21719       as_fatal ("%s", err_msg);
21720     }
21721
21722   p = frag_var (rs_align_code,
21723                 MAX_MEM_FOR_RS_ALIGN_CODE,
21724                 1,
21725                 (relax_substateT) max,
21726                 (symbolS *) NULL,
21727                 (offsetT) n,
21728                 (char *) NULL);
21729   *p = 0;
21730 }
21731
21732 /* Perform target specific initialisation of a frag.
21733    Note - despite the name this initialisation is not done when the frag
21734    is created, but only when its type is assigned.  A frag can be created
21735    and used a long time before its type is set, so beware of assuming that
21736    this initialisationis performed first.  */
21737
21738 #ifndef OBJ_ELF
21739 void
21740 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
21741 {
21742   /* Record whether this frag is in an ARM or a THUMB area.  */
21743   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21744 }
21745
21746 #else /* OBJ_ELF is defined.  */
21747 void
21748 arm_init_frag (fragS * fragP, int max_chars)
21749 {
21750   int frag_thumb_mode;
21751
21752   /* If the current ARM vs THUMB mode has not already
21753      been recorded into this frag then do so now.  */
21754   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
21755     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21756
21757   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
21758
21759   /* Record a mapping symbol for alignment frags.  We will delete this
21760      later if the alignment ends up empty.  */
21761   switch (fragP->fr_type)
21762     {
21763     case rs_align:
21764     case rs_align_test:
21765     case rs_fill:
21766       mapping_state_2 (MAP_DATA, max_chars);
21767       break;
21768     case rs_align_code:
21769       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
21770       break;
21771     default:
21772       break;
21773     }
21774 }
21775
21776 /* When we change sections we need to issue a new mapping symbol.  */
21777
21778 void
21779 arm_elf_change_section (void)
21780 {
21781   /* Link an unlinked unwind index table section to the .text section.  */
21782   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
21783       && elf_linked_to_section (now_seg) == NULL)
21784     elf_linked_to_section (now_seg) = text_section;
21785 }
21786
21787 int
21788 arm_elf_section_type (const char * str, size_t len)
21789 {
21790   if (len == 5 && strncmp (str, "exidx", 5) == 0)
21791     return SHT_ARM_EXIDX;
21792
21793   return -1;
21794 }
21795 \f
21796 /* Code to deal with unwinding tables.  */
21797
21798 static void add_unwind_adjustsp (offsetT);
21799
21800 /* Generate any deferred unwind frame offset.  */
21801
21802 static void
21803 flush_pending_unwind (void)
21804 {
21805   offsetT offset;
21806
21807   offset = unwind.pending_offset;
21808   unwind.pending_offset = 0;
21809   if (offset != 0)
21810     add_unwind_adjustsp (offset);
21811 }
21812
21813 /* Add an opcode to this list for this function.  Two-byte opcodes should
21814    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
21815    order.  */
21816
21817 static void
21818 add_unwind_opcode (valueT op, int length)
21819 {
21820   /* Add any deferred stack adjustment.  */
21821   if (unwind.pending_offset)
21822     flush_pending_unwind ();
21823
21824   unwind.sp_restored = 0;
21825
21826   if (unwind.opcode_count + length > unwind.opcode_alloc)
21827     {
21828       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
21829       if (unwind.opcodes)
21830         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
21831                                      unwind.opcode_alloc);
21832       else
21833         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
21834     }
21835   while (length > 0)
21836     {
21837       length--;
21838       unwind.opcodes[unwind.opcode_count] = op & 0xff;
21839       op >>= 8;
21840       unwind.opcode_count++;
21841     }
21842 }
21843
21844 /* Add unwind opcodes to adjust the stack pointer.  */
21845
21846 static void
21847 add_unwind_adjustsp (offsetT offset)
21848 {
21849   valueT op;
21850
21851   if (offset > 0x200)
21852     {
21853       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
21854       char bytes[5];
21855       int n;
21856       valueT o;
21857
21858       /* Long form: 0xb2, uleb128.  */
21859       /* This might not fit in a word so add the individual bytes,
21860          remembering the list is built in reverse order.  */
21861       o = (valueT) ((offset - 0x204) >> 2);
21862       if (o == 0)
21863         add_unwind_opcode (0, 1);
21864
21865       /* Calculate the uleb128 encoding of the offset.  */
21866       n = 0;
21867       while (o)
21868         {
21869           bytes[n] = o & 0x7f;
21870           o >>= 7;
21871           if (o)
21872             bytes[n] |= 0x80;
21873           n++;
21874         }
21875       /* Add the insn.  */
21876       for (; n; n--)
21877         add_unwind_opcode (bytes[n - 1], 1);
21878       add_unwind_opcode (0xb2, 1);
21879     }
21880   else if (offset > 0x100)
21881     {
21882       /* Two short opcodes.  */
21883       add_unwind_opcode (0x3f, 1);
21884       op = (offset - 0x104) >> 2;
21885       add_unwind_opcode (op, 1);
21886     }
21887   else if (offset > 0)
21888     {
21889       /* Short opcode.  */
21890       op = (offset - 4) >> 2;
21891       add_unwind_opcode (op, 1);
21892     }
21893   else if (offset < 0)
21894     {
21895       offset = -offset;
21896       while (offset > 0x100)
21897         {
21898           add_unwind_opcode (0x7f, 1);
21899           offset -= 0x100;
21900         }
21901       op = ((offset - 4) >> 2) | 0x40;
21902       add_unwind_opcode (op, 1);
21903     }
21904 }
21905
21906 /* Finish the list of unwind opcodes for this function.  */
21907 static void
21908 finish_unwind_opcodes (void)
21909 {
21910   valueT op;
21911
21912   if (unwind.fp_used)
21913     {
21914       /* Adjust sp as necessary.  */
21915       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
21916       flush_pending_unwind ();
21917
21918       /* After restoring sp from the frame pointer.  */
21919       op = 0x90 | unwind.fp_reg;
21920       add_unwind_opcode (op, 1);
21921     }
21922   else
21923     flush_pending_unwind ();
21924 }
21925
21926
21927 /* Start an exception table entry.  If idx is nonzero this is an index table
21928    entry.  */
21929
21930 static void
21931 start_unwind_section (const segT text_seg, int idx)
21932 {
21933   const char * text_name;
21934   const char * prefix;
21935   const char * prefix_once;
21936   const char * group_name;
21937   char * sec_name;
21938   int type;
21939   int flags;
21940   int linkonce;
21941
21942   if (idx)
21943     {
21944       prefix = ELF_STRING_ARM_unwind;
21945       prefix_once = ELF_STRING_ARM_unwind_once;
21946       type = SHT_ARM_EXIDX;
21947     }
21948   else
21949     {
21950       prefix = ELF_STRING_ARM_unwind_info;
21951       prefix_once = ELF_STRING_ARM_unwind_info_once;
21952       type = SHT_PROGBITS;
21953     }
21954
21955   text_name = segment_name (text_seg);
21956   if (streq (text_name, ".text"))
21957     text_name = "";
21958
21959   if (strncmp (text_name, ".gnu.linkonce.t.",
21960                strlen (".gnu.linkonce.t.")) == 0)
21961     {
21962       prefix = prefix_once;
21963       text_name += strlen (".gnu.linkonce.t.");
21964     }
21965
21966   sec_name = concat (prefix, text_name, (char *) NULL);
21967
21968   flags = SHF_ALLOC;
21969   linkonce = 0;
21970   group_name = 0;
21971
21972   /* Handle COMDAT group.  */
21973   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
21974     {
21975       group_name = elf_group_name (text_seg);
21976       if (group_name == NULL)
21977         {
21978           as_bad (_("Group section `%s' has no group signature"),
21979                   segment_name (text_seg));
21980           ignore_rest_of_line ();
21981           return;
21982         }
21983       flags |= SHF_GROUP;
21984       linkonce = 1;
21985     }
21986
21987   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
21988
21989   /* Set the section link for index tables.  */
21990   if (idx)
21991     elf_linked_to_section (now_seg) = text_seg;
21992 }
21993
21994
21995 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
21996    personality routine data.  Returns zero, or the index table value for
21997    an inline entry.  */
21998
21999 static valueT
22000 create_unwind_entry (int have_data)
22001 {
22002   int size;
22003   addressT where;
22004   char *ptr;
22005   /* The current word of data.  */
22006   valueT data;
22007   /* The number of bytes left in this word.  */
22008   int n;
22009
22010   finish_unwind_opcodes ();
22011
22012   /* Remember the current text section.  */
22013   unwind.saved_seg = now_seg;
22014   unwind.saved_subseg = now_subseg;
22015
22016   start_unwind_section (now_seg, 0);
22017
22018   if (unwind.personality_routine == NULL)
22019     {
22020       if (unwind.personality_index == -2)
22021         {
22022           if (have_data)
22023             as_bad (_("handlerdata in cantunwind frame"));
22024           return 1; /* EXIDX_CANTUNWIND.  */
22025         }
22026
22027       /* Use a default personality routine if none is specified.  */
22028       if (unwind.personality_index == -1)
22029         {
22030           if (unwind.opcode_count > 3)
22031             unwind.personality_index = 1;
22032           else
22033             unwind.personality_index = 0;
22034         }
22035
22036       /* Space for the personality routine entry.  */
22037       if (unwind.personality_index == 0)
22038         {
22039           if (unwind.opcode_count > 3)
22040             as_bad (_("too many unwind opcodes for personality routine 0"));
22041
22042           if (!have_data)
22043             {
22044               /* All the data is inline in the index table.  */
22045               data = 0x80;
22046               n = 3;
22047               while (unwind.opcode_count > 0)
22048                 {
22049                   unwind.opcode_count--;
22050                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22051                   n--;
22052                 }
22053
22054               /* Pad with "finish" opcodes.  */
22055               while (n--)
22056                 data = (data << 8) | 0xb0;
22057
22058               return data;
22059             }
22060           size = 0;
22061         }
22062       else
22063         /* We get two opcodes "free" in the first word.  */
22064         size = unwind.opcode_count - 2;
22065     }
22066   else
22067     {
22068       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
22069       if (unwind.personality_index != -1)
22070         {
22071           as_bad (_("attempt to recreate an unwind entry"));
22072           return 1;
22073         }
22074
22075       /* An extra byte is required for the opcode count.        */
22076       size = unwind.opcode_count + 1;
22077     }
22078
22079   size = (size + 3) >> 2;
22080   if (size > 0xff)
22081     as_bad (_("too many unwind opcodes"));
22082
22083   frag_align (2, 0, 0);
22084   record_alignment (now_seg, 2);
22085   unwind.table_entry = expr_build_dot ();
22086
22087   /* Allocate the table entry.  */
22088   ptr = frag_more ((size << 2) + 4);
22089   /* PR 13449: Zero the table entries in case some of them are not used.  */
22090   memset (ptr, 0, (size << 2) + 4);
22091   where = frag_now_fix () - ((size << 2) + 4);
22092
22093   switch (unwind.personality_index)
22094     {
22095     case -1:
22096       /* ??? Should this be a PLT generating relocation?  */
22097       /* Custom personality routine.  */
22098       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
22099                BFD_RELOC_ARM_PREL31);
22100
22101       where += 4;
22102       ptr += 4;
22103
22104       /* Set the first byte to the number of additional words.  */
22105       data = size > 0 ? size - 1 : 0;
22106       n = 3;
22107       break;
22108
22109     /* ABI defined personality routines.  */
22110     case 0:
22111       /* Three opcodes bytes are packed into the first word.  */
22112       data = 0x80;
22113       n = 3;
22114       break;
22115
22116     case 1:
22117     case 2:
22118       /* The size and first two opcode bytes go in the first word.  */
22119       data = ((0x80 + unwind.personality_index) << 8) | size;
22120       n = 2;
22121       break;
22122
22123     default:
22124       /* Should never happen.  */
22125       abort ();
22126     }
22127
22128   /* Pack the opcodes into words (MSB first), reversing the list at the same
22129      time.  */
22130   while (unwind.opcode_count > 0)
22131     {
22132       if (n == 0)
22133         {
22134           md_number_to_chars (ptr, data, 4);
22135           ptr += 4;
22136           n = 4;
22137           data = 0;
22138         }
22139       unwind.opcode_count--;
22140       n--;
22141       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22142     }
22143
22144   /* Finish off the last word.  */
22145   if (n < 4)
22146     {
22147       /* Pad with "finish" opcodes.  */
22148       while (n--)
22149         data = (data << 8) | 0xb0;
22150
22151       md_number_to_chars (ptr, data, 4);
22152     }
22153
22154   if (!have_data)
22155     {
22156       /* Add an empty descriptor if there is no user-specified data.   */
22157       ptr = frag_more (4);
22158       md_number_to_chars (ptr, 0, 4);
22159     }
22160
22161   return 0;
22162 }
22163
22164
22165 /* Initialize the DWARF-2 unwind information for this procedure.  */
22166
22167 void
22168 tc_arm_frame_initial_instructions (void)
22169 {
22170   cfi_add_CFA_def_cfa (REG_SP, 0);
22171 }
22172 #endif /* OBJ_ELF */
22173
22174 /* Convert REGNAME to a DWARF-2 register number.  */
22175
22176 int
22177 tc_arm_regname_to_dw2regnum (char *regname)
22178 {
22179   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
22180   if (reg != FAIL)
22181     return reg;
22182
22183   /* PR 16694: Allow VFP registers as well.  */
22184   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
22185   if (reg != FAIL)
22186     return 64 + reg;
22187
22188   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
22189   if (reg != FAIL)
22190     return reg + 256;
22191
22192   return -1;
22193 }
22194
22195 #ifdef TE_PE
22196 void
22197 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
22198 {
22199   expressionS exp;
22200
22201   exp.X_op = O_secrel;
22202   exp.X_add_symbol = symbol;
22203   exp.X_add_number = 0;
22204   emit_expr (&exp, size);
22205 }
22206 #endif
22207
22208 /* MD interface: Symbol and relocation handling.  */
22209
22210 /* Return the address within the segment that a PC-relative fixup is
22211    relative to.  For ARM, PC-relative fixups applied to instructions
22212    are generally relative to the location of the fixup plus 8 bytes.
22213    Thumb branches are offset by 4, and Thumb loads relative to PC
22214    require special handling.  */
22215
22216 long
22217 md_pcrel_from_section (fixS * fixP, segT seg)
22218 {
22219   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
22220
22221   /* If this is pc-relative and we are going to emit a relocation
22222      then we just want to put out any pipeline compensation that the linker
22223      will need.  Otherwise we want to use the calculated base.
22224      For WinCE we skip the bias for externals as well, since this
22225      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
22226   if (fixP->fx_pcrel
22227       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
22228           || (arm_force_relocation (fixP)
22229 #ifdef TE_WINCE
22230               && !S_IS_EXTERNAL (fixP->fx_addsy)
22231 #endif
22232               )))
22233     base = 0;
22234
22235
22236   switch (fixP->fx_r_type)
22237     {
22238       /* PC relative addressing on the Thumb is slightly odd as the
22239          bottom two bits of the PC are forced to zero for the
22240          calculation.  This happens *after* application of the
22241          pipeline offset.  However, Thumb adrl already adjusts for
22242          this, so we need not do it again.  */
22243     case BFD_RELOC_ARM_THUMB_ADD:
22244       return base & ~3;
22245
22246     case BFD_RELOC_ARM_THUMB_OFFSET:
22247     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22248     case BFD_RELOC_ARM_T32_ADD_PC12:
22249     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22250       return (base + 4) & ~3;
22251
22252       /* Thumb branches are simply offset by +4.  */
22253     case BFD_RELOC_THUMB_PCREL_BRANCH7:
22254     case BFD_RELOC_THUMB_PCREL_BRANCH9:
22255     case BFD_RELOC_THUMB_PCREL_BRANCH12:
22256     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22257     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22258       return base + 4;
22259
22260     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22261       if (fixP->fx_addsy
22262           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22263           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22264           && ARM_IS_FUNC (fixP->fx_addsy)
22265           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22266         base = fixP->fx_where + fixP->fx_frag->fr_address;
22267        return base + 4;
22268
22269       /* BLX is like branches above, but forces the low two bits of PC to
22270          zero.  */
22271     case BFD_RELOC_THUMB_PCREL_BLX:
22272       if (fixP->fx_addsy
22273           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22274           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22275           && THUMB_IS_FUNC (fixP->fx_addsy)
22276           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22277         base = fixP->fx_where + fixP->fx_frag->fr_address;
22278       return (base + 4) & ~3;
22279
22280       /* ARM mode branches are offset by +8.  However, the Windows CE
22281          loader expects the relocation not to take this into account.  */
22282     case BFD_RELOC_ARM_PCREL_BLX:
22283       if (fixP->fx_addsy
22284           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22285           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22286           && ARM_IS_FUNC (fixP->fx_addsy)
22287           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22288         base = fixP->fx_where + fixP->fx_frag->fr_address;
22289       return base + 8;
22290
22291     case BFD_RELOC_ARM_PCREL_CALL:
22292       if (fixP->fx_addsy
22293           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22294           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22295           && THUMB_IS_FUNC (fixP->fx_addsy)
22296           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22297         base = fixP->fx_where + fixP->fx_frag->fr_address;
22298       return base + 8;
22299
22300     case BFD_RELOC_ARM_PCREL_BRANCH:
22301     case BFD_RELOC_ARM_PCREL_JUMP:
22302     case BFD_RELOC_ARM_PLT32:
22303 #ifdef TE_WINCE
22304       /* When handling fixups immediately, because we have already
22305          discovered the value of a symbol, or the address of the frag involved
22306          we must account for the offset by +8, as the OS loader will never see the reloc.
22307          see fixup_segment() in write.c
22308          The S_IS_EXTERNAL test handles the case of global symbols.
22309          Those need the calculated base, not just the pipe compensation the linker will need.  */
22310       if (fixP->fx_pcrel
22311           && fixP->fx_addsy != NULL
22312           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22313           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
22314         return base + 8;
22315       return base;
22316 #else
22317       return base + 8;
22318 #endif
22319
22320
22321       /* ARM mode loads relative to PC are also offset by +8.  Unlike
22322          branches, the Windows CE loader *does* expect the relocation
22323          to take this into account.  */
22324     case BFD_RELOC_ARM_OFFSET_IMM:
22325     case BFD_RELOC_ARM_OFFSET_IMM8:
22326     case BFD_RELOC_ARM_HWLITERAL:
22327     case BFD_RELOC_ARM_LITERAL:
22328     case BFD_RELOC_ARM_CP_OFF_IMM:
22329       return base + 8;
22330
22331
22332       /* Other PC-relative relocations are un-offset.  */
22333     default:
22334       return base;
22335     }
22336 }
22337
22338 static bfd_boolean flag_warn_syms = TRUE;
22339
22340 bfd_boolean
22341 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
22342 {
22343   /* PR 18347 - Warn if the user attempts to create a symbol with the same
22344      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
22345      does mean that the resulting code might be very confusing to the reader.
22346      Also this warning can be triggered if the user omits an operand before
22347      an immediate address, eg:
22348
22349        LDR =foo
22350
22351      GAS treats this as an assignment of the value of the symbol foo to a
22352      symbol LDR, and so (without this code) it will not issue any kind of
22353      warning or error message.
22354
22355      Note - ARM instructions are case-insensitive but the strings in the hash
22356      table are all stored in lower case, so we must first ensure that name is
22357      lower case too.  */
22358   if (flag_warn_syms && arm_ops_hsh)
22359     {
22360       char * nbuf = strdup (name);
22361       char * p;
22362
22363       for (p = nbuf; *p; p++)
22364         *p = TOLOWER (*p);
22365       if (hash_find (arm_ops_hsh, nbuf) != NULL)
22366         {
22367           static struct hash_control * already_warned = NULL;
22368
22369           if (already_warned == NULL)
22370             already_warned = hash_new ();
22371           /* Only warn about the symbol once.  To keep the code
22372              simple we let hash_insert do the lookup for us.  */
22373           if (hash_insert (already_warned, name, NULL) == NULL)
22374             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
22375         }
22376       else
22377         free (nbuf);
22378     }
22379
22380   return FALSE;
22381 }
22382
22383 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
22384    Otherwise we have no need to default values of symbols.  */
22385
22386 symbolS *
22387 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
22388 {
22389 #ifdef OBJ_ELF
22390   if (name[0] == '_' && name[1] == 'G'
22391       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
22392     {
22393       if (!GOT_symbol)
22394         {
22395           if (symbol_find (name))
22396             as_bad (_("GOT already in the symbol table"));
22397
22398           GOT_symbol = symbol_new (name, undefined_section,
22399                                    (valueT) 0, & zero_address_frag);
22400         }
22401
22402       return GOT_symbol;
22403     }
22404 #endif
22405
22406   return NULL;
22407 }
22408
22409 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
22410    computed as two separate immediate values, added together.  We
22411    already know that this value cannot be computed by just one ARM
22412    instruction.  */
22413
22414 static unsigned int
22415 validate_immediate_twopart (unsigned int   val,
22416                             unsigned int * highpart)
22417 {
22418   unsigned int a;
22419   unsigned int i;
22420
22421   for (i = 0; i < 32; i += 2)
22422     if (((a = rotate_left (val, i)) & 0xff) != 0)
22423       {
22424         if (a & 0xff00)
22425           {
22426             if (a & ~ 0xffff)
22427               continue;
22428             * highpart = (a  >> 8) | ((i + 24) << 7);
22429           }
22430         else if (a & 0xff0000)
22431           {
22432             if (a & 0xff000000)
22433               continue;
22434             * highpart = (a >> 16) | ((i + 16) << 7);
22435           }
22436         else
22437           {
22438             gas_assert (a & 0xff000000);
22439             * highpart = (a >> 24) | ((i + 8) << 7);
22440           }
22441
22442         return (a & 0xff) | (i << 7);
22443       }
22444
22445   return FAIL;
22446 }
22447
22448 static int
22449 validate_offset_imm (unsigned int val, int hwse)
22450 {
22451   if ((hwse && val > 255) || val > 4095)
22452     return FAIL;
22453   return val;
22454 }
22455
22456 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
22457    negative immediate constant by altering the instruction.  A bit of
22458    a hack really.
22459         MOV <-> MVN
22460         AND <-> BIC
22461         ADC <-> SBC
22462         by inverting the second operand, and
22463         ADD <-> SUB
22464         CMP <-> CMN
22465         by negating the second operand.  */
22466
22467 static int
22468 negate_data_op (unsigned long * instruction,
22469                 unsigned long   value)
22470 {
22471   int op, new_inst;
22472   unsigned long negated, inverted;
22473
22474   negated = encode_arm_immediate (-value);
22475   inverted = encode_arm_immediate (~value);
22476
22477   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
22478   switch (op)
22479     {
22480       /* First negates.  */
22481     case OPCODE_SUB:             /* ADD <-> SUB  */
22482       new_inst = OPCODE_ADD;
22483       value = negated;
22484       break;
22485
22486     case OPCODE_ADD:
22487       new_inst = OPCODE_SUB;
22488       value = negated;
22489       break;
22490
22491     case OPCODE_CMP:             /* CMP <-> CMN  */
22492       new_inst = OPCODE_CMN;
22493       value = negated;
22494       break;
22495
22496     case OPCODE_CMN:
22497       new_inst = OPCODE_CMP;
22498       value = negated;
22499       break;
22500
22501       /* Now Inverted ops.  */
22502     case OPCODE_MOV:             /* MOV <-> MVN  */
22503       new_inst = OPCODE_MVN;
22504       value = inverted;
22505       break;
22506
22507     case OPCODE_MVN:
22508       new_inst = OPCODE_MOV;
22509       value = inverted;
22510       break;
22511
22512     case OPCODE_AND:             /* AND <-> BIC  */
22513       new_inst = OPCODE_BIC;
22514       value = inverted;
22515       break;
22516
22517     case OPCODE_BIC:
22518       new_inst = OPCODE_AND;
22519       value = inverted;
22520       break;
22521
22522     case OPCODE_ADC:              /* ADC <-> SBC  */
22523       new_inst = OPCODE_SBC;
22524       value = inverted;
22525       break;
22526
22527     case OPCODE_SBC:
22528       new_inst = OPCODE_ADC;
22529       value = inverted;
22530       break;
22531
22532       /* We cannot do anything.  */
22533     default:
22534       return FAIL;
22535     }
22536
22537   if (value == (unsigned) FAIL)
22538     return FAIL;
22539
22540   *instruction &= OPCODE_MASK;
22541   *instruction |= new_inst << DATA_OP_SHIFT;
22542   return value;
22543 }
22544
22545 /* Like negate_data_op, but for Thumb-2.   */
22546
22547 static unsigned int
22548 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
22549 {
22550   int op, new_inst;
22551   int rd;
22552   unsigned int negated, inverted;
22553
22554   negated = encode_thumb32_immediate (-value);
22555   inverted = encode_thumb32_immediate (~value);
22556
22557   rd = (*instruction >> 8) & 0xf;
22558   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
22559   switch (op)
22560     {
22561       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
22562     case T2_OPCODE_SUB:
22563       new_inst = T2_OPCODE_ADD;
22564       value = negated;
22565       break;
22566
22567     case T2_OPCODE_ADD:
22568       new_inst = T2_OPCODE_SUB;
22569       value = negated;
22570       break;
22571
22572       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
22573     case T2_OPCODE_ORR:
22574       new_inst = T2_OPCODE_ORN;
22575       value = inverted;
22576       break;
22577
22578     case T2_OPCODE_ORN:
22579       new_inst = T2_OPCODE_ORR;
22580       value = inverted;
22581       break;
22582
22583       /* AND <-> BIC.  TST has no inverted equivalent.  */
22584     case T2_OPCODE_AND:
22585       new_inst = T2_OPCODE_BIC;
22586       if (rd == 15)
22587         value = FAIL;
22588       else
22589         value = inverted;
22590       break;
22591
22592     case T2_OPCODE_BIC:
22593       new_inst = T2_OPCODE_AND;
22594       value = inverted;
22595       break;
22596
22597       /* ADC <-> SBC  */
22598     case T2_OPCODE_ADC:
22599       new_inst = T2_OPCODE_SBC;
22600       value = inverted;
22601       break;
22602
22603     case T2_OPCODE_SBC:
22604       new_inst = T2_OPCODE_ADC;
22605       value = inverted;
22606       break;
22607
22608       /* We cannot do anything.  */
22609     default:
22610       return FAIL;
22611     }
22612
22613   if (value == (unsigned int)FAIL)
22614     return FAIL;
22615
22616   *instruction &= T2_OPCODE_MASK;
22617   *instruction |= new_inst << T2_DATA_OP_SHIFT;
22618   return value;
22619 }
22620
22621 /* Read a 32-bit thumb instruction from buf.  */
22622 static unsigned long
22623 get_thumb32_insn (char * buf)
22624 {
22625   unsigned long insn;
22626   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
22627   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22628
22629   return insn;
22630 }
22631
22632
22633 /* We usually want to set the low bit on the address of thumb function
22634    symbols.  In particular .word foo - . should have the low bit set.
22635    Generic code tries to fold the difference of two symbols to
22636    a constant.  Prevent this and force a relocation when the first symbols
22637    is a thumb function.  */
22638
22639 bfd_boolean
22640 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
22641 {
22642   if (op == O_subtract
22643       && l->X_op == O_symbol
22644       && r->X_op == O_symbol
22645       && THUMB_IS_FUNC (l->X_add_symbol))
22646     {
22647       l->X_op = O_subtract;
22648       l->X_op_symbol = r->X_add_symbol;
22649       l->X_add_number -= r->X_add_number;
22650       return TRUE;
22651     }
22652
22653   /* Process as normal.  */
22654   return FALSE;
22655 }
22656
22657 /* Encode Thumb2 unconditional branches and calls. The encoding
22658    for the 2 are identical for the immediate values.  */
22659
22660 static void
22661 encode_thumb2_b_bl_offset (char * buf, offsetT value)
22662 {
22663 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
22664   offsetT newval;
22665   offsetT newval2;
22666   addressT S, I1, I2, lo, hi;
22667
22668   S = (value >> 24) & 0x01;
22669   I1 = (value >> 23) & 0x01;
22670   I2 = (value >> 22) & 0x01;
22671   hi = (value >> 12) & 0x3ff;
22672   lo = (value >> 1) & 0x7ff;
22673   newval   = md_chars_to_number (buf, THUMB_SIZE);
22674   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22675   newval  |= (S << 10) | hi;
22676   newval2 &=  ~T2I1I2MASK;
22677   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
22678   md_number_to_chars (buf, newval, THUMB_SIZE);
22679   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22680 }
22681
22682 void
22683 md_apply_fix (fixS *    fixP,
22684                valueT * valP,
22685                segT     seg)
22686 {
22687   offsetT        value = * valP;
22688   offsetT        newval;
22689   unsigned int   newimm;
22690   unsigned long  temp;
22691   int            sign;
22692   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
22693
22694   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
22695
22696   /* Note whether this will delete the relocation.  */
22697
22698   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
22699     fixP->fx_done = 1;
22700
22701   /* On a 64-bit host, silently truncate 'value' to 32 bits for
22702      consistency with the behaviour on 32-bit hosts.  Remember value
22703      for emit_reloc.  */
22704   value &= 0xffffffff;
22705   value ^= 0x80000000;
22706   value -= 0x80000000;
22707
22708   *valP = value;
22709   fixP->fx_addnumber = value;
22710
22711   /* Same treatment for fixP->fx_offset.  */
22712   fixP->fx_offset &= 0xffffffff;
22713   fixP->fx_offset ^= 0x80000000;
22714   fixP->fx_offset -= 0x80000000;
22715
22716   switch (fixP->fx_r_type)
22717     {
22718     case BFD_RELOC_NONE:
22719       /* This will need to go in the object file.  */
22720       fixP->fx_done = 0;
22721       break;
22722
22723     case BFD_RELOC_ARM_IMMEDIATE:
22724       /* We claim that this fixup has been processed here,
22725          even if in fact we generate an error because we do
22726          not have a reloc for it, so tc_gen_reloc will reject it.  */
22727       fixP->fx_done = 1;
22728
22729       if (fixP->fx_addsy)
22730         {
22731           const char *msg = 0;
22732
22733           if (! S_IS_DEFINED (fixP->fx_addsy))
22734             msg = _("undefined symbol %s used as an immediate value");
22735           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22736             msg = _("symbol %s is in a different section");
22737           else if (S_IS_WEAK (fixP->fx_addsy))
22738             msg = _("symbol %s is weak and may be overridden later");
22739
22740           if (msg)
22741             {
22742               as_bad_where (fixP->fx_file, fixP->fx_line,
22743                             msg, S_GET_NAME (fixP->fx_addsy));
22744               break;
22745             }
22746         }
22747
22748       temp = md_chars_to_number (buf, INSN_SIZE);
22749
22750       /* If the offset is negative, we should use encoding A2 for ADR.  */
22751       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
22752         newimm = negate_data_op (&temp, value);
22753       else
22754         {
22755           newimm = encode_arm_immediate (value);
22756
22757           /* If the instruction will fail, see if we can fix things up by
22758              changing the opcode.  */
22759           if (newimm == (unsigned int) FAIL)
22760             newimm = negate_data_op (&temp, value);
22761         }
22762
22763       if (newimm == (unsigned int) FAIL)
22764         {
22765           as_bad_where (fixP->fx_file, fixP->fx_line,
22766                         _("invalid constant (%lx) after fixup"),
22767                         (unsigned long) value);
22768           break;
22769         }
22770
22771       newimm |= (temp & 0xfffff000);
22772       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22773       break;
22774
22775     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
22776       {
22777         unsigned int highpart = 0;
22778         unsigned int newinsn  = 0xe1a00000; /* nop.  */
22779
22780         if (fixP->fx_addsy)
22781           {
22782             const char *msg = 0;
22783
22784             if (! S_IS_DEFINED (fixP->fx_addsy))
22785               msg = _("undefined symbol %s used as an immediate value");
22786             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22787               msg = _("symbol %s is in a different section");
22788             else if (S_IS_WEAK (fixP->fx_addsy))
22789               msg = _("symbol %s is weak and may be overridden later");
22790
22791             if (msg)
22792               {
22793                 as_bad_where (fixP->fx_file, fixP->fx_line,
22794                               msg, S_GET_NAME (fixP->fx_addsy));
22795                 break;
22796               }
22797           }
22798
22799         newimm = encode_arm_immediate (value);
22800         temp = md_chars_to_number (buf, INSN_SIZE);
22801
22802         /* If the instruction will fail, see if we can fix things up by
22803            changing the opcode.  */
22804         if (newimm == (unsigned int) FAIL
22805             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
22806           {
22807             /* No ?  OK - try using two ADD instructions to generate
22808                the value.  */
22809             newimm = validate_immediate_twopart (value, & highpart);
22810
22811             /* Yes - then make sure that the second instruction is
22812                also an add.  */
22813             if (newimm != (unsigned int) FAIL)
22814               newinsn = temp;
22815             /* Still No ?  Try using a negated value.  */
22816             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
22817               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
22818             /* Otherwise - give up.  */
22819             else
22820               {
22821                 as_bad_where (fixP->fx_file, fixP->fx_line,
22822                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
22823                               (long) value);
22824                 break;
22825               }
22826
22827             /* Replace the first operand in the 2nd instruction (which
22828                is the PC) with the destination register.  We have
22829                already added in the PC in the first instruction and we
22830                do not want to do it again.  */
22831             newinsn &= ~ 0xf0000;
22832             newinsn |= ((newinsn & 0x0f000) << 4);
22833           }
22834
22835         newimm |= (temp & 0xfffff000);
22836         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22837
22838         highpart |= (newinsn & 0xfffff000);
22839         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
22840       }
22841       break;
22842
22843     case BFD_RELOC_ARM_OFFSET_IMM:
22844       if (!fixP->fx_done && seg->use_rela_p)
22845         value = 0;
22846       /* Fall through.  */
22847
22848     case BFD_RELOC_ARM_LITERAL:
22849       sign = value > 0;
22850
22851       if (value < 0)
22852         value = - value;
22853
22854       if (validate_offset_imm (value, 0) == FAIL)
22855         {
22856           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
22857             as_bad_where (fixP->fx_file, fixP->fx_line,
22858                           _("invalid literal constant: pool needs to be closer"));
22859           else
22860             as_bad_where (fixP->fx_file, fixP->fx_line,
22861                           _("bad immediate value for offset (%ld)"),
22862                           (long) value);
22863           break;
22864         }
22865
22866       newval = md_chars_to_number (buf, INSN_SIZE);
22867       if (value == 0)
22868         newval &= 0xfffff000;
22869       else
22870         {
22871           newval &= 0xff7ff000;
22872           newval |= value | (sign ? INDEX_UP : 0);
22873         }
22874       md_number_to_chars (buf, newval, INSN_SIZE);
22875       break;
22876
22877     case BFD_RELOC_ARM_OFFSET_IMM8:
22878     case BFD_RELOC_ARM_HWLITERAL:
22879       sign = value > 0;
22880
22881       if (value < 0)
22882         value = - value;
22883
22884       if (validate_offset_imm (value, 1) == FAIL)
22885         {
22886           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
22887             as_bad_where (fixP->fx_file, fixP->fx_line,
22888                           _("invalid literal constant: pool needs to be closer"));
22889           else
22890             as_bad_where (fixP->fx_file, fixP->fx_line,
22891                           _("bad immediate value for 8-bit offset (%ld)"),
22892                           (long) value);
22893           break;
22894         }
22895
22896       newval = md_chars_to_number (buf, INSN_SIZE);
22897       if (value == 0)
22898         newval &= 0xfffff0f0;
22899       else
22900         {
22901           newval &= 0xff7ff0f0;
22902           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
22903         }
22904       md_number_to_chars (buf, newval, INSN_SIZE);
22905       break;
22906
22907     case BFD_RELOC_ARM_T32_OFFSET_U8:
22908       if (value < 0 || value > 1020 || value % 4 != 0)
22909         as_bad_where (fixP->fx_file, fixP->fx_line,
22910                       _("bad immediate value for offset (%ld)"), (long) value);
22911       value /= 4;
22912
22913       newval = md_chars_to_number (buf+2, THUMB_SIZE);
22914       newval |= value;
22915       md_number_to_chars (buf+2, newval, THUMB_SIZE);
22916       break;
22917
22918     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22919       /* This is a complicated relocation used for all varieties of Thumb32
22920          load/store instruction with immediate offset:
22921
22922          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
22923                                                    *4, optional writeback(W)
22924                                                    (doubleword load/store)
22925
22926          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
22927          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
22928          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
22929          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
22930          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
22931
22932          Uppercase letters indicate bits that are already encoded at
22933          this point.  Lowercase letters are our problem.  For the
22934          second block of instructions, the secondary opcode nybble
22935          (bits 8..11) is present, and bit 23 is zero, even if this is
22936          a PC-relative operation.  */
22937       newval = md_chars_to_number (buf, THUMB_SIZE);
22938       newval <<= 16;
22939       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
22940
22941       if ((newval & 0xf0000000) == 0xe0000000)
22942         {
22943           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
22944           if (value >= 0)
22945             newval |= (1 << 23);
22946           else
22947             value = -value;
22948           if (value % 4 != 0)
22949             {
22950               as_bad_where (fixP->fx_file, fixP->fx_line,
22951                             _("offset not a multiple of 4"));
22952               break;
22953             }
22954           value /= 4;
22955           if (value > 0xff)
22956             {
22957               as_bad_where (fixP->fx_file, fixP->fx_line,
22958                             _("offset out of range"));
22959               break;
22960             }
22961           newval &= ~0xff;
22962         }
22963       else if ((newval & 0x000f0000) == 0x000f0000)
22964         {
22965           /* PC-relative, 12-bit offset.  */
22966           if (value >= 0)
22967             newval |= (1 << 23);
22968           else
22969             value = -value;
22970           if (value > 0xfff)
22971             {
22972               as_bad_where (fixP->fx_file, fixP->fx_line,
22973                             _("offset out of range"));
22974               break;
22975             }
22976           newval &= ~0xfff;
22977         }
22978       else if ((newval & 0x00000100) == 0x00000100)
22979         {
22980           /* Writeback: 8-bit, +/- offset.  */
22981           if (value >= 0)
22982             newval |= (1 << 9);
22983           else
22984             value = -value;
22985           if (value > 0xff)
22986             {
22987               as_bad_where (fixP->fx_file, fixP->fx_line,
22988                             _("offset out of range"));
22989               break;
22990             }
22991           newval &= ~0xff;
22992         }
22993       else if ((newval & 0x00000f00) == 0x00000e00)
22994         {
22995           /* T-instruction: positive 8-bit offset.  */
22996           if (value < 0 || value > 0xff)
22997             {
22998               as_bad_where (fixP->fx_file, fixP->fx_line,
22999                             _("offset out of range"));
23000               break;
23001             }
23002           newval &= ~0xff;
23003           newval |= value;
23004         }
23005       else
23006         {
23007           /* Positive 12-bit or negative 8-bit offset.  */
23008           int limit;
23009           if (value >= 0)
23010             {
23011               newval |= (1 << 23);
23012               limit = 0xfff;
23013             }
23014           else
23015             {
23016               value = -value;
23017               limit = 0xff;
23018             }
23019           if (value > limit)
23020             {
23021               as_bad_where (fixP->fx_file, fixP->fx_line,
23022                             _("offset out of range"));
23023               break;
23024             }
23025           newval &= ~limit;
23026         }
23027
23028       newval |= value;
23029       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
23030       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
23031       break;
23032
23033     case BFD_RELOC_ARM_SHIFT_IMM:
23034       newval = md_chars_to_number (buf, INSN_SIZE);
23035       if (((unsigned long) value) > 32
23036           || (value == 32
23037               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
23038         {
23039           as_bad_where (fixP->fx_file, fixP->fx_line,
23040                         _("shift expression is too large"));
23041           break;
23042         }
23043
23044       if (value == 0)
23045         /* Shifts of zero must be done as lsl.  */
23046         newval &= ~0x60;
23047       else if (value == 32)
23048         value = 0;
23049       newval &= 0xfffff07f;
23050       newval |= (value & 0x1f) << 7;
23051       md_number_to_chars (buf, newval, INSN_SIZE);
23052       break;
23053
23054     case BFD_RELOC_ARM_T32_IMMEDIATE:
23055     case BFD_RELOC_ARM_T32_ADD_IMM:
23056     case BFD_RELOC_ARM_T32_IMM12:
23057     case BFD_RELOC_ARM_T32_ADD_PC12:
23058       /* We claim that this fixup has been processed here,
23059          even if in fact we generate an error because we do
23060          not have a reloc for it, so tc_gen_reloc will reject it.  */
23061       fixP->fx_done = 1;
23062
23063       if (fixP->fx_addsy
23064           && ! S_IS_DEFINED (fixP->fx_addsy))
23065         {
23066           as_bad_where (fixP->fx_file, fixP->fx_line,
23067                         _("undefined symbol %s used as an immediate value"),
23068                         S_GET_NAME (fixP->fx_addsy));
23069           break;
23070         }
23071
23072       newval = md_chars_to_number (buf, THUMB_SIZE);
23073       newval <<= 16;
23074       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
23075
23076       newimm = FAIL;
23077       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23078           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23079         {
23080           newimm = encode_thumb32_immediate (value);
23081           if (newimm == (unsigned int) FAIL)
23082             newimm = thumb32_negate_data_op (&newval, value);
23083         }
23084       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
23085           && newimm == (unsigned int) FAIL)
23086         {
23087           /* Turn add/sum into addw/subw.  */
23088           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23089             newval = (newval & 0xfeffffff) | 0x02000000;
23090           /* No flat 12-bit imm encoding for addsw/subsw.  */
23091           if ((newval & 0x00100000) == 0)
23092             {
23093               /* 12 bit immediate for addw/subw.  */
23094               if (value < 0)
23095                 {
23096                   value = -value;
23097                   newval ^= 0x00a00000;
23098                 }
23099               if (value > 0xfff)
23100                 newimm = (unsigned int) FAIL;
23101               else
23102                 newimm = value;
23103             }
23104         }
23105
23106       if (newimm == (unsigned int)FAIL)
23107         {
23108           as_bad_where (fixP->fx_file, fixP->fx_line,
23109                         _("invalid constant (%lx) after fixup"),
23110                         (unsigned long) value);
23111           break;
23112         }
23113
23114       newval |= (newimm & 0x800) << 15;
23115       newval |= (newimm & 0x700) << 4;
23116       newval |= (newimm & 0x0ff);
23117
23118       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
23119       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
23120       break;
23121
23122     case BFD_RELOC_ARM_SMC:
23123       if (((unsigned long) value) > 0xffff)
23124         as_bad_where (fixP->fx_file, fixP->fx_line,
23125                       _("invalid smc expression"));
23126       newval = md_chars_to_number (buf, INSN_SIZE);
23127       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23128       md_number_to_chars (buf, newval, INSN_SIZE);
23129       break;
23130
23131     case BFD_RELOC_ARM_HVC:
23132       if (((unsigned long) value) > 0xffff)
23133         as_bad_where (fixP->fx_file, fixP->fx_line,
23134                       _("invalid hvc expression"));
23135       newval = md_chars_to_number (buf, INSN_SIZE);
23136       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23137       md_number_to_chars (buf, newval, INSN_SIZE);
23138       break;
23139
23140     case BFD_RELOC_ARM_SWI:
23141       if (fixP->tc_fix_data != 0)
23142         {
23143           if (((unsigned long) value) > 0xff)
23144             as_bad_where (fixP->fx_file, fixP->fx_line,
23145                           _("invalid swi expression"));
23146           newval = md_chars_to_number (buf, THUMB_SIZE);
23147           newval |= value;
23148           md_number_to_chars (buf, newval, THUMB_SIZE);
23149         }
23150       else
23151         {
23152           if (((unsigned long) value) > 0x00ffffff)
23153             as_bad_where (fixP->fx_file, fixP->fx_line,
23154                           _("invalid swi expression"));
23155           newval = md_chars_to_number (buf, INSN_SIZE);
23156           newval |= value;
23157           md_number_to_chars (buf, newval, INSN_SIZE);
23158         }
23159       break;
23160
23161     case BFD_RELOC_ARM_MULTI:
23162       if (((unsigned long) value) > 0xffff)
23163         as_bad_where (fixP->fx_file, fixP->fx_line,
23164                       _("invalid expression in load/store multiple"));
23165       newval = value | md_chars_to_number (buf, INSN_SIZE);
23166       md_number_to_chars (buf, newval, INSN_SIZE);
23167       break;
23168
23169 #ifdef OBJ_ELF
23170     case BFD_RELOC_ARM_PCREL_CALL:
23171
23172       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23173           && fixP->fx_addsy
23174           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23175           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23176           && THUMB_IS_FUNC (fixP->fx_addsy))
23177         /* Flip the bl to blx. This is a simple flip
23178            bit here because we generate PCREL_CALL for
23179            unconditional bls.  */
23180         {
23181           newval = md_chars_to_number (buf, INSN_SIZE);
23182           newval = newval | 0x10000000;
23183           md_number_to_chars (buf, newval, INSN_SIZE);
23184           temp = 1;
23185           fixP->fx_done = 1;
23186         }
23187       else
23188         temp = 3;
23189       goto arm_branch_common;
23190
23191     case BFD_RELOC_ARM_PCREL_JUMP:
23192       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23193           && fixP->fx_addsy
23194           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23195           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23196           && THUMB_IS_FUNC (fixP->fx_addsy))
23197         {
23198           /* This would map to a bl<cond>, b<cond>,
23199              b<always> to a Thumb function. We
23200              need to force a relocation for this particular
23201              case.  */
23202           newval = md_chars_to_number (buf, INSN_SIZE);
23203           fixP->fx_done = 0;
23204         }
23205       /* Fall through.  */
23206
23207     case BFD_RELOC_ARM_PLT32:
23208 #endif
23209     case BFD_RELOC_ARM_PCREL_BRANCH:
23210       temp = 3;
23211       goto arm_branch_common;
23212
23213     case BFD_RELOC_ARM_PCREL_BLX:
23214
23215       temp = 1;
23216       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23217           && fixP->fx_addsy
23218           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23219           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23220           && ARM_IS_FUNC (fixP->fx_addsy))
23221         {
23222           /* Flip the blx to a bl and warn.  */
23223           const char *name = S_GET_NAME (fixP->fx_addsy);
23224           newval = 0xeb000000;
23225           as_warn_where (fixP->fx_file, fixP->fx_line,
23226                          _("blx to '%s' an ARM ISA state function changed to bl"),
23227                           name);
23228           md_number_to_chars (buf, newval, INSN_SIZE);
23229           temp = 3;
23230           fixP->fx_done = 1;
23231         }
23232
23233 #ifdef OBJ_ELF
23234        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23235          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
23236 #endif
23237
23238     arm_branch_common:
23239       /* We are going to store value (shifted right by two) in the
23240          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
23241          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
23242          also be be clear.  */
23243       if (value & temp)
23244         as_bad_where (fixP->fx_file, fixP->fx_line,
23245                       _("misaligned branch destination"));
23246       if ((value & (offsetT)0xfe000000) != (offsetT)0
23247           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
23248         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23249
23250       if (fixP->fx_done || !seg->use_rela_p)
23251         {
23252           newval = md_chars_to_number (buf, INSN_SIZE);
23253           newval |= (value >> 2) & 0x00ffffff;
23254           /* Set the H bit on BLX instructions.  */
23255           if (temp == 1)
23256             {
23257               if (value & 2)
23258                 newval |= 0x01000000;
23259               else
23260                 newval &= ~0x01000000;
23261             }
23262           md_number_to_chars (buf, newval, INSN_SIZE);
23263         }
23264       break;
23265
23266     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
23267       /* CBZ can only branch forward.  */
23268
23269       /* Attempts to use CBZ to branch to the next instruction
23270          (which, strictly speaking, are prohibited) will be turned into
23271          no-ops.
23272
23273          FIXME: It may be better to remove the instruction completely and
23274          perform relaxation.  */
23275       if (value == -2)
23276         {
23277           newval = md_chars_to_number (buf, THUMB_SIZE);
23278           newval = 0xbf00; /* NOP encoding T1 */
23279           md_number_to_chars (buf, newval, THUMB_SIZE);
23280         }
23281       else
23282         {
23283           if (value & ~0x7e)
23284             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23285
23286           if (fixP->fx_done || !seg->use_rela_p)
23287             {
23288               newval = md_chars_to_number (buf, THUMB_SIZE);
23289               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
23290               md_number_to_chars (buf, newval, THUMB_SIZE);
23291             }
23292         }
23293       break;
23294
23295     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
23296       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
23297         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23298
23299       if (fixP->fx_done || !seg->use_rela_p)
23300         {
23301           newval = md_chars_to_number (buf, THUMB_SIZE);
23302           newval |= (value & 0x1ff) >> 1;
23303           md_number_to_chars (buf, newval, THUMB_SIZE);
23304         }
23305       break;
23306
23307     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
23308       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
23309         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23310
23311       if (fixP->fx_done || !seg->use_rela_p)
23312         {
23313           newval = md_chars_to_number (buf, THUMB_SIZE);
23314           newval |= (value & 0xfff) >> 1;
23315           md_number_to_chars (buf, newval, THUMB_SIZE);
23316         }
23317       break;
23318
23319     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23320       if (fixP->fx_addsy
23321           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23322           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23323           && ARM_IS_FUNC (fixP->fx_addsy)
23324           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23325         {
23326           /* Force a relocation for a branch 20 bits wide.  */
23327           fixP->fx_done = 0;
23328         }
23329       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
23330         as_bad_where (fixP->fx_file, fixP->fx_line,
23331                       _("conditional branch out of range"));
23332
23333       if (fixP->fx_done || !seg->use_rela_p)
23334         {
23335           offsetT newval2;
23336           addressT S, J1, J2, lo, hi;
23337
23338           S  = (value & 0x00100000) >> 20;
23339           J2 = (value & 0x00080000) >> 19;
23340           J1 = (value & 0x00040000) >> 18;
23341           hi = (value & 0x0003f000) >> 12;
23342           lo = (value & 0x00000ffe) >> 1;
23343
23344           newval   = md_chars_to_number (buf, THUMB_SIZE);
23345           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23346           newval  |= (S << 10) | hi;
23347           newval2 |= (J1 << 13) | (J2 << 11) | lo;
23348           md_number_to_chars (buf, newval, THUMB_SIZE);
23349           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23350         }
23351       break;
23352
23353     case BFD_RELOC_THUMB_PCREL_BLX:
23354       /* If there is a blx from a thumb state function to
23355          another thumb function flip this to a bl and warn
23356          about it.  */
23357
23358       if (fixP->fx_addsy
23359           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23360           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23361           && THUMB_IS_FUNC (fixP->fx_addsy))
23362         {
23363           const char *name = S_GET_NAME (fixP->fx_addsy);
23364           as_warn_where (fixP->fx_file, fixP->fx_line,
23365                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
23366                          name);
23367           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23368           newval = newval | 0x1000;
23369           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23370           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23371           fixP->fx_done = 1;
23372         }
23373
23374
23375       goto thumb_bl_common;
23376
23377     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23378       /* A bl from Thumb state ISA to an internal ARM state function
23379          is converted to a blx.  */
23380       if (fixP->fx_addsy
23381           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23382           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23383           && ARM_IS_FUNC (fixP->fx_addsy)
23384           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23385         {
23386           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23387           newval = newval & ~0x1000;
23388           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23389           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
23390           fixP->fx_done = 1;
23391         }
23392
23393     thumb_bl_common:
23394
23395       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23396         /* For a BLX instruction, make sure that the relocation is rounded up
23397            to a word boundary.  This follows the semantics of the instruction
23398            which specifies that bit 1 of the target address will come from bit
23399            1 of the base address.  */
23400         value = (value + 3) & ~ 3;
23401
23402 #ifdef OBJ_ELF
23403        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
23404            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23405          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23406 #endif
23407
23408       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
23409         {
23410           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
23411             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23412           else if ((value & ~0x1ffffff)
23413                    && ((value & ~0x1ffffff) != ~0x1ffffff))
23414             as_bad_where (fixP->fx_file, fixP->fx_line,
23415                           _("Thumb2 branch out of range"));
23416         }
23417
23418       if (fixP->fx_done || !seg->use_rela_p)
23419         encode_thumb2_b_bl_offset (buf, value);
23420
23421       break;
23422
23423     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23424       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
23425         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23426
23427       if (fixP->fx_done || !seg->use_rela_p)
23428           encode_thumb2_b_bl_offset (buf, value);
23429
23430       break;
23431
23432     case BFD_RELOC_8:
23433       if (fixP->fx_done || !seg->use_rela_p)
23434         *buf = value;
23435       break;
23436
23437     case BFD_RELOC_16:
23438       if (fixP->fx_done || !seg->use_rela_p)
23439         md_number_to_chars (buf, value, 2);
23440       break;
23441
23442 #ifdef OBJ_ELF
23443     case BFD_RELOC_ARM_TLS_CALL:
23444     case BFD_RELOC_ARM_THM_TLS_CALL:
23445     case BFD_RELOC_ARM_TLS_DESCSEQ:
23446     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
23447     case BFD_RELOC_ARM_TLS_GOTDESC:
23448     case BFD_RELOC_ARM_TLS_GD32:
23449     case BFD_RELOC_ARM_TLS_LE32:
23450     case BFD_RELOC_ARM_TLS_IE32:
23451     case BFD_RELOC_ARM_TLS_LDM32:
23452     case BFD_RELOC_ARM_TLS_LDO32:
23453       S_SET_THREAD_LOCAL (fixP->fx_addsy);
23454       break;
23455
23456     case BFD_RELOC_ARM_GOT32:
23457     case BFD_RELOC_ARM_GOTOFF:
23458       break;
23459
23460     case BFD_RELOC_ARM_GOT_PREL:
23461       if (fixP->fx_done || !seg->use_rela_p)
23462         md_number_to_chars (buf, value, 4);
23463       break;
23464
23465     case BFD_RELOC_ARM_TARGET2:
23466       /* TARGET2 is not partial-inplace, so we need to write the
23467          addend here for REL targets, because it won't be written out
23468          during reloc processing later.  */
23469       if (fixP->fx_done || !seg->use_rela_p)
23470         md_number_to_chars (buf, fixP->fx_offset, 4);
23471       break;
23472 #endif
23473
23474     case BFD_RELOC_RVA:
23475     case BFD_RELOC_32:
23476     case BFD_RELOC_ARM_TARGET1:
23477     case BFD_RELOC_ARM_ROSEGREL32:
23478     case BFD_RELOC_ARM_SBREL32:
23479     case BFD_RELOC_32_PCREL:
23480 #ifdef TE_PE
23481     case BFD_RELOC_32_SECREL:
23482 #endif
23483       if (fixP->fx_done || !seg->use_rela_p)
23484 #ifdef TE_WINCE
23485         /* For WinCE we only do this for pcrel fixups.  */
23486         if (fixP->fx_done || fixP->fx_pcrel)
23487 #endif
23488           md_number_to_chars (buf, value, 4);
23489       break;
23490
23491 #ifdef OBJ_ELF
23492     case BFD_RELOC_ARM_PREL31:
23493       if (fixP->fx_done || !seg->use_rela_p)
23494         {
23495           newval = md_chars_to_number (buf, 4) & 0x80000000;
23496           if ((value ^ (value >> 1)) & 0x40000000)
23497             {
23498               as_bad_where (fixP->fx_file, fixP->fx_line,
23499                             _("rel31 relocation overflow"));
23500             }
23501           newval |= value & 0x7fffffff;
23502           md_number_to_chars (buf, newval, 4);
23503         }
23504       break;
23505 #endif
23506
23507     case BFD_RELOC_ARM_CP_OFF_IMM:
23508     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
23509       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
23510         newval = md_chars_to_number (buf, INSN_SIZE);
23511       else
23512         newval = get_thumb32_insn (buf);
23513       if ((newval & 0x0f200f00) == 0x0d000900)
23514         {
23515           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
23516              has permitted values that are multiples of 2, in the range 0
23517              to 510.  */
23518           if (value < -510 || value > 510 || (value & 1))
23519             as_bad_where (fixP->fx_file, fixP->fx_line,
23520                           _("co-processor offset out of range"));
23521         }
23522       else if (value < -1023 || value > 1023 || (value & 3))
23523         as_bad_where (fixP->fx_file, fixP->fx_line,
23524                       _("co-processor offset out of range"));
23525     cp_off_common:
23526       sign = value > 0;
23527       if (value < 0)
23528         value = -value;
23529       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23530           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
23531         newval = md_chars_to_number (buf, INSN_SIZE);
23532       else
23533         newval = get_thumb32_insn (buf);
23534       if (value == 0)
23535         newval &= 0xffffff00;
23536       else
23537         {
23538           newval &= 0xff7fff00;
23539           if ((newval & 0x0f200f00) == 0x0d000900)
23540             {
23541               /* This is a fp16 vstr/vldr.
23542
23543                  It requires the immediate offset in the instruction is shifted
23544                  left by 1 to be a half-word offset.
23545
23546                  Here, left shift by 1 first, and later right shift by 2
23547                  should get the right offset.  */
23548               value <<= 1;
23549             }
23550           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
23551         }
23552       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23553           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
23554         md_number_to_chars (buf, newval, INSN_SIZE);
23555       else
23556         put_thumb32_insn (buf, newval);
23557       break;
23558
23559     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
23560     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
23561       if (value < -255 || value > 255)
23562         as_bad_where (fixP->fx_file, fixP->fx_line,
23563                       _("co-processor offset out of range"));
23564       value *= 4;
23565       goto cp_off_common;
23566
23567     case BFD_RELOC_ARM_THUMB_OFFSET:
23568       newval = md_chars_to_number (buf, THUMB_SIZE);
23569       /* Exactly what ranges, and where the offset is inserted depends
23570          on the type of instruction, we can establish this from the
23571          top 4 bits.  */
23572       switch (newval >> 12)
23573         {
23574         case 4: /* PC load.  */
23575           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
23576              forced to zero for these loads; md_pcrel_from has already
23577              compensated for this.  */
23578           if (value & 3)
23579             as_bad_where (fixP->fx_file, fixP->fx_line,
23580                           _("invalid offset, target not word aligned (0x%08lX)"),
23581                           (((unsigned long) fixP->fx_frag->fr_address
23582                             + (unsigned long) fixP->fx_where) & ~3)
23583                           + (unsigned long) value);
23584
23585           if (value & ~0x3fc)
23586             as_bad_where (fixP->fx_file, fixP->fx_line,
23587                           _("invalid offset, value too big (0x%08lX)"),
23588                           (long) value);
23589
23590           newval |= value >> 2;
23591           break;
23592
23593         case 9: /* SP load/store.  */
23594           if (value & ~0x3fc)
23595             as_bad_where (fixP->fx_file, fixP->fx_line,
23596                           _("invalid offset, value too big (0x%08lX)"),
23597                           (long) value);
23598           newval |= value >> 2;
23599           break;
23600
23601         case 6: /* Word load/store.  */
23602           if (value & ~0x7c)
23603             as_bad_where (fixP->fx_file, fixP->fx_line,
23604                           _("invalid offset, value too big (0x%08lX)"),
23605                           (long) value);
23606           newval |= value << 4; /* 6 - 2.  */
23607           break;
23608
23609         case 7: /* Byte load/store.  */
23610           if (value & ~0x1f)
23611             as_bad_where (fixP->fx_file, fixP->fx_line,
23612                           _("invalid offset, value too big (0x%08lX)"),
23613                           (long) value);
23614           newval |= value << 6;
23615           break;
23616
23617         case 8: /* Halfword load/store.  */
23618           if (value & ~0x3e)
23619             as_bad_where (fixP->fx_file, fixP->fx_line,
23620                           _("invalid offset, value too big (0x%08lX)"),
23621                           (long) value);
23622           newval |= value << 5; /* 6 - 1.  */
23623           break;
23624
23625         default:
23626           as_bad_where (fixP->fx_file, fixP->fx_line,
23627                         "Unable to process relocation for thumb opcode: %lx",
23628                         (unsigned long) newval);
23629           break;
23630         }
23631       md_number_to_chars (buf, newval, THUMB_SIZE);
23632       break;
23633
23634     case BFD_RELOC_ARM_THUMB_ADD:
23635       /* This is a complicated relocation, since we use it for all of
23636          the following immediate relocations:
23637
23638             3bit ADD/SUB
23639             8bit ADD/SUB
23640             9bit ADD/SUB SP word-aligned
23641            10bit ADD PC/SP word-aligned
23642
23643          The type of instruction being processed is encoded in the
23644          instruction field:
23645
23646            0x8000  SUB
23647            0x00F0  Rd
23648            0x000F  Rs
23649       */
23650       newval = md_chars_to_number (buf, THUMB_SIZE);
23651       {
23652         int rd = (newval >> 4) & 0xf;
23653         int rs = newval & 0xf;
23654         int subtract = !!(newval & 0x8000);
23655
23656         /* Check for HI regs, only very restricted cases allowed:
23657            Adjusting SP, and using PC or SP to get an address.  */
23658         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
23659             || (rs > 7 && rs != REG_SP && rs != REG_PC))
23660           as_bad_where (fixP->fx_file, fixP->fx_line,
23661                         _("invalid Hi register with immediate"));
23662
23663         /* If value is negative, choose the opposite instruction.  */
23664         if (value < 0)
23665           {
23666             value = -value;
23667             subtract = !subtract;
23668             if (value < 0)
23669               as_bad_where (fixP->fx_file, fixP->fx_line,
23670                             _("immediate value out of range"));
23671           }
23672
23673         if (rd == REG_SP)
23674           {
23675             if (value & ~0x1fc)
23676               as_bad_where (fixP->fx_file, fixP->fx_line,
23677                             _("invalid immediate for stack address calculation"));
23678             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
23679             newval |= value >> 2;
23680           }
23681         else if (rs == REG_PC || rs == REG_SP)
23682           {
23683             /* PR gas/18541.  If the addition is for a defined symbol
23684                within range of an ADR instruction then accept it.  */
23685             if (subtract
23686                 && value == 4
23687                 && fixP->fx_addsy != NULL)
23688               {
23689                 subtract = 0;
23690
23691                 if (! S_IS_DEFINED (fixP->fx_addsy)
23692                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
23693                     || S_IS_WEAK (fixP->fx_addsy))
23694                   {
23695                     as_bad_where (fixP->fx_file, fixP->fx_line,
23696                                   _("address calculation needs a strongly defined nearby symbol"));
23697                   }
23698                 else
23699                   {
23700                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
23701
23702                     /* Round up to the next 4-byte boundary.  */
23703                     if (v & 3)
23704                       v = (v + 3) & ~ 3;
23705                     else
23706                       v += 4;
23707                     v = S_GET_VALUE (fixP->fx_addsy) - v;
23708
23709                     if (v & ~0x3fc)
23710                       {
23711                         as_bad_where (fixP->fx_file, fixP->fx_line,
23712                                       _("symbol too far away"));
23713                       }
23714                     else
23715                       {
23716                         fixP->fx_done = 1;
23717                         value = v;
23718                       }
23719                   }
23720               }
23721
23722             if (subtract || value & ~0x3fc)
23723               as_bad_where (fixP->fx_file, fixP->fx_line,
23724                             _("invalid immediate for address calculation (value = 0x%08lX)"),
23725                             (unsigned long) (subtract ? - value : value));
23726             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
23727             newval |= rd << 8;
23728             newval |= value >> 2;
23729           }
23730         else if (rs == rd)
23731           {
23732             if (value & ~0xff)
23733               as_bad_where (fixP->fx_file, fixP->fx_line,
23734                             _("immediate value out of range"));
23735             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
23736             newval |= (rd << 8) | value;
23737           }
23738         else
23739           {
23740             if (value & ~0x7)
23741               as_bad_where (fixP->fx_file, fixP->fx_line,
23742                             _("immediate value out of range"));
23743             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
23744             newval |= rd | (rs << 3) | (value << 6);
23745           }
23746       }
23747       md_number_to_chars (buf, newval, THUMB_SIZE);
23748       break;
23749
23750     case BFD_RELOC_ARM_THUMB_IMM:
23751       newval = md_chars_to_number (buf, THUMB_SIZE);
23752       if (value < 0 || value > 255)
23753         as_bad_where (fixP->fx_file, fixP->fx_line,
23754                       _("invalid immediate: %ld is out of range"),
23755                       (long) value);
23756       newval |= value;
23757       md_number_to_chars (buf, newval, THUMB_SIZE);
23758       break;
23759
23760     case BFD_RELOC_ARM_THUMB_SHIFT:
23761       /* 5bit shift value (0..32).  LSL cannot take 32.  */
23762       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
23763       temp = newval & 0xf800;
23764       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
23765         as_bad_where (fixP->fx_file, fixP->fx_line,
23766                       _("invalid shift value: %ld"), (long) value);
23767       /* Shifts of zero must be encoded as LSL.  */
23768       if (value == 0)
23769         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
23770       /* Shifts of 32 are encoded as zero.  */
23771       else if (value == 32)
23772         value = 0;
23773       newval |= value << 6;
23774       md_number_to_chars (buf, newval, THUMB_SIZE);
23775       break;
23776
23777     case BFD_RELOC_VTABLE_INHERIT:
23778     case BFD_RELOC_VTABLE_ENTRY:
23779       fixP->fx_done = 0;
23780       return;
23781
23782     case BFD_RELOC_ARM_MOVW:
23783     case BFD_RELOC_ARM_MOVT:
23784     case BFD_RELOC_ARM_THUMB_MOVW:
23785     case BFD_RELOC_ARM_THUMB_MOVT:
23786       if (fixP->fx_done || !seg->use_rela_p)
23787         {
23788           /* REL format relocations are limited to a 16-bit addend.  */
23789           if (!fixP->fx_done)
23790             {
23791               if (value < -0x8000 || value > 0x7fff)
23792                   as_bad_where (fixP->fx_file, fixP->fx_line,
23793                                 _("offset out of range"));
23794             }
23795           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23796                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23797             {
23798               value >>= 16;
23799             }
23800
23801           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23802               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23803             {
23804               newval = get_thumb32_insn (buf);
23805               newval &= 0xfbf08f00;
23806               newval |= (value & 0xf000) << 4;
23807               newval |= (value & 0x0800) << 15;
23808               newval |= (value & 0x0700) << 4;
23809               newval |= (value & 0x00ff);
23810               put_thumb32_insn (buf, newval);
23811             }
23812           else
23813             {
23814               newval = md_chars_to_number (buf, 4);
23815               newval &= 0xfff0f000;
23816               newval |= value & 0x0fff;
23817               newval |= (value & 0xf000) << 4;
23818               md_number_to_chars (buf, newval, 4);
23819             }
23820         }
23821       return;
23822
23823    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
23824    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
23825    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
23826    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
23827       gas_assert (!fixP->fx_done);
23828       {
23829         bfd_vma insn;
23830         bfd_boolean is_mov;
23831         bfd_vma encoded_addend = value;
23832
23833         /* Check that addend can be encoded in instruction.  */
23834         if (!seg->use_rela_p && (value < 0 || value > 255))
23835           as_bad_where (fixP->fx_file, fixP->fx_line,
23836                         _("the offset 0x%08lX is not representable"),
23837                         (unsigned long) encoded_addend);
23838
23839         /* Extract the instruction.  */
23840         insn = md_chars_to_number (buf, THUMB_SIZE);
23841         is_mov = (insn & 0xf800) == 0x2000;
23842
23843         /* Encode insn.  */
23844         if (is_mov)
23845           {
23846             if (!seg->use_rela_p)
23847               insn |= encoded_addend;
23848           }
23849         else
23850           {
23851             int rd, rs;
23852
23853             /* Extract the instruction.  */
23854              /* Encoding is the following
23855                 0x8000  SUB
23856                 0x00F0  Rd
23857                 0x000F  Rs
23858              */
23859              /* The following conditions must be true :
23860                 - ADD
23861                 - Rd == Rs
23862                 - Rd <= 7
23863              */
23864             rd = (insn >> 4) & 0xf;
23865             rs = insn & 0xf;
23866             if ((insn & 0x8000) || (rd != rs) || rd > 7)
23867               as_bad_where (fixP->fx_file, fixP->fx_line,
23868                         _("Unable to process relocation for thumb opcode: %lx"),
23869                         (unsigned long) insn);
23870
23871             /* Encode as ADD immediate8 thumb 1 code.  */
23872             insn = 0x3000 | (rd << 8);
23873
23874             /* Place the encoded addend into the first 8 bits of the
23875                instruction.  */
23876             if (!seg->use_rela_p)
23877               insn |= encoded_addend;
23878           }
23879
23880         /* Update the instruction.  */
23881         md_number_to_chars (buf, insn, THUMB_SIZE);
23882       }
23883       break;
23884
23885    case BFD_RELOC_ARM_ALU_PC_G0_NC:
23886    case BFD_RELOC_ARM_ALU_PC_G0:
23887    case BFD_RELOC_ARM_ALU_PC_G1_NC:
23888    case BFD_RELOC_ARM_ALU_PC_G1:
23889    case BFD_RELOC_ARM_ALU_PC_G2:
23890    case BFD_RELOC_ARM_ALU_SB_G0_NC:
23891    case BFD_RELOC_ARM_ALU_SB_G0:
23892    case BFD_RELOC_ARM_ALU_SB_G1_NC:
23893    case BFD_RELOC_ARM_ALU_SB_G1:
23894    case BFD_RELOC_ARM_ALU_SB_G2:
23895      gas_assert (!fixP->fx_done);
23896      if (!seg->use_rela_p)
23897        {
23898          bfd_vma insn;
23899          bfd_vma encoded_addend;
23900          bfd_vma addend_abs = abs (value);
23901
23902          /* Check that the absolute value of the addend can be
23903             expressed as an 8-bit constant plus a rotation.  */
23904          encoded_addend = encode_arm_immediate (addend_abs);
23905          if (encoded_addend == (unsigned int) FAIL)
23906            as_bad_where (fixP->fx_file, fixP->fx_line,
23907                          _("the offset 0x%08lX is not representable"),
23908                          (unsigned long) addend_abs);
23909
23910          /* Extract the instruction.  */
23911          insn = md_chars_to_number (buf, INSN_SIZE);
23912
23913          /* If the addend is positive, use an ADD instruction.
23914             Otherwise use a SUB.  Take care not to destroy the S bit.  */
23915          insn &= 0xff1fffff;
23916          if (value < 0)
23917            insn |= 1 << 22;
23918          else
23919            insn |= 1 << 23;
23920
23921          /* Place the encoded addend into the first 12 bits of the
23922             instruction.  */
23923          insn &= 0xfffff000;
23924          insn |= encoded_addend;
23925
23926          /* Update the instruction.  */
23927          md_number_to_chars (buf, insn, INSN_SIZE);
23928        }
23929      break;
23930
23931     case BFD_RELOC_ARM_LDR_PC_G0:
23932     case BFD_RELOC_ARM_LDR_PC_G1:
23933     case BFD_RELOC_ARM_LDR_PC_G2:
23934     case BFD_RELOC_ARM_LDR_SB_G0:
23935     case BFD_RELOC_ARM_LDR_SB_G1:
23936     case BFD_RELOC_ARM_LDR_SB_G2:
23937       gas_assert (!fixP->fx_done);
23938       if (!seg->use_rela_p)
23939         {
23940           bfd_vma insn;
23941           bfd_vma addend_abs = abs (value);
23942
23943           /* Check that the absolute value of the addend can be
23944              encoded in 12 bits.  */
23945           if (addend_abs >= 0x1000)
23946             as_bad_where (fixP->fx_file, fixP->fx_line,
23947                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
23948                           (unsigned long) addend_abs);
23949
23950           /* Extract the instruction.  */
23951           insn = md_chars_to_number (buf, INSN_SIZE);
23952
23953           /* If the addend is negative, clear bit 23 of the instruction.
23954              Otherwise set it.  */
23955           if (value < 0)
23956             insn &= ~(1 << 23);
23957           else
23958             insn |= 1 << 23;
23959
23960           /* Place the absolute value of the addend into the first 12 bits
23961              of the instruction.  */
23962           insn &= 0xfffff000;
23963           insn |= addend_abs;
23964
23965           /* Update the instruction.  */
23966           md_number_to_chars (buf, insn, INSN_SIZE);
23967         }
23968       break;
23969
23970     case BFD_RELOC_ARM_LDRS_PC_G0:
23971     case BFD_RELOC_ARM_LDRS_PC_G1:
23972     case BFD_RELOC_ARM_LDRS_PC_G2:
23973     case BFD_RELOC_ARM_LDRS_SB_G0:
23974     case BFD_RELOC_ARM_LDRS_SB_G1:
23975     case BFD_RELOC_ARM_LDRS_SB_G2:
23976       gas_assert (!fixP->fx_done);
23977       if (!seg->use_rela_p)
23978         {
23979           bfd_vma insn;
23980           bfd_vma addend_abs = abs (value);
23981
23982           /* Check that the absolute value of the addend can be
23983              encoded in 8 bits.  */
23984           if (addend_abs >= 0x100)
23985             as_bad_where (fixP->fx_file, fixP->fx_line,
23986                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
23987                           (unsigned long) addend_abs);
23988
23989           /* Extract the instruction.  */
23990           insn = md_chars_to_number (buf, INSN_SIZE);
23991
23992           /* If the addend is negative, clear bit 23 of the instruction.
23993              Otherwise set it.  */
23994           if (value < 0)
23995             insn &= ~(1 << 23);
23996           else
23997             insn |= 1 << 23;
23998
23999           /* Place the first four bits of the absolute value of the addend
24000              into the first 4 bits of the instruction, and the remaining
24001              four into bits 8 .. 11.  */
24002           insn &= 0xfffff0f0;
24003           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
24004
24005           /* Update the instruction.  */
24006           md_number_to_chars (buf, insn, INSN_SIZE);
24007         }
24008       break;
24009
24010     case BFD_RELOC_ARM_LDC_PC_G0:
24011     case BFD_RELOC_ARM_LDC_PC_G1:
24012     case BFD_RELOC_ARM_LDC_PC_G2:
24013     case BFD_RELOC_ARM_LDC_SB_G0:
24014     case BFD_RELOC_ARM_LDC_SB_G1:
24015     case BFD_RELOC_ARM_LDC_SB_G2:
24016       gas_assert (!fixP->fx_done);
24017       if (!seg->use_rela_p)
24018         {
24019           bfd_vma insn;
24020           bfd_vma addend_abs = abs (value);
24021
24022           /* Check that the absolute value of the addend is a multiple of
24023              four and, when divided by four, fits in 8 bits.  */
24024           if (addend_abs & 0x3)
24025             as_bad_where (fixP->fx_file, fixP->fx_line,
24026                           _("bad offset 0x%08lX (must be word-aligned)"),
24027                           (unsigned long) addend_abs);
24028
24029           if ((addend_abs >> 2) > 0xff)
24030             as_bad_where (fixP->fx_file, fixP->fx_line,
24031                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
24032                           (unsigned long) addend_abs);
24033
24034           /* Extract the instruction.  */
24035           insn = md_chars_to_number (buf, INSN_SIZE);
24036
24037           /* If the addend is negative, clear bit 23 of the instruction.
24038              Otherwise set it.  */
24039           if (value < 0)
24040             insn &= ~(1 << 23);
24041           else
24042             insn |= 1 << 23;
24043
24044           /* Place the addend (divided by four) into the first eight
24045              bits of the instruction.  */
24046           insn &= 0xfffffff0;
24047           insn |= addend_abs >> 2;
24048
24049           /* Update the instruction.  */
24050           md_number_to_chars (buf, insn, INSN_SIZE);
24051         }
24052       break;
24053
24054     case BFD_RELOC_ARM_V4BX:
24055       /* This will need to go in the object file.  */
24056       fixP->fx_done = 0;
24057       break;
24058
24059     case BFD_RELOC_UNUSED:
24060     default:
24061       as_bad_where (fixP->fx_file, fixP->fx_line,
24062                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
24063     }
24064 }
24065
24066 /* Translate internal representation of relocation info to BFD target
24067    format.  */
24068
24069 arelent *
24070 tc_gen_reloc (asection *section, fixS *fixp)
24071 {
24072   arelent * reloc;
24073   bfd_reloc_code_real_type code;
24074
24075   reloc = XNEW (arelent);
24076
24077   reloc->sym_ptr_ptr = XNEW (asymbol *);
24078   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
24079   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
24080
24081   if (fixp->fx_pcrel)
24082     {
24083       if (section->use_rela_p)
24084         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
24085       else
24086         fixp->fx_offset = reloc->address;
24087     }
24088   reloc->addend = fixp->fx_offset;
24089
24090   switch (fixp->fx_r_type)
24091     {
24092     case BFD_RELOC_8:
24093       if (fixp->fx_pcrel)
24094         {
24095           code = BFD_RELOC_8_PCREL;
24096           break;
24097         }
24098       /* Fall through.  */
24099
24100     case BFD_RELOC_16:
24101       if (fixp->fx_pcrel)
24102         {
24103           code = BFD_RELOC_16_PCREL;
24104           break;
24105         }
24106       /* Fall through.  */
24107
24108     case BFD_RELOC_32:
24109       if (fixp->fx_pcrel)
24110         {
24111           code = BFD_RELOC_32_PCREL;
24112           break;
24113         }
24114       /* Fall through.  */
24115
24116     case BFD_RELOC_ARM_MOVW:
24117       if (fixp->fx_pcrel)
24118         {
24119           code = BFD_RELOC_ARM_MOVW_PCREL;
24120           break;
24121         }
24122       /* Fall through.  */
24123
24124     case BFD_RELOC_ARM_MOVT:
24125       if (fixp->fx_pcrel)
24126         {
24127           code = BFD_RELOC_ARM_MOVT_PCREL;
24128           break;
24129         }
24130       /* Fall through.  */
24131
24132     case BFD_RELOC_ARM_THUMB_MOVW:
24133       if (fixp->fx_pcrel)
24134         {
24135           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
24136           break;
24137         }
24138       /* Fall through.  */
24139
24140     case BFD_RELOC_ARM_THUMB_MOVT:
24141       if (fixp->fx_pcrel)
24142         {
24143           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
24144           break;
24145         }
24146       /* Fall through.  */
24147
24148     case BFD_RELOC_NONE:
24149     case BFD_RELOC_ARM_PCREL_BRANCH:
24150     case BFD_RELOC_ARM_PCREL_BLX:
24151     case BFD_RELOC_RVA:
24152     case BFD_RELOC_THUMB_PCREL_BRANCH7:
24153     case BFD_RELOC_THUMB_PCREL_BRANCH9:
24154     case BFD_RELOC_THUMB_PCREL_BRANCH12:
24155     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24156     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24157     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24158     case BFD_RELOC_VTABLE_ENTRY:
24159     case BFD_RELOC_VTABLE_INHERIT:
24160 #ifdef TE_PE
24161     case BFD_RELOC_32_SECREL:
24162 #endif
24163       code = fixp->fx_r_type;
24164       break;
24165
24166     case BFD_RELOC_THUMB_PCREL_BLX:
24167 #ifdef OBJ_ELF
24168       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
24169         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
24170       else
24171 #endif
24172         code = BFD_RELOC_THUMB_PCREL_BLX;
24173       break;
24174
24175     case BFD_RELOC_ARM_LITERAL:
24176     case BFD_RELOC_ARM_HWLITERAL:
24177       /* If this is called then the a literal has
24178          been referenced across a section boundary.  */
24179       as_bad_where (fixp->fx_file, fixp->fx_line,
24180                     _("literal referenced across section boundary"));
24181       return NULL;
24182
24183 #ifdef OBJ_ELF
24184     case BFD_RELOC_ARM_TLS_CALL:
24185     case BFD_RELOC_ARM_THM_TLS_CALL:
24186     case BFD_RELOC_ARM_TLS_DESCSEQ:
24187     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
24188     case BFD_RELOC_ARM_GOT32:
24189     case BFD_RELOC_ARM_GOTOFF:
24190     case BFD_RELOC_ARM_GOT_PREL:
24191     case BFD_RELOC_ARM_PLT32:
24192     case BFD_RELOC_ARM_TARGET1:
24193     case BFD_RELOC_ARM_ROSEGREL32:
24194     case BFD_RELOC_ARM_SBREL32:
24195     case BFD_RELOC_ARM_PREL31:
24196     case BFD_RELOC_ARM_TARGET2:
24197     case BFD_RELOC_ARM_TLS_LDO32:
24198     case BFD_RELOC_ARM_PCREL_CALL:
24199     case BFD_RELOC_ARM_PCREL_JUMP:
24200     case BFD_RELOC_ARM_ALU_PC_G0_NC:
24201     case BFD_RELOC_ARM_ALU_PC_G0:
24202     case BFD_RELOC_ARM_ALU_PC_G1_NC:
24203     case BFD_RELOC_ARM_ALU_PC_G1:
24204     case BFD_RELOC_ARM_ALU_PC_G2:
24205     case BFD_RELOC_ARM_LDR_PC_G0:
24206     case BFD_RELOC_ARM_LDR_PC_G1:
24207     case BFD_RELOC_ARM_LDR_PC_G2:
24208     case BFD_RELOC_ARM_LDRS_PC_G0:
24209     case BFD_RELOC_ARM_LDRS_PC_G1:
24210     case BFD_RELOC_ARM_LDRS_PC_G2:
24211     case BFD_RELOC_ARM_LDC_PC_G0:
24212     case BFD_RELOC_ARM_LDC_PC_G1:
24213     case BFD_RELOC_ARM_LDC_PC_G2:
24214     case BFD_RELOC_ARM_ALU_SB_G0_NC:
24215     case BFD_RELOC_ARM_ALU_SB_G0:
24216     case BFD_RELOC_ARM_ALU_SB_G1_NC:
24217     case BFD_RELOC_ARM_ALU_SB_G1:
24218     case BFD_RELOC_ARM_ALU_SB_G2:
24219     case BFD_RELOC_ARM_LDR_SB_G0:
24220     case BFD_RELOC_ARM_LDR_SB_G1:
24221     case BFD_RELOC_ARM_LDR_SB_G2:
24222     case BFD_RELOC_ARM_LDRS_SB_G0:
24223     case BFD_RELOC_ARM_LDRS_SB_G1:
24224     case BFD_RELOC_ARM_LDRS_SB_G2:
24225     case BFD_RELOC_ARM_LDC_SB_G0:
24226     case BFD_RELOC_ARM_LDC_SB_G1:
24227     case BFD_RELOC_ARM_LDC_SB_G2:
24228     case BFD_RELOC_ARM_V4BX:
24229     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24230     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24231     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24232     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24233       code = fixp->fx_r_type;
24234       break;
24235
24236     case BFD_RELOC_ARM_TLS_GOTDESC:
24237     case BFD_RELOC_ARM_TLS_GD32:
24238     case BFD_RELOC_ARM_TLS_LE32:
24239     case BFD_RELOC_ARM_TLS_IE32:
24240     case BFD_RELOC_ARM_TLS_LDM32:
24241       /* BFD will include the symbol's address in the addend.
24242          But we don't want that, so subtract it out again here.  */
24243       if (!S_IS_COMMON (fixp->fx_addsy))
24244         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
24245       code = fixp->fx_r_type;
24246       break;
24247 #endif
24248
24249     case BFD_RELOC_ARM_IMMEDIATE:
24250       as_bad_where (fixp->fx_file, fixp->fx_line,
24251                     _("internal relocation (type: IMMEDIATE) not fixed up"));
24252       return NULL;
24253
24254     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
24255       as_bad_where (fixp->fx_file, fixp->fx_line,
24256                     _("ADRL used for a symbol not defined in the same file"));
24257       return NULL;
24258
24259     case BFD_RELOC_ARM_OFFSET_IMM:
24260       if (section->use_rela_p)
24261         {
24262           code = fixp->fx_r_type;
24263           break;
24264         }
24265
24266       if (fixp->fx_addsy != NULL
24267           && !S_IS_DEFINED (fixp->fx_addsy)
24268           && S_IS_LOCAL (fixp->fx_addsy))
24269         {
24270           as_bad_where (fixp->fx_file, fixp->fx_line,
24271                         _("undefined local label `%s'"),
24272                         S_GET_NAME (fixp->fx_addsy));
24273           return NULL;
24274         }
24275
24276       as_bad_where (fixp->fx_file, fixp->fx_line,
24277                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
24278       return NULL;
24279
24280     default:
24281       {
24282         const char * type;
24283
24284         switch (fixp->fx_r_type)
24285           {
24286           case BFD_RELOC_NONE:             type = "NONE";         break;
24287           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
24288           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
24289           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
24290           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
24291           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
24292           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
24293           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
24294           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
24295           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
24296           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
24297           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
24298           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
24299           default:                         type = _("<unknown>"); break;
24300           }
24301         as_bad_where (fixp->fx_file, fixp->fx_line,
24302                       _("cannot represent %s relocation in this object file format"),
24303                       type);
24304         return NULL;
24305       }
24306     }
24307
24308 #ifdef OBJ_ELF
24309   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
24310       && GOT_symbol
24311       && fixp->fx_addsy == GOT_symbol)
24312     {
24313       code = BFD_RELOC_ARM_GOTPC;
24314       reloc->addend = fixp->fx_offset = reloc->address;
24315     }
24316 #endif
24317
24318   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
24319
24320   if (reloc->howto == NULL)
24321     {
24322       as_bad_where (fixp->fx_file, fixp->fx_line,
24323                     _("cannot represent %s relocation in this object file format"),
24324                     bfd_get_reloc_code_name (code));
24325       return NULL;
24326     }
24327
24328   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
24329      vtable entry to be used in the relocation's section offset.  */
24330   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24331     reloc->address = fixp->fx_offset;
24332
24333   return reloc;
24334 }
24335
24336 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
24337
24338 void
24339 cons_fix_new_arm (fragS *       frag,
24340                   int           where,
24341                   int           size,
24342                   expressionS * exp,
24343                   bfd_reloc_code_real_type reloc)
24344 {
24345   int pcrel = 0;
24346
24347   /* Pick a reloc.
24348      FIXME: @@ Should look at CPU word size.  */
24349   switch (size)
24350     {
24351     case 1:
24352       reloc = BFD_RELOC_8;
24353       break;
24354     case 2:
24355       reloc = BFD_RELOC_16;
24356       break;
24357     case 4:
24358     default:
24359       reloc = BFD_RELOC_32;
24360       break;
24361     case 8:
24362       reloc = BFD_RELOC_64;
24363       break;
24364     }
24365
24366 #ifdef TE_PE
24367   if (exp->X_op == O_secrel)
24368   {
24369     exp->X_op = O_symbol;
24370     reloc = BFD_RELOC_32_SECREL;
24371   }
24372 #endif
24373
24374   fix_new_exp (frag, where, size, exp, pcrel, reloc);
24375 }
24376
24377 #if defined (OBJ_COFF)
24378 void
24379 arm_validate_fix (fixS * fixP)
24380 {
24381   /* If the destination of the branch is a defined symbol which does not have
24382      the THUMB_FUNC attribute, then we must be calling a function which has
24383      the (interfacearm) attribute.  We look for the Thumb entry point to that
24384      function and change the branch to refer to that function instead.  */
24385   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
24386       && fixP->fx_addsy != NULL
24387       && S_IS_DEFINED (fixP->fx_addsy)
24388       && ! THUMB_IS_FUNC (fixP->fx_addsy))
24389     {
24390       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
24391     }
24392 }
24393 #endif
24394
24395
24396 int
24397 arm_force_relocation (struct fix * fixp)
24398 {
24399 #if defined (OBJ_COFF) && defined (TE_PE)
24400   if (fixp->fx_r_type == BFD_RELOC_RVA)
24401     return 1;
24402 #endif
24403
24404   /* In case we have a call or a branch to a function in ARM ISA mode from
24405      a thumb function or vice-versa force the relocation. These relocations
24406      are cleared off for some cores that might have blx and simple transformations
24407      are possible.  */
24408
24409 #ifdef OBJ_ELF
24410   switch (fixp->fx_r_type)
24411     {
24412     case BFD_RELOC_ARM_PCREL_JUMP:
24413     case BFD_RELOC_ARM_PCREL_CALL:
24414     case BFD_RELOC_THUMB_PCREL_BLX:
24415       if (THUMB_IS_FUNC (fixp->fx_addsy))
24416         return 1;
24417       break;
24418
24419     case BFD_RELOC_ARM_PCREL_BLX:
24420     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24421     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24422     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24423       if (ARM_IS_FUNC (fixp->fx_addsy))
24424         return 1;
24425       break;
24426
24427     default:
24428       break;
24429     }
24430 #endif
24431
24432   /* Resolve these relocations even if the symbol is extern or weak.
24433      Technically this is probably wrong due to symbol preemption.
24434      In practice these relocations do not have enough range to be useful
24435      at dynamic link time, and some code (e.g. in the Linux kernel)
24436      expects these references to be resolved.  */
24437   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
24438       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
24439       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
24440       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
24441       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24442       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
24443       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
24444       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
24445       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
24446       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
24447       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
24448       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
24449       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
24450       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
24451     return 0;
24452
24453   /* Always leave these relocations for the linker.  */
24454   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
24455        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
24456       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
24457     return 1;
24458
24459   /* Always generate relocations against function symbols.  */
24460   if (fixp->fx_r_type == BFD_RELOC_32
24461       && fixp->fx_addsy
24462       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
24463     return 1;
24464
24465   return generic_force_reloc (fixp);
24466 }
24467
24468 #if defined (OBJ_ELF) || defined (OBJ_COFF)
24469 /* Relocations against function names must be left unadjusted,
24470    so that the linker can use this information to generate interworking
24471    stubs.  The MIPS version of this function
24472    also prevents relocations that are mips-16 specific, but I do not
24473    know why it does this.
24474
24475    FIXME:
24476    There is one other problem that ought to be addressed here, but
24477    which currently is not:  Taking the address of a label (rather
24478    than a function) and then later jumping to that address.  Such
24479    addresses also ought to have their bottom bit set (assuming that
24480    they reside in Thumb code), but at the moment they will not.  */
24481
24482 bfd_boolean
24483 arm_fix_adjustable (fixS * fixP)
24484 {
24485   if (fixP->fx_addsy == NULL)
24486     return 1;
24487
24488   /* Preserve relocations against symbols with function type.  */
24489   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
24490     return FALSE;
24491
24492   if (THUMB_IS_FUNC (fixP->fx_addsy)
24493       && fixP->fx_subsy == NULL)
24494     return FALSE;
24495
24496   /* We need the symbol name for the VTABLE entries.  */
24497   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
24498       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24499     return FALSE;
24500
24501   /* Don't allow symbols to be discarded on GOT related relocs.  */
24502   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
24503       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
24504       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
24505       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
24506       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
24507       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
24508       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
24509       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
24510       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
24511       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
24512       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
24513       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
24514       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
24515       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
24516     return FALSE;
24517
24518   /* Similarly for group relocations.  */
24519   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
24520        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
24521       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
24522     return FALSE;
24523
24524   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
24525   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
24526       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
24527       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
24528       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
24529       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
24530       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
24531       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
24532       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
24533     return FALSE;
24534
24535   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
24536      offsets, so keep these symbols.  */
24537   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
24538       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
24539     return FALSE;
24540
24541   return TRUE;
24542 }
24543 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
24544
24545 #ifdef OBJ_ELF
24546 const char *
24547 elf32_arm_target_format (void)
24548 {
24549 #ifdef TE_SYMBIAN
24550   return (target_big_endian
24551           ? "elf32-bigarm-symbian"
24552           : "elf32-littlearm-symbian");
24553 #elif defined (TE_VXWORKS)
24554   return (target_big_endian
24555           ? "elf32-bigarm-vxworks"
24556           : "elf32-littlearm-vxworks");
24557 #elif defined (TE_NACL)
24558   return (target_big_endian
24559           ? "elf32-bigarm-nacl"
24560           : "elf32-littlearm-nacl");
24561 #else
24562   if (target_big_endian)
24563     return "elf32-bigarm";
24564   else
24565     return "elf32-littlearm";
24566 #endif
24567 }
24568
24569 void
24570 armelf_frob_symbol (symbolS * symp,
24571                     int *     puntp)
24572 {
24573   elf_frob_symbol (symp, puntp);
24574 }
24575 #endif
24576
24577 /* MD interface: Finalization.  */
24578
24579 void
24580 arm_cleanup (void)
24581 {
24582   literal_pool * pool;
24583
24584   /* Ensure that all the IT blocks are properly closed.  */
24585   check_it_blocks_finished ();
24586
24587   for (pool = list_of_pools; pool; pool = pool->next)
24588     {
24589       /* Put it at the end of the relevant section.  */
24590       subseg_set (pool->section, pool->sub_section);
24591 #ifdef OBJ_ELF
24592       arm_elf_change_section ();
24593 #endif
24594       s_ltorg (0);
24595     }
24596 }
24597
24598 #ifdef OBJ_ELF
24599 /* Remove any excess mapping symbols generated for alignment frags in
24600    SEC.  We may have created a mapping symbol before a zero byte
24601    alignment; remove it if there's a mapping symbol after the
24602    alignment.  */
24603 static void
24604 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
24605                        void *dummy ATTRIBUTE_UNUSED)
24606 {
24607   segment_info_type *seginfo = seg_info (sec);
24608   fragS *fragp;
24609
24610   if (seginfo == NULL || seginfo->frchainP == NULL)
24611     return;
24612
24613   for (fragp = seginfo->frchainP->frch_root;
24614        fragp != NULL;
24615        fragp = fragp->fr_next)
24616     {
24617       symbolS *sym = fragp->tc_frag_data.last_map;
24618       fragS *next = fragp->fr_next;
24619
24620       /* Variable-sized frags have been converted to fixed size by
24621          this point.  But if this was variable-sized to start with,
24622          there will be a fixed-size frag after it.  So don't handle
24623          next == NULL.  */
24624       if (sym == NULL || next == NULL)
24625         continue;
24626
24627       if (S_GET_VALUE (sym) < next->fr_address)
24628         /* Not at the end of this frag.  */
24629         continue;
24630       know (S_GET_VALUE (sym) == next->fr_address);
24631
24632       do
24633         {
24634           if (next->tc_frag_data.first_map != NULL)
24635             {
24636               /* Next frag starts with a mapping symbol.  Discard this
24637                  one.  */
24638               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24639               break;
24640             }
24641
24642           if (next->fr_next == NULL)
24643             {
24644               /* This mapping symbol is at the end of the section.  Discard
24645                  it.  */
24646               know (next->fr_fix == 0 && next->fr_var == 0);
24647               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24648               break;
24649             }
24650
24651           /* As long as we have empty frags without any mapping symbols,
24652              keep looking.  */
24653           /* If the next frag is non-empty and does not start with a
24654              mapping symbol, then this mapping symbol is required.  */
24655           if (next->fr_address != next->fr_next->fr_address)
24656             break;
24657
24658           next = next->fr_next;
24659         }
24660       while (next != NULL);
24661     }
24662 }
24663 #endif
24664
24665 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
24666    ARM ones.  */
24667
24668 void
24669 arm_adjust_symtab (void)
24670 {
24671 #ifdef OBJ_COFF
24672   symbolS * sym;
24673
24674   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24675     {
24676       if (ARM_IS_THUMB (sym))
24677         {
24678           if (THUMB_IS_FUNC (sym))
24679             {
24680               /* Mark the symbol as a Thumb function.  */
24681               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
24682                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
24683                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
24684
24685               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
24686                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
24687               else
24688                 as_bad (_("%s: unexpected function type: %d"),
24689                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
24690             }
24691           else switch (S_GET_STORAGE_CLASS (sym))
24692             {
24693             case C_EXT:
24694               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
24695               break;
24696             case C_STAT:
24697               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
24698               break;
24699             case C_LABEL:
24700               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
24701               break;
24702             default:
24703               /* Do nothing.  */
24704               break;
24705             }
24706         }
24707
24708       if (ARM_IS_INTERWORK (sym))
24709         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
24710     }
24711 #endif
24712 #ifdef OBJ_ELF
24713   symbolS * sym;
24714   char      bind;
24715
24716   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24717     {
24718       if (ARM_IS_THUMB (sym))
24719         {
24720           elf_symbol_type * elf_sym;
24721
24722           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
24723           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
24724
24725           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
24726                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
24727             {
24728               /* If it's a .thumb_func, declare it as so,
24729                  otherwise tag label as .code 16.  */
24730               if (THUMB_IS_FUNC (sym))
24731                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
24732                                          ST_BRANCH_TO_THUMB);
24733               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
24734                 elf_sym->internal_elf_sym.st_info =
24735                   ELF_ST_INFO (bind, STT_ARM_16BIT);
24736             }
24737         }
24738     }
24739
24740   /* Remove any overlapping mapping symbols generated by alignment frags.  */
24741   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
24742   /* Now do generic ELF adjustments.  */
24743   elf_adjust_symtab ();
24744 #endif
24745 }
24746
24747 /* MD interface: Initialization.  */
24748
24749 static void
24750 set_constant_flonums (void)
24751 {
24752   int i;
24753
24754   for (i = 0; i < NUM_FLOAT_VALS; i++)
24755     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
24756       abort ();
24757 }
24758
24759 /* Auto-select Thumb mode if it's the only available instruction set for the
24760    given architecture.  */
24761
24762 static void
24763 autoselect_thumb_from_cpu_variant (void)
24764 {
24765   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
24766     opcode_select (16);
24767 }
24768
24769 void
24770 md_begin (void)
24771 {
24772   unsigned mach;
24773   unsigned int i;
24774
24775   if (   (arm_ops_hsh = hash_new ()) == NULL
24776       || (arm_cond_hsh = hash_new ()) == NULL
24777       || (arm_shift_hsh = hash_new ()) == NULL
24778       || (arm_psr_hsh = hash_new ()) == NULL
24779       || (arm_v7m_psr_hsh = hash_new ()) == NULL
24780       || (arm_reg_hsh = hash_new ()) == NULL
24781       || (arm_reloc_hsh = hash_new ()) == NULL
24782       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
24783     as_fatal (_("virtual memory exhausted"));
24784
24785   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
24786     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
24787   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
24788     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
24789   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
24790     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
24791   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
24792     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
24793   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
24794     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
24795                  (void *) (v7m_psrs + i));
24796   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
24797     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
24798   for (i = 0;
24799        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
24800        i++)
24801     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
24802                  (void *) (barrier_opt_names + i));
24803 #ifdef OBJ_ELF
24804   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
24805     {
24806       struct reloc_entry * entry = reloc_names + i;
24807
24808       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
24809         /* This makes encode_branch() use the EABI versions of this relocation.  */
24810         entry->reloc = BFD_RELOC_UNUSED;
24811
24812       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
24813     }
24814 #endif
24815
24816   set_constant_flonums ();
24817
24818   /* Set the cpu variant based on the command-line options.  We prefer
24819      -mcpu= over -march= if both are set (as for GCC); and we prefer
24820      -mfpu= over any other way of setting the floating point unit.
24821      Use of legacy options with new options are faulted.  */
24822   if (legacy_cpu)
24823     {
24824       if (mcpu_cpu_opt || march_cpu_opt)
24825         as_bad (_("use of old and new-style options to set CPU type"));
24826
24827       mcpu_cpu_opt = legacy_cpu;
24828     }
24829   else if (!mcpu_cpu_opt)
24830     mcpu_cpu_opt = march_cpu_opt;
24831
24832   if (legacy_fpu)
24833     {
24834       if (mfpu_opt)
24835         as_bad (_("use of old and new-style options to set FPU type"));
24836
24837       mfpu_opt = legacy_fpu;
24838     }
24839   else if (!mfpu_opt)
24840     {
24841 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
24842         || defined (TE_NetBSD) || defined (TE_VXWORKS))
24843       /* Some environments specify a default FPU.  If they don't, infer it
24844          from the processor.  */
24845       if (mcpu_fpu_opt)
24846         mfpu_opt = mcpu_fpu_opt;
24847       else
24848         mfpu_opt = march_fpu_opt;
24849 #else
24850       mfpu_opt = &fpu_default;
24851 #endif
24852     }
24853
24854   if (!mfpu_opt)
24855     {
24856       if (mcpu_cpu_opt != NULL)
24857         mfpu_opt = &fpu_default;
24858       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
24859         mfpu_opt = &fpu_arch_vfp_v2;
24860       else
24861         mfpu_opt = &fpu_arch_fpa;
24862     }
24863
24864 #ifdef CPU_DEFAULT
24865   if (!mcpu_cpu_opt)
24866     {
24867       mcpu_cpu_opt = &cpu_default;
24868       selected_cpu = cpu_default;
24869     }
24870   else if (no_cpu_selected ())
24871     selected_cpu = cpu_default;
24872 #else
24873   if (mcpu_cpu_opt)
24874     selected_cpu = *mcpu_cpu_opt;
24875   else
24876     mcpu_cpu_opt = &arm_arch_any;
24877 #endif
24878
24879   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24880
24881   autoselect_thumb_from_cpu_variant ();
24882
24883   arm_arch_used = thumb_arch_used = arm_arch_none;
24884
24885 #if defined OBJ_COFF || defined OBJ_ELF
24886   {
24887     unsigned int flags = 0;
24888
24889 #if defined OBJ_ELF
24890     flags = meabi_flags;
24891
24892     switch (meabi_flags)
24893       {
24894       case EF_ARM_EABI_UNKNOWN:
24895 #endif
24896         /* Set the flags in the private structure.  */
24897         if (uses_apcs_26)      flags |= F_APCS26;
24898         if (support_interwork) flags |= F_INTERWORK;
24899         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
24900         if (pic_code)          flags |= F_PIC;
24901         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
24902           flags |= F_SOFT_FLOAT;
24903
24904         switch (mfloat_abi_opt)
24905           {
24906           case ARM_FLOAT_ABI_SOFT:
24907           case ARM_FLOAT_ABI_SOFTFP:
24908             flags |= F_SOFT_FLOAT;
24909             break;
24910
24911           case ARM_FLOAT_ABI_HARD:
24912             if (flags & F_SOFT_FLOAT)
24913               as_bad (_("hard-float conflicts with specified fpu"));
24914             break;
24915           }
24916
24917         /* Using pure-endian doubles (even if soft-float).      */
24918         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
24919           flags |= F_VFP_FLOAT;
24920
24921 #if defined OBJ_ELF
24922         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
24923             flags |= EF_ARM_MAVERICK_FLOAT;
24924         break;
24925
24926       case EF_ARM_EABI_VER4:
24927       case EF_ARM_EABI_VER5:
24928         /* No additional flags to set.  */
24929         break;
24930
24931       default:
24932         abort ();
24933       }
24934 #endif
24935     bfd_set_private_flags (stdoutput, flags);
24936
24937     /* We have run out flags in the COFF header to encode the
24938        status of ATPCS support, so instead we create a dummy,
24939        empty, debug section called .arm.atpcs.  */
24940     if (atpcs)
24941       {
24942         asection * sec;
24943
24944         sec = bfd_make_section (stdoutput, ".arm.atpcs");
24945
24946         if (sec != NULL)
24947           {
24948             bfd_set_section_flags
24949               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
24950             bfd_set_section_size (stdoutput, sec, 0);
24951             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
24952           }
24953       }
24954   }
24955 #endif
24956
24957   /* Record the CPU type as well.  */
24958   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
24959     mach = bfd_mach_arm_iWMMXt2;
24960   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
24961     mach = bfd_mach_arm_iWMMXt;
24962   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
24963     mach = bfd_mach_arm_XScale;
24964   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
24965     mach = bfd_mach_arm_ep9312;
24966   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
24967     mach = bfd_mach_arm_5TE;
24968   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
24969     {
24970       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24971         mach = bfd_mach_arm_5T;
24972       else
24973         mach = bfd_mach_arm_5;
24974     }
24975   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
24976     {
24977       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24978         mach = bfd_mach_arm_4T;
24979       else
24980         mach = bfd_mach_arm_4;
24981     }
24982   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
24983     mach = bfd_mach_arm_3M;
24984   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
24985     mach = bfd_mach_arm_3;
24986   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
24987     mach = bfd_mach_arm_2a;
24988   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
24989     mach = bfd_mach_arm_2;
24990   else
24991     mach = bfd_mach_arm_unknown;
24992
24993   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
24994 }
24995
24996 /* Command line processing.  */
24997
24998 /* md_parse_option
24999       Invocation line includes a switch not recognized by the base assembler.
25000       See if it's a processor-specific option.
25001
25002       This routine is somewhat complicated by the need for backwards
25003       compatibility (since older releases of gcc can't be changed).
25004       The new options try to make the interface as compatible as
25005       possible with GCC.
25006
25007       New options (supported) are:
25008
25009               -mcpu=<cpu name>           Assemble for selected processor
25010               -march=<architecture name> Assemble for selected architecture
25011               -mfpu=<fpu architecture>   Assemble for selected FPU.
25012               -EB/-mbig-endian           Big-endian
25013               -EL/-mlittle-endian        Little-endian
25014               -k                         Generate PIC code
25015               -mthumb                    Start in Thumb mode
25016               -mthumb-interwork          Code supports ARM/Thumb interworking
25017
25018               -m[no-]warn-deprecated     Warn about deprecated features
25019               -m[no-]warn-syms           Warn when symbols match instructions
25020
25021       For now we will also provide support for:
25022
25023               -mapcs-32                  32-bit Program counter
25024               -mapcs-26                  26-bit Program counter
25025               -macps-float               Floats passed in FP registers
25026               -mapcs-reentrant           Reentrant code
25027               -matpcs
25028       (sometime these will probably be replaced with -mapcs=<list of options>
25029       and -matpcs=<list of options>)
25030
25031       The remaining options are only supported for back-wards compatibility.
25032       Cpu variants, the arm part is optional:
25033               -m[arm]1                Currently not supported.
25034               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
25035               -m[arm]3                Arm 3 processor
25036               -m[arm]6[xx],           Arm 6 processors
25037               -m[arm]7[xx][t][[d]m]   Arm 7 processors
25038               -m[arm]8[10]            Arm 8 processors
25039               -m[arm]9[20][tdmi]      Arm 9 processors
25040               -mstrongarm[110[0]]     StrongARM processors
25041               -mxscale                XScale processors
25042               -m[arm]v[2345[t[e]]]    Arm architectures
25043               -mall                   All (except the ARM1)
25044       FP variants:
25045               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
25046               -mfpe-old               (No float load/store multiples)
25047               -mvfpxd                 VFP Single precision
25048               -mvfp                   All VFP
25049               -mno-fpu                Disable all floating point instructions
25050
25051       The following CPU names are recognized:
25052               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
25053               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
25054               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
25055               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
25056               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
25057               arm10t arm10e, arm1020t, arm1020e, arm10200e,
25058               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
25059
25060       */
25061
25062 const char * md_shortopts = "m:k";
25063
25064 #ifdef ARM_BI_ENDIAN
25065 #define OPTION_EB (OPTION_MD_BASE + 0)
25066 #define OPTION_EL (OPTION_MD_BASE + 1)
25067 #else
25068 #if TARGET_BYTES_BIG_ENDIAN
25069 #define OPTION_EB (OPTION_MD_BASE + 0)
25070 #else
25071 #define OPTION_EL (OPTION_MD_BASE + 1)
25072 #endif
25073 #endif
25074 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
25075
25076 struct option md_longopts[] =
25077 {
25078 #ifdef OPTION_EB
25079   {"EB", no_argument, NULL, OPTION_EB},
25080 #endif
25081 #ifdef OPTION_EL
25082   {"EL", no_argument, NULL, OPTION_EL},
25083 #endif
25084   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
25085   {NULL, no_argument, NULL, 0}
25086 };
25087
25088
25089 size_t md_longopts_size = sizeof (md_longopts);
25090
25091 struct arm_option_table
25092 {
25093   const char *option;           /* Option name to match.  */
25094   const char *help;             /* Help information.  */
25095   int  *var;            /* Variable to change.  */
25096   int   value;          /* What to change it to.  */
25097   const char *deprecated;       /* If non-null, print this message.  */
25098 };
25099
25100 struct arm_option_table arm_opts[] =
25101 {
25102   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
25103   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
25104   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
25105    &support_interwork, 1, NULL},
25106   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
25107   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
25108   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
25109    1, NULL},
25110   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
25111   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
25112   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
25113   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
25114    NULL},
25115
25116   /* These are recognized by the assembler, but have no affect on code.  */
25117   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
25118   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
25119
25120   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
25121   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
25122    &warn_on_deprecated, 0, NULL},
25123   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
25124   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
25125   {NULL, NULL, NULL, 0, NULL}
25126 };
25127
25128 struct arm_legacy_option_table
25129 {
25130   const char *option;                           /* Option name to match.  */
25131   const arm_feature_set **var;          /* Variable to change.  */
25132   const arm_feature_set value;          /* What to change it to.  */
25133   const char *deprecated;                       /* If non-null, print this message.  */
25134 };
25135
25136 const struct arm_legacy_option_table arm_legacy_opts[] =
25137 {
25138   /* DON'T add any new processors to this list -- we want the whole list
25139      to go away...  Add them to the processors table instead.  */
25140   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25141   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25142   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25143   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25144   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25145   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25146   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25147   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25148   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25149   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25150   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25151   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25152   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25153   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25154   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25155   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25156   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25157   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25158   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25159   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25160   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25161   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25162   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25163   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25164   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25165   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25166   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25167   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25168   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25169   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25170   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25171   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25172   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25173   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25174   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25175   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25176   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25177   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25178   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25179   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25180   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25181   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25182   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25183   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25184   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25185   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25186   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25187   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25188   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25189   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25190   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25191   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25192   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25193   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25194   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25195   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25196   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25197   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25198   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25199   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25200   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25201   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25202   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25203   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25204   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25205   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25206   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25207   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25208   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
25209   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
25210    N_("use -mcpu=strongarm110")},
25211   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
25212    N_("use -mcpu=strongarm1100")},
25213   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
25214    N_("use -mcpu=strongarm1110")},
25215   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
25216   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
25217   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
25218
25219   /* Architecture variants -- don't add any more to this list either.  */
25220   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25221   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25222   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25223   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25224   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25225   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25226   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25227   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25228   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25229   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25230   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25231   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25232   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25233   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25234   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25235   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25236   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25237   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25238
25239   /* Floating point variants -- don't add any more to this list either.  */
25240   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
25241   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
25242   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
25243   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
25244    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
25245
25246   {NULL, NULL, ARM_ARCH_NONE, NULL}
25247 };
25248
25249 struct arm_cpu_option_table
25250 {
25251   const char *name;
25252   size_t name_len;
25253   const arm_feature_set value;
25254   /* For some CPUs we assume an FPU unless the user explicitly sets
25255      -mfpu=...  */
25256   const arm_feature_set default_fpu;
25257   /* The canonical name of the CPU, or NULL to use NAME converted to upper
25258      case.  */
25259   const char *canonical_name;
25260 };
25261
25262 /* This list should, at a minimum, contain all the cpu names
25263    recognized by GCC.  */
25264 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
25265 static const struct arm_cpu_option_table arm_cpus[] =
25266 {
25267   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
25268   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
25269   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
25270   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
25271   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
25272   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25273   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25274   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25275   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25276   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25277   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25278   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25279   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25280   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25281   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25282   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25283   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25284   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25285   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25286   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25287   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25288   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25289   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25290   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25291   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25292   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25293   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25294   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25295   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25296   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25297   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25298   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25299   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25300   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25301   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25302   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25303   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25304   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25305   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25306   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
25307   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25308   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25309   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25310   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25311   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25312   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25313   /* For V5 or later processors we default to using VFP; but the user
25314      should really set the FPU type explicitly.  */
25315   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25316   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25317   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
25318   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
25319   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
25320   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25321   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
25322   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25323   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25324   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
25325   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25326   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25327   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25328   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25329   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25330   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
25331   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25332   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25333   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25334   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
25335                                                                  "ARM1026EJ-S"),
25336   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
25337   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25338   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25339   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25340   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25341   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25342   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
25343   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
25344   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
25345                                                                  "ARM1136JF-S"),
25346   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
25347   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
25348   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
25349   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
25350   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
25351   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6KZ,   FPU_NONE,        NULL),
25352   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6KZ,   FPU_ARCH_VFP_V2, NULL),
25353   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
25354                                                  FPU_NONE,        "Cortex-A5"),
25355   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25356                                                                   "Cortex-A7"),
25357   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
25358                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
25359                                                         | FPU_NEON_EXT_V1),
25360                                                                   "Cortex-A8"),
25361   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
25362                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
25363                                                         | FPU_NEON_EXT_V1),
25364                                                                   "Cortex-A9"),
25365   ARM_CPU_OPT ("cortex-a12",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25366                                                                   "Cortex-A12"),
25367   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25368                                                                   "Cortex-A15"),
25369   ARM_CPU_OPT ("cortex-a17",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25370                                                                   "Cortex-A17"),
25371   ARM_CPU_OPT ("cortex-a32",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25372                                                                   "Cortex-A32"),
25373   ARM_CPU_OPT ("cortex-a35",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25374                                                                   "Cortex-A35"),
25375   ARM_CPU_OPT ("cortex-a53",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25376                                                                   "Cortex-A53"),
25377   ARM_CPU_OPT ("cortex-a57",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25378                                                                   "Cortex-A57"),
25379   ARM_CPU_OPT ("cortex-a72",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25380                                                                   "Cortex-A72"),
25381   ARM_CPU_OPT ("cortex-a73",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25382                                                                   "Cortex-A73"),
25383   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
25384   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
25385                                                                   "Cortex-R4F"),
25386   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
25387                                                  FPU_NONE,        "Cortex-R5"),
25388   ARM_CPU_OPT ("cortex-r7",     ARM_ARCH_V7R_IDIV,
25389                                                  FPU_ARCH_VFP_V3D16,
25390                                                                   "Cortex-R7"),
25391   ARM_CPU_OPT ("cortex-r8",     ARM_ARCH_V7R_IDIV,
25392                                                  FPU_ARCH_VFP_V3D16,
25393                                                                   "Cortex-R8"),
25394   ARM_CPU_OPT ("cortex-m7",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M7"),
25395   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
25396   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
25397   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
25398   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
25399   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
25400   ARM_CPU_OPT ("exynos-m1",     ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25401                                                                   "Samsung " \
25402                                                                   "Exynos M1"),
25403   ARM_CPU_OPT ("qdf24xx",       ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25404                                                                   "Qualcomm "
25405                                                                   "QDF24XX"),
25406
25407   /* ??? XSCALE is really an architecture.  */
25408   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
25409   /* ??? iwmmxt is not a processor.  */
25410   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
25411   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
25412   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
25413   /* Maverick */
25414   ARM_CPU_OPT ("ep9312",        ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
25415                                                  FPU_ARCH_MAVERICK, "ARM920T"),
25416   /* Marvell processors.  */
25417   ARM_CPU_OPT ("marvell-pj4",   ARM_FEATURE_CORE (ARM_AEXT_V7A | ARM_EXT_MP
25418                                                   | ARM_EXT_SEC,
25419                                                   ARM_EXT2_V6T2_V8M),
25420                                                 FPU_ARCH_VFP_V3D16, NULL),
25421   ARM_CPU_OPT ("marvell-whitney", ARM_FEATURE_CORE (ARM_AEXT_V7A | ARM_EXT_MP
25422                                                     | ARM_EXT_SEC,
25423                                                     ARM_EXT2_V6T2_V8M),
25424                                                FPU_ARCH_NEON_VFP_V4, NULL),
25425   /* APM X-Gene family.  */
25426   ARM_CPU_OPT ("xgene1",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25427                                                                   "APM X-Gene 1"),
25428   ARM_CPU_OPT ("xgene2",        ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25429                                                                   "APM X-Gene 2"),
25430
25431   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
25432 };
25433 #undef ARM_CPU_OPT
25434
25435 struct arm_arch_option_table
25436 {
25437   const char *name;
25438   size_t name_len;
25439   const arm_feature_set value;
25440   const arm_feature_set default_fpu;
25441 };
25442
25443 /* This list should, at a minimum, contain all the architecture names
25444    recognized by GCC.  */
25445 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
25446 static const struct arm_arch_option_table arm_archs[] =
25447 {
25448   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
25449   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
25450   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
25451   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
25452   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
25453   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
25454   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
25455   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
25456   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
25457   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
25458   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
25459   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
25460   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
25461   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
25462   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
25463   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
25464   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
25465   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
25466   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
25467   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
25468   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
25469   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
25470      kept to preserve existing behaviour.  */
25471   ARM_ARCH_OPT ("armv6kz",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
25472   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
25473   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
25474   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
25475   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
25476   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
25477      kept to preserve existing behaviour.  */
25478   ARM_ARCH_OPT ("armv6kzt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
25479   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
25480   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
25481   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
25482   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
25483   /* The official spelling of the ARMv7 profile variants is the dashed form.
25484      Accept the non-dashed form for compatibility with old toolchains.  */
25485   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
25486   ARM_ARCH_OPT ("armv7ve",      ARM_ARCH_V7VE,   FPU_ARCH_VFP),
25487   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
25488   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
25489   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
25490   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
25491   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
25492   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
25493   ARM_ARCH_OPT ("armv8-m.base", ARM_ARCH_V8M_BASE, FPU_ARCH_VFP),
25494   ARM_ARCH_OPT ("armv8-m.main", ARM_ARCH_V8M_MAIN, FPU_ARCH_VFP),
25495   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
25496   ARM_ARCH_OPT ("armv8.1-a",    ARM_ARCH_V8_1A,  FPU_ARCH_VFP),
25497   ARM_ARCH_OPT ("armv8.2-a",    ARM_ARCH_V8_2A,  FPU_ARCH_VFP),
25498   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
25499   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
25500   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
25501   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
25502 };
25503 #undef ARM_ARCH_OPT
25504
25505 /* ISA extensions in the co-processor and main instruction set space.  */
25506 struct arm_option_extension_value_table
25507 {
25508   const char *name;
25509   size_t name_len;
25510   const arm_feature_set merge_value;
25511   const arm_feature_set clear_value;
25512   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
25513      indicates that an extension is available for all architectures while
25514      ARM_ANY marks an empty entry.  */
25515   const arm_feature_set allowed_archs[2];
25516 };
25517
25518 /* The following table must be in alphabetical order with a NULL last entry.
25519    */
25520 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
25521 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
25522 static const struct arm_option_extension_value_table arm_extensions[] =
25523 {
25524   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
25525                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25526   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25527                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
25528                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25529   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
25530                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
25531                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
25532   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
25533                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25534   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
25535                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
25536                         ARM_ARCH_V8_2A),
25537   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
25538                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
25539                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
25540                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
25541   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
25542                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
25543   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
25544                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
25545   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
25546                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
25547   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
25548                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
25549                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
25550                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
25551   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
25552                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
25553                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
25554   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
25555                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
25556                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25557   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
25558                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
25559                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25560   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
25561                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
25562                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25563   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
25564                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
25565                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
25566                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
25567   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
25568                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
25569                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25570   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
25571                                      | ARM_EXT_DIV),
25572                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
25573                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
25574   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
25575                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
25576   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
25577 };
25578 #undef ARM_EXT_OPT
25579
25580 /* ISA floating-point and Advanced SIMD extensions.  */
25581 struct arm_option_fpu_value_table
25582 {
25583   const char *name;
25584   const arm_feature_set value;
25585 };
25586
25587 /* This list should, at a minimum, contain all the fpu names
25588    recognized by GCC.  */
25589 static const struct arm_option_fpu_value_table arm_fpus[] =
25590 {
25591   {"softfpa",           FPU_NONE},
25592   {"fpe",               FPU_ARCH_FPE},
25593   {"fpe2",              FPU_ARCH_FPE},
25594   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
25595   {"fpa",               FPU_ARCH_FPA},
25596   {"fpa10",             FPU_ARCH_FPA},
25597   {"fpa11",             FPU_ARCH_FPA},
25598   {"arm7500fe",         FPU_ARCH_FPA},
25599   {"softvfp",           FPU_ARCH_VFP},
25600   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
25601   {"vfp",               FPU_ARCH_VFP_V2},
25602   {"vfp9",              FPU_ARCH_VFP_V2},
25603   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
25604   {"vfp10",             FPU_ARCH_VFP_V2},
25605   {"vfp10-r0",          FPU_ARCH_VFP_V1},
25606   {"vfpxd",             FPU_ARCH_VFP_V1xD},
25607   {"vfpv2",             FPU_ARCH_VFP_V2},
25608   {"vfpv3",             FPU_ARCH_VFP_V3},
25609   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
25610   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
25611   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
25612   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
25613   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
25614   {"arm1020t",          FPU_ARCH_VFP_V1},
25615   {"arm1020e",          FPU_ARCH_VFP_V2},
25616   {"arm1136jfs",        FPU_ARCH_VFP_V2},
25617   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
25618   {"maverick",          FPU_ARCH_MAVERICK},
25619   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
25620   {"neon-fp16",         FPU_ARCH_NEON_FP16},
25621   {"vfpv4",             FPU_ARCH_VFP_V4},
25622   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
25623   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
25624   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
25625   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
25626   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
25627   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
25628   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
25629   {"crypto-neon-fp-armv8",
25630                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
25631   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
25632   {"crypto-neon-fp-armv8.1",
25633                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
25634   {NULL,                ARM_ARCH_NONE}
25635 };
25636
25637 struct arm_option_value_table
25638 {
25639   const char *name;
25640   long value;
25641 };
25642
25643 static const struct arm_option_value_table arm_float_abis[] =
25644 {
25645   {"hard",      ARM_FLOAT_ABI_HARD},
25646   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
25647   {"soft",      ARM_FLOAT_ABI_SOFT},
25648   {NULL,        0}
25649 };
25650
25651 #ifdef OBJ_ELF
25652 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
25653 static const struct arm_option_value_table arm_eabis[] =
25654 {
25655   {"gnu",       EF_ARM_EABI_UNKNOWN},
25656   {"4",         EF_ARM_EABI_VER4},
25657   {"5",         EF_ARM_EABI_VER5},
25658   {NULL,        0}
25659 };
25660 #endif
25661
25662 struct arm_long_option_table
25663 {
25664   const char * option;          /* Substring to match.  */
25665   const char * help;                    /* Help information.  */
25666   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
25667   const char * deprecated;              /* If non-null, print this message.  */
25668 };
25669
25670 static bfd_boolean
25671 arm_parse_extension (const char *str, const arm_feature_set **opt_p)
25672 {
25673   arm_feature_set *ext_set = XNEW (arm_feature_set);
25674
25675   /* We insist on extensions being specified in alphabetical order, and with
25676      extensions being added before being removed.  We achieve this by having
25677      the global ARM_EXTENSIONS table in alphabetical order, and using the
25678      ADDING_VALUE variable to indicate whether we are adding an extension (1)
25679      or removing it (0) and only allowing it to change in the order
25680      -1 -> 1 -> 0.  */
25681   const struct arm_option_extension_value_table * opt = NULL;
25682   const arm_feature_set arm_any = ARM_ANY;
25683   int adding_value = -1;
25684
25685   /* Copy the feature set, so that we can modify it.  */
25686   *ext_set = **opt_p;
25687   *opt_p = ext_set;
25688
25689   while (str != NULL && *str != 0)
25690     {
25691       const char *ext;
25692       size_t len;
25693
25694       if (*str != '+')
25695         {
25696           as_bad (_("invalid architectural extension"));
25697           return FALSE;
25698         }
25699
25700       str++;
25701       ext = strchr (str, '+');
25702
25703       if (ext != NULL)
25704         len = ext - str;
25705       else
25706         len = strlen (str);
25707
25708       if (len >= 2 && strncmp (str, "no", 2) == 0)
25709         {
25710           if (adding_value != 0)
25711             {
25712               adding_value = 0;
25713               opt = arm_extensions;
25714             }
25715
25716           len -= 2;
25717           str += 2;
25718         }
25719       else if (len > 0)
25720         {
25721           if (adding_value == -1)
25722             {
25723               adding_value = 1;
25724               opt = arm_extensions;
25725             }
25726           else if (adding_value != 1)
25727             {
25728               as_bad (_("must specify extensions to add before specifying "
25729                         "those to remove"));
25730               return FALSE;
25731             }
25732         }
25733
25734       if (len == 0)
25735         {
25736           as_bad (_("missing architectural extension"));
25737           return FALSE;
25738         }
25739
25740       gas_assert (adding_value != -1);
25741       gas_assert (opt != NULL);
25742
25743       /* Scan over the options table trying to find an exact match. */
25744       for (; opt->name != NULL; opt++)
25745         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25746           {
25747             int i, nb_allowed_archs =
25748               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
25749             /* Check we can apply the extension to this architecture.  */
25750             for (i = 0; i < nb_allowed_archs; i++)
25751               {
25752                 /* Empty entry.  */
25753                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
25754                   continue;
25755                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *ext_set))
25756                   break;
25757               }
25758             if (i == nb_allowed_archs)
25759               {
25760                 as_bad (_("extension does not apply to the base architecture"));
25761                 return FALSE;
25762               }
25763
25764             /* Add or remove the extension.  */
25765             if (adding_value)
25766               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
25767             else
25768               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
25769
25770             break;
25771           }
25772
25773       if (opt->name == NULL)
25774         {
25775           /* Did we fail to find an extension because it wasn't specified in
25776              alphabetical order, or because it does not exist?  */
25777
25778           for (opt = arm_extensions; opt->name != NULL; opt++)
25779             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25780               break;
25781
25782           if (opt->name == NULL)
25783             as_bad (_("unknown architectural extension `%s'"), str);
25784           else
25785             as_bad (_("architectural extensions must be specified in "
25786                       "alphabetical order"));
25787
25788           return FALSE;
25789         }
25790       else
25791         {
25792           /* We should skip the extension we've just matched the next time
25793              round.  */
25794           opt++;
25795         }
25796
25797       str = ext;
25798     };
25799
25800   return TRUE;
25801 }
25802
25803 static bfd_boolean
25804 arm_parse_cpu (const char *str)
25805 {
25806   const struct arm_cpu_option_table *opt;
25807   const char *ext = strchr (str, '+');
25808   size_t len;
25809
25810   if (ext != NULL)
25811     len = ext - str;
25812   else
25813     len = strlen (str);
25814
25815   if (len == 0)
25816     {
25817       as_bad (_("missing cpu name `%s'"), str);
25818       return FALSE;
25819     }
25820
25821   for (opt = arm_cpus; opt->name != NULL; opt++)
25822     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25823       {
25824         mcpu_cpu_opt = &opt->value;
25825         mcpu_fpu_opt = &opt->default_fpu;
25826         if (opt->canonical_name)
25827           {
25828             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
25829             strcpy (selected_cpu_name, opt->canonical_name);
25830           }
25831         else
25832           {
25833             size_t i;
25834
25835             if (len >= sizeof selected_cpu_name)
25836               len = (sizeof selected_cpu_name) - 1;
25837
25838             for (i = 0; i < len; i++)
25839               selected_cpu_name[i] = TOUPPER (opt->name[i]);
25840             selected_cpu_name[i] = 0;
25841           }
25842
25843         if (ext != NULL)
25844           return arm_parse_extension (ext, &mcpu_cpu_opt);
25845
25846         return TRUE;
25847       }
25848
25849   as_bad (_("unknown cpu `%s'"), str);
25850   return FALSE;
25851 }
25852
25853 static bfd_boolean
25854 arm_parse_arch (const char *str)
25855 {
25856   const struct arm_arch_option_table *opt;
25857   const char *ext = strchr (str, '+');
25858   size_t len;
25859
25860   if (ext != NULL)
25861     len = ext - str;
25862   else
25863     len = strlen (str);
25864
25865   if (len == 0)
25866     {
25867       as_bad (_("missing architecture name `%s'"), str);
25868       return FALSE;
25869     }
25870
25871   for (opt = arm_archs; opt->name != NULL; opt++)
25872     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25873       {
25874         march_cpu_opt = &opt->value;
25875         march_fpu_opt = &opt->default_fpu;
25876         strcpy (selected_cpu_name, opt->name);
25877
25878         if (ext != NULL)
25879           return arm_parse_extension (ext, &march_cpu_opt);
25880
25881         return TRUE;
25882       }
25883
25884   as_bad (_("unknown architecture `%s'\n"), str);
25885   return FALSE;
25886 }
25887
25888 static bfd_boolean
25889 arm_parse_fpu (const char * str)
25890 {
25891   const struct arm_option_fpu_value_table * opt;
25892
25893   for (opt = arm_fpus; opt->name != NULL; opt++)
25894     if (streq (opt->name, str))
25895       {
25896         mfpu_opt = &opt->value;
25897         return TRUE;
25898       }
25899
25900   as_bad (_("unknown floating point format `%s'\n"), str);
25901   return FALSE;
25902 }
25903
25904 static bfd_boolean
25905 arm_parse_float_abi (const char * str)
25906 {
25907   const struct arm_option_value_table * opt;
25908
25909   for (opt = arm_float_abis; opt->name != NULL; opt++)
25910     if (streq (opt->name, str))
25911       {
25912         mfloat_abi_opt = opt->value;
25913         return TRUE;
25914       }
25915
25916   as_bad (_("unknown floating point abi `%s'\n"), str);
25917   return FALSE;
25918 }
25919
25920 #ifdef OBJ_ELF
25921 static bfd_boolean
25922 arm_parse_eabi (const char * str)
25923 {
25924   const struct arm_option_value_table *opt;
25925
25926   for (opt = arm_eabis; opt->name != NULL; opt++)
25927     if (streq (opt->name, str))
25928       {
25929         meabi_flags = opt->value;
25930         return TRUE;
25931       }
25932   as_bad (_("unknown EABI `%s'\n"), str);
25933   return FALSE;
25934 }
25935 #endif
25936
25937 static bfd_boolean
25938 arm_parse_it_mode (const char * str)
25939 {
25940   bfd_boolean ret = TRUE;
25941
25942   if (streq ("arm", str))
25943     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
25944   else if (streq ("thumb", str))
25945     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
25946   else if (streq ("always", str))
25947     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
25948   else if (streq ("never", str))
25949     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
25950   else
25951     {
25952       as_bad (_("unknown implicit IT mode `%s', should be "\
25953                 "arm, thumb, always, or never."), str);
25954       ret = FALSE;
25955     }
25956
25957   return ret;
25958 }
25959
25960 static bfd_boolean
25961 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
25962 {
25963   codecomposer_syntax = TRUE;
25964   arm_comment_chars[0] = ';';
25965   arm_line_separator_chars[0] = 0;
25966   return TRUE;
25967 }
25968
25969 struct arm_long_option_table arm_long_opts[] =
25970 {
25971   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
25972    arm_parse_cpu, NULL},
25973   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
25974    arm_parse_arch, NULL},
25975   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
25976    arm_parse_fpu, NULL},
25977   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
25978    arm_parse_float_abi, NULL},
25979 #ifdef OBJ_ELF
25980   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
25981    arm_parse_eabi, NULL},
25982 #endif
25983   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
25984    arm_parse_it_mode, NULL},
25985   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
25986    arm_ccs_mode, NULL},
25987   {NULL, NULL, 0, NULL}
25988 };
25989
25990 int
25991 md_parse_option (int c, const char * arg)
25992 {
25993   struct arm_option_table *opt;
25994   const struct arm_legacy_option_table *fopt;
25995   struct arm_long_option_table *lopt;
25996
25997   switch (c)
25998     {
25999 #ifdef OPTION_EB
26000     case OPTION_EB:
26001       target_big_endian = 1;
26002       break;
26003 #endif
26004
26005 #ifdef OPTION_EL
26006     case OPTION_EL:
26007       target_big_endian = 0;
26008       break;
26009 #endif
26010
26011     case OPTION_FIX_V4BX:
26012       fix_v4bx = TRUE;
26013       break;
26014
26015     case 'a':
26016       /* Listing option.  Just ignore these, we don't support additional
26017          ones.  */
26018       return 0;
26019
26020     default:
26021       for (opt = arm_opts; opt->option != NULL; opt++)
26022         {
26023           if (c == opt->option[0]
26024               && ((arg == NULL && opt->option[1] == 0)
26025                   || streq (arg, opt->option + 1)))
26026             {
26027               /* If the option is deprecated, tell the user.  */
26028               if (warn_on_deprecated && opt->deprecated != NULL)
26029                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26030                            arg ? arg : "", _(opt->deprecated));
26031
26032               if (opt->var != NULL)
26033                 *opt->var = opt->value;
26034
26035               return 1;
26036             }
26037         }
26038
26039       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
26040         {
26041           if (c == fopt->option[0]
26042               && ((arg == NULL && fopt->option[1] == 0)
26043                   || streq (arg, fopt->option + 1)))
26044             {
26045               /* If the option is deprecated, tell the user.  */
26046               if (warn_on_deprecated && fopt->deprecated != NULL)
26047                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26048                            arg ? arg : "", _(fopt->deprecated));
26049
26050               if (fopt->var != NULL)
26051                 *fopt->var = &fopt->value;
26052
26053               return 1;
26054             }
26055         }
26056
26057       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26058         {
26059           /* These options are expected to have an argument.  */
26060           if (c == lopt->option[0]
26061               && arg != NULL
26062               && strncmp (arg, lopt->option + 1,
26063                           strlen (lopt->option + 1)) == 0)
26064             {
26065               /* If the option is deprecated, tell the user.  */
26066               if (warn_on_deprecated && lopt->deprecated != NULL)
26067                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
26068                            _(lopt->deprecated));
26069
26070               /* Call the sup-option parser.  */
26071               return lopt->func (arg + strlen (lopt->option) - 1);
26072             }
26073         }
26074
26075       return 0;
26076     }
26077
26078   return 1;
26079 }
26080
26081 void
26082 md_show_usage (FILE * fp)
26083 {
26084   struct arm_option_table *opt;
26085   struct arm_long_option_table *lopt;
26086
26087   fprintf (fp, _(" ARM-specific assembler options:\n"));
26088
26089   for (opt = arm_opts; opt->option != NULL; opt++)
26090     if (opt->help != NULL)
26091       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
26092
26093   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26094     if (lopt->help != NULL)
26095       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
26096
26097 #ifdef OPTION_EB
26098   fprintf (fp, _("\
26099   -EB                     assemble code for a big-endian cpu\n"));
26100 #endif
26101
26102 #ifdef OPTION_EL
26103   fprintf (fp, _("\
26104   -EL                     assemble code for a little-endian cpu\n"));
26105 #endif
26106
26107   fprintf (fp, _("\
26108   --fix-v4bx              Allow BX in ARMv4 code\n"));
26109 }
26110
26111
26112 #ifdef OBJ_ELF
26113 typedef struct
26114 {
26115   int val;
26116   arm_feature_set flags;
26117 } cpu_arch_ver_table;
26118
26119 /* Mapping from CPU features to EABI CPU arch values.  As a general rule, table
26120    must be sorted least features first but some reordering is needed, eg. for
26121    Thumb-2 instructions to be detected as coming from ARMv6T2.  */
26122 static const cpu_arch_ver_table cpu_arch_ver[] =
26123 {
26124     {1, ARM_ARCH_V4},
26125     {2, ARM_ARCH_V4T},
26126     {3, ARM_ARCH_V5},
26127     {3, ARM_ARCH_V5T},
26128     {4, ARM_ARCH_V5TE},
26129     {5, ARM_ARCH_V5TEJ},
26130     {6, ARM_ARCH_V6},
26131     {9, ARM_ARCH_V6K},
26132     {7, ARM_ARCH_V6Z},
26133     {11, ARM_ARCH_V6M},
26134     {12, ARM_ARCH_V6SM},
26135     {8, ARM_ARCH_V6T2},
26136     {10, ARM_ARCH_V7VE},
26137     {10, ARM_ARCH_V7R},
26138     {10, ARM_ARCH_V7M},
26139     {14, ARM_ARCH_V8A},
26140     {16, ARM_ARCH_V8M_BASE},
26141     {17, ARM_ARCH_V8M_MAIN},
26142     {0, ARM_ARCH_NONE}
26143 };
26144
26145 /* Set an attribute if it has not already been set by the user.  */
26146 static void
26147 aeabi_set_attribute_int (int tag, int value)
26148 {
26149   if (tag < 1
26150       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26151       || !attributes_set_explicitly[tag])
26152     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
26153 }
26154
26155 static void
26156 aeabi_set_attribute_string (int tag, const char *value)
26157 {
26158   if (tag < 1
26159       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26160       || !attributes_set_explicitly[tag])
26161     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
26162 }
26163
26164 /* Set the public EABI object attributes.  */
26165 void
26166 aeabi_set_public_attributes (void)
26167 {
26168   int arch;
26169   char profile;
26170   int virt_sec = 0;
26171   int fp16_optional = 0;
26172   arm_feature_set arm_arch = ARM_ARCH_NONE;
26173   arm_feature_set flags;
26174   arm_feature_set tmp;
26175   arm_feature_set arm_arch_v8m_base = ARM_ARCH_V8M_BASE;
26176   const cpu_arch_ver_table *p;
26177
26178   /* Choose the architecture based on the capabilities of the requested cpu
26179      (if any) and/or the instructions actually used.  */
26180   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
26181   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
26182   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
26183
26184   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
26185     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
26186
26187   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
26188     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
26189
26190   selected_cpu = flags;
26191
26192   /* Allow the user to override the reported architecture.  */
26193   if (object_arch)
26194     {
26195       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
26196       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
26197     }
26198
26199   /* We need to make sure that the attributes do not identify us as v6S-M
26200      when the only v6S-M feature in use is the Operating System Extensions.  */
26201   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
26202       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
26203         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
26204
26205   tmp = flags;
26206   arch = 0;
26207   for (p = cpu_arch_ver; p->val; p++)
26208     {
26209       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
26210         {
26211           arch = p->val;
26212           arm_arch = p->flags;
26213           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
26214         }
26215     }
26216
26217   /* The table lookup above finds the last architecture to contribute
26218      a new feature.  Unfortunately, Tag13 is a subset of the union of
26219      v6T2 and v7-M, so it is never seen as contributing a new feature.
26220      We can not search for the last entry which is entirely used,
26221      because if no CPU is specified we build up only those flags
26222      actually used.  Perhaps we should separate out the specified
26223      and implicit cases.  Avoid taking this path for -march=all by
26224      checking for contradictory v7-A / v7-M features.  */
26225   if (arch == TAG_CPU_ARCH_V7
26226       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
26227       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
26228       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
26229     {
26230       arch = TAG_CPU_ARCH_V7E_M;
26231       arm_arch = (arm_feature_set) ARM_ARCH_V7EM;
26232     }
26233
26234   ARM_CLEAR_FEATURE (tmp, flags, arm_arch_v8m_base);
26235   if (arch == TAG_CPU_ARCH_V8M_BASE && ARM_CPU_HAS_FEATURE (tmp, arm_arch_any))
26236     {
26237       arch = TAG_CPU_ARCH_V8M_MAIN;
26238       arm_arch = (arm_feature_set) ARM_ARCH_V8M_MAIN;
26239     }
26240
26241   /* In cpu_arch_ver ARMv8-A is before ARMv8-M for atomics to be detected as
26242      coming from ARMv8-A.  However, since ARMv8-A has more instructions than
26243      ARMv8-M, -march=all must be detected as ARMv8-A.  */
26244   if (arch == TAG_CPU_ARCH_V8M_MAIN
26245       && ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
26246     {
26247       arch = TAG_CPU_ARCH_V8;
26248       arm_arch = (arm_feature_set) ARM_ARCH_V8A;
26249     }
26250
26251   /* Tag_CPU_name.  */
26252   if (selected_cpu_name[0])
26253     {
26254       char *q;
26255
26256       q = selected_cpu_name;
26257       if (strncmp (q, "armv", 4) == 0)
26258         {
26259           int i;
26260
26261           q += 4;
26262           for (i = 0; q[i]; i++)
26263             q[i] = TOUPPER (q[i]);
26264         }
26265       aeabi_set_attribute_string (Tag_CPU_name, q);
26266     }
26267
26268   /* Tag_CPU_arch.  */
26269   aeabi_set_attribute_int (Tag_CPU_arch, arch);
26270
26271   /* Tag_CPU_arch_profile.  */
26272   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
26273       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26274       || (ARM_CPU_HAS_FEATURE (flags, arm_ext_atomics)
26275           && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only)))
26276     profile = 'A';
26277   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
26278     profile = 'R';
26279   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
26280     profile = 'M';
26281   else
26282     profile = '\0';
26283
26284   if (profile != '\0')
26285     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
26286
26287   /* Tag_DSP_extension.  */
26288   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_dsp))
26289     {
26290       arm_feature_set ext;
26291
26292       /* DSP instructions not in architecture.  */
26293       ARM_CLEAR_FEATURE (ext, flags, arm_arch);
26294       if (ARM_CPU_HAS_FEATURE (ext, arm_ext_dsp))
26295         aeabi_set_attribute_int (Tag_DSP_extension, 1);
26296     }
26297
26298   /* Tag_ARM_ISA_use.  */
26299   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
26300       || arch == 0)
26301     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
26302
26303   /* Tag_THUMB_ISA_use.  */
26304   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
26305       || arch == 0)
26306     {
26307       int thumb_isa_use;
26308
26309       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26310           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
26311         thumb_isa_use = 3;
26312       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
26313         thumb_isa_use = 2;
26314       else
26315         thumb_isa_use = 1;
26316       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
26317     }
26318
26319   /* Tag_VFP_arch.  */
26320   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
26321     aeabi_set_attribute_int (Tag_VFP_arch,
26322                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
26323                              ? 7 : 8);
26324   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
26325     aeabi_set_attribute_int (Tag_VFP_arch,
26326                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
26327                              ? 5 : 6);
26328   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
26329     {
26330       fp16_optional = 1;
26331       aeabi_set_attribute_int (Tag_VFP_arch, 3);
26332     }
26333   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
26334     {
26335       aeabi_set_attribute_int (Tag_VFP_arch, 4);
26336       fp16_optional = 1;
26337     }
26338   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
26339     aeabi_set_attribute_int (Tag_VFP_arch, 2);
26340   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
26341            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
26342     aeabi_set_attribute_int (Tag_VFP_arch, 1);
26343
26344   /* Tag_ABI_HardFP_use.  */
26345   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
26346       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
26347     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
26348
26349   /* Tag_WMMX_arch.  */
26350   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
26351     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
26352   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
26353     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
26354
26355   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
26356   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
26357     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
26358   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
26359     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
26360   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
26361     {
26362       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
26363         {
26364           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
26365         }
26366       else
26367         {
26368           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
26369           fp16_optional = 1;
26370         }
26371     }
26372
26373   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
26374   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
26375     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
26376
26377   /* Tag_DIV_use.
26378
26379      We set Tag_DIV_use to two when integer divide instructions have been used
26380      in ARM state, or when Thumb integer divide instructions have been used,
26381      but we have no architecture profile set, nor have we any ARM instructions.
26382
26383      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
26384      by the base architecture.
26385
26386      For new architectures we will have to check these tests.  */
26387   gas_assert (arch <= TAG_CPU_ARCH_V8
26388               || (arch >= TAG_CPU_ARCH_V8M_BASE
26389                   && arch <= TAG_CPU_ARCH_V8M_MAIN));
26390   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26391       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
26392     aeabi_set_attribute_int (Tag_DIV_use, 0);
26393   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
26394            || (profile == '\0'
26395                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
26396                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
26397     aeabi_set_attribute_int (Tag_DIV_use, 2);
26398
26399   /* Tag_MP_extension_use.  */
26400   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
26401     aeabi_set_attribute_int (Tag_MPextension_use, 1);
26402
26403   /* Tag Virtualization_use.  */
26404   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
26405     virt_sec |= 1;
26406   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
26407     virt_sec |= 2;
26408   if (virt_sec != 0)
26409     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
26410 }
26411
26412 /* Add the default contents for the .ARM.attributes section.  */
26413 void
26414 arm_md_end (void)
26415 {
26416   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
26417     return;
26418
26419   aeabi_set_public_attributes ();
26420 }
26421 #endif /* OBJ_ELF */
26422
26423
26424 /* Parse a .cpu directive.  */
26425
26426 static void
26427 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
26428 {
26429   const struct arm_cpu_option_table *opt;
26430   char *name;
26431   char saved_char;
26432
26433   name = input_line_pointer;
26434   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26435     input_line_pointer++;
26436   saved_char = *input_line_pointer;
26437   *input_line_pointer = 0;
26438
26439   /* Skip the first "all" entry.  */
26440   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
26441     if (streq (opt->name, name))
26442       {
26443         mcpu_cpu_opt = &opt->value;
26444         selected_cpu = opt->value;
26445         if (opt->canonical_name)
26446           strcpy (selected_cpu_name, opt->canonical_name);
26447         else
26448           {
26449             int i;
26450             for (i = 0; opt->name[i]; i++)
26451               selected_cpu_name[i] = TOUPPER (opt->name[i]);
26452
26453             selected_cpu_name[i] = 0;
26454           }
26455         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26456         *input_line_pointer = saved_char;
26457         demand_empty_rest_of_line ();
26458         return;
26459       }
26460   as_bad (_("unknown cpu `%s'"), name);
26461   *input_line_pointer = saved_char;
26462   ignore_rest_of_line ();
26463 }
26464
26465
26466 /* Parse a .arch directive.  */
26467
26468 static void
26469 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
26470 {
26471   const struct arm_arch_option_table *opt;
26472   char saved_char;
26473   char *name;
26474
26475   name = input_line_pointer;
26476   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26477     input_line_pointer++;
26478   saved_char = *input_line_pointer;
26479   *input_line_pointer = 0;
26480
26481   /* Skip the first "all" entry.  */
26482   for (opt = arm_archs + 1; opt->name != NULL; opt++)
26483     if (streq (opt->name, name))
26484       {
26485         mcpu_cpu_opt = &opt->value;
26486         selected_cpu = opt->value;
26487         strcpy (selected_cpu_name, opt->name);
26488         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26489         *input_line_pointer = saved_char;
26490         demand_empty_rest_of_line ();
26491         return;
26492       }
26493
26494   as_bad (_("unknown architecture `%s'\n"), name);
26495   *input_line_pointer = saved_char;
26496   ignore_rest_of_line ();
26497 }
26498
26499
26500 /* Parse a .object_arch directive.  */
26501
26502 static void
26503 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
26504 {
26505   const struct arm_arch_option_table *opt;
26506   char saved_char;
26507   char *name;
26508
26509   name = input_line_pointer;
26510   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26511     input_line_pointer++;
26512   saved_char = *input_line_pointer;
26513   *input_line_pointer = 0;
26514
26515   /* Skip the first "all" entry.  */
26516   for (opt = arm_archs + 1; opt->name != NULL; opt++)
26517     if (streq (opt->name, name))
26518       {
26519         object_arch = &opt->value;
26520         *input_line_pointer = saved_char;
26521         demand_empty_rest_of_line ();
26522         return;
26523       }
26524
26525   as_bad (_("unknown architecture `%s'\n"), name);
26526   *input_line_pointer = saved_char;
26527   ignore_rest_of_line ();
26528 }
26529
26530 /* Parse a .arch_extension directive.  */
26531
26532 static void
26533 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
26534 {
26535   const struct arm_option_extension_value_table *opt;
26536   const arm_feature_set arm_any = ARM_ANY;
26537   char saved_char;
26538   char *name;
26539   int adding_value = 1;
26540
26541   name = input_line_pointer;
26542   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26543     input_line_pointer++;
26544   saved_char = *input_line_pointer;
26545   *input_line_pointer = 0;
26546
26547   if (strlen (name) >= 2
26548       && strncmp (name, "no", 2) == 0)
26549     {
26550       adding_value = 0;
26551       name += 2;
26552     }
26553
26554   for (opt = arm_extensions; opt->name != NULL; opt++)
26555     if (streq (opt->name, name))
26556       {
26557         int i, nb_allowed_archs =
26558           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
26559         for (i = 0; i < nb_allowed_archs; i++)
26560           {
26561             /* Empty entry.  */
26562             if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
26563               continue;
26564             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *mcpu_cpu_opt))
26565               break;
26566           }
26567
26568         if (i == nb_allowed_archs)
26569           {
26570             as_bad (_("architectural extension `%s' is not allowed for the "
26571                       "current base architecture"), name);
26572             break;
26573           }
26574
26575         if (adding_value)
26576           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu,
26577                                   opt->merge_value);
26578         else
26579           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->clear_value);
26580
26581         mcpu_cpu_opt = &selected_cpu;
26582         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26583         *input_line_pointer = saved_char;
26584         demand_empty_rest_of_line ();
26585         return;
26586       }
26587
26588   if (opt->name == NULL)
26589     as_bad (_("unknown architecture extension `%s'\n"), name);
26590
26591   *input_line_pointer = saved_char;
26592   ignore_rest_of_line ();
26593 }
26594
26595 /* Parse a .fpu directive.  */
26596
26597 static void
26598 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
26599 {
26600   const struct arm_option_fpu_value_table *opt;
26601   char saved_char;
26602   char *name;
26603
26604   name = input_line_pointer;
26605   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26606     input_line_pointer++;
26607   saved_char = *input_line_pointer;
26608   *input_line_pointer = 0;
26609
26610   for (opt = arm_fpus; opt->name != NULL; opt++)
26611     if (streq (opt->name, name))
26612       {
26613         mfpu_opt = &opt->value;
26614         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26615         *input_line_pointer = saved_char;
26616         demand_empty_rest_of_line ();
26617         return;
26618       }
26619
26620   as_bad (_("unknown floating point format `%s'\n"), name);
26621   *input_line_pointer = saved_char;
26622   ignore_rest_of_line ();
26623 }
26624
26625 /* Copy symbol information.  */
26626
26627 void
26628 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
26629 {
26630   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
26631 }
26632
26633 #ifdef OBJ_ELF
26634 /* Given a symbolic attribute NAME, return the proper integer value.
26635    Returns -1 if the attribute is not known.  */
26636
26637 int
26638 arm_convert_symbolic_attribute (const char *name)
26639 {
26640   static const struct
26641   {
26642     const char * name;
26643     const int    tag;
26644   }
26645   attribute_table[] =
26646     {
26647       /* When you modify this table you should
26648          also modify the list in doc/c-arm.texi.  */
26649 #define T(tag) {#tag, tag}
26650       T (Tag_CPU_raw_name),
26651       T (Tag_CPU_name),
26652       T (Tag_CPU_arch),
26653       T (Tag_CPU_arch_profile),
26654       T (Tag_ARM_ISA_use),
26655       T (Tag_THUMB_ISA_use),
26656       T (Tag_FP_arch),
26657       T (Tag_VFP_arch),
26658       T (Tag_WMMX_arch),
26659       T (Tag_Advanced_SIMD_arch),
26660       T (Tag_PCS_config),
26661       T (Tag_ABI_PCS_R9_use),
26662       T (Tag_ABI_PCS_RW_data),
26663       T (Tag_ABI_PCS_RO_data),
26664       T (Tag_ABI_PCS_GOT_use),
26665       T (Tag_ABI_PCS_wchar_t),
26666       T (Tag_ABI_FP_rounding),
26667       T (Tag_ABI_FP_denormal),
26668       T (Tag_ABI_FP_exceptions),
26669       T (Tag_ABI_FP_user_exceptions),
26670       T (Tag_ABI_FP_number_model),
26671       T (Tag_ABI_align_needed),
26672       T (Tag_ABI_align8_needed),
26673       T (Tag_ABI_align_preserved),
26674       T (Tag_ABI_align8_preserved),
26675       T (Tag_ABI_enum_size),
26676       T (Tag_ABI_HardFP_use),
26677       T (Tag_ABI_VFP_args),
26678       T (Tag_ABI_WMMX_args),
26679       T (Tag_ABI_optimization_goals),
26680       T (Tag_ABI_FP_optimization_goals),
26681       T (Tag_compatibility),
26682       T (Tag_CPU_unaligned_access),
26683       T (Tag_FP_HP_extension),
26684       T (Tag_VFP_HP_extension),
26685       T (Tag_ABI_FP_16bit_format),
26686       T (Tag_MPextension_use),
26687       T (Tag_DIV_use),
26688       T (Tag_nodefaults),
26689       T (Tag_also_compatible_with),
26690       T (Tag_conformance),
26691       T (Tag_T2EE_use),
26692       T (Tag_Virtualization_use),
26693       T (Tag_DSP_extension),
26694       /* We deliberately do not include Tag_MPextension_use_legacy.  */
26695 #undef T
26696     };
26697   unsigned int i;
26698
26699   if (name == NULL)
26700     return -1;
26701
26702   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
26703     if (streq (name, attribute_table[i].name))
26704       return attribute_table[i].tag;
26705
26706   return -1;
26707 }
26708
26709
26710 /* Apply sym value for relocations only in the case that they are for
26711    local symbols in the same segment as the fixup and you have the
26712    respective architectural feature for blx and simple switches.  */
26713 int
26714 arm_apply_sym_value (struct fix * fixP, segT this_seg)
26715 {
26716   if (fixP->fx_addsy
26717       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26718       /* PR 17444: If the local symbol is in a different section then a reloc
26719          will always be generated for it, so applying the symbol value now
26720          will result in a double offset being stored in the relocation.  */
26721       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
26722       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
26723     {
26724       switch (fixP->fx_r_type)
26725         {
26726         case BFD_RELOC_ARM_PCREL_BLX:
26727         case BFD_RELOC_THUMB_PCREL_BRANCH23:
26728           if (ARM_IS_FUNC (fixP->fx_addsy))
26729             return 1;
26730           break;
26731
26732         case BFD_RELOC_ARM_PCREL_CALL:
26733         case BFD_RELOC_THUMB_PCREL_BLX:
26734           if (THUMB_IS_FUNC (fixP->fx_addsy))
26735             return 1;
26736           break;
26737
26738         default:
26739           break;
26740         }
26741
26742     }
26743   return 0;
26744 }
26745 #endif /* OBJ_ELF */