[Arm] Add CSDB instruction
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994-2018 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 arm_feature_set *        dyn_mcpu_ext_opt = NULL;
151 static const arm_feature_set *  mcpu_fpu_opt = NULL;
152 static const arm_feature_set *  march_cpu_opt = NULL;
153 static arm_feature_set *        dyn_march_ext_opt = NULL;
154 static const arm_feature_set *  march_fpu_opt = NULL;
155 static const arm_feature_set *  mfpu_opt = NULL;
156 static const arm_feature_set *  object_arch = NULL;
157
158 /* Constants for known architecture features.  */
159 static const arm_feature_set fpu_default = FPU_DEFAULT;
160 static const arm_feature_set fpu_arch_vfp_v1 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V1;
161 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
162 static const arm_feature_set fpu_arch_vfp_v3 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V3;
163 static const arm_feature_set fpu_arch_neon_v1 ATTRIBUTE_UNUSED = FPU_ARCH_NEON_V1;
164 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
165 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
166 #ifdef OBJ_ELF
167 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
168 #endif
169 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
170
171 #ifdef CPU_DEFAULT
172 static const arm_feature_set cpu_default = CPU_DEFAULT;
173 #endif
174
175 static const arm_feature_set arm_ext_v1 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
176 static const arm_feature_set arm_ext_v2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V2);
177 static const arm_feature_set arm_ext_v2s = ARM_FEATURE_CORE_LOW (ARM_EXT_V2S);
178 static const arm_feature_set arm_ext_v3 = ARM_FEATURE_CORE_LOW (ARM_EXT_V3);
179 static const arm_feature_set arm_ext_v3m = ARM_FEATURE_CORE_LOW (ARM_EXT_V3M);
180 static const arm_feature_set arm_ext_v4 = ARM_FEATURE_CORE_LOW (ARM_EXT_V4);
181 static const arm_feature_set arm_ext_v4t = ARM_FEATURE_CORE_LOW (ARM_EXT_V4T);
182 static const arm_feature_set arm_ext_v5 = ARM_FEATURE_CORE_LOW (ARM_EXT_V5);
183 static const arm_feature_set arm_ext_v4t_5 =
184   ARM_FEATURE_CORE_LOW (ARM_EXT_V4T | ARM_EXT_V5);
185 static const arm_feature_set arm_ext_v5t = ARM_FEATURE_CORE_LOW (ARM_EXT_V5T);
186 static const arm_feature_set arm_ext_v5e = ARM_FEATURE_CORE_LOW (ARM_EXT_V5E);
187 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP);
188 static const arm_feature_set arm_ext_v5j = ARM_FEATURE_CORE_LOW (ARM_EXT_V5J);
189 static const arm_feature_set arm_ext_v6 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6);
190 static const arm_feature_set arm_ext_v6k = ARM_FEATURE_CORE_LOW (ARM_EXT_V6K);
191 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6T2);
192 static const arm_feature_set arm_ext_v6_notm =
193   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_NOTM);
194 static const arm_feature_set arm_ext_v6_dsp =
195   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_DSP);
196 static const arm_feature_set arm_ext_barrier =
197   ARM_FEATURE_CORE_LOW (ARM_EXT_BARRIER);
198 static const arm_feature_set arm_ext_msr =
199   ARM_FEATURE_CORE_LOW (ARM_EXT_THUMB_MSR);
200 static const arm_feature_set arm_ext_div = ARM_FEATURE_CORE_LOW (ARM_EXT_DIV);
201 static const arm_feature_set arm_ext_v7 = ARM_FEATURE_CORE_LOW (ARM_EXT_V7);
202 static const arm_feature_set arm_ext_v7a = ARM_FEATURE_CORE_LOW (ARM_EXT_V7A);
203 static const arm_feature_set arm_ext_v7r = ARM_FEATURE_CORE_LOW (ARM_EXT_V7R);
204 #ifdef OBJ_ELF
205 static const arm_feature_set ATTRIBUTE_UNUSED arm_ext_v7m = ARM_FEATURE_CORE_LOW (ARM_EXT_V7M);
206 #endif
207 static const arm_feature_set arm_ext_v8 = ARM_FEATURE_CORE_LOW (ARM_EXT_V8);
208 static const arm_feature_set arm_ext_m =
209   ARM_FEATURE_CORE (ARM_EXT_V6M | ARM_EXT_V7M,
210                     ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
211 static const arm_feature_set arm_ext_mp = ARM_FEATURE_CORE_LOW (ARM_EXT_MP);
212 static const arm_feature_set arm_ext_sec = ARM_FEATURE_CORE_LOW (ARM_EXT_SEC);
213 static const arm_feature_set arm_ext_os = ARM_FEATURE_CORE_LOW (ARM_EXT_OS);
214 static const arm_feature_set arm_ext_adiv = ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV);
215 static const arm_feature_set arm_ext_virt = ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT);
216 static const arm_feature_set arm_ext_pan = ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN);
217 static const arm_feature_set arm_ext_v8m = ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M);
218 static const arm_feature_set arm_ext_v8m_main =
219   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M_MAIN);
220 /* Instructions in ARMv8-M only found in M profile architectures.  */
221 static const arm_feature_set arm_ext_v8m_m_only =
222   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
223 static const arm_feature_set arm_ext_v6t2_v8m =
224   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V6T2_V8M);
225 /* Instructions shared between ARMv8-A and ARMv8-M.  */
226 static const arm_feature_set arm_ext_atomics =
227   ARM_FEATURE_CORE_HIGH (ARM_EXT2_ATOMICS);
228 #ifdef OBJ_ELF
229 /* DSP instructions Tag_DSP_extension refers to.  */
230 static const arm_feature_set arm_ext_dsp =
231   ARM_FEATURE_CORE_LOW (ARM_EXT_V5E | ARM_EXT_V5ExP | ARM_EXT_V6_DSP);
232 #endif
233 static const arm_feature_set arm_ext_ras =
234   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS);
235 /* FP16 instructions.  */
236 static const arm_feature_set arm_ext_fp16 =
237   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST);
238 static const arm_feature_set arm_ext_fp16_fml =
239   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_FML);
240 static const arm_feature_set arm_ext_v8_2 =
241   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_2A);
242 static const arm_feature_set arm_ext_v8_3 =
243   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_3A);
244
245 static const arm_feature_set arm_arch_any = ARM_ANY;
246 #ifdef OBJ_ELF
247 static const arm_feature_set fpu_any = FPU_ANY;
248 #endif
249 static const arm_feature_set arm_arch_full ATTRIBUTE_UNUSED = ARM_FEATURE (-1, -1, -1);
250 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
251 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
252
253 static const arm_feature_set arm_cext_iwmmxt2 =
254   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
255 static const arm_feature_set arm_cext_iwmmxt =
256   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
257 static const arm_feature_set arm_cext_xscale =
258   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
259 static const arm_feature_set arm_cext_maverick =
260   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
261 static const arm_feature_set fpu_fpa_ext_v1 =
262   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
263 static const arm_feature_set fpu_fpa_ext_v2 =
264   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
265 static const arm_feature_set fpu_vfp_ext_v1xd =
266   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
267 static const arm_feature_set fpu_vfp_ext_v1 =
268   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
269 static const arm_feature_set fpu_vfp_ext_v2 =
270   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
271 static const arm_feature_set fpu_vfp_ext_v3xd =
272   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
273 static const arm_feature_set fpu_vfp_ext_v3 =
274   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
275 static const arm_feature_set fpu_vfp_ext_d32 =
276   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
277 static const arm_feature_set fpu_neon_ext_v1 =
278   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
279 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
280   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
281 #ifdef OBJ_ELF
282 static const arm_feature_set fpu_vfp_fp16 =
283   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
284 static const arm_feature_set fpu_neon_ext_fma =
285   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
286 #endif
287 static const arm_feature_set fpu_vfp_ext_fma =
288   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
289 static const arm_feature_set fpu_vfp_ext_armv8 =
290   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
291 static const arm_feature_set fpu_vfp_ext_armv8xd =
292   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
293 static const arm_feature_set fpu_neon_ext_armv8 =
294   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
295 static const arm_feature_set fpu_crypto_ext_armv8 =
296   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
297 static const arm_feature_set crc_ext_armv8 =
298   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
299 static const arm_feature_set fpu_neon_ext_v8_1 =
300   ARM_FEATURE_COPROC (FPU_NEON_EXT_RDMA);
301 static const arm_feature_set fpu_neon_ext_dotprod =
302   ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD);
303
304 static int mfloat_abi_opt = -1;
305 /* Record user cpu selection for object attributes.  */
306 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
307 /* Must be long enough to hold any of the names in arm_cpus.  */
308 static char selected_cpu_name[20];
309
310 extern FLONUM_TYPE generic_floating_point_number;
311
312 /* Return if no cpu was selected on command-line.  */
313 static bfd_boolean
314 no_cpu_selected (void)
315 {
316   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
317 }
318
319 #ifdef OBJ_ELF
320 # ifdef EABI_DEFAULT
321 static int meabi_flags = EABI_DEFAULT;
322 # else
323 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
324 # endif
325
326 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
327
328 bfd_boolean
329 arm_is_eabi (void)
330 {
331   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
332 }
333 #endif
334
335 #ifdef OBJ_ELF
336 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
337 symbolS * GOT_symbol;
338 #endif
339
340 /* 0: assemble for ARM,
341    1: assemble for Thumb,
342    2: assemble for Thumb even though target CPU does not support thumb
343       instructions.  */
344 static int thumb_mode = 0;
345 /* A value distinct from the possible values for thumb_mode that we
346    can use to record whether thumb_mode has been copied into the
347    tc_frag_data field of a frag.  */
348 #define MODE_RECORDED (1 << 4)
349
350 /* Specifies the intrinsic IT insn behavior mode.  */
351 enum implicit_it_mode
352 {
353   IMPLICIT_IT_MODE_NEVER  = 0x00,
354   IMPLICIT_IT_MODE_ARM    = 0x01,
355   IMPLICIT_IT_MODE_THUMB  = 0x02,
356   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
357 };
358 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
359
360 /* If unified_syntax is true, we are processing the new unified
361    ARM/Thumb syntax.  Important differences from the old ARM mode:
362
363      - Immediate operands do not require a # prefix.
364      - Conditional affixes always appear at the end of the
365        instruction.  (For backward compatibility, those instructions
366        that formerly had them in the middle, continue to accept them
367        there.)
368      - The IT instruction may appear, and if it does is validated
369        against subsequent conditional affixes.  It does not generate
370        machine code.
371
372    Important differences from the old Thumb mode:
373
374      - Immediate operands do not require a # prefix.
375      - Most of the V6T2 instructions are only available in unified mode.
376      - The .N and .W suffixes are recognized and honored (it is an error
377        if they cannot be honored).
378      - All instructions set the flags if and only if they have an 's' affix.
379      - Conditional affixes may be used.  They are validated against
380        preceding IT instructions.  Unlike ARM mode, you cannot use a
381        conditional affix except in the scope of an IT instruction.  */
382
383 static bfd_boolean unified_syntax = FALSE;
384
385 /* An immediate operand can start with #, and ld*, st*, pld operands
386    can contain [ and ].  We need to tell APP not to elide whitespace
387    before a [, which can appear as the first operand for pld.
388    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
389 const char arm_symbol_chars[] = "#[]{}";
390
391 enum neon_el_type
392 {
393   NT_invtype,
394   NT_untyped,
395   NT_integer,
396   NT_float,
397   NT_poly,
398   NT_signed,
399   NT_unsigned
400 };
401
402 struct neon_type_el
403 {
404   enum neon_el_type type;
405   unsigned size;
406 };
407
408 #define NEON_MAX_TYPE_ELS 4
409
410 struct neon_type
411 {
412   struct neon_type_el el[NEON_MAX_TYPE_ELS];
413   unsigned elems;
414 };
415
416 enum it_instruction_type
417 {
418    OUTSIDE_IT_INSN,
419    INSIDE_IT_INSN,
420    INSIDE_IT_LAST_INSN,
421    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
422                               if inside, should be the last one.  */
423    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
424                               i.e. BKPT and NOP.  */
425    IT_INSN                 /* The IT insn has been parsed.  */
426 };
427
428 /* The maximum number of operands we need.  */
429 #define ARM_IT_MAX_OPERANDS 6
430
431 struct arm_it
432 {
433   const char *  error;
434   unsigned long instruction;
435   int           size;
436   int           size_req;
437   int           cond;
438   /* "uncond_value" is set to the value in place of the conditional field in
439      unconditional versions of the instruction, or -1 if nothing is
440      appropriate.  */
441   int           uncond_value;
442   struct neon_type vectype;
443   /* This does not indicate an actual NEON instruction, only that
444      the mnemonic accepts neon-style type suffixes.  */
445   int           is_neon;
446   /* Set to the opcode if the instruction needs relaxation.
447      Zero if the instruction is not relaxed.  */
448   unsigned long relax;
449   struct
450   {
451     bfd_reloc_code_real_type type;
452     expressionS              exp;
453     int                      pc_rel;
454   } reloc;
455
456   enum it_instruction_type it_insn_type;
457
458   struct
459   {
460     unsigned reg;
461     signed int imm;
462     struct neon_type_el vectype;
463     unsigned present    : 1;  /* Operand present.  */
464     unsigned isreg      : 1;  /* Operand was a register.  */
465     unsigned immisreg   : 1;  /* .imm field is a second register.  */
466     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
467     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
468     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
469     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
470        instructions. This allows us to disambiguate ARM <-> vector insns.  */
471     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
472     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
473     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
474     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
475     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
476     unsigned writeback  : 1;  /* Operand has trailing !  */
477     unsigned preind     : 1;  /* Preindexed address.  */
478     unsigned postind    : 1;  /* Postindexed address.  */
479     unsigned negative   : 1;  /* Index register was negated.  */
480     unsigned shifted    : 1;  /* Shift applied to operation.  */
481     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
482   } operands[ARM_IT_MAX_OPERANDS];
483 };
484
485 static struct arm_it inst;
486
487 #define NUM_FLOAT_VALS 8
488
489 const char * fp_const[] =
490 {
491   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
492 };
493
494 /* Number of littlenums required to hold an extended precision number.  */
495 #define MAX_LITTLENUMS 6
496
497 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
498
499 #define FAIL    (-1)
500 #define SUCCESS (0)
501
502 #define SUFF_S 1
503 #define SUFF_D 2
504 #define SUFF_E 3
505 #define SUFF_P 4
506
507 #define CP_T_X   0x00008000
508 #define CP_T_Y   0x00400000
509
510 #define CONDS_BIT        0x00100000
511 #define LOAD_BIT         0x00100000
512
513 #define DOUBLE_LOAD_FLAG 0x00000001
514
515 struct asm_cond
516 {
517   const char *   template_name;
518   unsigned long  value;
519 };
520
521 #define COND_ALWAYS 0xE
522
523 struct asm_psr
524 {
525   const char *   template_name;
526   unsigned long  field;
527 };
528
529 struct asm_barrier_opt
530 {
531   const char *    template_name;
532   unsigned long   value;
533   const arm_feature_set arch;
534 };
535
536 /* The bit that distinguishes CPSR and SPSR.  */
537 #define SPSR_BIT   (1 << 22)
538
539 /* The individual PSR flag bits.  */
540 #define PSR_c   (1 << 16)
541 #define PSR_x   (1 << 17)
542 #define PSR_s   (1 << 18)
543 #define PSR_f   (1 << 19)
544
545 struct reloc_entry
546 {
547   const char *              name;
548   bfd_reloc_code_real_type  reloc;
549 };
550
551 enum vfp_reg_pos
552 {
553   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
554   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
555 };
556
557 enum vfp_ldstm_type
558 {
559   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
560 };
561
562 /* Bits for DEFINED field in neon_typed_alias.  */
563 #define NTA_HASTYPE  1
564 #define NTA_HASINDEX 2
565
566 struct neon_typed_alias
567 {
568   unsigned char        defined;
569   unsigned char        index;
570   struct neon_type_el  eltype;
571 };
572
573 /* ARM register categories.  This includes coprocessor numbers and various
574    architecture extensions' registers.  Each entry should have an error message
575    in reg_expected_msgs below.  */
576 enum arm_reg_type
577 {
578   REG_TYPE_RN,
579   REG_TYPE_CP,
580   REG_TYPE_CN,
581   REG_TYPE_FN,
582   REG_TYPE_VFS,
583   REG_TYPE_VFD,
584   REG_TYPE_NQ,
585   REG_TYPE_VFSD,
586   REG_TYPE_NDQ,
587   REG_TYPE_NSD,
588   REG_TYPE_NSDQ,
589   REG_TYPE_VFC,
590   REG_TYPE_MVF,
591   REG_TYPE_MVD,
592   REG_TYPE_MVFX,
593   REG_TYPE_MVDX,
594   REG_TYPE_MVAX,
595   REG_TYPE_DSPSC,
596   REG_TYPE_MMXWR,
597   REG_TYPE_MMXWC,
598   REG_TYPE_MMXWCG,
599   REG_TYPE_XSCALE,
600   REG_TYPE_RNB
601 };
602
603 /* Structure for a hash table entry for a register.
604    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
605    information which states whether a vector type or index is specified (for a
606    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
607 struct reg_entry
608 {
609   const char *               name;
610   unsigned int               number;
611   unsigned char              type;
612   unsigned char              builtin;
613   struct neon_typed_alias *  neon;
614 };
615
616 /* Diagnostics used when we don't get a register of the expected type.  */
617 const char * const reg_expected_msgs[] =
618 {
619   [REG_TYPE_RN]     = N_("ARM register expected"),
620   [REG_TYPE_CP]     = N_("bad or missing co-processor number"),
621   [REG_TYPE_CN]     = N_("co-processor register expected"),
622   [REG_TYPE_FN]     = N_("FPA register expected"),
623   [REG_TYPE_VFS]    = N_("VFP single precision register expected"),
624   [REG_TYPE_VFD]    = N_("VFP/Neon double precision register expected"),
625   [REG_TYPE_NQ]     = N_("Neon quad precision register expected"),
626   [REG_TYPE_VFSD]   = N_("VFP single or double precision register expected"),
627   [REG_TYPE_NDQ]    = N_("Neon double or quad precision register expected"),
628   [REG_TYPE_NSD]    = N_("Neon single or double precision register expected"),
629   [REG_TYPE_NSDQ]   = N_("VFP single, double or Neon quad precision register"
630                          " expected"),
631   [REG_TYPE_VFC]    = N_("VFP system register expected"),
632   [REG_TYPE_MVF]    = N_("Maverick MVF register expected"),
633   [REG_TYPE_MVD]    = N_("Maverick MVD register expected"),
634   [REG_TYPE_MVFX]   = N_("Maverick MVFX register expected"),
635   [REG_TYPE_MVDX]   = N_("Maverick MVDX register expected"),
636   [REG_TYPE_MVAX]   = N_("Maverick MVAX register expected"),
637   [REG_TYPE_DSPSC]  = N_("Maverick DSPSC register expected"),
638   [REG_TYPE_MMXWR]  = N_("iWMMXt data register expected"),
639   [REG_TYPE_MMXWC]  = N_("iWMMXt control register expected"),
640   [REG_TYPE_MMXWCG] = N_("iWMMXt scalar register expected"),
641   [REG_TYPE_XSCALE] = N_("XScale accumulator register expected"),
642   [REG_TYPE_RNB]    = N_("")
643 };
644
645 /* Some well known registers that we refer to directly elsewhere.  */
646 #define REG_R12 12
647 #define REG_SP  13
648 #define REG_LR  14
649 #define REG_PC  15
650
651 /* ARM instructions take 4bytes in the object file, Thumb instructions
652    take 2:  */
653 #define INSN_SIZE       4
654
655 struct asm_opcode
656 {
657   /* Basic string to match.  */
658   const char * template_name;
659
660   /* Parameters to instruction.  */
661   unsigned int operands[8];
662
663   /* Conditional tag - see opcode_lookup.  */
664   unsigned int tag : 4;
665
666   /* Basic instruction code.  */
667   unsigned int avalue : 28;
668
669   /* Thumb-format instruction code.  */
670   unsigned int tvalue;
671
672   /* Which architecture variant provides this instruction.  */
673   const arm_feature_set * avariant;
674   const arm_feature_set * tvariant;
675
676   /* Function to call to encode instruction in ARM format.  */
677   void (* aencode) (void);
678
679   /* Function to call to encode instruction in Thumb format.  */
680   void (* tencode) (void);
681 };
682
683 /* Defines for various bits that we will want to toggle.  */
684 #define INST_IMMEDIATE  0x02000000
685 #define OFFSET_REG      0x02000000
686 #define HWOFFSET_IMM    0x00400000
687 #define SHIFT_BY_REG    0x00000010
688 #define PRE_INDEX       0x01000000
689 #define INDEX_UP        0x00800000
690 #define WRITE_BACK      0x00200000
691 #define LDM_TYPE_2_OR_3 0x00400000
692 #define CPSI_MMOD       0x00020000
693
694 #define LITERAL_MASK    0xf000f000
695 #define OPCODE_MASK     0xfe1fffff
696 #define V4_STR_BIT      0x00000020
697 #define VLDR_VMOV_SAME  0x0040f000
698
699 #define T2_SUBS_PC_LR   0xf3de8f00
700
701 #define DATA_OP_SHIFT   21
702 #define SBIT_SHIFT      20
703
704 #define T2_OPCODE_MASK  0xfe1fffff
705 #define T2_DATA_OP_SHIFT 21
706 #define T2_SBIT_SHIFT    20
707
708 #define A_COND_MASK         0xf0000000
709 #define A_PUSH_POP_OP_MASK  0x0fff0000
710
711 /* Opcodes for pushing/poping registers to/from the stack.  */
712 #define A1_OPCODE_PUSH    0x092d0000
713 #define A2_OPCODE_PUSH    0x052d0004
714 #define A2_OPCODE_POP     0x049d0004
715
716 /* Codes to distinguish the arithmetic instructions.  */
717 #define OPCODE_AND      0
718 #define OPCODE_EOR      1
719 #define OPCODE_SUB      2
720 #define OPCODE_RSB      3
721 #define OPCODE_ADD      4
722 #define OPCODE_ADC      5
723 #define OPCODE_SBC      6
724 #define OPCODE_RSC      7
725 #define OPCODE_TST      8
726 #define OPCODE_TEQ      9
727 #define OPCODE_CMP      10
728 #define OPCODE_CMN      11
729 #define OPCODE_ORR      12
730 #define OPCODE_MOV      13
731 #define OPCODE_BIC      14
732 #define OPCODE_MVN      15
733
734 #define T2_OPCODE_AND   0
735 #define T2_OPCODE_BIC   1
736 #define T2_OPCODE_ORR   2
737 #define T2_OPCODE_ORN   3
738 #define T2_OPCODE_EOR   4
739 #define T2_OPCODE_ADD   8
740 #define T2_OPCODE_ADC   10
741 #define T2_OPCODE_SBC   11
742 #define T2_OPCODE_SUB   13
743 #define T2_OPCODE_RSB   14
744
745 #define T_OPCODE_MUL 0x4340
746 #define T_OPCODE_TST 0x4200
747 #define T_OPCODE_CMN 0x42c0
748 #define T_OPCODE_NEG 0x4240
749 #define T_OPCODE_MVN 0x43c0
750
751 #define T_OPCODE_ADD_R3 0x1800
752 #define T_OPCODE_SUB_R3 0x1a00
753 #define T_OPCODE_ADD_HI 0x4400
754 #define T_OPCODE_ADD_ST 0xb000
755 #define T_OPCODE_SUB_ST 0xb080
756 #define T_OPCODE_ADD_SP 0xa800
757 #define T_OPCODE_ADD_PC 0xa000
758 #define T_OPCODE_ADD_I8 0x3000
759 #define T_OPCODE_SUB_I8 0x3800
760 #define T_OPCODE_ADD_I3 0x1c00
761 #define T_OPCODE_SUB_I3 0x1e00
762
763 #define T_OPCODE_ASR_R  0x4100
764 #define T_OPCODE_LSL_R  0x4080
765 #define T_OPCODE_LSR_R  0x40c0
766 #define T_OPCODE_ROR_R  0x41c0
767 #define T_OPCODE_ASR_I  0x1000
768 #define T_OPCODE_LSL_I  0x0000
769 #define T_OPCODE_LSR_I  0x0800
770
771 #define T_OPCODE_MOV_I8 0x2000
772 #define T_OPCODE_CMP_I8 0x2800
773 #define T_OPCODE_CMP_LR 0x4280
774 #define T_OPCODE_MOV_HR 0x4600
775 #define T_OPCODE_CMP_HR 0x4500
776
777 #define T_OPCODE_LDR_PC 0x4800
778 #define T_OPCODE_LDR_SP 0x9800
779 #define T_OPCODE_STR_SP 0x9000
780 #define T_OPCODE_LDR_IW 0x6800
781 #define T_OPCODE_STR_IW 0x6000
782 #define T_OPCODE_LDR_IH 0x8800
783 #define T_OPCODE_STR_IH 0x8000
784 #define T_OPCODE_LDR_IB 0x7800
785 #define T_OPCODE_STR_IB 0x7000
786 #define T_OPCODE_LDR_RW 0x5800
787 #define T_OPCODE_STR_RW 0x5000
788 #define T_OPCODE_LDR_RH 0x5a00
789 #define T_OPCODE_STR_RH 0x5200
790 #define T_OPCODE_LDR_RB 0x5c00
791 #define T_OPCODE_STR_RB 0x5400
792
793 #define T_OPCODE_PUSH   0xb400
794 #define T_OPCODE_POP    0xbc00
795
796 #define T_OPCODE_BRANCH 0xe000
797
798 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
799 #define THUMB_PP_PC_LR 0x0100
800 #define THUMB_LOAD_BIT 0x0800
801 #define THUMB2_LOAD_BIT 0x00100000
802
803 #define BAD_ARGS        _("bad arguments to instruction")
804 #define BAD_SP          _("r13 not allowed here")
805 #define BAD_PC          _("r15 not allowed here")
806 #define BAD_COND        _("instruction cannot be conditional")
807 #define BAD_OVERLAP     _("registers may not be the same")
808 #define BAD_HIREG       _("lo register required")
809 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
810 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
811 #define BAD_BRANCH      _("branch must be last instruction in IT block")
812 #define BAD_NOT_IT      _("instruction not allowed in IT block")
813 #define BAD_FPU         _("selected FPU does not support instruction")
814 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
815 #define BAD_IT_COND     _("incorrect condition in IT block")
816 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
817 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
818 #define BAD_PC_ADDRESSING \
819         _("cannot use register index with PC-relative addressing")
820 #define BAD_PC_WRITEBACK \
821         _("cannot use writeback with PC-relative addressing")
822 #define BAD_RANGE       _("branch out of range")
823 #define BAD_FP16        _("selected processor does not support fp16 instruction")
824 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
825 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
826
827 static struct hash_control * arm_ops_hsh;
828 static struct hash_control * arm_cond_hsh;
829 static struct hash_control * arm_shift_hsh;
830 static struct hash_control * arm_psr_hsh;
831 static struct hash_control * arm_v7m_psr_hsh;
832 static struct hash_control * arm_reg_hsh;
833 static struct hash_control * arm_reloc_hsh;
834 static struct hash_control * arm_barrier_opt_hsh;
835
836 /* Stuff needed to resolve the label ambiguity
837    As:
838      ...
839      label:   <insn>
840    may differ from:
841      ...
842      label:
843               <insn>  */
844
845 symbolS *  last_label_seen;
846 static int label_is_thumb_function_name = FALSE;
847
848 /* Literal pool structure.  Held on a per-section
849    and per-sub-section basis.  */
850
851 #define MAX_LITERAL_POOL_SIZE 1024
852 typedef struct literal_pool
853 {
854   expressionS            literals [MAX_LITERAL_POOL_SIZE];
855   unsigned int           next_free_entry;
856   unsigned int           id;
857   symbolS *              symbol;
858   segT                   section;
859   subsegT                sub_section;
860 #ifdef OBJ_ELF
861   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
862 #endif
863   struct literal_pool *  next;
864   unsigned int           alignment;
865 } literal_pool;
866
867 /* Pointer to a linked list of literal pools.  */
868 literal_pool * list_of_pools = NULL;
869
870 typedef enum asmfunc_states
871 {
872   OUTSIDE_ASMFUNC,
873   WAITING_ASMFUNC_NAME,
874   WAITING_ENDASMFUNC
875 } asmfunc_states;
876
877 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
878
879 #ifdef OBJ_ELF
880 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
881 #else
882 static struct current_it now_it;
883 #endif
884
885 static inline int
886 now_it_compatible (int cond)
887 {
888   return (cond & ~1) == (now_it.cc & ~1);
889 }
890
891 static inline int
892 conditional_insn (void)
893 {
894   return inst.cond != COND_ALWAYS;
895 }
896
897 static int in_it_block (void);
898
899 static int handle_it_state (void);
900
901 static void force_automatic_it_block_close (void);
902
903 static void it_fsm_post_encode (void);
904
905 #define set_it_insn_type(type)                  \
906   do                                            \
907     {                                           \
908       inst.it_insn_type = type;                 \
909       if (handle_it_state () == FAIL)           \
910         return;                                 \
911     }                                           \
912   while (0)
913
914 #define set_it_insn_type_nonvoid(type, failret) \
915   do                                            \
916     {                                           \
917       inst.it_insn_type = type;                 \
918       if (handle_it_state () == FAIL)           \
919         return failret;                         \
920     }                                           \
921   while(0)
922
923 #define set_it_insn_type_last()                         \
924   do                                                    \
925     {                                                   \
926       if (inst.cond == COND_ALWAYS)                     \
927         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
928       else                                              \
929         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
930     }                                                   \
931   while (0)
932
933 /* Pure syntax.  */
934
935 /* This array holds the chars that always start a comment.  If the
936    pre-processor is disabled, these aren't very useful.  */
937 char arm_comment_chars[] = "@";
938
939 /* This array holds the chars that only start a comment at the beginning of
940    a line.  If the line seems to have the form '# 123 filename'
941    .line and .file directives will appear in the pre-processed output.  */
942 /* Note that input_file.c hand checks for '#' at the beginning of the
943    first line of the input file.  This is because the compiler outputs
944    #NO_APP at the beginning of its output.  */
945 /* Also note that comments like this one will always work.  */
946 const char line_comment_chars[] = "#";
947
948 char arm_line_separator_chars[] = ";";
949
950 /* Chars that can be used to separate mant
951    from exp in floating point numbers.  */
952 const char EXP_CHARS[] = "eE";
953
954 /* Chars that mean this number is a floating point constant.  */
955 /* As in 0f12.456  */
956 /* or    0d1.2345e12  */
957
958 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
959
960 /* Prefix characters that indicate the start of an immediate
961    value.  */
962 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
963
964 /* Separator character handling.  */
965
966 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
967
968 static inline int
969 skip_past_char (char ** str, char c)
970 {
971   /* PR gas/14987: Allow for whitespace before the expected character.  */
972   skip_whitespace (*str);
973
974   if (**str == c)
975     {
976       (*str)++;
977       return SUCCESS;
978     }
979   else
980     return FAIL;
981 }
982
983 #define skip_past_comma(str) skip_past_char (str, ',')
984
985 /* Arithmetic expressions (possibly involving symbols).  */
986
987 /* Return TRUE if anything in the expression is a bignum.  */
988
989 static bfd_boolean
990 walk_no_bignums (symbolS * sp)
991 {
992   if (symbol_get_value_expression (sp)->X_op == O_big)
993     return TRUE;
994
995   if (symbol_get_value_expression (sp)->X_add_symbol)
996     {
997       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
998               || (symbol_get_value_expression (sp)->X_op_symbol
999                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
1000     }
1001
1002   return FALSE;
1003 }
1004
1005 static bfd_boolean in_my_get_expression = FALSE;
1006
1007 /* Third argument to my_get_expression.  */
1008 #define GE_NO_PREFIX 0
1009 #define GE_IMM_PREFIX 1
1010 #define GE_OPT_PREFIX 2
1011 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1012    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1013 #define GE_OPT_PREFIX_BIG 3
1014
1015 static int
1016 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1017 {
1018   char * save_in;
1019   segT   seg;
1020
1021   /* In unified syntax, all prefixes are optional.  */
1022   if (unified_syntax)
1023     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1024                   : GE_OPT_PREFIX;
1025
1026   switch (prefix_mode)
1027     {
1028     case GE_NO_PREFIX: break;
1029     case GE_IMM_PREFIX:
1030       if (!is_immediate_prefix (**str))
1031         {
1032           inst.error = _("immediate expression requires a # prefix");
1033           return FAIL;
1034         }
1035       (*str)++;
1036       break;
1037     case GE_OPT_PREFIX:
1038     case GE_OPT_PREFIX_BIG:
1039       if (is_immediate_prefix (**str))
1040         (*str)++;
1041       break;
1042     default:
1043       abort ();
1044     }
1045
1046   memset (ep, 0, sizeof (expressionS));
1047
1048   save_in = input_line_pointer;
1049   input_line_pointer = *str;
1050   in_my_get_expression = TRUE;
1051   seg = expression (ep);
1052   in_my_get_expression = FALSE;
1053
1054   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1055     {
1056       /* We found a bad or missing expression in md_operand().  */
1057       *str = input_line_pointer;
1058       input_line_pointer = save_in;
1059       if (inst.error == NULL)
1060         inst.error = (ep->X_op == O_absent
1061                       ? _("missing expression") :_("bad expression"));
1062       return 1;
1063     }
1064
1065 #ifdef OBJ_AOUT
1066   if (seg != absolute_section
1067       && seg != text_section
1068       && seg != data_section
1069       && seg != bss_section
1070       && seg != undefined_section)
1071     {
1072       inst.error = _("bad segment");
1073       *str = input_line_pointer;
1074       input_line_pointer = save_in;
1075       return 1;
1076     }
1077 #else
1078   (void) seg;
1079 #endif
1080
1081   /* Get rid of any bignums now, so that we don't generate an error for which
1082      we can't establish a line number later on.  Big numbers are never valid
1083      in instructions, which is where this routine is always called.  */
1084   if (prefix_mode != GE_OPT_PREFIX_BIG
1085       && (ep->X_op == O_big
1086           || (ep->X_add_symbol
1087               && (walk_no_bignums (ep->X_add_symbol)
1088                   || (ep->X_op_symbol
1089                       && walk_no_bignums (ep->X_op_symbol))))))
1090     {
1091       inst.error = _("invalid constant");
1092       *str = input_line_pointer;
1093       input_line_pointer = save_in;
1094       return 1;
1095     }
1096
1097   *str = input_line_pointer;
1098   input_line_pointer = save_in;
1099   return SUCCESS;
1100 }
1101
1102 /* Turn a string in input_line_pointer into a floating point constant
1103    of type TYPE, and store the appropriate bytes in *LITP.  The number
1104    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1105    returned, or NULL on OK.
1106
1107    Note that fp constants aren't represent in the normal way on the ARM.
1108    In big endian mode, things are as expected.  However, in little endian
1109    mode fp constants are big-endian word-wise, and little-endian byte-wise
1110    within the words.  For example, (double) 1.1 in big endian mode is
1111    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1112    the byte sequence 99 99 f1 3f 9a 99 99 99.
1113
1114    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1115
1116 const char *
1117 md_atof (int type, char * litP, int * sizeP)
1118 {
1119   int prec;
1120   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1121   char *t;
1122   int i;
1123
1124   switch (type)
1125     {
1126     case 'f':
1127     case 'F':
1128     case 's':
1129     case 'S':
1130       prec = 2;
1131       break;
1132
1133     case 'd':
1134     case 'D':
1135     case 'r':
1136     case 'R':
1137       prec = 4;
1138       break;
1139
1140     case 'x':
1141     case 'X':
1142       prec = 5;
1143       break;
1144
1145     case 'p':
1146     case 'P':
1147       prec = 5;
1148       break;
1149
1150     default:
1151       *sizeP = 0;
1152       return _("Unrecognized or unsupported floating point constant");
1153     }
1154
1155   t = atof_ieee (input_line_pointer, type, words);
1156   if (t)
1157     input_line_pointer = t;
1158   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1159
1160   if (target_big_endian)
1161     {
1162       for (i = 0; i < prec; i++)
1163         {
1164           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1165           litP += sizeof (LITTLENUM_TYPE);
1166         }
1167     }
1168   else
1169     {
1170       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1171         for (i = prec - 1; i >= 0; i--)
1172           {
1173             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1174             litP += sizeof (LITTLENUM_TYPE);
1175           }
1176       else
1177         /* For a 4 byte float the order of elements in `words' is 1 0.
1178            For an 8 byte float the order is 1 0 3 2.  */
1179         for (i = 0; i < prec; i += 2)
1180           {
1181             md_number_to_chars (litP, (valueT) words[i + 1],
1182                                 sizeof (LITTLENUM_TYPE));
1183             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1184                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1185             litP += 2 * sizeof (LITTLENUM_TYPE);
1186           }
1187     }
1188
1189   return NULL;
1190 }
1191
1192 /* We handle all bad expressions here, so that we can report the faulty
1193    instruction in the error message.  */
1194
1195 void
1196 md_operand (expressionS * exp)
1197 {
1198   if (in_my_get_expression)
1199     exp->X_op = O_illegal;
1200 }
1201
1202 /* Immediate values.  */
1203
1204 #ifdef OBJ_ELF
1205 /* Generic immediate-value read function for use in directives.
1206    Accepts anything that 'expression' can fold to a constant.
1207    *val receives the number.  */
1208
1209 static int
1210 immediate_for_directive (int *val)
1211 {
1212   expressionS exp;
1213   exp.X_op = O_illegal;
1214
1215   if (is_immediate_prefix (*input_line_pointer))
1216     {
1217       input_line_pointer++;
1218       expression (&exp);
1219     }
1220
1221   if (exp.X_op != O_constant)
1222     {
1223       as_bad (_("expected #constant"));
1224       ignore_rest_of_line ();
1225       return FAIL;
1226     }
1227   *val = exp.X_add_number;
1228   return SUCCESS;
1229 }
1230 #endif
1231
1232 /* Register parsing.  */
1233
1234 /* Generic register parser.  CCP points to what should be the
1235    beginning of a register name.  If it is indeed a valid register
1236    name, advance CCP over it and return the reg_entry structure;
1237    otherwise return NULL.  Does not issue diagnostics.  */
1238
1239 static struct reg_entry *
1240 arm_reg_parse_multi (char **ccp)
1241 {
1242   char *start = *ccp;
1243   char *p;
1244   struct reg_entry *reg;
1245
1246   skip_whitespace (start);
1247
1248 #ifdef REGISTER_PREFIX
1249   if (*start != REGISTER_PREFIX)
1250     return NULL;
1251   start++;
1252 #endif
1253 #ifdef OPTIONAL_REGISTER_PREFIX
1254   if (*start == OPTIONAL_REGISTER_PREFIX)
1255     start++;
1256 #endif
1257
1258   p = start;
1259   if (!ISALPHA (*p) || !is_name_beginner (*p))
1260     return NULL;
1261
1262   do
1263     p++;
1264   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1265
1266   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1267
1268   if (!reg)
1269     return NULL;
1270
1271   *ccp = p;
1272   return reg;
1273 }
1274
1275 static int
1276 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1277                     enum arm_reg_type type)
1278 {
1279   /* Alternative syntaxes are accepted for a few register classes.  */
1280   switch (type)
1281     {
1282     case REG_TYPE_MVF:
1283     case REG_TYPE_MVD:
1284     case REG_TYPE_MVFX:
1285     case REG_TYPE_MVDX:
1286       /* Generic coprocessor register names are allowed for these.  */
1287       if (reg && reg->type == REG_TYPE_CN)
1288         return reg->number;
1289       break;
1290
1291     case REG_TYPE_CP:
1292       /* For backward compatibility, a bare number is valid here.  */
1293       {
1294         unsigned long processor = strtoul (start, ccp, 10);
1295         if (*ccp != start && processor <= 15)
1296           return processor;
1297       }
1298       /* Fall through.  */
1299
1300     case REG_TYPE_MMXWC:
1301       /* WC includes WCG.  ??? I'm not sure this is true for all
1302          instructions that take WC registers.  */
1303       if (reg && reg->type == REG_TYPE_MMXWCG)
1304         return reg->number;
1305       break;
1306
1307     default:
1308       break;
1309     }
1310
1311   return FAIL;
1312 }
1313
1314 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1315    return value is the register number or FAIL.  */
1316
1317 static int
1318 arm_reg_parse (char **ccp, enum arm_reg_type type)
1319 {
1320   char *start = *ccp;
1321   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1322   int ret;
1323
1324   /* Do not allow a scalar (reg+index) to parse as a register.  */
1325   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1326     return FAIL;
1327
1328   if (reg && reg->type == type)
1329     return reg->number;
1330
1331   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1332     return ret;
1333
1334   *ccp = start;
1335   return FAIL;
1336 }
1337
1338 /* Parse a Neon type specifier. *STR should point at the leading '.'
1339    character. Does no verification at this stage that the type fits the opcode
1340    properly. E.g.,
1341
1342      .i32.i32.s16
1343      .s32.f32
1344      .u16
1345
1346    Can all be legally parsed by this function.
1347
1348    Fills in neon_type struct pointer with parsed information, and updates STR
1349    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1350    type, FAIL if not.  */
1351
1352 static int
1353 parse_neon_type (struct neon_type *type, char **str)
1354 {
1355   char *ptr = *str;
1356
1357   if (type)
1358     type->elems = 0;
1359
1360   while (type->elems < NEON_MAX_TYPE_ELS)
1361     {
1362       enum neon_el_type thistype = NT_untyped;
1363       unsigned thissize = -1u;
1364
1365       if (*ptr != '.')
1366         break;
1367
1368       ptr++;
1369
1370       /* Just a size without an explicit type.  */
1371       if (ISDIGIT (*ptr))
1372         goto parsesize;
1373
1374       switch (TOLOWER (*ptr))
1375         {
1376         case 'i': thistype = NT_integer; break;
1377         case 'f': thistype = NT_float; break;
1378         case 'p': thistype = NT_poly; break;
1379         case 's': thistype = NT_signed; break;
1380         case 'u': thistype = NT_unsigned; break;
1381         case 'd':
1382           thistype = NT_float;
1383           thissize = 64;
1384           ptr++;
1385           goto done;
1386         default:
1387           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1388           return FAIL;
1389         }
1390
1391       ptr++;
1392
1393       /* .f is an abbreviation for .f32.  */
1394       if (thistype == NT_float && !ISDIGIT (*ptr))
1395         thissize = 32;
1396       else
1397         {
1398         parsesize:
1399           thissize = strtoul (ptr, &ptr, 10);
1400
1401           if (thissize != 8 && thissize != 16 && thissize != 32
1402               && thissize != 64)
1403             {
1404               as_bad (_("bad size %d in type specifier"), thissize);
1405               return FAIL;
1406             }
1407         }
1408
1409       done:
1410       if (type)
1411         {
1412           type->el[type->elems].type = thistype;
1413           type->el[type->elems].size = thissize;
1414           type->elems++;
1415         }
1416     }
1417
1418   /* Empty/missing type is not a successful parse.  */
1419   if (type->elems == 0)
1420     return FAIL;
1421
1422   *str = ptr;
1423
1424   return SUCCESS;
1425 }
1426
1427 /* Errors may be set multiple times during parsing or bit encoding
1428    (particularly in the Neon bits), but usually the earliest error which is set
1429    will be the most meaningful. Avoid overwriting it with later (cascading)
1430    errors by calling this function.  */
1431
1432 static void
1433 first_error (const char *err)
1434 {
1435   if (!inst.error)
1436     inst.error = err;
1437 }
1438
1439 /* Parse a single type, e.g. ".s32", leading period included.  */
1440 static int
1441 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1442 {
1443   char *str = *ccp;
1444   struct neon_type optype;
1445
1446   if (*str == '.')
1447     {
1448       if (parse_neon_type (&optype, &str) == SUCCESS)
1449         {
1450           if (optype.elems == 1)
1451             *vectype = optype.el[0];
1452           else
1453             {
1454               first_error (_("only one type should be specified for operand"));
1455               return FAIL;
1456             }
1457         }
1458       else
1459         {
1460           first_error (_("vector type expected"));
1461           return FAIL;
1462         }
1463     }
1464   else
1465     return FAIL;
1466
1467   *ccp = str;
1468
1469   return SUCCESS;
1470 }
1471
1472 /* Special meanings for indices (which have a range of 0-7), which will fit into
1473    a 4-bit integer.  */
1474
1475 #define NEON_ALL_LANES          15
1476 #define NEON_INTERLEAVE_LANES   14
1477
1478 /* Parse either a register or a scalar, with an optional type. Return the
1479    register number, and optionally fill in the actual type of the register
1480    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1481    type/index information in *TYPEINFO.  */
1482
1483 static int
1484 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1485                            enum arm_reg_type *rtype,
1486                            struct neon_typed_alias *typeinfo)
1487 {
1488   char *str = *ccp;
1489   struct reg_entry *reg = arm_reg_parse_multi (&str);
1490   struct neon_typed_alias atype;
1491   struct neon_type_el parsetype;
1492
1493   atype.defined = 0;
1494   atype.index = -1;
1495   atype.eltype.type = NT_invtype;
1496   atype.eltype.size = -1;
1497
1498   /* Try alternate syntax for some types of register. Note these are mutually
1499      exclusive with the Neon syntax extensions.  */
1500   if (reg == NULL)
1501     {
1502       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1503       if (altreg != FAIL)
1504         *ccp = str;
1505       if (typeinfo)
1506         *typeinfo = atype;
1507       return altreg;
1508     }
1509
1510   /* Undo polymorphism when a set of register types may be accepted.  */
1511   if ((type == REG_TYPE_NDQ
1512        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1513       || (type == REG_TYPE_VFSD
1514           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1515       || (type == REG_TYPE_NSDQ
1516           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1517               || reg->type == REG_TYPE_NQ))
1518       || (type == REG_TYPE_NSD
1519           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1520       || (type == REG_TYPE_MMXWC
1521           && (reg->type == REG_TYPE_MMXWCG)))
1522     type = (enum arm_reg_type) reg->type;
1523
1524   if (type != reg->type)
1525     return FAIL;
1526
1527   if (reg->neon)
1528     atype = *reg->neon;
1529
1530   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1531     {
1532       if ((atype.defined & NTA_HASTYPE) != 0)
1533         {
1534           first_error (_("can't redefine type for operand"));
1535           return FAIL;
1536         }
1537       atype.defined |= NTA_HASTYPE;
1538       atype.eltype = parsetype;
1539     }
1540
1541   if (skip_past_char (&str, '[') == SUCCESS)
1542     {
1543       if (type != REG_TYPE_VFD
1544           && !(type == REG_TYPE_VFS
1545                && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8_2)))
1546         {
1547           first_error (_("only D registers may be indexed"));
1548           return FAIL;
1549         }
1550
1551       if ((atype.defined & NTA_HASINDEX) != 0)
1552         {
1553           first_error (_("can't change index for operand"));
1554           return FAIL;
1555         }
1556
1557       atype.defined |= NTA_HASINDEX;
1558
1559       if (skip_past_char (&str, ']') == SUCCESS)
1560         atype.index = NEON_ALL_LANES;
1561       else
1562         {
1563           expressionS exp;
1564
1565           my_get_expression (&exp, &str, GE_NO_PREFIX);
1566
1567           if (exp.X_op != O_constant)
1568             {
1569               first_error (_("constant expression required"));
1570               return FAIL;
1571             }
1572
1573           if (skip_past_char (&str, ']') == FAIL)
1574             return FAIL;
1575
1576           atype.index = exp.X_add_number;
1577         }
1578     }
1579
1580   if (typeinfo)
1581     *typeinfo = atype;
1582
1583   if (rtype)
1584     *rtype = type;
1585
1586   *ccp = str;
1587
1588   return reg->number;
1589 }
1590
1591 /* Like arm_reg_parse, but allow allow the following extra features:
1592     - If RTYPE is non-zero, return the (possibly restricted) type of the
1593       register (e.g. Neon double or quad reg when either has been requested).
1594     - If this is a Neon vector type with additional type information, fill
1595       in the struct pointed to by VECTYPE (if non-NULL).
1596    This function will fault on encountering a scalar.  */
1597
1598 static int
1599 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1600                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1601 {
1602   struct neon_typed_alias atype;
1603   char *str = *ccp;
1604   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1605
1606   if (reg == FAIL)
1607     return FAIL;
1608
1609   /* Do not allow regname(... to parse as a register.  */
1610   if (*str == '(')
1611     return FAIL;
1612
1613   /* Do not allow a scalar (reg+index) to parse as a register.  */
1614   if ((atype.defined & NTA_HASINDEX) != 0)
1615     {
1616       first_error (_("register operand expected, but got scalar"));
1617       return FAIL;
1618     }
1619
1620   if (vectype)
1621     *vectype = atype.eltype;
1622
1623   *ccp = str;
1624
1625   return reg;
1626 }
1627
1628 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1629 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1630
1631 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1632    have enough information to be able to do a good job bounds-checking. So, we
1633    just do easy checks here, and do further checks later.  */
1634
1635 static int
1636 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1637 {
1638   int reg;
1639   char *str = *ccp;
1640   struct neon_typed_alias atype;
1641   enum arm_reg_type reg_type = REG_TYPE_VFD;
1642
1643   if (elsize == 4)
1644     reg_type = REG_TYPE_VFS;
1645
1646   reg = parse_typed_reg_or_scalar (&str, reg_type, NULL, &atype);
1647
1648   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1649     return FAIL;
1650
1651   if (atype.index == NEON_ALL_LANES)
1652     {
1653       first_error (_("scalar must have an index"));
1654       return FAIL;
1655     }
1656   else if (atype.index >= 64 / elsize)
1657     {
1658       first_error (_("scalar index out of range"));
1659       return FAIL;
1660     }
1661
1662   if (type)
1663     *type = atype.eltype;
1664
1665   *ccp = str;
1666
1667   return reg * 16 + atype.index;
1668 }
1669
1670 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1671
1672 static long
1673 parse_reg_list (char ** strp)
1674 {
1675   char * str = * strp;
1676   long   range = 0;
1677   int    another_range;
1678
1679   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1680   do
1681     {
1682       skip_whitespace (str);
1683
1684       another_range = 0;
1685
1686       if (*str == '{')
1687         {
1688           int in_range = 0;
1689           int cur_reg = -1;
1690
1691           str++;
1692           do
1693             {
1694               int reg;
1695
1696               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1697                 {
1698                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1699                   return FAIL;
1700                 }
1701
1702               if (in_range)
1703                 {
1704                   int i;
1705
1706                   if (reg <= cur_reg)
1707                     {
1708                       first_error (_("bad range in register list"));
1709                       return FAIL;
1710                     }
1711
1712                   for (i = cur_reg + 1; i < reg; i++)
1713                     {
1714                       if (range & (1 << i))
1715                         as_tsktsk
1716                           (_("Warning: duplicated register (r%d) in register list"),
1717                            i);
1718                       else
1719                         range |= 1 << i;
1720                     }
1721                   in_range = 0;
1722                 }
1723
1724               if (range & (1 << reg))
1725                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1726                            reg);
1727               else if (reg <= cur_reg)
1728                 as_tsktsk (_("Warning: register range not in ascending order"));
1729
1730               range |= 1 << reg;
1731               cur_reg = reg;
1732             }
1733           while (skip_past_comma (&str) != FAIL
1734                  || (in_range = 1, *str++ == '-'));
1735           str--;
1736
1737           if (skip_past_char (&str, '}') == FAIL)
1738             {
1739               first_error (_("missing `}'"));
1740               return FAIL;
1741             }
1742         }
1743       else
1744         {
1745           expressionS exp;
1746
1747           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1748             return FAIL;
1749
1750           if (exp.X_op == O_constant)
1751             {
1752               if (exp.X_add_number
1753                   != (exp.X_add_number & 0x0000ffff))
1754                 {
1755                   inst.error = _("invalid register mask");
1756                   return FAIL;
1757                 }
1758
1759               if ((range & exp.X_add_number) != 0)
1760                 {
1761                   int regno = range & exp.X_add_number;
1762
1763                   regno &= -regno;
1764                   regno = (1 << regno) - 1;
1765                   as_tsktsk
1766                     (_("Warning: duplicated register (r%d) in register list"),
1767                      regno);
1768                 }
1769
1770               range |= exp.X_add_number;
1771             }
1772           else
1773             {
1774               if (inst.reloc.type != 0)
1775                 {
1776                   inst.error = _("expression too complex");
1777                   return FAIL;
1778                 }
1779
1780               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1781               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1782               inst.reloc.pc_rel = 0;
1783             }
1784         }
1785
1786       if (*str == '|' || *str == '+')
1787         {
1788           str++;
1789           another_range = 1;
1790         }
1791     }
1792   while (another_range);
1793
1794   *strp = str;
1795   return range;
1796 }
1797
1798 /* Types of registers in a list.  */
1799
1800 enum reg_list_els
1801 {
1802   REGLIST_VFP_S,
1803   REGLIST_VFP_D,
1804   REGLIST_NEON_D
1805 };
1806
1807 /* Parse a VFP register list.  If the string is invalid return FAIL.
1808    Otherwise return the number of registers, and set PBASE to the first
1809    register.  Parses registers of type ETYPE.
1810    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1811      - Q registers can be used to specify pairs of D registers
1812      - { } can be omitted from around a singleton register list
1813          FIXME: This is not implemented, as it would require backtracking in
1814          some cases, e.g.:
1815            vtbl.8 d3,d4,d5
1816          This could be done (the meaning isn't really ambiguous), but doesn't
1817          fit in well with the current parsing framework.
1818      - 32 D registers may be used (also true for VFPv3).
1819    FIXME: Types are ignored in these register lists, which is probably a
1820    bug.  */
1821
1822 static int
1823 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1824 {
1825   char *str = *ccp;
1826   int base_reg;
1827   int new_base;
1828   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1829   int max_regs = 0;
1830   int count = 0;
1831   int warned = 0;
1832   unsigned long mask = 0;
1833   int i;
1834
1835   if (skip_past_char (&str, '{') == FAIL)
1836     {
1837       inst.error = _("expecting {");
1838       return FAIL;
1839     }
1840
1841   switch (etype)
1842     {
1843     case REGLIST_VFP_S:
1844       regtype = REG_TYPE_VFS;
1845       max_regs = 32;
1846       break;
1847
1848     case REGLIST_VFP_D:
1849       regtype = REG_TYPE_VFD;
1850       break;
1851
1852     case REGLIST_NEON_D:
1853       regtype = REG_TYPE_NDQ;
1854       break;
1855     }
1856
1857   if (etype != REGLIST_VFP_S)
1858     {
1859       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1860       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1861         {
1862           max_regs = 32;
1863           if (thumb_mode)
1864             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1865                                     fpu_vfp_ext_d32);
1866           else
1867             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1868                                     fpu_vfp_ext_d32);
1869         }
1870       else
1871         max_regs = 16;
1872     }
1873
1874   base_reg = max_regs;
1875
1876   do
1877     {
1878       int setmask = 1, addregs = 1;
1879
1880       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1881
1882       if (new_base == FAIL)
1883         {
1884           first_error (_(reg_expected_msgs[regtype]));
1885           return FAIL;
1886         }
1887
1888       if (new_base >= max_regs)
1889         {
1890           first_error (_("register out of range in list"));
1891           return FAIL;
1892         }
1893
1894       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1895       if (regtype == REG_TYPE_NQ)
1896         {
1897           setmask = 3;
1898           addregs = 2;
1899         }
1900
1901       if (new_base < base_reg)
1902         base_reg = new_base;
1903
1904       if (mask & (setmask << new_base))
1905         {
1906           first_error (_("invalid register list"));
1907           return FAIL;
1908         }
1909
1910       if ((mask >> new_base) != 0 && ! warned)
1911         {
1912           as_tsktsk (_("register list not in ascending order"));
1913           warned = 1;
1914         }
1915
1916       mask |= setmask << new_base;
1917       count += addregs;
1918
1919       if (*str == '-') /* We have the start of a range expression */
1920         {
1921           int high_range;
1922
1923           str++;
1924
1925           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1926               == FAIL)
1927             {
1928               inst.error = gettext (reg_expected_msgs[regtype]);
1929               return FAIL;
1930             }
1931
1932           if (high_range >= max_regs)
1933             {
1934               first_error (_("register out of range in list"));
1935               return FAIL;
1936             }
1937
1938           if (regtype == REG_TYPE_NQ)
1939             high_range = high_range + 1;
1940
1941           if (high_range <= new_base)
1942             {
1943               inst.error = _("register range not in ascending order");
1944               return FAIL;
1945             }
1946
1947           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1948             {
1949               if (mask & (setmask << new_base))
1950                 {
1951                   inst.error = _("invalid register list");
1952                   return FAIL;
1953                 }
1954
1955               mask |= setmask << new_base;
1956               count += addregs;
1957             }
1958         }
1959     }
1960   while (skip_past_comma (&str) != FAIL);
1961
1962   str++;
1963
1964   /* Sanity check -- should have raised a parse error above.  */
1965   if (count == 0 || count > max_regs)
1966     abort ();
1967
1968   *pbase = base_reg;
1969
1970   /* Final test -- the registers must be consecutive.  */
1971   mask >>= base_reg;
1972   for (i = 0; i < count; i++)
1973     {
1974       if ((mask & (1u << i)) == 0)
1975         {
1976           inst.error = _("non-contiguous register range");
1977           return FAIL;
1978         }
1979     }
1980
1981   *ccp = str;
1982
1983   return count;
1984 }
1985
1986 /* True if two alias types are the same.  */
1987
1988 static bfd_boolean
1989 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1990 {
1991   if (!a && !b)
1992     return TRUE;
1993
1994   if (!a || !b)
1995     return FALSE;
1996
1997   if (a->defined != b->defined)
1998     return FALSE;
1999
2000   if ((a->defined & NTA_HASTYPE) != 0
2001       && (a->eltype.type != b->eltype.type
2002           || a->eltype.size != b->eltype.size))
2003     return FALSE;
2004
2005   if ((a->defined & NTA_HASINDEX) != 0
2006       && (a->index != b->index))
2007     return FALSE;
2008
2009   return TRUE;
2010 }
2011
2012 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
2013    The base register is put in *PBASE.
2014    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
2015    the return value.
2016    The register stride (minus one) is put in bit 4 of the return value.
2017    Bits [6:5] encode the list length (minus one).
2018    The type of the list elements is put in *ELTYPE, if non-NULL.  */
2019
2020 #define NEON_LANE(X)            ((X) & 0xf)
2021 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
2022 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2023
2024 static int
2025 parse_neon_el_struct_list (char **str, unsigned *pbase,
2026                            struct neon_type_el *eltype)
2027 {
2028   char *ptr = *str;
2029   int base_reg = -1;
2030   int reg_incr = -1;
2031   int count = 0;
2032   int lane = -1;
2033   int leading_brace = 0;
2034   enum arm_reg_type rtype = REG_TYPE_NDQ;
2035   const char *const incr_error = _("register stride must be 1 or 2");
2036   const char *const type_error = _("mismatched element/structure types in list");
2037   struct neon_typed_alias firsttype;
2038   firsttype.defined = 0;
2039   firsttype.eltype.type = NT_invtype;
2040   firsttype.eltype.size = -1;
2041   firsttype.index = -1;
2042
2043   if (skip_past_char (&ptr, '{') == SUCCESS)
2044     leading_brace = 1;
2045
2046   do
2047     {
2048       struct neon_typed_alias atype;
2049       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2050
2051       if (getreg == FAIL)
2052         {
2053           first_error (_(reg_expected_msgs[rtype]));
2054           return FAIL;
2055         }
2056
2057       if (base_reg == -1)
2058         {
2059           base_reg = getreg;
2060           if (rtype == REG_TYPE_NQ)
2061             {
2062               reg_incr = 1;
2063             }
2064           firsttype = atype;
2065         }
2066       else if (reg_incr == -1)
2067         {
2068           reg_incr = getreg - base_reg;
2069           if (reg_incr < 1 || reg_incr > 2)
2070             {
2071               first_error (_(incr_error));
2072               return FAIL;
2073             }
2074         }
2075       else if (getreg != base_reg + reg_incr * count)
2076         {
2077           first_error (_(incr_error));
2078           return FAIL;
2079         }
2080
2081       if (! neon_alias_types_same (&atype, &firsttype))
2082         {
2083           first_error (_(type_error));
2084           return FAIL;
2085         }
2086
2087       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2088          modes.  */
2089       if (ptr[0] == '-')
2090         {
2091           struct neon_typed_alias htype;
2092           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2093           if (lane == -1)
2094             lane = NEON_INTERLEAVE_LANES;
2095           else if (lane != NEON_INTERLEAVE_LANES)
2096             {
2097               first_error (_(type_error));
2098               return FAIL;
2099             }
2100           if (reg_incr == -1)
2101             reg_incr = 1;
2102           else if (reg_incr != 1)
2103             {
2104               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2105               return FAIL;
2106             }
2107           ptr++;
2108           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2109           if (hireg == FAIL)
2110             {
2111               first_error (_(reg_expected_msgs[rtype]));
2112               return FAIL;
2113             }
2114           if (! neon_alias_types_same (&htype, &firsttype))
2115             {
2116               first_error (_(type_error));
2117               return FAIL;
2118             }
2119           count += hireg + dregs - getreg;
2120           continue;
2121         }
2122
2123       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2124       if (rtype == REG_TYPE_NQ)
2125         {
2126           count += 2;
2127           continue;
2128         }
2129
2130       if ((atype.defined & NTA_HASINDEX) != 0)
2131         {
2132           if (lane == -1)
2133             lane = atype.index;
2134           else if (lane != atype.index)
2135             {
2136               first_error (_(type_error));
2137               return FAIL;
2138             }
2139         }
2140       else if (lane == -1)
2141         lane = NEON_INTERLEAVE_LANES;
2142       else if (lane != NEON_INTERLEAVE_LANES)
2143         {
2144           first_error (_(type_error));
2145           return FAIL;
2146         }
2147       count++;
2148     }
2149   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2150
2151   /* No lane set by [x]. We must be interleaving structures.  */
2152   if (lane == -1)
2153     lane = NEON_INTERLEAVE_LANES;
2154
2155   /* Sanity check.  */
2156   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2157       || (count > 1 && reg_incr == -1))
2158     {
2159       first_error (_("error parsing element/structure list"));
2160       return FAIL;
2161     }
2162
2163   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2164     {
2165       first_error (_("expected }"));
2166       return FAIL;
2167     }
2168
2169   if (reg_incr == -1)
2170     reg_incr = 1;
2171
2172   if (eltype)
2173     *eltype = firsttype.eltype;
2174
2175   *pbase = base_reg;
2176   *str = ptr;
2177
2178   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2179 }
2180
2181 /* Parse an explicit relocation suffix on an expression.  This is
2182    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2183    arm_reloc_hsh contains no entries, so this function can only
2184    succeed if there is no () after the word.  Returns -1 on error,
2185    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2186
2187 static int
2188 parse_reloc (char **str)
2189 {
2190   struct reloc_entry *r;
2191   char *p, *q;
2192
2193   if (**str != '(')
2194     return BFD_RELOC_UNUSED;
2195
2196   p = *str + 1;
2197   q = p;
2198
2199   while (*q && *q != ')' && *q != ',')
2200     q++;
2201   if (*q != ')')
2202     return -1;
2203
2204   if ((r = (struct reloc_entry *)
2205        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2206     return -1;
2207
2208   *str = q + 1;
2209   return r->reloc;
2210 }
2211
2212 /* Directives: register aliases.  */
2213
2214 static struct reg_entry *
2215 insert_reg_alias (char *str, unsigned number, int type)
2216 {
2217   struct reg_entry *new_reg;
2218   const char *name;
2219
2220   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2221     {
2222       if (new_reg->builtin)
2223         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2224
2225       /* Only warn about a redefinition if it's not defined as the
2226          same register.  */
2227       else if (new_reg->number != number || new_reg->type != type)
2228         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2229
2230       return NULL;
2231     }
2232
2233   name = xstrdup (str);
2234   new_reg = XNEW (struct reg_entry);
2235
2236   new_reg->name = name;
2237   new_reg->number = number;
2238   new_reg->type = type;
2239   new_reg->builtin = FALSE;
2240   new_reg->neon = NULL;
2241
2242   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2243     abort ();
2244
2245   return new_reg;
2246 }
2247
2248 static void
2249 insert_neon_reg_alias (char *str, int number, int type,
2250                        struct neon_typed_alias *atype)
2251 {
2252   struct reg_entry *reg = insert_reg_alias (str, number, type);
2253
2254   if (!reg)
2255     {
2256       first_error (_("attempt to redefine typed alias"));
2257       return;
2258     }
2259
2260   if (atype)
2261     {
2262       reg->neon = XNEW (struct neon_typed_alias);
2263       *reg->neon = *atype;
2264     }
2265 }
2266
2267 /* Look for the .req directive.  This is of the form:
2268
2269         new_register_name .req existing_register_name
2270
2271    If we find one, or if it looks sufficiently like one that we want to
2272    handle any error here, return TRUE.  Otherwise return FALSE.  */
2273
2274 static bfd_boolean
2275 create_register_alias (char * newname, char *p)
2276 {
2277   struct reg_entry *old;
2278   char *oldname, *nbuf;
2279   size_t nlen;
2280
2281   /* The input scrubber ensures that whitespace after the mnemonic is
2282      collapsed to single spaces.  */
2283   oldname = p;
2284   if (strncmp (oldname, " .req ", 6) != 0)
2285     return FALSE;
2286
2287   oldname += 6;
2288   if (*oldname == '\0')
2289     return FALSE;
2290
2291   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2292   if (!old)
2293     {
2294       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2295       return TRUE;
2296     }
2297
2298   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2299      the desired alias name, and p points to its end.  If not, then
2300      the desired alias name is in the global original_case_string.  */
2301 #ifdef TC_CASE_SENSITIVE
2302   nlen = p - newname;
2303 #else
2304   newname = original_case_string;
2305   nlen = strlen (newname);
2306 #endif
2307
2308   nbuf = xmemdup0 (newname, nlen);
2309
2310   /* Create aliases under the new name as stated; an all-lowercase
2311      version of the new name; and an all-uppercase version of the new
2312      name.  */
2313   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2314     {
2315       for (p = nbuf; *p; p++)
2316         *p = TOUPPER (*p);
2317
2318       if (strncmp (nbuf, newname, nlen))
2319         {
2320           /* If this attempt to create an additional alias fails, do not bother
2321              trying to create the all-lower case alias.  We will fail and issue
2322              a second, duplicate error message.  This situation arises when the
2323              programmer does something like:
2324                foo .req r0
2325                Foo .req r1
2326              The second .req creates the "Foo" alias but then fails to create
2327              the artificial FOO alias because it has already been created by the
2328              first .req.  */
2329           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2330             {
2331               free (nbuf);
2332               return TRUE;
2333             }
2334         }
2335
2336       for (p = nbuf; *p; p++)
2337         *p = TOLOWER (*p);
2338
2339       if (strncmp (nbuf, newname, nlen))
2340         insert_reg_alias (nbuf, old->number, old->type);
2341     }
2342
2343   free (nbuf);
2344   return TRUE;
2345 }
2346
2347 /* Create a Neon typed/indexed register alias using directives, e.g.:
2348      X .dn d5.s32[1]
2349      Y .qn 6.s16
2350      Z .dn d7
2351      T .dn Z[0]
2352    These typed registers can be used instead of the types specified after the
2353    Neon mnemonic, so long as all operands given have types. Types can also be
2354    specified directly, e.g.:
2355      vadd d0.s32, d1.s32, d2.s32  */
2356
2357 static bfd_boolean
2358 create_neon_reg_alias (char *newname, char *p)
2359 {
2360   enum arm_reg_type basetype;
2361   struct reg_entry *basereg;
2362   struct reg_entry mybasereg;
2363   struct neon_type ntype;
2364   struct neon_typed_alias typeinfo;
2365   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2366   int namelen;
2367
2368   typeinfo.defined = 0;
2369   typeinfo.eltype.type = NT_invtype;
2370   typeinfo.eltype.size = -1;
2371   typeinfo.index = -1;
2372
2373   nameend = p;
2374
2375   if (strncmp (p, " .dn ", 5) == 0)
2376     basetype = REG_TYPE_VFD;
2377   else if (strncmp (p, " .qn ", 5) == 0)
2378     basetype = REG_TYPE_NQ;
2379   else
2380     return FALSE;
2381
2382   p += 5;
2383
2384   if (*p == '\0')
2385     return FALSE;
2386
2387   basereg = arm_reg_parse_multi (&p);
2388
2389   if (basereg && basereg->type != basetype)
2390     {
2391       as_bad (_("bad type for register"));
2392       return FALSE;
2393     }
2394
2395   if (basereg == NULL)
2396     {
2397       expressionS exp;
2398       /* Try parsing as an integer.  */
2399       my_get_expression (&exp, &p, GE_NO_PREFIX);
2400       if (exp.X_op != O_constant)
2401         {
2402           as_bad (_("expression must be constant"));
2403           return FALSE;
2404         }
2405       basereg = &mybasereg;
2406       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2407                                                   : exp.X_add_number;
2408       basereg->neon = 0;
2409     }
2410
2411   if (basereg->neon)
2412     typeinfo = *basereg->neon;
2413
2414   if (parse_neon_type (&ntype, &p) == SUCCESS)
2415     {
2416       /* We got a type.  */
2417       if (typeinfo.defined & NTA_HASTYPE)
2418         {
2419           as_bad (_("can't redefine the type of a register alias"));
2420           return FALSE;
2421         }
2422
2423       typeinfo.defined |= NTA_HASTYPE;
2424       if (ntype.elems != 1)
2425         {
2426           as_bad (_("you must specify a single type only"));
2427           return FALSE;
2428         }
2429       typeinfo.eltype = ntype.el[0];
2430     }
2431
2432   if (skip_past_char (&p, '[') == SUCCESS)
2433     {
2434       expressionS exp;
2435       /* We got a scalar index.  */
2436
2437       if (typeinfo.defined & NTA_HASINDEX)
2438         {
2439           as_bad (_("can't redefine the index of a scalar alias"));
2440           return FALSE;
2441         }
2442
2443       my_get_expression (&exp, &p, GE_NO_PREFIX);
2444
2445       if (exp.X_op != O_constant)
2446         {
2447           as_bad (_("scalar index must be constant"));
2448           return FALSE;
2449         }
2450
2451       typeinfo.defined |= NTA_HASINDEX;
2452       typeinfo.index = exp.X_add_number;
2453
2454       if (skip_past_char (&p, ']') == FAIL)
2455         {
2456           as_bad (_("expecting ]"));
2457           return FALSE;
2458         }
2459     }
2460
2461   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2462      the desired alias name, and p points to its end.  If not, then
2463      the desired alias name is in the global original_case_string.  */
2464 #ifdef TC_CASE_SENSITIVE
2465   namelen = nameend - newname;
2466 #else
2467   newname = original_case_string;
2468   namelen = strlen (newname);
2469 #endif
2470
2471   namebuf = xmemdup0 (newname, namelen);
2472
2473   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2474                          typeinfo.defined != 0 ? &typeinfo : NULL);
2475
2476   /* Insert name in all uppercase.  */
2477   for (p = namebuf; *p; p++)
2478     *p = TOUPPER (*p);
2479
2480   if (strncmp (namebuf, newname, namelen))
2481     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2482                            typeinfo.defined != 0 ? &typeinfo : NULL);
2483
2484   /* Insert name in all lowercase.  */
2485   for (p = namebuf; *p; p++)
2486     *p = TOLOWER (*p);
2487
2488   if (strncmp (namebuf, newname, namelen))
2489     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2490                            typeinfo.defined != 0 ? &typeinfo : NULL);
2491
2492   free (namebuf);
2493   return TRUE;
2494 }
2495
2496 /* Should never be called, as .req goes between the alias and the
2497    register name, not at the beginning of the line.  */
2498
2499 static void
2500 s_req (int a ATTRIBUTE_UNUSED)
2501 {
2502   as_bad (_("invalid syntax for .req directive"));
2503 }
2504
2505 static void
2506 s_dn (int a ATTRIBUTE_UNUSED)
2507 {
2508   as_bad (_("invalid syntax for .dn directive"));
2509 }
2510
2511 static void
2512 s_qn (int a ATTRIBUTE_UNUSED)
2513 {
2514   as_bad (_("invalid syntax for .qn directive"));
2515 }
2516
2517 /* The .unreq directive deletes an alias which was previously defined
2518    by .req.  For example:
2519
2520        my_alias .req r11
2521        .unreq my_alias    */
2522
2523 static void
2524 s_unreq (int a ATTRIBUTE_UNUSED)
2525 {
2526   char * name;
2527   char saved_char;
2528
2529   name = input_line_pointer;
2530
2531   while (*input_line_pointer != 0
2532          && *input_line_pointer != ' '
2533          && *input_line_pointer != '\n')
2534     ++input_line_pointer;
2535
2536   saved_char = *input_line_pointer;
2537   *input_line_pointer = 0;
2538
2539   if (!*name)
2540     as_bad (_("invalid syntax for .unreq directive"));
2541   else
2542     {
2543       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2544                                                               name);
2545
2546       if (!reg)
2547         as_bad (_("unknown register alias '%s'"), name);
2548       else if (reg->builtin)
2549         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2550                  name);
2551       else
2552         {
2553           char * p;
2554           char * nbuf;
2555
2556           hash_delete (arm_reg_hsh, name, FALSE);
2557           free ((char *) reg->name);
2558           if (reg->neon)
2559             free (reg->neon);
2560           free (reg);
2561
2562           /* Also locate the all upper case and all lower case versions.
2563              Do not complain if we cannot find one or the other as it
2564              was probably deleted above.  */
2565
2566           nbuf = strdup (name);
2567           for (p = nbuf; *p; p++)
2568             *p = TOUPPER (*p);
2569           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2570           if (reg)
2571             {
2572               hash_delete (arm_reg_hsh, nbuf, FALSE);
2573               free ((char *) reg->name);
2574               if (reg->neon)
2575                 free (reg->neon);
2576               free (reg);
2577             }
2578
2579           for (p = nbuf; *p; p++)
2580             *p = TOLOWER (*p);
2581           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2582           if (reg)
2583             {
2584               hash_delete (arm_reg_hsh, nbuf, FALSE);
2585               free ((char *) reg->name);
2586               if (reg->neon)
2587                 free (reg->neon);
2588               free (reg);
2589             }
2590
2591           free (nbuf);
2592         }
2593     }
2594
2595   *input_line_pointer = saved_char;
2596   demand_empty_rest_of_line ();
2597 }
2598
2599 /* Directives: Instruction set selection.  */
2600
2601 #ifdef OBJ_ELF
2602 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2603    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2604    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2605    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2606
2607 /* Create a new mapping symbol for the transition to STATE.  */
2608
2609 static void
2610 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2611 {
2612   symbolS * symbolP;
2613   const char * symname;
2614   int type;
2615
2616   switch (state)
2617     {
2618     case MAP_DATA:
2619       symname = "$d";
2620       type = BSF_NO_FLAGS;
2621       break;
2622     case MAP_ARM:
2623       symname = "$a";
2624       type = BSF_NO_FLAGS;
2625       break;
2626     case MAP_THUMB:
2627       symname = "$t";
2628       type = BSF_NO_FLAGS;
2629       break;
2630     default:
2631       abort ();
2632     }
2633
2634   symbolP = symbol_new (symname, now_seg, value, frag);
2635   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2636
2637   switch (state)
2638     {
2639     case MAP_ARM:
2640       THUMB_SET_FUNC (symbolP, 0);
2641       ARM_SET_THUMB (symbolP, 0);
2642       ARM_SET_INTERWORK (symbolP, support_interwork);
2643       break;
2644
2645     case MAP_THUMB:
2646       THUMB_SET_FUNC (symbolP, 1);
2647       ARM_SET_THUMB (symbolP, 1);
2648       ARM_SET_INTERWORK (symbolP, support_interwork);
2649       break;
2650
2651     case MAP_DATA:
2652     default:
2653       break;
2654     }
2655
2656   /* Save the mapping symbols for future reference.  Also check that
2657      we do not place two mapping symbols at the same offset within a
2658      frag.  We'll handle overlap between frags in
2659      check_mapping_symbols.
2660
2661      If .fill or other data filling directive generates zero sized data,
2662      the mapping symbol for the following code will have the same value
2663      as the one generated for the data filling directive.  In this case,
2664      we replace the old symbol with the new one at the same address.  */
2665   if (value == 0)
2666     {
2667       if (frag->tc_frag_data.first_map != NULL)
2668         {
2669           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2670           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2671         }
2672       frag->tc_frag_data.first_map = symbolP;
2673     }
2674   if (frag->tc_frag_data.last_map != NULL)
2675     {
2676       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2677       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2678         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2679     }
2680   frag->tc_frag_data.last_map = symbolP;
2681 }
2682
2683 /* We must sometimes convert a region marked as code to data during
2684    code alignment, if an odd number of bytes have to be padded.  The
2685    code mapping symbol is pushed to an aligned address.  */
2686
2687 static void
2688 insert_data_mapping_symbol (enum mstate state,
2689                             valueT value, fragS *frag, offsetT bytes)
2690 {
2691   /* If there was already a mapping symbol, remove it.  */
2692   if (frag->tc_frag_data.last_map != NULL
2693       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2694     {
2695       symbolS *symp = frag->tc_frag_data.last_map;
2696
2697       if (value == 0)
2698         {
2699           know (frag->tc_frag_data.first_map == symp);
2700           frag->tc_frag_data.first_map = NULL;
2701         }
2702       frag->tc_frag_data.last_map = NULL;
2703       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2704     }
2705
2706   make_mapping_symbol (MAP_DATA, value, frag);
2707   make_mapping_symbol (state, value + bytes, frag);
2708 }
2709
2710 static void mapping_state_2 (enum mstate state, int max_chars);
2711
2712 /* Set the mapping state to STATE.  Only call this when about to
2713    emit some STATE bytes to the file.  */
2714
2715 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2716 void
2717 mapping_state (enum mstate state)
2718 {
2719   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2720
2721   if (mapstate == state)
2722     /* The mapping symbol has already been emitted.
2723        There is nothing else to do.  */
2724     return;
2725
2726   if (state == MAP_ARM || state == MAP_THUMB)
2727     /*  PR gas/12931
2728         All ARM instructions require 4-byte alignment.
2729         (Almost) all Thumb instructions require 2-byte alignment.
2730
2731         When emitting instructions into any section, mark the section
2732         appropriately.
2733
2734         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2735         but themselves require 2-byte alignment; this applies to some
2736         PC- relative forms.  However, these cases will involve implicit
2737         literal pool generation or an explicit .align >=2, both of
2738         which will cause the section to me marked with sufficient
2739         alignment.  Thus, we don't handle those cases here.  */
2740     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2741
2742   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2743     /* This case will be evaluated later.  */
2744     return;
2745
2746   mapping_state_2 (state, 0);
2747 }
2748
2749 /* Same as mapping_state, but MAX_CHARS bytes have already been
2750    allocated.  Put the mapping symbol that far back.  */
2751
2752 static void
2753 mapping_state_2 (enum mstate state, int max_chars)
2754 {
2755   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2756
2757   if (!SEG_NORMAL (now_seg))
2758     return;
2759
2760   if (mapstate == state)
2761     /* The mapping symbol has already been emitted.
2762        There is nothing else to do.  */
2763     return;
2764
2765   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2766           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2767     {
2768       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2769       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2770
2771       if (add_symbol)
2772         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2773     }
2774
2775   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2776   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2777 }
2778 #undef TRANSITION
2779 #else
2780 #define mapping_state(x) ((void)0)
2781 #define mapping_state_2(x, y) ((void)0)
2782 #endif
2783
2784 /* Find the real, Thumb encoded start of a Thumb function.  */
2785
2786 #ifdef OBJ_COFF
2787 static symbolS *
2788 find_real_start (symbolS * symbolP)
2789 {
2790   char *       real_start;
2791   const char * name = S_GET_NAME (symbolP);
2792   symbolS *    new_target;
2793
2794   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2795 #define STUB_NAME ".real_start_of"
2796
2797   if (name == NULL)
2798     abort ();
2799
2800   /* The compiler may generate BL instructions to local labels because
2801      it needs to perform a branch to a far away location. These labels
2802      do not have a corresponding ".real_start_of" label.  We check
2803      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2804      the ".real_start_of" convention for nonlocal branches.  */
2805   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2806     return symbolP;
2807
2808   real_start = concat (STUB_NAME, name, NULL);
2809   new_target = symbol_find (real_start);
2810   free (real_start);
2811
2812   if (new_target == NULL)
2813     {
2814       as_warn (_("Failed to find real start of function: %s\n"), name);
2815       new_target = symbolP;
2816     }
2817
2818   return new_target;
2819 }
2820 #endif
2821
2822 static void
2823 opcode_select (int width)
2824 {
2825   switch (width)
2826     {
2827     case 16:
2828       if (! thumb_mode)
2829         {
2830           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2831             as_bad (_("selected processor does not support THUMB opcodes"));
2832
2833           thumb_mode = 1;
2834           /* No need to force the alignment, since we will have been
2835              coming from ARM mode, which is word-aligned.  */
2836           record_alignment (now_seg, 1);
2837         }
2838       break;
2839
2840     case 32:
2841       if (thumb_mode)
2842         {
2843           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2844             as_bad (_("selected processor does not support ARM opcodes"));
2845
2846           thumb_mode = 0;
2847
2848           if (!need_pass_2)
2849             frag_align (2, 0, 0);
2850
2851           record_alignment (now_seg, 1);
2852         }
2853       break;
2854
2855     default:
2856       as_bad (_("invalid instruction size selected (%d)"), width);
2857     }
2858 }
2859
2860 static void
2861 s_arm (int ignore ATTRIBUTE_UNUSED)
2862 {
2863   opcode_select (32);
2864   demand_empty_rest_of_line ();
2865 }
2866
2867 static void
2868 s_thumb (int ignore ATTRIBUTE_UNUSED)
2869 {
2870   opcode_select (16);
2871   demand_empty_rest_of_line ();
2872 }
2873
2874 static void
2875 s_code (int unused ATTRIBUTE_UNUSED)
2876 {
2877   int temp;
2878
2879   temp = get_absolute_expression ();
2880   switch (temp)
2881     {
2882     case 16:
2883     case 32:
2884       opcode_select (temp);
2885       break;
2886
2887     default:
2888       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2889     }
2890 }
2891
2892 static void
2893 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2894 {
2895   /* If we are not already in thumb mode go into it, EVEN if
2896      the target processor does not support thumb instructions.
2897      This is used by gcc/config/arm/lib1funcs.asm for example
2898      to compile interworking support functions even if the
2899      target processor should not support interworking.  */
2900   if (! thumb_mode)
2901     {
2902       thumb_mode = 2;
2903       record_alignment (now_seg, 1);
2904     }
2905
2906   demand_empty_rest_of_line ();
2907 }
2908
2909 static void
2910 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2911 {
2912   s_thumb (0);
2913
2914   /* The following label is the name/address of the start of a Thumb function.
2915      We need to know this for the interworking support.  */
2916   label_is_thumb_function_name = TRUE;
2917 }
2918
2919 /* Perform a .set directive, but also mark the alias as
2920    being a thumb function.  */
2921
2922 static void
2923 s_thumb_set (int equiv)
2924 {
2925   /* XXX the following is a duplicate of the code for s_set() in read.c
2926      We cannot just call that code as we need to get at the symbol that
2927      is created.  */
2928   char *    name;
2929   char      delim;
2930   char *    end_name;
2931   symbolS * symbolP;
2932
2933   /* Especial apologies for the random logic:
2934      This just grew, and could be parsed much more simply!
2935      Dean - in haste.  */
2936   delim     = get_symbol_name (& name);
2937   end_name  = input_line_pointer;
2938   (void) restore_line_pointer (delim);
2939
2940   if (*input_line_pointer != ',')
2941     {
2942       *end_name = 0;
2943       as_bad (_("expected comma after name \"%s\""), name);
2944       *end_name = delim;
2945       ignore_rest_of_line ();
2946       return;
2947     }
2948
2949   input_line_pointer++;
2950   *end_name = 0;
2951
2952   if (name[0] == '.' && name[1] == '\0')
2953     {
2954       /* XXX - this should not happen to .thumb_set.  */
2955       abort ();
2956     }
2957
2958   if ((symbolP = symbol_find (name)) == NULL
2959       && (symbolP = md_undefined_symbol (name)) == NULL)
2960     {
2961 #ifndef NO_LISTING
2962       /* When doing symbol listings, play games with dummy fragments living
2963          outside the normal fragment chain to record the file and line info
2964          for this symbol.  */
2965       if (listing & LISTING_SYMBOLS)
2966         {
2967           extern struct list_info_struct * listing_tail;
2968           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2969
2970           memset (dummy_frag, 0, sizeof (fragS));
2971           dummy_frag->fr_type = rs_fill;
2972           dummy_frag->line = listing_tail;
2973           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2974           dummy_frag->fr_symbol = symbolP;
2975         }
2976       else
2977 #endif
2978         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2979
2980 #ifdef OBJ_COFF
2981       /* "set" symbols are local unless otherwise specified.  */
2982       SF_SET_LOCAL (symbolP);
2983 #endif /* OBJ_COFF  */
2984     }                           /* Make a new symbol.  */
2985
2986   symbol_table_insert (symbolP);
2987
2988   * end_name = delim;
2989
2990   if (equiv
2991       && S_IS_DEFINED (symbolP)
2992       && S_GET_SEGMENT (symbolP) != reg_section)
2993     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2994
2995   pseudo_set (symbolP);
2996
2997   demand_empty_rest_of_line ();
2998
2999   /* XXX Now we come to the Thumb specific bit of code.  */
3000
3001   THUMB_SET_FUNC (symbolP, 1);
3002   ARM_SET_THUMB (symbolP, 1);
3003 #if defined OBJ_ELF || defined OBJ_COFF
3004   ARM_SET_INTERWORK (symbolP, support_interwork);
3005 #endif
3006 }
3007
3008 /* Directives: Mode selection.  */
3009
3010 /* .syntax [unified|divided] - choose the new unified syntax
3011    (same for Arm and Thumb encoding, modulo slight differences in what
3012    can be represented) or the old divergent syntax for each mode.  */
3013 static void
3014 s_syntax (int unused ATTRIBUTE_UNUSED)
3015 {
3016   char *name, delim;
3017
3018   delim = get_symbol_name (& name);
3019
3020   if (!strcasecmp (name, "unified"))
3021     unified_syntax = TRUE;
3022   else if (!strcasecmp (name, "divided"))
3023     unified_syntax = FALSE;
3024   else
3025     {
3026       as_bad (_("unrecognized syntax mode \"%s\""), name);
3027       return;
3028     }
3029   (void) restore_line_pointer (delim);
3030   demand_empty_rest_of_line ();
3031 }
3032
3033 /* Directives: sectioning and alignment.  */
3034
3035 static void
3036 s_bss (int ignore ATTRIBUTE_UNUSED)
3037 {
3038   /* We don't support putting frags in the BSS segment, we fake it by
3039      marking in_bss, then looking at s_skip for clues.  */
3040   subseg_set (bss_section, 0);
3041   demand_empty_rest_of_line ();
3042
3043 #ifdef md_elf_section_change_hook
3044   md_elf_section_change_hook ();
3045 #endif
3046 }
3047
3048 static void
3049 s_even (int ignore ATTRIBUTE_UNUSED)
3050 {
3051   /* Never make frag if expect extra pass.  */
3052   if (!need_pass_2)
3053     frag_align (1, 0, 0);
3054
3055   record_alignment (now_seg, 1);
3056
3057   demand_empty_rest_of_line ();
3058 }
3059
3060 /* Directives: CodeComposer Studio.  */
3061
3062 /*  .ref  (for CodeComposer Studio syntax only).  */
3063 static void
3064 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3065 {
3066   if (codecomposer_syntax)
3067     ignore_rest_of_line ();
3068   else
3069     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3070 }
3071
3072 /*  If name is not NULL, then it is used for marking the beginning of a
3073     function, whereas if it is NULL then it means the function end.  */
3074 static void
3075 asmfunc_debug (const char * name)
3076 {
3077   static const char * last_name = NULL;
3078
3079   if (name != NULL)
3080     {
3081       gas_assert (last_name == NULL);
3082       last_name = name;
3083
3084       if (debug_type == DEBUG_STABS)
3085          stabs_generate_asm_func (name, name);
3086     }
3087   else
3088     {
3089       gas_assert (last_name != NULL);
3090
3091       if (debug_type == DEBUG_STABS)
3092         stabs_generate_asm_endfunc (last_name, last_name);
3093
3094       last_name = NULL;
3095     }
3096 }
3097
3098 static void
3099 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3100 {
3101   if (codecomposer_syntax)
3102     {
3103       switch (asmfunc_state)
3104         {
3105         case OUTSIDE_ASMFUNC:
3106           asmfunc_state = WAITING_ASMFUNC_NAME;
3107           break;
3108
3109         case WAITING_ASMFUNC_NAME:
3110           as_bad (_(".asmfunc repeated."));
3111           break;
3112
3113         case WAITING_ENDASMFUNC:
3114           as_bad (_(".asmfunc without function."));
3115           break;
3116         }
3117       demand_empty_rest_of_line ();
3118     }
3119   else
3120     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3121 }
3122
3123 static void
3124 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3125 {
3126   if (codecomposer_syntax)
3127     {
3128       switch (asmfunc_state)
3129         {
3130         case OUTSIDE_ASMFUNC:
3131           as_bad (_(".endasmfunc without a .asmfunc."));
3132           break;
3133
3134         case WAITING_ASMFUNC_NAME:
3135           as_bad (_(".endasmfunc without function."));
3136           break;
3137
3138         case WAITING_ENDASMFUNC:
3139           asmfunc_state = OUTSIDE_ASMFUNC;
3140           asmfunc_debug (NULL);
3141           break;
3142         }
3143       demand_empty_rest_of_line ();
3144     }
3145   else
3146     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3147 }
3148
3149 static void
3150 s_ccs_def (int name)
3151 {
3152   if (codecomposer_syntax)
3153     s_globl (name);
3154   else
3155     as_bad (_(".def pseudo-op only available with -mccs flag."));
3156 }
3157
3158 /* Directives: Literal pools.  */
3159
3160 static literal_pool *
3161 find_literal_pool (void)
3162 {
3163   literal_pool * pool;
3164
3165   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3166     {
3167       if (pool->section == now_seg
3168           && pool->sub_section == now_subseg)
3169         break;
3170     }
3171
3172   return pool;
3173 }
3174
3175 static literal_pool *
3176 find_or_make_literal_pool (void)
3177 {
3178   /* Next literal pool ID number.  */
3179   static unsigned int latest_pool_num = 1;
3180   literal_pool *      pool;
3181
3182   pool = find_literal_pool ();
3183
3184   if (pool == NULL)
3185     {
3186       /* Create a new pool.  */
3187       pool = XNEW (literal_pool);
3188       if (! pool)
3189         return NULL;
3190
3191       pool->next_free_entry = 0;
3192       pool->section         = now_seg;
3193       pool->sub_section     = now_subseg;
3194       pool->next            = list_of_pools;
3195       pool->symbol          = NULL;
3196       pool->alignment       = 2;
3197
3198       /* Add it to the list.  */
3199       list_of_pools = pool;
3200     }
3201
3202   /* New pools, and emptied pools, will have a NULL symbol.  */
3203   if (pool->symbol == NULL)
3204     {
3205       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3206                                     (valueT) 0, &zero_address_frag);
3207       pool->id = latest_pool_num ++;
3208     }
3209
3210   /* Done.  */
3211   return pool;
3212 }
3213
3214 /* Add the literal in the global 'inst'
3215    structure to the relevant literal pool.  */
3216
3217 static int
3218 add_to_lit_pool (unsigned int nbytes)
3219 {
3220 #define PADDING_SLOT 0x1
3221 #define LIT_ENTRY_SIZE_MASK 0xFF
3222   literal_pool * pool;
3223   unsigned int entry, pool_size = 0;
3224   bfd_boolean padding_slot_p = FALSE;
3225   unsigned imm1 = 0;
3226   unsigned imm2 = 0;
3227
3228   if (nbytes == 8)
3229     {
3230       imm1 = inst.operands[1].imm;
3231       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3232                : inst.reloc.exp.X_unsigned ? 0
3233                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3234       if (target_big_endian)
3235         {
3236           imm1 = imm2;
3237           imm2 = inst.operands[1].imm;
3238         }
3239     }
3240
3241   pool = find_or_make_literal_pool ();
3242
3243   /* Check if this literal value is already in the pool.  */
3244   for (entry = 0; entry < pool->next_free_entry; entry ++)
3245     {
3246       if (nbytes == 4)
3247         {
3248           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3249               && (inst.reloc.exp.X_op == O_constant)
3250               && (pool->literals[entry].X_add_number
3251                   == inst.reloc.exp.X_add_number)
3252               && (pool->literals[entry].X_md == nbytes)
3253               && (pool->literals[entry].X_unsigned
3254                   == inst.reloc.exp.X_unsigned))
3255             break;
3256
3257           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3258               && (inst.reloc.exp.X_op == O_symbol)
3259               && (pool->literals[entry].X_add_number
3260                   == inst.reloc.exp.X_add_number)
3261               && (pool->literals[entry].X_add_symbol
3262                   == inst.reloc.exp.X_add_symbol)
3263               && (pool->literals[entry].X_op_symbol
3264                   == inst.reloc.exp.X_op_symbol)
3265               && (pool->literals[entry].X_md == nbytes))
3266             break;
3267         }
3268       else if ((nbytes == 8)
3269                && !(pool_size & 0x7)
3270                && ((entry + 1) != pool->next_free_entry)
3271                && (pool->literals[entry].X_op == O_constant)
3272                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3273                && (pool->literals[entry].X_unsigned
3274                    == inst.reloc.exp.X_unsigned)
3275                && (pool->literals[entry + 1].X_op == O_constant)
3276                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3277                && (pool->literals[entry + 1].X_unsigned
3278                    == inst.reloc.exp.X_unsigned))
3279         break;
3280
3281       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3282       if (padding_slot_p && (nbytes == 4))
3283         break;
3284
3285       pool_size += 4;
3286     }
3287
3288   /* Do we need to create a new entry?  */
3289   if (entry == pool->next_free_entry)
3290     {
3291       if (entry >= MAX_LITERAL_POOL_SIZE)
3292         {
3293           inst.error = _("literal pool overflow");
3294           return FAIL;
3295         }
3296
3297       if (nbytes == 8)
3298         {
3299           /* For 8-byte entries, we align to an 8-byte boundary,
3300              and split it into two 4-byte entries, because on 32-bit
3301              host, 8-byte constants are treated as big num, thus
3302              saved in "generic_bignum" which will be overwritten
3303              by later assignments.
3304
3305              We also need to make sure there is enough space for
3306              the split.
3307
3308              We also check to make sure the literal operand is a
3309              constant number.  */
3310           if (!(inst.reloc.exp.X_op == O_constant
3311                 || inst.reloc.exp.X_op == O_big))
3312             {
3313               inst.error = _("invalid type for literal pool");
3314               return FAIL;
3315             }
3316           else if (pool_size & 0x7)
3317             {
3318               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3319                 {
3320                   inst.error = _("literal pool overflow");
3321                   return FAIL;
3322                 }
3323
3324               pool->literals[entry] = inst.reloc.exp;
3325               pool->literals[entry].X_op = O_constant;
3326               pool->literals[entry].X_add_number = 0;
3327               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3328               pool->next_free_entry += 1;
3329               pool_size += 4;
3330             }
3331           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3332             {
3333               inst.error = _("literal pool overflow");
3334               return FAIL;
3335             }
3336
3337           pool->literals[entry] = inst.reloc.exp;
3338           pool->literals[entry].X_op = O_constant;
3339           pool->literals[entry].X_add_number = imm1;
3340           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3341           pool->literals[entry++].X_md = 4;
3342           pool->literals[entry] = inst.reloc.exp;
3343           pool->literals[entry].X_op = O_constant;
3344           pool->literals[entry].X_add_number = imm2;
3345           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3346           pool->literals[entry].X_md = 4;
3347           pool->alignment = 3;
3348           pool->next_free_entry += 1;
3349         }
3350       else
3351         {
3352           pool->literals[entry] = inst.reloc.exp;
3353           pool->literals[entry].X_md = 4;
3354         }
3355
3356 #ifdef OBJ_ELF
3357       /* PR ld/12974: Record the location of the first source line to reference
3358          this entry in the literal pool.  If it turns out during linking that the
3359          symbol does not exist we will be able to give an accurate line number for
3360          the (first use of the) missing reference.  */
3361       if (debug_type == DEBUG_DWARF2)
3362         dwarf2_where (pool->locs + entry);
3363 #endif
3364       pool->next_free_entry += 1;
3365     }
3366   else if (padding_slot_p)
3367     {
3368       pool->literals[entry] = inst.reloc.exp;
3369       pool->literals[entry].X_md = nbytes;
3370     }
3371
3372   inst.reloc.exp.X_op         = O_symbol;
3373   inst.reloc.exp.X_add_number = pool_size;
3374   inst.reloc.exp.X_add_symbol = pool->symbol;
3375
3376   return SUCCESS;
3377 }
3378
3379 bfd_boolean
3380 tc_start_label_without_colon (void)
3381 {
3382   bfd_boolean ret = TRUE;
3383
3384   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3385     {
3386       const char *label = input_line_pointer;
3387
3388       while (!is_end_of_line[(int) label[-1]])
3389         --label;
3390
3391       if (*label == '.')
3392         {
3393           as_bad (_("Invalid label '%s'"), label);
3394           ret = FALSE;
3395         }
3396
3397       asmfunc_debug (label);
3398
3399       asmfunc_state = WAITING_ENDASMFUNC;
3400     }
3401
3402   return ret;
3403 }
3404
3405 /* Can't use symbol_new here, so have to create a symbol and then at
3406    a later date assign it a value. That's what these functions do.  */
3407
3408 static void
3409 symbol_locate (symbolS *    symbolP,
3410                const char * name,       /* It is copied, the caller can modify.  */
3411                segT         segment,    /* Segment identifier (SEG_<something>).  */
3412                valueT       valu,       /* Symbol value.  */
3413                fragS *      frag)       /* Associated fragment.  */
3414 {
3415   size_t name_length;
3416   char * preserved_copy_of_name;
3417
3418   name_length = strlen (name) + 1;   /* +1 for \0.  */
3419   obstack_grow (&notes, name, name_length);
3420   preserved_copy_of_name = (char *) obstack_finish (&notes);
3421
3422 #ifdef tc_canonicalize_symbol_name
3423   preserved_copy_of_name =
3424     tc_canonicalize_symbol_name (preserved_copy_of_name);
3425 #endif
3426
3427   S_SET_NAME (symbolP, preserved_copy_of_name);
3428
3429   S_SET_SEGMENT (symbolP, segment);
3430   S_SET_VALUE (symbolP, valu);
3431   symbol_clear_list_pointers (symbolP);
3432
3433   symbol_set_frag (symbolP, frag);
3434
3435   /* Link to end of symbol chain.  */
3436   {
3437     extern int symbol_table_frozen;
3438
3439     if (symbol_table_frozen)
3440       abort ();
3441   }
3442
3443   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3444
3445   obj_symbol_new_hook (symbolP);
3446
3447 #ifdef tc_symbol_new_hook
3448   tc_symbol_new_hook (symbolP);
3449 #endif
3450
3451 #ifdef DEBUG_SYMS
3452   verify_symbol_chain (symbol_rootP, symbol_lastP);
3453 #endif /* DEBUG_SYMS  */
3454 }
3455
3456 static void
3457 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3458 {
3459   unsigned int entry;
3460   literal_pool * pool;
3461   char sym_name[20];
3462
3463   pool = find_literal_pool ();
3464   if (pool == NULL
3465       || pool->symbol == NULL
3466       || pool->next_free_entry == 0)
3467     return;
3468
3469   /* Align pool as you have word accesses.
3470      Only make a frag if we have to.  */
3471   if (!need_pass_2)
3472     frag_align (pool->alignment, 0, 0);
3473
3474   record_alignment (now_seg, 2);
3475
3476 #ifdef OBJ_ELF
3477   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3478   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3479 #endif
3480   sprintf (sym_name, "$$lit_\002%x", pool->id);
3481
3482   symbol_locate (pool->symbol, sym_name, now_seg,
3483                  (valueT) frag_now_fix (), frag_now);
3484   symbol_table_insert (pool->symbol);
3485
3486   ARM_SET_THUMB (pool->symbol, thumb_mode);
3487
3488 #if defined OBJ_COFF || defined OBJ_ELF
3489   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3490 #endif
3491
3492   for (entry = 0; entry < pool->next_free_entry; entry ++)
3493     {
3494 #ifdef OBJ_ELF
3495       if (debug_type == DEBUG_DWARF2)
3496         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3497 #endif
3498       /* First output the expression in the instruction to the pool.  */
3499       emit_expr (&(pool->literals[entry]),
3500                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3501     }
3502
3503   /* Mark the pool as empty.  */
3504   pool->next_free_entry = 0;
3505   pool->symbol = NULL;
3506 }
3507
3508 #ifdef OBJ_ELF
3509 /* Forward declarations for functions below, in the MD interface
3510    section.  */
3511 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3512 static valueT create_unwind_entry (int);
3513 static void start_unwind_section (const segT, int);
3514 static void add_unwind_opcode (valueT, int);
3515 static void flush_pending_unwind (void);
3516
3517 /* Directives: Data.  */
3518
3519 static void
3520 s_arm_elf_cons (int nbytes)
3521 {
3522   expressionS exp;
3523
3524 #ifdef md_flush_pending_output
3525   md_flush_pending_output ();
3526 #endif
3527
3528   if (is_it_end_of_statement ())
3529     {
3530       demand_empty_rest_of_line ();
3531       return;
3532     }
3533
3534 #ifdef md_cons_align
3535   md_cons_align (nbytes);
3536 #endif
3537
3538   mapping_state (MAP_DATA);
3539   do
3540     {
3541       int reloc;
3542       char *base = input_line_pointer;
3543
3544       expression (& exp);
3545
3546       if (exp.X_op != O_symbol)
3547         emit_expr (&exp, (unsigned int) nbytes);
3548       else
3549         {
3550           char *before_reloc = input_line_pointer;
3551           reloc = parse_reloc (&input_line_pointer);
3552           if (reloc == -1)
3553             {
3554               as_bad (_("unrecognized relocation suffix"));
3555               ignore_rest_of_line ();
3556               return;
3557             }
3558           else if (reloc == BFD_RELOC_UNUSED)
3559             emit_expr (&exp, (unsigned int) nbytes);
3560           else
3561             {
3562               reloc_howto_type *howto = (reloc_howto_type *)
3563                   bfd_reloc_type_lookup (stdoutput,
3564                                          (bfd_reloc_code_real_type) reloc);
3565               int size = bfd_get_reloc_size (howto);
3566
3567               if (reloc == BFD_RELOC_ARM_PLT32)
3568                 {
3569                   as_bad (_("(plt) is only valid on branch targets"));
3570                   reloc = BFD_RELOC_UNUSED;
3571                   size = 0;
3572                 }
3573
3574               if (size > nbytes)
3575                 as_bad (ngettext ("%s relocations do not fit in %d byte",
3576                                   "%s relocations do not fit in %d bytes",
3577                                   nbytes),
3578                         howto->name, nbytes);
3579               else
3580                 {
3581                   /* We've parsed an expression stopping at O_symbol.
3582                      But there may be more expression left now that we
3583                      have parsed the relocation marker.  Parse it again.
3584                      XXX Surely there is a cleaner way to do this.  */
3585                   char *p = input_line_pointer;
3586                   int offset;
3587                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3588
3589                   memcpy (save_buf, base, input_line_pointer - base);
3590                   memmove (base + (input_line_pointer - before_reloc),
3591                            base, before_reloc - base);
3592
3593                   input_line_pointer = base + (input_line_pointer-before_reloc);
3594                   expression (&exp);
3595                   memcpy (base, save_buf, p - base);
3596
3597                   offset = nbytes - size;
3598                   p = frag_more (nbytes);
3599                   memset (p, 0, nbytes);
3600                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3601                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3602                   free (save_buf);
3603                 }
3604             }
3605         }
3606     }
3607   while (*input_line_pointer++ == ',');
3608
3609   /* Put terminator back into stream.  */
3610   input_line_pointer --;
3611   demand_empty_rest_of_line ();
3612 }
3613
3614 /* Emit an expression containing a 32-bit thumb instruction.
3615    Implementation based on put_thumb32_insn.  */
3616
3617 static void
3618 emit_thumb32_expr (expressionS * exp)
3619 {
3620   expressionS exp_high = *exp;
3621
3622   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3623   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3624   exp->X_add_number &= 0xffff;
3625   emit_expr (exp, (unsigned int) THUMB_SIZE);
3626 }
3627
3628 /*  Guess the instruction size based on the opcode.  */
3629
3630 static int
3631 thumb_insn_size (int opcode)
3632 {
3633   if ((unsigned int) opcode < 0xe800u)
3634     return 2;
3635   else if ((unsigned int) opcode >= 0xe8000000u)
3636     return 4;
3637   else
3638     return 0;
3639 }
3640
3641 static bfd_boolean
3642 emit_insn (expressionS *exp, int nbytes)
3643 {
3644   int size = 0;
3645
3646   if (exp->X_op == O_constant)
3647     {
3648       size = nbytes;
3649
3650       if (size == 0)
3651         size = thumb_insn_size (exp->X_add_number);
3652
3653       if (size != 0)
3654         {
3655           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3656             {
3657               as_bad (_(".inst.n operand too big. "\
3658                         "Use .inst.w instead"));
3659               size = 0;
3660             }
3661           else
3662             {
3663               if (now_it.state == AUTOMATIC_IT_BLOCK)
3664                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3665               else
3666                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3667
3668               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3669                 emit_thumb32_expr (exp);
3670               else
3671                 emit_expr (exp, (unsigned int) size);
3672
3673               it_fsm_post_encode ();
3674             }
3675         }
3676       else
3677         as_bad (_("cannot determine Thumb instruction size. "   \
3678                   "Use .inst.n/.inst.w instead"));
3679     }
3680   else
3681     as_bad (_("constant expression required"));
3682
3683   return (size != 0);
3684 }
3685
3686 /* Like s_arm_elf_cons but do not use md_cons_align and
3687    set the mapping state to MAP_ARM/MAP_THUMB.  */
3688
3689 static void
3690 s_arm_elf_inst (int nbytes)
3691 {
3692   if (is_it_end_of_statement ())
3693     {
3694       demand_empty_rest_of_line ();
3695       return;
3696     }
3697
3698   /* Calling mapping_state () here will not change ARM/THUMB,
3699      but will ensure not to be in DATA state.  */
3700
3701   if (thumb_mode)
3702     mapping_state (MAP_THUMB);
3703   else
3704     {
3705       if (nbytes != 0)
3706         {
3707           as_bad (_("width suffixes are invalid in ARM mode"));
3708           ignore_rest_of_line ();
3709           return;
3710         }
3711
3712       nbytes = 4;
3713
3714       mapping_state (MAP_ARM);
3715     }
3716
3717   do
3718     {
3719       expressionS exp;
3720
3721       expression (& exp);
3722
3723       if (! emit_insn (& exp, nbytes))
3724         {
3725           ignore_rest_of_line ();
3726           return;
3727         }
3728     }
3729   while (*input_line_pointer++ == ',');
3730
3731   /* Put terminator back into stream.  */
3732   input_line_pointer --;
3733   demand_empty_rest_of_line ();
3734 }
3735
3736 /* Parse a .rel31 directive.  */
3737
3738 static void
3739 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3740 {
3741   expressionS exp;
3742   char *p;
3743   valueT highbit;
3744
3745   highbit = 0;
3746   if (*input_line_pointer == '1')
3747     highbit = 0x80000000;
3748   else if (*input_line_pointer != '0')
3749     as_bad (_("expected 0 or 1"));
3750
3751   input_line_pointer++;
3752   if (*input_line_pointer != ',')
3753     as_bad (_("missing comma"));
3754   input_line_pointer++;
3755
3756 #ifdef md_flush_pending_output
3757   md_flush_pending_output ();
3758 #endif
3759
3760 #ifdef md_cons_align
3761   md_cons_align (4);
3762 #endif
3763
3764   mapping_state (MAP_DATA);
3765
3766   expression (&exp);
3767
3768   p = frag_more (4);
3769   md_number_to_chars (p, highbit, 4);
3770   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3771                BFD_RELOC_ARM_PREL31);
3772
3773   demand_empty_rest_of_line ();
3774 }
3775
3776 /* Directives: AEABI stack-unwind tables.  */
3777
3778 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3779
3780 static void
3781 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3782 {
3783   demand_empty_rest_of_line ();
3784   if (unwind.proc_start)
3785     {
3786       as_bad (_("duplicate .fnstart directive"));
3787       return;
3788     }
3789
3790   /* Mark the start of the function.  */
3791   unwind.proc_start = expr_build_dot ();
3792
3793   /* Reset the rest of the unwind info.  */
3794   unwind.opcode_count = 0;
3795   unwind.table_entry = NULL;
3796   unwind.personality_routine = NULL;
3797   unwind.personality_index = -1;
3798   unwind.frame_size = 0;
3799   unwind.fp_offset = 0;
3800   unwind.fp_reg = REG_SP;
3801   unwind.fp_used = 0;
3802   unwind.sp_restored = 0;
3803 }
3804
3805
3806 /* Parse a handlerdata directive.  Creates the exception handling table entry
3807    for the function.  */
3808
3809 static void
3810 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3811 {
3812   demand_empty_rest_of_line ();
3813   if (!unwind.proc_start)
3814     as_bad (MISSING_FNSTART);
3815
3816   if (unwind.table_entry)
3817     as_bad (_("duplicate .handlerdata directive"));
3818
3819   create_unwind_entry (1);
3820 }
3821
3822 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3823
3824 static void
3825 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3826 {
3827   long where;
3828   char *ptr;
3829   valueT val;
3830   unsigned int marked_pr_dependency;
3831
3832   demand_empty_rest_of_line ();
3833
3834   if (!unwind.proc_start)
3835     {
3836       as_bad (_(".fnend directive without .fnstart"));
3837       return;
3838     }
3839
3840   /* Add eh table entry.  */
3841   if (unwind.table_entry == NULL)
3842     val = create_unwind_entry (0);
3843   else
3844     val = 0;
3845
3846   /* Add index table entry.  This is two words.  */
3847   start_unwind_section (unwind.saved_seg, 1);
3848   frag_align (2, 0, 0);
3849   record_alignment (now_seg, 2);
3850
3851   ptr = frag_more (8);
3852   memset (ptr, 0, 8);
3853   where = frag_now_fix () - 8;
3854
3855   /* Self relative offset of the function start.  */
3856   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3857            BFD_RELOC_ARM_PREL31);
3858
3859   /* Indicate dependency on EHABI-defined personality routines to the
3860      linker, if it hasn't been done already.  */
3861   marked_pr_dependency
3862     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3863   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3864       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3865     {
3866       static const char *const name[] =
3867         {
3868           "__aeabi_unwind_cpp_pr0",
3869           "__aeabi_unwind_cpp_pr1",
3870           "__aeabi_unwind_cpp_pr2"
3871         };
3872       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3873       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3874       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3875         |= 1 << unwind.personality_index;
3876     }
3877
3878   if (val)
3879     /* Inline exception table entry.  */
3880     md_number_to_chars (ptr + 4, val, 4);
3881   else
3882     /* Self relative offset of the table entry.  */
3883     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3884              BFD_RELOC_ARM_PREL31);
3885
3886   /* Restore the original section.  */
3887   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3888
3889   unwind.proc_start = NULL;
3890 }
3891
3892
3893 /* Parse an unwind_cantunwind directive.  */
3894
3895 static void
3896 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3897 {
3898   demand_empty_rest_of_line ();
3899   if (!unwind.proc_start)
3900     as_bad (MISSING_FNSTART);
3901
3902   if (unwind.personality_routine || unwind.personality_index != -1)
3903     as_bad (_("personality routine specified for cantunwind frame"));
3904
3905   unwind.personality_index = -2;
3906 }
3907
3908
3909 /* Parse a personalityindex directive.  */
3910
3911 static void
3912 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3913 {
3914   expressionS exp;
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 .personalityindex directive"));
3921
3922   expression (&exp);
3923
3924   if (exp.X_op != O_constant
3925       || exp.X_add_number < 0 || exp.X_add_number > 15)
3926     {
3927       as_bad (_("bad personality routine number"));
3928       ignore_rest_of_line ();
3929       return;
3930     }
3931
3932   unwind.personality_index = exp.X_add_number;
3933
3934   demand_empty_rest_of_line ();
3935 }
3936
3937
3938 /* Parse a personality directive.  */
3939
3940 static void
3941 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3942 {
3943   char *name, *p, c;
3944
3945   if (!unwind.proc_start)
3946     as_bad (MISSING_FNSTART);
3947
3948   if (unwind.personality_routine || unwind.personality_index != -1)
3949     as_bad (_("duplicate .personality directive"));
3950
3951   c = get_symbol_name (& name);
3952   p = input_line_pointer;
3953   if (c == '"')
3954     ++ input_line_pointer;
3955   unwind.personality_routine = symbol_find_or_make (name);
3956   *p = c;
3957   demand_empty_rest_of_line ();
3958 }
3959
3960
3961 /* Parse a directive saving core registers.  */
3962
3963 static void
3964 s_arm_unwind_save_core (void)
3965 {
3966   valueT op;
3967   long range;
3968   int n;
3969
3970   range = parse_reg_list (&input_line_pointer);
3971   if (range == FAIL)
3972     {
3973       as_bad (_("expected register list"));
3974       ignore_rest_of_line ();
3975       return;
3976     }
3977
3978   demand_empty_rest_of_line ();
3979
3980   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3981      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3982      ip because it is clobbered by calls.  */
3983   if (unwind.sp_restored && unwind.fp_reg == 12
3984       && (range & 0x3000) == 0x1000)
3985     {
3986       unwind.opcode_count--;
3987       unwind.sp_restored = 0;
3988       range = (range | 0x2000) & ~0x1000;
3989       unwind.pending_offset = 0;
3990     }
3991
3992   /* Pop r4-r15.  */
3993   if (range & 0xfff0)
3994     {
3995       /* See if we can use the short opcodes.  These pop a block of up to 8
3996          registers starting with r4, plus maybe r14.  */
3997       for (n = 0; n < 8; n++)
3998         {
3999           /* Break at the first non-saved register.      */
4000           if ((range & (1 << (n + 4))) == 0)
4001             break;
4002         }
4003       /* See if there are any other bits set.  */
4004       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
4005         {
4006           /* Use the long form.  */
4007           op = 0x8000 | ((range >> 4) & 0xfff);
4008           add_unwind_opcode (op, 2);
4009         }
4010       else
4011         {
4012           /* Use the short form.  */
4013           if (range & 0x4000)
4014             op = 0xa8; /* Pop r14.      */
4015           else
4016             op = 0xa0; /* Do not pop r14.  */
4017           op |= (n - 1);
4018           add_unwind_opcode (op, 1);
4019         }
4020     }
4021
4022   /* Pop r0-r3.  */
4023   if (range & 0xf)
4024     {
4025       op = 0xb100 | (range & 0xf);
4026       add_unwind_opcode (op, 2);
4027     }
4028
4029   /* Record the number of bytes pushed.  */
4030   for (n = 0; n < 16; n++)
4031     {
4032       if (range & (1 << n))
4033         unwind.frame_size += 4;
4034     }
4035 }
4036
4037
4038 /* Parse a directive saving FPA registers.  */
4039
4040 static void
4041 s_arm_unwind_save_fpa (int reg)
4042 {
4043   expressionS exp;
4044   int num_regs;
4045   valueT op;
4046
4047   /* Get Number of registers to transfer.  */
4048   if (skip_past_comma (&input_line_pointer) != FAIL)
4049     expression (&exp);
4050   else
4051     exp.X_op = O_illegal;
4052
4053   if (exp.X_op != O_constant)
4054     {
4055       as_bad (_("expected , <constant>"));
4056       ignore_rest_of_line ();
4057       return;
4058     }
4059
4060   num_regs = exp.X_add_number;
4061
4062   if (num_regs < 1 || num_regs > 4)
4063     {
4064       as_bad (_("number of registers must be in the range [1:4]"));
4065       ignore_rest_of_line ();
4066       return;
4067     }
4068
4069   demand_empty_rest_of_line ();
4070
4071   if (reg == 4)
4072     {
4073       /* Short form.  */
4074       op = 0xb4 | (num_regs - 1);
4075       add_unwind_opcode (op, 1);
4076     }
4077   else
4078     {
4079       /* Long form.  */
4080       op = 0xc800 | (reg << 4) | (num_regs - 1);
4081       add_unwind_opcode (op, 2);
4082     }
4083   unwind.frame_size += num_regs * 12;
4084 }
4085
4086
4087 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4088
4089 static void
4090 s_arm_unwind_save_vfp_armv6 (void)
4091 {
4092   int count;
4093   unsigned int start;
4094   valueT op;
4095   int num_vfpv3_regs = 0;
4096   int num_regs_below_16;
4097
4098   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
4099   if (count == FAIL)
4100     {
4101       as_bad (_("expected register list"));
4102       ignore_rest_of_line ();
4103       return;
4104     }
4105
4106   demand_empty_rest_of_line ();
4107
4108   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4109      than FSTMX/FLDMX-style ones).  */
4110
4111   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4112   if (start >= 16)
4113     num_vfpv3_regs = count;
4114   else if (start + count > 16)
4115     num_vfpv3_regs = start + count - 16;
4116
4117   if (num_vfpv3_regs > 0)
4118     {
4119       int start_offset = start > 16 ? start - 16 : 0;
4120       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4121       add_unwind_opcode (op, 2);
4122     }
4123
4124   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4125   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4126   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4127   if (num_regs_below_16 > 0)
4128     {
4129       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4130       add_unwind_opcode (op, 2);
4131     }
4132
4133   unwind.frame_size += count * 8;
4134 }
4135
4136
4137 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4138
4139 static void
4140 s_arm_unwind_save_vfp (void)
4141 {
4142   int count;
4143   unsigned int reg;
4144   valueT op;
4145
4146   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4147   if (count == FAIL)
4148     {
4149       as_bad (_("expected register list"));
4150       ignore_rest_of_line ();
4151       return;
4152     }
4153
4154   demand_empty_rest_of_line ();
4155
4156   if (reg == 8)
4157     {
4158       /* Short form.  */
4159       op = 0xb8 | (count - 1);
4160       add_unwind_opcode (op, 1);
4161     }
4162   else
4163     {
4164       /* Long form.  */
4165       op = 0xb300 | (reg << 4) | (count - 1);
4166       add_unwind_opcode (op, 2);
4167     }
4168   unwind.frame_size += count * 8 + 4;
4169 }
4170
4171
4172 /* Parse a directive saving iWMMXt data registers.  */
4173
4174 static void
4175 s_arm_unwind_save_mmxwr (void)
4176 {
4177   int reg;
4178   int hi_reg;
4179   int i;
4180   unsigned mask = 0;
4181   valueT op;
4182
4183   if (*input_line_pointer == '{')
4184     input_line_pointer++;
4185
4186   do
4187     {
4188       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4189
4190       if (reg == FAIL)
4191         {
4192           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4193           goto error;
4194         }
4195
4196       if (mask >> reg)
4197         as_tsktsk (_("register list not in ascending order"));
4198       mask |= 1 << reg;
4199
4200       if (*input_line_pointer == '-')
4201         {
4202           input_line_pointer++;
4203           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4204           if (hi_reg == FAIL)
4205             {
4206               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4207               goto error;
4208             }
4209           else if (reg >= hi_reg)
4210             {
4211               as_bad (_("bad register range"));
4212               goto error;
4213             }
4214           for (; reg < hi_reg; reg++)
4215             mask |= 1 << reg;
4216         }
4217     }
4218   while (skip_past_comma (&input_line_pointer) != FAIL);
4219
4220   skip_past_char (&input_line_pointer, '}');
4221
4222   demand_empty_rest_of_line ();
4223
4224   /* Generate any deferred opcodes because we're going to be looking at
4225      the list.  */
4226   flush_pending_unwind ();
4227
4228   for (i = 0; i < 16; i++)
4229     {
4230       if (mask & (1 << i))
4231         unwind.frame_size += 8;
4232     }
4233
4234   /* Attempt to combine with a previous opcode.  We do this because gcc
4235      likes to output separate unwind directives for a single block of
4236      registers.  */
4237   if (unwind.opcode_count > 0)
4238     {
4239       i = unwind.opcodes[unwind.opcode_count - 1];
4240       if ((i & 0xf8) == 0xc0)
4241         {
4242           i &= 7;
4243           /* Only merge if the blocks are contiguous.  */
4244           if (i < 6)
4245             {
4246               if ((mask & 0xfe00) == (1 << 9))
4247                 {
4248                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4249                   unwind.opcode_count--;
4250                 }
4251             }
4252           else if (i == 6 && unwind.opcode_count >= 2)
4253             {
4254               i = unwind.opcodes[unwind.opcode_count - 2];
4255               reg = i >> 4;
4256               i &= 0xf;
4257
4258               op = 0xffff << (reg - 1);
4259               if (reg > 0
4260                   && ((mask & op) == (1u << (reg - 1))))
4261                 {
4262                   op = (1 << (reg + i + 1)) - 1;
4263                   op &= ~((1 << reg) - 1);
4264                   mask |= op;
4265                   unwind.opcode_count -= 2;
4266                 }
4267             }
4268         }
4269     }
4270
4271   hi_reg = 15;
4272   /* We want to generate opcodes in the order the registers have been
4273      saved, ie. descending order.  */
4274   for (reg = 15; reg >= -1; reg--)
4275     {
4276       /* Save registers in blocks.  */
4277       if (reg < 0
4278           || !(mask & (1 << reg)))
4279         {
4280           /* We found an unsaved reg.  Generate opcodes to save the
4281              preceding block.   */
4282           if (reg != hi_reg)
4283             {
4284               if (reg == 9)
4285                 {
4286                   /* Short form.  */
4287                   op = 0xc0 | (hi_reg - 10);
4288                   add_unwind_opcode (op, 1);
4289                 }
4290               else
4291                 {
4292                   /* Long form.  */
4293                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4294                   add_unwind_opcode (op, 2);
4295                 }
4296             }
4297           hi_reg = reg - 1;
4298         }
4299     }
4300
4301   return;
4302 error:
4303   ignore_rest_of_line ();
4304 }
4305
4306 static void
4307 s_arm_unwind_save_mmxwcg (void)
4308 {
4309   int reg;
4310   int hi_reg;
4311   unsigned mask = 0;
4312   valueT op;
4313
4314   if (*input_line_pointer == '{')
4315     input_line_pointer++;
4316
4317   skip_whitespace (input_line_pointer);
4318
4319   do
4320     {
4321       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4322
4323       if (reg == FAIL)
4324         {
4325           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4326           goto error;
4327         }
4328
4329       reg -= 8;
4330       if (mask >> reg)
4331         as_tsktsk (_("register list not in ascending order"));
4332       mask |= 1 << reg;
4333
4334       if (*input_line_pointer == '-')
4335         {
4336           input_line_pointer++;
4337           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4338           if (hi_reg == FAIL)
4339             {
4340               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4341               goto error;
4342             }
4343           else if (reg >= hi_reg)
4344             {
4345               as_bad (_("bad register range"));
4346               goto error;
4347             }
4348           for (; reg < hi_reg; reg++)
4349             mask |= 1 << reg;
4350         }
4351     }
4352   while (skip_past_comma (&input_line_pointer) != FAIL);
4353
4354   skip_past_char (&input_line_pointer, '}');
4355
4356   demand_empty_rest_of_line ();
4357
4358   /* Generate any deferred opcodes because we're going to be looking at
4359      the list.  */
4360   flush_pending_unwind ();
4361
4362   for (reg = 0; reg < 16; reg++)
4363     {
4364       if (mask & (1 << reg))
4365         unwind.frame_size += 4;
4366     }
4367   op = 0xc700 | mask;
4368   add_unwind_opcode (op, 2);
4369   return;
4370 error:
4371   ignore_rest_of_line ();
4372 }
4373
4374
4375 /* Parse an unwind_save directive.
4376    If the argument is non-zero, this is a .vsave directive.  */
4377
4378 static void
4379 s_arm_unwind_save (int arch_v6)
4380 {
4381   char *peek;
4382   struct reg_entry *reg;
4383   bfd_boolean had_brace = FALSE;
4384
4385   if (!unwind.proc_start)
4386     as_bad (MISSING_FNSTART);
4387
4388   /* Figure out what sort of save we have.  */
4389   peek = input_line_pointer;
4390
4391   if (*peek == '{')
4392     {
4393       had_brace = TRUE;
4394       peek++;
4395     }
4396
4397   reg = arm_reg_parse_multi (&peek);
4398
4399   if (!reg)
4400     {
4401       as_bad (_("register expected"));
4402       ignore_rest_of_line ();
4403       return;
4404     }
4405
4406   switch (reg->type)
4407     {
4408     case REG_TYPE_FN:
4409       if (had_brace)
4410         {
4411           as_bad (_("FPA .unwind_save does not take a register list"));
4412           ignore_rest_of_line ();
4413           return;
4414         }
4415       input_line_pointer = peek;
4416       s_arm_unwind_save_fpa (reg->number);
4417       return;
4418
4419     case REG_TYPE_RN:
4420       s_arm_unwind_save_core ();
4421       return;
4422
4423     case REG_TYPE_VFD:
4424       if (arch_v6)
4425         s_arm_unwind_save_vfp_armv6 ();
4426       else
4427         s_arm_unwind_save_vfp ();
4428       return;
4429
4430     case REG_TYPE_MMXWR:
4431       s_arm_unwind_save_mmxwr ();
4432       return;
4433
4434     case REG_TYPE_MMXWCG:
4435       s_arm_unwind_save_mmxwcg ();
4436       return;
4437
4438     default:
4439       as_bad (_(".unwind_save does not support this kind of register"));
4440       ignore_rest_of_line ();
4441     }
4442 }
4443
4444
4445 /* Parse an unwind_movsp directive.  */
4446
4447 static void
4448 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4449 {
4450   int reg;
4451   valueT op;
4452   int offset;
4453
4454   if (!unwind.proc_start)
4455     as_bad (MISSING_FNSTART);
4456
4457   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4458   if (reg == FAIL)
4459     {
4460       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4461       ignore_rest_of_line ();
4462       return;
4463     }
4464
4465   /* Optional constant.  */
4466   if (skip_past_comma (&input_line_pointer) != FAIL)
4467     {
4468       if (immediate_for_directive (&offset) == FAIL)
4469         return;
4470     }
4471   else
4472     offset = 0;
4473
4474   demand_empty_rest_of_line ();
4475
4476   if (reg == REG_SP || reg == REG_PC)
4477     {
4478       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4479       return;
4480     }
4481
4482   if (unwind.fp_reg != REG_SP)
4483     as_bad (_("unexpected .unwind_movsp directive"));
4484
4485   /* Generate opcode to restore the value.  */
4486   op = 0x90 | reg;
4487   add_unwind_opcode (op, 1);
4488
4489   /* Record the information for later.  */
4490   unwind.fp_reg = reg;
4491   unwind.fp_offset = unwind.frame_size - offset;
4492   unwind.sp_restored = 1;
4493 }
4494
4495 /* Parse an unwind_pad directive.  */
4496
4497 static void
4498 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4499 {
4500   int offset;
4501
4502   if (!unwind.proc_start)
4503     as_bad (MISSING_FNSTART);
4504
4505   if (immediate_for_directive (&offset) == FAIL)
4506     return;
4507
4508   if (offset & 3)
4509     {
4510       as_bad (_("stack increment must be multiple of 4"));
4511       ignore_rest_of_line ();
4512       return;
4513     }
4514
4515   /* Don't generate any opcodes, just record the details for later.  */
4516   unwind.frame_size += offset;
4517   unwind.pending_offset += offset;
4518
4519   demand_empty_rest_of_line ();
4520 }
4521
4522 /* Parse an unwind_setfp directive.  */
4523
4524 static void
4525 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4526 {
4527   int sp_reg;
4528   int fp_reg;
4529   int offset;
4530
4531   if (!unwind.proc_start)
4532     as_bad (MISSING_FNSTART);
4533
4534   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4535   if (skip_past_comma (&input_line_pointer) == FAIL)
4536     sp_reg = FAIL;
4537   else
4538     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4539
4540   if (fp_reg == FAIL || sp_reg == FAIL)
4541     {
4542       as_bad (_("expected <reg>, <reg>"));
4543       ignore_rest_of_line ();
4544       return;
4545     }
4546
4547   /* Optional constant.  */
4548   if (skip_past_comma (&input_line_pointer) != FAIL)
4549     {
4550       if (immediate_for_directive (&offset) == FAIL)
4551         return;
4552     }
4553   else
4554     offset = 0;
4555
4556   demand_empty_rest_of_line ();
4557
4558   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4559     {
4560       as_bad (_("register must be either sp or set by a previous"
4561                 "unwind_movsp directive"));
4562       return;
4563     }
4564
4565   /* Don't generate any opcodes, just record the information for later.  */
4566   unwind.fp_reg = fp_reg;
4567   unwind.fp_used = 1;
4568   if (sp_reg == REG_SP)
4569     unwind.fp_offset = unwind.frame_size - offset;
4570   else
4571     unwind.fp_offset -= offset;
4572 }
4573
4574 /* Parse an unwind_raw directive.  */
4575
4576 static void
4577 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4578 {
4579   expressionS exp;
4580   /* This is an arbitrary limit.         */
4581   unsigned char op[16];
4582   int count;
4583
4584   if (!unwind.proc_start)
4585     as_bad (MISSING_FNSTART);
4586
4587   expression (&exp);
4588   if (exp.X_op == O_constant
4589       && skip_past_comma (&input_line_pointer) != FAIL)
4590     {
4591       unwind.frame_size += exp.X_add_number;
4592       expression (&exp);
4593     }
4594   else
4595     exp.X_op = O_illegal;
4596
4597   if (exp.X_op != O_constant)
4598     {
4599       as_bad (_("expected <offset>, <opcode>"));
4600       ignore_rest_of_line ();
4601       return;
4602     }
4603
4604   count = 0;
4605
4606   /* Parse the opcode.  */
4607   for (;;)
4608     {
4609       if (count >= 16)
4610         {
4611           as_bad (_("unwind opcode too long"));
4612           ignore_rest_of_line ();
4613         }
4614       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4615         {
4616           as_bad (_("invalid unwind opcode"));
4617           ignore_rest_of_line ();
4618           return;
4619         }
4620       op[count++] = exp.X_add_number;
4621
4622       /* Parse the next byte.  */
4623       if (skip_past_comma (&input_line_pointer) == FAIL)
4624         break;
4625
4626       expression (&exp);
4627     }
4628
4629   /* Add the opcode bytes in reverse order.  */
4630   while (count--)
4631     add_unwind_opcode (op[count], 1);
4632
4633   demand_empty_rest_of_line ();
4634 }
4635
4636
4637 /* Parse a .eabi_attribute directive.  */
4638
4639 static void
4640 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4641 {
4642   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4643
4644   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4645     attributes_set_explicitly[tag] = 1;
4646 }
4647
4648 /* Emit a tls fix for the symbol.  */
4649
4650 static void
4651 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4652 {
4653   char *p;
4654   expressionS exp;
4655 #ifdef md_flush_pending_output
4656   md_flush_pending_output ();
4657 #endif
4658
4659 #ifdef md_cons_align
4660   md_cons_align (4);
4661 #endif
4662
4663   /* Since we're just labelling the code, there's no need to define a
4664      mapping symbol.  */
4665   expression (&exp);
4666   p = obstack_next_free (&frchain_now->frch_obstack);
4667   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4668                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4669                : BFD_RELOC_ARM_TLS_DESCSEQ);
4670 }
4671 #endif /* OBJ_ELF */
4672
4673 static void s_arm_arch (int);
4674 static void s_arm_object_arch (int);
4675 static void s_arm_cpu (int);
4676 static void s_arm_fpu (int);
4677 static void s_arm_arch_extension (int);
4678
4679 #ifdef TE_PE
4680
4681 static void
4682 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4683 {
4684   expressionS exp;
4685
4686   do
4687     {
4688       expression (&exp);
4689       if (exp.X_op == O_symbol)
4690         exp.X_op = O_secrel;
4691
4692       emit_expr (&exp, 4);
4693     }
4694   while (*input_line_pointer++ == ',');
4695
4696   input_line_pointer--;
4697   demand_empty_rest_of_line ();
4698 }
4699 #endif /* TE_PE */
4700
4701 /* This table describes all the machine specific pseudo-ops the assembler
4702    has to support.  The fields are:
4703      pseudo-op name without dot
4704      function to call to execute this pseudo-op
4705      Integer arg to pass to the function.  */
4706
4707 const pseudo_typeS md_pseudo_table[] =
4708 {
4709   /* Never called because '.req' does not start a line.  */
4710   { "req",         s_req,         0 },
4711   /* Following two are likewise never called.  */
4712   { "dn",          s_dn,          0 },
4713   { "qn",          s_qn,          0 },
4714   { "unreq",       s_unreq,       0 },
4715   { "bss",         s_bss,         0 },
4716   { "align",       s_align_ptwo,  2 },
4717   { "arm",         s_arm,         0 },
4718   { "thumb",       s_thumb,       0 },
4719   { "code",        s_code,        0 },
4720   { "force_thumb", s_force_thumb, 0 },
4721   { "thumb_func",  s_thumb_func,  0 },
4722   { "thumb_set",   s_thumb_set,   0 },
4723   { "even",        s_even,        0 },
4724   { "ltorg",       s_ltorg,       0 },
4725   { "pool",        s_ltorg,       0 },
4726   { "syntax",      s_syntax,      0 },
4727   { "cpu",         s_arm_cpu,     0 },
4728   { "arch",        s_arm_arch,    0 },
4729   { "object_arch", s_arm_object_arch,   0 },
4730   { "fpu",         s_arm_fpu,     0 },
4731   { "arch_extension", s_arm_arch_extension, 0 },
4732 #ifdef OBJ_ELF
4733   { "word",             s_arm_elf_cons, 4 },
4734   { "long",             s_arm_elf_cons, 4 },
4735   { "inst.n",           s_arm_elf_inst, 2 },
4736   { "inst.w",           s_arm_elf_inst, 4 },
4737   { "inst",             s_arm_elf_inst, 0 },
4738   { "rel31",            s_arm_rel31,      0 },
4739   { "fnstart",          s_arm_unwind_fnstart,   0 },
4740   { "fnend",            s_arm_unwind_fnend,     0 },
4741   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4742   { "personality",      s_arm_unwind_personality, 0 },
4743   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4744   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4745   { "save",             s_arm_unwind_save,      0 },
4746   { "vsave",            s_arm_unwind_save,      1 },
4747   { "movsp",            s_arm_unwind_movsp,     0 },
4748   { "pad",              s_arm_unwind_pad,       0 },
4749   { "setfp",            s_arm_unwind_setfp,     0 },
4750   { "unwind_raw",       s_arm_unwind_raw,       0 },
4751   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4752   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4753 #else
4754   { "word",        cons, 4},
4755
4756   /* These are used for dwarf.  */
4757   {"2byte", cons, 2},
4758   {"4byte", cons, 4},
4759   {"8byte", cons, 8},
4760   /* These are used for dwarf2.  */
4761   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4762   { "loc",  dwarf2_directive_loc,  0 },
4763   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4764 #endif
4765   { "extend",      float_cons, 'x' },
4766   { "ldouble",     float_cons, 'x' },
4767   { "packed",      float_cons, 'p' },
4768 #ifdef TE_PE
4769   {"secrel32", pe_directive_secrel, 0},
4770 #endif
4771
4772   /* These are for compatibility with CodeComposer Studio.  */
4773   {"ref",          s_ccs_ref,        0},
4774   {"def",          s_ccs_def,        0},
4775   {"asmfunc",      s_ccs_asmfunc,    0},
4776   {"endasmfunc",   s_ccs_endasmfunc, 0},
4777
4778   { 0, 0, 0 }
4779 };
4780 \f
4781 /* Parser functions used exclusively in instruction operands.  */
4782
4783 /* Generic immediate-value read function for use in insn parsing.
4784    STR points to the beginning of the immediate (the leading #);
4785    VAL receives the value; if the value is outside [MIN, MAX]
4786    issue an error.  PREFIX_OPT is true if the immediate prefix is
4787    optional.  */
4788
4789 static int
4790 parse_immediate (char **str, int *val, int min, int max,
4791                  bfd_boolean prefix_opt)
4792 {
4793   expressionS exp;
4794
4795   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4796   if (exp.X_op != O_constant)
4797     {
4798       inst.error = _("constant expression required");
4799       return FAIL;
4800     }
4801
4802   if (exp.X_add_number < min || exp.X_add_number > max)
4803     {
4804       inst.error = _("immediate value out of range");
4805       return FAIL;
4806     }
4807
4808   *val = exp.X_add_number;
4809   return SUCCESS;
4810 }
4811
4812 /* Less-generic immediate-value read function with the possibility of loading a
4813    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4814    instructions. Puts the result directly in inst.operands[i].  */
4815
4816 static int
4817 parse_big_immediate (char **str, int i, expressionS *in_exp,
4818                      bfd_boolean allow_symbol_p)
4819 {
4820   expressionS exp;
4821   expressionS *exp_p = in_exp ? in_exp : &exp;
4822   char *ptr = *str;
4823
4824   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4825
4826   if (exp_p->X_op == O_constant)
4827     {
4828       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4829       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4830          O_constant.  We have to be careful not to break compilation for
4831          32-bit X_add_number, though.  */
4832       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4833         {
4834           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4835           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4836                                   & 0xffffffff);
4837           inst.operands[i].regisimm = 1;
4838         }
4839     }
4840   else if (exp_p->X_op == O_big
4841            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4842     {
4843       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4844
4845       /* Bignums have their least significant bits in
4846          generic_bignum[0]. Make sure we put 32 bits in imm and
4847          32 bits in reg,  in a (hopefully) portable way.  */
4848       gas_assert (parts != 0);
4849
4850       /* Make sure that the number is not too big.
4851          PR 11972: Bignums can now be sign-extended to the
4852          size of a .octa so check that the out of range bits
4853          are all zero or all one.  */
4854       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4855         {
4856           LITTLENUM_TYPE m = -1;
4857
4858           if (generic_bignum[parts * 2] != 0
4859               && generic_bignum[parts * 2] != m)
4860             return FAIL;
4861
4862           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4863             if (generic_bignum[j] != generic_bignum[j-1])
4864               return FAIL;
4865         }
4866
4867       inst.operands[i].imm = 0;
4868       for (j = 0; j < parts; j++, idx++)
4869         inst.operands[i].imm |= generic_bignum[idx]
4870                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4871       inst.operands[i].reg = 0;
4872       for (j = 0; j < parts; j++, idx++)
4873         inst.operands[i].reg |= generic_bignum[idx]
4874                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4875       inst.operands[i].regisimm = 1;
4876     }
4877   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4878     return FAIL;
4879
4880   *str = ptr;
4881
4882   return SUCCESS;
4883 }
4884
4885 /* Returns the pseudo-register number of an FPA immediate constant,
4886    or FAIL if there isn't a valid constant here.  */
4887
4888 static int
4889 parse_fpa_immediate (char ** str)
4890 {
4891   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4892   char *         save_in;
4893   expressionS    exp;
4894   int            i;
4895   int            j;
4896
4897   /* First try and match exact strings, this is to guarantee
4898      that some formats will work even for cross assembly.  */
4899
4900   for (i = 0; fp_const[i]; i++)
4901     {
4902       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4903         {
4904           char *start = *str;
4905
4906           *str += strlen (fp_const[i]);
4907           if (is_end_of_line[(unsigned char) **str])
4908             return i + 8;
4909           *str = start;
4910         }
4911     }
4912
4913   /* Just because we didn't get a match doesn't mean that the constant
4914      isn't valid, just that it is in a format that we don't
4915      automatically recognize.  Try parsing it with the standard
4916      expression routines.  */
4917
4918   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4919
4920   /* Look for a raw floating point number.  */
4921   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4922       && is_end_of_line[(unsigned char) *save_in])
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 = save_in;
4935               return i + 8;
4936             }
4937         }
4938     }
4939
4940   /* Try and parse a more complex expression, this will probably fail
4941      unless the code uses a floating point prefix (eg "0f").  */
4942   save_in = input_line_pointer;
4943   input_line_pointer = *str;
4944   if (expression (&exp) == absolute_section
4945       && exp.X_op == O_big
4946       && exp.X_add_number < 0)
4947     {
4948       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4949          Ditto for 15.  */
4950 #define X_PRECISION 5
4951 #define E_PRECISION 15L
4952       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
4953         {
4954           for (i = 0; i < NUM_FLOAT_VALS; i++)
4955             {
4956               for (j = 0; j < MAX_LITTLENUMS; j++)
4957                 {
4958                   if (words[j] != fp_values[i][j])
4959                     break;
4960                 }
4961
4962               if (j == MAX_LITTLENUMS)
4963                 {
4964                   *str = input_line_pointer;
4965                   input_line_pointer = save_in;
4966                   return i + 8;
4967                 }
4968             }
4969         }
4970     }
4971
4972   *str = input_line_pointer;
4973   input_line_pointer = save_in;
4974   inst.error = _("invalid FPA immediate expression");
4975   return FAIL;
4976 }
4977
4978 /* Returns 1 if a number has "quarter-precision" float format
4979    0baBbbbbbc defgh000 00000000 00000000.  */
4980
4981 static int
4982 is_quarter_float (unsigned imm)
4983 {
4984   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4985   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4986 }
4987
4988
4989 /* Detect the presence of a floating point or integer zero constant,
4990    i.e. #0.0 or #0.  */
4991
4992 static bfd_boolean
4993 parse_ifimm_zero (char **in)
4994 {
4995   int error_code;
4996
4997   if (!is_immediate_prefix (**in))
4998     {
4999       /* In unified syntax, all prefixes are optional.  */
5000       if (!unified_syntax)
5001         return FALSE;
5002     }
5003   else
5004     ++*in;
5005
5006   /* Accept #0x0 as a synonym for #0.  */
5007   if (strncmp (*in, "0x", 2) == 0)
5008     {
5009       int val;
5010       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
5011         return FALSE;
5012       return TRUE;
5013     }
5014
5015   error_code = atof_generic (in, ".", EXP_CHARS,
5016                              &generic_floating_point_number);
5017
5018   if (!error_code
5019       && generic_floating_point_number.sign == '+'
5020       && (generic_floating_point_number.low
5021           > generic_floating_point_number.leader))
5022     return TRUE;
5023
5024   return FALSE;
5025 }
5026
5027 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5028    0baBbbbbbc defgh000 00000000 00000000.
5029    The zero and minus-zero cases need special handling, since they can't be
5030    encoded in the "quarter-precision" float format, but can nonetheless be
5031    loaded as integer constants.  */
5032
5033 static unsigned
5034 parse_qfloat_immediate (char **ccp, int *immed)
5035 {
5036   char *str = *ccp;
5037   char *fpnum;
5038   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5039   int found_fpchar = 0;
5040
5041   skip_past_char (&str, '#');
5042
5043   /* We must not accidentally parse an integer as a floating-point number. Make
5044      sure that the value we parse is not an integer by checking for special
5045      characters '.' or 'e'.
5046      FIXME: This is a horrible hack, but doing better is tricky because type
5047      information isn't in a very usable state at parse time.  */
5048   fpnum = str;
5049   skip_whitespace (fpnum);
5050
5051   if (strncmp (fpnum, "0x", 2) == 0)
5052     return FAIL;
5053   else
5054     {
5055       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5056         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5057           {
5058             found_fpchar = 1;
5059             break;
5060           }
5061
5062       if (!found_fpchar)
5063         return FAIL;
5064     }
5065
5066   if ((str = atof_ieee (str, 's', words)) != NULL)
5067     {
5068       unsigned fpword = 0;
5069       int i;
5070
5071       /* Our FP word must be 32 bits (single-precision FP).  */
5072       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5073         {
5074           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5075           fpword |= words[i];
5076         }
5077
5078       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5079         *immed = fpword;
5080       else
5081         return FAIL;
5082
5083       *ccp = str;
5084
5085       return SUCCESS;
5086     }
5087
5088   return FAIL;
5089 }
5090
5091 /* Shift operands.  */
5092 enum shift_kind
5093 {
5094   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5095 };
5096
5097 struct asm_shift_name
5098 {
5099   const char      *name;
5100   enum shift_kind  kind;
5101 };
5102
5103 /* Third argument to parse_shift.  */
5104 enum parse_shift_mode
5105 {
5106   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5107   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5108   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5109   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5110   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5111 };
5112
5113 /* Parse a <shift> specifier on an ARM data processing instruction.
5114    This has three forms:
5115
5116      (LSL|LSR|ASL|ASR|ROR) Rs
5117      (LSL|LSR|ASL|ASR|ROR) #imm
5118      RRX
5119
5120    Note that ASL is assimilated to LSL in the instruction encoding, and
5121    RRX to ROR #0 (which cannot be written as such).  */
5122
5123 static int
5124 parse_shift (char **str, int i, enum parse_shift_mode mode)
5125 {
5126   const struct asm_shift_name *shift_name;
5127   enum shift_kind shift;
5128   char *s = *str;
5129   char *p = s;
5130   int reg;
5131
5132   for (p = *str; ISALPHA (*p); p++)
5133     ;
5134
5135   if (p == *str)
5136     {
5137       inst.error = _("shift expression expected");
5138       return FAIL;
5139     }
5140
5141   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5142                                                             p - *str);
5143
5144   if (shift_name == NULL)
5145     {
5146       inst.error = _("shift expression expected");
5147       return FAIL;
5148     }
5149
5150   shift = shift_name->kind;
5151
5152   switch (mode)
5153     {
5154     case NO_SHIFT_RESTRICT:
5155     case SHIFT_IMMEDIATE:   break;
5156
5157     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5158       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5159         {
5160           inst.error = _("'LSL' or 'ASR' required");
5161           return FAIL;
5162         }
5163       break;
5164
5165     case SHIFT_LSL_IMMEDIATE:
5166       if (shift != SHIFT_LSL)
5167         {
5168           inst.error = _("'LSL' required");
5169           return FAIL;
5170         }
5171       break;
5172
5173     case SHIFT_ASR_IMMEDIATE:
5174       if (shift != SHIFT_ASR)
5175         {
5176           inst.error = _("'ASR' required");
5177           return FAIL;
5178         }
5179       break;
5180
5181     default: abort ();
5182     }
5183
5184   if (shift != SHIFT_RRX)
5185     {
5186       /* Whitespace can appear here if the next thing is a bare digit.  */
5187       skip_whitespace (p);
5188
5189       if (mode == NO_SHIFT_RESTRICT
5190           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5191         {
5192           inst.operands[i].imm = reg;
5193           inst.operands[i].immisreg = 1;
5194         }
5195       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5196         return FAIL;
5197     }
5198   inst.operands[i].shift_kind = shift;
5199   inst.operands[i].shifted = 1;
5200   *str = p;
5201   return SUCCESS;
5202 }
5203
5204 /* Parse a <shifter_operand> for an ARM data processing instruction:
5205
5206       #<immediate>
5207       #<immediate>, <rotate>
5208       <Rm>
5209       <Rm>, <shift>
5210
5211    where <shift> is defined by parse_shift above, and <rotate> is a
5212    multiple of 2 between 0 and 30.  Validation of immediate operands
5213    is deferred to md_apply_fix.  */
5214
5215 static int
5216 parse_shifter_operand (char **str, int i)
5217 {
5218   int value;
5219   expressionS exp;
5220
5221   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5222     {
5223       inst.operands[i].reg = value;
5224       inst.operands[i].isreg = 1;
5225
5226       /* parse_shift will override this if appropriate */
5227       inst.reloc.exp.X_op = O_constant;
5228       inst.reloc.exp.X_add_number = 0;
5229
5230       if (skip_past_comma (str) == FAIL)
5231         return SUCCESS;
5232
5233       /* Shift operation on register.  */
5234       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5235     }
5236
5237   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5238     return FAIL;
5239
5240   if (skip_past_comma (str) == SUCCESS)
5241     {
5242       /* #x, y -- ie explicit rotation by Y.  */
5243       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5244         return FAIL;
5245
5246       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5247         {
5248           inst.error = _("constant expression expected");
5249           return FAIL;
5250         }
5251
5252       value = exp.X_add_number;
5253       if (value < 0 || value > 30 || value % 2 != 0)
5254         {
5255           inst.error = _("invalid rotation");
5256           return FAIL;
5257         }
5258       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5259         {
5260           inst.error = _("invalid constant");
5261           return FAIL;
5262         }
5263
5264       /* Encode as specified.  */
5265       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5266       return SUCCESS;
5267     }
5268
5269   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5270   inst.reloc.pc_rel = 0;
5271   return SUCCESS;
5272 }
5273
5274 /* Group relocation information.  Each entry in the table contains the
5275    textual name of the relocation as may appear in assembler source
5276    and must end with a colon.
5277    Along with this textual name are the relocation codes to be used if
5278    the corresponding instruction is an ALU instruction (ADD or SUB only),
5279    an LDR, an LDRS, or an LDC.  */
5280
5281 struct group_reloc_table_entry
5282 {
5283   const char *name;
5284   int alu_code;
5285   int ldr_code;
5286   int ldrs_code;
5287   int ldc_code;
5288 };
5289
5290 typedef enum
5291 {
5292   /* Varieties of non-ALU group relocation.  */
5293
5294   GROUP_LDR,
5295   GROUP_LDRS,
5296   GROUP_LDC
5297 } group_reloc_type;
5298
5299 static struct group_reloc_table_entry group_reloc_table[] =
5300   { /* Program counter relative: */
5301     { "pc_g0_nc",
5302       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5303       0,                                /* LDR */
5304       0,                                /* LDRS */
5305       0 },                              /* LDC */
5306     { "pc_g0",
5307       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5308       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5309       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5310       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5311     { "pc_g1_nc",
5312       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5313       0,                                /* LDR */
5314       0,                                /* LDRS */
5315       0 },                              /* LDC */
5316     { "pc_g1",
5317       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5318       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5319       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5320       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5321     { "pc_g2",
5322       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5323       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5324       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5325       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5326     /* Section base relative */
5327     { "sb_g0_nc",
5328       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5329       0,                                /* LDR */
5330       0,                                /* LDRS */
5331       0 },                              /* LDC */
5332     { "sb_g0",
5333       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5334       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5335       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5336       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5337     { "sb_g1_nc",
5338       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5339       0,                                /* LDR */
5340       0,                                /* LDRS */
5341       0 },                              /* LDC */
5342     { "sb_g1",
5343       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5344       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5345       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5346       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5347     { "sb_g2",
5348       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5349       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5350       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5351       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5352     /* Absolute thumb alu relocations.  */
5353     { "lower0_7",
5354       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5355       0,                                /* LDR.  */
5356       0,                                /* LDRS.  */
5357       0 },                              /* LDC.  */
5358     { "lower8_15",
5359       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5360       0,                                /* LDR.  */
5361       0,                                /* LDRS.  */
5362       0 },                              /* LDC.  */
5363     { "upper0_7",
5364       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5365       0,                                /* LDR.  */
5366       0,                                /* LDRS.  */
5367       0 },                              /* LDC.  */
5368     { "upper8_15",
5369       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5370       0,                                /* LDR.  */
5371       0,                                /* LDRS.  */
5372       0 } };                            /* LDC.  */
5373
5374 /* Given the address of a pointer pointing to the textual name of a group
5375    relocation as may appear in assembler source, attempt to find its details
5376    in group_reloc_table.  The pointer will be updated to the character after
5377    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5378    otherwise.  On success, *entry will be updated to point at the relevant
5379    group_reloc_table entry. */
5380
5381 static int
5382 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5383 {
5384   unsigned int i;
5385   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5386     {
5387       int length = strlen (group_reloc_table[i].name);
5388
5389       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5390           && (*str)[length] == ':')
5391         {
5392           *out = &group_reloc_table[i];
5393           *str += (length + 1);
5394           return SUCCESS;
5395         }
5396     }
5397
5398   return FAIL;
5399 }
5400
5401 /* Parse a <shifter_operand> for an ARM data processing instruction
5402    (as for parse_shifter_operand) where group relocations are allowed:
5403
5404       #<immediate>
5405       #<immediate>, <rotate>
5406       #:<group_reloc>:<expression>
5407       <Rm>
5408       <Rm>, <shift>
5409
5410    where <group_reloc> is one of the strings defined in group_reloc_table.
5411    The hashes are optional.
5412
5413    Everything else is as for parse_shifter_operand.  */
5414
5415 static parse_operand_result
5416 parse_shifter_operand_group_reloc (char **str, int i)
5417 {
5418   /* Determine if we have the sequence of characters #: or just :
5419      coming next.  If we do, then we check for a group relocation.
5420      If we don't, punt the whole lot to parse_shifter_operand.  */
5421
5422   if (((*str)[0] == '#' && (*str)[1] == ':')
5423       || (*str)[0] == ':')
5424     {
5425       struct group_reloc_table_entry *entry;
5426
5427       if ((*str)[0] == '#')
5428         (*str) += 2;
5429       else
5430         (*str)++;
5431
5432       /* Try to parse a group relocation.  Anything else is an error.  */
5433       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5434         {
5435           inst.error = _("unknown group relocation");
5436           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5437         }
5438
5439       /* We now have the group relocation table entry corresponding to
5440          the name in the assembler source.  Next, we parse the expression.  */
5441       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5442         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5443
5444       /* Record the relocation type (always the ALU variant here).  */
5445       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5446       gas_assert (inst.reloc.type != 0);
5447
5448       return PARSE_OPERAND_SUCCESS;
5449     }
5450   else
5451     return parse_shifter_operand (str, i) == SUCCESS
5452            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5453
5454   /* Never reached.  */
5455 }
5456
5457 /* Parse a Neon alignment expression.  Information is written to
5458    inst.operands[i].  We assume the initial ':' has been skipped.
5459
5460    align        .imm = align << 8, .immisalign=1, .preind=0  */
5461 static parse_operand_result
5462 parse_neon_alignment (char **str, int i)
5463 {
5464   char *p = *str;
5465   expressionS exp;
5466
5467   my_get_expression (&exp, &p, GE_NO_PREFIX);
5468
5469   if (exp.X_op != O_constant)
5470     {
5471       inst.error = _("alignment must be constant");
5472       return PARSE_OPERAND_FAIL;
5473     }
5474
5475   inst.operands[i].imm = exp.X_add_number << 8;
5476   inst.operands[i].immisalign = 1;
5477   /* Alignments are not pre-indexes.  */
5478   inst.operands[i].preind = 0;
5479
5480   *str = p;
5481   return PARSE_OPERAND_SUCCESS;
5482 }
5483
5484 /* Parse all forms of an ARM address expression.  Information is written
5485    to inst.operands[i] and/or inst.reloc.
5486
5487    Preindexed addressing (.preind=1):
5488
5489    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5490    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5491    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5492                        .shift_kind=shift .reloc.exp=shift_imm
5493
5494    These three may have a trailing ! which causes .writeback to be set also.
5495
5496    Postindexed addressing (.postind=1, .writeback=1):
5497
5498    [Rn], #offset       .reg=Rn .reloc.exp=offset
5499    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5500    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5501                        .shift_kind=shift .reloc.exp=shift_imm
5502
5503    Unindexed addressing (.preind=0, .postind=0):
5504
5505    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5506
5507    Other:
5508
5509    [Rn]{!}             shorthand for [Rn,#0]{!}
5510    =immediate          .isreg=0 .reloc.exp=immediate
5511    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5512
5513   It is the caller's responsibility to check for addressing modes not
5514   supported by the instruction, and to set inst.reloc.type.  */
5515
5516 static parse_operand_result
5517 parse_address_main (char **str, int i, int group_relocations,
5518                     group_reloc_type group_type)
5519 {
5520   char *p = *str;
5521   int reg;
5522
5523   if (skip_past_char (&p, '[') == FAIL)
5524     {
5525       if (skip_past_char (&p, '=') == FAIL)
5526         {
5527           /* Bare address - translate to PC-relative offset.  */
5528           inst.reloc.pc_rel = 1;
5529           inst.operands[i].reg = REG_PC;
5530           inst.operands[i].isreg = 1;
5531           inst.operands[i].preind = 1;
5532
5533           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5534             return PARSE_OPERAND_FAIL;
5535         }
5536       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5537                                     /*allow_symbol_p=*/TRUE))
5538         return PARSE_OPERAND_FAIL;
5539
5540       *str = p;
5541       return PARSE_OPERAND_SUCCESS;
5542     }
5543
5544   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5545   skip_whitespace (p);
5546
5547   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5548     {
5549       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5550       return PARSE_OPERAND_FAIL;
5551     }
5552   inst.operands[i].reg = reg;
5553   inst.operands[i].isreg = 1;
5554
5555   if (skip_past_comma (&p) == SUCCESS)
5556     {
5557       inst.operands[i].preind = 1;
5558
5559       if (*p == '+') p++;
5560       else if (*p == '-') p++, inst.operands[i].negative = 1;
5561
5562       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5563         {
5564           inst.operands[i].imm = reg;
5565           inst.operands[i].immisreg = 1;
5566
5567           if (skip_past_comma (&p) == SUCCESS)
5568             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5569               return PARSE_OPERAND_FAIL;
5570         }
5571       else if (skip_past_char (&p, ':') == SUCCESS)
5572         {
5573           /* FIXME: '@' should be used here, but it's filtered out by generic
5574              code before we get to see it here. This may be subject to
5575              change.  */
5576           parse_operand_result result = parse_neon_alignment (&p, i);
5577
5578           if (result != PARSE_OPERAND_SUCCESS)
5579             return result;
5580         }
5581       else
5582         {
5583           if (inst.operands[i].negative)
5584             {
5585               inst.operands[i].negative = 0;
5586               p--;
5587             }
5588
5589           if (group_relocations
5590               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5591             {
5592               struct group_reloc_table_entry *entry;
5593
5594               /* Skip over the #: or : sequence.  */
5595               if (*p == '#')
5596                 p += 2;
5597               else
5598                 p++;
5599
5600               /* Try to parse a group relocation.  Anything else is an
5601                  error.  */
5602               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5603                 {
5604                   inst.error = _("unknown group relocation");
5605                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5606                 }
5607
5608               /* We now have the group relocation table entry corresponding to
5609                  the name in the assembler source.  Next, we parse the
5610                  expression.  */
5611               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5612                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5613
5614               /* Record the relocation type.  */
5615               switch (group_type)
5616                 {
5617                   case GROUP_LDR:
5618                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5619                     break;
5620
5621                   case GROUP_LDRS:
5622                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5623                     break;
5624
5625                   case GROUP_LDC:
5626                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5627                     break;
5628
5629                   default:
5630                     gas_assert (0);
5631                 }
5632
5633               if (inst.reloc.type == 0)
5634                 {
5635                   inst.error = _("this group relocation is not allowed on this instruction");
5636                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5637                 }
5638             }
5639           else
5640             {
5641               char *q = p;
5642
5643               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5644                 return PARSE_OPERAND_FAIL;
5645               /* If the offset is 0, find out if it's a +0 or -0.  */
5646               if (inst.reloc.exp.X_op == O_constant
5647                   && inst.reloc.exp.X_add_number == 0)
5648                 {
5649                   skip_whitespace (q);
5650                   if (*q == '#')
5651                     {
5652                       q++;
5653                       skip_whitespace (q);
5654                     }
5655                   if (*q == '-')
5656                     inst.operands[i].negative = 1;
5657                 }
5658             }
5659         }
5660     }
5661   else if (skip_past_char (&p, ':') == SUCCESS)
5662     {
5663       /* FIXME: '@' should be used here, but it's filtered out by generic code
5664          before we get to see it here. This may be subject to change.  */
5665       parse_operand_result result = parse_neon_alignment (&p, i);
5666
5667       if (result != PARSE_OPERAND_SUCCESS)
5668         return result;
5669     }
5670
5671   if (skip_past_char (&p, ']') == FAIL)
5672     {
5673       inst.error = _("']' expected");
5674       return PARSE_OPERAND_FAIL;
5675     }
5676
5677   if (skip_past_char (&p, '!') == SUCCESS)
5678     inst.operands[i].writeback = 1;
5679
5680   else if (skip_past_comma (&p) == SUCCESS)
5681     {
5682       if (skip_past_char (&p, '{') == SUCCESS)
5683         {
5684           /* [Rn], {expr} - unindexed, with option */
5685           if (parse_immediate (&p, &inst.operands[i].imm,
5686                                0, 255, TRUE) == FAIL)
5687             return PARSE_OPERAND_FAIL;
5688
5689           if (skip_past_char (&p, '}') == FAIL)
5690             {
5691               inst.error = _("'}' expected at end of 'option' field");
5692               return PARSE_OPERAND_FAIL;
5693             }
5694           if (inst.operands[i].preind)
5695             {
5696               inst.error = _("cannot combine index with option");
5697               return PARSE_OPERAND_FAIL;
5698             }
5699           *str = p;
5700           return PARSE_OPERAND_SUCCESS;
5701         }
5702       else
5703         {
5704           inst.operands[i].postind = 1;
5705           inst.operands[i].writeback = 1;
5706
5707           if (inst.operands[i].preind)
5708             {
5709               inst.error = _("cannot combine pre- and post-indexing");
5710               return PARSE_OPERAND_FAIL;
5711             }
5712
5713           if (*p == '+') p++;
5714           else if (*p == '-') p++, inst.operands[i].negative = 1;
5715
5716           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5717             {
5718               /* We might be using the immediate for alignment already. If we
5719                  are, OR the register number into the low-order bits.  */
5720               if (inst.operands[i].immisalign)
5721                 inst.operands[i].imm |= reg;
5722               else
5723                 inst.operands[i].imm = reg;
5724               inst.operands[i].immisreg = 1;
5725
5726               if (skip_past_comma (&p) == SUCCESS)
5727                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5728                   return PARSE_OPERAND_FAIL;
5729             }
5730           else
5731             {
5732               char *q = p;
5733
5734               if (inst.operands[i].negative)
5735                 {
5736                   inst.operands[i].negative = 0;
5737                   p--;
5738                 }
5739               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5740                 return PARSE_OPERAND_FAIL;
5741               /* If the offset is 0, find out if it's a +0 or -0.  */
5742               if (inst.reloc.exp.X_op == O_constant
5743                   && inst.reloc.exp.X_add_number == 0)
5744                 {
5745                   skip_whitespace (q);
5746                   if (*q == '#')
5747                     {
5748                       q++;
5749                       skip_whitespace (q);
5750                     }
5751                   if (*q == '-')
5752                     inst.operands[i].negative = 1;
5753                 }
5754             }
5755         }
5756     }
5757
5758   /* If at this point neither .preind nor .postind is set, we have a
5759      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5760   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5761     {
5762       inst.operands[i].preind = 1;
5763       inst.reloc.exp.X_op = O_constant;
5764       inst.reloc.exp.X_add_number = 0;
5765     }
5766   *str = p;
5767   return PARSE_OPERAND_SUCCESS;
5768 }
5769
5770 static int
5771 parse_address (char **str, int i)
5772 {
5773   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5774          ? SUCCESS : FAIL;
5775 }
5776
5777 static parse_operand_result
5778 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5779 {
5780   return parse_address_main (str, i, 1, type);
5781 }
5782
5783 /* Parse an operand for a MOVW or MOVT instruction.  */
5784 static int
5785 parse_half (char **str)
5786 {
5787   char * p;
5788
5789   p = *str;
5790   skip_past_char (&p, '#');
5791   if (strncasecmp (p, ":lower16:", 9) == 0)
5792     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5793   else if (strncasecmp (p, ":upper16:", 9) == 0)
5794     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5795
5796   if (inst.reloc.type != BFD_RELOC_UNUSED)
5797     {
5798       p += 9;
5799       skip_whitespace (p);
5800     }
5801
5802   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5803     return FAIL;
5804
5805   if (inst.reloc.type == BFD_RELOC_UNUSED)
5806     {
5807       if (inst.reloc.exp.X_op != O_constant)
5808         {
5809           inst.error = _("constant expression expected");
5810           return FAIL;
5811         }
5812       if (inst.reloc.exp.X_add_number < 0
5813           || inst.reloc.exp.X_add_number > 0xffff)
5814         {
5815           inst.error = _("immediate value out of range");
5816           return FAIL;
5817         }
5818     }
5819   *str = p;
5820   return SUCCESS;
5821 }
5822
5823 /* Miscellaneous. */
5824
5825 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5826    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5827 static int
5828 parse_psr (char **str, bfd_boolean lhs)
5829 {
5830   char *p;
5831   unsigned long psr_field;
5832   const struct asm_psr *psr;
5833   char *start;
5834   bfd_boolean is_apsr = FALSE;
5835   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5836
5837   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5838      be TRUE, but we want to ignore it in this case as we are building for any
5839      CPU type, including non-m variants.  */
5840   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5841     m_profile = FALSE;
5842
5843   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5844      feature for ease of use and backwards compatibility.  */
5845   p = *str;
5846   if (strncasecmp (p, "SPSR", 4) == 0)
5847     {
5848       if (m_profile)
5849         goto unsupported_psr;
5850
5851       psr_field = SPSR_BIT;
5852     }
5853   else if (strncasecmp (p, "CPSR", 4) == 0)
5854     {
5855       if (m_profile)
5856         goto unsupported_psr;
5857
5858       psr_field = 0;
5859     }
5860   else if (strncasecmp (p, "APSR", 4) == 0)
5861     {
5862       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5863          and ARMv7-R architecture CPUs.  */
5864       is_apsr = TRUE;
5865       psr_field = 0;
5866     }
5867   else if (m_profile)
5868     {
5869       start = p;
5870       do
5871         p++;
5872       while (ISALNUM (*p) || *p == '_');
5873
5874       if (strncasecmp (start, "iapsr", 5) == 0
5875           || strncasecmp (start, "eapsr", 5) == 0
5876           || strncasecmp (start, "xpsr", 4) == 0
5877           || strncasecmp (start, "psr", 3) == 0)
5878         p = start + strcspn (start, "rR") + 1;
5879
5880       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5881                                                   p - start);
5882
5883       if (!psr)
5884         return FAIL;
5885
5886       /* If APSR is being written, a bitfield may be specified.  Note that
5887          APSR itself is handled above.  */
5888       if (psr->field <= 3)
5889         {
5890           psr_field = psr->field;
5891           is_apsr = TRUE;
5892           goto check_suffix;
5893         }
5894
5895       *str = p;
5896       /* M-profile MSR instructions have the mask field set to "10", except
5897          *PSR variants which modify APSR, which may use a different mask (and
5898          have been handled already).  Do that by setting the PSR_f field
5899          here.  */
5900       return psr->field | (lhs ? PSR_f : 0);
5901     }
5902   else
5903     goto unsupported_psr;
5904
5905   p += 4;
5906 check_suffix:
5907   if (*p == '_')
5908     {
5909       /* A suffix follows.  */
5910       p++;
5911       start = p;
5912
5913       do
5914         p++;
5915       while (ISALNUM (*p) || *p == '_');
5916
5917       if (is_apsr)
5918         {
5919           /* APSR uses a notation for bits, rather than fields.  */
5920           unsigned int nzcvq_bits = 0;
5921           unsigned int g_bit = 0;
5922           char *bit;
5923
5924           for (bit = start; bit != p; bit++)
5925             {
5926               switch (TOLOWER (*bit))
5927                 {
5928                 case 'n':
5929                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5930                   break;
5931
5932                 case 'z':
5933                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5934                   break;
5935
5936                 case 'c':
5937                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5938                   break;
5939
5940                 case 'v':
5941                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5942                   break;
5943
5944                 case 'q':
5945                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5946                   break;
5947
5948                 case 'g':
5949                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5950                   break;
5951
5952                 default:
5953                   inst.error = _("unexpected bit specified after APSR");
5954                   return FAIL;
5955                 }
5956             }
5957
5958           if (nzcvq_bits == 0x1f)
5959             psr_field |= PSR_f;
5960
5961           if (g_bit == 0x1)
5962             {
5963               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5964                 {
5965                   inst.error = _("selected processor does not "
5966                                  "support DSP extension");
5967                   return FAIL;
5968                 }
5969
5970               psr_field |= PSR_s;
5971             }
5972
5973           if ((nzcvq_bits & 0x20) != 0
5974               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5975               || (g_bit & 0x2) != 0)
5976             {
5977               inst.error = _("bad bitmask specified after APSR");
5978               return FAIL;
5979             }
5980         }
5981       else
5982         {
5983           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5984                                                       p - start);
5985           if (!psr)
5986             goto error;
5987
5988           psr_field |= psr->field;
5989         }
5990     }
5991   else
5992     {
5993       if (ISALNUM (*p))
5994         goto error;    /* Garbage after "[CS]PSR".  */
5995
5996       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5997          is deprecated, but allow it anyway.  */
5998       if (is_apsr && lhs)
5999         {
6000           psr_field |= PSR_f;
6001           as_tsktsk (_("writing to APSR without specifying a bitmask is "
6002                        "deprecated"));
6003         }
6004       else if (!m_profile)
6005         /* These bits are never right for M-profile devices: don't set them
6006            (only code paths which read/write APSR reach here).  */
6007         psr_field |= (PSR_c | PSR_f);
6008     }
6009   *str = p;
6010   return psr_field;
6011
6012  unsupported_psr:
6013   inst.error = _("selected processor does not support requested special "
6014                  "purpose register");
6015   return FAIL;
6016
6017  error:
6018   inst.error = _("flag for {c}psr instruction expected");
6019   return FAIL;
6020 }
6021
6022 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
6023    value suitable for splatting into the AIF field of the instruction.  */
6024
6025 static int
6026 parse_cps_flags (char **str)
6027 {
6028   int val = 0;
6029   int saw_a_flag = 0;
6030   char *s = *str;
6031
6032   for (;;)
6033     switch (*s++)
6034       {
6035       case '\0': case ',':
6036         goto done;
6037
6038       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6039       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6040       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6041
6042       default:
6043         inst.error = _("unrecognized CPS flag");
6044         return FAIL;
6045       }
6046
6047  done:
6048   if (saw_a_flag == 0)
6049     {
6050       inst.error = _("missing CPS flags");
6051       return FAIL;
6052     }
6053
6054   *str = s - 1;
6055   return val;
6056 }
6057
6058 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6059    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6060
6061 static int
6062 parse_endian_specifier (char **str)
6063 {
6064   int little_endian;
6065   char *s = *str;
6066
6067   if (strncasecmp (s, "BE", 2))
6068     little_endian = 0;
6069   else if (strncasecmp (s, "LE", 2))
6070     little_endian = 1;
6071   else
6072     {
6073       inst.error = _("valid endian specifiers are be or le");
6074       return FAIL;
6075     }
6076
6077   if (ISALNUM (s[2]) || s[2] == '_')
6078     {
6079       inst.error = _("valid endian specifiers are be or le");
6080       return FAIL;
6081     }
6082
6083   *str = s + 2;
6084   return little_endian;
6085 }
6086
6087 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6088    value suitable for poking into the rotate field of an sxt or sxta
6089    instruction, or FAIL on error.  */
6090
6091 static int
6092 parse_ror (char **str)
6093 {
6094   int rot;
6095   char *s = *str;
6096
6097   if (strncasecmp (s, "ROR", 3) == 0)
6098     s += 3;
6099   else
6100     {
6101       inst.error = _("missing rotation field after comma");
6102       return FAIL;
6103     }
6104
6105   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6106     return FAIL;
6107
6108   switch (rot)
6109     {
6110     case  0: *str = s; return 0x0;
6111     case  8: *str = s; return 0x1;
6112     case 16: *str = s; return 0x2;
6113     case 24: *str = s; return 0x3;
6114
6115     default:
6116       inst.error = _("rotation can only be 0, 8, 16, or 24");
6117       return FAIL;
6118     }
6119 }
6120
6121 /* Parse a conditional code (from conds[] below).  The value returned is in the
6122    range 0 .. 14, or FAIL.  */
6123 static int
6124 parse_cond (char **str)
6125 {
6126   char *q;
6127   const struct asm_cond *c;
6128   int n;
6129   /* Condition codes are always 2 characters, so matching up to
6130      3 characters is sufficient.  */
6131   char cond[3];
6132
6133   q = *str;
6134   n = 0;
6135   while (ISALPHA (*q) && n < 3)
6136     {
6137       cond[n] = TOLOWER (*q);
6138       q++;
6139       n++;
6140     }
6141
6142   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6143   if (!c)
6144     {
6145       inst.error = _("condition required");
6146       return FAIL;
6147     }
6148
6149   *str = q;
6150   return c->value;
6151 }
6152
6153 /* Record a use of the given feature.  */
6154 static void
6155 record_feature_use (const arm_feature_set *feature)
6156 {
6157   if (thumb_mode)
6158     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6159   else
6160     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6161 }
6162
6163 /* If the given feature available in the selected CPU, mark it as used.
6164    Returns TRUE iff feature is available.  */
6165 static bfd_boolean
6166 mark_feature_used (const arm_feature_set *feature)
6167 {
6168   /* Ensure the option is valid on the current architecture.  */
6169   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6170     return FALSE;
6171
6172   /* Add the appropriate architecture feature for the barrier option used.
6173      */
6174   record_feature_use (feature);
6175
6176   return TRUE;
6177 }
6178
6179 /* Parse an option for a barrier instruction.  Returns the encoding for the
6180    option, or FAIL.  */
6181 static int
6182 parse_barrier (char **str)
6183 {
6184   char *p, *q;
6185   const struct asm_barrier_opt *o;
6186
6187   p = q = *str;
6188   while (ISALPHA (*q))
6189     q++;
6190
6191   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6192                                                     q - p);
6193   if (!o)
6194     return FAIL;
6195
6196   if (!mark_feature_used (&o->arch))
6197     return FAIL;
6198
6199   *str = q;
6200   return o->value;
6201 }
6202
6203 /* Parse the operands of a table branch instruction.  Similar to a memory
6204    operand.  */
6205 static int
6206 parse_tb (char **str)
6207 {
6208   char * p = *str;
6209   int reg;
6210
6211   if (skip_past_char (&p, '[') == FAIL)
6212     {
6213       inst.error = _("'[' expected");
6214       return FAIL;
6215     }
6216
6217   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6218     {
6219       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6220       return FAIL;
6221     }
6222   inst.operands[0].reg = reg;
6223
6224   if (skip_past_comma (&p) == FAIL)
6225     {
6226       inst.error = _("',' expected");
6227       return FAIL;
6228     }
6229
6230   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6231     {
6232       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6233       return FAIL;
6234     }
6235   inst.operands[0].imm = reg;
6236
6237   if (skip_past_comma (&p) == SUCCESS)
6238     {
6239       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6240         return FAIL;
6241       if (inst.reloc.exp.X_add_number != 1)
6242         {
6243           inst.error = _("invalid shift");
6244           return FAIL;
6245         }
6246       inst.operands[0].shifted = 1;
6247     }
6248
6249   if (skip_past_char (&p, ']') == FAIL)
6250     {
6251       inst.error = _("']' expected");
6252       return FAIL;
6253     }
6254   *str = p;
6255   return SUCCESS;
6256 }
6257
6258 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6259    information on the types the operands can take and how they are encoded.
6260    Up to four operands may be read; this function handles setting the
6261    ".present" field for each read operand itself.
6262    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6263    else returns FAIL.  */
6264
6265 static int
6266 parse_neon_mov (char **str, int *which_operand)
6267 {
6268   int i = *which_operand, val;
6269   enum arm_reg_type rtype;
6270   char *ptr = *str;
6271   struct neon_type_el optype;
6272
6273   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6274     {
6275       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6276       inst.operands[i].reg = val;
6277       inst.operands[i].isscalar = 1;
6278       inst.operands[i].vectype = optype;
6279       inst.operands[i++].present = 1;
6280
6281       if (skip_past_comma (&ptr) == FAIL)
6282         goto wanted_comma;
6283
6284       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6285         goto wanted_arm;
6286
6287       inst.operands[i].reg = val;
6288       inst.operands[i].isreg = 1;
6289       inst.operands[i].present = 1;
6290     }
6291   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6292            != FAIL)
6293     {
6294       /* Cases 0, 1, 2, 3, 5 (D only).  */
6295       if (skip_past_comma (&ptr) == FAIL)
6296         goto wanted_comma;
6297
6298       inst.operands[i].reg = val;
6299       inst.operands[i].isreg = 1;
6300       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6301       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6302       inst.operands[i].isvec = 1;
6303       inst.operands[i].vectype = optype;
6304       inst.operands[i++].present = 1;
6305
6306       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6307         {
6308           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6309              Case 13: VMOV <Sd>, <Rm>  */
6310           inst.operands[i].reg = val;
6311           inst.operands[i].isreg = 1;
6312           inst.operands[i].present = 1;
6313
6314           if (rtype == REG_TYPE_NQ)
6315             {
6316               first_error (_("can't use Neon quad register here"));
6317               return FAIL;
6318             }
6319           else if (rtype != REG_TYPE_VFS)
6320             {
6321               i++;
6322               if (skip_past_comma (&ptr) == FAIL)
6323                 goto wanted_comma;
6324               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6325                 goto wanted_arm;
6326               inst.operands[i].reg = val;
6327               inst.operands[i].isreg = 1;
6328               inst.operands[i].present = 1;
6329             }
6330         }
6331       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6332                                            &optype)) != FAIL)
6333         {
6334           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6335              Case 1: VMOV<c><q> <Dd>, <Dm>
6336              Case 8: VMOV.F32 <Sd>, <Sm>
6337              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6338
6339           inst.operands[i].reg = val;
6340           inst.operands[i].isreg = 1;
6341           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6342           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6343           inst.operands[i].isvec = 1;
6344           inst.operands[i].vectype = optype;
6345           inst.operands[i].present = 1;
6346
6347           if (skip_past_comma (&ptr) == SUCCESS)
6348             {
6349               /* Case 15.  */
6350               i++;
6351
6352               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6353                 goto wanted_arm;
6354
6355               inst.operands[i].reg = val;
6356               inst.operands[i].isreg = 1;
6357               inst.operands[i++].present = 1;
6358
6359               if (skip_past_comma (&ptr) == FAIL)
6360                 goto wanted_comma;
6361
6362               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6363                 goto wanted_arm;
6364
6365               inst.operands[i].reg = val;
6366               inst.operands[i].isreg = 1;
6367               inst.operands[i].present = 1;
6368             }
6369         }
6370       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6371           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6372              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6373              Case 10: VMOV.F32 <Sd>, #<imm>
6374              Case 11: VMOV.F64 <Dd>, #<imm>  */
6375         inst.operands[i].immisfloat = 1;
6376       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6377                == SUCCESS)
6378           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6379              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6380         ;
6381       else
6382         {
6383           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6384           return FAIL;
6385         }
6386     }
6387   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6388     {
6389       /* Cases 6, 7.  */
6390       inst.operands[i].reg = val;
6391       inst.operands[i].isreg = 1;
6392       inst.operands[i++].present = 1;
6393
6394       if (skip_past_comma (&ptr) == FAIL)
6395         goto wanted_comma;
6396
6397       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6398         {
6399           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6400           inst.operands[i].reg = val;
6401           inst.operands[i].isscalar = 1;
6402           inst.operands[i].present = 1;
6403           inst.operands[i].vectype = optype;
6404         }
6405       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6406         {
6407           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6408           inst.operands[i].reg = val;
6409           inst.operands[i].isreg = 1;
6410           inst.operands[i++].present = 1;
6411
6412           if (skip_past_comma (&ptr) == FAIL)
6413             goto wanted_comma;
6414
6415           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6416               == FAIL)
6417             {
6418               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6419               return FAIL;
6420             }
6421
6422           inst.operands[i].reg = val;
6423           inst.operands[i].isreg = 1;
6424           inst.operands[i].isvec = 1;
6425           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6426           inst.operands[i].vectype = optype;
6427           inst.operands[i].present = 1;
6428
6429           if (rtype == REG_TYPE_VFS)
6430             {
6431               /* Case 14.  */
6432               i++;
6433               if (skip_past_comma (&ptr) == FAIL)
6434                 goto wanted_comma;
6435               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6436                                               &optype)) == FAIL)
6437                 {
6438                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6439                   return FAIL;
6440                 }
6441               inst.operands[i].reg = val;
6442               inst.operands[i].isreg = 1;
6443               inst.operands[i].isvec = 1;
6444               inst.operands[i].issingle = 1;
6445               inst.operands[i].vectype = optype;
6446               inst.operands[i].present = 1;
6447             }
6448         }
6449       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6450                != FAIL)
6451         {
6452           /* Case 13.  */
6453           inst.operands[i].reg = val;
6454           inst.operands[i].isreg = 1;
6455           inst.operands[i].isvec = 1;
6456           inst.operands[i].issingle = 1;
6457           inst.operands[i].vectype = optype;
6458           inst.operands[i].present = 1;
6459         }
6460     }
6461   else
6462     {
6463       first_error (_("parse error"));
6464       return FAIL;
6465     }
6466
6467   /* Successfully parsed the operands. Update args.  */
6468   *which_operand = i;
6469   *str = ptr;
6470   return SUCCESS;
6471
6472  wanted_comma:
6473   first_error (_("expected comma"));
6474   return FAIL;
6475
6476  wanted_arm:
6477   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6478   return FAIL;
6479 }
6480
6481 /* Use this macro when the operand constraints are different
6482    for ARM and THUMB (e.g. ldrd).  */
6483 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6484         ((arm_operand) | ((thumb_operand) << 16))
6485
6486 /* Matcher codes for parse_operands.  */
6487 enum operand_parse_code
6488 {
6489   OP_stop,      /* end of line */
6490
6491   OP_RR,        /* ARM register */
6492   OP_RRnpc,     /* ARM register, not r15 */
6493   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6494   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6495   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6496                    optional trailing ! */
6497   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6498   OP_RCP,       /* Coprocessor number */
6499   OP_RCN,       /* Coprocessor register */
6500   OP_RF,        /* FPA register */
6501   OP_RVS,       /* VFP single precision register */
6502   OP_RVD,       /* VFP double precision register (0..15) */
6503   OP_RND,       /* Neon double precision register (0..31) */
6504   OP_RNQ,       /* Neon quad precision register */
6505   OP_RVSD,      /* VFP single or double precision register */
6506   OP_RNSD,      /* Neon single or double precision register */
6507   OP_RNDQ,      /* Neon double or quad precision register */
6508   OP_RNSDQ,     /* Neon single, double or quad precision register */
6509   OP_RNSC,      /* Neon scalar D[X] */
6510   OP_RVC,       /* VFP control register */
6511   OP_RMF,       /* Maverick F register */
6512   OP_RMD,       /* Maverick D register */
6513   OP_RMFX,      /* Maverick FX register */
6514   OP_RMDX,      /* Maverick DX register */
6515   OP_RMAX,      /* Maverick AX register */
6516   OP_RMDS,      /* Maverick DSPSC register */
6517   OP_RIWR,      /* iWMMXt wR register */
6518   OP_RIWC,      /* iWMMXt wC register */
6519   OP_RIWG,      /* iWMMXt wCG register */
6520   OP_RXA,       /* XScale accumulator register */
6521
6522   OP_REGLST,    /* ARM register list */
6523   OP_VRSLST,    /* VFP single-precision register list */
6524   OP_VRDLST,    /* VFP double-precision register list */
6525   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6526   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6527   OP_NSTRLST,   /* Neon element/structure list */
6528
6529   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6530   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6531   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6532   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6533   OP_RNSD_RNSC, /* Neon S or D reg, or Neon scalar.  */
6534   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6535   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6536   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6537   OP_VMOV,      /* Neon VMOV operands.  */
6538   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6539   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6540   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6541
6542   OP_I0,        /* immediate zero */
6543   OP_I7,        /* immediate value 0 .. 7 */
6544   OP_I15,       /*                 0 .. 15 */
6545   OP_I16,       /*                 1 .. 16 */
6546   OP_I16z,      /*                 0 .. 16 */
6547   OP_I31,       /*                 0 .. 31 */
6548   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6549   OP_I32,       /*                 1 .. 32 */
6550   OP_I32z,      /*                 0 .. 32 */
6551   OP_I63,       /*                 0 .. 63 */
6552   OP_I63s,      /*               -64 .. 63 */
6553   OP_I64,       /*                 1 .. 64 */
6554   OP_I64z,      /*                 0 .. 64 */
6555   OP_I255,      /*                 0 .. 255 */
6556
6557   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6558   OP_I7b,       /*                             0 .. 7 */
6559   OP_I15b,      /*                             0 .. 15 */
6560   OP_I31b,      /*                             0 .. 31 */
6561
6562   OP_SH,        /* shifter operand */
6563   OP_SHG,       /* shifter operand with possible group relocation */
6564   OP_ADDR,      /* Memory address expression (any mode) */
6565   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6566   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6567   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6568   OP_EXP,       /* arbitrary expression */
6569   OP_EXPi,      /* same, with optional immediate prefix */
6570   OP_EXPr,      /* same, with optional relocation suffix */
6571   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6572   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
6573   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
6574
6575   OP_CPSF,      /* CPS flags */
6576   OP_ENDI,      /* Endianness specifier */
6577   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6578   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6579   OP_COND,      /* conditional code */
6580   OP_TB,        /* Table branch.  */
6581
6582   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6583
6584   OP_RRnpc_I0,  /* ARM register or literal 0 */
6585   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
6586   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6587   OP_RF_IF,     /* FPA register or immediate */
6588   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6589   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6590
6591   /* Optional operands.  */
6592   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6593   OP_oI31b,      /*                             0 .. 31 */
6594   OP_oI32b,      /*                             1 .. 32 */
6595   OP_oI32z,      /*                             0 .. 32 */
6596   OP_oIffffb,    /*                             0 .. 65535 */
6597   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6598
6599   OP_oRR,        /* ARM register */
6600   OP_oRRnpc,     /* ARM register, not the PC */
6601   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6602   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6603   OP_oRND,       /* Optional Neon double precision register */
6604   OP_oRNQ,       /* Optional Neon quad precision register */
6605   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6606   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6607   OP_oSHll,      /* LSL immediate */
6608   OP_oSHar,      /* ASR immediate */
6609   OP_oSHllar,    /* LSL or ASR immediate */
6610   OP_oROR,       /* ROR 0/8/16/24 */
6611   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6612
6613   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6614   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6615   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6616   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6617
6618   OP_FIRST_OPTIONAL = OP_oI7b
6619 };
6620
6621 /* Generic instruction operand parser.  This does no encoding and no
6622    semantic validation; it merely squirrels values away in the inst
6623    structure.  Returns SUCCESS or FAIL depending on whether the
6624    specified grammar matched.  */
6625 static int
6626 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6627 {
6628   unsigned const int *upat = pattern;
6629   char *backtrack_pos = 0;
6630   const char *backtrack_error = 0;
6631   int i, val = 0, backtrack_index = 0;
6632   enum arm_reg_type rtype;
6633   parse_operand_result result;
6634   unsigned int op_parse_code;
6635
6636 #define po_char_or_fail(chr)                    \
6637   do                                            \
6638     {                                           \
6639       if (skip_past_char (&str, chr) == FAIL)   \
6640         goto bad_args;                          \
6641     }                                           \
6642   while (0)
6643
6644 #define po_reg_or_fail(regtype)                                 \
6645   do                                                            \
6646     {                                                           \
6647       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6648                                  & inst.operands[i].vectype);   \
6649       if (val == FAIL)                                          \
6650         {                                                       \
6651           first_error (_(reg_expected_msgs[regtype]));          \
6652           goto failure;                                         \
6653         }                                                       \
6654       inst.operands[i].reg = val;                               \
6655       inst.operands[i].isreg = 1;                               \
6656       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6657       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6658       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6659                              || rtype == REG_TYPE_VFD           \
6660                              || rtype == REG_TYPE_NQ);          \
6661     }                                                           \
6662   while (0)
6663
6664 #define po_reg_or_goto(regtype, label)                          \
6665   do                                                            \
6666     {                                                           \
6667       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6668                                  & inst.operands[i].vectype);   \
6669       if (val == FAIL)                                          \
6670         goto label;                                             \
6671                                                                 \
6672       inst.operands[i].reg = val;                               \
6673       inst.operands[i].isreg = 1;                               \
6674       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6675       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6676       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6677                              || rtype == REG_TYPE_VFD           \
6678                              || rtype == REG_TYPE_NQ);          \
6679     }                                                           \
6680   while (0)
6681
6682 #define po_imm_or_fail(min, max, popt)                          \
6683   do                                                            \
6684     {                                                           \
6685       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6686         goto failure;                                           \
6687       inst.operands[i].imm = val;                               \
6688     }                                                           \
6689   while (0)
6690
6691 #define po_scalar_or_goto(elsz, label)                                  \
6692   do                                                                    \
6693     {                                                                   \
6694       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6695       if (val == FAIL)                                                  \
6696         goto label;                                                     \
6697       inst.operands[i].reg = val;                                       \
6698       inst.operands[i].isscalar = 1;                                    \
6699     }                                                                   \
6700   while (0)
6701
6702 #define po_misc_or_fail(expr)                   \
6703   do                                            \
6704     {                                           \
6705       if (expr)                                 \
6706         goto failure;                           \
6707     }                                           \
6708   while (0)
6709
6710 #define po_misc_or_fail_no_backtrack(expr)              \
6711   do                                                    \
6712     {                                                   \
6713       result = expr;                                    \
6714       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6715         backtrack_pos = 0;                              \
6716       if (result != PARSE_OPERAND_SUCCESS)              \
6717         goto failure;                                   \
6718     }                                                   \
6719   while (0)
6720
6721 #define po_barrier_or_imm(str)                             \
6722   do                                                       \
6723     {                                                      \
6724       val = parse_barrier (&str);                          \
6725       if (val == FAIL && ! ISALPHA (*str))                 \
6726         goto immediate;                                    \
6727       if (val == FAIL                                      \
6728           /* ISB can only take SY as an option.  */        \
6729           || ((inst.instruction & 0xf0) == 0x60            \
6730                && val != 0xf))                             \
6731         {                                                  \
6732            inst.error = _("invalid barrier type");         \
6733            backtrack_pos = 0;                              \
6734            goto failure;                                   \
6735         }                                                  \
6736     }                                                      \
6737   while (0)
6738
6739   skip_whitespace (str);
6740
6741   for (i = 0; upat[i] != OP_stop; i++)
6742     {
6743       op_parse_code = upat[i];
6744       if (op_parse_code >= 1<<16)
6745         op_parse_code = thumb ? (op_parse_code >> 16)
6746                                 : (op_parse_code & ((1<<16)-1));
6747
6748       if (op_parse_code >= OP_FIRST_OPTIONAL)
6749         {
6750           /* Remember where we are in case we need to backtrack.  */
6751           gas_assert (!backtrack_pos);
6752           backtrack_pos = str;
6753           backtrack_error = inst.error;
6754           backtrack_index = i;
6755         }
6756
6757       if (i > 0 && (i > 1 || inst.operands[0].present))
6758         po_char_or_fail (',');
6759
6760       switch (op_parse_code)
6761         {
6762           /* Registers */
6763         case OP_oRRnpc:
6764         case OP_oRRnpcsp:
6765         case OP_RRnpc:
6766         case OP_RRnpcsp:
6767         case OP_oRR:
6768         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6769         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6770         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6771         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6772         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6773         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6774         case OP_oRND:
6775         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6776         case OP_RVC:
6777           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6778           break;
6779           /* Also accept generic coprocessor regs for unknown registers.  */
6780           coproc_reg:
6781           po_reg_or_fail (REG_TYPE_CN);
6782           break;
6783         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6784         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6785         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6786         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6787         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6788         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6789         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6790         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6791         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6792         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6793         case OP_oRNQ:
6794         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6795         case OP_RNSD:  po_reg_or_fail (REG_TYPE_NSD);     break;
6796         case OP_oRNDQ:
6797         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6798         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6799         case OP_oRNSDQ:
6800         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6801
6802         /* Neon scalar. Using an element size of 8 means that some invalid
6803            scalars are accepted here, so deal with those in later code.  */
6804         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6805
6806         case OP_RNDQ_I0:
6807           {
6808             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6809             break;
6810             try_imm0:
6811             po_imm_or_fail (0, 0, TRUE);
6812           }
6813           break;
6814
6815         case OP_RVSD_I0:
6816           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6817           break;
6818
6819         case OP_RSVD_FI0:
6820           {
6821             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6822             break;
6823             try_ifimm0:
6824             if (parse_ifimm_zero (&str))
6825               inst.operands[i].imm = 0;
6826             else
6827             {
6828               inst.error
6829                 = _("only floating point zero is allowed as immediate value");
6830               goto failure;
6831             }
6832           }
6833           break;
6834
6835         case OP_RR_RNSC:
6836           {
6837             po_scalar_or_goto (8, try_rr);
6838             break;
6839             try_rr:
6840             po_reg_or_fail (REG_TYPE_RN);
6841           }
6842           break;
6843
6844         case OP_RNSDQ_RNSC:
6845           {
6846             po_scalar_or_goto (8, try_nsdq);
6847             break;
6848             try_nsdq:
6849             po_reg_or_fail (REG_TYPE_NSDQ);
6850           }
6851           break;
6852
6853         case OP_RNSD_RNSC:
6854           {
6855             po_scalar_or_goto (8, try_s_scalar);
6856             break;
6857             try_s_scalar:
6858             po_scalar_or_goto (4, try_nsd);
6859             break;
6860             try_nsd:
6861             po_reg_or_fail (REG_TYPE_NSD);
6862           }
6863           break;
6864
6865         case OP_RNDQ_RNSC:
6866           {
6867             po_scalar_or_goto (8, try_ndq);
6868             break;
6869             try_ndq:
6870             po_reg_or_fail (REG_TYPE_NDQ);
6871           }
6872           break;
6873
6874         case OP_RND_RNSC:
6875           {
6876             po_scalar_or_goto (8, try_vfd);
6877             break;
6878             try_vfd:
6879             po_reg_or_fail (REG_TYPE_VFD);
6880           }
6881           break;
6882
6883         case OP_VMOV:
6884           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6885              not careful then bad things might happen.  */
6886           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6887           break;
6888
6889         case OP_RNDQ_Ibig:
6890           {
6891             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6892             break;
6893             try_immbig:
6894             /* There's a possibility of getting a 64-bit immediate here, so
6895                we need special handling.  */
6896             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6897                 == FAIL)
6898               {
6899                 inst.error = _("immediate value is out of range");
6900                 goto failure;
6901               }
6902           }
6903           break;
6904
6905         case OP_RNDQ_I63b:
6906           {
6907             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6908             break;
6909             try_shimm:
6910             po_imm_or_fail (0, 63, TRUE);
6911           }
6912           break;
6913
6914         case OP_RRnpcb:
6915           po_char_or_fail ('[');
6916           po_reg_or_fail  (REG_TYPE_RN);
6917           po_char_or_fail (']');
6918           break;
6919
6920         case OP_RRnpctw:
6921         case OP_RRw:
6922         case OP_oRRw:
6923           po_reg_or_fail (REG_TYPE_RN);
6924           if (skip_past_char (&str, '!') == SUCCESS)
6925             inst.operands[i].writeback = 1;
6926           break;
6927
6928           /* Immediates */
6929         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6930         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6931         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6932         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6933         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6934         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6935         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6936         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6937         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6938         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6939         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6940         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6941
6942         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6943         case OP_oI7b:
6944         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6945         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6946         case OP_oI31b:
6947         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6948         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6949         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6950         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6951
6952           /* Immediate variants */
6953         case OP_oI255c:
6954           po_char_or_fail ('{');
6955           po_imm_or_fail (0, 255, TRUE);
6956           po_char_or_fail ('}');
6957           break;
6958
6959         case OP_I31w:
6960           /* The expression parser chokes on a trailing !, so we have
6961              to find it first and zap it.  */
6962           {
6963             char *s = str;
6964             while (*s && *s != ',')
6965               s++;
6966             if (s[-1] == '!')
6967               {
6968                 s[-1] = '\0';
6969                 inst.operands[i].writeback = 1;
6970               }
6971             po_imm_or_fail (0, 31, TRUE);
6972             if (str == s - 1)
6973               str = s;
6974           }
6975           break;
6976
6977           /* Expressions */
6978         case OP_EXPi:   EXPi:
6979           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6980                                               GE_OPT_PREFIX));
6981           break;
6982
6983         case OP_EXP:
6984           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6985                                               GE_NO_PREFIX));
6986           break;
6987
6988         case OP_EXPr:   EXPr:
6989           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6990                                               GE_NO_PREFIX));
6991           if (inst.reloc.exp.X_op == O_symbol)
6992             {
6993               val = parse_reloc (&str);
6994               if (val == -1)
6995                 {
6996                   inst.error = _("unrecognized relocation suffix");
6997                   goto failure;
6998                 }
6999               else if (val != BFD_RELOC_UNUSED)
7000                 {
7001                   inst.operands[i].imm = val;
7002                   inst.operands[i].hasreloc = 1;
7003                 }
7004             }
7005           break;
7006
7007           /* Operand for MOVW or MOVT.  */
7008         case OP_HALF:
7009           po_misc_or_fail (parse_half (&str));
7010           break;
7011
7012           /* Register or expression.  */
7013         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
7014         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
7015
7016           /* Register or immediate.  */
7017         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
7018         I0:               po_imm_or_fail (0, 0, FALSE);       break;
7019
7020         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
7021         IF:
7022           if (!is_immediate_prefix (*str))
7023             goto bad_args;
7024           str++;
7025           val = parse_fpa_immediate (&str);
7026           if (val == FAIL)
7027             goto failure;
7028           /* FPA immediates are encoded as registers 8-15.
7029              parse_fpa_immediate has already applied the offset.  */
7030           inst.operands[i].reg = val;
7031           inst.operands[i].isreg = 1;
7032           break;
7033
7034         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
7035         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
7036
7037           /* Two kinds of register.  */
7038         case OP_RIWR_RIWC:
7039           {
7040             struct reg_entry *rege = arm_reg_parse_multi (&str);
7041             if (!rege
7042                 || (rege->type != REG_TYPE_MMXWR
7043                     && rege->type != REG_TYPE_MMXWC
7044                     && rege->type != REG_TYPE_MMXWCG))
7045               {
7046                 inst.error = _("iWMMXt data or control register expected");
7047                 goto failure;
7048               }
7049             inst.operands[i].reg = rege->number;
7050             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7051           }
7052           break;
7053
7054         case OP_RIWC_RIWG:
7055           {
7056             struct reg_entry *rege = arm_reg_parse_multi (&str);
7057             if (!rege
7058                 || (rege->type != REG_TYPE_MMXWC
7059                     && rege->type != REG_TYPE_MMXWCG))
7060               {
7061                 inst.error = _("iWMMXt control register expected");
7062                 goto failure;
7063               }
7064             inst.operands[i].reg = rege->number;
7065             inst.operands[i].isreg = 1;
7066           }
7067           break;
7068
7069           /* Misc */
7070         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7071         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7072         case OP_oROR:    val = parse_ror (&str);                break;
7073         case OP_COND:    val = parse_cond (&str);               break;
7074         case OP_oBARRIER_I15:
7075           po_barrier_or_imm (str); break;
7076           immediate:
7077           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7078             goto failure;
7079           break;
7080
7081         case OP_wPSR:
7082         case OP_rPSR:
7083           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7084           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7085             {
7086               inst.error = _("Banked registers are not available with this "
7087                              "architecture.");
7088               goto failure;
7089             }
7090           break;
7091           try_psr:
7092           val = parse_psr (&str, op_parse_code == OP_wPSR);
7093           break;
7094
7095         case OP_APSR_RR:
7096           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7097           break;
7098           try_apsr:
7099           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7100              instruction).  */
7101           if (strncasecmp (str, "APSR_", 5) == 0)
7102             {
7103               unsigned found = 0;
7104               str += 5;
7105               while (found < 15)
7106                 switch (*str++)
7107                   {
7108                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7109                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7110                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7111                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7112                   default: found = 16;
7113                   }
7114               if (found != 15)
7115                 goto failure;
7116               inst.operands[i].isvec = 1;
7117               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7118               inst.operands[i].reg = REG_PC;
7119             }
7120           else
7121             goto failure;
7122           break;
7123
7124         case OP_TB:
7125           po_misc_or_fail (parse_tb (&str));
7126           break;
7127
7128           /* Register lists.  */
7129         case OP_REGLST:
7130           val = parse_reg_list (&str);
7131           if (*str == '^')
7132             {
7133               inst.operands[i].writeback = 1;
7134               str++;
7135             }
7136           break;
7137
7138         case OP_VRSLST:
7139           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7140           break;
7141
7142         case OP_VRDLST:
7143           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7144           break;
7145
7146         case OP_VRSDLST:
7147           /* Allow Q registers too.  */
7148           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7149                                     REGLIST_NEON_D);
7150           if (val == FAIL)
7151             {
7152               inst.error = NULL;
7153               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7154                                         REGLIST_VFP_S);
7155               inst.operands[i].issingle = 1;
7156             }
7157           break;
7158
7159         case OP_NRDLST:
7160           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7161                                     REGLIST_NEON_D);
7162           break;
7163
7164         case OP_NSTRLST:
7165           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7166                                            &inst.operands[i].vectype);
7167           break;
7168
7169           /* Addressing modes */
7170         case OP_ADDR:
7171           po_misc_or_fail (parse_address (&str, i));
7172           break;
7173
7174         case OP_ADDRGLDR:
7175           po_misc_or_fail_no_backtrack (
7176             parse_address_group_reloc (&str, i, GROUP_LDR));
7177           break;
7178
7179         case OP_ADDRGLDRS:
7180           po_misc_or_fail_no_backtrack (
7181             parse_address_group_reloc (&str, i, GROUP_LDRS));
7182           break;
7183
7184         case OP_ADDRGLDC:
7185           po_misc_or_fail_no_backtrack (
7186             parse_address_group_reloc (&str, i, GROUP_LDC));
7187           break;
7188
7189         case OP_SH:
7190           po_misc_or_fail (parse_shifter_operand (&str, i));
7191           break;
7192
7193         case OP_SHG:
7194           po_misc_or_fail_no_backtrack (
7195             parse_shifter_operand_group_reloc (&str, i));
7196           break;
7197
7198         case OP_oSHll:
7199           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7200           break;
7201
7202         case OP_oSHar:
7203           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7204           break;
7205
7206         case OP_oSHllar:
7207           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7208           break;
7209
7210         default:
7211           as_fatal (_("unhandled operand code %d"), op_parse_code);
7212         }
7213
7214       /* Various value-based sanity checks and shared operations.  We
7215          do not signal immediate failures for the register constraints;
7216          this allows a syntax error to take precedence.  */
7217       switch (op_parse_code)
7218         {
7219         case OP_oRRnpc:
7220         case OP_RRnpc:
7221         case OP_RRnpcb:
7222         case OP_RRw:
7223         case OP_oRRw:
7224         case OP_RRnpc_I0:
7225           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7226             inst.error = BAD_PC;
7227           break;
7228
7229         case OP_oRRnpcsp:
7230         case OP_RRnpcsp:
7231           if (inst.operands[i].isreg)
7232             {
7233               if (inst.operands[i].reg == REG_PC)
7234                 inst.error = BAD_PC;
7235               else if (inst.operands[i].reg == REG_SP
7236                        /* The restriction on Rd/Rt/Rt2 on Thumb mode has been
7237                           relaxed since ARMv8-A.  */
7238                        && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
7239                 {
7240                   gas_assert (thumb);
7241                   inst.error = BAD_SP;
7242                 }
7243             }
7244           break;
7245
7246         case OP_RRnpctw:
7247           if (inst.operands[i].isreg
7248               && inst.operands[i].reg == REG_PC
7249               && (inst.operands[i].writeback || thumb))
7250             inst.error = BAD_PC;
7251           break;
7252
7253         case OP_CPSF:
7254         case OP_ENDI:
7255         case OP_oROR:
7256         case OP_wPSR:
7257         case OP_rPSR:
7258         case OP_COND:
7259         case OP_oBARRIER_I15:
7260         case OP_REGLST:
7261         case OP_VRSLST:
7262         case OP_VRDLST:
7263         case OP_VRSDLST:
7264         case OP_NRDLST:
7265         case OP_NSTRLST:
7266           if (val == FAIL)
7267             goto failure;
7268           inst.operands[i].imm = val;
7269           break;
7270
7271         default:
7272           break;
7273         }
7274
7275       /* If we get here, this operand was successfully parsed.  */
7276       inst.operands[i].present = 1;
7277       continue;
7278
7279     bad_args:
7280       inst.error = BAD_ARGS;
7281
7282     failure:
7283       if (!backtrack_pos)
7284         {
7285           /* The parse routine should already have set inst.error, but set a
7286              default here just in case.  */
7287           if (!inst.error)
7288             inst.error = _("syntax error");
7289           return FAIL;
7290         }
7291
7292       /* Do not backtrack over a trailing optional argument that
7293          absorbed some text.  We will only fail again, with the
7294          'garbage following instruction' error message, which is
7295          probably less helpful than the current one.  */
7296       if (backtrack_index == i && backtrack_pos != str
7297           && upat[i+1] == OP_stop)
7298         {
7299           if (!inst.error)
7300             inst.error = _("syntax error");
7301           return FAIL;
7302         }
7303
7304       /* Try again, skipping the optional argument at backtrack_pos.  */
7305       str = backtrack_pos;
7306       inst.error = backtrack_error;
7307       inst.operands[backtrack_index].present = 0;
7308       i = backtrack_index;
7309       backtrack_pos = 0;
7310     }
7311
7312   /* Check that we have parsed all the arguments.  */
7313   if (*str != '\0' && !inst.error)
7314     inst.error = _("garbage following instruction");
7315
7316   return inst.error ? FAIL : SUCCESS;
7317 }
7318
7319 #undef po_char_or_fail
7320 #undef po_reg_or_fail
7321 #undef po_reg_or_goto
7322 #undef po_imm_or_fail
7323 #undef po_scalar_or_fail
7324 #undef po_barrier_or_imm
7325
7326 /* Shorthand macro for instruction encoding functions issuing errors.  */
7327 #define constraint(expr, err)                   \
7328   do                                            \
7329     {                                           \
7330       if (expr)                                 \
7331         {                                       \
7332           inst.error = err;                     \
7333           return;                               \
7334         }                                       \
7335     }                                           \
7336   while (0)
7337
7338 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7339    instructions are unpredictable if these registers are used.  This
7340    is the BadReg predicate in ARM's Thumb-2 documentation.
7341
7342    Before ARMv8-A, REG_PC and REG_SP were not allowed in quite a few
7343    places, while the restriction on REG_SP was relaxed since ARMv8-A.  */
7344 #define reject_bad_reg(reg)                                     \
7345   do                                                            \
7346    if (reg == REG_PC)                                           \
7347      {                                                          \
7348        inst.error = BAD_PC;                                     \
7349        return;                                                  \
7350      }                                                          \
7351    else if (reg == REG_SP                                       \
7352             && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))  \
7353      {                                                          \
7354        inst.error = BAD_SP;                                     \
7355        return;                                                  \
7356      }                                                          \
7357   while (0)
7358
7359 /* If REG is R13 (the stack pointer), warn that its use is
7360    deprecated.  */
7361 #define warn_deprecated_sp(reg)                 \
7362   do                                            \
7363     if (warn_on_deprecated && reg == REG_SP)    \
7364        as_tsktsk (_("use of r13 is deprecated"));       \
7365   while (0)
7366
7367 /* Functions for operand encoding.  ARM, then Thumb.  */
7368
7369 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7370
7371 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7372
7373    The only binary encoding difference is the Coprocessor number.  Coprocessor
7374    9 is used for half-precision calculations or conversions.  The format of the
7375    instruction is the same as the equivalent Coprocessor 10 instruction that
7376    exists for Single-Precision operation.  */
7377
7378 static void
7379 do_scalar_fp16_v82_encode (void)
7380 {
7381   if (inst.cond != COND_ALWAYS)
7382     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7383                " the behaviour is UNPREDICTABLE"));
7384   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7385               _(BAD_FP16));
7386
7387   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7388   mark_feature_used (&arm_ext_fp16);
7389 }
7390
7391 /* If VAL can be encoded in the immediate field of an ARM instruction,
7392    return the encoded form.  Otherwise, return FAIL.  */
7393
7394 static unsigned int
7395 encode_arm_immediate (unsigned int val)
7396 {
7397   unsigned int a, i;
7398
7399   if (val <= 0xff)
7400     return val;
7401
7402   for (i = 2; i < 32; i += 2)
7403     if ((a = rotate_left (val, i)) <= 0xff)
7404       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7405
7406   return FAIL;
7407 }
7408
7409 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7410    return the encoded form.  Otherwise, return FAIL.  */
7411 static unsigned int
7412 encode_thumb32_immediate (unsigned int val)
7413 {
7414   unsigned int a, i;
7415
7416   if (val <= 0xff)
7417     return val;
7418
7419   for (i = 1; i <= 24; i++)
7420     {
7421       a = val >> i;
7422       if ((val & ~(0xff << i)) == 0)
7423         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7424     }
7425
7426   a = val & 0xff;
7427   if (val == ((a << 16) | a))
7428     return 0x100 | a;
7429   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7430     return 0x300 | a;
7431
7432   a = val & 0xff00;
7433   if (val == ((a << 16) | a))
7434     return 0x200 | (a >> 8);
7435
7436   return FAIL;
7437 }
7438 /* Encode a VFP SP or DP register number into inst.instruction.  */
7439
7440 static void
7441 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7442 {
7443   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7444       && reg > 15)
7445     {
7446       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7447         {
7448           if (thumb_mode)
7449             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7450                                     fpu_vfp_ext_d32);
7451           else
7452             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7453                                     fpu_vfp_ext_d32);
7454         }
7455       else
7456         {
7457           first_error (_("D register out of range for selected VFP version"));
7458           return;
7459         }
7460     }
7461
7462   switch (pos)
7463     {
7464     case VFP_REG_Sd:
7465       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7466       break;
7467
7468     case VFP_REG_Sn:
7469       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7470       break;
7471
7472     case VFP_REG_Sm:
7473       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7474       break;
7475
7476     case VFP_REG_Dd:
7477       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7478       break;
7479
7480     case VFP_REG_Dn:
7481       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7482       break;
7483
7484     case VFP_REG_Dm:
7485       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7486       break;
7487
7488     default:
7489       abort ();
7490     }
7491 }
7492
7493 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7494    if any, is handled by md_apply_fix.   */
7495 static void
7496 encode_arm_shift (int i)
7497 {
7498   /* register-shifted register.  */
7499   if (inst.operands[i].immisreg)
7500     {
7501       int op_index;
7502       for (op_index = 0; op_index <= i; ++op_index)
7503         {
7504           /* Check the operand only when it's presented.  In pre-UAL syntax,
7505              if the destination register is the same as the first operand, two
7506              register form of the instruction can be used.  */
7507           if (inst.operands[op_index].present && inst.operands[op_index].isreg
7508               && inst.operands[op_index].reg == REG_PC)
7509             as_warn (UNPRED_REG ("r15"));
7510         }
7511
7512       if (inst.operands[i].imm == REG_PC)
7513         as_warn (UNPRED_REG ("r15"));
7514     }
7515
7516   if (inst.operands[i].shift_kind == SHIFT_RRX)
7517     inst.instruction |= SHIFT_ROR << 5;
7518   else
7519     {
7520       inst.instruction |= inst.operands[i].shift_kind << 5;
7521       if (inst.operands[i].immisreg)
7522         {
7523           inst.instruction |= SHIFT_BY_REG;
7524           inst.instruction |= inst.operands[i].imm << 8;
7525         }
7526       else
7527         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7528     }
7529 }
7530
7531 static void
7532 encode_arm_shifter_operand (int i)
7533 {
7534   if (inst.operands[i].isreg)
7535     {
7536       inst.instruction |= inst.operands[i].reg;
7537       encode_arm_shift (i);
7538     }
7539   else
7540     {
7541       inst.instruction |= INST_IMMEDIATE;
7542       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7543         inst.instruction |= inst.operands[i].imm;
7544     }
7545 }
7546
7547 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7548 static void
7549 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7550 {
7551   /* PR 14260:
7552      Generate an error if the operand is not a register.  */
7553   constraint (!inst.operands[i].isreg,
7554               _("Instruction does not support =N addresses"));
7555
7556   inst.instruction |= inst.operands[i].reg << 16;
7557
7558   if (inst.operands[i].preind)
7559     {
7560       if (is_t)
7561         {
7562           inst.error = _("instruction does not accept preindexed addressing");
7563           return;
7564         }
7565       inst.instruction |= PRE_INDEX;
7566       if (inst.operands[i].writeback)
7567         inst.instruction |= WRITE_BACK;
7568
7569     }
7570   else if (inst.operands[i].postind)
7571     {
7572       gas_assert (inst.operands[i].writeback);
7573       if (is_t)
7574         inst.instruction |= WRITE_BACK;
7575     }
7576   else /* unindexed - only for coprocessor */
7577     {
7578       inst.error = _("instruction does not accept unindexed addressing");
7579       return;
7580     }
7581
7582   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7583       && (((inst.instruction & 0x000f0000) >> 16)
7584           == ((inst.instruction & 0x0000f000) >> 12)))
7585     as_warn ((inst.instruction & LOAD_BIT)
7586              ? _("destination register same as write-back base")
7587              : _("source register same as write-back base"));
7588 }
7589
7590 /* inst.operands[i] was set up by parse_address.  Encode it into an
7591    ARM-format mode 2 load or store instruction.  If is_t is true,
7592    reject forms that cannot be used with a T instruction (i.e. not
7593    post-indexed).  */
7594 static void
7595 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7596 {
7597   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7598
7599   encode_arm_addr_mode_common (i, is_t);
7600
7601   if (inst.operands[i].immisreg)
7602     {
7603       constraint ((inst.operands[i].imm == REG_PC
7604                    || (is_pc && inst.operands[i].writeback)),
7605                   BAD_PC_ADDRESSING);
7606       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7607       inst.instruction |= inst.operands[i].imm;
7608       if (!inst.operands[i].negative)
7609         inst.instruction |= INDEX_UP;
7610       if (inst.operands[i].shifted)
7611         {
7612           if (inst.operands[i].shift_kind == SHIFT_RRX)
7613             inst.instruction |= SHIFT_ROR << 5;
7614           else
7615             {
7616               inst.instruction |= inst.operands[i].shift_kind << 5;
7617               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7618             }
7619         }
7620     }
7621   else /* immediate offset in inst.reloc */
7622     {
7623       if (is_pc && !inst.reloc.pc_rel)
7624         {
7625           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7626
7627           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7628              cannot use PC in addressing.
7629              PC cannot be used in writeback addressing, either.  */
7630           constraint ((is_t || inst.operands[i].writeback),
7631                       BAD_PC_ADDRESSING);
7632
7633           /* Use of PC in str is deprecated for ARMv7.  */
7634           if (warn_on_deprecated
7635               && !is_load
7636               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7637             as_tsktsk (_("use of PC in this instruction is deprecated"));
7638         }
7639
7640       if (inst.reloc.type == BFD_RELOC_UNUSED)
7641         {
7642           /* Prefer + for zero encoded value.  */
7643           if (!inst.operands[i].negative)
7644             inst.instruction |= INDEX_UP;
7645           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7646         }
7647     }
7648 }
7649
7650 /* inst.operands[i] was set up by parse_address.  Encode it into an
7651    ARM-format mode 3 load or store instruction.  Reject forms that
7652    cannot be used with such instructions.  If is_t is true, reject
7653    forms that cannot be used with a T instruction (i.e. not
7654    post-indexed).  */
7655 static void
7656 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7657 {
7658   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7659     {
7660       inst.error = _("instruction does not accept scaled register index");
7661       return;
7662     }
7663
7664   encode_arm_addr_mode_common (i, is_t);
7665
7666   if (inst.operands[i].immisreg)
7667     {
7668       constraint ((inst.operands[i].imm == REG_PC
7669                    || (is_t && inst.operands[i].reg == REG_PC)),
7670                   BAD_PC_ADDRESSING);
7671       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7672                   BAD_PC_WRITEBACK);
7673       inst.instruction |= inst.operands[i].imm;
7674       if (!inst.operands[i].negative)
7675         inst.instruction |= INDEX_UP;
7676     }
7677   else /* immediate offset in inst.reloc */
7678     {
7679       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7680                    && inst.operands[i].writeback),
7681                   BAD_PC_WRITEBACK);
7682       inst.instruction |= HWOFFSET_IMM;
7683       if (inst.reloc.type == BFD_RELOC_UNUSED)
7684         {
7685           /* Prefer + for zero encoded value.  */
7686           if (!inst.operands[i].negative)
7687             inst.instruction |= INDEX_UP;
7688
7689           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7690         }
7691     }
7692 }
7693
7694 /* Write immediate bits [7:0] to the following locations:
7695
7696   |28/24|23     19|18 16|15                    4|3     0|
7697   |  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|
7698
7699   This function is used by VMOV/VMVN/VORR/VBIC.  */
7700
7701 static void
7702 neon_write_immbits (unsigned immbits)
7703 {
7704   inst.instruction |= immbits & 0xf;
7705   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7706   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7707 }
7708
7709 /* Invert low-order SIZE bits of XHI:XLO.  */
7710
7711 static void
7712 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7713 {
7714   unsigned immlo = xlo ? *xlo : 0;
7715   unsigned immhi = xhi ? *xhi : 0;
7716
7717   switch (size)
7718     {
7719     case 8:
7720       immlo = (~immlo) & 0xff;
7721       break;
7722
7723     case 16:
7724       immlo = (~immlo) & 0xffff;
7725       break;
7726
7727     case 64:
7728       immhi = (~immhi) & 0xffffffff;
7729       /* fall through.  */
7730
7731     case 32:
7732       immlo = (~immlo) & 0xffffffff;
7733       break;
7734
7735     default:
7736       abort ();
7737     }
7738
7739   if (xlo)
7740     *xlo = immlo;
7741
7742   if (xhi)
7743     *xhi = immhi;
7744 }
7745
7746 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7747    A, B, C, D.  */
7748
7749 static int
7750 neon_bits_same_in_bytes (unsigned imm)
7751 {
7752   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7753          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7754          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7755          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7756 }
7757
7758 /* For immediate of above form, return 0bABCD.  */
7759
7760 static unsigned
7761 neon_squash_bits (unsigned imm)
7762 {
7763   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7764          | ((imm & 0x01000000) >> 21);
7765 }
7766
7767 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7768
7769 static unsigned
7770 neon_qfloat_bits (unsigned imm)
7771 {
7772   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7773 }
7774
7775 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7776    the instruction. *OP is passed as the initial value of the op field, and
7777    may be set to a different value depending on the constant (i.e.
7778    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7779    MVN).  If the immediate looks like a repeated pattern then also
7780    try smaller element sizes.  */
7781
7782 static int
7783 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7784                          unsigned *immbits, int *op, int size,
7785                          enum neon_el_type type)
7786 {
7787   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7788      float.  */
7789   if (type == NT_float && !float_p)
7790     return FAIL;
7791
7792   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7793     {
7794       if (size != 32 || *op == 1)
7795         return FAIL;
7796       *immbits = neon_qfloat_bits (immlo);
7797       return 0xf;
7798     }
7799
7800   if (size == 64)
7801     {
7802       if (neon_bits_same_in_bytes (immhi)
7803           && neon_bits_same_in_bytes (immlo))
7804         {
7805           if (*op == 1)
7806             return FAIL;
7807           *immbits = (neon_squash_bits (immhi) << 4)
7808                      | neon_squash_bits (immlo);
7809           *op = 1;
7810           return 0xe;
7811         }
7812
7813       if (immhi != immlo)
7814         return FAIL;
7815     }
7816
7817   if (size >= 32)
7818     {
7819       if (immlo == (immlo & 0x000000ff))
7820         {
7821           *immbits = immlo;
7822           return 0x0;
7823         }
7824       else if (immlo == (immlo & 0x0000ff00))
7825         {
7826           *immbits = immlo >> 8;
7827           return 0x2;
7828         }
7829       else if (immlo == (immlo & 0x00ff0000))
7830         {
7831           *immbits = immlo >> 16;
7832           return 0x4;
7833         }
7834       else if (immlo == (immlo & 0xff000000))
7835         {
7836           *immbits = immlo >> 24;
7837           return 0x6;
7838         }
7839       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7840         {
7841           *immbits = (immlo >> 8) & 0xff;
7842           return 0xc;
7843         }
7844       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7845         {
7846           *immbits = (immlo >> 16) & 0xff;
7847           return 0xd;
7848         }
7849
7850       if ((immlo & 0xffff) != (immlo >> 16))
7851         return FAIL;
7852       immlo &= 0xffff;
7853     }
7854
7855   if (size >= 16)
7856     {
7857       if (immlo == (immlo & 0x000000ff))
7858         {
7859           *immbits = immlo;
7860           return 0x8;
7861         }
7862       else if (immlo == (immlo & 0x0000ff00))
7863         {
7864           *immbits = immlo >> 8;
7865           return 0xa;
7866         }
7867
7868       if ((immlo & 0xff) != (immlo >> 8))
7869         return FAIL;
7870       immlo &= 0xff;
7871     }
7872
7873   if (immlo == (immlo & 0x000000ff))
7874     {
7875       /* Don't allow MVN with 8-bit immediate.  */
7876       if (*op == 1)
7877         return FAIL;
7878       *immbits = immlo;
7879       return 0xe;
7880     }
7881
7882   return FAIL;
7883 }
7884
7885 #if defined BFD_HOST_64_BIT
7886 /* Returns TRUE if double precision value V may be cast
7887    to single precision without loss of accuracy.  */
7888
7889 static bfd_boolean
7890 is_double_a_single (bfd_int64_t v)
7891 {
7892   int exp = (int)((v >> 52) & 0x7FF);
7893   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7894
7895   return (exp == 0 || exp == 0x7FF
7896           || (exp >= 1023 - 126 && exp <= 1023 + 127))
7897     && (mantissa & 0x1FFFFFFFl) == 0;
7898 }
7899
7900 /* Returns a double precision value casted to single precision
7901    (ignoring the least significant bits in exponent and mantissa).  */
7902
7903 static int
7904 double_to_single (bfd_int64_t v)
7905 {
7906   int sign = (int) ((v >> 63) & 1l);
7907   int exp = (int) ((v >> 52) & 0x7FF);
7908   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7909
7910   if (exp == 0x7FF)
7911     exp = 0xFF;
7912   else
7913     {
7914       exp = exp - 1023 + 127;
7915       if (exp >= 0xFF)
7916         {
7917           /* Infinity.  */
7918           exp = 0x7F;
7919           mantissa = 0;
7920         }
7921       else if (exp < 0)
7922         {
7923           /* No denormalized numbers.  */
7924           exp = 0;
7925           mantissa = 0;
7926         }
7927     }
7928   mantissa >>= 29;
7929   return (sign << 31) | (exp << 23) | mantissa;
7930 }
7931 #endif /* BFD_HOST_64_BIT */
7932
7933 enum lit_type
7934 {
7935   CONST_THUMB,
7936   CONST_ARM,
7937   CONST_VEC
7938 };
7939
7940 static void do_vfp_nsyn_opcode (const char *);
7941
7942 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7943    Determine whether it can be performed with a move instruction; if
7944    it can, convert inst.instruction to that move instruction and
7945    return TRUE; if it can't, convert inst.instruction to a literal-pool
7946    load and return FALSE.  If this is not a valid thing to do in the
7947    current context, set inst.error and return TRUE.
7948
7949    inst.operands[i] describes the destination register.  */
7950
7951 static bfd_boolean
7952 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7953 {
7954   unsigned long tbit;
7955   bfd_boolean thumb_p = (t == CONST_THUMB);
7956   bfd_boolean arm_p   = (t == CONST_ARM);
7957
7958   if (thumb_p)
7959     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7960   else
7961     tbit = LOAD_BIT;
7962
7963   if ((inst.instruction & tbit) == 0)
7964     {
7965       inst.error = _("invalid pseudo operation");
7966       return TRUE;
7967     }
7968
7969   if (inst.reloc.exp.X_op != O_constant
7970       && inst.reloc.exp.X_op != O_symbol
7971       && inst.reloc.exp.X_op != O_big)
7972     {
7973       inst.error = _("constant expression expected");
7974       return TRUE;
7975     }
7976
7977   if (inst.reloc.exp.X_op == O_constant
7978       || inst.reloc.exp.X_op == O_big)
7979     {
7980 #if defined BFD_HOST_64_BIT
7981       bfd_int64_t v;
7982 #else
7983       offsetT v;
7984 #endif
7985       if (inst.reloc.exp.X_op == O_big)
7986         {
7987           LITTLENUM_TYPE w[X_PRECISION];
7988           LITTLENUM_TYPE * l;
7989
7990           if (inst.reloc.exp.X_add_number == -1)
7991             {
7992               gen_to_words (w, X_PRECISION, E_PRECISION);
7993               l = w;
7994               /* FIXME: Should we check words w[2..5] ?  */
7995             }
7996           else
7997             l = generic_bignum;
7998
7999 #if defined BFD_HOST_64_BIT
8000           v =
8001             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
8002                   << LITTLENUM_NUMBER_OF_BITS)
8003                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
8004                 << LITTLENUM_NUMBER_OF_BITS)
8005                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
8006               << LITTLENUM_NUMBER_OF_BITS)
8007              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
8008 #else
8009           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
8010             |  (l[0] & LITTLENUM_MASK);
8011 #endif
8012         }
8013       else
8014         v = inst.reloc.exp.X_add_number;
8015
8016       if (!inst.operands[i].issingle)
8017         {
8018           if (thumb_p)
8019             {
8020               /* LDR should not use lead in a flag-setting instruction being
8021                  chosen so we do not check whether movs can be used.  */
8022
8023               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
8024                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8025                   && inst.operands[i].reg != 13
8026                   && inst.operands[i].reg != 15)
8027                 {
8028                   /* Check if on thumb2 it can be done with a mov.w, mvn or
8029                      movw instruction.  */
8030                   unsigned int newimm;
8031                   bfd_boolean isNegated;
8032
8033                   newimm = encode_thumb32_immediate (v);
8034                   if (newimm != (unsigned int) FAIL)
8035                     isNegated = FALSE;
8036                   else
8037                     {
8038                       newimm = encode_thumb32_immediate (~v);
8039                       if (newimm != (unsigned int) FAIL)
8040                         isNegated = TRUE;
8041                     }
8042
8043                   /* The number can be loaded with a mov.w or mvn
8044                      instruction.  */
8045                   if (newimm != (unsigned int) FAIL
8046                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
8047                     {
8048                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
8049                                           | (inst.operands[i].reg << 8));
8050                       /* Change to MOVN.  */
8051                       inst.instruction |= (isNegated ? 0x200000 : 0);
8052                       inst.instruction |= (newimm & 0x800) << 15;
8053                       inst.instruction |= (newimm & 0x700) << 4;
8054                       inst.instruction |= (newimm & 0x0ff);
8055                       return TRUE;
8056                     }
8057                   /* The number can be loaded with a movw instruction.  */
8058                   else if ((v & ~0xFFFF) == 0
8059                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8060                     {
8061                       int imm = v & 0xFFFF;
8062
8063                       inst.instruction = 0xf2400000;  /* MOVW.  */
8064                       inst.instruction |= (inst.operands[i].reg << 8);
8065                       inst.instruction |= (imm & 0xf000) << 4;
8066                       inst.instruction |= (imm & 0x0800) << 15;
8067                       inst.instruction |= (imm & 0x0700) << 4;
8068                       inst.instruction |= (imm & 0x00ff);
8069                       return TRUE;
8070                     }
8071                 }
8072             }
8073           else if (arm_p)
8074             {
8075               int value = encode_arm_immediate (v);
8076
8077               if (value != FAIL)
8078                 {
8079                   /* This can be done with a mov instruction.  */
8080                   inst.instruction &= LITERAL_MASK;
8081                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8082                   inst.instruction |= value & 0xfff;
8083                   return TRUE;
8084                 }
8085
8086               value = encode_arm_immediate (~ v);
8087               if (value != FAIL)
8088                 {
8089                   /* This can be done with a mvn instruction.  */
8090                   inst.instruction &= LITERAL_MASK;
8091                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8092                   inst.instruction |= value & 0xfff;
8093                   return TRUE;
8094                 }
8095             }
8096           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8097             {
8098               int op = 0;
8099               unsigned immbits = 0;
8100               unsigned immlo = inst.operands[1].imm;
8101               unsigned immhi = inst.operands[1].regisimm
8102                 ? inst.operands[1].reg
8103                 : inst.reloc.exp.X_unsigned
8104                 ? 0
8105                 : ((bfd_int64_t)((int) immlo)) >> 32;
8106               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8107                                                    &op, 64, NT_invtype);
8108
8109               if (cmode == FAIL)
8110                 {
8111                   neon_invert_size (&immlo, &immhi, 64);
8112                   op = !op;
8113                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8114                                                    &op, 64, NT_invtype);
8115                 }
8116
8117               if (cmode != FAIL)
8118                 {
8119                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8120                     | (1 << 23)
8121                     | (cmode << 8)
8122                     | (op << 5)
8123                     | (1 << 4);
8124
8125                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8126                   if (thumb_mode)
8127                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8128                   else
8129                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8130                   neon_write_immbits (immbits);
8131                   return TRUE;
8132                 }
8133             }
8134         }
8135
8136       if (t == CONST_VEC)
8137         {
8138           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8139           if (inst.operands[i].issingle
8140               && is_quarter_float (inst.operands[1].imm)
8141               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8142             {
8143               inst.operands[1].imm =
8144                 neon_qfloat_bits (v);
8145               do_vfp_nsyn_opcode ("fconsts");
8146               return TRUE;
8147             }
8148
8149           /* If our host does not support a 64-bit type then we cannot perform
8150              the following optimization.  This mean that there will be a
8151              discrepancy between the output produced by an assembler built for
8152              a 32-bit-only host and the output produced from a 64-bit host, but
8153              this cannot be helped.  */
8154 #if defined BFD_HOST_64_BIT
8155           else if (!inst.operands[1].issingle
8156                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8157             {
8158               if (is_double_a_single (v)
8159                   && is_quarter_float (double_to_single (v)))
8160                 {
8161                   inst.operands[1].imm =
8162                     neon_qfloat_bits (double_to_single (v));
8163                   do_vfp_nsyn_opcode ("fconstd");
8164                   return TRUE;
8165                 }
8166             }
8167 #endif
8168         }
8169     }
8170
8171   if (add_to_lit_pool ((!inst.operands[i].isvec
8172                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8173     return TRUE;
8174
8175   inst.operands[1].reg = REG_PC;
8176   inst.operands[1].isreg = 1;
8177   inst.operands[1].preind = 1;
8178   inst.reloc.pc_rel = 1;
8179   inst.reloc.type = (thumb_p
8180                      ? BFD_RELOC_ARM_THUMB_OFFSET
8181                      : (mode_3
8182                         ? BFD_RELOC_ARM_HWLITERAL
8183                         : BFD_RELOC_ARM_LITERAL));
8184   return FALSE;
8185 }
8186
8187 /* inst.operands[i] was set up by parse_address.  Encode it into an
8188    ARM-format instruction.  Reject all forms which cannot be encoded
8189    into a coprocessor load/store instruction.  If wb_ok is false,
8190    reject use of writeback; if unind_ok is false, reject use of
8191    unindexed addressing.  If reloc_override is not 0, use it instead
8192    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8193    (in which case it is preserved).  */
8194
8195 static int
8196 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8197 {
8198   if (!inst.operands[i].isreg)
8199     {
8200       /* PR 18256 */
8201       if (! inst.operands[0].isvec)
8202         {
8203           inst.error = _("invalid co-processor operand");
8204           return FAIL;
8205         }
8206       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8207         return SUCCESS;
8208     }
8209
8210   inst.instruction |= inst.operands[i].reg << 16;
8211
8212   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8213
8214   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8215     {
8216       gas_assert (!inst.operands[i].writeback);
8217       if (!unind_ok)
8218         {
8219           inst.error = _("instruction does not support unindexed addressing");
8220           return FAIL;
8221         }
8222       inst.instruction |= inst.operands[i].imm;
8223       inst.instruction |= INDEX_UP;
8224       return SUCCESS;
8225     }
8226
8227   if (inst.operands[i].preind)
8228     inst.instruction |= PRE_INDEX;
8229
8230   if (inst.operands[i].writeback)
8231     {
8232       if (inst.operands[i].reg == REG_PC)
8233         {
8234           inst.error = _("pc may not be used with write-back");
8235           return FAIL;
8236         }
8237       if (!wb_ok)
8238         {
8239           inst.error = _("instruction does not support writeback");
8240           return FAIL;
8241         }
8242       inst.instruction |= WRITE_BACK;
8243     }
8244
8245   if (reloc_override)
8246     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
8247   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
8248             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
8249            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
8250     {
8251       if (thumb_mode)
8252         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8253       else
8254         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
8255     }
8256
8257   /* Prefer + for zero encoded value.  */
8258   if (!inst.operands[i].negative)
8259     inst.instruction |= INDEX_UP;
8260
8261   return SUCCESS;
8262 }
8263
8264 /* Functions for instruction encoding, sorted by sub-architecture.
8265    First some generics; their names are taken from the conventional
8266    bit positions for register arguments in ARM format instructions.  */
8267
8268 static void
8269 do_noargs (void)
8270 {
8271 }
8272
8273 static void
8274 do_rd (void)
8275 {
8276   inst.instruction |= inst.operands[0].reg << 12;
8277 }
8278
8279 static void
8280 do_rn (void)
8281 {
8282   inst.instruction |= inst.operands[0].reg << 16;
8283 }
8284
8285 static void
8286 do_rd_rm (void)
8287 {
8288   inst.instruction |= inst.operands[0].reg << 12;
8289   inst.instruction |= inst.operands[1].reg;
8290 }
8291
8292 static void
8293 do_rm_rn (void)
8294 {
8295   inst.instruction |= inst.operands[0].reg;
8296   inst.instruction |= inst.operands[1].reg << 16;
8297 }
8298
8299 static void
8300 do_rd_rn (void)
8301 {
8302   inst.instruction |= inst.operands[0].reg << 12;
8303   inst.instruction |= inst.operands[1].reg << 16;
8304 }
8305
8306 static void
8307 do_rn_rd (void)
8308 {
8309   inst.instruction |= inst.operands[0].reg << 16;
8310   inst.instruction |= inst.operands[1].reg << 12;
8311 }
8312
8313 static void
8314 do_tt (void)
8315 {
8316   inst.instruction |= inst.operands[0].reg << 8;
8317   inst.instruction |= inst.operands[1].reg << 16;
8318 }
8319
8320 static bfd_boolean
8321 check_obsolete (const arm_feature_set *feature, const char *msg)
8322 {
8323   if (ARM_CPU_IS_ANY (cpu_variant))
8324     {
8325       as_tsktsk ("%s", msg);
8326       return TRUE;
8327     }
8328   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8329     {
8330       as_bad ("%s", msg);
8331       return TRUE;
8332     }
8333
8334   return FALSE;
8335 }
8336
8337 static void
8338 do_rd_rm_rn (void)
8339 {
8340   unsigned Rn = inst.operands[2].reg;
8341   /* Enforce restrictions on SWP instruction.  */
8342   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8343     {
8344       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8345                   _("Rn must not overlap other operands"));
8346
8347       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8348        */
8349       if (!check_obsolete (&arm_ext_v8,
8350                            _("swp{b} use is obsoleted for ARMv8 and later"))
8351           && warn_on_deprecated
8352           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8353         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8354     }
8355
8356   inst.instruction |= inst.operands[0].reg << 12;
8357   inst.instruction |= inst.operands[1].reg;
8358   inst.instruction |= Rn << 16;
8359 }
8360
8361 static void
8362 do_rd_rn_rm (void)
8363 {
8364   inst.instruction |= inst.operands[0].reg << 12;
8365   inst.instruction |= inst.operands[1].reg << 16;
8366   inst.instruction |= inst.operands[2].reg;
8367 }
8368
8369 static void
8370 do_rm_rd_rn (void)
8371 {
8372   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8373   constraint (((inst.reloc.exp.X_op != O_constant
8374                 && inst.reloc.exp.X_op != O_illegal)
8375                || inst.reloc.exp.X_add_number != 0),
8376               BAD_ADDR_MODE);
8377   inst.instruction |= inst.operands[0].reg;
8378   inst.instruction |= inst.operands[1].reg << 12;
8379   inst.instruction |= inst.operands[2].reg << 16;
8380 }
8381
8382 static void
8383 do_imm0 (void)
8384 {
8385   inst.instruction |= inst.operands[0].imm;
8386 }
8387
8388 static void
8389 do_rd_cpaddr (void)
8390 {
8391   inst.instruction |= inst.operands[0].reg << 12;
8392   encode_arm_cp_address (1, TRUE, TRUE, 0);
8393 }
8394
8395 /* ARM instructions, in alphabetical order by function name (except
8396    that wrapper functions appear immediately after the function they
8397    wrap).  */
8398
8399 /* This is a pseudo-op of the form "adr rd, label" to be converted
8400    into a relative address of the form "add rd, pc, #label-.-8".  */
8401
8402 static void
8403 do_adr (void)
8404 {
8405   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8406
8407   /* Frag hacking will turn this into a sub instruction if the offset turns
8408      out to be negative.  */
8409   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8410   inst.reloc.pc_rel = 1;
8411   inst.reloc.exp.X_add_number -= 8;
8412
8413   if (inst.reloc.exp.X_op == O_symbol
8414       && inst.reloc.exp.X_add_symbol != NULL
8415       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
8416       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
8417     inst.reloc.exp.X_add_number += 1;
8418 }
8419
8420 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8421    into a relative address of the form:
8422    add rd, pc, #low(label-.-8)"
8423    add rd, rd, #high(label-.-8)"  */
8424
8425 static void
8426 do_adrl (void)
8427 {
8428   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8429
8430   /* Frag hacking will turn this into a sub instruction if the offset turns
8431      out to be negative.  */
8432   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8433   inst.reloc.pc_rel            = 1;
8434   inst.size                    = INSN_SIZE * 2;
8435   inst.reloc.exp.X_add_number -= 8;
8436
8437   if (inst.reloc.exp.X_op == O_symbol
8438       && inst.reloc.exp.X_add_symbol != NULL
8439       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
8440       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
8441     inst.reloc.exp.X_add_number += 1;
8442 }
8443
8444 static void
8445 do_arit (void)
8446 {
8447   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
8448               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
8449               THUMB1_RELOC_ONLY);
8450   if (!inst.operands[1].present)
8451     inst.operands[1].reg = inst.operands[0].reg;
8452   inst.instruction |= inst.operands[0].reg << 12;
8453   inst.instruction |= inst.operands[1].reg << 16;
8454   encode_arm_shifter_operand (2);
8455 }
8456
8457 static void
8458 do_barrier (void)
8459 {
8460   if (inst.operands[0].present)
8461     inst.instruction |= inst.operands[0].imm;
8462   else
8463     inst.instruction |= 0xf;
8464 }
8465
8466 static void
8467 do_bfc (void)
8468 {
8469   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8470   constraint (msb > 32, _("bit-field extends past end of register"));
8471   /* The instruction encoding stores the LSB and MSB,
8472      not the LSB and width.  */
8473   inst.instruction |= inst.operands[0].reg << 12;
8474   inst.instruction |= inst.operands[1].imm << 7;
8475   inst.instruction |= (msb - 1) << 16;
8476 }
8477
8478 static void
8479 do_bfi (void)
8480 {
8481   unsigned int msb;
8482
8483   /* #0 in second position is alternative syntax for bfc, which is
8484      the same instruction but with REG_PC in the Rm field.  */
8485   if (!inst.operands[1].isreg)
8486     inst.operands[1].reg = REG_PC;
8487
8488   msb = inst.operands[2].imm + inst.operands[3].imm;
8489   constraint (msb > 32, _("bit-field extends past end of register"));
8490   /* The instruction encoding stores the LSB and MSB,
8491      not the LSB and width.  */
8492   inst.instruction |= inst.operands[0].reg << 12;
8493   inst.instruction |= inst.operands[1].reg;
8494   inst.instruction |= inst.operands[2].imm << 7;
8495   inst.instruction |= (msb - 1) << 16;
8496 }
8497
8498 static void
8499 do_bfx (void)
8500 {
8501   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8502               _("bit-field extends past end of register"));
8503   inst.instruction |= inst.operands[0].reg << 12;
8504   inst.instruction |= inst.operands[1].reg;
8505   inst.instruction |= inst.operands[2].imm << 7;
8506   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8507 }
8508
8509 /* ARM V5 breakpoint instruction (argument parse)
8510      BKPT <16 bit unsigned immediate>
8511      Instruction is not conditional.
8512         The bit pattern given in insns[] has the COND_ALWAYS condition,
8513         and it is an error if the caller tried to override that.  */
8514
8515 static void
8516 do_bkpt (void)
8517 {
8518   /* Top 12 of 16 bits to bits 19:8.  */
8519   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8520
8521   /* Bottom 4 of 16 bits to bits 3:0.  */
8522   inst.instruction |= inst.operands[0].imm & 0xf;
8523 }
8524
8525 static void
8526 encode_branch (int default_reloc)
8527 {
8528   if (inst.operands[0].hasreloc)
8529     {
8530       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8531                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8532                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8533       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8534         ? BFD_RELOC_ARM_PLT32
8535         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8536     }
8537   else
8538     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8539   inst.reloc.pc_rel = 1;
8540 }
8541
8542 static void
8543 do_branch (void)
8544 {
8545 #ifdef OBJ_ELF
8546   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8547     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8548   else
8549 #endif
8550     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8551 }
8552
8553 static void
8554 do_bl (void)
8555 {
8556 #ifdef OBJ_ELF
8557   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8558     {
8559       if (inst.cond == COND_ALWAYS)
8560         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8561       else
8562         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8563     }
8564   else
8565 #endif
8566     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8567 }
8568
8569 /* ARM V5 branch-link-exchange instruction (argument parse)
8570      BLX <target_addr>          ie BLX(1)
8571      BLX{<condition>} <Rm>      ie BLX(2)
8572    Unfortunately, there are two different opcodes for this mnemonic.
8573    So, the insns[].value is not used, and the code here zaps values
8574         into inst.instruction.
8575    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8576
8577 static void
8578 do_blx (void)
8579 {
8580   if (inst.operands[0].isreg)
8581     {
8582       /* Arg is a register; the opcode provided by insns[] is correct.
8583          It is not illegal to do "blx pc", just useless.  */
8584       if (inst.operands[0].reg == REG_PC)
8585         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8586
8587       inst.instruction |= inst.operands[0].reg;
8588     }
8589   else
8590     {
8591       /* Arg is an address; this instruction cannot be executed
8592          conditionally, and the opcode must be adjusted.
8593          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8594          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8595       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8596       inst.instruction = 0xfa000000;
8597       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8598     }
8599 }
8600
8601 static void
8602 do_bx (void)
8603 {
8604   bfd_boolean want_reloc;
8605
8606   if (inst.operands[0].reg == REG_PC)
8607     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8608
8609   inst.instruction |= inst.operands[0].reg;
8610   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8611      it is for ARMv4t or earlier.  */
8612   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8613   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
8614       want_reloc = TRUE;
8615
8616 #ifdef OBJ_ELF
8617   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8618 #endif
8619     want_reloc = FALSE;
8620
8621   if (want_reloc)
8622     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8623 }
8624
8625
8626 /* ARM v5TEJ.  Jump to Jazelle code.  */
8627
8628 static void
8629 do_bxj (void)
8630 {
8631   if (inst.operands[0].reg == REG_PC)
8632     as_tsktsk (_("use of r15 in bxj is not really useful"));
8633
8634   inst.instruction |= inst.operands[0].reg;
8635 }
8636
8637 /* Co-processor data operation:
8638       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8639       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8640 static void
8641 do_cdp (void)
8642 {
8643   inst.instruction |= inst.operands[0].reg << 8;
8644   inst.instruction |= inst.operands[1].imm << 20;
8645   inst.instruction |= inst.operands[2].reg << 12;
8646   inst.instruction |= inst.operands[3].reg << 16;
8647   inst.instruction |= inst.operands[4].reg;
8648   inst.instruction |= inst.operands[5].imm << 5;
8649 }
8650
8651 static void
8652 do_cmp (void)
8653 {
8654   inst.instruction |= inst.operands[0].reg << 16;
8655   encode_arm_shifter_operand (1);
8656 }
8657
8658 /* Transfer between coprocessor and ARM registers.
8659    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8660    MRC2
8661    MCR{cond}
8662    MCR2
8663
8664    No special properties.  */
8665
8666 struct deprecated_coproc_regs_s
8667 {
8668   unsigned cp;
8669   int opc1;
8670   unsigned crn;
8671   unsigned crm;
8672   int opc2;
8673   arm_feature_set deprecated;
8674   arm_feature_set obsoleted;
8675   const char *dep_msg;
8676   const char *obs_msg;
8677 };
8678
8679 #define DEPR_ACCESS_V8 \
8680   N_("This coprocessor register access is deprecated in ARMv8")
8681
8682 /* Table of all deprecated coprocessor registers.  */
8683 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8684 {
8685     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8686      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8687      DEPR_ACCESS_V8, NULL},
8688     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8689      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8690      DEPR_ACCESS_V8, NULL},
8691     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8692      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8693      DEPR_ACCESS_V8, NULL},
8694     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8695      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8696      DEPR_ACCESS_V8, NULL},
8697     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8698      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8699      DEPR_ACCESS_V8, NULL},
8700 };
8701
8702 #undef DEPR_ACCESS_V8
8703
8704 static const size_t deprecated_coproc_reg_count =
8705   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8706
8707 static void
8708 do_co_reg (void)
8709 {
8710   unsigned Rd;
8711   size_t i;
8712
8713   Rd = inst.operands[2].reg;
8714   if (thumb_mode)
8715     {
8716       if (inst.instruction == 0xee000010
8717           || inst.instruction == 0xfe000010)
8718         /* MCR, MCR2  */
8719         reject_bad_reg (Rd);
8720       else if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
8721         /* MRC, MRC2  */
8722         constraint (Rd == REG_SP, BAD_SP);
8723     }
8724   else
8725     {
8726       /* MCR */
8727       if (inst.instruction == 0xe000010)
8728         constraint (Rd == REG_PC, BAD_PC);
8729     }
8730
8731     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8732       {
8733         const struct deprecated_coproc_regs_s *r =
8734           deprecated_coproc_regs + i;
8735
8736         if (inst.operands[0].reg == r->cp
8737             && inst.operands[1].imm == r->opc1
8738             && inst.operands[3].reg == r->crn
8739             && inst.operands[4].reg == r->crm
8740             && inst.operands[5].imm == r->opc2)
8741           {
8742             if (! ARM_CPU_IS_ANY (cpu_variant)
8743                 && warn_on_deprecated
8744                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8745               as_tsktsk ("%s", r->dep_msg);
8746           }
8747       }
8748
8749   inst.instruction |= inst.operands[0].reg << 8;
8750   inst.instruction |= inst.operands[1].imm << 21;
8751   inst.instruction |= Rd << 12;
8752   inst.instruction |= inst.operands[3].reg << 16;
8753   inst.instruction |= inst.operands[4].reg;
8754   inst.instruction |= inst.operands[5].imm << 5;
8755 }
8756
8757 /* Transfer between coprocessor register and pair of ARM registers.
8758    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8759    MCRR2
8760    MRRC{cond}
8761    MRRC2
8762
8763    Two XScale instructions are special cases of these:
8764
8765      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8766      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8767
8768    Result unpredictable if Rd or Rn is R15.  */
8769
8770 static void
8771 do_co_reg2c (void)
8772 {
8773   unsigned Rd, Rn;
8774
8775   Rd = inst.operands[2].reg;
8776   Rn = inst.operands[3].reg;
8777
8778   if (thumb_mode)
8779     {
8780       reject_bad_reg (Rd);
8781       reject_bad_reg (Rn);
8782     }
8783   else
8784     {
8785       constraint (Rd == REG_PC, BAD_PC);
8786       constraint (Rn == REG_PC, BAD_PC);
8787     }
8788
8789   /* Only check the MRRC{2} variants.  */
8790   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
8791     {
8792        /* If Rd == Rn, error that the operation is
8793           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
8794        constraint (Rd == Rn, BAD_OVERLAP);
8795     }
8796
8797   inst.instruction |= inst.operands[0].reg << 8;
8798   inst.instruction |= inst.operands[1].imm << 4;
8799   inst.instruction |= Rd << 12;
8800   inst.instruction |= Rn << 16;
8801   inst.instruction |= inst.operands[4].reg;
8802 }
8803
8804 static void
8805 do_cpsi (void)
8806 {
8807   inst.instruction |= inst.operands[0].imm << 6;
8808   if (inst.operands[1].present)
8809     {
8810       inst.instruction |= CPSI_MMOD;
8811       inst.instruction |= inst.operands[1].imm;
8812     }
8813 }
8814
8815 static void
8816 do_dbg (void)
8817 {
8818   inst.instruction |= inst.operands[0].imm;
8819 }
8820
8821 static void
8822 do_div (void)
8823 {
8824   unsigned Rd, Rn, Rm;
8825
8826   Rd = inst.operands[0].reg;
8827   Rn = (inst.operands[1].present
8828         ? inst.operands[1].reg : Rd);
8829   Rm = inst.operands[2].reg;
8830
8831   constraint ((Rd == REG_PC), BAD_PC);
8832   constraint ((Rn == REG_PC), BAD_PC);
8833   constraint ((Rm == REG_PC), BAD_PC);
8834
8835   inst.instruction |= Rd << 16;
8836   inst.instruction |= Rn << 0;
8837   inst.instruction |= Rm << 8;
8838 }
8839
8840 static void
8841 do_it (void)
8842 {
8843   /* There is no IT instruction in ARM mode.  We
8844      process it to do the validation as if in
8845      thumb mode, just in case the code gets
8846      assembled for thumb using the unified syntax.  */
8847
8848   inst.size = 0;
8849   if (unified_syntax)
8850     {
8851       set_it_insn_type (IT_INSN);
8852       now_it.mask = (inst.instruction & 0xf) | 0x10;
8853       now_it.cc = inst.operands[0].imm;
8854     }
8855 }
8856
8857 /* If there is only one register in the register list,
8858    then return its register number.  Otherwise return -1.  */
8859 static int
8860 only_one_reg_in_list (int range)
8861 {
8862   int i = ffs (range) - 1;
8863   return (i > 15 || range != (1 << i)) ? -1 : i;
8864 }
8865
8866 static void
8867 encode_ldmstm(int from_push_pop_mnem)
8868 {
8869   int base_reg = inst.operands[0].reg;
8870   int range = inst.operands[1].imm;
8871   int one_reg;
8872
8873   inst.instruction |= base_reg << 16;
8874   inst.instruction |= range;
8875
8876   if (inst.operands[1].writeback)
8877     inst.instruction |= LDM_TYPE_2_OR_3;
8878
8879   if (inst.operands[0].writeback)
8880     {
8881       inst.instruction |= WRITE_BACK;
8882       /* Check for unpredictable uses of writeback.  */
8883       if (inst.instruction & LOAD_BIT)
8884         {
8885           /* Not allowed in LDM type 2.  */
8886           if ((inst.instruction & LDM_TYPE_2_OR_3)
8887               && ((range & (1 << REG_PC)) == 0))
8888             as_warn (_("writeback of base register is UNPREDICTABLE"));
8889           /* Only allowed if base reg not in list for other types.  */
8890           else if (range & (1 << base_reg))
8891             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8892         }
8893       else /* STM.  */
8894         {
8895           /* Not allowed for type 2.  */
8896           if (inst.instruction & LDM_TYPE_2_OR_3)
8897             as_warn (_("writeback of base register is UNPREDICTABLE"));
8898           /* Only allowed if base reg not in list, or first in list.  */
8899           else if ((range & (1 << base_reg))
8900                    && (range & ((1 << base_reg) - 1)))
8901             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8902         }
8903     }
8904
8905   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8906   one_reg = only_one_reg_in_list (range);
8907   if (from_push_pop_mnem && one_reg >= 0)
8908     {
8909       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8910
8911       if (is_push && one_reg == 13 /* SP */)
8912         /* PR 22483: The A2 encoding cannot be used when
8913            pushing the stack pointer as this is UNPREDICTABLE.  */
8914         return;
8915
8916       inst.instruction &= A_COND_MASK;
8917       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8918       inst.instruction |= one_reg << 12;
8919     }
8920 }
8921
8922 static void
8923 do_ldmstm (void)
8924 {
8925   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8926 }
8927
8928 /* ARMv5TE load-consecutive (argument parse)
8929    Mode is like LDRH.
8930
8931      LDRccD R, mode
8932      STRccD R, mode.  */
8933
8934 static void
8935 do_ldrd (void)
8936 {
8937   constraint (inst.operands[0].reg % 2 != 0,
8938               _("first transfer register must be even"));
8939   constraint (inst.operands[1].present
8940               && inst.operands[1].reg != inst.operands[0].reg + 1,
8941               _("can only transfer two consecutive registers"));
8942   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8943   constraint (!inst.operands[2].isreg, _("'[' expected"));
8944
8945   if (!inst.operands[1].present)
8946     inst.operands[1].reg = inst.operands[0].reg + 1;
8947
8948   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8949      register and the first register written; we have to diagnose
8950      overlap between the base and the second register written here.  */
8951
8952   if (inst.operands[2].reg == inst.operands[1].reg
8953       && (inst.operands[2].writeback || inst.operands[2].postind))
8954     as_warn (_("base register written back, and overlaps "
8955                "second transfer register"));
8956
8957   if (!(inst.instruction & V4_STR_BIT))
8958     {
8959       /* For an index-register load, the index register must not overlap the
8960         destination (even if not write-back).  */
8961       if (inst.operands[2].immisreg
8962               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8963               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8964         as_warn (_("index register overlaps transfer register"));
8965     }
8966   inst.instruction |= inst.operands[0].reg << 12;
8967   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8968 }
8969
8970 static void
8971 do_ldrex (void)
8972 {
8973   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8974               || inst.operands[1].postind || inst.operands[1].writeback
8975               || inst.operands[1].immisreg || inst.operands[1].shifted
8976               || inst.operands[1].negative
8977               /* This can arise if the programmer has written
8978                    strex rN, rM, foo
8979                  or if they have mistakenly used a register name as the last
8980                  operand,  eg:
8981                    strex rN, rM, rX
8982                  It is very difficult to distinguish between these two cases
8983                  because "rX" might actually be a label. ie the register
8984                  name has been occluded by a symbol of the same name. So we
8985                  just generate a general 'bad addressing mode' type error
8986                  message and leave it up to the programmer to discover the
8987                  true cause and fix their mistake.  */
8988               || (inst.operands[1].reg == REG_PC),
8989               BAD_ADDR_MODE);
8990
8991   constraint (inst.reloc.exp.X_op != O_constant
8992               || inst.reloc.exp.X_add_number != 0,
8993               _("offset must be zero in ARM encoding"));
8994
8995   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8996
8997   inst.instruction |= inst.operands[0].reg << 12;
8998   inst.instruction |= inst.operands[1].reg << 16;
8999   inst.reloc.type = BFD_RELOC_UNUSED;
9000 }
9001
9002 static void
9003 do_ldrexd (void)
9004 {
9005   constraint (inst.operands[0].reg % 2 != 0,
9006               _("even register required"));
9007   constraint (inst.operands[1].present
9008               && inst.operands[1].reg != inst.operands[0].reg + 1,
9009               _("can only load two consecutive registers"));
9010   /* If op 1 were present and equal to PC, this function wouldn't
9011      have been called in the first place.  */
9012   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
9013
9014   inst.instruction |= inst.operands[0].reg << 12;
9015   inst.instruction |= inst.operands[2].reg << 16;
9016 }
9017
9018 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
9019    which is not a multiple of four is UNPREDICTABLE.  */
9020 static void
9021 check_ldr_r15_aligned (void)
9022 {
9023   constraint (!(inst.operands[1].immisreg)
9024               && (inst.operands[0].reg == REG_PC
9025               && inst.operands[1].reg == REG_PC
9026               && (inst.reloc.exp.X_add_number & 0x3)),
9027               _("ldr to register 15 must be 4-byte aligned"));
9028 }
9029
9030 static void
9031 do_ldst (void)
9032 {
9033   inst.instruction |= inst.operands[0].reg << 12;
9034   if (!inst.operands[1].isreg)
9035     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
9036       return;
9037   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
9038   check_ldr_r15_aligned ();
9039 }
9040
9041 static void
9042 do_ldstt (void)
9043 {
9044   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9045      reject [Rn,...].  */
9046   if (inst.operands[1].preind)
9047     {
9048       constraint (inst.reloc.exp.X_op != O_constant
9049                   || inst.reloc.exp.X_add_number != 0,
9050                   _("this instruction requires a post-indexed address"));
9051
9052       inst.operands[1].preind = 0;
9053       inst.operands[1].postind = 1;
9054       inst.operands[1].writeback = 1;
9055     }
9056   inst.instruction |= inst.operands[0].reg << 12;
9057   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
9058 }
9059
9060 /* Halfword and signed-byte load/store operations.  */
9061
9062 static void
9063 do_ldstv4 (void)
9064 {
9065   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9066   inst.instruction |= inst.operands[0].reg << 12;
9067   if (!inst.operands[1].isreg)
9068     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
9069       return;
9070   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
9071 }
9072
9073 static void
9074 do_ldsttv4 (void)
9075 {
9076   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9077      reject [Rn,...].  */
9078   if (inst.operands[1].preind)
9079     {
9080       constraint (inst.reloc.exp.X_op != O_constant
9081                   || inst.reloc.exp.X_add_number != 0,
9082                   _("this instruction requires a post-indexed address"));
9083
9084       inst.operands[1].preind = 0;
9085       inst.operands[1].postind = 1;
9086       inst.operands[1].writeback = 1;
9087     }
9088   inst.instruction |= inst.operands[0].reg << 12;
9089   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9090 }
9091
9092 /* Co-processor register load/store.
9093    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9094 static void
9095 do_lstc (void)
9096 {
9097   inst.instruction |= inst.operands[0].reg << 8;
9098   inst.instruction |= inst.operands[1].reg << 12;
9099   encode_arm_cp_address (2, TRUE, TRUE, 0);
9100 }
9101
9102 static void
9103 do_mlas (void)
9104 {
9105   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9106   if (inst.operands[0].reg == inst.operands[1].reg
9107       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9108       && !(inst.instruction & 0x00400000))
9109     as_tsktsk (_("Rd and Rm should be different in mla"));
9110
9111   inst.instruction |= inst.operands[0].reg << 16;
9112   inst.instruction |= inst.operands[1].reg;
9113   inst.instruction |= inst.operands[2].reg << 8;
9114   inst.instruction |= inst.operands[3].reg << 12;
9115 }
9116
9117 static void
9118 do_mov (void)
9119 {
9120   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9121               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9122               THUMB1_RELOC_ONLY);
9123   inst.instruction |= inst.operands[0].reg << 12;
9124   encode_arm_shifter_operand (1);
9125 }
9126
9127 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9128 static void
9129 do_mov16 (void)
9130 {
9131   bfd_vma imm;
9132   bfd_boolean top;
9133
9134   top = (inst.instruction & 0x00400000) != 0;
9135   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
9136               _(":lower16: not allowed in this instruction"));
9137   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
9138               _(":upper16: not allowed in this instruction"));
9139   inst.instruction |= inst.operands[0].reg << 12;
9140   if (inst.reloc.type == BFD_RELOC_UNUSED)
9141     {
9142       imm = inst.reloc.exp.X_add_number;
9143       /* The value is in two pieces: 0:11, 16:19.  */
9144       inst.instruction |= (imm & 0x00000fff);
9145       inst.instruction |= (imm & 0x0000f000) << 4;
9146     }
9147 }
9148
9149 static int
9150 do_vfp_nsyn_mrs (void)
9151 {
9152   if (inst.operands[0].isvec)
9153     {
9154       if (inst.operands[1].reg != 1)
9155         first_error (_("operand 1 must be FPSCR"));
9156       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9157       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9158       do_vfp_nsyn_opcode ("fmstat");
9159     }
9160   else if (inst.operands[1].isvec)
9161     do_vfp_nsyn_opcode ("fmrx");
9162   else
9163     return FAIL;
9164
9165   return SUCCESS;
9166 }
9167
9168 static int
9169 do_vfp_nsyn_msr (void)
9170 {
9171   if (inst.operands[0].isvec)
9172     do_vfp_nsyn_opcode ("fmxr");
9173   else
9174     return FAIL;
9175
9176   return SUCCESS;
9177 }
9178
9179 static void
9180 do_vmrs (void)
9181 {
9182   unsigned Rt = inst.operands[0].reg;
9183
9184   if (thumb_mode && Rt == REG_SP)
9185     {
9186       inst.error = BAD_SP;
9187       return;
9188     }
9189
9190   /* MVFR2 is only valid at ARMv8-A.  */
9191   if (inst.operands[1].reg == 5)
9192     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9193                 _(BAD_FPU));
9194
9195   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9196   if (!inst.operands[0].isvec && Rt == REG_PC)
9197     {
9198       inst.error = BAD_PC;
9199       return;
9200     }
9201
9202   /* If we get through parsing the register name, we just insert the number
9203      generated into the instruction without further validation.  */
9204   inst.instruction |= (inst.operands[1].reg << 16);
9205   inst.instruction |= (Rt << 12);
9206 }
9207
9208 static void
9209 do_vmsr (void)
9210 {
9211   unsigned Rt = inst.operands[1].reg;
9212
9213   if (thumb_mode)
9214     reject_bad_reg (Rt);
9215   else if (Rt == REG_PC)
9216     {
9217       inst.error = BAD_PC;
9218       return;
9219     }
9220
9221   /* MVFR2 is only valid for ARMv8-A.  */
9222   if (inst.operands[0].reg == 5)
9223     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
9224                 _(BAD_FPU));
9225
9226   /* If we get through parsing the register name, we just insert the number
9227      generated into the instruction without further validation.  */
9228   inst.instruction |= (inst.operands[0].reg << 16);
9229   inst.instruction |= (Rt << 12);
9230 }
9231
9232 static void
9233 do_mrs (void)
9234 {
9235   unsigned br;
9236
9237   if (do_vfp_nsyn_mrs () == SUCCESS)
9238     return;
9239
9240   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9241   inst.instruction |= inst.operands[0].reg << 12;
9242
9243   if (inst.operands[1].isreg)
9244     {
9245       br = inst.operands[1].reg;
9246       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf0000))
9247         as_bad (_("bad register for mrs"));
9248     }
9249   else
9250     {
9251       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9252       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9253                   != (PSR_c|PSR_f),
9254                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9255       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9256     }
9257
9258   inst.instruction |= br;
9259 }
9260
9261 /* Two possible forms:
9262       "{C|S}PSR_<field>, Rm",
9263       "{C|S}PSR_f, #expression".  */
9264
9265 static void
9266 do_msr (void)
9267 {
9268   if (do_vfp_nsyn_msr () == SUCCESS)
9269     return;
9270
9271   inst.instruction |= inst.operands[0].imm;
9272   if (inst.operands[1].isreg)
9273     inst.instruction |= inst.operands[1].reg;
9274   else
9275     {
9276       inst.instruction |= INST_IMMEDIATE;
9277       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
9278       inst.reloc.pc_rel = 0;
9279     }
9280 }
9281
9282 static void
9283 do_mul (void)
9284 {
9285   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9286
9287   if (!inst.operands[2].present)
9288     inst.operands[2].reg = inst.operands[0].reg;
9289   inst.instruction |= inst.operands[0].reg << 16;
9290   inst.instruction |= inst.operands[1].reg;
9291   inst.instruction |= inst.operands[2].reg << 8;
9292
9293   if (inst.operands[0].reg == inst.operands[1].reg
9294       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9295     as_tsktsk (_("Rd and Rm should be different in mul"));
9296 }
9297
9298 /* Long Multiply Parser
9299    UMULL RdLo, RdHi, Rm, Rs
9300    SMULL RdLo, RdHi, Rm, Rs
9301    UMLAL RdLo, RdHi, Rm, Rs
9302    SMLAL RdLo, RdHi, Rm, Rs.  */
9303
9304 static void
9305 do_mull (void)
9306 {
9307   inst.instruction |= inst.operands[0].reg << 12;
9308   inst.instruction |= inst.operands[1].reg << 16;
9309   inst.instruction |= inst.operands[2].reg;
9310   inst.instruction |= inst.operands[3].reg << 8;
9311
9312   /* rdhi and rdlo must be different.  */
9313   if (inst.operands[0].reg == inst.operands[1].reg)
9314     as_tsktsk (_("rdhi and rdlo must be different"));
9315
9316   /* rdhi, rdlo and rm must all be different before armv6.  */
9317   if ((inst.operands[0].reg == inst.operands[2].reg
9318       || inst.operands[1].reg == inst.operands[2].reg)
9319       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9320     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9321 }
9322
9323 static void
9324 do_nop (void)
9325 {
9326   if (inst.operands[0].present
9327       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9328     {
9329       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9330       inst.instruction &= 0xf0000000;
9331       inst.instruction |= 0x0320f000;
9332       if (inst.operands[0].present)
9333         inst.instruction |= inst.operands[0].imm;
9334     }
9335 }
9336
9337 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9338    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9339    Condition defaults to COND_ALWAYS.
9340    Error if Rd, Rn or Rm are R15.  */
9341
9342 static void
9343 do_pkhbt (void)
9344 {
9345   inst.instruction |= inst.operands[0].reg << 12;
9346   inst.instruction |= inst.operands[1].reg << 16;
9347   inst.instruction |= inst.operands[2].reg;
9348   if (inst.operands[3].present)
9349     encode_arm_shift (3);
9350 }
9351
9352 /* ARM V6 PKHTB (Argument Parse).  */
9353
9354 static void
9355 do_pkhtb (void)
9356 {
9357   if (!inst.operands[3].present)
9358     {
9359       /* If the shift specifier is omitted, turn the instruction
9360          into pkhbt rd, rm, rn. */
9361       inst.instruction &= 0xfff00010;
9362       inst.instruction |= inst.operands[0].reg << 12;
9363       inst.instruction |= inst.operands[1].reg;
9364       inst.instruction |= inst.operands[2].reg << 16;
9365     }
9366   else
9367     {
9368       inst.instruction |= inst.operands[0].reg << 12;
9369       inst.instruction |= inst.operands[1].reg << 16;
9370       inst.instruction |= inst.operands[2].reg;
9371       encode_arm_shift (3);
9372     }
9373 }
9374
9375 /* ARMv5TE: Preload-Cache
9376    MP Extensions: Preload for write
9377
9378     PLD(W) <addr_mode>
9379
9380   Syntactically, like LDR with B=1, W=0, L=1.  */
9381
9382 static void
9383 do_pld (void)
9384 {
9385   constraint (!inst.operands[0].isreg,
9386               _("'[' expected after PLD mnemonic"));
9387   constraint (inst.operands[0].postind,
9388               _("post-indexed expression used in preload instruction"));
9389   constraint (inst.operands[0].writeback,
9390               _("writeback used in preload instruction"));
9391   constraint (!inst.operands[0].preind,
9392               _("unindexed addressing used in preload instruction"));
9393   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9394 }
9395
9396 /* ARMv7: PLI <addr_mode>  */
9397 static void
9398 do_pli (void)
9399 {
9400   constraint (!inst.operands[0].isreg,
9401               _("'[' expected after PLI mnemonic"));
9402   constraint (inst.operands[0].postind,
9403               _("post-indexed expression used in preload instruction"));
9404   constraint (inst.operands[0].writeback,
9405               _("writeback used in preload instruction"));
9406   constraint (!inst.operands[0].preind,
9407               _("unindexed addressing used in preload instruction"));
9408   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9409   inst.instruction &= ~PRE_INDEX;
9410 }
9411
9412 static void
9413 do_push_pop (void)
9414 {
9415   constraint (inst.operands[0].writeback,
9416               _("push/pop do not support {reglist}^"));
9417   inst.operands[1] = inst.operands[0];
9418   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9419   inst.operands[0].isreg = 1;
9420   inst.operands[0].writeback = 1;
9421   inst.operands[0].reg = REG_SP;
9422   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9423 }
9424
9425 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9426    word at the specified address and the following word
9427    respectively.
9428    Unconditionally executed.
9429    Error if Rn is R15.  */
9430
9431 static void
9432 do_rfe (void)
9433 {
9434   inst.instruction |= inst.operands[0].reg << 16;
9435   if (inst.operands[0].writeback)
9436     inst.instruction |= WRITE_BACK;
9437 }
9438
9439 /* ARM V6 ssat (argument parse).  */
9440
9441 static void
9442 do_ssat (void)
9443 {
9444   inst.instruction |= inst.operands[0].reg << 12;
9445   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9446   inst.instruction |= inst.operands[2].reg;
9447
9448   if (inst.operands[3].present)
9449     encode_arm_shift (3);
9450 }
9451
9452 /* ARM V6 usat (argument parse).  */
9453
9454 static void
9455 do_usat (void)
9456 {
9457   inst.instruction |= inst.operands[0].reg << 12;
9458   inst.instruction |= inst.operands[1].imm << 16;
9459   inst.instruction |= inst.operands[2].reg;
9460
9461   if (inst.operands[3].present)
9462     encode_arm_shift (3);
9463 }
9464
9465 /* ARM V6 ssat16 (argument parse).  */
9466
9467 static void
9468 do_ssat16 (void)
9469 {
9470   inst.instruction |= inst.operands[0].reg << 12;
9471   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9472   inst.instruction |= inst.operands[2].reg;
9473 }
9474
9475 static void
9476 do_usat16 (void)
9477 {
9478   inst.instruction |= inst.operands[0].reg << 12;
9479   inst.instruction |= inst.operands[1].imm << 16;
9480   inst.instruction |= inst.operands[2].reg;
9481 }
9482
9483 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9484    preserving the other bits.
9485
9486    setend <endian_specifier>, where <endian_specifier> is either
9487    BE or LE.  */
9488
9489 static void
9490 do_setend (void)
9491 {
9492   if (warn_on_deprecated
9493       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9494       as_tsktsk (_("setend use is deprecated for ARMv8"));
9495
9496   if (inst.operands[0].imm)
9497     inst.instruction |= 0x200;
9498 }
9499
9500 static void
9501 do_shift (void)
9502 {
9503   unsigned int Rm = (inst.operands[1].present
9504                      ? inst.operands[1].reg
9505                      : inst.operands[0].reg);
9506
9507   inst.instruction |= inst.operands[0].reg << 12;
9508   inst.instruction |= Rm;
9509   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9510     {
9511       inst.instruction |= inst.operands[2].reg << 8;
9512       inst.instruction |= SHIFT_BY_REG;
9513       /* PR 12854: Error on extraneous shifts.  */
9514       constraint (inst.operands[2].shifted,
9515                   _("extraneous shift as part of operand to shift insn"));
9516     }
9517   else
9518     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9519 }
9520
9521 static void
9522 do_smc (void)
9523 {
9524   inst.reloc.type = BFD_RELOC_ARM_SMC;
9525   inst.reloc.pc_rel = 0;
9526 }
9527
9528 static void
9529 do_hvc (void)
9530 {
9531   inst.reloc.type = BFD_RELOC_ARM_HVC;
9532   inst.reloc.pc_rel = 0;
9533 }
9534
9535 static void
9536 do_swi (void)
9537 {
9538   inst.reloc.type = BFD_RELOC_ARM_SWI;
9539   inst.reloc.pc_rel = 0;
9540 }
9541
9542 static void
9543 do_setpan (void)
9544 {
9545   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9546               _("selected processor does not support SETPAN instruction"));
9547
9548   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9549 }
9550
9551 static void
9552 do_t_setpan (void)
9553 {
9554   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9555               _("selected processor does not support SETPAN instruction"));
9556
9557   inst.instruction |= (inst.operands[0].imm << 3);
9558 }
9559
9560 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9561    SMLAxy{cond} Rd,Rm,Rs,Rn
9562    SMLAWy{cond} Rd,Rm,Rs,Rn
9563    Error if any register is R15.  */
9564
9565 static void
9566 do_smla (void)
9567 {
9568   inst.instruction |= inst.operands[0].reg << 16;
9569   inst.instruction |= inst.operands[1].reg;
9570   inst.instruction |= inst.operands[2].reg << 8;
9571   inst.instruction |= inst.operands[3].reg << 12;
9572 }
9573
9574 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9575    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9576    Error if any register is R15.
9577    Warning if Rdlo == Rdhi.  */
9578
9579 static void
9580 do_smlal (void)
9581 {
9582   inst.instruction |= inst.operands[0].reg << 12;
9583   inst.instruction |= inst.operands[1].reg << 16;
9584   inst.instruction |= inst.operands[2].reg;
9585   inst.instruction |= inst.operands[3].reg << 8;
9586
9587   if (inst.operands[0].reg == inst.operands[1].reg)
9588     as_tsktsk (_("rdhi and rdlo must be different"));
9589 }
9590
9591 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9592    SMULxy{cond} Rd,Rm,Rs
9593    Error if any register is R15.  */
9594
9595 static void
9596 do_smul (void)
9597 {
9598   inst.instruction |= inst.operands[0].reg << 16;
9599   inst.instruction |= inst.operands[1].reg;
9600   inst.instruction |= inst.operands[2].reg << 8;
9601 }
9602
9603 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9604    the same for both ARM and Thumb-2.  */
9605
9606 static void
9607 do_srs (void)
9608 {
9609   int reg;
9610
9611   if (inst.operands[0].present)
9612     {
9613       reg = inst.operands[0].reg;
9614       constraint (reg != REG_SP, _("SRS base register must be r13"));
9615     }
9616   else
9617     reg = REG_SP;
9618
9619   inst.instruction |= reg << 16;
9620   inst.instruction |= inst.operands[1].imm;
9621   if (inst.operands[0].writeback || inst.operands[1].writeback)
9622     inst.instruction |= WRITE_BACK;
9623 }
9624
9625 /* ARM V6 strex (argument parse).  */
9626
9627 static void
9628 do_strex (void)
9629 {
9630   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9631               || inst.operands[2].postind || inst.operands[2].writeback
9632               || inst.operands[2].immisreg || inst.operands[2].shifted
9633               || inst.operands[2].negative
9634               /* See comment in do_ldrex().  */
9635               || (inst.operands[2].reg == REG_PC),
9636               BAD_ADDR_MODE);
9637
9638   constraint (inst.operands[0].reg == inst.operands[1].reg
9639               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9640
9641   constraint (inst.reloc.exp.X_op != O_constant
9642               || inst.reloc.exp.X_add_number != 0,
9643               _("offset must be zero in ARM encoding"));
9644
9645   inst.instruction |= inst.operands[0].reg << 12;
9646   inst.instruction |= inst.operands[1].reg;
9647   inst.instruction |= inst.operands[2].reg << 16;
9648   inst.reloc.type = BFD_RELOC_UNUSED;
9649 }
9650
9651 static void
9652 do_t_strexbh (void)
9653 {
9654   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9655               || inst.operands[2].postind || inst.operands[2].writeback
9656               || inst.operands[2].immisreg || inst.operands[2].shifted
9657               || inst.operands[2].negative,
9658               BAD_ADDR_MODE);
9659
9660   constraint (inst.operands[0].reg == inst.operands[1].reg
9661               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9662
9663   do_rm_rd_rn ();
9664 }
9665
9666 static void
9667 do_strexd (void)
9668 {
9669   constraint (inst.operands[1].reg % 2 != 0,
9670               _("even register required"));
9671   constraint (inst.operands[2].present
9672               && inst.operands[2].reg != inst.operands[1].reg + 1,
9673               _("can only store two consecutive registers"));
9674   /* If op 2 were present and equal to PC, this function wouldn't
9675      have been called in the first place.  */
9676   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9677
9678   constraint (inst.operands[0].reg == inst.operands[1].reg
9679               || inst.operands[0].reg == inst.operands[1].reg + 1
9680               || inst.operands[0].reg == inst.operands[3].reg,
9681               BAD_OVERLAP);
9682
9683   inst.instruction |= inst.operands[0].reg << 12;
9684   inst.instruction |= inst.operands[1].reg;
9685   inst.instruction |= inst.operands[3].reg << 16;
9686 }
9687
9688 /* ARM V8 STRL.  */
9689 static void
9690 do_stlex (void)
9691 {
9692   constraint (inst.operands[0].reg == inst.operands[1].reg
9693               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9694
9695   do_rd_rm_rn ();
9696 }
9697
9698 static void
9699 do_t_stlex (void)
9700 {
9701   constraint (inst.operands[0].reg == inst.operands[1].reg
9702               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9703
9704   do_rm_rd_rn ();
9705 }
9706
9707 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9708    extends it to 32-bits, and adds the result to a value in another
9709    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9710    before extracting the 16-bit value.
9711    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9712    Condition defaults to COND_ALWAYS.
9713    Error if any register uses R15.  */
9714
9715 static void
9716 do_sxtah (void)
9717 {
9718   inst.instruction |= inst.operands[0].reg << 12;
9719   inst.instruction |= inst.operands[1].reg << 16;
9720   inst.instruction |= inst.operands[2].reg;
9721   inst.instruction |= inst.operands[3].imm << 10;
9722 }
9723
9724 /* ARM V6 SXTH.
9725
9726    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9727    Condition defaults to COND_ALWAYS.
9728    Error if any register uses R15.  */
9729
9730 static void
9731 do_sxth (void)
9732 {
9733   inst.instruction |= inst.operands[0].reg << 12;
9734   inst.instruction |= inst.operands[1].reg;
9735   inst.instruction |= inst.operands[2].imm << 10;
9736 }
9737 \f
9738 /* VFP instructions.  In a logical order: SP variant first, monad
9739    before dyad, arithmetic then move then load/store.  */
9740
9741 static void
9742 do_vfp_sp_monadic (void)
9743 {
9744   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9745   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9746 }
9747
9748 static void
9749 do_vfp_sp_dyadic (void)
9750 {
9751   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9752   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9753   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9754 }
9755
9756 static void
9757 do_vfp_sp_compare_z (void)
9758 {
9759   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9760 }
9761
9762 static void
9763 do_vfp_dp_sp_cvt (void)
9764 {
9765   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9766   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9767 }
9768
9769 static void
9770 do_vfp_sp_dp_cvt (void)
9771 {
9772   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9773   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9774 }
9775
9776 static void
9777 do_vfp_reg_from_sp (void)
9778 {
9779   inst.instruction |= inst.operands[0].reg << 12;
9780   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9781 }
9782
9783 static void
9784 do_vfp_reg2_from_sp2 (void)
9785 {
9786   constraint (inst.operands[2].imm != 2,
9787               _("only two consecutive VFP SP registers allowed here"));
9788   inst.instruction |= inst.operands[0].reg << 12;
9789   inst.instruction |= inst.operands[1].reg << 16;
9790   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9791 }
9792
9793 static void
9794 do_vfp_sp_from_reg (void)
9795 {
9796   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9797   inst.instruction |= inst.operands[1].reg << 12;
9798 }
9799
9800 static void
9801 do_vfp_sp2_from_reg2 (void)
9802 {
9803   constraint (inst.operands[0].imm != 2,
9804               _("only two consecutive VFP SP registers allowed here"));
9805   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9806   inst.instruction |= inst.operands[1].reg << 12;
9807   inst.instruction |= inst.operands[2].reg << 16;
9808 }
9809
9810 static void
9811 do_vfp_sp_ldst (void)
9812 {
9813   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9814   encode_arm_cp_address (1, FALSE, TRUE, 0);
9815 }
9816
9817 static void
9818 do_vfp_dp_ldst (void)
9819 {
9820   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9821   encode_arm_cp_address (1, FALSE, TRUE, 0);
9822 }
9823
9824
9825 static void
9826 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9827 {
9828   if (inst.operands[0].writeback)
9829     inst.instruction |= WRITE_BACK;
9830   else
9831     constraint (ldstm_type != VFP_LDSTMIA,
9832                 _("this addressing mode requires base-register writeback"));
9833   inst.instruction |= inst.operands[0].reg << 16;
9834   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9835   inst.instruction |= inst.operands[1].imm;
9836 }
9837
9838 static void
9839 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9840 {
9841   int count;
9842
9843   if (inst.operands[0].writeback)
9844     inst.instruction |= WRITE_BACK;
9845   else
9846     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9847                 _("this addressing mode requires base-register writeback"));
9848
9849   inst.instruction |= inst.operands[0].reg << 16;
9850   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9851
9852   count = inst.operands[1].imm << 1;
9853   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9854     count += 1;
9855
9856   inst.instruction |= count;
9857 }
9858
9859 static void
9860 do_vfp_sp_ldstmia (void)
9861 {
9862   vfp_sp_ldstm (VFP_LDSTMIA);
9863 }
9864
9865 static void
9866 do_vfp_sp_ldstmdb (void)
9867 {
9868   vfp_sp_ldstm (VFP_LDSTMDB);
9869 }
9870
9871 static void
9872 do_vfp_dp_ldstmia (void)
9873 {
9874   vfp_dp_ldstm (VFP_LDSTMIA);
9875 }
9876
9877 static void
9878 do_vfp_dp_ldstmdb (void)
9879 {
9880   vfp_dp_ldstm (VFP_LDSTMDB);
9881 }
9882
9883 static void
9884 do_vfp_xp_ldstmia (void)
9885 {
9886   vfp_dp_ldstm (VFP_LDSTMIAX);
9887 }
9888
9889 static void
9890 do_vfp_xp_ldstmdb (void)
9891 {
9892   vfp_dp_ldstm (VFP_LDSTMDBX);
9893 }
9894
9895 static void
9896 do_vfp_dp_rd_rm (void)
9897 {
9898   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9899   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9900 }
9901
9902 static void
9903 do_vfp_dp_rn_rd (void)
9904 {
9905   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9906   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9907 }
9908
9909 static void
9910 do_vfp_dp_rd_rn (void)
9911 {
9912   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9913   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9914 }
9915
9916 static void
9917 do_vfp_dp_rd_rn_rm (void)
9918 {
9919   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9920   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9921   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9922 }
9923
9924 static void
9925 do_vfp_dp_rd (void)
9926 {
9927   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9928 }
9929
9930 static void
9931 do_vfp_dp_rm_rd_rn (void)
9932 {
9933   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9934   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9935   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9936 }
9937
9938 /* VFPv3 instructions.  */
9939 static void
9940 do_vfp_sp_const (void)
9941 {
9942   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9943   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9944   inst.instruction |= (inst.operands[1].imm & 0x0f);
9945 }
9946
9947 static void
9948 do_vfp_dp_const (void)
9949 {
9950   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9951   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9952   inst.instruction |= (inst.operands[1].imm & 0x0f);
9953 }
9954
9955 static void
9956 vfp_conv (int srcsize)
9957 {
9958   int immbits = srcsize - inst.operands[1].imm;
9959
9960   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9961     {
9962       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9963          i.e. immbits must be in range 0 - 16.  */
9964       inst.error = _("immediate value out of range, expected range [0, 16]");
9965       return;
9966     }
9967   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9968     {
9969       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9970          i.e. immbits must be in range 0 - 31.  */
9971       inst.error = _("immediate value out of range, expected range [1, 32]");
9972       return;
9973     }
9974
9975   inst.instruction |= (immbits & 1) << 5;
9976   inst.instruction |= (immbits >> 1);
9977 }
9978
9979 static void
9980 do_vfp_sp_conv_16 (void)
9981 {
9982   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9983   vfp_conv (16);
9984 }
9985
9986 static void
9987 do_vfp_dp_conv_16 (void)
9988 {
9989   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9990   vfp_conv (16);
9991 }
9992
9993 static void
9994 do_vfp_sp_conv_32 (void)
9995 {
9996   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9997   vfp_conv (32);
9998 }
9999
10000 static void
10001 do_vfp_dp_conv_32 (void)
10002 {
10003   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
10004   vfp_conv (32);
10005 }
10006 \f
10007 /* FPA instructions.  Also in a logical order.  */
10008
10009 static void
10010 do_fpa_cmp (void)
10011 {
10012   inst.instruction |= inst.operands[0].reg << 16;
10013   inst.instruction |= inst.operands[1].reg;
10014 }
10015
10016 static void
10017 do_fpa_ldmstm (void)
10018 {
10019   inst.instruction |= inst.operands[0].reg << 12;
10020   switch (inst.operands[1].imm)
10021     {
10022     case 1: inst.instruction |= CP_T_X;          break;
10023     case 2: inst.instruction |= CP_T_Y;          break;
10024     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
10025     case 4:                                      break;
10026     default: abort ();
10027     }
10028
10029   if (inst.instruction & (PRE_INDEX | INDEX_UP))
10030     {
10031       /* The instruction specified "ea" or "fd", so we can only accept
10032          [Rn]{!}.  The instruction does not really support stacking or
10033          unstacking, so we have to emulate these by setting appropriate
10034          bits and offsets.  */
10035       constraint (inst.reloc.exp.X_op != O_constant
10036                   || inst.reloc.exp.X_add_number != 0,
10037                   _("this instruction does not support indexing"));
10038
10039       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
10040         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
10041
10042       if (!(inst.instruction & INDEX_UP))
10043         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
10044
10045       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
10046         {
10047           inst.operands[2].preind = 0;
10048           inst.operands[2].postind = 1;
10049         }
10050     }
10051
10052   encode_arm_cp_address (2, TRUE, TRUE, 0);
10053 }
10054 \f
10055 /* iWMMXt instructions: strictly in alphabetical order.  */
10056
10057 static void
10058 do_iwmmxt_tandorc (void)
10059 {
10060   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
10061 }
10062
10063 static void
10064 do_iwmmxt_textrc (void)
10065 {
10066   inst.instruction |= inst.operands[0].reg << 12;
10067   inst.instruction |= inst.operands[1].imm;
10068 }
10069
10070 static void
10071 do_iwmmxt_textrm (void)
10072 {
10073   inst.instruction |= inst.operands[0].reg << 12;
10074   inst.instruction |= inst.operands[1].reg << 16;
10075   inst.instruction |= inst.operands[2].imm;
10076 }
10077
10078 static void
10079 do_iwmmxt_tinsr (void)
10080 {
10081   inst.instruction |= inst.operands[0].reg << 16;
10082   inst.instruction |= inst.operands[1].reg << 12;
10083   inst.instruction |= inst.operands[2].imm;
10084 }
10085
10086 static void
10087 do_iwmmxt_tmia (void)
10088 {
10089   inst.instruction |= inst.operands[0].reg << 5;
10090   inst.instruction |= inst.operands[1].reg;
10091   inst.instruction |= inst.operands[2].reg << 12;
10092 }
10093
10094 static void
10095 do_iwmmxt_waligni (void)
10096 {
10097   inst.instruction |= inst.operands[0].reg << 12;
10098   inst.instruction |= inst.operands[1].reg << 16;
10099   inst.instruction |= inst.operands[2].reg;
10100   inst.instruction |= inst.operands[3].imm << 20;
10101 }
10102
10103 static void
10104 do_iwmmxt_wmerge (void)
10105 {
10106   inst.instruction |= inst.operands[0].reg << 12;
10107   inst.instruction |= inst.operands[1].reg << 16;
10108   inst.instruction |= inst.operands[2].reg;
10109   inst.instruction |= inst.operands[3].imm << 21;
10110 }
10111
10112 static void
10113 do_iwmmxt_wmov (void)
10114 {
10115   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10116   inst.instruction |= inst.operands[0].reg << 12;
10117   inst.instruction |= inst.operands[1].reg << 16;
10118   inst.instruction |= inst.operands[1].reg;
10119 }
10120
10121 static void
10122 do_iwmmxt_wldstbh (void)
10123 {
10124   int reloc;
10125   inst.instruction |= inst.operands[0].reg << 12;
10126   if (thumb_mode)
10127     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10128   else
10129     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10130   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10131 }
10132
10133 static void
10134 do_iwmmxt_wldstw (void)
10135 {
10136   /* RIWR_RIWC clears .isreg for a control register.  */
10137   if (!inst.operands[0].isreg)
10138     {
10139       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10140       inst.instruction |= 0xf0000000;
10141     }
10142
10143   inst.instruction |= inst.operands[0].reg << 12;
10144   encode_arm_cp_address (1, TRUE, TRUE, 0);
10145 }
10146
10147 static void
10148 do_iwmmxt_wldstd (void)
10149 {
10150   inst.instruction |= inst.operands[0].reg << 12;
10151   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10152       && inst.operands[1].immisreg)
10153     {
10154       inst.instruction &= ~0x1a000ff;
10155       inst.instruction |= (0xfU << 28);
10156       if (inst.operands[1].preind)
10157         inst.instruction |= PRE_INDEX;
10158       if (!inst.operands[1].negative)
10159         inst.instruction |= INDEX_UP;
10160       if (inst.operands[1].writeback)
10161         inst.instruction |= WRITE_BACK;
10162       inst.instruction |= inst.operands[1].reg << 16;
10163       inst.instruction |= inst.reloc.exp.X_add_number << 4;
10164       inst.instruction |= inst.operands[1].imm;
10165     }
10166   else
10167     encode_arm_cp_address (1, TRUE, FALSE, 0);
10168 }
10169
10170 static void
10171 do_iwmmxt_wshufh (void)
10172 {
10173   inst.instruction |= inst.operands[0].reg << 12;
10174   inst.instruction |= inst.operands[1].reg << 16;
10175   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10176   inst.instruction |= (inst.operands[2].imm & 0x0f);
10177 }
10178
10179 static void
10180 do_iwmmxt_wzero (void)
10181 {
10182   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10183   inst.instruction |= inst.operands[0].reg;
10184   inst.instruction |= inst.operands[0].reg << 12;
10185   inst.instruction |= inst.operands[0].reg << 16;
10186 }
10187
10188 static void
10189 do_iwmmxt_wrwrwr_or_imm5 (void)
10190 {
10191   if (inst.operands[2].isreg)
10192     do_rd_rn_rm ();
10193   else {
10194     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10195                 _("immediate operand requires iWMMXt2"));
10196     do_rd_rn ();
10197     if (inst.operands[2].imm == 0)
10198       {
10199         switch ((inst.instruction >> 20) & 0xf)
10200           {
10201           case 4:
10202           case 5:
10203           case 6:
10204           case 7:
10205             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10206             inst.operands[2].imm = 16;
10207             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10208             break;
10209           case 8:
10210           case 9:
10211           case 10:
10212           case 11:
10213             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10214             inst.operands[2].imm = 32;
10215             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10216             break;
10217           case 12:
10218           case 13:
10219           case 14:
10220           case 15:
10221             {
10222               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10223               unsigned long wrn;
10224               wrn = (inst.instruction >> 16) & 0xf;
10225               inst.instruction &= 0xff0fff0f;
10226               inst.instruction |= wrn;
10227               /* Bail out here; the instruction is now assembled.  */
10228               return;
10229             }
10230           }
10231       }
10232     /* Map 32 -> 0, etc.  */
10233     inst.operands[2].imm &= 0x1f;
10234     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10235   }
10236 }
10237 \f
10238 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10239    operations first, then control, shift, and load/store.  */
10240
10241 /* Insns like "foo X,Y,Z".  */
10242
10243 static void
10244 do_mav_triple (void)
10245 {
10246   inst.instruction |= inst.operands[0].reg << 16;
10247   inst.instruction |= inst.operands[1].reg;
10248   inst.instruction |= inst.operands[2].reg << 12;
10249 }
10250
10251 /* Insns like "foo W,X,Y,Z".
10252     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10253
10254 static void
10255 do_mav_quad (void)
10256 {
10257   inst.instruction |= inst.operands[0].reg << 5;
10258   inst.instruction |= inst.operands[1].reg << 12;
10259   inst.instruction |= inst.operands[2].reg << 16;
10260   inst.instruction |= inst.operands[3].reg;
10261 }
10262
10263 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10264 static void
10265 do_mav_dspsc (void)
10266 {
10267   inst.instruction |= inst.operands[1].reg << 12;
10268 }
10269
10270 /* Maverick shift immediate instructions.
10271    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10272    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10273
10274 static void
10275 do_mav_shift (void)
10276 {
10277   int imm = inst.operands[2].imm;
10278
10279   inst.instruction |= inst.operands[0].reg << 12;
10280   inst.instruction |= inst.operands[1].reg << 16;
10281
10282   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10283      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10284      Bit 4 should be 0.  */
10285   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10286
10287   inst.instruction |= imm;
10288 }
10289 \f
10290 /* XScale instructions.  Also sorted arithmetic before move.  */
10291
10292 /* Xscale multiply-accumulate (argument parse)
10293      MIAcc   acc0,Rm,Rs
10294      MIAPHcc acc0,Rm,Rs
10295      MIAxycc acc0,Rm,Rs.  */
10296
10297 static void
10298 do_xsc_mia (void)
10299 {
10300   inst.instruction |= inst.operands[1].reg;
10301   inst.instruction |= inst.operands[2].reg << 12;
10302 }
10303
10304 /* Xscale move-accumulator-register (argument parse)
10305
10306      MARcc   acc0,RdLo,RdHi.  */
10307
10308 static void
10309 do_xsc_mar (void)
10310 {
10311   inst.instruction |= inst.operands[1].reg << 12;
10312   inst.instruction |= inst.operands[2].reg << 16;
10313 }
10314
10315 /* Xscale move-register-accumulator (argument parse)
10316
10317      MRAcc   RdLo,RdHi,acc0.  */
10318
10319 static void
10320 do_xsc_mra (void)
10321 {
10322   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10323   inst.instruction |= inst.operands[0].reg << 12;
10324   inst.instruction |= inst.operands[1].reg << 16;
10325 }
10326 \f
10327 /* Encoding functions relevant only to Thumb.  */
10328
10329 /* inst.operands[i] is a shifted-register operand; encode
10330    it into inst.instruction in the format used by Thumb32.  */
10331
10332 static void
10333 encode_thumb32_shifted_operand (int i)
10334 {
10335   unsigned int value = inst.reloc.exp.X_add_number;
10336   unsigned int shift = inst.operands[i].shift_kind;
10337
10338   constraint (inst.operands[i].immisreg,
10339               _("shift by register not allowed in thumb mode"));
10340   inst.instruction |= inst.operands[i].reg;
10341   if (shift == SHIFT_RRX)
10342     inst.instruction |= SHIFT_ROR << 4;
10343   else
10344     {
10345       constraint (inst.reloc.exp.X_op != O_constant,
10346                   _("expression too complex"));
10347
10348       constraint (value > 32
10349                   || (value == 32 && (shift == SHIFT_LSL
10350                                       || shift == SHIFT_ROR)),
10351                   _("shift expression is too large"));
10352
10353       if (value == 0)
10354         shift = SHIFT_LSL;
10355       else if (value == 32)
10356         value = 0;
10357
10358       inst.instruction |= shift << 4;
10359       inst.instruction |= (value & 0x1c) << 10;
10360       inst.instruction |= (value & 0x03) << 6;
10361     }
10362 }
10363
10364
10365 /* inst.operands[i] was set up by parse_address.  Encode it into a
10366    Thumb32 format load or store instruction.  Reject forms that cannot
10367    be used with such instructions.  If is_t is true, reject forms that
10368    cannot be used with a T instruction; if is_d is true, reject forms
10369    that cannot be used with a D instruction.  If it is a store insn,
10370    reject PC in Rn.  */
10371
10372 static void
10373 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10374 {
10375   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10376
10377   constraint (!inst.operands[i].isreg,
10378               _("Instruction does not support =N addresses"));
10379
10380   inst.instruction |= inst.operands[i].reg << 16;
10381   if (inst.operands[i].immisreg)
10382     {
10383       constraint (is_pc, BAD_PC_ADDRESSING);
10384       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10385       constraint (inst.operands[i].negative,
10386                   _("Thumb does not support negative register indexing"));
10387       constraint (inst.operands[i].postind,
10388                   _("Thumb does not support register post-indexing"));
10389       constraint (inst.operands[i].writeback,
10390                   _("Thumb does not support register indexing with writeback"));
10391       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10392                   _("Thumb supports only LSL in shifted register indexing"));
10393
10394       inst.instruction |= inst.operands[i].imm;
10395       if (inst.operands[i].shifted)
10396         {
10397           constraint (inst.reloc.exp.X_op != O_constant,
10398                       _("expression too complex"));
10399           constraint (inst.reloc.exp.X_add_number < 0
10400                       || inst.reloc.exp.X_add_number > 3,
10401                       _("shift out of range"));
10402           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10403         }
10404       inst.reloc.type = BFD_RELOC_UNUSED;
10405     }
10406   else if (inst.operands[i].preind)
10407     {
10408       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10409       constraint (is_t && inst.operands[i].writeback,
10410                   _("cannot use writeback with this instruction"));
10411       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10412                   BAD_PC_ADDRESSING);
10413
10414       if (is_d)
10415         {
10416           inst.instruction |= 0x01000000;
10417           if (inst.operands[i].writeback)
10418             inst.instruction |= 0x00200000;
10419         }
10420       else
10421         {
10422           inst.instruction |= 0x00000c00;
10423           if (inst.operands[i].writeback)
10424             inst.instruction |= 0x00000100;
10425         }
10426       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10427     }
10428   else if (inst.operands[i].postind)
10429     {
10430       gas_assert (inst.operands[i].writeback);
10431       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10432       constraint (is_t, _("cannot use post-indexing with this instruction"));
10433
10434       if (is_d)
10435         inst.instruction |= 0x00200000;
10436       else
10437         inst.instruction |= 0x00000900;
10438       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10439     }
10440   else /* unindexed - only for coprocessor */
10441     inst.error = _("instruction does not accept unindexed addressing");
10442 }
10443
10444 /* Table of Thumb instructions which exist in both 16- and 32-bit
10445    encodings (the latter only in post-V6T2 cores).  The index is the
10446    value used in the insns table below.  When there is more than one
10447    possible 16-bit encoding for the instruction, this table always
10448    holds variant (1).
10449    Also contains several pseudo-instructions used during relaxation.  */
10450 #define T16_32_TAB                              \
10451   X(_adc,   4140, eb400000),                    \
10452   X(_adcs,  4140, eb500000),                    \
10453   X(_add,   1c00, eb000000),                    \
10454   X(_adds,  1c00, eb100000),                    \
10455   X(_addi,  0000, f1000000),                    \
10456   X(_addis, 0000, f1100000),                    \
10457   X(_add_pc,000f, f20f0000),                    \
10458   X(_add_sp,000d, f10d0000),                    \
10459   X(_adr,   000f, f20f0000),                    \
10460   X(_and,   4000, ea000000),                    \
10461   X(_ands,  4000, ea100000),                    \
10462   X(_asr,   1000, fa40f000),                    \
10463   X(_asrs,  1000, fa50f000),                    \
10464   X(_b,     e000, f000b000),                    \
10465   X(_bcond, d000, f0008000),                    \
10466   X(_bic,   4380, ea200000),                    \
10467   X(_bics,  4380, ea300000),                    \
10468   X(_cmn,   42c0, eb100f00),                    \
10469   X(_cmp,   2800, ebb00f00),                    \
10470   X(_cpsie, b660, f3af8400),                    \
10471   X(_cpsid, b670, f3af8600),                    \
10472   X(_cpy,   4600, ea4f0000),                    \
10473   X(_dec_sp,80dd, f1ad0d00),                    \
10474   X(_eor,   4040, ea800000),                    \
10475   X(_eors,  4040, ea900000),                    \
10476   X(_inc_sp,00dd, f10d0d00),                    \
10477   X(_ldmia, c800, e8900000),                    \
10478   X(_ldr,   6800, f8500000),                    \
10479   X(_ldrb,  7800, f8100000),                    \
10480   X(_ldrh,  8800, f8300000),                    \
10481   X(_ldrsb, 5600, f9100000),                    \
10482   X(_ldrsh, 5e00, f9300000),                    \
10483   X(_ldr_pc,4800, f85f0000),                    \
10484   X(_ldr_pc2,4800, f85f0000),                   \
10485   X(_ldr_sp,9800, f85d0000),                    \
10486   X(_lsl,   0000, fa00f000),                    \
10487   X(_lsls,  0000, fa10f000),                    \
10488   X(_lsr,   0800, fa20f000),                    \
10489   X(_lsrs,  0800, fa30f000),                    \
10490   X(_mov,   2000, ea4f0000),                    \
10491   X(_movs,  2000, ea5f0000),                    \
10492   X(_mul,   4340, fb00f000),                     \
10493   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10494   X(_mvn,   43c0, ea6f0000),                    \
10495   X(_mvns,  43c0, ea7f0000),                    \
10496   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10497   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10498   X(_orr,   4300, ea400000),                    \
10499   X(_orrs,  4300, ea500000),                    \
10500   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10501   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10502   X(_rev,   ba00, fa90f080),                    \
10503   X(_rev16, ba40, fa90f090),                    \
10504   X(_revsh, bac0, fa90f0b0),                    \
10505   X(_ror,   41c0, fa60f000),                    \
10506   X(_rors,  41c0, fa70f000),                    \
10507   X(_sbc,   4180, eb600000),                    \
10508   X(_sbcs,  4180, eb700000),                    \
10509   X(_stmia, c000, e8800000),                    \
10510   X(_str,   6000, f8400000),                    \
10511   X(_strb,  7000, f8000000),                    \
10512   X(_strh,  8000, f8200000),                    \
10513   X(_str_sp,9000, f84d0000),                    \
10514   X(_sub,   1e00, eba00000),                    \
10515   X(_subs,  1e00, ebb00000),                    \
10516   X(_subi,  8000, f1a00000),                    \
10517   X(_subis, 8000, f1b00000),                    \
10518   X(_sxtb,  b240, fa4ff080),                    \
10519   X(_sxth,  b200, fa0ff080),                    \
10520   X(_tst,   4200, ea100f00),                    \
10521   X(_uxtb,  b2c0, fa5ff080),                    \
10522   X(_uxth,  b280, fa1ff080),                    \
10523   X(_nop,   bf00, f3af8000),                    \
10524   X(_yield, bf10, f3af8001),                    \
10525   X(_wfe,   bf20, f3af8002),                    \
10526   X(_wfi,   bf30, f3af8003),                    \
10527   X(_sev,   bf40, f3af8004),                    \
10528   X(_sevl,  bf50, f3af8005),                    \
10529   X(_udf,   de00, f7f0a000)
10530
10531 /* To catch errors in encoding functions, the codes are all offset by
10532    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10533    as 16-bit instructions.  */
10534 #define X(a,b,c) T_MNEM##a
10535 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10536 #undef X
10537
10538 #define X(a,b,c) 0x##b
10539 static const unsigned short thumb_op16[] = { T16_32_TAB };
10540 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10541 #undef X
10542
10543 #define X(a,b,c) 0x##c
10544 static const unsigned int thumb_op32[] = { T16_32_TAB };
10545 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10546 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10547 #undef X
10548 #undef T16_32_TAB
10549
10550 /* Thumb instruction encoders, in alphabetical order.  */
10551
10552 /* ADDW or SUBW.  */
10553
10554 static void
10555 do_t_add_sub_w (void)
10556 {
10557   int Rd, Rn;
10558
10559   Rd = inst.operands[0].reg;
10560   Rn = inst.operands[1].reg;
10561
10562   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10563      is the SP-{plus,minus}-immediate form of the instruction.  */
10564   if (Rn == REG_SP)
10565     constraint (Rd == REG_PC, BAD_PC);
10566   else
10567     reject_bad_reg (Rd);
10568
10569   inst.instruction |= (Rn << 16) | (Rd << 8);
10570   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10571 }
10572
10573 /* Parse an add or subtract instruction.  We get here with inst.instruction
10574    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10575
10576 static void
10577 do_t_add_sub (void)
10578 {
10579   int Rd, Rs, Rn;
10580
10581   Rd = inst.operands[0].reg;
10582   Rs = (inst.operands[1].present
10583         ? inst.operands[1].reg    /* Rd, Rs, foo */
10584         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10585
10586   if (Rd == REG_PC)
10587     set_it_insn_type_last ();
10588
10589   if (unified_syntax)
10590     {
10591       bfd_boolean flags;
10592       bfd_boolean narrow;
10593       int opcode;
10594
10595       flags = (inst.instruction == T_MNEM_adds
10596                || inst.instruction == T_MNEM_subs);
10597       if (flags)
10598         narrow = !in_it_block ();
10599       else
10600         narrow = in_it_block ();
10601       if (!inst.operands[2].isreg)
10602         {
10603           int add;
10604
10605           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10606             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10607
10608           add = (inst.instruction == T_MNEM_add
10609                  || inst.instruction == T_MNEM_adds);
10610           opcode = 0;
10611           if (inst.size_req != 4)
10612             {
10613               /* Attempt to use a narrow opcode, with relaxation if
10614                  appropriate.  */
10615               if (Rd == REG_SP && Rs == REG_SP && !flags)
10616                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10617               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10618                 opcode = T_MNEM_add_sp;
10619               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10620                 opcode = T_MNEM_add_pc;
10621               else if (Rd <= 7 && Rs <= 7 && narrow)
10622                 {
10623                   if (flags)
10624                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10625                   else
10626                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10627                 }
10628               if (opcode)
10629                 {
10630                   inst.instruction = THUMB_OP16(opcode);
10631                   inst.instruction |= (Rd << 4) | Rs;
10632                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10633                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
10634                   {
10635                     if (inst.size_req == 2)
10636                       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10637                     else
10638                       inst.relax = opcode;
10639                   }
10640                 }
10641               else
10642                 constraint (inst.size_req == 2, BAD_HIREG);
10643             }
10644           if (inst.size_req == 4
10645               || (inst.size_req != 2 && !opcode))
10646             {
10647               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10648                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
10649                           THUMB1_RELOC_ONLY);
10650               if (Rd == REG_PC)
10651                 {
10652                   constraint (add, BAD_PC);
10653                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10654                              _("only SUBS PC, LR, #const allowed"));
10655                   constraint (inst.reloc.exp.X_op != O_constant,
10656                               _("expression too complex"));
10657                   constraint (inst.reloc.exp.X_add_number < 0
10658                               || inst.reloc.exp.X_add_number > 0xff,
10659                              _("immediate value out of range"));
10660                   inst.instruction = T2_SUBS_PC_LR
10661                                      | inst.reloc.exp.X_add_number;
10662                   inst.reloc.type = BFD_RELOC_UNUSED;
10663                   return;
10664                 }
10665               else if (Rs == REG_PC)
10666                 {
10667                   /* Always use addw/subw.  */
10668                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10669                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10670                 }
10671               else
10672                 {
10673                   inst.instruction = THUMB_OP32 (inst.instruction);
10674                   inst.instruction = (inst.instruction & 0xe1ffffff)
10675                                      | 0x10000000;
10676                   if (flags)
10677                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10678                   else
10679                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10680                 }
10681               inst.instruction |= Rd << 8;
10682               inst.instruction |= Rs << 16;
10683             }
10684         }
10685       else
10686         {
10687           unsigned int value = inst.reloc.exp.X_add_number;
10688           unsigned int shift = inst.operands[2].shift_kind;
10689
10690           Rn = inst.operands[2].reg;
10691           /* See if we can do this with a 16-bit instruction.  */
10692           if (!inst.operands[2].shifted && inst.size_req != 4)
10693             {
10694               if (Rd > 7 || Rs > 7 || Rn > 7)
10695                 narrow = FALSE;
10696
10697               if (narrow)
10698                 {
10699                   inst.instruction = ((inst.instruction == T_MNEM_adds
10700                                        || inst.instruction == T_MNEM_add)
10701                                       ? T_OPCODE_ADD_R3
10702                                       : T_OPCODE_SUB_R3);
10703                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10704                   return;
10705                 }
10706
10707               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10708                 {
10709                   /* Thumb-1 cores (except v6-M) require at least one high
10710                      register in a narrow non flag setting add.  */
10711                   if (Rd > 7 || Rn > 7
10712                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10713                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10714                     {
10715                       if (Rd == Rn)
10716                         {
10717                           Rn = Rs;
10718                           Rs = Rd;
10719                         }
10720                       inst.instruction = T_OPCODE_ADD_HI;
10721                       inst.instruction |= (Rd & 8) << 4;
10722                       inst.instruction |= (Rd & 7);
10723                       inst.instruction |= Rn << 3;
10724                       return;
10725                     }
10726                 }
10727             }
10728
10729           constraint (Rd == REG_PC, BAD_PC);
10730           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
10731             constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10732           constraint (Rs == REG_PC, BAD_PC);
10733           reject_bad_reg (Rn);
10734
10735           /* If we get here, it can't be done in 16 bits.  */
10736           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10737                       _("shift must be constant"));
10738           inst.instruction = THUMB_OP32 (inst.instruction);
10739           inst.instruction |= Rd << 8;
10740           inst.instruction |= Rs << 16;
10741           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10742                       _("shift value over 3 not allowed in thumb mode"));
10743           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10744                       _("only LSL shift allowed in thumb mode"));
10745           encode_thumb32_shifted_operand (2);
10746         }
10747     }
10748   else
10749     {
10750       constraint (inst.instruction == T_MNEM_adds
10751                   || inst.instruction == T_MNEM_subs,
10752                   BAD_THUMB32);
10753
10754       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10755         {
10756           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10757                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10758                       BAD_HIREG);
10759
10760           inst.instruction = (inst.instruction == T_MNEM_add
10761                               ? 0x0000 : 0x8000);
10762           inst.instruction |= (Rd << 4) | Rs;
10763           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10764           return;
10765         }
10766
10767       Rn = inst.operands[2].reg;
10768       constraint (inst.operands[2].shifted, _("unshifted register required"));
10769
10770       /* We now have Rd, Rs, and Rn set to registers.  */
10771       if (Rd > 7 || Rs > 7 || Rn > 7)
10772         {
10773           /* Can't do this for SUB.      */
10774           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10775           inst.instruction = T_OPCODE_ADD_HI;
10776           inst.instruction |= (Rd & 8) << 4;
10777           inst.instruction |= (Rd & 7);
10778           if (Rs == Rd)
10779             inst.instruction |= Rn << 3;
10780           else if (Rn == Rd)
10781             inst.instruction |= Rs << 3;
10782           else
10783             constraint (1, _("dest must overlap one source register"));
10784         }
10785       else
10786         {
10787           inst.instruction = (inst.instruction == T_MNEM_add
10788                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10789           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10790         }
10791     }
10792 }
10793
10794 static void
10795 do_t_adr (void)
10796 {
10797   unsigned Rd;
10798
10799   Rd = inst.operands[0].reg;
10800   reject_bad_reg (Rd);
10801
10802   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10803     {
10804       /* Defer to section relaxation.  */
10805       inst.relax = inst.instruction;
10806       inst.instruction = THUMB_OP16 (inst.instruction);
10807       inst.instruction |= Rd << 4;
10808     }
10809   else if (unified_syntax && inst.size_req != 2)
10810     {
10811       /* Generate a 32-bit opcode.  */
10812       inst.instruction = THUMB_OP32 (inst.instruction);
10813       inst.instruction |= Rd << 8;
10814       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10815       inst.reloc.pc_rel = 1;
10816     }
10817   else
10818     {
10819       /* Generate a 16-bit opcode.  */
10820       inst.instruction = THUMB_OP16 (inst.instruction);
10821       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10822       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10823       inst.reloc.pc_rel = 1;
10824       inst.instruction |= Rd << 4;
10825     }
10826
10827   if (inst.reloc.exp.X_op == O_symbol
10828       && inst.reloc.exp.X_add_symbol != NULL
10829       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
10830       && THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
10831     inst.reloc.exp.X_add_number += 1;
10832 }
10833
10834 /* Arithmetic instructions for which there is just one 16-bit
10835    instruction encoding, and it allows only two low registers.
10836    For maximal compatibility with ARM syntax, we allow three register
10837    operands even when Thumb-32 instructions are not available, as long
10838    as the first two are identical.  For instance, both "sbc r0,r1" and
10839    "sbc r0,r0,r1" are allowed.  */
10840 static void
10841 do_t_arit3 (void)
10842 {
10843   int Rd, Rs, Rn;
10844
10845   Rd = inst.operands[0].reg;
10846   Rs = (inst.operands[1].present
10847         ? inst.operands[1].reg    /* Rd, Rs, foo */
10848         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10849   Rn = inst.operands[2].reg;
10850
10851   reject_bad_reg (Rd);
10852   reject_bad_reg (Rs);
10853   if (inst.operands[2].isreg)
10854     reject_bad_reg (Rn);
10855
10856   if (unified_syntax)
10857     {
10858       if (!inst.operands[2].isreg)
10859         {
10860           /* For an immediate, we always generate a 32-bit opcode;
10861              section relaxation will shrink it later if possible.  */
10862           inst.instruction = THUMB_OP32 (inst.instruction);
10863           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10864           inst.instruction |= Rd << 8;
10865           inst.instruction |= Rs << 16;
10866           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10867         }
10868       else
10869         {
10870           bfd_boolean narrow;
10871
10872           /* See if we can do this with a 16-bit instruction.  */
10873           if (THUMB_SETS_FLAGS (inst.instruction))
10874             narrow = !in_it_block ();
10875           else
10876             narrow = in_it_block ();
10877
10878           if (Rd > 7 || Rn > 7 || Rs > 7)
10879             narrow = FALSE;
10880           if (inst.operands[2].shifted)
10881             narrow = FALSE;
10882           if (inst.size_req == 4)
10883             narrow = FALSE;
10884
10885           if (narrow
10886               && Rd == Rs)
10887             {
10888               inst.instruction = THUMB_OP16 (inst.instruction);
10889               inst.instruction |= Rd;
10890               inst.instruction |= Rn << 3;
10891               return;
10892             }
10893
10894           /* If we get here, it can't be done in 16 bits.  */
10895           constraint (inst.operands[2].shifted
10896                       && inst.operands[2].immisreg,
10897                       _("shift must be constant"));
10898           inst.instruction = THUMB_OP32 (inst.instruction);
10899           inst.instruction |= Rd << 8;
10900           inst.instruction |= Rs << 16;
10901           encode_thumb32_shifted_operand (2);
10902         }
10903     }
10904   else
10905     {
10906       /* On its face this is a lie - the instruction does set the
10907          flags.  However, the only supported mnemonic in this mode
10908          says it doesn't.  */
10909       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10910
10911       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10912                   _("unshifted register required"));
10913       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10914       constraint (Rd != Rs,
10915                   _("dest and source1 must be the same register"));
10916
10917       inst.instruction = THUMB_OP16 (inst.instruction);
10918       inst.instruction |= Rd;
10919       inst.instruction |= Rn << 3;
10920     }
10921 }
10922
10923 /* Similarly, but for instructions where the arithmetic operation is
10924    commutative, so we can allow either of them to be different from
10925    the destination operand in a 16-bit instruction.  For instance, all
10926    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10927    accepted.  */
10928 static void
10929 do_t_arit3c (void)
10930 {
10931   int Rd, Rs, Rn;
10932
10933   Rd = inst.operands[0].reg;
10934   Rs = (inst.operands[1].present
10935         ? inst.operands[1].reg    /* Rd, Rs, foo */
10936         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10937   Rn = inst.operands[2].reg;
10938
10939   reject_bad_reg (Rd);
10940   reject_bad_reg (Rs);
10941   if (inst.operands[2].isreg)
10942     reject_bad_reg (Rn);
10943
10944   if (unified_syntax)
10945     {
10946       if (!inst.operands[2].isreg)
10947         {
10948           /* For an immediate, we always generate a 32-bit opcode;
10949              section relaxation will shrink it later if possible.  */
10950           inst.instruction = THUMB_OP32 (inst.instruction);
10951           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10952           inst.instruction |= Rd << 8;
10953           inst.instruction |= Rs << 16;
10954           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10955         }
10956       else
10957         {
10958           bfd_boolean narrow;
10959
10960           /* See if we can do this with a 16-bit instruction.  */
10961           if (THUMB_SETS_FLAGS (inst.instruction))
10962             narrow = !in_it_block ();
10963           else
10964             narrow = in_it_block ();
10965
10966           if (Rd > 7 || Rn > 7 || Rs > 7)
10967             narrow = FALSE;
10968           if (inst.operands[2].shifted)
10969             narrow = FALSE;
10970           if (inst.size_req == 4)
10971             narrow = FALSE;
10972
10973           if (narrow)
10974             {
10975               if (Rd == Rs)
10976                 {
10977                   inst.instruction = THUMB_OP16 (inst.instruction);
10978                   inst.instruction |= Rd;
10979                   inst.instruction |= Rn << 3;
10980                   return;
10981                 }
10982               if (Rd == Rn)
10983                 {
10984                   inst.instruction = THUMB_OP16 (inst.instruction);
10985                   inst.instruction |= Rd;
10986                   inst.instruction |= Rs << 3;
10987                   return;
10988                 }
10989             }
10990
10991           /* If we get here, it can't be done in 16 bits.  */
10992           constraint (inst.operands[2].shifted
10993                       && inst.operands[2].immisreg,
10994                       _("shift must be constant"));
10995           inst.instruction = THUMB_OP32 (inst.instruction);
10996           inst.instruction |= Rd << 8;
10997           inst.instruction |= Rs << 16;
10998           encode_thumb32_shifted_operand (2);
10999         }
11000     }
11001   else
11002     {
11003       /* On its face this is a lie - the instruction does set the
11004          flags.  However, the only supported mnemonic in this mode
11005          says it doesn't.  */
11006       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
11007
11008       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
11009                   _("unshifted register required"));
11010       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
11011
11012       inst.instruction = THUMB_OP16 (inst.instruction);
11013       inst.instruction |= Rd;
11014
11015       if (Rd == Rs)
11016         inst.instruction |= Rn << 3;
11017       else if (Rd == Rn)
11018         inst.instruction |= Rs << 3;
11019       else
11020         constraint (1, _("dest must overlap one source register"));
11021     }
11022 }
11023
11024 static void
11025 do_t_bfc (void)
11026 {
11027   unsigned Rd;
11028   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
11029   constraint (msb > 32, _("bit-field extends past end of register"));
11030   /* The instruction encoding stores the LSB and MSB,
11031      not the LSB and width.  */
11032   Rd = inst.operands[0].reg;
11033   reject_bad_reg (Rd);
11034   inst.instruction |= Rd << 8;
11035   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
11036   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
11037   inst.instruction |= msb - 1;
11038 }
11039
11040 static void
11041 do_t_bfi (void)
11042 {
11043   int Rd, Rn;
11044   unsigned int msb;
11045
11046   Rd = inst.operands[0].reg;
11047   reject_bad_reg (Rd);
11048
11049   /* #0 in second position is alternative syntax for bfc, which is
11050      the same instruction but with REG_PC in the Rm field.  */
11051   if (!inst.operands[1].isreg)
11052     Rn = REG_PC;
11053   else
11054     {
11055       Rn = inst.operands[1].reg;
11056       reject_bad_reg (Rn);
11057     }
11058
11059   msb = inst.operands[2].imm + inst.operands[3].imm;
11060   constraint (msb > 32, _("bit-field extends past end of register"));
11061   /* The instruction encoding stores the LSB and MSB,
11062      not the LSB and width.  */
11063   inst.instruction |= Rd << 8;
11064   inst.instruction |= Rn << 16;
11065   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11066   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11067   inst.instruction |= msb - 1;
11068 }
11069
11070 static void
11071 do_t_bfx (void)
11072 {
11073   unsigned Rd, Rn;
11074
11075   Rd = inst.operands[0].reg;
11076   Rn = inst.operands[1].reg;
11077
11078   reject_bad_reg (Rd);
11079   reject_bad_reg (Rn);
11080
11081   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
11082               _("bit-field extends past end of register"));
11083   inst.instruction |= Rd << 8;
11084   inst.instruction |= Rn << 16;
11085   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
11086   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
11087   inst.instruction |= inst.operands[3].imm - 1;
11088 }
11089
11090 /* ARM V5 Thumb BLX (argument parse)
11091         BLX <target_addr>       which is BLX(1)
11092         BLX <Rm>                which is BLX(2)
11093    Unfortunately, there are two different opcodes for this mnemonic.
11094    So, the insns[].value is not used, and the code here zaps values
11095         into inst.instruction.
11096
11097    ??? How to take advantage of the additional two bits of displacement
11098    available in Thumb32 mode?  Need new relocation?  */
11099
11100 static void
11101 do_t_blx (void)
11102 {
11103   set_it_insn_type_last ();
11104
11105   if (inst.operands[0].isreg)
11106     {
11107       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11108       /* We have a register, so this is BLX(2).  */
11109       inst.instruction |= inst.operands[0].reg << 3;
11110     }
11111   else
11112     {
11113       /* No register.  This must be BLX(1).  */
11114       inst.instruction = 0xf000e800;
11115       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11116     }
11117 }
11118
11119 static void
11120 do_t_branch (void)
11121 {
11122   int opcode;
11123   int cond;
11124   bfd_reloc_code_real_type reloc;
11125
11126   cond = inst.cond;
11127   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
11128
11129   if (in_it_block ())
11130     {
11131       /* Conditional branches inside IT blocks are encoded as unconditional
11132          branches.  */
11133       cond = COND_ALWAYS;
11134     }
11135   else
11136     cond = inst.cond;
11137
11138   if (cond != COND_ALWAYS)
11139     opcode = T_MNEM_bcond;
11140   else
11141     opcode = inst.instruction;
11142
11143   if (unified_syntax
11144       && (inst.size_req == 4
11145           || (inst.size_req != 2
11146               && (inst.operands[0].hasreloc
11147                   || inst.reloc.exp.X_op == O_constant))))
11148     {
11149       inst.instruction = THUMB_OP32(opcode);
11150       if (cond == COND_ALWAYS)
11151         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11152       else
11153         {
11154           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11155                       _("selected architecture does not support "
11156                         "wide conditional branch instruction"));
11157
11158           gas_assert (cond != 0xF);
11159           inst.instruction |= cond << 22;
11160           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11161         }
11162     }
11163   else
11164     {
11165       inst.instruction = THUMB_OP16(opcode);
11166       if (cond == COND_ALWAYS)
11167         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11168       else
11169         {
11170           inst.instruction |= cond << 8;
11171           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11172         }
11173       /* Allow section relaxation.  */
11174       if (unified_syntax && inst.size_req != 2)
11175         inst.relax = opcode;
11176     }
11177   inst.reloc.type = reloc;
11178   inst.reloc.pc_rel = 1;
11179 }
11180
11181 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11182    between the two is the maximum immediate allowed - which is passed in
11183    RANGE.  */
11184 static void
11185 do_t_bkpt_hlt1 (int range)
11186 {
11187   constraint (inst.cond != COND_ALWAYS,
11188               _("instruction is always unconditional"));
11189   if (inst.operands[0].present)
11190     {
11191       constraint (inst.operands[0].imm > range,
11192                   _("immediate value out of range"));
11193       inst.instruction |= inst.operands[0].imm;
11194     }
11195
11196   set_it_insn_type (NEUTRAL_IT_INSN);
11197 }
11198
11199 static void
11200 do_t_hlt (void)
11201 {
11202   do_t_bkpt_hlt1 (63);
11203 }
11204
11205 static void
11206 do_t_bkpt (void)
11207 {
11208   do_t_bkpt_hlt1 (255);
11209 }
11210
11211 static void
11212 do_t_branch23 (void)
11213 {
11214   set_it_insn_type_last ();
11215   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11216
11217   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11218      this file.  We used to simply ignore the PLT reloc type here --
11219      the branch encoding is now needed to deal with TLSCALL relocs.
11220      So if we see a PLT reloc now, put it back to how it used to be to
11221      keep the preexisting behaviour.  */
11222   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
11223     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11224
11225 #if defined(OBJ_COFF)
11226   /* If the destination of the branch is a defined symbol which does not have
11227      the THUMB_FUNC attribute, then we must be calling a function which has
11228      the (interfacearm) attribute.  We look for the Thumb entry point to that
11229      function and change the branch to refer to that function instead.  */
11230   if (   inst.reloc.exp.X_op == O_symbol
11231       && inst.reloc.exp.X_add_symbol != NULL
11232       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
11233       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
11234     inst.reloc.exp.X_add_symbol =
11235       find_real_start (inst.reloc.exp.X_add_symbol);
11236 #endif
11237 }
11238
11239 static void
11240 do_t_bx (void)
11241 {
11242   set_it_insn_type_last ();
11243   inst.instruction |= inst.operands[0].reg << 3;
11244   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11245      should cause the alignment to be checked once it is known.  This is
11246      because BX PC only works if the instruction is word aligned.  */
11247 }
11248
11249 static void
11250 do_t_bxj (void)
11251 {
11252   int Rm;
11253
11254   set_it_insn_type_last ();
11255   Rm = inst.operands[0].reg;
11256   reject_bad_reg (Rm);
11257   inst.instruction |= Rm << 16;
11258 }
11259
11260 static void
11261 do_t_clz (void)
11262 {
11263   unsigned Rd;
11264   unsigned Rm;
11265
11266   Rd = inst.operands[0].reg;
11267   Rm = inst.operands[1].reg;
11268
11269   reject_bad_reg (Rd);
11270   reject_bad_reg (Rm);
11271
11272   inst.instruction |= Rd << 8;
11273   inst.instruction |= Rm << 16;
11274   inst.instruction |= Rm;
11275 }
11276
11277 static void
11278 do_t_csdb (void)
11279 {
11280   set_it_insn_type (OUTSIDE_IT_INSN);
11281 }
11282
11283 static void
11284 do_t_cps (void)
11285 {
11286   set_it_insn_type (OUTSIDE_IT_INSN);
11287   inst.instruction |= inst.operands[0].imm;
11288 }
11289
11290 static void
11291 do_t_cpsi (void)
11292 {
11293   set_it_insn_type (OUTSIDE_IT_INSN);
11294   if (unified_syntax
11295       && (inst.operands[1].present || inst.size_req == 4)
11296       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11297     {
11298       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11299       inst.instruction = 0xf3af8000;
11300       inst.instruction |= imod << 9;
11301       inst.instruction |= inst.operands[0].imm << 5;
11302       if (inst.operands[1].present)
11303         inst.instruction |= 0x100 | inst.operands[1].imm;
11304     }
11305   else
11306     {
11307       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11308                   && (inst.operands[0].imm & 4),
11309                   _("selected processor does not support 'A' form "
11310                     "of this instruction"));
11311       constraint (inst.operands[1].present || inst.size_req == 4,
11312                   _("Thumb does not support the 2-argument "
11313                     "form of this instruction"));
11314       inst.instruction |= inst.operands[0].imm;
11315     }
11316 }
11317
11318 /* THUMB CPY instruction (argument parse).  */
11319
11320 static void
11321 do_t_cpy (void)
11322 {
11323   if (inst.size_req == 4)
11324     {
11325       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11326       inst.instruction |= inst.operands[0].reg << 8;
11327       inst.instruction |= inst.operands[1].reg;
11328     }
11329   else
11330     {
11331       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11332       inst.instruction |= (inst.operands[0].reg & 0x7);
11333       inst.instruction |= inst.operands[1].reg << 3;
11334     }
11335 }
11336
11337 static void
11338 do_t_cbz (void)
11339 {
11340   set_it_insn_type (OUTSIDE_IT_INSN);
11341   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11342   inst.instruction |= inst.operands[0].reg;
11343   inst.reloc.pc_rel = 1;
11344   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11345 }
11346
11347 static void
11348 do_t_dbg (void)
11349 {
11350   inst.instruction |= inst.operands[0].imm;
11351 }
11352
11353 static void
11354 do_t_div (void)
11355 {
11356   unsigned Rd, Rn, Rm;
11357
11358   Rd = inst.operands[0].reg;
11359   Rn = (inst.operands[1].present
11360         ? inst.operands[1].reg : Rd);
11361   Rm = inst.operands[2].reg;
11362
11363   reject_bad_reg (Rd);
11364   reject_bad_reg (Rn);
11365   reject_bad_reg (Rm);
11366
11367   inst.instruction |= Rd << 8;
11368   inst.instruction |= Rn << 16;
11369   inst.instruction |= Rm;
11370 }
11371
11372 static void
11373 do_t_hint (void)
11374 {
11375   if (unified_syntax && inst.size_req == 4)
11376     inst.instruction = THUMB_OP32 (inst.instruction);
11377   else
11378     inst.instruction = THUMB_OP16 (inst.instruction);
11379 }
11380
11381 static void
11382 do_t_it (void)
11383 {
11384   unsigned int cond = inst.operands[0].imm;
11385
11386   set_it_insn_type (IT_INSN);
11387   now_it.mask = (inst.instruction & 0xf) | 0x10;
11388   now_it.cc = cond;
11389   now_it.warn_deprecated = FALSE;
11390
11391   /* If the condition is a negative condition, invert the mask.  */
11392   if ((cond & 0x1) == 0x0)
11393     {
11394       unsigned int mask = inst.instruction & 0x000f;
11395
11396       if ((mask & 0x7) == 0)
11397         {
11398           /* No conversion needed.  */
11399           now_it.block_length = 1;
11400         }
11401       else if ((mask & 0x3) == 0)
11402         {
11403           mask ^= 0x8;
11404           now_it.block_length = 2;
11405         }
11406       else if ((mask & 0x1) == 0)
11407         {
11408           mask ^= 0xC;
11409           now_it.block_length = 3;
11410         }
11411       else
11412         {
11413           mask ^= 0xE;
11414           now_it.block_length = 4;
11415         }
11416
11417       inst.instruction &= 0xfff0;
11418       inst.instruction |= mask;
11419     }
11420
11421   inst.instruction |= cond << 4;
11422 }
11423
11424 /* Helper function used for both push/pop and ldm/stm.  */
11425 static void
11426 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11427 {
11428   bfd_boolean load;
11429
11430   load = (inst.instruction & (1 << 20)) != 0;
11431
11432   if (mask & (1 << 13))
11433     inst.error =  _("SP not allowed in register list");
11434
11435   if ((mask & (1 << base)) != 0
11436       && writeback)
11437     inst.error = _("having the base register in the register list when "
11438                    "using write back is UNPREDICTABLE");
11439
11440   if (load)
11441     {
11442       if (mask & (1 << 15))
11443         {
11444           if (mask & (1 << 14))
11445             inst.error = _("LR and PC should not both be in register list");
11446           else
11447             set_it_insn_type_last ();
11448         }
11449     }
11450   else
11451     {
11452       if (mask & (1 << 15))
11453         inst.error = _("PC not allowed in register list");
11454     }
11455
11456   if ((mask & (mask - 1)) == 0)
11457     {
11458       /* Single register transfers implemented as str/ldr.  */
11459       if (writeback)
11460         {
11461           if (inst.instruction & (1 << 23))
11462             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11463           else
11464             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11465         }
11466       else
11467         {
11468           if (inst.instruction & (1 << 23))
11469             inst.instruction = 0x00800000; /* ia -> [base] */
11470           else
11471             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11472         }
11473
11474       inst.instruction |= 0xf8400000;
11475       if (load)
11476         inst.instruction |= 0x00100000;
11477
11478       mask = ffs (mask) - 1;
11479       mask <<= 12;
11480     }
11481   else if (writeback)
11482     inst.instruction |= WRITE_BACK;
11483
11484   inst.instruction |= mask;
11485   inst.instruction |= base << 16;
11486 }
11487
11488 static void
11489 do_t_ldmstm (void)
11490 {
11491   /* This really doesn't seem worth it.  */
11492   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11493               _("expression too complex"));
11494   constraint (inst.operands[1].writeback,
11495               _("Thumb load/store multiple does not support {reglist}^"));
11496
11497   if (unified_syntax)
11498     {
11499       bfd_boolean narrow;
11500       unsigned mask;
11501
11502       narrow = FALSE;
11503       /* See if we can use a 16-bit instruction.  */
11504       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11505           && inst.size_req != 4
11506           && !(inst.operands[1].imm & ~0xff))
11507         {
11508           mask = 1 << inst.operands[0].reg;
11509
11510           if (inst.operands[0].reg <= 7)
11511             {
11512               if (inst.instruction == T_MNEM_stmia
11513                   ? inst.operands[0].writeback
11514                   : (inst.operands[0].writeback
11515                      == !(inst.operands[1].imm & mask)))
11516                 {
11517                   if (inst.instruction == T_MNEM_stmia
11518                       && (inst.operands[1].imm & mask)
11519                       && (inst.operands[1].imm & (mask - 1)))
11520                     as_warn (_("value stored for r%d is UNKNOWN"),
11521                              inst.operands[0].reg);
11522
11523                   inst.instruction = THUMB_OP16 (inst.instruction);
11524                   inst.instruction |= inst.operands[0].reg << 8;
11525                   inst.instruction |= inst.operands[1].imm;
11526                   narrow = TRUE;
11527                 }
11528               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11529                 {
11530                   /* This means 1 register in reg list one of 3 situations:
11531                      1. Instruction is stmia, but without writeback.
11532                      2. lmdia without writeback, but with Rn not in
11533                         reglist.
11534                      3. ldmia with writeback, but with Rn in reglist.
11535                      Case 3 is UNPREDICTABLE behaviour, so we handle
11536                      case 1 and 2 which can be converted into a 16-bit
11537                      str or ldr. The SP cases are handled below.  */
11538                   unsigned long opcode;
11539                   /* First, record an error for Case 3.  */
11540                   if (inst.operands[1].imm & mask
11541                       && inst.operands[0].writeback)
11542                     inst.error =
11543                         _("having the base register in the register list when "
11544                           "using write back is UNPREDICTABLE");
11545
11546                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11547                                                              : T_MNEM_ldr);
11548                   inst.instruction = THUMB_OP16 (opcode);
11549                   inst.instruction |= inst.operands[0].reg << 3;
11550                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11551                   narrow = TRUE;
11552                 }
11553             }
11554           else if (inst.operands[0] .reg == REG_SP)
11555             {
11556               if (inst.operands[0].writeback)
11557                 {
11558                   inst.instruction =
11559                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11560                                     ? T_MNEM_push : T_MNEM_pop);
11561                   inst.instruction |= inst.operands[1].imm;
11562                   narrow = TRUE;
11563                 }
11564               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11565                 {
11566                   inst.instruction =
11567                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11568                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11569                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11570                   narrow = TRUE;
11571                 }
11572             }
11573         }
11574
11575       if (!narrow)
11576         {
11577           if (inst.instruction < 0xffff)
11578             inst.instruction = THUMB_OP32 (inst.instruction);
11579
11580           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11581                                 inst.operands[0].writeback);
11582         }
11583     }
11584   else
11585     {
11586       constraint (inst.operands[0].reg > 7
11587                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11588       constraint (inst.instruction != T_MNEM_ldmia
11589                   && inst.instruction != T_MNEM_stmia,
11590                   _("Thumb-2 instruction only valid in unified syntax"));
11591       if (inst.instruction == T_MNEM_stmia)
11592         {
11593           if (!inst.operands[0].writeback)
11594             as_warn (_("this instruction will write back the base register"));
11595           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11596               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11597             as_warn (_("value stored for r%d is UNKNOWN"),
11598                      inst.operands[0].reg);
11599         }
11600       else
11601         {
11602           if (!inst.operands[0].writeback
11603               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11604             as_warn (_("this instruction will write back the base register"));
11605           else if (inst.operands[0].writeback
11606                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11607             as_warn (_("this instruction will not write back the base register"));
11608         }
11609
11610       inst.instruction = THUMB_OP16 (inst.instruction);
11611       inst.instruction |= inst.operands[0].reg << 8;
11612       inst.instruction |= inst.operands[1].imm;
11613     }
11614 }
11615
11616 static void
11617 do_t_ldrex (void)
11618 {
11619   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11620               || inst.operands[1].postind || inst.operands[1].writeback
11621               || inst.operands[1].immisreg || inst.operands[1].shifted
11622               || inst.operands[1].negative,
11623               BAD_ADDR_MODE);
11624
11625   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11626
11627   inst.instruction |= inst.operands[0].reg << 12;
11628   inst.instruction |= inst.operands[1].reg << 16;
11629   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11630 }
11631
11632 static void
11633 do_t_ldrexd (void)
11634 {
11635   if (!inst.operands[1].present)
11636     {
11637       constraint (inst.operands[0].reg == REG_LR,
11638                   _("r14 not allowed as first register "
11639                     "when second register is omitted"));
11640       inst.operands[1].reg = inst.operands[0].reg + 1;
11641     }
11642   constraint (inst.operands[0].reg == inst.operands[1].reg,
11643               BAD_OVERLAP);
11644
11645   inst.instruction |= inst.operands[0].reg << 12;
11646   inst.instruction |= inst.operands[1].reg << 8;
11647   inst.instruction |= inst.operands[2].reg << 16;
11648 }
11649
11650 static void
11651 do_t_ldst (void)
11652 {
11653   unsigned long opcode;
11654   int Rn;
11655
11656   if (inst.operands[0].isreg
11657       && !inst.operands[0].preind
11658       && inst.operands[0].reg == REG_PC)
11659     set_it_insn_type_last ();
11660
11661   opcode = inst.instruction;
11662   if (unified_syntax)
11663     {
11664       if (!inst.operands[1].isreg)
11665         {
11666           if (opcode <= 0xffff)
11667             inst.instruction = THUMB_OP32 (opcode);
11668           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11669             return;
11670         }
11671       if (inst.operands[1].isreg
11672           && !inst.operands[1].writeback
11673           && !inst.operands[1].shifted && !inst.operands[1].postind
11674           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11675           && opcode <= 0xffff
11676           && inst.size_req != 4)
11677         {
11678           /* Insn may have a 16-bit form.  */
11679           Rn = inst.operands[1].reg;
11680           if (inst.operands[1].immisreg)
11681             {
11682               inst.instruction = THUMB_OP16 (opcode);
11683               /* [Rn, Rik] */
11684               if (Rn <= 7 && inst.operands[1].imm <= 7)
11685                 goto op16;
11686               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11687                 reject_bad_reg (inst.operands[1].imm);
11688             }
11689           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11690                     && opcode != T_MNEM_ldrsb)
11691                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11692                    || (Rn == REG_SP && opcode == T_MNEM_str))
11693             {
11694               /* [Rn, #const] */
11695               if (Rn > 7)
11696                 {
11697                   if (Rn == REG_PC)
11698                     {
11699                       if (inst.reloc.pc_rel)
11700                         opcode = T_MNEM_ldr_pc2;
11701                       else
11702                         opcode = T_MNEM_ldr_pc;
11703                     }
11704                   else
11705                     {
11706                       if (opcode == T_MNEM_ldr)
11707                         opcode = T_MNEM_ldr_sp;
11708                       else
11709                         opcode = T_MNEM_str_sp;
11710                     }
11711                   inst.instruction = inst.operands[0].reg << 8;
11712                 }
11713               else
11714                 {
11715                   inst.instruction = inst.operands[0].reg;
11716                   inst.instruction |= inst.operands[1].reg << 3;
11717                 }
11718               inst.instruction |= THUMB_OP16 (opcode);
11719               if (inst.size_req == 2)
11720                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11721               else
11722                 inst.relax = opcode;
11723               return;
11724             }
11725         }
11726       /* Definitely a 32-bit variant.  */
11727
11728       /* Warning for Erratum 752419.  */
11729       if (opcode == T_MNEM_ldr
11730           && inst.operands[0].reg == REG_SP
11731           && inst.operands[1].writeback == 1
11732           && !inst.operands[1].immisreg)
11733         {
11734           if (no_cpu_selected ()
11735               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11736                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11737                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11738             as_warn (_("This instruction may be unpredictable "
11739                        "if executed on M-profile cores "
11740                        "with interrupts enabled."));
11741         }
11742
11743       /* Do some validations regarding addressing modes.  */
11744       if (inst.operands[1].immisreg)
11745         reject_bad_reg (inst.operands[1].imm);
11746
11747       constraint (inst.operands[1].writeback == 1
11748                   && inst.operands[0].reg == inst.operands[1].reg,
11749                   BAD_OVERLAP);
11750
11751       inst.instruction = THUMB_OP32 (opcode);
11752       inst.instruction |= inst.operands[0].reg << 12;
11753       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11754       check_ldr_r15_aligned ();
11755       return;
11756     }
11757
11758   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11759
11760   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11761     {
11762       /* Only [Rn,Rm] is acceptable.  */
11763       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11764       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11765                   || inst.operands[1].postind || inst.operands[1].shifted
11766                   || inst.operands[1].negative,
11767                   _("Thumb does not support this addressing mode"));
11768       inst.instruction = THUMB_OP16 (inst.instruction);
11769       goto op16;
11770     }
11771
11772   inst.instruction = THUMB_OP16 (inst.instruction);
11773   if (!inst.operands[1].isreg)
11774     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11775       return;
11776
11777   constraint (!inst.operands[1].preind
11778               || inst.operands[1].shifted
11779               || inst.operands[1].writeback,
11780               _("Thumb does not support this addressing mode"));
11781   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11782     {
11783       constraint (inst.instruction & 0x0600,
11784                   _("byte or halfword not valid for base register"));
11785       constraint (inst.operands[1].reg == REG_PC
11786                   && !(inst.instruction & THUMB_LOAD_BIT),
11787                   _("r15 based store not allowed"));
11788       constraint (inst.operands[1].immisreg,
11789                   _("invalid base register for register offset"));
11790
11791       if (inst.operands[1].reg == REG_PC)
11792         inst.instruction = T_OPCODE_LDR_PC;
11793       else if (inst.instruction & THUMB_LOAD_BIT)
11794         inst.instruction = T_OPCODE_LDR_SP;
11795       else
11796         inst.instruction = T_OPCODE_STR_SP;
11797
11798       inst.instruction |= inst.operands[0].reg << 8;
11799       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11800       return;
11801     }
11802
11803   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11804   if (!inst.operands[1].immisreg)
11805     {
11806       /* Immediate offset.  */
11807       inst.instruction |= inst.operands[0].reg;
11808       inst.instruction |= inst.operands[1].reg << 3;
11809       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11810       return;
11811     }
11812
11813   /* Register offset.  */
11814   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11815   constraint (inst.operands[1].negative,
11816               _("Thumb does not support this addressing mode"));
11817
11818  op16:
11819   switch (inst.instruction)
11820     {
11821     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11822     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11823     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11824     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11825     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11826     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11827     case 0x5600 /* ldrsb */:
11828     case 0x5e00 /* ldrsh */: break;
11829     default: abort ();
11830     }
11831
11832   inst.instruction |= inst.operands[0].reg;
11833   inst.instruction |= inst.operands[1].reg << 3;
11834   inst.instruction |= inst.operands[1].imm << 6;
11835 }
11836
11837 static void
11838 do_t_ldstd (void)
11839 {
11840   if (!inst.operands[1].present)
11841     {
11842       inst.operands[1].reg = inst.operands[0].reg + 1;
11843       constraint (inst.operands[0].reg == REG_LR,
11844                   _("r14 not allowed here"));
11845       constraint (inst.operands[0].reg == REG_R12,
11846                   _("r12 not allowed here"));
11847     }
11848
11849   if (inst.operands[2].writeback
11850       && (inst.operands[0].reg == inst.operands[2].reg
11851       || inst.operands[1].reg == inst.operands[2].reg))
11852     as_warn (_("base register written back, and overlaps "
11853                "one of transfer registers"));
11854
11855   inst.instruction |= inst.operands[0].reg << 12;
11856   inst.instruction |= inst.operands[1].reg << 8;
11857   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11858 }
11859
11860 static void
11861 do_t_ldstt (void)
11862 {
11863   inst.instruction |= inst.operands[0].reg << 12;
11864   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11865 }
11866
11867 static void
11868 do_t_mla (void)
11869 {
11870   unsigned Rd, Rn, Rm, Ra;
11871
11872   Rd = inst.operands[0].reg;
11873   Rn = inst.operands[1].reg;
11874   Rm = inst.operands[2].reg;
11875   Ra = inst.operands[3].reg;
11876
11877   reject_bad_reg (Rd);
11878   reject_bad_reg (Rn);
11879   reject_bad_reg (Rm);
11880   reject_bad_reg (Ra);
11881
11882   inst.instruction |= Rd << 8;
11883   inst.instruction |= Rn << 16;
11884   inst.instruction |= Rm;
11885   inst.instruction |= Ra << 12;
11886 }
11887
11888 static void
11889 do_t_mlal (void)
11890 {
11891   unsigned RdLo, RdHi, Rn, Rm;
11892
11893   RdLo = inst.operands[0].reg;
11894   RdHi = inst.operands[1].reg;
11895   Rn = inst.operands[2].reg;
11896   Rm = inst.operands[3].reg;
11897
11898   reject_bad_reg (RdLo);
11899   reject_bad_reg (RdHi);
11900   reject_bad_reg (Rn);
11901   reject_bad_reg (Rm);
11902
11903   inst.instruction |= RdLo << 12;
11904   inst.instruction |= RdHi << 8;
11905   inst.instruction |= Rn << 16;
11906   inst.instruction |= Rm;
11907 }
11908
11909 static void
11910 do_t_mov_cmp (void)
11911 {
11912   unsigned Rn, Rm;
11913
11914   Rn = inst.operands[0].reg;
11915   Rm = inst.operands[1].reg;
11916
11917   if (Rn == REG_PC)
11918     set_it_insn_type_last ();
11919
11920   if (unified_syntax)
11921     {
11922       int r0off = (inst.instruction == T_MNEM_mov
11923                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11924       unsigned long opcode;
11925       bfd_boolean narrow;
11926       bfd_boolean low_regs;
11927
11928       low_regs = (Rn <= 7 && Rm <= 7);
11929       opcode = inst.instruction;
11930       if (in_it_block ())
11931         narrow = opcode != T_MNEM_movs;
11932       else
11933         narrow = opcode != T_MNEM_movs || low_regs;
11934       if (inst.size_req == 4
11935           || inst.operands[1].shifted)
11936         narrow = FALSE;
11937
11938       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11939       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11940           && !inst.operands[1].shifted
11941           && Rn == REG_PC
11942           && Rm == REG_LR)
11943         {
11944           inst.instruction = T2_SUBS_PC_LR;
11945           return;
11946         }
11947
11948       if (opcode == T_MNEM_cmp)
11949         {
11950           constraint (Rn == REG_PC, BAD_PC);
11951           if (narrow)
11952             {
11953               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11954                  but valid.  */
11955               warn_deprecated_sp (Rm);
11956               /* R15 was documented as a valid choice for Rm in ARMv6,
11957                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11958                  tools reject R15, so we do too.  */
11959               constraint (Rm == REG_PC, BAD_PC);
11960             }
11961           else
11962             reject_bad_reg (Rm);
11963         }
11964       else if (opcode == T_MNEM_mov
11965                || opcode == T_MNEM_movs)
11966         {
11967           if (inst.operands[1].isreg)
11968             {
11969               if (opcode == T_MNEM_movs)
11970                 {
11971                   reject_bad_reg (Rn);
11972                   reject_bad_reg (Rm);
11973                 }
11974               else if (narrow)
11975                 {
11976                   /* This is mov.n.  */
11977                   if ((Rn == REG_SP || Rn == REG_PC)
11978                       && (Rm == REG_SP || Rm == REG_PC))
11979                     {
11980                       as_tsktsk (_("Use of r%u as a source register is "
11981                                  "deprecated when r%u is the destination "
11982                                  "register."), Rm, Rn);
11983                     }
11984                 }
11985               else
11986                 {
11987                   /* This is mov.w.  */
11988                   constraint (Rn == REG_PC, BAD_PC);
11989                   constraint (Rm == REG_PC, BAD_PC);
11990                   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
11991                     constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11992                 }
11993             }
11994           else
11995             reject_bad_reg (Rn);
11996         }
11997
11998       if (!inst.operands[1].isreg)
11999         {
12000           /* Immediate operand.  */
12001           if (!in_it_block () && opcode == T_MNEM_mov)
12002             narrow = 0;
12003           if (low_regs && narrow)
12004             {
12005               inst.instruction = THUMB_OP16 (opcode);
12006               inst.instruction |= Rn << 8;
12007               if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12008                   || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
12009                 {
12010                   if (inst.size_req == 2)
12011                     inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12012                   else
12013                     inst.relax = opcode;
12014                 }
12015             }
12016           else
12017             {
12018               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
12019                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
12020                           THUMB1_RELOC_ONLY);
12021
12022               inst.instruction = THUMB_OP32 (inst.instruction);
12023               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12024               inst.instruction |= Rn << r0off;
12025               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12026             }
12027         }
12028       else if (inst.operands[1].shifted && inst.operands[1].immisreg
12029                && (inst.instruction == T_MNEM_mov
12030                    || inst.instruction == T_MNEM_movs))
12031         {
12032           /* Register shifts are encoded as separate shift instructions.  */
12033           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
12034
12035           if (in_it_block ())
12036             narrow = !flags;
12037           else
12038             narrow = flags;
12039
12040           if (inst.size_req == 4)
12041             narrow = FALSE;
12042
12043           if (!low_regs || inst.operands[1].imm > 7)
12044             narrow = FALSE;
12045
12046           if (Rn != Rm)
12047             narrow = FALSE;
12048
12049           switch (inst.operands[1].shift_kind)
12050             {
12051             case SHIFT_LSL:
12052               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
12053               break;
12054             case SHIFT_ASR:
12055               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
12056               break;
12057             case SHIFT_LSR:
12058               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
12059               break;
12060             case SHIFT_ROR:
12061               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
12062               break;
12063             default:
12064               abort ();
12065             }
12066
12067           inst.instruction = opcode;
12068           if (narrow)
12069             {
12070               inst.instruction |= Rn;
12071               inst.instruction |= inst.operands[1].imm << 3;
12072             }
12073           else
12074             {
12075               if (flags)
12076                 inst.instruction |= CONDS_BIT;
12077
12078               inst.instruction |= Rn << 8;
12079               inst.instruction |= Rm << 16;
12080               inst.instruction |= inst.operands[1].imm;
12081             }
12082         }
12083       else if (!narrow)
12084         {
12085           /* Some mov with immediate shift have narrow variants.
12086              Register shifts are handled above.  */
12087           if (low_regs && inst.operands[1].shifted
12088               && (inst.instruction == T_MNEM_mov
12089                   || inst.instruction == T_MNEM_movs))
12090             {
12091               if (in_it_block ())
12092                 narrow = (inst.instruction == T_MNEM_mov);
12093               else
12094                 narrow = (inst.instruction == T_MNEM_movs);
12095             }
12096
12097           if (narrow)
12098             {
12099               switch (inst.operands[1].shift_kind)
12100                 {
12101                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12102                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12103                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12104                 default: narrow = FALSE; break;
12105                 }
12106             }
12107
12108           if (narrow)
12109             {
12110               inst.instruction |= Rn;
12111               inst.instruction |= Rm << 3;
12112               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12113             }
12114           else
12115             {
12116               inst.instruction = THUMB_OP32 (inst.instruction);
12117               inst.instruction |= Rn << r0off;
12118               encode_thumb32_shifted_operand (1);
12119             }
12120         }
12121       else
12122         switch (inst.instruction)
12123           {
12124           case T_MNEM_mov:
12125             /* In v4t or v5t a move of two lowregs produces unpredictable
12126                results. Don't allow this.  */
12127             if (low_regs)
12128               {
12129                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12130                             "MOV Rd, Rs with two low registers is not "
12131                             "permitted on this architecture");
12132                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12133                                         arm_ext_v6);
12134               }
12135
12136             inst.instruction = T_OPCODE_MOV_HR;
12137             inst.instruction |= (Rn & 0x8) << 4;
12138             inst.instruction |= (Rn & 0x7);
12139             inst.instruction |= Rm << 3;
12140             break;
12141
12142           case T_MNEM_movs:
12143             /* We know we have low registers at this point.
12144                Generate LSLS Rd, Rs, #0.  */
12145             inst.instruction = T_OPCODE_LSL_I;
12146             inst.instruction |= Rn;
12147             inst.instruction |= Rm << 3;
12148             break;
12149
12150           case T_MNEM_cmp:
12151             if (low_regs)
12152               {
12153                 inst.instruction = T_OPCODE_CMP_LR;
12154                 inst.instruction |= Rn;
12155                 inst.instruction |= Rm << 3;
12156               }
12157             else
12158               {
12159                 inst.instruction = T_OPCODE_CMP_HR;
12160                 inst.instruction |= (Rn & 0x8) << 4;
12161                 inst.instruction |= (Rn & 0x7);
12162                 inst.instruction |= Rm << 3;
12163               }
12164             break;
12165           }
12166       return;
12167     }
12168
12169   inst.instruction = THUMB_OP16 (inst.instruction);
12170
12171   /* PR 10443: Do not silently ignore shifted operands.  */
12172   constraint (inst.operands[1].shifted,
12173               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12174
12175   if (inst.operands[1].isreg)
12176     {
12177       if (Rn < 8 && Rm < 8)
12178         {
12179           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12180              since a MOV instruction produces unpredictable results.  */
12181           if (inst.instruction == T_OPCODE_MOV_I8)
12182             inst.instruction = T_OPCODE_ADD_I3;
12183           else
12184             inst.instruction = T_OPCODE_CMP_LR;
12185
12186           inst.instruction |= Rn;
12187           inst.instruction |= Rm << 3;
12188         }
12189       else
12190         {
12191           if (inst.instruction == T_OPCODE_MOV_I8)
12192             inst.instruction = T_OPCODE_MOV_HR;
12193           else
12194             inst.instruction = T_OPCODE_CMP_HR;
12195           do_t_cpy ();
12196         }
12197     }
12198   else
12199     {
12200       constraint (Rn > 7,
12201                   _("only lo regs allowed with immediate"));
12202       inst.instruction |= Rn << 8;
12203       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12204     }
12205 }
12206
12207 static void
12208 do_t_mov16 (void)
12209 {
12210   unsigned Rd;
12211   bfd_vma imm;
12212   bfd_boolean top;
12213
12214   top = (inst.instruction & 0x00800000) != 0;
12215   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
12216     {
12217       constraint (top, _(":lower16: not allowed in this instruction"));
12218       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
12219     }
12220   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
12221     {
12222       constraint (!top, _(":upper16: not allowed in this instruction"));
12223       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
12224     }
12225
12226   Rd = inst.operands[0].reg;
12227   reject_bad_reg (Rd);
12228
12229   inst.instruction |= Rd << 8;
12230   if (inst.reloc.type == BFD_RELOC_UNUSED)
12231     {
12232       imm = inst.reloc.exp.X_add_number;
12233       inst.instruction |= (imm & 0xf000) << 4;
12234       inst.instruction |= (imm & 0x0800) << 15;
12235       inst.instruction |= (imm & 0x0700) << 4;
12236       inst.instruction |= (imm & 0x00ff);
12237     }
12238 }
12239
12240 static void
12241 do_t_mvn_tst (void)
12242 {
12243   unsigned Rn, Rm;
12244
12245   Rn = inst.operands[0].reg;
12246   Rm = inst.operands[1].reg;
12247
12248   if (inst.instruction == T_MNEM_cmp
12249       || inst.instruction == T_MNEM_cmn)
12250     constraint (Rn == REG_PC, BAD_PC);
12251   else
12252     reject_bad_reg (Rn);
12253   reject_bad_reg (Rm);
12254
12255   if (unified_syntax)
12256     {
12257       int r0off = (inst.instruction == T_MNEM_mvn
12258                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12259       bfd_boolean narrow;
12260
12261       if (inst.size_req == 4
12262           || inst.instruction > 0xffff
12263           || inst.operands[1].shifted
12264           || Rn > 7 || Rm > 7)
12265         narrow = FALSE;
12266       else if (inst.instruction == T_MNEM_cmn
12267                || inst.instruction == T_MNEM_tst)
12268         narrow = TRUE;
12269       else if (THUMB_SETS_FLAGS (inst.instruction))
12270         narrow = !in_it_block ();
12271       else
12272         narrow = in_it_block ();
12273
12274       if (!inst.operands[1].isreg)
12275         {
12276           /* For an immediate, we always generate a 32-bit opcode;
12277              section relaxation will shrink it later if possible.  */
12278           if (inst.instruction < 0xffff)
12279             inst.instruction = THUMB_OP32 (inst.instruction);
12280           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12281           inst.instruction |= Rn << r0off;
12282           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12283         }
12284       else
12285         {
12286           /* See if we can do this with a 16-bit instruction.  */
12287           if (narrow)
12288             {
12289               inst.instruction = THUMB_OP16 (inst.instruction);
12290               inst.instruction |= Rn;
12291               inst.instruction |= Rm << 3;
12292             }
12293           else
12294             {
12295               constraint (inst.operands[1].shifted
12296                           && inst.operands[1].immisreg,
12297                           _("shift must be constant"));
12298               if (inst.instruction < 0xffff)
12299                 inst.instruction = THUMB_OP32 (inst.instruction);
12300               inst.instruction |= Rn << r0off;
12301               encode_thumb32_shifted_operand (1);
12302             }
12303         }
12304     }
12305   else
12306     {
12307       constraint (inst.instruction > 0xffff
12308                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12309       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12310                   _("unshifted register required"));
12311       constraint (Rn > 7 || Rm > 7,
12312                   BAD_HIREG);
12313
12314       inst.instruction = THUMB_OP16 (inst.instruction);
12315       inst.instruction |= Rn;
12316       inst.instruction |= Rm << 3;
12317     }
12318 }
12319
12320 static void
12321 do_t_mrs (void)
12322 {
12323   unsigned Rd;
12324
12325   if (do_vfp_nsyn_mrs () == SUCCESS)
12326     return;
12327
12328   Rd = inst.operands[0].reg;
12329   reject_bad_reg (Rd);
12330   inst.instruction |= Rd << 8;
12331
12332   if (inst.operands[1].isreg)
12333     {
12334       unsigned br = inst.operands[1].reg;
12335       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12336         as_bad (_("bad register for mrs"));
12337
12338       inst.instruction |= br & (0xf << 16);
12339       inst.instruction |= (br & 0x300) >> 4;
12340       inst.instruction |= (br & SPSR_BIT) >> 2;
12341     }
12342   else
12343     {
12344       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12345
12346       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12347         {
12348           /* PR gas/12698:  The constraint is only applied for m_profile.
12349              If the user has specified -march=all, we want to ignore it as
12350              we are building for any CPU type, including non-m variants.  */
12351           bfd_boolean m_profile =
12352             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12353           constraint ((flags != 0) && m_profile, _("selected processor does "
12354                                                    "not support requested special purpose register"));
12355         }
12356       else
12357         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12358            devices).  */
12359         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12360                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12361
12362       inst.instruction |= (flags & SPSR_BIT) >> 2;
12363       inst.instruction |= inst.operands[1].imm & 0xff;
12364       inst.instruction |= 0xf0000;
12365     }
12366 }
12367
12368 static void
12369 do_t_msr (void)
12370 {
12371   int flags;
12372   unsigned Rn;
12373
12374   if (do_vfp_nsyn_msr () == SUCCESS)
12375     return;
12376
12377   constraint (!inst.operands[1].isreg,
12378               _("Thumb encoding does not support an immediate here"));
12379
12380   if (inst.operands[0].isreg)
12381     flags = (int)(inst.operands[0].reg);
12382   else
12383     flags = inst.operands[0].imm;
12384
12385   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12386     {
12387       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12388
12389       /* PR gas/12698:  The constraint is only applied for m_profile.
12390          If the user has specified -march=all, we want to ignore it as
12391          we are building for any CPU type, including non-m variants.  */
12392       bfd_boolean m_profile =
12393         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12394       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12395            && (bits & ~(PSR_s | PSR_f)) != 0)
12396           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12397               && bits != PSR_f)) && m_profile,
12398           _("selected processor does not support requested special "
12399             "purpose register"));
12400     }
12401   else
12402      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12403                  "requested special purpose register"));
12404
12405   Rn = inst.operands[1].reg;
12406   reject_bad_reg (Rn);
12407
12408   inst.instruction |= (flags & SPSR_BIT) >> 2;
12409   inst.instruction |= (flags & 0xf0000) >> 8;
12410   inst.instruction |= (flags & 0x300) >> 4;
12411   inst.instruction |= (flags & 0xff);
12412   inst.instruction |= Rn << 16;
12413 }
12414
12415 static void
12416 do_t_mul (void)
12417 {
12418   bfd_boolean narrow;
12419   unsigned Rd, Rn, Rm;
12420
12421   if (!inst.operands[2].present)
12422     inst.operands[2].reg = inst.operands[0].reg;
12423
12424   Rd = inst.operands[0].reg;
12425   Rn = inst.operands[1].reg;
12426   Rm = inst.operands[2].reg;
12427
12428   if (unified_syntax)
12429     {
12430       if (inst.size_req == 4
12431           || (Rd != Rn
12432               && Rd != Rm)
12433           || Rn > 7
12434           || Rm > 7)
12435         narrow = FALSE;
12436       else if (inst.instruction == T_MNEM_muls)
12437         narrow = !in_it_block ();
12438       else
12439         narrow = in_it_block ();
12440     }
12441   else
12442     {
12443       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12444       constraint (Rn > 7 || Rm > 7,
12445                   BAD_HIREG);
12446       narrow = TRUE;
12447     }
12448
12449   if (narrow)
12450     {
12451       /* 16-bit MULS/Conditional MUL.  */
12452       inst.instruction = THUMB_OP16 (inst.instruction);
12453       inst.instruction |= Rd;
12454
12455       if (Rd == Rn)
12456         inst.instruction |= Rm << 3;
12457       else if (Rd == Rm)
12458         inst.instruction |= Rn << 3;
12459       else
12460         constraint (1, _("dest must overlap one source register"));
12461     }
12462   else
12463     {
12464       constraint (inst.instruction != T_MNEM_mul,
12465                   _("Thumb-2 MUL must not set flags"));
12466       /* 32-bit MUL.  */
12467       inst.instruction = THUMB_OP32 (inst.instruction);
12468       inst.instruction |= Rd << 8;
12469       inst.instruction |= Rn << 16;
12470       inst.instruction |= Rm << 0;
12471
12472       reject_bad_reg (Rd);
12473       reject_bad_reg (Rn);
12474       reject_bad_reg (Rm);
12475     }
12476 }
12477
12478 static void
12479 do_t_mull (void)
12480 {
12481   unsigned RdLo, RdHi, Rn, Rm;
12482
12483   RdLo = inst.operands[0].reg;
12484   RdHi = inst.operands[1].reg;
12485   Rn = inst.operands[2].reg;
12486   Rm = inst.operands[3].reg;
12487
12488   reject_bad_reg (RdLo);
12489   reject_bad_reg (RdHi);
12490   reject_bad_reg (Rn);
12491   reject_bad_reg (Rm);
12492
12493   inst.instruction |= RdLo << 12;
12494   inst.instruction |= RdHi << 8;
12495   inst.instruction |= Rn << 16;
12496   inst.instruction |= Rm;
12497
12498  if (RdLo == RdHi)
12499     as_tsktsk (_("rdhi and rdlo must be different"));
12500 }
12501
12502 static void
12503 do_t_nop (void)
12504 {
12505   set_it_insn_type (NEUTRAL_IT_INSN);
12506
12507   if (unified_syntax)
12508     {
12509       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12510         {
12511           inst.instruction = THUMB_OP32 (inst.instruction);
12512           inst.instruction |= inst.operands[0].imm;
12513         }
12514       else
12515         {
12516           /* PR9722: Check for Thumb2 availability before
12517              generating a thumb2 nop instruction.  */
12518           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12519             {
12520               inst.instruction = THUMB_OP16 (inst.instruction);
12521               inst.instruction |= inst.operands[0].imm << 4;
12522             }
12523           else
12524             inst.instruction = 0x46c0;
12525         }
12526     }
12527   else
12528     {
12529       constraint (inst.operands[0].present,
12530                   _("Thumb does not support NOP with hints"));
12531       inst.instruction = 0x46c0;
12532     }
12533 }
12534
12535 static void
12536 do_t_neg (void)
12537 {
12538   if (unified_syntax)
12539     {
12540       bfd_boolean narrow;
12541
12542       if (THUMB_SETS_FLAGS (inst.instruction))
12543         narrow = !in_it_block ();
12544       else
12545         narrow = in_it_block ();
12546       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12547         narrow = FALSE;
12548       if (inst.size_req == 4)
12549         narrow = FALSE;
12550
12551       if (!narrow)
12552         {
12553           inst.instruction = THUMB_OP32 (inst.instruction);
12554           inst.instruction |= inst.operands[0].reg << 8;
12555           inst.instruction |= inst.operands[1].reg << 16;
12556         }
12557       else
12558         {
12559           inst.instruction = THUMB_OP16 (inst.instruction);
12560           inst.instruction |= inst.operands[0].reg;
12561           inst.instruction |= inst.operands[1].reg << 3;
12562         }
12563     }
12564   else
12565     {
12566       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12567                   BAD_HIREG);
12568       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12569
12570       inst.instruction = THUMB_OP16 (inst.instruction);
12571       inst.instruction |= inst.operands[0].reg;
12572       inst.instruction |= inst.operands[1].reg << 3;
12573     }
12574 }
12575
12576 static void
12577 do_t_orn (void)
12578 {
12579   unsigned Rd, Rn;
12580
12581   Rd = inst.operands[0].reg;
12582   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12583
12584   reject_bad_reg (Rd);
12585   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12586   reject_bad_reg (Rn);
12587
12588   inst.instruction |= Rd << 8;
12589   inst.instruction |= Rn << 16;
12590
12591   if (!inst.operands[2].isreg)
12592     {
12593       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12594       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12595     }
12596   else
12597     {
12598       unsigned Rm;
12599
12600       Rm = inst.operands[2].reg;
12601       reject_bad_reg (Rm);
12602
12603       constraint (inst.operands[2].shifted
12604                   && inst.operands[2].immisreg,
12605                   _("shift must be constant"));
12606       encode_thumb32_shifted_operand (2);
12607     }
12608 }
12609
12610 static void
12611 do_t_pkhbt (void)
12612 {
12613   unsigned Rd, Rn, Rm;
12614
12615   Rd = inst.operands[0].reg;
12616   Rn = inst.operands[1].reg;
12617   Rm = inst.operands[2].reg;
12618
12619   reject_bad_reg (Rd);
12620   reject_bad_reg (Rn);
12621   reject_bad_reg (Rm);
12622
12623   inst.instruction |= Rd << 8;
12624   inst.instruction |= Rn << 16;
12625   inst.instruction |= Rm;
12626   if (inst.operands[3].present)
12627     {
12628       unsigned int val = inst.reloc.exp.X_add_number;
12629       constraint (inst.reloc.exp.X_op != O_constant,
12630                   _("expression too complex"));
12631       inst.instruction |= (val & 0x1c) << 10;
12632       inst.instruction |= (val & 0x03) << 6;
12633     }
12634 }
12635
12636 static void
12637 do_t_pkhtb (void)
12638 {
12639   if (!inst.operands[3].present)
12640     {
12641       unsigned Rtmp;
12642
12643       inst.instruction &= ~0x00000020;
12644
12645       /* PR 10168.  Swap the Rm and Rn registers.  */
12646       Rtmp = inst.operands[1].reg;
12647       inst.operands[1].reg = inst.operands[2].reg;
12648       inst.operands[2].reg = Rtmp;
12649     }
12650   do_t_pkhbt ();
12651 }
12652
12653 static void
12654 do_t_pld (void)
12655 {
12656   if (inst.operands[0].immisreg)
12657     reject_bad_reg (inst.operands[0].imm);
12658
12659   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12660 }
12661
12662 static void
12663 do_t_push_pop (void)
12664 {
12665   unsigned mask;
12666
12667   constraint (inst.operands[0].writeback,
12668               _("push/pop do not support {reglist}^"));
12669   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12670               _("expression too complex"));
12671
12672   mask = inst.operands[0].imm;
12673   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12674     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12675   else if (inst.size_req != 4
12676            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
12677                                        ? REG_LR : REG_PC)))
12678     {
12679       inst.instruction = THUMB_OP16 (inst.instruction);
12680       inst.instruction |= THUMB_PP_PC_LR;
12681       inst.instruction |= mask & 0xff;
12682     }
12683   else if (unified_syntax)
12684     {
12685       inst.instruction = THUMB_OP32 (inst.instruction);
12686       encode_thumb2_ldmstm (13, mask, TRUE);
12687     }
12688   else
12689     {
12690       inst.error = _("invalid register list to push/pop instruction");
12691       return;
12692     }
12693 }
12694
12695 static void
12696 do_t_rbit (void)
12697 {
12698   unsigned Rd, Rm;
12699
12700   Rd = inst.operands[0].reg;
12701   Rm = inst.operands[1].reg;
12702
12703   reject_bad_reg (Rd);
12704   reject_bad_reg (Rm);
12705
12706   inst.instruction |= Rd << 8;
12707   inst.instruction |= Rm << 16;
12708   inst.instruction |= Rm;
12709 }
12710
12711 static void
12712 do_t_rev (void)
12713 {
12714   unsigned Rd, Rm;
12715
12716   Rd = inst.operands[0].reg;
12717   Rm = inst.operands[1].reg;
12718
12719   reject_bad_reg (Rd);
12720   reject_bad_reg (Rm);
12721
12722   if (Rd <= 7 && Rm <= 7
12723       && inst.size_req != 4)
12724     {
12725       inst.instruction = THUMB_OP16 (inst.instruction);
12726       inst.instruction |= Rd;
12727       inst.instruction |= Rm << 3;
12728     }
12729   else if (unified_syntax)
12730     {
12731       inst.instruction = THUMB_OP32 (inst.instruction);
12732       inst.instruction |= Rd << 8;
12733       inst.instruction |= Rm << 16;
12734       inst.instruction |= Rm;
12735     }
12736   else
12737     inst.error = BAD_HIREG;
12738 }
12739
12740 static void
12741 do_t_rrx (void)
12742 {
12743   unsigned Rd, Rm;
12744
12745   Rd = inst.operands[0].reg;
12746   Rm = inst.operands[1].reg;
12747
12748   reject_bad_reg (Rd);
12749   reject_bad_reg (Rm);
12750
12751   inst.instruction |= Rd << 8;
12752   inst.instruction |= Rm;
12753 }
12754
12755 static void
12756 do_t_rsb (void)
12757 {
12758   unsigned Rd, Rs;
12759
12760   Rd = inst.operands[0].reg;
12761   Rs = (inst.operands[1].present
12762         ? inst.operands[1].reg    /* Rd, Rs, foo */
12763         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12764
12765   reject_bad_reg (Rd);
12766   reject_bad_reg (Rs);
12767   if (inst.operands[2].isreg)
12768     reject_bad_reg (inst.operands[2].reg);
12769
12770   inst.instruction |= Rd << 8;
12771   inst.instruction |= Rs << 16;
12772   if (!inst.operands[2].isreg)
12773     {
12774       bfd_boolean narrow;
12775
12776       if ((inst.instruction & 0x00100000) != 0)
12777         narrow = !in_it_block ();
12778       else
12779         narrow = in_it_block ();
12780
12781       if (Rd > 7 || Rs > 7)
12782         narrow = FALSE;
12783
12784       if (inst.size_req == 4 || !unified_syntax)
12785         narrow = FALSE;
12786
12787       if (inst.reloc.exp.X_op != O_constant
12788           || inst.reloc.exp.X_add_number != 0)
12789         narrow = FALSE;
12790
12791       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12792          relaxation, but it doesn't seem worth the hassle.  */
12793       if (narrow)
12794         {
12795           inst.reloc.type = BFD_RELOC_UNUSED;
12796           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12797           inst.instruction |= Rs << 3;
12798           inst.instruction |= Rd;
12799         }
12800       else
12801         {
12802           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12803           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12804         }
12805     }
12806   else
12807     encode_thumb32_shifted_operand (2);
12808 }
12809
12810 static void
12811 do_t_setend (void)
12812 {
12813   if (warn_on_deprecated
12814       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12815       as_tsktsk (_("setend use is deprecated for ARMv8"));
12816
12817   set_it_insn_type (OUTSIDE_IT_INSN);
12818   if (inst.operands[0].imm)
12819     inst.instruction |= 0x8;
12820 }
12821
12822 static void
12823 do_t_shift (void)
12824 {
12825   if (!inst.operands[1].present)
12826     inst.operands[1].reg = inst.operands[0].reg;
12827
12828   if (unified_syntax)
12829     {
12830       bfd_boolean narrow;
12831       int shift_kind;
12832
12833       switch (inst.instruction)
12834         {
12835         case T_MNEM_asr:
12836         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12837         case T_MNEM_lsl:
12838         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12839         case T_MNEM_lsr:
12840         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12841         case T_MNEM_ror:
12842         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12843         default: abort ();
12844         }
12845
12846       if (THUMB_SETS_FLAGS (inst.instruction))
12847         narrow = !in_it_block ();
12848       else
12849         narrow = in_it_block ();
12850       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12851         narrow = FALSE;
12852       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12853         narrow = FALSE;
12854       if (inst.operands[2].isreg
12855           && (inst.operands[1].reg != inst.operands[0].reg
12856               || inst.operands[2].reg > 7))
12857         narrow = FALSE;
12858       if (inst.size_req == 4)
12859         narrow = FALSE;
12860
12861       reject_bad_reg (inst.operands[0].reg);
12862       reject_bad_reg (inst.operands[1].reg);
12863
12864       if (!narrow)
12865         {
12866           if (inst.operands[2].isreg)
12867             {
12868               reject_bad_reg (inst.operands[2].reg);
12869               inst.instruction = THUMB_OP32 (inst.instruction);
12870               inst.instruction |= inst.operands[0].reg << 8;
12871               inst.instruction |= inst.operands[1].reg << 16;
12872               inst.instruction |= inst.operands[2].reg;
12873
12874               /* PR 12854: Error on extraneous shifts.  */
12875               constraint (inst.operands[2].shifted,
12876                           _("extraneous shift as part of operand to shift insn"));
12877             }
12878           else
12879             {
12880               inst.operands[1].shifted = 1;
12881               inst.operands[1].shift_kind = shift_kind;
12882               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12883                                              ? T_MNEM_movs : T_MNEM_mov);
12884               inst.instruction |= inst.operands[0].reg << 8;
12885               encode_thumb32_shifted_operand (1);
12886               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12887               inst.reloc.type = BFD_RELOC_UNUSED;
12888             }
12889         }
12890       else
12891         {
12892           if (inst.operands[2].isreg)
12893             {
12894               switch (shift_kind)
12895                 {
12896                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12897                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12898                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12899                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12900                 default: abort ();
12901                 }
12902
12903               inst.instruction |= inst.operands[0].reg;
12904               inst.instruction |= inst.operands[2].reg << 3;
12905
12906               /* PR 12854: Error on extraneous shifts.  */
12907               constraint (inst.operands[2].shifted,
12908                           _("extraneous shift as part of operand to shift insn"));
12909             }
12910           else
12911             {
12912               switch (shift_kind)
12913                 {
12914                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12915                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12916                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12917                 default: abort ();
12918                 }
12919               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12920               inst.instruction |= inst.operands[0].reg;
12921               inst.instruction |= inst.operands[1].reg << 3;
12922             }
12923         }
12924     }
12925   else
12926     {
12927       constraint (inst.operands[0].reg > 7
12928                   || inst.operands[1].reg > 7, BAD_HIREG);
12929       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12930
12931       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12932         {
12933           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12934           constraint (inst.operands[0].reg != inst.operands[1].reg,
12935                       _("source1 and dest must be same register"));
12936
12937           switch (inst.instruction)
12938             {
12939             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12940             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12941             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12942             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12943             default: abort ();
12944             }
12945
12946           inst.instruction |= inst.operands[0].reg;
12947           inst.instruction |= inst.operands[2].reg << 3;
12948
12949           /* PR 12854: Error on extraneous shifts.  */
12950           constraint (inst.operands[2].shifted,
12951                       _("extraneous shift as part of operand to shift insn"));
12952         }
12953       else
12954         {
12955           switch (inst.instruction)
12956             {
12957             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12958             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12959             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12960             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12961             default: abort ();
12962             }
12963           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12964           inst.instruction |= inst.operands[0].reg;
12965           inst.instruction |= inst.operands[1].reg << 3;
12966         }
12967     }
12968 }
12969
12970 static void
12971 do_t_simd (void)
12972 {
12973   unsigned Rd, Rn, Rm;
12974
12975   Rd = inst.operands[0].reg;
12976   Rn = inst.operands[1].reg;
12977   Rm = inst.operands[2].reg;
12978
12979   reject_bad_reg (Rd);
12980   reject_bad_reg (Rn);
12981   reject_bad_reg (Rm);
12982
12983   inst.instruction |= Rd << 8;
12984   inst.instruction |= Rn << 16;
12985   inst.instruction |= Rm;
12986 }
12987
12988 static void
12989 do_t_simd2 (void)
12990 {
12991   unsigned Rd, Rn, Rm;
12992
12993   Rd = inst.operands[0].reg;
12994   Rm = inst.operands[1].reg;
12995   Rn = inst.operands[2].reg;
12996
12997   reject_bad_reg (Rd);
12998   reject_bad_reg (Rn);
12999   reject_bad_reg (Rm);
13000
13001   inst.instruction |= Rd << 8;
13002   inst.instruction |= Rn << 16;
13003   inst.instruction |= Rm;
13004 }
13005
13006 static void
13007 do_t_smc (void)
13008 {
13009   unsigned int value = inst.reloc.exp.X_add_number;
13010   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
13011               _("SMC is not permitted on this architecture"));
13012   constraint (inst.reloc.exp.X_op != O_constant,
13013               _("expression too complex"));
13014   inst.reloc.type = BFD_RELOC_UNUSED;
13015   inst.instruction |= (value & 0xf000) >> 12;
13016   inst.instruction |= (value & 0x0ff0);
13017   inst.instruction |= (value & 0x000f) << 16;
13018   /* PR gas/15623: SMC instructions must be last in an IT block.  */
13019   set_it_insn_type_last ();
13020 }
13021
13022 static void
13023 do_t_hvc (void)
13024 {
13025   unsigned int value = inst.reloc.exp.X_add_number;
13026
13027   inst.reloc.type = BFD_RELOC_UNUSED;
13028   inst.instruction |= (value & 0x0fff);
13029   inst.instruction |= (value & 0xf000) << 4;
13030 }
13031
13032 static void
13033 do_t_ssat_usat (int bias)
13034 {
13035   unsigned Rd, Rn;
13036
13037   Rd = inst.operands[0].reg;
13038   Rn = inst.operands[2].reg;
13039
13040   reject_bad_reg (Rd);
13041   reject_bad_reg (Rn);
13042
13043   inst.instruction |= Rd << 8;
13044   inst.instruction |= inst.operands[1].imm - bias;
13045   inst.instruction |= Rn << 16;
13046
13047   if (inst.operands[3].present)
13048     {
13049       offsetT shift_amount = inst.reloc.exp.X_add_number;
13050
13051       inst.reloc.type = BFD_RELOC_UNUSED;
13052
13053       constraint (inst.reloc.exp.X_op != O_constant,
13054                   _("expression too complex"));
13055
13056       if (shift_amount != 0)
13057         {
13058           constraint (shift_amount > 31,
13059                       _("shift expression is too large"));
13060
13061           if (inst.operands[3].shift_kind == SHIFT_ASR)
13062             inst.instruction |= 0x00200000;  /* sh bit.  */
13063
13064           inst.instruction |= (shift_amount & 0x1c) << 10;
13065           inst.instruction |= (shift_amount & 0x03) << 6;
13066         }
13067     }
13068 }
13069
13070 static void
13071 do_t_ssat (void)
13072 {
13073   do_t_ssat_usat (1);
13074 }
13075
13076 static void
13077 do_t_ssat16 (void)
13078 {
13079   unsigned Rd, Rn;
13080
13081   Rd = inst.operands[0].reg;
13082   Rn = inst.operands[2].reg;
13083
13084   reject_bad_reg (Rd);
13085   reject_bad_reg (Rn);
13086
13087   inst.instruction |= Rd << 8;
13088   inst.instruction |= inst.operands[1].imm - 1;
13089   inst.instruction |= Rn << 16;
13090 }
13091
13092 static void
13093 do_t_strex (void)
13094 {
13095   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
13096               || inst.operands[2].postind || inst.operands[2].writeback
13097               || inst.operands[2].immisreg || inst.operands[2].shifted
13098               || inst.operands[2].negative,
13099               BAD_ADDR_MODE);
13100
13101   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
13102
13103   inst.instruction |= inst.operands[0].reg << 8;
13104   inst.instruction |= inst.operands[1].reg << 12;
13105   inst.instruction |= inst.operands[2].reg << 16;
13106   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
13107 }
13108
13109 static void
13110 do_t_strexd (void)
13111 {
13112   if (!inst.operands[2].present)
13113     inst.operands[2].reg = inst.operands[1].reg + 1;
13114
13115   constraint (inst.operands[0].reg == inst.operands[1].reg
13116               || inst.operands[0].reg == inst.operands[2].reg
13117               || inst.operands[0].reg == inst.operands[3].reg,
13118               BAD_OVERLAP);
13119
13120   inst.instruction |= inst.operands[0].reg;
13121   inst.instruction |= inst.operands[1].reg << 12;
13122   inst.instruction |= inst.operands[2].reg << 8;
13123   inst.instruction |= inst.operands[3].reg << 16;
13124 }
13125
13126 static void
13127 do_t_sxtah (void)
13128 {
13129   unsigned Rd, Rn, Rm;
13130
13131   Rd = inst.operands[0].reg;
13132   Rn = inst.operands[1].reg;
13133   Rm = inst.operands[2].reg;
13134
13135   reject_bad_reg (Rd);
13136   reject_bad_reg (Rn);
13137   reject_bad_reg (Rm);
13138
13139   inst.instruction |= Rd << 8;
13140   inst.instruction |= Rn << 16;
13141   inst.instruction |= Rm;
13142   inst.instruction |= inst.operands[3].imm << 4;
13143 }
13144
13145 static void
13146 do_t_sxth (void)
13147 {
13148   unsigned Rd, Rm;
13149
13150   Rd = inst.operands[0].reg;
13151   Rm = inst.operands[1].reg;
13152
13153   reject_bad_reg (Rd);
13154   reject_bad_reg (Rm);
13155
13156   if (inst.instruction <= 0xffff
13157       && inst.size_req != 4
13158       && Rd <= 7 && Rm <= 7
13159       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13160     {
13161       inst.instruction = THUMB_OP16 (inst.instruction);
13162       inst.instruction |= Rd;
13163       inst.instruction |= Rm << 3;
13164     }
13165   else if (unified_syntax)
13166     {
13167       if (inst.instruction <= 0xffff)
13168         inst.instruction = THUMB_OP32 (inst.instruction);
13169       inst.instruction |= Rd << 8;
13170       inst.instruction |= Rm;
13171       inst.instruction |= inst.operands[2].imm << 4;
13172     }
13173   else
13174     {
13175       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13176                   _("Thumb encoding does not support rotation"));
13177       constraint (1, BAD_HIREG);
13178     }
13179 }
13180
13181 static void
13182 do_t_swi (void)
13183 {
13184   inst.reloc.type = BFD_RELOC_ARM_SWI;
13185 }
13186
13187 static void
13188 do_t_tb (void)
13189 {
13190   unsigned Rn, Rm;
13191   int half;
13192
13193   half = (inst.instruction & 0x10) != 0;
13194   set_it_insn_type_last ();
13195   constraint (inst.operands[0].immisreg,
13196               _("instruction requires register index"));
13197
13198   Rn = inst.operands[0].reg;
13199   Rm = inst.operands[0].imm;
13200
13201   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
13202     constraint (Rn == REG_SP, BAD_SP);
13203   reject_bad_reg (Rm);
13204
13205   constraint (!half && inst.operands[0].shifted,
13206               _("instruction does not allow shifted index"));
13207   inst.instruction |= (Rn << 16) | Rm;
13208 }
13209
13210 static void
13211 do_t_udf (void)
13212 {
13213   if (!inst.operands[0].present)
13214     inst.operands[0].imm = 0;
13215
13216   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13217     {
13218       constraint (inst.size_req == 2,
13219                   _("immediate value out of range"));
13220       inst.instruction = THUMB_OP32 (inst.instruction);
13221       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13222       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13223     }
13224   else
13225     {
13226       inst.instruction = THUMB_OP16 (inst.instruction);
13227       inst.instruction |= inst.operands[0].imm;
13228     }
13229
13230   set_it_insn_type (NEUTRAL_IT_INSN);
13231 }
13232
13233
13234 static void
13235 do_t_usat (void)
13236 {
13237   do_t_ssat_usat (0);
13238 }
13239
13240 static void
13241 do_t_usat16 (void)
13242 {
13243   unsigned Rd, Rn;
13244
13245   Rd = inst.operands[0].reg;
13246   Rn = inst.operands[2].reg;
13247
13248   reject_bad_reg (Rd);
13249   reject_bad_reg (Rn);
13250
13251   inst.instruction |= Rd << 8;
13252   inst.instruction |= inst.operands[1].imm;
13253   inst.instruction |= Rn << 16;
13254 }
13255
13256 /* Neon instruction encoder helpers.  */
13257
13258 /* Encodings for the different types for various Neon opcodes.  */
13259
13260 /* An "invalid" code for the following tables.  */
13261 #define N_INV -1u
13262
13263 struct neon_tab_entry
13264 {
13265   unsigned integer;
13266   unsigned float_or_poly;
13267   unsigned scalar_or_imm;
13268 };
13269
13270 /* Map overloaded Neon opcodes to their respective encodings.  */
13271 #define NEON_ENC_TAB                                    \
13272   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13273   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13274   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13275   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13276   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13277   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13278   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13279   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13280   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13281   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13282   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13283   /* Register variants of the following two instructions are encoded as
13284      vcge / vcgt with the operands reversed.  */        \
13285   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13286   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13287   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13288   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13289   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13290   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13291   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13292   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13293   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13294   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13295   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13296   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13297   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13298   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13299   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13300   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13301   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13302   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13303   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13304   X(vand,       0x0000110, N_INV,     0x0800030),       \
13305   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13306   X(veor,       0x1000110, N_INV,     N_INV),           \
13307   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13308   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13309   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13310   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13311   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13312   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13313   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13314   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13315   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13316   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13317   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13318   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13319   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13320   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13321   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13322   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13323   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13324   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13325   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13326   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13327   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13328   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13329   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13330   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13331   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13332   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13333   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13334   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13335   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13336   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13337   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13338   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13339   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13340   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13341   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13342   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13343   X(aes,        0x3b00300, N_INV,     N_INV),           \
13344   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13345   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13346   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13347
13348 enum neon_opc
13349 {
13350 #define X(OPC,I,F,S) N_MNEM_##OPC
13351 NEON_ENC_TAB
13352 #undef X
13353 };
13354
13355 static const struct neon_tab_entry neon_enc_tab[] =
13356 {
13357 #define X(OPC,I,F,S) { (I), (F), (S) }
13358 NEON_ENC_TAB
13359 #undef X
13360 };
13361
13362 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13363 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13364 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13365 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13366 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13367 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13368 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13369 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13370 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13371 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13372 #define NEON_ENC_SINGLE_(X) \
13373   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13374 #define NEON_ENC_DOUBLE_(X) \
13375   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13376 #define NEON_ENC_FPV8_(X) \
13377   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13378
13379 #define NEON_ENCODE(type, inst)                                 \
13380   do                                                            \
13381     {                                                           \
13382       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13383       inst.is_neon = 1;                                         \
13384     }                                                           \
13385   while (0)
13386
13387 #define check_neon_suffixes                                             \
13388   do                                                                    \
13389     {                                                                   \
13390       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13391         {                                                               \
13392           as_bad (_("invalid neon suffix for non neon instruction"));   \
13393           return;                                                       \
13394         }                                                               \
13395     }                                                                   \
13396   while (0)
13397
13398 /* Define shapes for instruction operands. The following mnemonic characters
13399    are used in this table:
13400
13401      F - VFP S<n> register
13402      D - Neon D<n> register
13403      Q - Neon Q<n> register
13404      I - Immediate
13405      S - Scalar
13406      R - ARM register
13407      L - D<n> register list
13408
13409    This table is used to generate various data:
13410      - enumerations of the form NS_DDR to be used as arguments to
13411        neon_select_shape.
13412      - a table classifying shapes into single, double, quad, mixed.
13413      - a table used to drive neon_select_shape.  */
13414
13415 #define NEON_SHAPE_DEF                  \
13416   X(3, (D, D, D), DOUBLE),              \
13417   X(3, (Q, Q, Q), QUAD),                \
13418   X(3, (D, D, I), DOUBLE),              \
13419   X(3, (Q, Q, I), QUAD),                \
13420   X(3, (D, D, S), DOUBLE),              \
13421   X(3, (Q, Q, S), QUAD),                \
13422   X(2, (D, D), DOUBLE),                 \
13423   X(2, (Q, Q), QUAD),                   \
13424   X(2, (D, S), DOUBLE),                 \
13425   X(2, (Q, S), QUAD),                   \
13426   X(2, (D, R), DOUBLE),                 \
13427   X(2, (Q, R), QUAD),                   \
13428   X(2, (D, I), DOUBLE),                 \
13429   X(2, (Q, I), QUAD),                   \
13430   X(3, (D, L, D), DOUBLE),              \
13431   X(2, (D, Q), MIXED),                  \
13432   X(2, (Q, D), MIXED),                  \
13433   X(3, (D, Q, I), MIXED),               \
13434   X(3, (Q, D, I), MIXED),               \
13435   X(3, (Q, D, D), MIXED),               \
13436   X(3, (D, Q, Q), MIXED),               \
13437   X(3, (Q, Q, D), MIXED),               \
13438   X(3, (Q, D, S), MIXED),               \
13439   X(3, (D, Q, S), MIXED),               \
13440   X(4, (D, D, D, I), DOUBLE),           \
13441   X(4, (Q, Q, Q, I), QUAD),             \
13442   X(4, (D, D, S, I), DOUBLE),           \
13443   X(4, (Q, Q, S, I), QUAD),             \
13444   X(2, (F, F), SINGLE),                 \
13445   X(3, (F, F, F), SINGLE),              \
13446   X(2, (F, I), SINGLE),                 \
13447   X(2, (F, D), MIXED),                  \
13448   X(2, (D, F), MIXED),                  \
13449   X(3, (F, F, I), MIXED),               \
13450   X(4, (R, R, F, F), SINGLE),           \
13451   X(4, (F, F, R, R), SINGLE),           \
13452   X(3, (D, R, R), DOUBLE),              \
13453   X(3, (R, R, D), DOUBLE),              \
13454   X(2, (S, R), SINGLE),                 \
13455   X(2, (R, S), SINGLE),                 \
13456   X(2, (F, R), SINGLE),                 \
13457   X(2, (R, F), SINGLE),                 \
13458 /* Half float shape supported so far.  */\
13459   X (2, (H, D), MIXED),                 \
13460   X (2, (D, H), MIXED),                 \
13461   X (2, (H, F), MIXED),                 \
13462   X (2, (F, H), MIXED),                 \
13463   X (2, (H, H), HALF),                  \
13464   X (2, (H, R), HALF),                  \
13465   X (2, (R, H), HALF),                  \
13466   X (2, (H, I), HALF),                  \
13467   X (3, (H, H, H), HALF),               \
13468   X (3, (H, F, I), MIXED),              \
13469   X (3, (F, H, I), MIXED),              \
13470   X (3, (D, H, H), MIXED),              \
13471   X (3, (D, H, S), MIXED)
13472
13473 #define S2(A,B)         NS_##A##B
13474 #define S3(A,B,C)       NS_##A##B##C
13475 #define S4(A,B,C,D)     NS_##A##B##C##D
13476
13477 #define X(N, L, C) S##N L
13478
13479 enum neon_shape
13480 {
13481   NEON_SHAPE_DEF,
13482   NS_NULL
13483 };
13484
13485 #undef X
13486 #undef S2
13487 #undef S3
13488 #undef S4
13489
13490 enum neon_shape_class
13491 {
13492   SC_HALF,
13493   SC_SINGLE,
13494   SC_DOUBLE,
13495   SC_QUAD,
13496   SC_MIXED
13497 };
13498
13499 #define X(N, L, C) SC_##C
13500
13501 static enum neon_shape_class neon_shape_class[] =
13502 {
13503   NEON_SHAPE_DEF
13504 };
13505
13506 #undef X
13507
13508 enum neon_shape_el
13509 {
13510   SE_H,
13511   SE_F,
13512   SE_D,
13513   SE_Q,
13514   SE_I,
13515   SE_S,
13516   SE_R,
13517   SE_L
13518 };
13519
13520 /* Register widths of above.  */
13521 static unsigned neon_shape_el_size[] =
13522 {
13523   16,
13524   32,
13525   64,
13526   128,
13527   0,
13528   32,
13529   32,
13530   0
13531 };
13532
13533 struct neon_shape_info
13534 {
13535   unsigned els;
13536   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13537 };
13538
13539 #define S2(A,B)         { SE_##A, SE_##B }
13540 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13541 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13542
13543 #define X(N, L, C) { N, S##N L }
13544
13545 static struct neon_shape_info neon_shape_tab[] =
13546 {
13547   NEON_SHAPE_DEF
13548 };
13549
13550 #undef X
13551 #undef S2
13552 #undef S3
13553 #undef S4
13554
13555 /* Bit masks used in type checking given instructions.
13556   'N_EQK' means the type must be the same as (or based on in some way) the key
13557    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13558    set, various other bits can be set as well in order to modify the meaning of
13559    the type constraint.  */
13560
13561 enum neon_type_mask
13562 {
13563   N_S8   = 0x0000001,
13564   N_S16  = 0x0000002,
13565   N_S32  = 0x0000004,
13566   N_S64  = 0x0000008,
13567   N_U8   = 0x0000010,
13568   N_U16  = 0x0000020,
13569   N_U32  = 0x0000040,
13570   N_U64  = 0x0000080,
13571   N_I8   = 0x0000100,
13572   N_I16  = 0x0000200,
13573   N_I32  = 0x0000400,
13574   N_I64  = 0x0000800,
13575   N_8    = 0x0001000,
13576   N_16   = 0x0002000,
13577   N_32   = 0x0004000,
13578   N_64   = 0x0008000,
13579   N_P8   = 0x0010000,
13580   N_P16  = 0x0020000,
13581   N_F16  = 0x0040000,
13582   N_F32  = 0x0080000,
13583   N_F64  = 0x0100000,
13584   N_P64  = 0x0200000,
13585   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13586   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13587   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13588   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13589   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13590   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13591   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13592   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13593   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13594   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13595   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13596   N_UTYP = 0,
13597   N_MAX_NONSPECIAL = N_P64
13598 };
13599
13600 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13601
13602 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13603 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13604 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13605 #define N_S_32     (N_S8 | N_S16 | N_S32)
13606 #define N_F_16_32  (N_F16 | N_F32)
13607 #define N_SUF_32   (N_SU_32 | N_F_16_32)
13608 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13609 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
13610 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
13611
13612 /* Pass this as the first type argument to neon_check_type to ignore types
13613    altogether.  */
13614 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13615
13616 /* Select a "shape" for the current instruction (describing register types or
13617    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13618    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13619    function of operand parsing, so this function doesn't need to be called.
13620    Shapes should be listed in order of decreasing length.  */
13621
13622 static enum neon_shape
13623 neon_select_shape (enum neon_shape shape, ...)
13624 {
13625   va_list ap;
13626   enum neon_shape first_shape = shape;
13627
13628   /* Fix missing optional operands. FIXME: we don't know at this point how
13629      many arguments we should have, so this makes the assumption that we have
13630      > 1. This is true of all current Neon opcodes, I think, but may not be
13631      true in the future.  */
13632   if (!inst.operands[1].present)
13633     inst.operands[1] = inst.operands[0];
13634
13635   va_start (ap, shape);
13636
13637   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13638     {
13639       unsigned j;
13640       int matches = 1;
13641
13642       for (j = 0; j < neon_shape_tab[shape].els; j++)
13643         {
13644           if (!inst.operands[j].present)
13645             {
13646               matches = 0;
13647               break;
13648             }
13649
13650           switch (neon_shape_tab[shape].el[j])
13651             {
13652               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
13653                  a VFP single precision register operand, it's essentially
13654                  means only half of the register is used.
13655
13656                  If the type specifier is given after the mnemonics, the
13657                  information is stored in inst.vectype.  If the type specifier
13658                  is given after register operand, the information is stored
13659                  in inst.operands[].vectype.
13660
13661                  When there is only one type specifier, and all the register
13662                  operands are the same type of hardware register, the type
13663                  specifier applies to all register operands.
13664
13665                  If no type specifier is given, the shape is inferred from
13666                  operand information.
13667
13668                  for example:
13669                  vadd.f16 s0, s1, s2:           NS_HHH
13670                  vabs.f16 s0, s1:               NS_HH
13671                  vmov.f16 s0, r1:               NS_HR
13672                  vmov.f16 r0, s1:               NS_RH
13673                  vcvt.f16 r0, s1:               NS_RH
13674                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
13675                  vcvt.f16.s32   s2, s2:         NS_HF
13676               */
13677             case SE_H:
13678               if (!(inst.operands[j].isreg
13679                     && inst.operands[j].isvec
13680                     && inst.operands[j].issingle
13681                     && !inst.operands[j].isquad
13682                     && ((inst.vectype.elems == 1
13683                          && inst.vectype.el[0].size == 16)
13684                         || (inst.vectype.elems > 1
13685                             && inst.vectype.el[j].size == 16)
13686                         || (inst.vectype.elems == 0
13687                             && inst.operands[j].vectype.type != NT_invtype
13688                             && inst.operands[j].vectype.size == 16))))
13689                 matches = 0;
13690               break;
13691
13692             case SE_F:
13693               if (!(inst.operands[j].isreg
13694                     && inst.operands[j].isvec
13695                     && inst.operands[j].issingle
13696                     && !inst.operands[j].isquad
13697                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
13698                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
13699                         || (inst.vectype.elems == 0
13700                             && (inst.operands[j].vectype.size == 32
13701                                 || inst.operands[j].vectype.type == NT_invtype)))))
13702                 matches = 0;
13703               break;
13704
13705             case SE_D:
13706               if (!(inst.operands[j].isreg
13707                     && inst.operands[j].isvec
13708                     && !inst.operands[j].isquad
13709                     && !inst.operands[j].issingle))
13710                 matches = 0;
13711               break;
13712
13713             case SE_R:
13714               if (!(inst.operands[j].isreg
13715                     && !inst.operands[j].isvec))
13716                 matches = 0;
13717               break;
13718
13719             case SE_Q:
13720               if (!(inst.operands[j].isreg
13721                     && inst.operands[j].isvec
13722                     && inst.operands[j].isquad
13723                     && !inst.operands[j].issingle))
13724                 matches = 0;
13725               break;
13726
13727             case SE_I:
13728               if (!(!inst.operands[j].isreg
13729                     && !inst.operands[j].isscalar))
13730                 matches = 0;
13731               break;
13732
13733             case SE_S:
13734               if (!(!inst.operands[j].isreg
13735                     && inst.operands[j].isscalar))
13736                 matches = 0;
13737               break;
13738
13739             case SE_L:
13740               break;
13741             }
13742           if (!matches)
13743             break;
13744         }
13745       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13746         /* We've matched all the entries in the shape table, and we don't
13747            have any left over operands which have not been matched.  */
13748         break;
13749     }
13750
13751   va_end (ap);
13752
13753   if (shape == NS_NULL && first_shape != NS_NULL)
13754     first_error (_("invalid instruction shape"));
13755
13756   return shape;
13757 }
13758
13759 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13760    means the Q bit should be set).  */
13761
13762 static int
13763 neon_quad (enum neon_shape shape)
13764 {
13765   return neon_shape_class[shape] == SC_QUAD;
13766 }
13767
13768 static void
13769 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13770                        unsigned *g_size)
13771 {
13772   /* Allow modification to be made to types which are constrained to be
13773      based on the key element, based on bits set alongside N_EQK.  */
13774   if ((typebits & N_EQK) != 0)
13775     {
13776       if ((typebits & N_HLF) != 0)
13777         *g_size /= 2;
13778       else if ((typebits & N_DBL) != 0)
13779         *g_size *= 2;
13780       if ((typebits & N_SGN) != 0)
13781         *g_type = NT_signed;
13782       else if ((typebits & N_UNS) != 0)
13783         *g_type = NT_unsigned;
13784       else if ((typebits & N_INT) != 0)
13785         *g_type = NT_integer;
13786       else if ((typebits & N_FLT) != 0)
13787         *g_type = NT_float;
13788       else if ((typebits & N_SIZ) != 0)
13789         *g_type = NT_untyped;
13790     }
13791 }
13792
13793 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13794    operand type, i.e. the single type specified in a Neon instruction when it
13795    is the only one given.  */
13796
13797 static struct neon_type_el
13798 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13799 {
13800   struct neon_type_el dest = *key;
13801
13802   gas_assert ((thisarg & N_EQK) != 0);
13803
13804   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13805
13806   return dest;
13807 }
13808
13809 /* Convert Neon type and size into compact bitmask representation.  */
13810
13811 static enum neon_type_mask
13812 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13813 {
13814   switch (type)
13815     {
13816     case NT_untyped:
13817       switch (size)
13818         {
13819         case 8:  return N_8;
13820         case 16: return N_16;
13821         case 32: return N_32;
13822         case 64: return N_64;
13823         default: ;
13824         }
13825       break;
13826
13827     case NT_integer:
13828       switch (size)
13829         {
13830         case 8:  return N_I8;
13831         case 16: return N_I16;
13832         case 32: return N_I32;
13833         case 64: return N_I64;
13834         default: ;
13835         }
13836       break;
13837
13838     case NT_float:
13839       switch (size)
13840         {
13841         case 16: return N_F16;
13842         case 32: return N_F32;
13843         case 64: return N_F64;
13844         default: ;
13845         }
13846       break;
13847
13848     case NT_poly:
13849       switch (size)
13850         {
13851         case 8:  return N_P8;
13852         case 16: return N_P16;
13853         case 64: return N_P64;
13854         default: ;
13855         }
13856       break;
13857
13858     case NT_signed:
13859       switch (size)
13860         {
13861         case 8:  return N_S8;
13862         case 16: return N_S16;
13863         case 32: return N_S32;
13864         case 64: return N_S64;
13865         default: ;
13866         }
13867       break;
13868
13869     case NT_unsigned:
13870       switch (size)
13871         {
13872         case 8:  return N_U8;
13873         case 16: return N_U16;
13874         case 32: return N_U32;
13875         case 64: return N_U64;
13876         default: ;
13877         }
13878       break;
13879
13880     default: ;
13881     }
13882
13883   return N_UTYP;
13884 }
13885
13886 /* Convert compact Neon bitmask type representation to a type and size. Only
13887    handles the case where a single bit is set in the mask.  */
13888
13889 static int
13890 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13891                      enum neon_type_mask mask)
13892 {
13893   if ((mask & N_EQK) != 0)
13894     return FAIL;
13895
13896   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13897     *size = 8;
13898   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13899     *size = 16;
13900   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13901     *size = 32;
13902   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13903     *size = 64;
13904   else
13905     return FAIL;
13906
13907   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13908     *type = NT_signed;
13909   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13910     *type = NT_unsigned;
13911   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13912     *type = NT_integer;
13913   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13914     *type = NT_untyped;
13915   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13916     *type = NT_poly;
13917   else if ((mask & (N_F_ALL)) != 0)
13918     *type = NT_float;
13919   else
13920     return FAIL;
13921
13922   return SUCCESS;
13923 }
13924
13925 /* Modify a bitmask of allowed types. This is only needed for type
13926    relaxation.  */
13927
13928 static unsigned
13929 modify_types_allowed (unsigned allowed, unsigned mods)
13930 {
13931   unsigned size;
13932   enum neon_el_type type;
13933   unsigned destmask;
13934   int i;
13935
13936   destmask = 0;
13937
13938   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13939     {
13940       if (el_type_of_type_chk (&type, &size,
13941                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13942         {
13943           neon_modify_type_size (mods, &type, &size);
13944           destmask |= type_chk_of_el_type (type, size);
13945         }
13946     }
13947
13948   return destmask;
13949 }
13950
13951 /* Check type and return type classification.
13952    The manual states (paraphrase): If one datatype is given, it indicates the
13953    type given in:
13954     - the second operand, if there is one
13955     - the operand, if there is no second operand
13956     - the result, if there are no operands.
13957    This isn't quite good enough though, so we use a concept of a "key" datatype
13958    which is set on a per-instruction basis, which is the one which matters when
13959    only one data type is written.
13960    Note: this function has side-effects (e.g. filling in missing operands). All
13961    Neon instructions should call it before performing bit encoding.  */
13962
13963 static struct neon_type_el
13964 neon_check_type (unsigned els, enum neon_shape ns, ...)
13965 {
13966   va_list ap;
13967   unsigned i, pass, key_el = 0;
13968   unsigned types[NEON_MAX_TYPE_ELS];
13969   enum neon_el_type k_type = NT_invtype;
13970   unsigned k_size = -1u;
13971   struct neon_type_el badtype = {NT_invtype, -1};
13972   unsigned key_allowed = 0;
13973
13974   /* Optional registers in Neon instructions are always (not) in operand 1.
13975      Fill in the missing operand here, if it was omitted.  */
13976   if (els > 1 && !inst.operands[1].present)
13977     inst.operands[1] = inst.operands[0];
13978
13979   /* Suck up all the varargs.  */
13980   va_start (ap, ns);
13981   for (i = 0; i < els; i++)
13982     {
13983       unsigned thisarg = va_arg (ap, unsigned);
13984       if (thisarg == N_IGNORE_TYPE)
13985         {
13986           va_end (ap);
13987           return badtype;
13988         }
13989       types[i] = thisarg;
13990       if ((thisarg & N_KEY) != 0)
13991         key_el = i;
13992     }
13993   va_end (ap);
13994
13995   if (inst.vectype.elems > 0)
13996     for (i = 0; i < els; i++)
13997       if (inst.operands[i].vectype.type != NT_invtype)
13998         {
13999           first_error (_("types specified in both the mnemonic and operands"));
14000           return badtype;
14001         }
14002
14003   /* Duplicate inst.vectype elements here as necessary.
14004      FIXME: No idea if this is exactly the same as the ARM assembler,
14005      particularly when an insn takes one register and one non-register
14006      operand. */
14007   if (inst.vectype.elems == 1 && els > 1)
14008     {
14009       unsigned j;
14010       inst.vectype.elems = els;
14011       inst.vectype.el[key_el] = inst.vectype.el[0];
14012       for (j = 0; j < els; j++)
14013         if (j != key_el)
14014           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14015                                                   types[j]);
14016     }
14017   else if (inst.vectype.elems == 0 && els > 0)
14018     {
14019       unsigned j;
14020       /* No types were given after the mnemonic, so look for types specified
14021          after each operand. We allow some flexibility here; as long as the
14022          "key" operand has a type, we can infer the others.  */
14023       for (j = 0; j < els; j++)
14024         if (inst.operands[j].vectype.type != NT_invtype)
14025           inst.vectype.el[j] = inst.operands[j].vectype;
14026
14027       if (inst.operands[key_el].vectype.type != NT_invtype)
14028         {
14029           for (j = 0; j < els; j++)
14030             if (inst.operands[j].vectype.type == NT_invtype)
14031               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
14032                                                       types[j]);
14033         }
14034       else
14035         {
14036           first_error (_("operand types can't be inferred"));
14037           return badtype;
14038         }
14039     }
14040   else if (inst.vectype.elems != els)
14041     {
14042       first_error (_("type specifier has the wrong number of parts"));
14043       return badtype;
14044     }
14045
14046   for (pass = 0; pass < 2; pass++)
14047     {
14048       for (i = 0; i < els; i++)
14049         {
14050           unsigned thisarg = types[i];
14051           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
14052             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
14053           enum neon_el_type g_type = inst.vectype.el[i].type;
14054           unsigned g_size = inst.vectype.el[i].size;
14055
14056           /* Decay more-specific signed & unsigned types to sign-insensitive
14057              integer types if sign-specific variants are unavailable.  */
14058           if ((g_type == NT_signed || g_type == NT_unsigned)
14059               && (types_allowed & N_SU_ALL) == 0)
14060             g_type = NT_integer;
14061
14062           /* If only untyped args are allowed, decay any more specific types to
14063              them. Some instructions only care about signs for some element
14064              sizes, so handle that properly.  */
14065           if (((types_allowed & N_UNT) == 0)
14066               && ((g_size == 8 && (types_allowed & N_8) != 0)
14067                   || (g_size == 16 && (types_allowed & N_16) != 0)
14068                   || (g_size == 32 && (types_allowed & N_32) != 0)
14069                   || (g_size == 64 && (types_allowed & N_64) != 0)))
14070             g_type = NT_untyped;
14071
14072           if (pass == 0)
14073             {
14074               if ((thisarg & N_KEY) != 0)
14075                 {
14076                   k_type = g_type;
14077                   k_size = g_size;
14078                   key_allowed = thisarg & ~N_KEY;
14079
14080                   /* Check architecture constraint on FP16 extension.  */
14081                   if (k_size == 16
14082                       && k_type == NT_float
14083                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14084                     {
14085                       inst.error = _(BAD_FP16);
14086                       return badtype;
14087                     }
14088                 }
14089             }
14090           else
14091             {
14092               if ((thisarg & N_VFP) != 0)
14093                 {
14094                   enum neon_shape_el regshape;
14095                   unsigned regwidth, match;
14096
14097                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
14098                   if (ns == NS_NULL)
14099                     {
14100                       first_error (_("invalid instruction shape"));
14101                       return badtype;
14102                     }
14103                   regshape = neon_shape_tab[ns].el[i];
14104                   regwidth = neon_shape_el_size[regshape];
14105
14106                   /* In VFP mode, operands must match register widths. If we
14107                      have a key operand, use its width, else use the width of
14108                      the current operand.  */
14109                   if (k_size != -1u)
14110                     match = k_size;
14111                   else
14112                     match = g_size;
14113
14114                   /* FP16 will use a single precision register.  */
14115                   if (regwidth == 32 && match == 16)
14116                     {
14117                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14118                         match = regwidth;
14119                       else
14120                         {
14121                           inst.error = _(BAD_FP16);
14122                           return badtype;
14123                         }
14124                     }
14125
14126                   if (regwidth != match)
14127                     {
14128                       first_error (_("operand size must match register width"));
14129                       return badtype;
14130                     }
14131                 }
14132
14133               if ((thisarg & N_EQK) == 0)
14134                 {
14135                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
14136
14137                   if ((given_type & types_allowed) == 0)
14138                     {
14139                       first_error (_("bad type in Neon instruction"));
14140                       return badtype;
14141                     }
14142                 }
14143               else
14144                 {
14145                   enum neon_el_type mod_k_type = k_type;
14146                   unsigned mod_k_size = k_size;
14147                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
14148                   if (g_type != mod_k_type || g_size != mod_k_size)
14149                     {
14150                       first_error (_("inconsistent types in Neon instruction"));
14151                       return badtype;
14152                     }
14153                 }
14154             }
14155         }
14156     }
14157
14158   return inst.vectype.el[key_el];
14159 }
14160
14161 /* Neon-style VFP instruction forwarding.  */
14162
14163 /* Thumb VFP instructions have 0xE in the condition field.  */
14164
14165 static void
14166 do_vfp_cond_or_thumb (void)
14167 {
14168   inst.is_neon = 1;
14169
14170   if (thumb_mode)
14171     inst.instruction |= 0xe0000000;
14172   else
14173     inst.instruction |= inst.cond << 28;
14174 }
14175
14176 /* Look up and encode a simple mnemonic, for use as a helper function for the
14177    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
14178    etc.  It is assumed that operand parsing has already been done, and that the
14179    operands are in the form expected by the given opcode (this isn't necessarily
14180    the same as the form in which they were parsed, hence some massaging must
14181    take place before this function is called).
14182    Checks current arch version against that in the looked-up opcode.  */
14183
14184 static void
14185 do_vfp_nsyn_opcode (const char *opname)
14186 {
14187   const struct asm_opcode *opcode;
14188
14189   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
14190
14191   if (!opcode)
14192     abort ();
14193
14194   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
14195                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
14196               _(BAD_FPU));
14197
14198   inst.is_neon = 1;
14199
14200   if (thumb_mode)
14201     {
14202       inst.instruction = opcode->tvalue;
14203       opcode->tencode ();
14204     }
14205   else
14206     {
14207       inst.instruction = (inst.cond << 28) | opcode->avalue;
14208       opcode->aencode ();
14209     }
14210 }
14211
14212 static void
14213 do_vfp_nsyn_add_sub (enum neon_shape rs)
14214 {
14215   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
14216
14217   if (rs == NS_FFF || rs == NS_HHH)
14218     {
14219       if (is_add)
14220         do_vfp_nsyn_opcode ("fadds");
14221       else
14222         do_vfp_nsyn_opcode ("fsubs");
14223
14224       /* ARMv8.2 fp16 instruction.  */
14225       if (rs == NS_HHH)
14226         do_scalar_fp16_v82_encode ();
14227     }
14228   else
14229     {
14230       if (is_add)
14231         do_vfp_nsyn_opcode ("faddd");
14232       else
14233         do_vfp_nsyn_opcode ("fsubd");
14234     }
14235 }
14236
14237 /* Check operand types to see if this is a VFP instruction, and if so call
14238    PFN ().  */
14239
14240 static int
14241 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
14242 {
14243   enum neon_shape rs;
14244   struct neon_type_el et;
14245
14246   switch (args)
14247     {
14248     case 2:
14249       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14250       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14251       break;
14252
14253     case 3:
14254       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14255       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14256                             N_F_ALL | N_KEY | N_VFP);
14257       break;
14258
14259     default:
14260       abort ();
14261     }
14262
14263   if (et.type != NT_invtype)
14264     {
14265       pfn (rs);
14266       return SUCCESS;
14267     }
14268
14269   inst.error = NULL;
14270   return FAIL;
14271 }
14272
14273 static void
14274 do_vfp_nsyn_mla_mls (enum neon_shape rs)
14275 {
14276   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
14277
14278   if (rs == NS_FFF || rs == NS_HHH)
14279     {
14280       if (is_mla)
14281         do_vfp_nsyn_opcode ("fmacs");
14282       else
14283         do_vfp_nsyn_opcode ("fnmacs");
14284
14285       /* ARMv8.2 fp16 instruction.  */
14286       if (rs == NS_HHH)
14287         do_scalar_fp16_v82_encode ();
14288     }
14289   else
14290     {
14291       if (is_mla)
14292         do_vfp_nsyn_opcode ("fmacd");
14293       else
14294         do_vfp_nsyn_opcode ("fnmacd");
14295     }
14296 }
14297
14298 static void
14299 do_vfp_nsyn_fma_fms (enum neon_shape rs)
14300 {
14301   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
14302
14303   if (rs == NS_FFF || rs == NS_HHH)
14304     {
14305       if (is_fma)
14306         do_vfp_nsyn_opcode ("ffmas");
14307       else
14308         do_vfp_nsyn_opcode ("ffnmas");
14309
14310       /* ARMv8.2 fp16 instruction.  */
14311       if (rs == NS_HHH)
14312         do_scalar_fp16_v82_encode ();
14313     }
14314   else
14315     {
14316       if (is_fma)
14317         do_vfp_nsyn_opcode ("ffmad");
14318       else
14319         do_vfp_nsyn_opcode ("ffnmad");
14320     }
14321 }
14322
14323 static void
14324 do_vfp_nsyn_mul (enum neon_shape rs)
14325 {
14326   if (rs == NS_FFF || rs == NS_HHH)
14327     {
14328       do_vfp_nsyn_opcode ("fmuls");
14329
14330       /* ARMv8.2 fp16 instruction.  */
14331       if (rs == NS_HHH)
14332         do_scalar_fp16_v82_encode ();
14333     }
14334   else
14335     do_vfp_nsyn_opcode ("fmuld");
14336 }
14337
14338 static void
14339 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14340 {
14341   int is_neg = (inst.instruction & 0x80) != 0;
14342   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
14343
14344   if (rs == NS_FF || rs == NS_HH)
14345     {
14346       if (is_neg)
14347         do_vfp_nsyn_opcode ("fnegs");
14348       else
14349         do_vfp_nsyn_opcode ("fabss");
14350
14351       /* ARMv8.2 fp16 instruction.  */
14352       if (rs == NS_HH)
14353         do_scalar_fp16_v82_encode ();
14354     }
14355   else
14356     {
14357       if (is_neg)
14358         do_vfp_nsyn_opcode ("fnegd");
14359       else
14360         do_vfp_nsyn_opcode ("fabsd");
14361     }
14362 }
14363
14364 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14365    insns belong to Neon, and are handled elsewhere.  */
14366
14367 static void
14368 do_vfp_nsyn_ldm_stm (int is_dbmode)
14369 {
14370   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14371   if (is_ldm)
14372     {
14373       if (is_dbmode)
14374         do_vfp_nsyn_opcode ("fldmdbs");
14375       else
14376         do_vfp_nsyn_opcode ("fldmias");
14377     }
14378   else
14379     {
14380       if (is_dbmode)
14381         do_vfp_nsyn_opcode ("fstmdbs");
14382       else
14383         do_vfp_nsyn_opcode ("fstmias");
14384     }
14385 }
14386
14387 static void
14388 do_vfp_nsyn_sqrt (void)
14389 {
14390   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14391   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14392
14393   if (rs == NS_FF || rs == NS_HH)
14394     {
14395       do_vfp_nsyn_opcode ("fsqrts");
14396
14397       /* ARMv8.2 fp16 instruction.  */
14398       if (rs == NS_HH)
14399         do_scalar_fp16_v82_encode ();
14400     }
14401   else
14402     do_vfp_nsyn_opcode ("fsqrtd");
14403 }
14404
14405 static void
14406 do_vfp_nsyn_div (void)
14407 {
14408   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14409   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14410                    N_F_ALL | N_KEY | N_VFP);
14411
14412   if (rs == NS_FFF || rs == NS_HHH)
14413     {
14414       do_vfp_nsyn_opcode ("fdivs");
14415
14416       /* ARMv8.2 fp16 instruction.  */
14417       if (rs == NS_HHH)
14418         do_scalar_fp16_v82_encode ();
14419     }
14420   else
14421     do_vfp_nsyn_opcode ("fdivd");
14422 }
14423
14424 static void
14425 do_vfp_nsyn_nmul (void)
14426 {
14427   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14428   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14429                    N_F_ALL | N_KEY | N_VFP);
14430
14431   if (rs == NS_FFF || rs == NS_HHH)
14432     {
14433       NEON_ENCODE (SINGLE, inst);
14434       do_vfp_sp_dyadic ();
14435
14436       /* ARMv8.2 fp16 instruction.  */
14437       if (rs == NS_HHH)
14438         do_scalar_fp16_v82_encode ();
14439     }
14440   else
14441     {
14442       NEON_ENCODE (DOUBLE, inst);
14443       do_vfp_dp_rd_rn_rm ();
14444     }
14445   do_vfp_cond_or_thumb ();
14446
14447 }
14448
14449 static void
14450 do_vfp_nsyn_cmp (void)
14451 {
14452   enum neon_shape rs;
14453   if (inst.operands[1].isreg)
14454     {
14455       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14456       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14457
14458       if (rs == NS_FF || rs == NS_HH)
14459         {
14460           NEON_ENCODE (SINGLE, inst);
14461           do_vfp_sp_monadic ();
14462         }
14463       else
14464         {
14465           NEON_ENCODE (DOUBLE, inst);
14466           do_vfp_dp_rd_rm ();
14467         }
14468     }
14469   else
14470     {
14471       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
14472       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
14473
14474       switch (inst.instruction & 0x0fffffff)
14475         {
14476         case N_MNEM_vcmp:
14477           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
14478           break;
14479         case N_MNEM_vcmpe:
14480           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
14481           break;
14482         default:
14483           abort ();
14484         }
14485
14486       if (rs == NS_FI || rs == NS_HI)
14487         {
14488           NEON_ENCODE (SINGLE, inst);
14489           do_vfp_sp_compare_z ();
14490         }
14491       else
14492         {
14493           NEON_ENCODE (DOUBLE, inst);
14494           do_vfp_dp_rd ();
14495         }
14496     }
14497   do_vfp_cond_or_thumb ();
14498
14499   /* ARMv8.2 fp16 instruction.  */
14500   if (rs == NS_HI || rs == NS_HH)
14501     do_scalar_fp16_v82_encode ();
14502 }
14503
14504 static void
14505 nsyn_insert_sp (void)
14506 {
14507   inst.operands[1] = inst.operands[0];
14508   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
14509   inst.operands[0].reg = REG_SP;
14510   inst.operands[0].isreg = 1;
14511   inst.operands[0].writeback = 1;
14512   inst.operands[0].present = 1;
14513 }
14514
14515 static void
14516 do_vfp_nsyn_push (void)
14517 {
14518   nsyn_insert_sp ();
14519
14520   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14521               _("register list must contain at least 1 and at most 16 "
14522                 "registers"));
14523
14524   if (inst.operands[1].issingle)
14525     do_vfp_nsyn_opcode ("fstmdbs");
14526   else
14527     do_vfp_nsyn_opcode ("fstmdbd");
14528 }
14529
14530 static void
14531 do_vfp_nsyn_pop (void)
14532 {
14533   nsyn_insert_sp ();
14534
14535   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14536               _("register list must contain at least 1 and at most 16 "
14537                 "registers"));
14538
14539   if (inst.operands[1].issingle)
14540     do_vfp_nsyn_opcode ("fldmias");
14541   else
14542     do_vfp_nsyn_opcode ("fldmiad");
14543 }
14544
14545 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14546    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14547
14548 static void
14549 neon_dp_fixup (struct arm_it* insn)
14550 {
14551   unsigned int i = insn->instruction;
14552   insn->is_neon = 1;
14553
14554   if (thumb_mode)
14555     {
14556       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14557       if (i & (1 << 24))
14558         i |= 1 << 28;
14559
14560       i &= ~(1 << 24);
14561
14562       i |= 0xef000000;
14563     }
14564   else
14565     i |= 0xf2000000;
14566
14567   insn->instruction = i;
14568 }
14569
14570 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14571    (0, 1, 2, 3).  */
14572
14573 static unsigned
14574 neon_logbits (unsigned x)
14575 {
14576   return ffs (x) - 4;
14577 }
14578
14579 #define LOW4(R) ((R) & 0xf)
14580 #define HI1(R) (((R) >> 4) & 1)
14581
14582 /* Encode insns with bit pattern:
14583
14584   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14585   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14586
14587   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14588   different meaning for some instruction.  */
14589
14590 static void
14591 neon_three_same (int isquad, int ubit, int size)
14592 {
14593   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14594   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14595   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14596   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14597   inst.instruction |= LOW4 (inst.operands[2].reg);
14598   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14599   inst.instruction |= (isquad != 0) << 6;
14600   inst.instruction |= (ubit != 0) << 24;
14601   if (size != -1)
14602     inst.instruction |= neon_logbits (size) << 20;
14603
14604   neon_dp_fixup (&inst);
14605 }
14606
14607 /* Encode instructions of the form:
14608
14609   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14610   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14611
14612   Don't write size if SIZE == -1.  */
14613
14614 static void
14615 neon_two_same (int qbit, int ubit, int size)
14616 {
14617   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14618   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14619   inst.instruction |= LOW4 (inst.operands[1].reg);
14620   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14621   inst.instruction |= (qbit != 0) << 6;
14622   inst.instruction |= (ubit != 0) << 24;
14623
14624   if (size != -1)
14625     inst.instruction |= neon_logbits (size) << 18;
14626
14627   neon_dp_fixup (&inst);
14628 }
14629
14630 /* Neon instruction encoders, in approximate order of appearance.  */
14631
14632 static void
14633 do_neon_dyadic_i_su (void)
14634 {
14635   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14636   struct neon_type_el et = neon_check_type (3, rs,
14637     N_EQK, N_EQK, N_SU_32 | N_KEY);
14638   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14639 }
14640
14641 static void
14642 do_neon_dyadic_i64_su (void)
14643 {
14644   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14645   struct neon_type_el et = neon_check_type (3, rs,
14646     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14647   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14648 }
14649
14650 static void
14651 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14652                 unsigned immbits)
14653 {
14654   unsigned size = et.size >> 3;
14655   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14656   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14657   inst.instruction |= LOW4 (inst.operands[1].reg);
14658   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14659   inst.instruction |= (isquad != 0) << 6;
14660   inst.instruction |= immbits << 16;
14661   inst.instruction |= (size >> 3) << 7;
14662   inst.instruction |= (size & 0x7) << 19;
14663   if (write_ubit)
14664     inst.instruction |= (uval != 0) << 24;
14665
14666   neon_dp_fixup (&inst);
14667 }
14668
14669 static void
14670 do_neon_shl_imm (void)
14671 {
14672   if (!inst.operands[2].isreg)
14673     {
14674       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14675       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14676       int imm = inst.operands[2].imm;
14677
14678       constraint (imm < 0 || (unsigned)imm >= et.size,
14679                   _("immediate out of range for shift"));
14680       NEON_ENCODE (IMMED, inst);
14681       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14682     }
14683   else
14684     {
14685       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14686       struct neon_type_el et = neon_check_type (3, rs,
14687         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14688       unsigned int tmp;
14689
14690       /* VSHL/VQSHL 3-register variants have syntax such as:
14691            vshl.xx Dd, Dm, Dn
14692          whereas other 3-register operations encoded by neon_three_same have
14693          syntax like:
14694            vadd.xx Dd, Dn, Dm
14695          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14696          here.  */
14697       tmp = inst.operands[2].reg;
14698       inst.operands[2].reg = inst.operands[1].reg;
14699       inst.operands[1].reg = tmp;
14700       NEON_ENCODE (INTEGER, inst);
14701       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14702     }
14703 }
14704
14705 static void
14706 do_neon_qshl_imm (void)
14707 {
14708   if (!inst.operands[2].isreg)
14709     {
14710       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14711       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14712       int imm = inst.operands[2].imm;
14713
14714       constraint (imm < 0 || (unsigned)imm >= et.size,
14715                   _("immediate out of range for shift"));
14716       NEON_ENCODE (IMMED, inst);
14717       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14718     }
14719   else
14720     {
14721       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14722       struct neon_type_el et = neon_check_type (3, rs,
14723         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14724       unsigned int tmp;
14725
14726       /* See note in do_neon_shl_imm.  */
14727       tmp = inst.operands[2].reg;
14728       inst.operands[2].reg = inst.operands[1].reg;
14729       inst.operands[1].reg = tmp;
14730       NEON_ENCODE (INTEGER, inst);
14731       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14732     }
14733 }
14734
14735 static void
14736 do_neon_rshl (void)
14737 {
14738   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14739   struct neon_type_el et = neon_check_type (3, rs,
14740     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14741   unsigned int tmp;
14742
14743   tmp = inst.operands[2].reg;
14744   inst.operands[2].reg = inst.operands[1].reg;
14745   inst.operands[1].reg = tmp;
14746   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14747 }
14748
14749 static int
14750 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14751 {
14752   /* Handle .I8 pseudo-instructions.  */
14753   if (size == 8)
14754     {
14755       /* Unfortunately, this will make everything apart from zero out-of-range.
14756          FIXME is this the intended semantics? There doesn't seem much point in
14757          accepting .I8 if so.  */
14758       immediate |= immediate << 8;
14759       size = 16;
14760     }
14761
14762   if (size >= 32)
14763     {
14764       if (immediate == (immediate & 0x000000ff))
14765         {
14766           *immbits = immediate;
14767           return 0x1;
14768         }
14769       else if (immediate == (immediate & 0x0000ff00))
14770         {
14771           *immbits = immediate >> 8;
14772           return 0x3;
14773         }
14774       else if (immediate == (immediate & 0x00ff0000))
14775         {
14776           *immbits = immediate >> 16;
14777           return 0x5;
14778         }
14779       else if (immediate == (immediate & 0xff000000))
14780         {
14781           *immbits = immediate >> 24;
14782           return 0x7;
14783         }
14784       if ((immediate & 0xffff) != (immediate >> 16))
14785         goto bad_immediate;
14786       immediate &= 0xffff;
14787     }
14788
14789   if (immediate == (immediate & 0x000000ff))
14790     {
14791       *immbits = immediate;
14792       return 0x9;
14793     }
14794   else if (immediate == (immediate & 0x0000ff00))
14795     {
14796       *immbits = immediate >> 8;
14797       return 0xb;
14798     }
14799
14800   bad_immediate:
14801   first_error (_("immediate value out of range"));
14802   return FAIL;
14803 }
14804
14805 static void
14806 do_neon_logic (void)
14807 {
14808   if (inst.operands[2].present && inst.operands[2].isreg)
14809     {
14810       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14811       neon_check_type (3, rs, N_IGNORE_TYPE);
14812       /* U bit and size field were set as part of the bitmask.  */
14813       NEON_ENCODE (INTEGER, inst);
14814       neon_three_same (neon_quad (rs), 0, -1);
14815     }
14816   else
14817     {
14818       const int three_ops_form = (inst.operands[2].present
14819                                   && !inst.operands[2].isreg);
14820       const int immoperand = (three_ops_form ? 2 : 1);
14821       enum neon_shape rs = (three_ops_form
14822                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14823                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14824       struct neon_type_el et = neon_check_type (2, rs,
14825         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14826       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14827       unsigned immbits;
14828       int cmode;
14829
14830       if (et.type == NT_invtype)
14831         return;
14832
14833       if (three_ops_form)
14834         constraint (inst.operands[0].reg != inst.operands[1].reg,
14835                     _("first and second operands shall be the same register"));
14836
14837       NEON_ENCODE (IMMED, inst);
14838
14839       immbits = inst.operands[immoperand].imm;
14840       if (et.size == 64)
14841         {
14842           /* .i64 is a pseudo-op, so the immediate must be a repeating
14843              pattern.  */
14844           if (immbits != (inst.operands[immoperand].regisimm ?
14845                           inst.operands[immoperand].reg : 0))
14846             {
14847               /* Set immbits to an invalid constant.  */
14848               immbits = 0xdeadbeef;
14849             }
14850         }
14851
14852       switch (opcode)
14853         {
14854         case N_MNEM_vbic:
14855           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14856           break;
14857
14858         case N_MNEM_vorr:
14859           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14860           break;
14861
14862         case N_MNEM_vand:
14863           /* Pseudo-instruction for VBIC.  */
14864           neon_invert_size (&immbits, 0, et.size);
14865           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14866           break;
14867
14868         case N_MNEM_vorn:
14869           /* Pseudo-instruction for VORR.  */
14870           neon_invert_size (&immbits, 0, et.size);
14871           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14872           break;
14873
14874         default:
14875           abort ();
14876         }
14877
14878       if (cmode == FAIL)
14879         return;
14880
14881       inst.instruction |= neon_quad (rs) << 6;
14882       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14883       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14884       inst.instruction |= cmode << 8;
14885       neon_write_immbits (immbits);
14886
14887       neon_dp_fixup (&inst);
14888     }
14889 }
14890
14891 static void
14892 do_neon_bitfield (void)
14893 {
14894   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14895   neon_check_type (3, rs, N_IGNORE_TYPE);
14896   neon_three_same (neon_quad (rs), 0, -1);
14897 }
14898
14899 static void
14900 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14901                   unsigned destbits)
14902 {
14903   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14904   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14905                                             types | N_KEY);
14906   if (et.type == NT_float)
14907     {
14908       NEON_ENCODE (FLOAT, inst);
14909       neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
14910     }
14911   else
14912     {
14913       NEON_ENCODE (INTEGER, inst);
14914       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14915     }
14916 }
14917
14918 static void
14919 do_neon_dyadic_if_su (void)
14920 {
14921   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14922 }
14923
14924 static void
14925 do_neon_dyadic_if_su_d (void)
14926 {
14927   /* This version only allow D registers, but that constraint is enforced during
14928      operand parsing so we don't need to do anything extra here.  */
14929   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14930 }
14931
14932 static void
14933 do_neon_dyadic_if_i_d (void)
14934 {
14935   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14936      affected if we specify unsigned args.  */
14937   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14938 }
14939
14940 enum vfp_or_neon_is_neon_bits
14941 {
14942   NEON_CHECK_CC = 1,
14943   NEON_CHECK_ARCH = 2,
14944   NEON_CHECK_ARCH8 = 4
14945 };
14946
14947 /* Call this function if an instruction which may have belonged to the VFP or
14948    Neon instruction sets, but turned out to be a Neon instruction (due to the
14949    operand types involved, etc.). We have to check and/or fix-up a couple of
14950    things:
14951
14952      - Make sure the user hasn't attempted to make a Neon instruction
14953        conditional.
14954      - Alter the value in the condition code field if necessary.
14955      - Make sure that the arch supports Neon instructions.
14956
14957    Which of these operations take place depends on bits from enum
14958    vfp_or_neon_is_neon_bits.
14959
14960    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14961    current instruction's condition is COND_ALWAYS, the condition field is
14962    changed to inst.uncond_value. This is necessary because instructions shared
14963    between VFP and Neon may be conditional for the VFP variants only, and the
14964    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14965
14966 static int
14967 vfp_or_neon_is_neon (unsigned check)
14968 {
14969   /* Conditions are always legal in Thumb mode (IT blocks).  */
14970   if (!thumb_mode && (check & NEON_CHECK_CC))
14971     {
14972       if (inst.cond != COND_ALWAYS)
14973         {
14974           first_error (_(BAD_COND));
14975           return FAIL;
14976         }
14977       if (inst.uncond_value != -1)
14978         inst.instruction |= inst.uncond_value << 28;
14979     }
14980
14981   if ((check & NEON_CHECK_ARCH)
14982       && !mark_feature_used (&fpu_neon_ext_v1))
14983     {
14984       first_error (_(BAD_FPU));
14985       return FAIL;
14986     }
14987
14988   if ((check & NEON_CHECK_ARCH8)
14989       && !mark_feature_used (&fpu_neon_ext_armv8))
14990     {
14991       first_error (_(BAD_FPU));
14992       return FAIL;
14993     }
14994
14995   return SUCCESS;
14996 }
14997
14998 static void
14999 do_neon_addsub_if_i (void)
15000 {
15001   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
15002     return;
15003
15004   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15005     return;
15006
15007   /* The "untyped" case can't happen. Do this to stop the "U" bit being
15008      affected if we specify unsigned args.  */
15009   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
15010 }
15011
15012 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
15013    result to be:
15014      V<op> A,B     (A is operand 0, B is operand 2)
15015    to mean:
15016      V<op> A,B,A
15017    not:
15018      V<op> A,B,B
15019    so handle that case specially.  */
15020
15021 static void
15022 neon_exchange_operands (void)
15023 {
15024   if (inst.operands[1].present)
15025     {
15026       void *scratch = xmalloc (sizeof (inst.operands[0]));
15027
15028       /* Swap operands[1] and operands[2].  */
15029       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
15030       inst.operands[1] = inst.operands[2];
15031       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
15032       free (scratch);
15033     }
15034   else
15035     {
15036       inst.operands[1] = inst.operands[2];
15037       inst.operands[2] = inst.operands[0];
15038     }
15039 }
15040
15041 static void
15042 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
15043 {
15044   if (inst.operands[2].isreg)
15045     {
15046       if (invert)
15047         neon_exchange_operands ();
15048       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
15049     }
15050   else
15051     {
15052       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15053       struct neon_type_el et = neon_check_type (2, rs,
15054         N_EQK | N_SIZ, immtypes | N_KEY);
15055
15056       NEON_ENCODE (IMMED, inst);
15057       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15058       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15059       inst.instruction |= LOW4 (inst.operands[1].reg);
15060       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15061       inst.instruction |= neon_quad (rs) << 6;
15062       inst.instruction |= (et.type == NT_float) << 10;
15063       inst.instruction |= neon_logbits (et.size) << 18;
15064
15065       neon_dp_fixup (&inst);
15066     }
15067 }
15068
15069 static void
15070 do_neon_cmp (void)
15071 {
15072   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
15073 }
15074
15075 static void
15076 do_neon_cmp_inv (void)
15077 {
15078   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
15079 }
15080
15081 static void
15082 do_neon_ceq (void)
15083 {
15084   neon_compare (N_IF_32, N_IF_32, FALSE);
15085 }
15086
15087 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
15088    scalars, which are encoded in 5 bits, M : Rm.
15089    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
15090    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
15091    index in M.
15092
15093    Dot Product instructions are similar to multiply instructions except elsize
15094    should always be 32.
15095
15096    This function translates SCALAR, which is GAS's internal encoding of indexed
15097    scalar register, to raw encoding.  There is also register and index range
15098    check based on ELSIZE.  */
15099
15100 static unsigned
15101 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
15102 {
15103   unsigned regno = NEON_SCALAR_REG (scalar);
15104   unsigned elno = NEON_SCALAR_INDEX (scalar);
15105
15106   switch (elsize)
15107     {
15108     case 16:
15109       if (regno > 7 || elno > 3)
15110         goto bad_scalar;
15111       return regno | (elno << 3);
15112
15113     case 32:
15114       if (regno > 15 || elno > 1)
15115         goto bad_scalar;
15116       return regno | (elno << 4);
15117
15118     default:
15119     bad_scalar:
15120       first_error (_("scalar out of range for multiply instruction"));
15121     }
15122
15123   return 0;
15124 }
15125
15126 /* Encode multiply / multiply-accumulate scalar instructions.  */
15127
15128 static void
15129 neon_mul_mac (struct neon_type_el et, int ubit)
15130 {
15131   unsigned scalar;
15132
15133   /* Give a more helpful error message if we have an invalid type.  */
15134   if (et.type == NT_invtype)
15135     return;
15136
15137   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
15138   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15139   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15140   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15141   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15142   inst.instruction |= LOW4 (scalar);
15143   inst.instruction |= HI1 (scalar) << 5;
15144   inst.instruction |= (et.type == NT_float) << 8;
15145   inst.instruction |= neon_logbits (et.size) << 20;
15146   inst.instruction |= (ubit != 0) << 24;
15147
15148   neon_dp_fixup (&inst);
15149 }
15150
15151 static void
15152 do_neon_mac_maybe_scalar (void)
15153 {
15154   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
15155     return;
15156
15157   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15158     return;
15159
15160   if (inst.operands[2].isscalar)
15161     {
15162       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15163       struct neon_type_el et = neon_check_type (3, rs,
15164         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
15165       NEON_ENCODE (SCALAR, inst);
15166       neon_mul_mac (et, neon_quad (rs));
15167     }
15168   else
15169     {
15170       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
15171          affected if we specify unsigned args.  */
15172       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15173     }
15174 }
15175
15176 static void
15177 do_neon_fmac (void)
15178 {
15179   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
15180     return;
15181
15182   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15183     return;
15184
15185   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15186 }
15187
15188 static void
15189 do_neon_tst (void)
15190 {
15191   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15192   struct neon_type_el et = neon_check_type (3, rs,
15193     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
15194   neon_three_same (neon_quad (rs), 0, et.size);
15195 }
15196
15197 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
15198    same types as the MAC equivalents. The polynomial type for this instruction
15199    is encoded the same as the integer type.  */
15200
15201 static void
15202 do_neon_mul (void)
15203 {
15204   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
15205     return;
15206
15207   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15208     return;
15209
15210   if (inst.operands[2].isscalar)
15211     do_neon_mac_maybe_scalar ();
15212   else
15213     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
15214 }
15215
15216 static void
15217 do_neon_qdmulh (void)
15218 {
15219   if (inst.operands[2].isscalar)
15220     {
15221       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15222       struct neon_type_el et = neon_check_type (3, rs,
15223         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15224       NEON_ENCODE (SCALAR, inst);
15225       neon_mul_mac (et, neon_quad (rs));
15226     }
15227   else
15228     {
15229       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15230       struct neon_type_el et = neon_check_type (3, rs,
15231         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15232       NEON_ENCODE (INTEGER, inst);
15233       /* The U bit (rounding) comes from bit mask.  */
15234       neon_three_same (neon_quad (rs), 0, et.size);
15235     }
15236 }
15237
15238 static void
15239 do_neon_qrdmlah (void)
15240 {
15241   /* Check we're on the correct architecture.  */
15242   if (!mark_feature_used (&fpu_neon_ext_armv8))
15243     inst.error =
15244       _("instruction form not available on this architecture.");
15245   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
15246     {
15247       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
15248       record_feature_use (&fpu_neon_ext_v8_1);
15249     }
15250
15251   if (inst.operands[2].isscalar)
15252     {
15253       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15254       struct neon_type_el et = neon_check_type (3, rs,
15255         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15256       NEON_ENCODE (SCALAR, inst);
15257       neon_mul_mac (et, neon_quad (rs));
15258     }
15259   else
15260     {
15261       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15262       struct neon_type_el et = neon_check_type (3, rs,
15263         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15264       NEON_ENCODE (INTEGER, inst);
15265       /* The U bit (rounding) comes from bit mask.  */
15266       neon_three_same (neon_quad (rs), 0, et.size);
15267     }
15268 }
15269
15270 static void
15271 do_neon_fcmp_absolute (void)
15272 {
15273   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15274   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15275                                             N_F_16_32 | N_KEY);
15276   /* Size field comes from bit mask.  */
15277   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
15278 }
15279
15280 static void
15281 do_neon_fcmp_absolute_inv (void)
15282 {
15283   neon_exchange_operands ();
15284   do_neon_fcmp_absolute ();
15285 }
15286
15287 static void
15288 do_neon_step (void)
15289 {
15290   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15291   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15292                                             N_F_16_32 | N_KEY);
15293   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15294 }
15295
15296 static void
15297 do_neon_abs_neg (void)
15298 {
15299   enum neon_shape rs;
15300   struct neon_type_el et;
15301
15302   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
15303     return;
15304
15305   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15306     return;
15307
15308   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15309   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
15310
15311   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15312   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15313   inst.instruction |= LOW4 (inst.operands[1].reg);
15314   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15315   inst.instruction |= neon_quad (rs) << 6;
15316   inst.instruction |= (et.type == NT_float) << 10;
15317   inst.instruction |= neon_logbits (et.size) << 18;
15318
15319   neon_dp_fixup (&inst);
15320 }
15321
15322 static void
15323 do_neon_sli (void)
15324 {
15325   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15326   struct neon_type_el et = neon_check_type (2, rs,
15327     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15328   int imm = inst.operands[2].imm;
15329   constraint (imm < 0 || (unsigned)imm >= et.size,
15330               _("immediate out of range for insert"));
15331   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15332 }
15333
15334 static void
15335 do_neon_sri (void)
15336 {
15337   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15338   struct neon_type_el et = neon_check_type (2, rs,
15339     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15340   int imm = inst.operands[2].imm;
15341   constraint (imm < 1 || (unsigned)imm > et.size,
15342               _("immediate out of range for insert"));
15343   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
15344 }
15345
15346 static void
15347 do_neon_qshlu_imm (void)
15348 {
15349   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15350   struct neon_type_el et = neon_check_type (2, rs,
15351     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
15352   int imm = inst.operands[2].imm;
15353   constraint (imm < 0 || (unsigned)imm >= et.size,
15354               _("immediate out of range for shift"));
15355   /* Only encodes the 'U present' variant of the instruction.
15356      In this case, signed types have OP (bit 8) set to 0.
15357      Unsigned types have OP set to 1.  */
15358   inst.instruction |= (et.type == NT_unsigned) << 8;
15359   /* The rest of the bits are the same as other immediate shifts.  */
15360   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15361 }
15362
15363 static void
15364 do_neon_qmovn (void)
15365 {
15366   struct neon_type_el et = neon_check_type (2, NS_DQ,
15367     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15368   /* Saturating move where operands can be signed or unsigned, and the
15369      destination has the same signedness.  */
15370   NEON_ENCODE (INTEGER, inst);
15371   if (et.type == NT_unsigned)
15372     inst.instruction |= 0xc0;
15373   else
15374     inst.instruction |= 0x80;
15375   neon_two_same (0, 1, et.size / 2);
15376 }
15377
15378 static void
15379 do_neon_qmovun (void)
15380 {
15381   struct neon_type_el et = neon_check_type (2, NS_DQ,
15382     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15383   /* Saturating move with unsigned results. Operands must be signed.  */
15384   NEON_ENCODE (INTEGER, inst);
15385   neon_two_same (0, 1, et.size / 2);
15386 }
15387
15388 static void
15389 do_neon_rshift_sat_narrow (void)
15390 {
15391   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15392      or unsigned. If operands are unsigned, results must also be unsigned.  */
15393   struct neon_type_el et = neon_check_type (2, NS_DQI,
15394     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15395   int imm = inst.operands[2].imm;
15396   /* This gets the bounds check, size encoding and immediate bits calculation
15397      right.  */
15398   et.size /= 2;
15399
15400   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
15401      VQMOVN.I<size> <Dd>, <Qm>.  */
15402   if (imm == 0)
15403     {
15404       inst.operands[2].present = 0;
15405       inst.instruction = N_MNEM_vqmovn;
15406       do_neon_qmovn ();
15407       return;
15408     }
15409
15410   constraint (imm < 1 || (unsigned)imm > et.size,
15411               _("immediate out of range"));
15412   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
15413 }
15414
15415 static void
15416 do_neon_rshift_sat_narrow_u (void)
15417 {
15418   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15419      or unsigned. If operands are unsigned, results must also be unsigned.  */
15420   struct neon_type_el et = neon_check_type (2, NS_DQI,
15421     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15422   int imm = inst.operands[2].imm;
15423   /* This gets the bounds check, size encoding and immediate bits calculation
15424      right.  */
15425   et.size /= 2;
15426
15427   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
15428      VQMOVUN.I<size> <Dd>, <Qm>.  */
15429   if (imm == 0)
15430     {
15431       inst.operands[2].present = 0;
15432       inst.instruction = N_MNEM_vqmovun;
15433       do_neon_qmovun ();
15434       return;
15435     }
15436
15437   constraint (imm < 1 || (unsigned)imm > et.size,
15438               _("immediate out of range"));
15439   /* FIXME: The manual is kind of unclear about what value U should have in
15440      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
15441      must be 1.  */
15442   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
15443 }
15444
15445 static void
15446 do_neon_movn (void)
15447 {
15448   struct neon_type_el et = neon_check_type (2, NS_DQ,
15449     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15450   NEON_ENCODE (INTEGER, inst);
15451   neon_two_same (0, 1, et.size / 2);
15452 }
15453
15454 static void
15455 do_neon_rshift_narrow (void)
15456 {
15457   struct neon_type_el et = neon_check_type (2, NS_DQI,
15458     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15459   int imm = inst.operands[2].imm;
15460   /* This gets the bounds check, size encoding and immediate bits calculation
15461      right.  */
15462   et.size /= 2;
15463
15464   /* If immediate is zero then we are a pseudo-instruction for
15465      VMOVN.I<size> <Dd>, <Qm>  */
15466   if (imm == 0)
15467     {
15468       inst.operands[2].present = 0;
15469       inst.instruction = N_MNEM_vmovn;
15470       do_neon_movn ();
15471       return;
15472     }
15473
15474   constraint (imm < 1 || (unsigned)imm > et.size,
15475               _("immediate out of range for narrowing operation"));
15476   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
15477 }
15478
15479 static void
15480 do_neon_shll (void)
15481 {
15482   /* FIXME: Type checking when lengthening.  */
15483   struct neon_type_el et = neon_check_type (2, NS_QDI,
15484     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
15485   unsigned imm = inst.operands[2].imm;
15486
15487   if (imm == et.size)
15488     {
15489       /* Maximum shift variant.  */
15490       NEON_ENCODE (INTEGER, inst);
15491       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15492       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15493       inst.instruction |= LOW4 (inst.operands[1].reg);
15494       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15495       inst.instruction |= neon_logbits (et.size) << 18;
15496
15497       neon_dp_fixup (&inst);
15498     }
15499   else
15500     {
15501       /* A more-specific type check for non-max versions.  */
15502       et = neon_check_type (2, NS_QDI,
15503         N_EQK | N_DBL, N_SU_32 | N_KEY);
15504       NEON_ENCODE (IMMED, inst);
15505       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
15506     }
15507 }
15508
15509 /* Check the various types for the VCVT instruction, and return which version
15510    the current instruction is.  */
15511
15512 #define CVT_FLAVOUR_VAR                                                       \
15513   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
15514   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
15515   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
15516   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
15517   /* Half-precision conversions.  */                                          \
15518   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15519   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15520   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
15521   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
15522   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
15523   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
15524   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
15525      Compared with single/double precision variants, only the co-processor    \
15526      field is different, so the encoding flow is reused here.  */             \
15527   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
15528   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
15529   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
15530   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
15531   /* VFP instructions.  */                                                    \
15532   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
15533   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
15534   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
15535   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
15536   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
15537   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
15538   /* VFP instructions with bitshift.  */                                      \
15539   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
15540   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
15541   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
15542   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
15543   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
15544   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
15545   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
15546   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
15547
15548 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
15549   neon_cvt_flavour_##C,
15550
15551 /* The different types of conversions we can do.  */
15552 enum neon_cvt_flavour
15553 {
15554   CVT_FLAVOUR_VAR
15555   neon_cvt_flavour_invalid,
15556   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
15557 };
15558
15559 #undef CVT_VAR
15560
15561 static enum neon_cvt_flavour
15562 get_neon_cvt_flavour (enum neon_shape rs)
15563 {
15564 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
15565   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
15566   if (et.type != NT_invtype)                            \
15567     {                                                   \
15568       inst.error = NULL;                                \
15569       return (neon_cvt_flavour_##C);                    \
15570     }
15571
15572   struct neon_type_el et;
15573   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15574                         || rs == NS_FF) ? N_VFP : 0;
15575   /* The instruction versions which take an immediate take one register
15576      argument, which is extended to the width of the full register. Thus the
15577      "source" and "destination" registers must have the same width.  Hack that
15578      here by making the size equal to the key (wider, in this case) operand.  */
15579   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15580
15581   CVT_FLAVOUR_VAR;
15582
15583   return neon_cvt_flavour_invalid;
15584 #undef CVT_VAR
15585 }
15586
15587 enum neon_cvt_mode
15588 {
15589   neon_cvt_mode_a,
15590   neon_cvt_mode_n,
15591   neon_cvt_mode_p,
15592   neon_cvt_mode_m,
15593   neon_cvt_mode_z,
15594   neon_cvt_mode_x,
15595   neon_cvt_mode_r
15596 };
15597
15598 /* Neon-syntax VFP conversions.  */
15599
15600 static void
15601 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15602 {
15603   const char *opname = 0;
15604
15605   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
15606       || rs == NS_FHI || rs == NS_HFI)
15607     {
15608       /* Conversions with immediate bitshift.  */
15609       const char *enc[] =
15610         {
15611 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15612           CVT_FLAVOUR_VAR
15613           NULL
15614 #undef CVT_VAR
15615         };
15616
15617       if (flavour < (int) ARRAY_SIZE (enc))
15618         {
15619           opname = enc[flavour];
15620           constraint (inst.operands[0].reg != inst.operands[1].reg,
15621                       _("operands 0 and 1 must be the same register"));
15622           inst.operands[1] = inst.operands[2];
15623           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15624         }
15625     }
15626   else
15627     {
15628       /* Conversions without bitshift.  */
15629       const char *enc[] =
15630         {
15631 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15632           CVT_FLAVOUR_VAR
15633           NULL
15634 #undef CVT_VAR
15635         };
15636
15637       if (flavour < (int) ARRAY_SIZE (enc))
15638         opname = enc[flavour];
15639     }
15640
15641   if (opname)
15642     do_vfp_nsyn_opcode (opname);
15643
15644   /* ARMv8.2 fp16 VCVT instruction.  */
15645   if (flavour == neon_cvt_flavour_s32_f16
15646       || flavour == neon_cvt_flavour_u32_f16
15647       || flavour == neon_cvt_flavour_f16_u32
15648       || flavour == neon_cvt_flavour_f16_s32)
15649     do_scalar_fp16_v82_encode ();
15650 }
15651
15652 static void
15653 do_vfp_nsyn_cvtz (void)
15654 {
15655   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
15656   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15657   const char *enc[] =
15658     {
15659 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15660       CVT_FLAVOUR_VAR
15661       NULL
15662 #undef CVT_VAR
15663     };
15664
15665   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15666     do_vfp_nsyn_opcode (enc[flavour]);
15667 }
15668
15669 static void
15670 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15671                       enum neon_cvt_mode mode)
15672 {
15673   int sz, op;
15674   int rm;
15675
15676   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15677      D register operands.  */
15678   if (flavour == neon_cvt_flavour_s32_f64
15679       || flavour == neon_cvt_flavour_u32_f64)
15680     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15681                 _(BAD_FPU));
15682
15683   if (flavour == neon_cvt_flavour_s32_f16
15684       || flavour == neon_cvt_flavour_u32_f16)
15685     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
15686                 _(BAD_FP16));
15687
15688   set_it_insn_type (OUTSIDE_IT_INSN);
15689
15690   switch (flavour)
15691     {
15692     case neon_cvt_flavour_s32_f64:
15693       sz = 1;
15694       op = 1;
15695       break;
15696     case neon_cvt_flavour_s32_f32:
15697       sz = 0;
15698       op = 1;
15699       break;
15700     case neon_cvt_flavour_s32_f16:
15701       sz = 0;
15702       op = 1;
15703       break;
15704     case neon_cvt_flavour_u32_f64:
15705       sz = 1;
15706       op = 0;
15707       break;
15708     case neon_cvt_flavour_u32_f32:
15709       sz = 0;
15710       op = 0;
15711       break;
15712     case neon_cvt_flavour_u32_f16:
15713       sz = 0;
15714       op = 0;
15715       break;
15716     default:
15717       first_error (_("invalid instruction shape"));
15718       return;
15719     }
15720
15721   switch (mode)
15722     {
15723     case neon_cvt_mode_a: rm = 0; break;
15724     case neon_cvt_mode_n: rm = 1; break;
15725     case neon_cvt_mode_p: rm = 2; break;
15726     case neon_cvt_mode_m: rm = 3; break;
15727     default: first_error (_("invalid rounding mode")); return;
15728     }
15729
15730   NEON_ENCODE (FPV8, inst);
15731   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15732   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15733   inst.instruction |= sz << 8;
15734
15735   /* ARMv8.2 fp16 VCVT instruction.  */
15736   if (flavour == neon_cvt_flavour_s32_f16
15737       ||flavour == neon_cvt_flavour_u32_f16)
15738     do_scalar_fp16_v82_encode ();
15739   inst.instruction |= op << 7;
15740   inst.instruction |= rm << 16;
15741   inst.instruction |= 0xf0000000;
15742   inst.is_neon = TRUE;
15743 }
15744
15745 static void
15746 do_neon_cvt_1 (enum neon_cvt_mode mode)
15747 {
15748   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15749                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
15750                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
15751                                           NS_NULL);
15752   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15753
15754   if (flavour == neon_cvt_flavour_invalid)
15755     return;
15756
15757   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15758   if (mode == neon_cvt_mode_z
15759       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15760       && (flavour == neon_cvt_flavour_s16_f16
15761           || flavour == neon_cvt_flavour_u16_f16
15762           || flavour == neon_cvt_flavour_s32_f32
15763           || flavour == neon_cvt_flavour_u32_f32
15764           || flavour == neon_cvt_flavour_s32_f64
15765           || flavour == neon_cvt_flavour_u32_f64)
15766       && (rs == NS_FD || rs == NS_FF))
15767     {
15768       do_vfp_nsyn_cvtz ();
15769       return;
15770     }
15771
15772   /* ARMv8.2 fp16 VCVT conversions.  */
15773   if (mode == neon_cvt_mode_z
15774       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
15775       && (flavour == neon_cvt_flavour_s32_f16
15776           || flavour == neon_cvt_flavour_u32_f16)
15777       && (rs == NS_FH))
15778     {
15779       do_vfp_nsyn_cvtz ();
15780       do_scalar_fp16_v82_encode ();
15781       return;
15782     }
15783
15784   /* VFP rather than Neon conversions.  */
15785   if (flavour >= neon_cvt_flavour_first_fp)
15786     {
15787       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15788         do_vfp_nsyn_cvt (rs, flavour);
15789       else
15790         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15791
15792       return;
15793     }
15794
15795   switch (rs)
15796     {
15797     case NS_DDI:
15798     case NS_QQI:
15799       {
15800         unsigned immbits;
15801         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
15802                              0x0000100, 0x1000100, 0x0, 0x1000000};
15803
15804         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15805           return;
15806
15807         /* Fixed-point conversion with #0 immediate is encoded as an
15808            integer conversion.  */
15809         if (inst.operands[2].present && inst.operands[2].imm == 0)
15810           goto int_encode;
15811         NEON_ENCODE (IMMED, inst);
15812         if (flavour != neon_cvt_flavour_invalid)
15813           inst.instruction |= enctab[flavour];
15814         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15815         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15816         inst.instruction |= LOW4 (inst.operands[1].reg);
15817         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15818         inst.instruction |= neon_quad (rs) << 6;
15819         inst.instruction |= 1 << 21;
15820         if (flavour < neon_cvt_flavour_s16_f16)
15821           {
15822             inst.instruction |= 1 << 21;
15823             immbits = 32 - inst.operands[2].imm;
15824             inst.instruction |= immbits << 16;
15825           }
15826         else
15827           {
15828             inst.instruction |= 3 << 20;
15829             immbits = 16 - inst.operands[2].imm;
15830             inst.instruction |= immbits << 16;
15831             inst.instruction &= ~(1 << 9);
15832           }
15833
15834         neon_dp_fixup (&inst);
15835       }
15836       break;
15837
15838     case NS_DD:
15839     case NS_QQ:
15840       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15841         {
15842           NEON_ENCODE (FLOAT, inst);
15843           set_it_insn_type (OUTSIDE_IT_INSN);
15844
15845           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15846             return;
15847
15848           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15849           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15850           inst.instruction |= LOW4 (inst.operands[1].reg);
15851           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15852           inst.instruction |= neon_quad (rs) << 6;
15853           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
15854                                || flavour == neon_cvt_flavour_u32_f32) << 7;
15855           inst.instruction |= mode << 8;
15856           if (flavour == neon_cvt_flavour_u16_f16
15857               || flavour == neon_cvt_flavour_s16_f16)
15858             /* Mask off the original size bits and reencode them.  */
15859             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
15860
15861           if (thumb_mode)
15862             inst.instruction |= 0xfc000000;
15863           else
15864             inst.instruction |= 0xf0000000;
15865         }
15866       else
15867         {
15868     int_encode:
15869           {
15870             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
15871                                   0x100, 0x180, 0x0, 0x080};
15872
15873             NEON_ENCODE (INTEGER, inst);
15874
15875             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15876               return;
15877
15878             if (flavour != neon_cvt_flavour_invalid)
15879               inst.instruction |= enctab[flavour];
15880
15881             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15882             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15883             inst.instruction |= LOW4 (inst.operands[1].reg);
15884             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15885             inst.instruction |= neon_quad (rs) << 6;
15886             if (flavour >= neon_cvt_flavour_s16_f16
15887                 && flavour <= neon_cvt_flavour_f16_u16)
15888               /* Half precision.  */
15889               inst.instruction |= 1 << 18;
15890             else
15891               inst.instruction |= 2 << 18;
15892
15893             neon_dp_fixup (&inst);
15894           }
15895         }
15896       break;
15897
15898     /* Half-precision conversions for Advanced SIMD -- neon.  */
15899     case NS_QD:
15900     case NS_DQ:
15901
15902       if ((rs == NS_DQ)
15903           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15904           {
15905             as_bad (_("operand size must match register width"));
15906             break;
15907           }
15908
15909       if ((rs == NS_QD)
15910           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15911           {
15912             as_bad (_("operand size must match register width"));
15913             break;
15914           }
15915
15916       if (rs == NS_DQ)
15917         inst.instruction = 0x3b60600;
15918       else
15919         inst.instruction = 0x3b60700;
15920
15921       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15922       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15923       inst.instruction |= LOW4 (inst.operands[1].reg);
15924       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15925       neon_dp_fixup (&inst);
15926       break;
15927
15928     default:
15929       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
15930       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15931         do_vfp_nsyn_cvt (rs, flavour);
15932       else
15933         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15934     }
15935 }
15936
15937 static void
15938 do_neon_cvtr (void)
15939 {
15940   do_neon_cvt_1 (neon_cvt_mode_x);
15941 }
15942
15943 static void
15944 do_neon_cvt (void)
15945 {
15946   do_neon_cvt_1 (neon_cvt_mode_z);
15947 }
15948
15949 static void
15950 do_neon_cvta (void)
15951 {
15952   do_neon_cvt_1 (neon_cvt_mode_a);
15953 }
15954
15955 static void
15956 do_neon_cvtn (void)
15957 {
15958   do_neon_cvt_1 (neon_cvt_mode_n);
15959 }
15960
15961 static void
15962 do_neon_cvtp (void)
15963 {
15964   do_neon_cvt_1 (neon_cvt_mode_p);
15965 }
15966
15967 static void
15968 do_neon_cvtm (void)
15969 {
15970   do_neon_cvt_1 (neon_cvt_mode_m);
15971 }
15972
15973 static void
15974 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15975 {
15976   if (is_double)
15977     mark_feature_used (&fpu_vfp_ext_armv8);
15978
15979   encode_arm_vfp_reg (inst.operands[0].reg,
15980                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
15981   encode_arm_vfp_reg (inst.operands[1].reg,
15982                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
15983   inst.instruction |= to ? 0x10000 : 0;
15984   inst.instruction |= t ? 0x80 : 0;
15985   inst.instruction |= is_double ? 0x100 : 0;
15986   do_vfp_cond_or_thumb ();
15987 }
15988
15989 static void
15990 do_neon_cvttb_1 (bfd_boolean t)
15991 {
15992   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
15993                                           NS_DF, NS_DH, NS_NULL);
15994
15995   if (rs == NS_NULL)
15996     return;
15997   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
15998     {
15999       inst.error = NULL;
16000       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
16001     }
16002   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
16003     {
16004       inst.error = NULL;
16005       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
16006     }
16007   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
16008     {
16009       /* The VCVTB and VCVTT instructions with D-register operands
16010          don't work for SP only targets.  */
16011       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16012                   _(BAD_FPU));
16013
16014       inst.error = NULL;
16015       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
16016     }
16017   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
16018     {
16019       /* The VCVTB and VCVTT instructions with D-register operands
16020          don't work for SP only targets.  */
16021       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16022                   _(BAD_FPU));
16023
16024       inst.error = NULL;
16025       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
16026     }
16027   else
16028     return;
16029 }
16030
16031 static void
16032 do_neon_cvtb (void)
16033 {
16034   do_neon_cvttb_1 (FALSE);
16035 }
16036
16037
16038 static void
16039 do_neon_cvtt (void)
16040 {
16041   do_neon_cvttb_1 (TRUE);
16042 }
16043
16044 static void
16045 neon_move_immediate (void)
16046 {
16047   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
16048   struct neon_type_el et = neon_check_type (2, rs,
16049     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
16050   unsigned immlo, immhi = 0, immbits;
16051   int op, cmode, float_p;
16052
16053   constraint (et.type == NT_invtype,
16054               _("operand size must be specified for immediate VMOV"));
16055
16056   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
16057   op = (inst.instruction & (1 << 5)) != 0;
16058
16059   immlo = inst.operands[1].imm;
16060   if (inst.operands[1].regisimm)
16061     immhi = inst.operands[1].reg;
16062
16063   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
16064               _("immediate has bits set outside the operand size"));
16065
16066   float_p = inst.operands[1].immisfloat;
16067
16068   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
16069                                         et.size, et.type)) == FAIL)
16070     {
16071       /* Invert relevant bits only.  */
16072       neon_invert_size (&immlo, &immhi, et.size);
16073       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
16074          with one or the other; those cases are caught by
16075          neon_cmode_for_move_imm.  */
16076       op = !op;
16077       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
16078                                             &op, et.size, et.type)) == FAIL)
16079         {
16080           first_error (_("immediate out of range"));
16081           return;
16082         }
16083     }
16084
16085   inst.instruction &= ~(1 << 5);
16086   inst.instruction |= op << 5;
16087
16088   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16089   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16090   inst.instruction |= neon_quad (rs) << 6;
16091   inst.instruction |= cmode << 8;
16092
16093   neon_write_immbits (immbits);
16094 }
16095
16096 static void
16097 do_neon_mvn (void)
16098 {
16099   if (inst.operands[1].isreg)
16100     {
16101       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16102
16103       NEON_ENCODE (INTEGER, inst);
16104       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16105       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16106       inst.instruction |= LOW4 (inst.operands[1].reg);
16107       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16108       inst.instruction |= neon_quad (rs) << 6;
16109     }
16110   else
16111     {
16112       NEON_ENCODE (IMMED, inst);
16113       neon_move_immediate ();
16114     }
16115
16116   neon_dp_fixup (&inst);
16117 }
16118
16119 /* Encode instructions of form:
16120
16121   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16122   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
16123
16124 static void
16125 neon_mixed_length (struct neon_type_el et, unsigned size)
16126 {
16127   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16128   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16129   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16130   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16131   inst.instruction |= LOW4 (inst.operands[2].reg);
16132   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16133   inst.instruction |= (et.type == NT_unsigned) << 24;
16134   inst.instruction |= neon_logbits (size) << 20;
16135
16136   neon_dp_fixup (&inst);
16137 }
16138
16139 static void
16140 do_neon_dyadic_long (void)
16141 {
16142   /* FIXME: Type checking for lengthening op.  */
16143   struct neon_type_el et = neon_check_type (3, NS_QDD,
16144     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
16145   neon_mixed_length (et, et.size);
16146 }
16147
16148 static void
16149 do_neon_abal (void)
16150 {
16151   struct neon_type_el et = neon_check_type (3, NS_QDD,
16152     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
16153   neon_mixed_length (et, et.size);
16154 }
16155
16156 static void
16157 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
16158 {
16159   if (inst.operands[2].isscalar)
16160     {
16161       struct neon_type_el et = neon_check_type (3, NS_QDS,
16162         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
16163       NEON_ENCODE (SCALAR, inst);
16164       neon_mul_mac (et, et.type == NT_unsigned);
16165     }
16166   else
16167     {
16168       struct neon_type_el et = neon_check_type (3, NS_QDD,
16169         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
16170       NEON_ENCODE (INTEGER, inst);
16171       neon_mixed_length (et, et.size);
16172     }
16173 }
16174
16175 static void
16176 do_neon_mac_maybe_scalar_long (void)
16177 {
16178   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
16179 }
16180
16181 /* Like neon_scalar_for_mul, this function generate Rm encoding from GAS's
16182    internal SCALAR.  QUAD_P is 1 if it's for Q format, otherwise it's 0.  */
16183
16184 static unsigned
16185 neon_scalar_for_fmac_fp16_long (unsigned scalar, unsigned quad_p)
16186 {
16187   unsigned regno = NEON_SCALAR_REG (scalar);
16188   unsigned elno = NEON_SCALAR_INDEX (scalar);
16189
16190   if (quad_p)
16191     {
16192       if (regno > 7 || elno > 3)
16193         goto bad_scalar;
16194
16195       return ((regno & 0x7)
16196               | ((elno & 0x1) << 3)
16197               | (((elno >> 1) & 0x1) << 5));
16198     }
16199   else
16200     {
16201       if (regno > 15 || elno > 1)
16202         goto bad_scalar;
16203
16204       return (((regno & 0x1) << 5)
16205               | ((regno >> 1) & 0x7)
16206               | ((elno & 0x1) << 3));
16207     }
16208
16209 bad_scalar:
16210   first_error (_("scalar out of range for multiply instruction"));
16211   return 0;
16212 }
16213
16214 static void
16215 do_neon_fmac_maybe_scalar_long (int subtype)
16216 {
16217   enum neon_shape rs;
16218   int high8;
16219   /* NOTE: vfmal/vfmsl use slightly different NEON three-same encoding.  'size"
16220      field (bits[21:20]) has different meaning.  For scalar index variant, it's
16221      used to differentiate add and subtract, otherwise it's with fixed value
16222      0x2.  */
16223   int size = -1;
16224
16225   if (inst.cond != COND_ALWAYS)
16226     as_warn (_("vfmal/vfmsl with FP16 type cannot be conditional, the "
16227                "behaviour is UNPREDICTABLE"));
16228
16229   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16_fml),
16230               _(BAD_FP16));
16231
16232   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
16233               _(BAD_FPU));
16234
16235   /* vfmal/vfmsl are in three-same D/Q register format or the third operand can
16236      be a scalar index register.  */
16237   if (inst.operands[2].isscalar)
16238     {
16239       high8 = 0xfe000000;
16240       if (subtype)
16241         size = 16;
16242       rs = neon_select_shape (NS_DHS, NS_QDS, NS_NULL);
16243     }
16244   else
16245     {
16246       high8 = 0xfc000000;
16247       size = 32;
16248       if (subtype)
16249         inst.instruction |= (0x1 << 23);
16250       rs = neon_select_shape (NS_DHH, NS_QDD, NS_NULL);
16251     }
16252
16253   neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_F16);
16254
16255   /* "opcode" from template has included "ubit", so simply pass 0 here.  Also,
16256      the "S" bit in size field has been reused to differentiate vfmal and vfmsl,
16257      so we simply pass -1 as size.  */
16258   unsigned quad_p = (rs == NS_QDD || rs == NS_QDS);
16259   neon_three_same (quad_p, 0, size);
16260
16261   /* Undo neon_dp_fixup.  Redo the high eight bits.  */
16262   inst.instruction &= 0x00ffffff;
16263   inst.instruction |= high8;
16264
16265 #define LOW1(R) ((R) & 0x1)
16266 #define HI4(R) (((R) >> 1) & 0xf)
16267   /* Unlike usually NEON three-same, encoding for Vn and Vm will depend on
16268      whether the instruction is in Q form and whether Vm is a scalar indexed
16269      operand.  */
16270   if (inst.operands[2].isscalar)
16271     {
16272       unsigned rm
16273         = neon_scalar_for_fmac_fp16_long (inst.operands[2].reg, quad_p);
16274       inst.instruction &= 0xffffffd0;
16275       inst.instruction |= rm;
16276
16277       if (!quad_p)
16278         {
16279           /* Redo Rn as well.  */
16280           inst.instruction &= 0xfff0ff7f;
16281           inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16282           inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16283         }
16284     }
16285   else if (!quad_p)
16286     {
16287       /* Redo Rn and Rm.  */
16288       inst.instruction &= 0xfff0ff50;
16289       inst.instruction |= HI4 (inst.operands[1].reg) << 16;
16290       inst.instruction |= LOW1 (inst.operands[1].reg) << 7;
16291       inst.instruction |= HI4 (inst.operands[2].reg);
16292       inst.instruction |= LOW1 (inst.operands[2].reg) << 5;
16293     }
16294 }
16295
16296 static void
16297 do_neon_vfmal (void)
16298 {
16299   return do_neon_fmac_maybe_scalar_long (0);
16300 }
16301
16302 static void
16303 do_neon_vfmsl (void)
16304 {
16305   return do_neon_fmac_maybe_scalar_long (1);
16306 }
16307
16308 static void
16309 do_neon_dyadic_wide (void)
16310 {
16311   struct neon_type_el et = neon_check_type (3, NS_QQD,
16312     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
16313   neon_mixed_length (et, et.size);
16314 }
16315
16316 static void
16317 do_neon_dyadic_narrow (void)
16318 {
16319   struct neon_type_el et = neon_check_type (3, NS_QDD,
16320     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
16321   /* Operand sign is unimportant, and the U bit is part of the opcode,
16322      so force the operand type to integer.  */
16323   et.type = NT_integer;
16324   neon_mixed_length (et, et.size / 2);
16325 }
16326
16327 static void
16328 do_neon_mul_sat_scalar_long (void)
16329 {
16330   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
16331 }
16332
16333 static void
16334 do_neon_vmull (void)
16335 {
16336   if (inst.operands[2].isscalar)
16337     do_neon_mac_maybe_scalar_long ();
16338   else
16339     {
16340       struct neon_type_el et = neon_check_type (3, NS_QDD,
16341         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
16342
16343       if (et.type == NT_poly)
16344         NEON_ENCODE (POLY, inst);
16345       else
16346         NEON_ENCODE (INTEGER, inst);
16347
16348       /* For polynomial encoding the U bit must be zero, and the size must
16349          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
16350          obviously, as 0b10).  */
16351       if (et.size == 64)
16352         {
16353           /* Check we're on the correct architecture.  */
16354           if (!mark_feature_used (&fpu_crypto_ext_armv8))
16355             inst.error =
16356               _("Instruction form not available on this architecture.");
16357
16358           et.size = 32;
16359         }
16360
16361       neon_mixed_length (et, et.size);
16362     }
16363 }
16364
16365 static void
16366 do_neon_ext (void)
16367 {
16368   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
16369   struct neon_type_el et = neon_check_type (3, rs,
16370     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16371   unsigned imm = (inst.operands[3].imm * et.size) / 8;
16372
16373   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
16374               _("shift out of range"));
16375   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16376   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16377   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16378   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16379   inst.instruction |= LOW4 (inst.operands[2].reg);
16380   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16381   inst.instruction |= neon_quad (rs) << 6;
16382   inst.instruction |= imm << 8;
16383
16384   neon_dp_fixup (&inst);
16385 }
16386
16387 static void
16388 do_neon_rev (void)
16389 {
16390   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16391   struct neon_type_el et = neon_check_type (2, rs,
16392     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16393   unsigned op = (inst.instruction >> 7) & 3;
16394   /* N (width of reversed regions) is encoded as part of the bitmask. We
16395      extract it here to check the elements to be reversed are smaller.
16396      Otherwise we'd get a reserved instruction.  */
16397   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
16398   gas_assert (elsize != 0);
16399   constraint (et.size >= elsize,
16400               _("elements must be smaller than reversal region"));
16401   neon_two_same (neon_quad (rs), 1, et.size);
16402 }
16403
16404 static void
16405 do_neon_dup (void)
16406 {
16407   if (inst.operands[1].isscalar)
16408     {
16409       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
16410       struct neon_type_el et = neon_check_type (2, rs,
16411         N_EQK, N_8 | N_16 | N_32 | N_KEY);
16412       unsigned sizebits = et.size >> 3;
16413       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
16414       int logsize = neon_logbits (et.size);
16415       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
16416
16417       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
16418         return;
16419
16420       NEON_ENCODE (SCALAR, inst);
16421       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16422       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16423       inst.instruction |= LOW4 (dm);
16424       inst.instruction |= HI1 (dm) << 5;
16425       inst.instruction |= neon_quad (rs) << 6;
16426       inst.instruction |= x << 17;
16427       inst.instruction |= sizebits << 16;
16428
16429       neon_dp_fixup (&inst);
16430     }
16431   else
16432     {
16433       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
16434       struct neon_type_el et = neon_check_type (2, rs,
16435         N_8 | N_16 | N_32 | N_KEY, N_EQK);
16436       /* Duplicate ARM register to lanes of vector.  */
16437       NEON_ENCODE (ARMREG, inst);
16438       switch (et.size)
16439         {
16440         case 8:  inst.instruction |= 0x400000; break;
16441         case 16: inst.instruction |= 0x000020; break;
16442         case 32: inst.instruction |= 0x000000; break;
16443         default: break;
16444         }
16445       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16446       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
16447       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
16448       inst.instruction |= neon_quad (rs) << 21;
16449       /* The encoding for this instruction is identical for the ARM and Thumb
16450          variants, except for the condition field.  */
16451       do_vfp_cond_or_thumb ();
16452     }
16453 }
16454
16455 /* VMOV has particularly many variations. It can be one of:
16456      0. VMOV<c><q> <Qd>, <Qm>
16457      1. VMOV<c><q> <Dd>, <Dm>
16458    (Register operations, which are VORR with Rm = Rn.)
16459      2. VMOV<c><q>.<dt> <Qd>, #<imm>
16460      3. VMOV<c><q>.<dt> <Dd>, #<imm>
16461    (Immediate loads.)
16462      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
16463    (ARM register to scalar.)
16464      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
16465    (Two ARM registers to vector.)
16466      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
16467    (Scalar to ARM register.)
16468      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
16469    (Vector to two ARM registers.)
16470      8. VMOV.F32 <Sd>, <Sm>
16471      9. VMOV.F64 <Dd>, <Dm>
16472    (VFP register moves.)
16473     10. VMOV.F32 <Sd>, #imm
16474     11. VMOV.F64 <Dd>, #imm
16475    (VFP float immediate load.)
16476     12. VMOV <Rd>, <Sm>
16477    (VFP single to ARM reg.)
16478     13. VMOV <Sd>, <Rm>
16479    (ARM reg to VFP single.)
16480     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
16481    (Two ARM regs to two VFP singles.)
16482     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
16483    (Two VFP singles to two ARM regs.)
16484
16485    These cases can be disambiguated using neon_select_shape, except cases 1/9
16486    and 3/11 which depend on the operand type too.
16487
16488    All the encoded bits are hardcoded by this function.
16489
16490    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
16491    Cases 5, 7 may be used with VFPv2 and above.
16492
16493    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
16494    can specify a type where it doesn't make sense to, and is ignored).  */
16495
16496 static void
16497 do_neon_mov (void)
16498 {
16499   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
16500                                           NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR,
16501                                           NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
16502                                           NS_HR, NS_RH, NS_HI, NS_NULL);
16503   struct neon_type_el et;
16504   const char *ldconst = 0;
16505
16506   switch (rs)
16507     {
16508     case NS_DD:  /* case 1/9.  */
16509       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16510       /* It is not an error here if no type is given.  */
16511       inst.error = NULL;
16512       if (et.type == NT_float && et.size == 64)
16513         {
16514           do_vfp_nsyn_opcode ("fcpyd");
16515           break;
16516         }
16517       /* fall through.  */
16518
16519     case NS_QQ:  /* case 0/1.  */
16520       {
16521         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16522           return;
16523         /* The architecture manual I have doesn't explicitly state which
16524            value the U bit should have for register->register moves, but
16525            the equivalent VORR instruction has U = 0, so do that.  */
16526         inst.instruction = 0x0200110;
16527         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16528         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16529         inst.instruction |= LOW4 (inst.operands[1].reg);
16530         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16531         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16532         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16533         inst.instruction |= neon_quad (rs) << 6;
16534
16535         neon_dp_fixup (&inst);
16536       }
16537       break;
16538
16539     case NS_DI:  /* case 3/11.  */
16540       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16541       inst.error = NULL;
16542       if (et.type == NT_float && et.size == 64)
16543         {
16544           /* case 11 (fconstd).  */
16545           ldconst = "fconstd";
16546           goto encode_fconstd;
16547         }
16548       /* fall through.  */
16549
16550     case NS_QI:  /* case 2/3.  */
16551       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16552         return;
16553       inst.instruction = 0x0800010;
16554       neon_move_immediate ();
16555       neon_dp_fixup (&inst);
16556       break;
16557
16558     case NS_SR:  /* case 4.  */
16559       {
16560         unsigned bcdebits = 0;
16561         int logsize;
16562         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
16563         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
16564
16565         /* .<size> is optional here, defaulting to .32. */
16566         if (inst.vectype.elems == 0
16567             && inst.operands[0].vectype.type == NT_invtype
16568             && inst.operands[1].vectype.type == NT_invtype)
16569           {
16570             inst.vectype.el[0].type = NT_untyped;
16571             inst.vectype.el[0].size = 32;
16572             inst.vectype.elems = 1;
16573           }
16574
16575         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
16576         logsize = neon_logbits (et.size);
16577
16578         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16579                     _(BAD_FPU));
16580         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16581                     && et.size != 32, _(BAD_FPU));
16582         constraint (et.type == NT_invtype, _("bad type for scalar"));
16583         constraint (x >= 64 / et.size, _("scalar index out of range"));
16584
16585         switch (et.size)
16586           {
16587           case 8:  bcdebits = 0x8; break;
16588           case 16: bcdebits = 0x1; break;
16589           case 32: bcdebits = 0x0; break;
16590           default: ;
16591           }
16592
16593         bcdebits |= x << logsize;
16594
16595         inst.instruction = 0xe000b10;
16596         do_vfp_cond_or_thumb ();
16597         inst.instruction |= LOW4 (dn) << 16;
16598         inst.instruction |= HI1 (dn) << 7;
16599         inst.instruction |= inst.operands[1].reg << 12;
16600         inst.instruction |= (bcdebits & 3) << 5;
16601         inst.instruction |= (bcdebits >> 2) << 21;
16602       }
16603       break;
16604
16605     case NS_DRR:  /* case 5 (fmdrr).  */
16606       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16607                   _(BAD_FPU));
16608
16609       inst.instruction = 0xc400b10;
16610       do_vfp_cond_or_thumb ();
16611       inst.instruction |= LOW4 (inst.operands[0].reg);
16612       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
16613       inst.instruction |= inst.operands[1].reg << 12;
16614       inst.instruction |= inst.operands[2].reg << 16;
16615       break;
16616
16617     case NS_RS:  /* case 6.  */
16618       {
16619         unsigned logsize;
16620         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
16621         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
16622         unsigned abcdebits = 0;
16623
16624         /* .<dt> is optional here, defaulting to .32. */
16625         if (inst.vectype.elems == 0
16626             && inst.operands[0].vectype.type == NT_invtype
16627             && inst.operands[1].vectype.type == NT_invtype)
16628           {
16629             inst.vectype.el[0].type = NT_untyped;
16630             inst.vectype.el[0].size = 32;
16631             inst.vectype.elems = 1;
16632           }
16633
16634         et = neon_check_type (2, NS_NULL,
16635                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
16636         logsize = neon_logbits (et.size);
16637
16638         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16639                     _(BAD_FPU));
16640         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16641                     && et.size != 32, _(BAD_FPU));
16642         constraint (et.type == NT_invtype, _("bad type for scalar"));
16643         constraint (x >= 64 / et.size, _("scalar index out of range"));
16644
16645         switch (et.size)
16646           {
16647           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
16648           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
16649           case 32: abcdebits = 0x00; break;
16650           default: ;
16651           }
16652
16653         abcdebits |= x << logsize;
16654         inst.instruction = 0xe100b10;
16655         do_vfp_cond_or_thumb ();
16656         inst.instruction |= LOW4 (dn) << 16;
16657         inst.instruction |= HI1 (dn) << 7;
16658         inst.instruction |= inst.operands[0].reg << 12;
16659         inst.instruction |= (abcdebits & 3) << 5;
16660         inst.instruction |= (abcdebits >> 2) << 21;
16661       }
16662       break;
16663
16664     case NS_RRD:  /* case 7 (fmrrd).  */
16665       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16666                   _(BAD_FPU));
16667
16668       inst.instruction = 0xc500b10;
16669       do_vfp_cond_or_thumb ();
16670       inst.instruction |= inst.operands[0].reg << 12;
16671       inst.instruction |= inst.operands[1].reg << 16;
16672       inst.instruction |= LOW4 (inst.operands[2].reg);
16673       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16674       break;
16675
16676     case NS_FF:  /* case 8 (fcpys).  */
16677       do_vfp_nsyn_opcode ("fcpys");
16678       break;
16679
16680     case NS_HI:
16681     case NS_FI:  /* case 10 (fconsts).  */
16682       ldconst = "fconsts";
16683       encode_fconstd:
16684       if (is_quarter_float (inst.operands[1].imm))
16685         {
16686           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
16687           do_vfp_nsyn_opcode (ldconst);
16688
16689           /* ARMv8.2 fp16 vmov.f16 instruction.  */
16690           if (rs == NS_HI)
16691             do_scalar_fp16_v82_encode ();
16692         }
16693       else
16694         first_error (_("immediate out of range"));
16695       break;
16696
16697     case NS_RH:
16698     case NS_RF:  /* case 12 (fmrs).  */
16699       do_vfp_nsyn_opcode ("fmrs");
16700       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16701       if (rs == NS_RH)
16702         do_scalar_fp16_v82_encode ();
16703       break;
16704
16705     case NS_HR:
16706     case NS_FR:  /* case 13 (fmsr).  */
16707       do_vfp_nsyn_opcode ("fmsr");
16708       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16709       if (rs == NS_HR)
16710         do_scalar_fp16_v82_encode ();
16711       break;
16712
16713     /* The encoders for the fmrrs and fmsrr instructions expect three operands
16714        (one of which is a list), but we have parsed four.  Do some fiddling to
16715        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
16716        expect.  */
16717     case NS_RRFF:  /* case 14 (fmrrs).  */
16718       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
16719                   _("VFP registers must be adjacent"));
16720       inst.operands[2].imm = 2;
16721       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16722       do_vfp_nsyn_opcode ("fmrrs");
16723       break;
16724
16725     case NS_FFRR:  /* case 15 (fmsrr).  */
16726       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
16727                   _("VFP registers must be adjacent"));
16728       inst.operands[1] = inst.operands[2];
16729       inst.operands[2] = inst.operands[3];
16730       inst.operands[0].imm = 2;
16731       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16732       do_vfp_nsyn_opcode ("fmsrr");
16733       break;
16734
16735     case NS_NULL:
16736       /* neon_select_shape has determined that the instruction
16737          shape is wrong and has already set the error message.  */
16738       break;
16739
16740     default:
16741       abort ();
16742     }
16743 }
16744
16745 static void
16746 do_neon_rshift_round_imm (void)
16747 {
16748   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16749   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16750   int imm = inst.operands[2].imm;
16751
16752   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
16753   if (imm == 0)
16754     {
16755       inst.operands[2].present = 0;
16756       do_neon_mov ();
16757       return;
16758     }
16759
16760   constraint (imm < 1 || (unsigned)imm > et.size,
16761               _("immediate out of range for shift"));
16762   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
16763                   et.size - imm);
16764 }
16765
16766 static void
16767 do_neon_movhf (void)
16768 {
16769   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
16770   constraint (rs != NS_HH, _("invalid suffix"));
16771
16772   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16773               _(BAD_FPU));
16774
16775   do_vfp_sp_monadic ();
16776
16777   inst.is_neon = 1;
16778   inst.instruction |= 0xf0000000;
16779 }
16780
16781 static void
16782 do_neon_movl (void)
16783 {
16784   struct neon_type_el et = neon_check_type (2, NS_QD,
16785     N_EQK | N_DBL, N_SU_32 | N_KEY);
16786   unsigned sizebits = et.size >> 3;
16787   inst.instruction |= sizebits << 19;
16788   neon_two_same (0, et.type == NT_unsigned, -1);
16789 }
16790
16791 static void
16792 do_neon_trn (void)
16793 {
16794   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16795   struct neon_type_el et = neon_check_type (2, rs,
16796     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16797   NEON_ENCODE (INTEGER, inst);
16798   neon_two_same (neon_quad (rs), 1, et.size);
16799 }
16800
16801 static void
16802 do_neon_zip_uzp (void)
16803 {
16804   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16805   struct neon_type_el et = neon_check_type (2, rs,
16806     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16807   if (rs == NS_DD && et.size == 32)
16808     {
16809       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16810       inst.instruction = N_MNEM_vtrn;
16811       do_neon_trn ();
16812       return;
16813     }
16814   neon_two_same (neon_quad (rs), 1, et.size);
16815 }
16816
16817 static void
16818 do_neon_sat_abs_neg (void)
16819 {
16820   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16821   struct neon_type_el et = neon_check_type (2, rs,
16822     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16823   neon_two_same (neon_quad (rs), 1, et.size);
16824 }
16825
16826 static void
16827 do_neon_pair_long (void)
16828 {
16829   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16830   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16831   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16832   inst.instruction |= (et.type == NT_unsigned) << 7;
16833   neon_two_same (neon_quad (rs), 1, et.size);
16834 }
16835
16836 static void
16837 do_neon_recip_est (void)
16838 {
16839   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16840   struct neon_type_el et = neon_check_type (2, rs,
16841     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
16842   inst.instruction |= (et.type == NT_float) << 8;
16843   neon_two_same (neon_quad (rs), 1, et.size);
16844 }
16845
16846 static void
16847 do_neon_cls (void)
16848 {
16849   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16850   struct neon_type_el et = neon_check_type (2, rs,
16851     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16852   neon_two_same (neon_quad (rs), 1, et.size);
16853 }
16854
16855 static void
16856 do_neon_clz (void)
16857 {
16858   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16859   struct neon_type_el et = neon_check_type (2, rs,
16860     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16861   neon_two_same (neon_quad (rs), 1, et.size);
16862 }
16863
16864 static void
16865 do_neon_cnt (void)
16866 {
16867   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16868   struct neon_type_el et = neon_check_type (2, rs,
16869     N_EQK | N_INT, N_8 | N_KEY);
16870   neon_two_same (neon_quad (rs), 1, et.size);
16871 }
16872
16873 static void
16874 do_neon_swp (void)
16875 {
16876   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16877   neon_two_same (neon_quad (rs), 1, -1);
16878 }
16879
16880 static void
16881 do_neon_tbl_tbx (void)
16882 {
16883   unsigned listlenbits;
16884   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16885
16886   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16887     {
16888       first_error (_("bad list length for table lookup"));
16889       return;
16890     }
16891
16892   listlenbits = inst.operands[1].imm - 1;
16893   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16894   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16895   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16896   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16897   inst.instruction |= LOW4 (inst.operands[2].reg);
16898   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16899   inst.instruction |= listlenbits << 8;
16900
16901   neon_dp_fixup (&inst);
16902 }
16903
16904 static void
16905 do_neon_ldm_stm (void)
16906 {
16907   /* P, U and L bits are part of bitmask.  */
16908   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16909   unsigned offsetbits = inst.operands[1].imm * 2;
16910
16911   if (inst.operands[1].issingle)
16912     {
16913       do_vfp_nsyn_ldm_stm (is_dbmode);
16914       return;
16915     }
16916
16917   constraint (is_dbmode && !inst.operands[0].writeback,
16918               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16919
16920   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16921               _("register list must contain at least 1 and at most 16 "
16922                 "registers"));
16923
16924   inst.instruction |= inst.operands[0].reg << 16;
16925   inst.instruction |= inst.operands[0].writeback << 21;
16926   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16927   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16928
16929   inst.instruction |= offsetbits;
16930
16931   do_vfp_cond_or_thumb ();
16932 }
16933
16934 static void
16935 do_neon_ldr_str (void)
16936 {
16937   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16938
16939   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16940      And is UNPREDICTABLE in thumb mode.  */
16941   if (!is_ldr
16942       && inst.operands[1].reg == REG_PC
16943       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16944     {
16945       if (thumb_mode)
16946         inst.error = _("Use of PC here is UNPREDICTABLE");
16947       else if (warn_on_deprecated)
16948         as_tsktsk (_("Use of PC here is deprecated"));
16949     }
16950
16951   if (inst.operands[0].issingle)
16952     {
16953       if (is_ldr)
16954         do_vfp_nsyn_opcode ("flds");
16955       else
16956         do_vfp_nsyn_opcode ("fsts");
16957
16958       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
16959       if (inst.vectype.el[0].size == 16)
16960         do_scalar_fp16_v82_encode ();
16961     }
16962   else
16963     {
16964       if (is_ldr)
16965         do_vfp_nsyn_opcode ("fldd");
16966       else
16967         do_vfp_nsyn_opcode ("fstd");
16968     }
16969 }
16970
16971 /* "interleave" version also handles non-interleaving register VLD1/VST1
16972    instructions.  */
16973
16974 static void
16975 do_neon_ld_st_interleave (void)
16976 {
16977   struct neon_type_el et = neon_check_type (1, NS_NULL,
16978                                             N_8 | N_16 | N_32 | N_64);
16979   unsigned alignbits = 0;
16980   unsigned idx;
16981   /* The bits in this table go:
16982      0: register stride of one (0) or two (1)
16983      1,2: register list length, minus one (1, 2, 3, 4).
16984      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
16985      We use -1 for invalid entries.  */
16986   const int typetable[] =
16987     {
16988       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
16989        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
16990        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
16991        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
16992     };
16993   int typebits;
16994
16995   if (et.type == NT_invtype)
16996     return;
16997
16998   if (inst.operands[1].immisalign)
16999     switch (inst.operands[1].imm >> 8)
17000       {
17001       case 64: alignbits = 1; break;
17002       case 128:
17003         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
17004             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17005           goto bad_alignment;
17006         alignbits = 2;
17007         break;
17008       case 256:
17009         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
17010           goto bad_alignment;
17011         alignbits = 3;
17012         break;
17013       default:
17014       bad_alignment:
17015         first_error (_("bad alignment"));
17016         return;
17017       }
17018
17019   inst.instruction |= alignbits << 4;
17020   inst.instruction |= neon_logbits (et.size) << 6;
17021
17022   /* Bits [4:6] of the immediate in a list specifier encode register stride
17023      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
17024      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
17025      up the right value for "type" in a table based on this value and the given
17026      list style, then stick it back.  */
17027   idx = ((inst.operands[0].imm >> 4) & 7)
17028         | (((inst.instruction >> 8) & 3) << 3);
17029
17030   typebits = typetable[idx];
17031
17032   constraint (typebits == -1, _("bad list type for instruction"));
17033   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
17034               _("bad element type for instruction"));
17035
17036   inst.instruction &= ~0xf00;
17037   inst.instruction |= typebits << 8;
17038 }
17039
17040 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
17041    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
17042    otherwise. The variable arguments are a list of pairs of legal (size, align)
17043    values, terminated with -1.  */
17044
17045 static int
17046 neon_alignment_bit (int size, int align, int *do_alignment, ...)
17047 {
17048   va_list ap;
17049   int result = FAIL, thissize, thisalign;
17050
17051   if (!inst.operands[1].immisalign)
17052     {
17053       *do_alignment = 0;
17054       return SUCCESS;
17055     }
17056
17057   va_start (ap, do_alignment);
17058
17059   do
17060     {
17061       thissize = va_arg (ap, int);
17062       if (thissize == -1)
17063         break;
17064       thisalign = va_arg (ap, int);
17065
17066       if (size == thissize && align == thisalign)
17067         result = SUCCESS;
17068     }
17069   while (result != SUCCESS);
17070
17071   va_end (ap);
17072
17073   if (result == SUCCESS)
17074     *do_alignment = 1;
17075   else
17076     first_error (_("unsupported alignment for instruction"));
17077
17078   return result;
17079 }
17080
17081 static void
17082 do_neon_ld_st_lane (void)
17083 {
17084   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17085   int align_good, do_alignment = 0;
17086   int logsize = neon_logbits (et.size);
17087   int align = inst.operands[1].imm >> 8;
17088   int n = (inst.instruction >> 8) & 3;
17089   int max_el = 64 / et.size;
17090
17091   if (et.type == NT_invtype)
17092     return;
17093
17094   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
17095               _("bad list length"));
17096   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
17097               _("scalar index out of range"));
17098   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
17099               && et.size == 8,
17100               _("stride of 2 unavailable when element size is 8"));
17101
17102   switch (n)
17103     {
17104     case 0:  /* VLD1 / VST1.  */
17105       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
17106                                        32, 32, -1);
17107       if (align_good == FAIL)
17108         return;
17109       if (do_alignment)
17110         {
17111           unsigned alignbits = 0;
17112           switch (et.size)
17113             {
17114             case 16: alignbits = 0x1; break;
17115             case 32: alignbits = 0x3; break;
17116             default: ;
17117             }
17118           inst.instruction |= alignbits << 4;
17119         }
17120       break;
17121
17122     case 1:  /* VLD2 / VST2.  */
17123       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
17124                       16, 32, 32, 64, -1);
17125       if (align_good == FAIL)
17126         return;
17127       if (do_alignment)
17128         inst.instruction |= 1 << 4;
17129       break;
17130
17131     case 2:  /* VLD3 / VST3.  */
17132       constraint (inst.operands[1].immisalign,
17133                   _("can't use alignment with this instruction"));
17134       break;
17135
17136     case 3:  /* VLD4 / VST4.  */
17137       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17138                                        16, 64, 32, 64, 32, 128, -1);
17139       if (align_good == FAIL)
17140         return;
17141       if (do_alignment)
17142         {
17143           unsigned alignbits = 0;
17144           switch (et.size)
17145             {
17146             case 8:  alignbits = 0x1; break;
17147             case 16: alignbits = 0x1; break;
17148             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
17149             default: ;
17150             }
17151           inst.instruction |= alignbits << 4;
17152         }
17153       break;
17154
17155     default: ;
17156     }
17157
17158   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
17159   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17160     inst.instruction |= 1 << (4 + logsize);
17161
17162   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
17163   inst.instruction |= logsize << 10;
17164 }
17165
17166 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
17167
17168 static void
17169 do_neon_ld_dup (void)
17170 {
17171   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
17172   int align_good, do_alignment = 0;
17173
17174   if (et.type == NT_invtype)
17175     return;
17176
17177   switch ((inst.instruction >> 8) & 3)
17178     {
17179     case 0:  /* VLD1.  */
17180       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
17181       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17182                                        &do_alignment, 16, 16, 32, 32, -1);
17183       if (align_good == FAIL)
17184         return;
17185       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
17186         {
17187         case 1: break;
17188         case 2: inst.instruction |= 1 << 5; break;
17189         default: first_error (_("bad list length")); return;
17190         }
17191       inst.instruction |= neon_logbits (et.size) << 6;
17192       break;
17193
17194     case 1:  /* VLD2.  */
17195       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
17196                                        &do_alignment, 8, 16, 16, 32, 32, 64,
17197                                        -1);
17198       if (align_good == FAIL)
17199         return;
17200       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
17201                   _("bad list length"));
17202       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17203         inst.instruction |= 1 << 5;
17204       inst.instruction |= neon_logbits (et.size) << 6;
17205       break;
17206
17207     case 2:  /* VLD3.  */
17208       constraint (inst.operands[1].immisalign,
17209                   _("can't use alignment with this instruction"));
17210       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
17211                   _("bad list length"));
17212       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17213         inst.instruction |= 1 << 5;
17214       inst.instruction |= neon_logbits (et.size) << 6;
17215       break;
17216
17217     case 3:  /* VLD4.  */
17218       {
17219         int align = inst.operands[1].imm >> 8;
17220         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
17221                                          16, 64, 32, 64, 32, 128, -1);
17222         if (align_good == FAIL)
17223           return;
17224         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
17225                     _("bad list length"));
17226         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17227           inst.instruction |= 1 << 5;
17228         if (et.size == 32 && align == 128)
17229           inst.instruction |= 0x3 << 6;
17230         else
17231           inst.instruction |= neon_logbits (et.size) << 6;
17232       }
17233       break;
17234
17235     default: ;
17236     }
17237
17238   inst.instruction |= do_alignment << 4;
17239 }
17240
17241 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
17242    apart from bits [11:4].  */
17243
17244 static void
17245 do_neon_ldx_stx (void)
17246 {
17247   if (inst.operands[1].isreg)
17248     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
17249
17250   switch (NEON_LANE (inst.operands[0].imm))
17251     {
17252     case NEON_INTERLEAVE_LANES:
17253       NEON_ENCODE (INTERLV, inst);
17254       do_neon_ld_st_interleave ();
17255       break;
17256
17257     case NEON_ALL_LANES:
17258       NEON_ENCODE (DUP, inst);
17259       if (inst.instruction == N_INV)
17260         {
17261           first_error ("only loads support such operands");
17262           break;
17263         }
17264       do_neon_ld_dup ();
17265       break;
17266
17267     default:
17268       NEON_ENCODE (LANE, inst);
17269       do_neon_ld_st_lane ();
17270     }
17271
17272   /* L bit comes from bit mask.  */
17273   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17274   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17275   inst.instruction |= inst.operands[1].reg << 16;
17276
17277   if (inst.operands[1].postind)
17278     {
17279       int postreg = inst.operands[1].imm & 0xf;
17280       constraint (!inst.operands[1].immisreg,
17281                   _("post-index must be a register"));
17282       constraint (postreg == 0xd || postreg == 0xf,
17283                   _("bad register for post-index"));
17284       inst.instruction |= postreg;
17285     }
17286   else
17287     {
17288       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17289       constraint (inst.reloc.exp.X_op != O_constant
17290                   || inst.reloc.exp.X_add_number != 0,
17291                   BAD_ADDR_MODE);
17292
17293       if (inst.operands[1].writeback)
17294         {
17295           inst.instruction |= 0xd;
17296         }
17297       else
17298         inst.instruction |= 0xf;
17299     }
17300
17301   if (thumb_mode)
17302     inst.instruction |= 0xf9000000;
17303   else
17304     inst.instruction |= 0xf4000000;
17305 }
17306
17307 /* FP v8.  */
17308 static void
17309 do_vfp_nsyn_fpv8 (enum neon_shape rs)
17310 {
17311   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17312      D register operands.  */
17313   if (neon_shape_class[rs] == SC_DOUBLE)
17314     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17315                 _(BAD_FPU));
17316
17317   NEON_ENCODE (FPV8, inst);
17318
17319   if (rs == NS_FFF || rs == NS_HHH)
17320     {
17321       do_vfp_sp_dyadic ();
17322
17323       /* ARMv8.2 fp16 instruction.  */
17324       if (rs == NS_HHH)
17325         do_scalar_fp16_v82_encode ();
17326     }
17327   else
17328     do_vfp_dp_rd_rn_rm ();
17329
17330   if (rs == NS_DDD)
17331     inst.instruction |= 0x100;
17332
17333   inst.instruction |= 0xf0000000;
17334 }
17335
17336 static void
17337 do_vsel (void)
17338 {
17339   set_it_insn_type (OUTSIDE_IT_INSN);
17340
17341   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
17342     first_error (_("invalid instruction shape"));
17343 }
17344
17345 static void
17346 do_vmaxnm (void)
17347 {
17348   set_it_insn_type (OUTSIDE_IT_INSN);
17349
17350   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
17351     return;
17352
17353   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17354     return;
17355
17356   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
17357 }
17358
17359 static void
17360 do_vrint_1 (enum neon_cvt_mode mode)
17361 {
17362   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
17363   struct neon_type_el et;
17364
17365   if (rs == NS_NULL)
17366     return;
17367
17368   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17369      D register operands.  */
17370   if (neon_shape_class[rs] == SC_DOUBLE)
17371     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17372                 _(BAD_FPU));
17373
17374   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
17375                         | N_VFP);
17376   if (et.type != NT_invtype)
17377     {
17378       /* VFP encodings.  */
17379       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
17380           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
17381         set_it_insn_type (OUTSIDE_IT_INSN);
17382
17383       NEON_ENCODE (FPV8, inst);
17384       if (rs == NS_FF || rs == NS_HH)
17385         do_vfp_sp_monadic ();
17386       else
17387         do_vfp_dp_rd_rm ();
17388
17389       switch (mode)
17390         {
17391         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
17392         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
17393         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
17394         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
17395         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
17396         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
17397         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
17398         default: abort ();
17399         }
17400
17401       inst.instruction |= (rs == NS_DD) << 8;
17402       do_vfp_cond_or_thumb ();
17403
17404       /* ARMv8.2 fp16 vrint instruction.  */
17405       if (rs == NS_HH)
17406       do_scalar_fp16_v82_encode ();
17407     }
17408   else
17409     {
17410       /* Neon encodings (or something broken...).  */
17411       inst.error = NULL;
17412       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
17413
17414       if (et.type == NT_invtype)
17415         return;
17416
17417       set_it_insn_type (OUTSIDE_IT_INSN);
17418       NEON_ENCODE (FLOAT, inst);
17419
17420       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17421         return;
17422
17423       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17424       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17425       inst.instruction |= LOW4 (inst.operands[1].reg);
17426       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17427       inst.instruction |= neon_quad (rs) << 6;
17428       /* Mask off the original size bits and reencode them.  */
17429       inst.instruction = ((inst.instruction & 0xfff3ffff)
17430                           | neon_logbits (et.size) << 18);
17431
17432       switch (mode)
17433         {
17434         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
17435         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
17436         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
17437         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
17438         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
17439         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
17440         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
17441         default: abort ();
17442         }
17443
17444       if (thumb_mode)
17445         inst.instruction |= 0xfc000000;
17446       else
17447         inst.instruction |= 0xf0000000;
17448     }
17449 }
17450
17451 static void
17452 do_vrintx (void)
17453 {
17454   do_vrint_1 (neon_cvt_mode_x);
17455 }
17456
17457 static void
17458 do_vrintz (void)
17459 {
17460   do_vrint_1 (neon_cvt_mode_z);
17461 }
17462
17463 static void
17464 do_vrintr (void)
17465 {
17466   do_vrint_1 (neon_cvt_mode_r);
17467 }
17468
17469 static void
17470 do_vrinta (void)
17471 {
17472   do_vrint_1 (neon_cvt_mode_a);
17473 }
17474
17475 static void
17476 do_vrintn (void)
17477 {
17478   do_vrint_1 (neon_cvt_mode_n);
17479 }
17480
17481 static void
17482 do_vrintp (void)
17483 {
17484   do_vrint_1 (neon_cvt_mode_p);
17485 }
17486
17487 static void
17488 do_vrintm (void)
17489 {
17490   do_vrint_1 (neon_cvt_mode_m);
17491 }
17492
17493 static unsigned
17494 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
17495 {
17496   unsigned regno = NEON_SCALAR_REG (opnd);
17497   unsigned elno = NEON_SCALAR_INDEX (opnd);
17498
17499   if (elsize == 16 && elno < 2 && regno < 16)
17500     return regno | (elno << 4);
17501   else if (elsize == 32 && elno == 0)
17502     return regno;
17503
17504   first_error (_("scalar out of range"));
17505   return 0;
17506 }
17507
17508 static void
17509 do_vcmla (void)
17510 {
17511   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17512               _(BAD_FPU));
17513   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17514   unsigned rot = inst.reloc.exp.X_add_number;
17515   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
17516               _("immediate out of range"));
17517   rot /= 90;
17518   if (inst.operands[2].isscalar)
17519     {
17520       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
17521       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17522                                        N_KEY | N_F16 | N_F32).size;
17523       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
17524       inst.is_neon = 1;
17525       inst.instruction = 0xfe000800;
17526       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17527       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17528       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17529       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17530       inst.instruction |= LOW4 (m);
17531       inst.instruction |= HI1 (m) << 5;
17532       inst.instruction |= neon_quad (rs) << 6;
17533       inst.instruction |= rot << 20;
17534       inst.instruction |= (size == 32) << 23;
17535     }
17536   else
17537     {
17538       enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17539       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17540                                        N_KEY | N_F16 | N_F32).size;
17541       neon_three_same (neon_quad (rs), 0, -1);
17542       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17543       inst.instruction |= 0xfc200800;
17544       inst.instruction |= rot << 23;
17545       inst.instruction |= (size == 32) << 20;
17546     }
17547 }
17548
17549 static void
17550 do_vcadd (void)
17551 {
17552   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17553               _(BAD_FPU));
17554   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17555   unsigned rot = inst.reloc.exp.X_add_number;
17556   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17557   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17558   unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17559                                    N_KEY | N_F16 | N_F32).size;
17560   neon_three_same (neon_quad (rs), 0, -1);
17561   inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17562   inst.instruction |= 0xfc800800;
17563   inst.instruction |= (rot == 270) << 24;
17564   inst.instruction |= (size == 32) << 20;
17565 }
17566
17567 /* Dot Product instructions encoding support.  */
17568
17569 static void
17570 do_neon_dotproduct (int unsigned_p)
17571 {
17572   enum neon_shape rs;
17573   unsigned scalar_oprd2 = 0;
17574   int high8;
17575
17576   if (inst.cond != COND_ALWAYS)
17577     as_warn (_("Dot Product instructions cannot be conditional,  the behaviour "
17578                "is UNPREDICTABLE"));
17579
17580   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17581               _(BAD_FPU));
17582
17583   /* Dot Product instructions are in three-same D/Q register format or the third
17584      operand can be a scalar index register.  */
17585   if (inst.operands[2].isscalar)
17586     {
17587       scalar_oprd2 = neon_scalar_for_mul (inst.operands[2].reg, 32);
17588       high8 = 0xfe000000;
17589       rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
17590     }
17591   else
17592     {
17593       high8 = 0xfc000000;
17594       rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
17595     }
17596
17597   if (unsigned_p)
17598     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_U8);
17599   else
17600     neon_check_type (3, rs, N_EQK, N_EQK, N_KEY | N_S8);
17601
17602   /* The "U" bit in traditional Three Same encoding is fixed to 0 for Dot
17603      Product instruction, so we pass 0 as the "ubit" parameter.  And the
17604      "Size" field are fixed to 0x2, so we pass 32 as the "size" parameter.  */
17605   neon_three_same (neon_quad (rs), 0, 32);
17606
17607   /* Undo neon_dp_fixup.  Dot Product instructions are using a slightly
17608      different NEON three-same encoding.  */
17609   inst.instruction &= 0x00ffffff;
17610   inst.instruction |= high8;
17611   /* Encode 'U' bit which indicates signedness.  */
17612   inst.instruction |= (unsigned_p ? 1 : 0) << 4;
17613   /* Re-encode operand2 if it's indexed scalar operand.  What has been encoded
17614      from inst.operand[2].reg in neon_three_same is GAS's internal encoding, not
17615      the instruction encoding.  */
17616   if (inst.operands[2].isscalar)
17617     {
17618       inst.instruction &= 0xffffffd0;
17619       inst.instruction |= LOW4 (scalar_oprd2);
17620       inst.instruction |= HI1 (scalar_oprd2) << 5;
17621     }
17622 }
17623
17624 /* Dot Product instructions for signed integer.  */
17625
17626 static void
17627 do_neon_dotproduct_s (void)
17628 {
17629   return do_neon_dotproduct (0);
17630 }
17631
17632 /* Dot Product instructions for unsigned integer.  */
17633
17634 static void
17635 do_neon_dotproduct_u (void)
17636 {
17637   return do_neon_dotproduct (1);
17638 }
17639
17640 /* Crypto v1 instructions.  */
17641 static void
17642 do_crypto_2op_1 (unsigned elttype, int op)
17643 {
17644   set_it_insn_type (OUTSIDE_IT_INSN);
17645
17646   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
17647       == NT_invtype)
17648     return;
17649
17650   inst.error = NULL;
17651
17652   NEON_ENCODE (INTEGER, inst);
17653   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17654   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17655   inst.instruction |= LOW4 (inst.operands[1].reg);
17656   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17657   if (op != -1)
17658     inst.instruction |= op << 6;
17659
17660   if (thumb_mode)
17661     inst.instruction |= 0xfc000000;
17662   else
17663     inst.instruction |= 0xf0000000;
17664 }
17665
17666 static void
17667 do_crypto_3op_1 (int u, int op)
17668 {
17669   set_it_insn_type (OUTSIDE_IT_INSN);
17670
17671   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
17672                        N_32 | N_UNT | N_KEY).type == NT_invtype)
17673     return;
17674
17675   inst.error = NULL;
17676
17677   NEON_ENCODE (INTEGER, inst);
17678   neon_three_same (1, u, 8 << op);
17679 }
17680
17681 static void
17682 do_aese (void)
17683 {
17684   do_crypto_2op_1 (N_8, 0);
17685 }
17686
17687 static void
17688 do_aesd (void)
17689 {
17690   do_crypto_2op_1 (N_8, 1);
17691 }
17692
17693 static void
17694 do_aesmc (void)
17695 {
17696   do_crypto_2op_1 (N_8, 2);
17697 }
17698
17699 static void
17700 do_aesimc (void)
17701 {
17702   do_crypto_2op_1 (N_8, 3);
17703 }
17704
17705 static void
17706 do_sha1c (void)
17707 {
17708   do_crypto_3op_1 (0, 0);
17709 }
17710
17711 static void
17712 do_sha1p (void)
17713 {
17714   do_crypto_3op_1 (0, 1);
17715 }
17716
17717 static void
17718 do_sha1m (void)
17719 {
17720   do_crypto_3op_1 (0, 2);
17721 }
17722
17723 static void
17724 do_sha1su0 (void)
17725 {
17726   do_crypto_3op_1 (0, 3);
17727 }
17728
17729 static void
17730 do_sha256h (void)
17731 {
17732   do_crypto_3op_1 (1, 0);
17733 }
17734
17735 static void
17736 do_sha256h2 (void)
17737 {
17738   do_crypto_3op_1 (1, 1);
17739 }
17740
17741 static void
17742 do_sha256su1 (void)
17743 {
17744   do_crypto_3op_1 (1, 2);
17745 }
17746
17747 static void
17748 do_sha1h (void)
17749 {
17750   do_crypto_2op_1 (N_32, -1);
17751 }
17752
17753 static void
17754 do_sha1su1 (void)
17755 {
17756   do_crypto_2op_1 (N_32, 0);
17757 }
17758
17759 static void
17760 do_sha256su0 (void)
17761 {
17762   do_crypto_2op_1 (N_32, 1);
17763 }
17764
17765 static void
17766 do_crc32_1 (unsigned int poly, unsigned int sz)
17767 {
17768   unsigned int Rd = inst.operands[0].reg;
17769   unsigned int Rn = inst.operands[1].reg;
17770   unsigned int Rm = inst.operands[2].reg;
17771
17772   set_it_insn_type (OUTSIDE_IT_INSN);
17773   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
17774   inst.instruction |= LOW4 (Rn) << 16;
17775   inst.instruction |= LOW4 (Rm);
17776   inst.instruction |= sz << (thumb_mode ? 4 : 21);
17777   inst.instruction |= poly << (thumb_mode ? 20 : 9);
17778
17779   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
17780     as_warn (UNPRED_REG ("r15"));
17781 }
17782
17783 static void
17784 do_crc32b (void)
17785 {
17786   do_crc32_1 (0, 0);
17787 }
17788
17789 static void
17790 do_crc32h (void)
17791 {
17792   do_crc32_1 (0, 1);
17793 }
17794
17795 static void
17796 do_crc32w (void)
17797 {
17798   do_crc32_1 (0, 2);
17799 }
17800
17801 static void
17802 do_crc32cb (void)
17803 {
17804   do_crc32_1 (1, 0);
17805 }
17806
17807 static void
17808 do_crc32ch (void)
17809 {
17810   do_crc32_1 (1, 1);
17811 }
17812
17813 static void
17814 do_crc32cw (void)
17815 {
17816   do_crc32_1 (1, 2);
17817 }
17818
17819 static void
17820 do_vjcvt (void)
17821 {
17822   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17823               _(BAD_FPU));
17824   neon_check_type (2, NS_FD, N_S32, N_F64);
17825   do_vfp_sp_dp_cvt ();
17826   do_vfp_cond_or_thumb ();
17827 }
17828
17829 \f
17830 /* Overall per-instruction processing.  */
17831
17832 /* We need to be able to fix up arbitrary expressions in some statements.
17833    This is so that we can handle symbols that are an arbitrary distance from
17834    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
17835    which returns part of an address in a form which will be valid for
17836    a data instruction.  We do this by pushing the expression into a symbol
17837    in the expr_section, and creating a fix for that.  */
17838
17839 static void
17840 fix_new_arm (fragS *       frag,
17841              int           where,
17842              short int     size,
17843              expressionS * exp,
17844              int           pc_rel,
17845              int           reloc)
17846 {
17847   fixS *           new_fix;
17848
17849   switch (exp->X_op)
17850     {
17851     case O_constant:
17852       if (pc_rel)
17853         {
17854           /* Create an absolute valued symbol, so we have something to
17855              refer to in the object file.  Unfortunately for us, gas's
17856              generic expression parsing will already have folded out
17857              any use of .set foo/.type foo %function that may have
17858              been used to set type information of the target location,
17859              that's being specified symbolically.  We have to presume
17860              the user knows what they are doing.  */
17861           char name[16 + 8];
17862           symbolS *symbol;
17863
17864           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
17865
17866           symbol = symbol_find_or_make (name);
17867           S_SET_SEGMENT (symbol, absolute_section);
17868           symbol_set_frag (symbol, &zero_address_frag);
17869           S_SET_VALUE (symbol, exp->X_add_number);
17870           exp->X_op = O_symbol;
17871           exp->X_add_symbol = symbol;
17872           exp->X_add_number = 0;
17873         }
17874       /* FALLTHROUGH */
17875     case O_symbol:
17876     case O_add:
17877     case O_subtract:
17878       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
17879                              (enum bfd_reloc_code_real) reloc);
17880       break;
17881
17882     default:
17883       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
17884                                   pc_rel, (enum bfd_reloc_code_real) reloc);
17885       break;
17886     }
17887
17888   /* Mark whether the fix is to a THUMB instruction, or an ARM
17889      instruction.  */
17890   new_fix->tc_fix_data = thumb_mode;
17891 }
17892
17893 /* Create a frg for an instruction requiring relaxation.  */
17894 static void
17895 output_relax_insn (void)
17896 {
17897   char * to;
17898   symbolS *sym;
17899   int offset;
17900
17901   /* The size of the instruction is unknown, so tie the debug info to the
17902      start of the instruction.  */
17903   dwarf2_emit_insn (0);
17904
17905   switch (inst.reloc.exp.X_op)
17906     {
17907     case O_symbol:
17908       sym = inst.reloc.exp.X_add_symbol;
17909       offset = inst.reloc.exp.X_add_number;
17910       break;
17911     case O_constant:
17912       sym = NULL;
17913       offset = inst.reloc.exp.X_add_number;
17914       break;
17915     default:
17916       sym = make_expr_symbol (&inst.reloc.exp);
17917       offset = 0;
17918       break;
17919   }
17920   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
17921                  inst.relax, sym, offset, NULL/*offset, opcode*/);
17922   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
17923 }
17924
17925 /* Write a 32-bit thumb instruction to buf.  */
17926 static void
17927 put_thumb32_insn (char * buf, unsigned long insn)
17928 {
17929   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
17930   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
17931 }
17932
17933 static void
17934 output_inst (const char * str)
17935 {
17936   char * to = NULL;
17937
17938   if (inst.error)
17939     {
17940       as_bad ("%s -- `%s'", inst.error, str);
17941       return;
17942     }
17943   if (inst.relax)
17944     {
17945       output_relax_insn ();
17946       return;
17947     }
17948   if (inst.size == 0)
17949     return;
17950
17951   to = frag_more (inst.size);
17952   /* PR 9814: Record the thumb mode into the current frag so that we know
17953      what type of NOP padding to use, if necessary.  We override any previous
17954      setting so that if the mode has changed then the NOPS that we use will
17955      match the encoding of the last instruction in the frag.  */
17956   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
17957
17958   if (thumb_mode && (inst.size > THUMB_SIZE))
17959     {
17960       gas_assert (inst.size == (2 * THUMB_SIZE));
17961       put_thumb32_insn (to, inst.instruction);
17962     }
17963   else if (inst.size > INSN_SIZE)
17964     {
17965       gas_assert (inst.size == (2 * INSN_SIZE));
17966       md_number_to_chars (to, inst.instruction, INSN_SIZE);
17967       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
17968     }
17969   else
17970     md_number_to_chars (to, inst.instruction, inst.size);
17971
17972   if (inst.reloc.type != BFD_RELOC_UNUSED)
17973     fix_new_arm (frag_now, to - frag_now->fr_literal,
17974                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
17975                  inst.reloc.type);
17976
17977   dwarf2_emit_insn (inst.size);
17978 }
17979
17980 static char *
17981 output_it_inst (int cond, int mask, char * to)
17982 {
17983   unsigned long instruction = 0xbf00;
17984
17985   mask &= 0xf;
17986   instruction |= mask;
17987   instruction |= cond << 4;
17988
17989   if (to == NULL)
17990     {
17991       to = frag_more (2);
17992 #ifdef OBJ_ELF
17993       dwarf2_emit_insn (2);
17994 #endif
17995     }
17996
17997   md_number_to_chars (to, instruction, 2);
17998
17999   return to;
18000 }
18001
18002 /* Tag values used in struct asm_opcode's tag field.  */
18003 enum opcode_tag
18004 {
18005   OT_unconditional,     /* Instruction cannot be conditionalized.
18006                            The ARM condition field is still 0xE.  */
18007   OT_unconditionalF,    /* Instruction cannot be conditionalized
18008                            and carries 0xF in its ARM condition field.  */
18009   OT_csuffix,           /* Instruction takes a conditional suffix.  */
18010   OT_csuffixF,          /* Some forms of the instruction take a conditional
18011                            suffix, others place 0xF where the condition field
18012                            would be.  */
18013   OT_cinfix3,           /* Instruction takes a conditional infix,
18014                            beginning at character index 3.  (In
18015                            unified mode, it becomes a suffix.)  */
18016   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
18017                             tsts, cmps, cmns, and teqs. */
18018   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
18019                            character index 3, even in unified mode.  Used for
18020                            legacy instructions where suffix and infix forms
18021                            may be ambiguous.  */
18022   OT_csuf_or_in3,       /* Instruction takes either a conditional
18023                            suffix or an infix at character index 3.  */
18024   OT_odd_infix_unc,     /* This is the unconditional variant of an
18025                            instruction that takes a conditional infix
18026                            at an unusual position.  In unified mode,
18027                            this variant will accept a suffix.  */
18028   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
18029                            are the conditional variants of instructions that
18030                            take conditional infixes in unusual positions.
18031                            The infix appears at character index
18032                            (tag - OT_odd_infix_0).  These are not accepted
18033                            in unified mode.  */
18034 };
18035
18036 /* Subroutine of md_assemble, responsible for looking up the primary
18037    opcode from the mnemonic the user wrote.  STR points to the
18038    beginning of the mnemonic.
18039
18040    This is not simply a hash table lookup, because of conditional
18041    variants.  Most instructions have conditional variants, which are
18042    expressed with a _conditional affix_ to the mnemonic.  If we were
18043    to encode each conditional variant as a literal string in the opcode
18044    table, it would have approximately 20,000 entries.
18045
18046    Most mnemonics take this affix as a suffix, and in unified syntax,
18047    'most' is upgraded to 'all'.  However, in the divided syntax, some
18048    instructions take the affix as an infix, notably the s-variants of
18049    the arithmetic instructions.  Of those instructions, all but six
18050    have the infix appear after the third character of the mnemonic.
18051
18052    Accordingly, the algorithm for looking up primary opcodes given
18053    an identifier is:
18054
18055    1. Look up the identifier in the opcode table.
18056       If we find a match, go to step U.
18057
18058    2. Look up the last two characters of the identifier in the
18059       conditions table.  If we find a match, look up the first N-2
18060       characters of the identifier in the opcode table.  If we
18061       find a match, go to step CE.
18062
18063    3. Look up the fourth and fifth characters of the identifier in
18064       the conditions table.  If we find a match, extract those
18065       characters from the identifier, and look up the remaining
18066       characters in the opcode table.  If we find a match, go
18067       to step CM.
18068
18069    4. Fail.
18070
18071    U. Examine the tag field of the opcode structure, in case this is
18072       one of the six instructions with its conditional infix in an
18073       unusual place.  If it is, the tag tells us where to find the
18074       infix; look it up in the conditions table and set inst.cond
18075       accordingly.  Otherwise, this is an unconditional instruction.
18076       Again set inst.cond accordingly.  Return the opcode structure.
18077
18078   CE. Examine the tag field to make sure this is an instruction that
18079       should receive a conditional suffix.  If it is not, fail.
18080       Otherwise, set inst.cond from the suffix we already looked up,
18081       and return the opcode structure.
18082
18083   CM. Examine the tag field to make sure this is an instruction that
18084       should receive a conditional infix after the third character.
18085       If it is not, fail.  Otherwise, undo the edits to the current
18086       line of input and proceed as for case CE.  */
18087
18088 static const struct asm_opcode *
18089 opcode_lookup (char **str)
18090 {
18091   char *end, *base;
18092   char *affix;
18093   const struct asm_opcode *opcode;
18094   const struct asm_cond *cond;
18095   char save[2];
18096
18097   /* Scan up to the end of the mnemonic, which must end in white space,
18098      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
18099   for (base = end = *str; *end != '\0'; end++)
18100     if (*end == ' ' || *end == '.')
18101       break;
18102
18103   if (end == base)
18104     return NULL;
18105
18106   /* Handle a possible width suffix and/or Neon type suffix.  */
18107   if (end[0] == '.')
18108     {
18109       int offset = 2;
18110
18111       /* The .w and .n suffixes are only valid if the unified syntax is in
18112          use.  */
18113       if (unified_syntax && end[1] == 'w')
18114         inst.size_req = 4;
18115       else if (unified_syntax && end[1] == 'n')
18116         inst.size_req = 2;
18117       else
18118         offset = 0;
18119
18120       inst.vectype.elems = 0;
18121
18122       *str = end + offset;
18123
18124       if (end[offset] == '.')
18125         {
18126           /* See if we have a Neon type suffix (possible in either unified or
18127              non-unified ARM syntax mode).  */
18128           if (parse_neon_type (&inst.vectype, str) == FAIL)
18129             return NULL;
18130         }
18131       else if (end[offset] != '\0' && end[offset] != ' ')
18132         return NULL;
18133     }
18134   else
18135     *str = end;
18136
18137   /* Look for unaffixed or special-case affixed mnemonic.  */
18138   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18139                                                     end - base);
18140   if (opcode)
18141     {
18142       /* step U */
18143       if (opcode->tag < OT_odd_infix_0)
18144         {
18145           inst.cond = COND_ALWAYS;
18146           return opcode;
18147         }
18148
18149       if (warn_on_deprecated && unified_syntax)
18150         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
18151       affix = base + (opcode->tag - OT_odd_infix_0);
18152       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18153       gas_assert (cond);
18154
18155       inst.cond = cond->value;
18156       return opcode;
18157     }
18158
18159   /* Cannot have a conditional suffix on a mnemonic of less than two
18160      characters.  */
18161   if (end - base < 3)
18162     return NULL;
18163
18164   /* Look for suffixed mnemonic.  */
18165   affix = end - 2;
18166   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18167   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18168                                                     affix - base);
18169   if (opcode && cond)
18170     {
18171       /* step CE */
18172       switch (opcode->tag)
18173         {
18174         case OT_cinfix3_legacy:
18175           /* Ignore conditional suffixes matched on infix only mnemonics.  */
18176           break;
18177
18178         case OT_cinfix3:
18179         case OT_cinfix3_deprecated:
18180         case OT_odd_infix_unc:
18181           if (!unified_syntax)
18182             return NULL;
18183           /* Fall through.  */
18184
18185         case OT_csuffix:
18186         case OT_csuffixF:
18187         case OT_csuf_or_in3:
18188           inst.cond = cond->value;
18189           return opcode;
18190
18191         case OT_unconditional:
18192         case OT_unconditionalF:
18193           if (thumb_mode)
18194             inst.cond = cond->value;
18195           else
18196             {
18197               /* Delayed diagnostic.  */
18198               inst.error = BAD_COND;
18199               inst.cond = COND_ALWAYS;
18200             }
18201           return opcode;
18202
18203         default:
18204           return NULL;
18205         }
18206     }
18207
18208   /* Cannot have a usual-position infix on a mnemonic of less than
18209      six characters (five would be a suffix).  */
18210   if (end - base < 6)
18211     return NULL;
18212
18213   /* Look for infixed mnemonic in the usual position.  */
18214   affix = base + 3;
18215   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
18216   if (!cond)
18217     return NULL;
18218
18219   memcpy (save, affix, 2);
18220   memmove (affix, affix + 2, (end - affix) - 2);
18221   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
18222                                                     (end - base) - 2);
18223   memmove (affix + 2, affix, (end - affix) - 2);
18224   memcpy (affix, save, 2);
18225
18226   if (opcode
18227       && (opcode->tag == OT_cinfix3
18228           || opcode->tag == OT_cinfix3_deprecated
18229           || opcode->tag == OT_csuf_or_in3
18230           || opcode->tag == OT_cinfix3_legacy))
18231     {
18232       /* Step CM.  */
18233       if (warn_on_deprecated && unified_syntax
18234           && (opcode->tag == OT_cinfix3
18235               || opcode->tag == OT_cinfix3_deprecated))
18236         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
18237
18238       inst.cond = cond->value;
18239       return opcode;
18240     }
18241
18242   return NULL;
18243 }
18244
18245 /* This function generates an initial IT instruction, leaving its block
18246    virtually open for the new instructions. Eventually,
18247    the mask will be updated by now_it_add_mask () each time
18248    a new instruction needs to be included in the IT block.
18249    Finally, the block is closed with close_automatic_it_block ().
18250    The block closure can be requested either from md_assemble (),
18251    a tencode (), or due to a label hook.  */
18252
18253 static void
18254 new_automatic_it_block (int cond)
18255 {
18256   now_it.state = AUTOMATIC_IT_BLOCK;
18257   now_it.mask = 0x18;
18258   now_it.cc = cond;
18259   now_it.block_length = 1;
18260   mapping_state (MAP_THUMB);
18261   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
18262   now_it.warn_deprecated = FALSE;
18263   now_it.insn_cond = TRUE;
18264 }
18265
18266 /* Close an automatic IT block.
18267    See comments in new_automatic_it_block ().  */
18268
18269 static void
18270 close_automatic_it_block (void)
18271 {
18272   now_it.mask = 0x10;
18273   now_it.block_length = 0;
18274 }
18275
18276 /* Update the mask of the current automatically-generated IT
18277    instruction. See comments in new_automatic_it_block ().  */
18278
18279 static void
18280 now_it_add_mask (int cond)
18281 {
18282 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
18283 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
18284                                               | ((bitvalue) << (nbit)))
18285   const int resulting_bit = (cond & 1);
18286
18287   now_it.mask &= 0xf;
18288   now_it.mask = SET_BIT_VALUE (now_it.mask,
18289                                    resulting_bit,
18290                                   (5 - now_it.block_length));
18291   now_it.mask = SET_BIT_VALUE (now_it.mask,
18292                                    1,
18293                                    ((5 - now_it.block_length) - 1) );
18294   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
18295
18296 #undef CLEAR_BIT
18297 #undef SET_BIT_VALUE
18298 }
18299
18300 /* The IT blocks handling machinery is accessed through the these functions:
18301      it_fsm_pre_encode ()               from md_assemble ()
18302      set_it_insn_type ()                optional, from the tencode functions
18303      set_it_insn_type_last ()           ditto
18304      in_it_block ()                     ditto
18305      it_fsm_post_encode ()              from md_assemble ()
18306      force_automatic_it_block_close ()  from label handling functions
18307
18308    Rationale:
18309      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
18310         initializing the IT insn type with a generic initial value depending
18311         on the inst.condition.
18312      2) During the tencode function, two things may happen:
18313         a) The tencode function overrides the IT insn type by
18314            calling either set_it_insn_type (type) or set_it_insn_type_last ().
18315         b) The tencode function queries the IT block state by
18316            calling in_it_block () (i.e. to determine narrow/not narrow mode).
18317
18318         Both set_it_insn_type and in_it_block run the internal FSM state
18319         handling function (handle_it_state), because: a) setting the IT insn
18320         type may incur in an invalid state (exiting the function),
18321         and b) querying the state requires the FSM to be updated.
18322         Specifically we want to avoid creating an IT block for conditional
18323         branches, so it_fsm_pre_encode is actually a guess and we can't
18324         determine whether an IT block is required until the tencode () routine
18325         has decided what type of instruction this actually it.
18326         Because of this, if set_it_insn_type and in_it_block have to be used,
18327         set_it_insn_type has to be called first.
18328
18329         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
18330         determines the insn IT type depending on the inst.cond code.
18331         When a tencode () routine encodes an instruction that can be
18332         either outside an IT block, or, in the case of being inside, has to be
18333         the last one, set_it_insn_type_last () will determine the proper
18334         IT instruction type based on the inst.cond code. Otherwise,
18335         set_it_insn_type can be called for overriding that logic or
18336         for covering other cases.
18337
18338         Calling handle_it_state () may not transition the IT block state to
18339         OUTSIDE_IT_BLOCK immediately, since the (current) state could be
18340         still queried. Instead, if the FSM determines that the state should
18341         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
18342         after the tencode () function: that's what it_fsm_post_encode () does.
18343
18344         Since in_it_block () calls the state handling function to get an
18345         updated state, an error may occur (due to invalid insns combination).
18346         In that case, inst.error is set.
18347         Therefore, inst.error has to be checked after the execution of
18348         the tencode () routine.
18349
18350      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
18351         any pending state change (if any) that didn't take place in
18352         handle_it_state () as explained above.  */
18353
18354 static void
18355 it_fsm_pre_encode (void)
18356 {
18357   if (inst.cond != COND_ALWAYS)
18358     inst.it_insn_type = INSIDE_IT_INSN;
18359   else
18360     inst.it_insn_type = OUTSIDE_IT_INSN;
18361
18362   now_it.state_handled = 0;
18363 }
18364
18365 /* IT state FSM handling function.  */
18366
18367 static int
18368 handle_it_state (void)
18369 {
18370   now_it.state_handled = 1;
18371   now_it.insn_cond = FALSE;
18372
18373   switch (now_it.state)
18374     {
18375     case OUTSIDE_IT_BLOCK:
18376       switch (inst.it_insn_type)
18377         {
18378         case OUTSIDE_IT_INSN:
18379           break;
18380
18381         case INSIDE_IT_INSN:
18382         case INSIDE_IT_LAST_INSN:
18383           if (thumb_mode == 0)
18384             {
18385               if (unified_syntax
18386                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
18387                 as_tsktsk (_("Warning: conditional outside an IT block"\
18388                              " for Thumb."));
18389             }
18390           else
18391             {
18392               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
18393                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
18394                 {
18395                   /* Automatically generate the IT instruction.  */
18396                   new_automatic_it_block (inst.cond);
18397                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
18398                     close_automatic_it_block ();
18399                 }
18400               else
18401                 {
18402                   inst.error = BAD_OUT_IT;
18403                   return FAIL;
18404                 }
18405             }
18406           break;
18407
18408         case IF_INSIDE_IT_LAST_INSN:
18409         case NEUTRAL_IT_INSN:
18410           break;
18411
18412         case IT_INSN:
18413           now_it.state = MANUAL_IT_BLOCK;
18414           now_it.block_length = 0;
18415           break;
18416         }
18417       break;
18418
18419     case AUTOMATIC_IT_BLOCK:
18420       /* Three things may happen now:
18421          a) We should increment current it block size;
18422          b) We should close current it block (closing insn or 4 insns);
18423          c) We should close current it block and start a new one (due
18424          to incompatible conditions or
18425          4 insns-length block reached).  */
18426
18427       switch (inst.it_insn_type)
18428         {
18429         case OUTSIDE_IT_INSN:
18430           /* The closure of the block shall happen immediately,
18431              so any in_it_block () call reports the block as closed.  */
18432           force_automatic_it_block_close ();
18433           break;
18434
18435         case INSIDE_IT_INSN:
18436         case INSIDE_IT_LAST_INSN:
18437         case IF_INSIDE_IT_LAST_INSN:
18438           now_it.block_length++;
18439
18440           if (now_it.block_length > 4
18441               || !now_it_compatible (inst.cond))
18442             {
18443               force_automatic_it_block_close ();
18444               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
18445                 new_automatic_it_block (inst.cond);
18446             }
18447           else
18448             {
18449               now_it.insn_cond = TRUE;
18450               now_it_add_mask (inst.cond);
18451             }
18452
18453           if (now_it.state == AUTOMATIC_IT_BLOCK
18454               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
18455                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
18456             close_automatic_it_block ();
18457           break;
18458
18459         case NEUTRAL_IT_INSN:
18460           now_it.block_length++;
18461           now_it.insn_cond = TRUE;
18462
18463           if (now_it.block_length > 4)
18464             force_automatic_it_block_close ();
18465           else
18466             now_it_add_mask (now_it.cc & 1);
18467           break;
18468
18469         case IT_INSN:
18470           close_automatic_it_block ();
18471           now_it.state = MANUAL_IT_BLOCK;
18472           break;
18473         }
18474       break;
18475
18476     case MANUAL_IT_BLOCK:
18477       {
18478         /* Check conditional suffixes.  */
18479         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
18480         int is_last;
18481         now_it.mask <<= 1;
18482         now_it.mask &= 0x1f;
18483         is_last = (now_it.mask == 0x10);
18484         now_it.insn_cond = TRUE;
18485
18486         switch (inst.it_insn_type)
18487           {
18488           case OUTSIDE_IT_INSN:
18489             inst.error = BAD_NOT_IT;
18490             return FAIL;
18491
18492           case INSIDE_IT_INSN:
18493             if (cond != inst.cond)
18494               {
18495                 inst.error = BAD_IT_COND;
18496                 return FAIL;
18497               }
18498             break;
18499
18500           case INSIDE_IT_LAST_INSN:
18501           case IF_INSIDE_IT_LAST_INSN:
18502             if (cond != inst.cond)
18503               {
18504                 inst.error = BAD_IT_COND;
18505                 return FAIL;
18506               }
18507             if (!is_last)
18508               {
18509                 inst.error = BAD_BRANCH;
18510                 return FAIL;
18511               }
18512             break;
18513
18514           case NEUTRAL_IT_INSN:
18515             /* The BKPT instruction is unconditional even in an IT block.  */
18516             break;
18517
18518           case IT_INSN:
18519             inst.error = BAD_IT_IT;
18520             return FAIL;
18521           }
18522       }
18523       break;
18524     }
18525
18526   return SUCCESS;
18527 }
18528
18529 struct depr_insn_mask
18530 {
18531   unsigned long pattern;
18532   unsigned long mask;
18533   const char* description;
18534 };
18535
18536 /* List of 16-bit instruction patterns deprecated in an IT block in
18537    ARMv8.  */
18538 static const struct depr_insn_mask depr_it_insns[] = {
18539   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
18540   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
18541   { 0xa000, 0xb800, N_("ADR") },
18542   { 0x4800, 0xf800, N_("Literal loads") },
18543   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
18544   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
18545   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
18546      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
18547   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
18548   { 0, 0, NULL }
18549 };
18550
18551 static void
18552 it_fsm_post_encode (void)
18553 {
18554   int is_last;
18555
18556   if (!now_it.state_handled)
18557     handle_it_state ();
18558
18559   if (now_it.insn_cond
18560       && !now_it.warn_deprecated
18561       && warn_on_deprecated
18562       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
18563     {
18564       if (inst.instruction >= 0x10000)
18565         {
18566           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
18567                      "deprecated in ARMv8"));
18568           now_it.warn_deprecated = TRUE;
18569         }
18570       else
18571         {
18572           const struct depr_insn_mask *p = depr_it_insns;
18573
18574           while (p->mask != 0)
18575             {
18576               if ((inst.instruction & p->mask) == p->pattern)
18577                 {
18578                   as_tsktsk (_("IT blocks containing 16-bit Thumb instructions "
18579                              "of the following class are deprecated in ARMv8: "
18580                              "%s"), p->description);
18581                   now_it.warn_deprecated = TRUE;
18582                   break;
18583                 }
18584
18585               ++p;
18586             }
18587         }
18588
18589       if (now_it.block_length > 1)
18590         {
18591           as_tsktsk (_("IT blocks containing more than one conditional "
18592                      "instruction are deprecated in ARMv8"));
18593           now_it.warn_deprecated = TRUE;
18594         }
18595     }
18596
18597   is_last = (now_it.mask == 0x10);
18598   if (is_last)
18599     {
18600       now_it.state = OUTSIDE_IT_BLOCK;
18601       now_it.mask = 0;
18602     }
18603 }
18604
18605 static void
18606 force_automatic_it_block_close (void)
18607 {
18608   if (now_it.state == AUTOMATIC_IT_BLOCK)
18609     {
18610       close_automatic_it_block ();
18611       now_it.state = OUTSIDE_IT_BLOCK;
18612       now_it.mask = 0;
18613     }
18614 }
18615
18616 static int
18617 in_it_block (void)
18618 {
18619   if (!now_it.state_handled)
18620     handle_it_state ();
18621
18622   return now_it.state != OUTSIDE_IT_BLOCK;
18623 }
18624
18625 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
18626    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
18627    here, hence the "known" in the function name.  */
18628
18629 static bfd_boolean
18630 known_t32_only_insn (const struct asm_opcode *opcode)
18631 {
18632   /* Original Thumb-1 wide instruction.  */
18633   if (opcode->tencode == do_t_blx
18634       || opcode->tencode == do_t_branch23
18635       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
18636       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
18637     return TRUE;
18638
18639   /* Wide-only instruction added to ARMv8-M Baseline.  */
18640   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
18641       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
18642       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
18643       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
18644     return TRUE;
18645
18646   return FALSE;
18647 }
18648
18649 /* Whether wide instruction variant can be used if available for a valid OPCODE
18650    in ARCH.  */
18651
18652 static bfd_boolean
18653 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
18654 {
18655   if (known_t32_only_insn (opcode))
18656     return TRUE;
18657
18658   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
18659      of variant T3 of B.W is checked in do_t_branch.  */
18660   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18661       && opcode->tencode == do_t_branch)
18662     return TRUE;
18663
18664   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
18665   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18666       && opcode->tencode == do_t_mov_cmp
18667       /* Make sure CMP instruction is not affected.  */
18668       && opcode->aencode == do_mov)
18669     return TRUE;
18670
18671   /* Wide instruction variants of all instructions with narrow *and* wide
18672      variants become available with ARMv6t2.  Other opcodes are either
18673      narrow-only or wide-only and are thus available if OPCODE is valid.  */
18674   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
18675     return TRUE;
18676
18677   /* OPCODE with narrow only instruction variant or wide variant not
18678      available.  */
18679   return FALSE;
18680 }
18681
18682 void
18683 md_assemble (char *str)
18684 {
18685   char *p = str;
18686   const struct asm_opcode * opcode;
18687
18688   /* Align the previous label if needed.  */
18689   if (last_label_seen != NULL)
18690     {
18691       symbol_set_frag (last_label_seen, frag_now);
18692       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
18693       S_SET_SEGMENT (last_label_seen, now_seg);
18694     }
18695
18696   memset (&inst, '\0', sizeof (inst));
18697   inst.reloc.type = BFD_RELOC_UNUSED;
18698
18699   opcode = opcode_lookup (&p);
18700   if (!opcode)
18701     {
18702       /* It wasn't an instruction, but it might be a register alias of
18703          the form alias .req reg, or a Neon .dn/.qn directive.  */
18704       if (! create_register_alias (str, p)
18705           && ! create_neon_reg_alias (str, p))
18706         as_bad (_("bad instruction `%s'"), str);
18707
18708       return;
18709     }
18710
18711   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
18712     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
18713
18714   /* The value which unconditional instructions should have in place of the
18715      condition field.  */
18716   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
18717
18718   if (thumb_mode)
18719     {
18720       arm_feature_set variant;
18721
18722       variant = cpu_variant;
18723       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
18724       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
18725         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
18726       /* Check that this instruction is supported for this CPU.  */
18727       if (!opcode->tvariant
18728           || (thumb_mode == 1
18729               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
18730         {
18731           if (opcode->tencode == do_t_swi)
18732             as_bad (_("SVC is not permitted on this architecture"));
18733           else
18734             as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
18735           return;
18736         }
18737       if (inst.cond != COND_ALWAYS && !unified_syntax
18738           && opcode->tencode != do_t_branch)
18739         {
18740           as_bad (_("Thumb does not support conditional execution"));
18741           return;
18742         }
18743
18744       /* Two things are addressed here:
18745          1) Implicit require narrow instructions on Thumb-1.
18746             This avoids relaxation accidentally introducing Thumb-2
18747             instructions.
18748          2) Reject wide instructions in non Thumb-2 cores.
18749
18750          Only instructions with narrow and wide variants need to be handled
18751          but selecting all non wide-only instructions is easier.  */
18752       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
18753           && !t32_insn_ok (variant, opcode))
18754         {
18755           if (inst.size_req == 0)
18756             inst.size_req = 2;
18757           else if (inst.size_req == 4)
18758             {
18759               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
18760                 as_bad (_("selected processor does not support 32bit wide "
18761                           "variant of instruction `%s'"), str);
18762               else
18763                 as_bad (_("selected processor does not support `%s' in "
18764                           "Thumb-2 mode"), str);
18765               return;
18766             }
18767         }
18768
18769       inst.instruction = opcode->tvalue;
18770
18771       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
18772         {
18773           /* Prepare the it_insn_type for those encodings that don't set
18774              it.  */
18775           it_fsm_pre_encode ();
18776
18777           opcode->tencode ();
18778
18779           it_fsm_post_encode ();
18780         }
18781
18782       if (!(inst.error || inst.relax))
18783         {
18784           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
18785           inst.size = (inst.instruction > 0xffff ? 4 : 2);
18786           if (inst.size_req && inst.size_req != inst.size)
18787             {
18788               as_bad (_("cannot honor width suffix -- `%s'"), str);
18789               return;
18790             }
18791         }
18792
18793       /* Something has gone badly wrong if we try to relax a fixed size
18794          instruction.  */
18795       gas_assert (inst.size_req == 0 || !inst.relax);
18796
18797       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18798                               *opcode->tvariant);
18799       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
18800          set those bits when Thumb-2 32-bit instructions are seen.  The impact
18801          of relaxable instructions will be considered later after we finish all
18802          relaxation.  */
18803       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
18804         variant = arm_arch_none;
18805       else
18806         variant = cpu_variant;
18807       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
18808         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18809                                 arm_ext_v6t2);
18810
18811       check_neon_suffixes;
18812
18813       if (!inst.error)
18814         {
18815           mapping_state (MAP_THUMB);
18816         }
18817     }
18818   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
18819     {
18820       bfd_boolean is_bx;
18821
18822       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
18823       is_bx = (opcode->aencode == do_bx);
18824
18825       /* Check that this instruction is supported for this CPU.  */
18826       if (!(is_bx && fix_v4bx)
18827           && !(opcode->avariant &&
18828                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
18829         {
18830           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
18831           return;
18832         }
18833       if (inst.size_req)
18834         {
18835           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
18836           return;
18837         }
18838
18839       inst.instruction = opcode->avalue;
18840       if (opcode->tag == OT_unconditionalF)
18841         inst.instruction |= 0xFU << 28;
18842       else
18843         inst.instruction |= inst.cond << 28;
18844       inst.size = INSN_SIZE;
18845       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
18846         {
18847           it_fsm_pre_encode ();
18848           opcode->aencode ();
18849           it_fsm_post_encode ();
18850         }
18851       /* Arm mode bx is marked as both v4T and v5 because it's still required
18852          on a hypothetical non-thumb v5 core.  */
18853       if (is_bx)
18854         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
18855       else
18856         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
18857                                 *opcode->avariant);
18858
18859       check_neon_suffixes;
18860
18861       if (!inst.error)
18862         {
18863           mapping_state (MAP_ARM);
18864         }
18865     }
18866   else
18867     {
18868       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
18869                 "-- `%s'"), str);
18870       return;
18871     }
18872   output_inst (str);
18873 }
18874
18875 static void
18876 check_it_blocks_finished (void)
18877 {
18878 #ifdef OBJ_ELF
18879   asection *sect;
18880
18881   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
18882     if (seg_info (sect)->tc_segment_info_data.current_it.state
18883         == MANUAL_IT_BLOCK)
18884       {
18885         as_warn (_("section '%s' finished with an open IT block."),
18886                  sect->name);
18887       }
18888 #else
18889   if (now_it.state == MANUAL_IT_BLOCK)
18890     as_warn (_("file finished with an open IT block."));
18891 #endif
18892 }
18893
18894 /* Various frobbings of labels and their addresses.  */
18895
18896 void
18897 arm_start_line_hook (void)
18898 {
18899   last_label_seen = NULL;
18900 }
18901
18902 void
18903 arm_frob_label (symbolS * sym)
18904 {
18905   last_label_seen = sym;
18906
18907   ARM_SET_THUMB (sym, thumb_mode);
18908
18909 #if defined OBJ_COFF || defined OBJ_ELF
18910   ARM_SET_INTERWORK (sym, support_interwork);
18911 #endif
18912
18913   force_automatic_it_block_close ();
18914
18915   /* Note - do not allow local symbols (.Lxxx) to be labelled
18916      as Thumb functions.  This is because these labels, whilst
18917      they exist inside Thumb code, are not the entry points for
18918      possible ARM->Thumb calls.  Also, these labels can be used
18919      as part of a computed goto or switch statement.  eg gcc
18920      can generate code that looks like this:
18921
18922                 ldr  r2, [pc, .Laaa]
18923                 lsl  r3, r3, #2
18924                 ldr  r2, [r3, r2]
18925                 mov  pc, r2
18926
18927        .Lbbb:  .word .Lxxx
18928        .Lccc:  .word .Lyyy
18929        ..etc...
18930        .Laaa:   .word Lbbb
18931
18932      The first instruction loads the address of the jump table.
18933      The second instruction converts a table index into a byte offset.
18934      The third instruction gets the jump address out of the table.
18935      The fourth instruction performs the jump.
18936
18937      If the address stored at .Laaa is that of a symbol which has the
18938      Thumb_Func bit set, then the linker will arrange for this address
18939      to have the bottom bit set, which in turn would mean that the
18940      address computation performed by the third instruction would end
18941      up with the bottom bit set.  Since the ARM is capable of unaligned
18942      word loads, the instruction would then load the incorrect address
18943      out of the jump table, and chaos would ensue.  */
18944   if (label_is_thumb_function_name
18945       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
18946       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
18947     {
18948       /* When the address of a Thumb function is taken the bottom
18949          bit of that address should be set.  This will allow
18950          interworking between Arm and Thumb functions to work
18951          correctly.  */
18952
18953       THUMB_SET_FUNC (sym, 1);
18954
18955       label_is_thumb_function_name = FALSE;
18956     }
18957
18958   dwarf2_emit_label (sym);
18959 }
18960
18961 bfd_boolean
18962 arm_data_in_code (void)
18963 {
18964   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
18965     {
18966       *input_line_pointer = '/';
18967       input_line_pointer += 5;
18968       *input_line_pointer = 0;
18969       return TRUE;
18970     }
18971
18972   return FALSE;
18973 }
18974
18975 char *
18976 arm_canonicalize_symbol_name (char * name)
18977 {
18978   int len;
18979
18980   if (thumb_mode && (len = strlen (name)) > 5
18981       && streq (name + len - 5, "/data"))
18982     *(name + len - 5) = 0;
18983
18984   return name;
18985 }
18986 \f
18987 /* Table of all register names defined by default.  The user can
18988    define additional names with .req.  Note that all register names
18989    should appear in both upper and lowercase variants.  Some registers
18990    also have mixed-case names.  */
18991
18992 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
18993 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
18994 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
18995 #define REGSET(p,t) \
18996   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
18997   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
18998   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
18999   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
19000 #define REGSETH(p,t) \
19001   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
19002   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
19003   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
19004   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
19005 #define REGSET2(p,t) \
19006   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
19007   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
19008   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
19009   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
19010 #define SPLRBANK(base,bank,t) \
19011   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
19012   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
19013   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
19014   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
19015   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
19016   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
19017
19018 static const struct reg_entry reg_names[] =
19019 {
19020   /* ARM integer registers.  */
19021   REGSET(r, RN), REGSET(R, RN),
19022
19023   /* ATPCS synonyms.  */
19024   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
19025   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
19026   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
19027
19028   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
19029   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
19030   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
19031
19032   /* Well-known aliases.  */
19033   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
19034   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
19035
19036   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
19037   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
19038
19039   /* Coprocessor numbers.  */
19040   REGSET(p, CP), REGSET(P, CP),
19041
19042   /* Coprocessor register numbers.  The "cr" variants are for backward
19043      compatibility.  */
19044   REGSET(c,  CN), REGSET(C, CN),
19045   REGSET(cr, CN), REGSET(CR, CN),
19046
19047   /* ARM banked registers.  */
19048   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
19049   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
19050   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
19051   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
19052   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
19053   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
19054   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
19055
19056   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
19057   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
19058   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
19059   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
19060   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
19061   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
19062   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
19063   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
19064
19065   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
19066   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
19067   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
19068   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
19069   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
19070   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
19071   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
19072   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
19073   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
19074
19075   /* FPA registers.  */
19076   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
19077   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
19078
19079   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
19080   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
19081
19082   /* VFP SP registers.  */
19083   REGSET(s,VFS),  REGSET(S,VFS),
19084   REGSETH(s,VFS), REGSETH(S,VFS),
19085
19086   /* VFP DP Registers.  */
19087   REGSET(d,VFD),  REGSET(D,VFD),
19088   /* Extra Neon DP registers.  */
19089   REGSETH(d,VFD), REGSETH(D,VFD),
19090
19091   /* Neon QP registers.  */
19092   REGSET2(q,NQ),  REGSET2(Q,NQ),
19093
19094   /* VFP control registers.  */
19095   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
19096   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
19097   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
19098   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
19099   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
19100   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
19101   REGDEF(mvfr2,5,VFC), REGDEF(MVFR2,5,VFC),
19102
19103   /* Maverick DSP coprocessor registers.  */
19104   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
19105   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
19106
19107   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
19108   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
19109   REGDEF(dspsc,0,DSPSC),
19110
19111   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
19112   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
19113   REGDEF(DSPSC,0,DSPSC),
19114
19115   /* iWMMXt data registers - p0, c0-15.  */
19116   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
19117
19118   /* iWMMXt control registers - p1, c0-3.  */
19119   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
19120   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
19121   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
19122   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
19123
19124   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
19125   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
19126   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
19127   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
19128   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
19129
19130   /* XScale accumulator registers.  */
19131   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
19132 };
19133 #undef REGDEF
19134 #undef REGNUM
19135 #undef REGSET
19136
19137 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
19138    within psr_required_here.  */
19139 static const struct asm_psr psrs[] =
19140 {
19141   /* Backward compatibility notation.  Note that "all" is no longer
19142      truly all possible PSR bits.  */
19143   {"all",  PSR_c | PSR_f},
19144   {"flg",  PSR_f},
19145   {"ctl",  PSR_c},
19146
19147   /* Individual flags.  */
19148   {"f",    PSR_f},
19149   {"c",    PSR_c},
19150   {"x",    PSR_x},
19151   {"s",    PSR_s},
19152
19153   /* Combinations of flags.  */
19154   {"fs",   PSR_f | PSR_s},
19155   {"fx",   PSR_f | PSR_x},
19156   {"fc",   PSR_f | PSR_c},
19157   {"sf",   PSR_s | PSR_f},
19158   {"sx",   PSR_s | PSR_x},
19159   {"sc",   PSR_s | PSR_c},
19160   {"xf",   PSR_x | PSR_f},
19161   {"xs",   PSR_x | PSR_s},
19162   {"xc",   PSR_x | PSR_c},
19163   {"cf",   PSR_c | PSR_f},
19164   {"cs",   PSR_c | PSR_s},
19165   {"cx",   PSR_c | PSR_x},
19166   {"fsx",  PSR_f | PSR_s | PSR_x},
19167   {"fsc",  PSR_f | PSR_s | PSR_c},
19168   {"fxs",  PSR_f | PSR_x | PSR_s},
19169   {"fxc",  PSR_f | PSR_x | PSR_c},
19170   {"fcs",  PSR_f | PSR_c | PSR_s},
19171   {"fcx",  PSR_f | PSR_c | PSR_x},
19172   {"sfx",  PSR_s | PSR_f | PSR_x},
19173   {"sfc",  PSR_s | PSR_f | PSR_c},
19174   {"sxf",  PSR_s | PSR_x | PSR_f},
19175   {"sxc",  PSR_s | PSR_x | PSR_c},
19176   {"scf",  PSR_s | PSR_c | PSR_f},
19177   {"scx",  PSR_s | PSR_c | PSR_x},
19178   {"xfs",  PSR_x | PSR_f | PSR_s},
19179   {"xfc",  PSR_x | PSR_f | PSR_c},
19180   {"xsf",  PSR_x | PSR_s | PSR_f},
19181   {"xsc",  PSR_x | PSR_s | PSR_c},
19182   {"xcf",  PSR_x | PSR_c | PSR_f},
19183   {"xcs",  PSR_x | PSR_c | PSR_s},
19184   {"cfs",  PSR_c | PSR_f | PSR_s},
19185   {"cfx",  PSR_c | PSR_f | PSR_x},
19186   {"csf",  PSR_c | PSR_s | PSR_f},
19187   {"csx",  PSR_c | PSR_s | PSR_x},
19188   {"cxf",  PSR_c | PSR_x | PSR_f},
19189   {"cxs",  PSR_c | PSR_x | PSR_s},
19190   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
19191   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
19192   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
19193   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
19194   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
19195   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
19196   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
19197   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
19198   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
19199   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
19200   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
19201   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
19202   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
19203   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
19204   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
19205   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
19206   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
19207   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
19208   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
19209   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
19210   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
19211   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
19212   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
19213   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
19214 };
19215
19216 /* Table of V7M psr names.  */
19217 static const struct asm_psr v7m_psrs[] =
19218 {
19219   {"apsr",         0x0 }, {"APSR",         0x0 },
19220   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
19221   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
19222   {"psr",          0x3 }, {"PSR",          0x3 },
19223   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
19224   {"ipsr",         0x5 }, {"IPSR",         0x5 },
19225   {"epsr",         0x6 }, {"EPSR",         0x6 },
19226   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
19227   {"msp",          0x8 }, {"MSP",          0x8 },
19228   {"psp",          0x9 }, {"PSP",          0x9 },
19229   {"msplim",       0xa }, {"MSPLIM",       0xa },
19230   {"psplim",       0xb }, {"PSPLIM",       0xb },
19231   {"primask",      0x10}, {"PRIMASK",      0x10},
19232   {"basepri",      0x11}, {"BASEPRI",      0x11},
19233   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
19234   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
19235   {"control",      0x14}, {"CONTROL",      0x14},
19236   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
19237   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
19238   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
19239   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
19240   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
19241   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
19242   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
19243   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
19244   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
19245 };
19246
19247 /* Table of all shift-in-operand names.  */
19248 static const struct asm_shift_name shift_names [] =
19249 {
19250   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
19251   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
19252   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
19253   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
19254   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
19255   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
19256 };
19257
19258 /* Table of all explicit relocation names.  */
19259 #ifdef OBJ_ELF
19260 static struct reloc_entry reloc_names[] =
19261 {
19262   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
19263   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
19264   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
19265   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
19266   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
19267   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
19268   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
19269   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
19270   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
19271   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
19272   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
19273   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
19274   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
19275         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
19276   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
19277         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
19278   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
19279         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
19280 };
19281 #endif
19282
19283 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
19284 static const struct asm_cond conds[] =
19285 {
19286   {"eq", 0x0},
19287   {"ne", 0x1},
19288   {"cs", 0x2}, {"hs", 0x2},
19289   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
19290   {"mi", 0x4},
19291   {"pl", 0x5},
19292   {"vs", 0x6},
19293   {"vc", 0x7},
19294   {"hi", 0x8},
19295   {"ls", 0x9},
19296   {"ge", 0xa},
19297   {"lt", 0xb},
19298   {"gt", 0xc},
19299   {"le", 0xd},
19300   {"al", 0xe}
19301 };
19302
19303 #define UL_BARRIER(L,U,CODE,FEAT) \
19304   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
19305   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
19306
19307 static struct asm_barrier_opt barrier_opt_names[] =
19308 {
19309   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
19310   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
19311   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
19312   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
19313   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
19314   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
19315   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
19316   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
19317   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
19318   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
19319   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
19320   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
19321   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
19322   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
19323   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
19324   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
19325 };
19326
19327 #undef UL_BARRIER
19328
19329 /* Table of ARM-format instructions.    */
19330
19331 /* Macros for gluing together operand strings.  N.B. In all cases
19332    other than OPS0, the trailing OP_stop comes from default
19333    zero-initialization of the unspecified elements of the array.  */
19334 #define OPS0()            { OP_stop, }
19335 #define OPS1(a)           { OP_##a, }
19336 #define OPS2(a,b)         { OP_##a,OP_##b, }
19337 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
19338 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
19339 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
19340 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
19341
19342 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
19343    This is useful when mixing operands for ARM and THUMB, i.e. using the
19344    MIX_ARM_THUMB_OPERANDS macro.
19345    In order to use these macros, prefix the number of operands with _
19346    e.g. _3.  */
19347 #define OPS_1(a)           { a, }
19348 #define OPS_2(a,b)         { a,b, }
19349 #define OPS_3(a,b,c)       { a,b,c, }
19350 #define OPS_4(a,b,c,d)     { a,b,c,d, }
19351 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
19352 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
19353
19354 /* These macros abstract out the exact format of the mnemonic table and
19355    save some repeated characters.  */
19356
19357 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
19358 #define TxCE(mnem, op, top, nops, ops, ae, te) \
19359   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
19360     THUMB_VARIANT, do_##ae, do_##te }
19361
19362 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
19363    a T_MNEM_xyz enumerator.  */
19364 #define TCE(mnem, aop, top, nops, ops, ae, te) \
19365       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
19366 #define tCE(mnem, aop, top, nops, ops, ae, te) \
19367       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19368
19369 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
19370    infix after the third character.  */
19371 #define TxC3(mnem, op, top, nops, ops, ae, te) \
19372   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
19373     THUMB_VARIANT, do_##ae, do_##te }
19374 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
19375   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
19376     THUMB_VARIANT, do_##ae, do_##te }
19377 #define TC3(mnem, aop, top, nops, ops, ae, te) \
19378       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
19379 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
19380       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
19381 #define tC3(mnem, aop, top, nops, ops, ae, te) \
19382       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19383 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
19384       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19385
19386 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
19387    field is still 0xE.  Many of the Thumb variants can be executed
19388    conditionally, so this is checked separately.  */
19389 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
19390   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19391     THUMB_VARIANT, do_##ae, do_##te }
19392
19393 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
19394    Used by mnemonics that have very minimal differences in the encoding for
19395    ARM and Thumb variants and can be handled in a common function.  */
19396 #define TUEc(mnem, op, top, nops, ops, en) \
19397   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19398     THUMB_VARIANT, do_##en, do_##en }
19399
19400 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
19401    condition code field.  */
19402 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
19403   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
19404     THUMB_VARIANT, do_##ae, do_##te }
19405
19406 /* ARM-only variants of all the above.  */
19407 #define CE(mnem,  op, nops, ops, ae)    \
19408   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19409
19410 #define C3(mnem, op, nops, ops, ae)     \
19411   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19412
19413 /* Legacy mnemonics that always have conditional infix after the third
19414    character.  */
19415 #define CL(mnem, op, nops, ops, ae)     \
19416   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19417     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19418
19419 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
19420 #define cCE(mnem,  op, nops, ops, ae)   \
19421   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19422
19423 /* Legacy coprocessor instructions where conditional infix and conditional
19424    suffix are ambiguous.  For consistency this includes all FPA instructions,
19425    not just the potentially ambiguous ones.  */
19426 #define cCL(mnem, op, nops, ops, ae)    \
19427   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19428     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19429
19430 /* Coprocessor, takes either a suffix or a position-3 infix
19431    (for an FPA corner case). */
19432 #define C3E(mnem, op, nops, ops, ae) \
19433   { mnem, OPS##nops ops, OT_csuf_or_in3, \
19434     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19435
19436 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
19437   { m1 #m2 m3, OPS##nops ops, \
19438     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
19439     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19440
19441 #define CM(m1, m2, op, nops, ops, ae)   \
19442   xCM_ (m1,   , m2, op, nops, ops, ae), \
19443   xCM_ (m1, eq, m2, op, nops, ops, ae), \
19444   xCM_ (m1, ne, m2, op, nops, ops, ae), \
19445   xCM_ (m1, cs, m2, op, nops, ops, ae), \
19446   xCM_ (m1, hs, m2, op, nops, ops, ae), \
19447   xCM_ (m1, cc, m2, op, nops, ops, ae), \
19448   xCM_ (m1, ul, m2, op, nops, ops, ae), \
19449   xCM_ (m1, lo, m2, op, nops, ops, ae), \
19450   xCM_ (m1, mi, m2, op, nops, ops, ae), \
19451   xCM_ (m1, pl, m2, op, nops, ops, ae), \
19452   xCM_ (m1, vs, m2, op, nops, ops, ae), \
19453   xCM_ (m1, vc, m2, op, nops, ops, ae), \
19454   xCM_ (m1, hi, m2, op, nops, ops, ae), \
19455   xCM_ (m1, ls, m2, op, nops, ops, ae), \
19456   xCM_ (m1, ge, m2, op, nops, ops, ae), \
19457   xCM_ (m1, lt, m2, op, nops, ops, ae), \
19458   xCM_ (m1, gt, m2, op, nops, ops, ae), \
19459   xCM_ (m1, le, m2, op, nops, ops, ae), \
19460   xCM_ (m1, al, m2, op, nops, ops, ae)
19461
19462 #define UE(mnem, op, nops, ops, ae)     \
19463   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19464
19465 #define UF(mnem, op, nops, ops, ae)     \
19466   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19467
19468 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
19469    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
19470    use the same encoding function for each.  */
19471 #define NUF(mnem, op, nops, ops, enc)                                   \
19472   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
19473     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19474
19475 /* Neon data processing, version which indirects through neon_enc_tab for
19476    the various overloaded versions of opcodes.  */
19477 #define nUF(mnem, op, nops, ops, enc)                                   \
19478   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
19479     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19480
19481 /* Neon insn with conditional suffix for the ARM version, non-overloaded
19482    version.  */
19483 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
19484   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
19485     THUMB_VARIANT, do_##enc, do_##enc }
19486
19487 #define NCE(mnem, op, nops, ops, enc)                                   \
19488    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19489
19490 #define NCEF(mnem, op, nops, ops, enc)                                  \
19491     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19492
19493 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
19494 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
19495   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
19496     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19497
19498 #define nCE(mnem, op, nops, ops, enc)                                   \
19499    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19500
19501 #define nCEF(mnem, op, nops, ops, enc)                                  \
19502     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19503
19504 #define do_0 0
19505
19506 static const struct asm_opcode insns[] =
19507 {
19508 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
19509 #define THUMB_VARIANT  & arm_ext_v4t
19510  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
19511  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
19512  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
19513  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
19514  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
19515  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
19516  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
19517  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
19518  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
19519  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
19520  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
19521  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
19522  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
19523  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
19524  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
19525  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
19526
19527  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
19528     for setting PSR flag bits.  They are obsolete in V6 and do not
19529     have Thumb equivalents. */
19530  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19531  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19532   CL("tstp",    110f000,           2, (RR, SH),      cmp),
19533  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19534  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19535   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
19536  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19537  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19538   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
19539
19540  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
19541  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
19542  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
19543  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
19544
19545  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
19546  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19547  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
19548                                                                 OP_RRnpc),
19549                                         OP_ADDRGLDR),ldst, t_ldst),
19550  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19551
19552  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19553  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19554  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19555  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19556  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19557  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19558
19559  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
19560  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
19561
19562   /* Pseudo ops.  */
19563  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
19564   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
19565  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
19566  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
19567
19568   /* Thumb-compatibility pseudo ops.  */
19569  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
19570  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
19571  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
19572  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
19573  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
19574  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
19575  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
19576  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
19577  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
19578  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
19579  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
19580  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
19581
19582  /* These may simplify to neg.  */
19583  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
19584  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
19585
19586 #undef THUMB_VARIANT
19587 #define THUMB_VARIANT  & arm_ext_os
19588
19589  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
19590  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
19591
19592 #undef  THUMB_VARIANT
19593 #define THUMB_VARIANT  & arm_ext_v6
19594
19595  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
19596
19597  /* V1 instructions with no Thumb analogue prior to V6T2.  */
19598 #undef  THUMB_VARIANT
19599 #define THUMB_VARIANT  & arm_ext_v6t2
19600
19601  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19602  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19603   CL("teqp",    130f000,           2, (RR, SH),      cmp),
19604
19605  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19606  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19607  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
19608  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19609
19610  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19611  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19612
19613  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19614  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19615
19616  /* V1 instructions with no Thumb analogue at all.  */
19617   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
19618   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
19619
19620   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
19621   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
19622   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
19623   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
19624   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
19625   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
19626   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
19627   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
19628
19629 #undef  ARM_VARIANT
19630 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
19631 #undef  THUMB_VARIANT
19632 #define THUMB_VARIANT  & arm_ext_v4t
19633
19634  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
19635  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
19636
19637 #undef  THUMB_VARIANT
19638 #define THUMB_VARIANT  & arm_ext_v6t2
19639
19640  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19641   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
19642
19643   /* Generic coprocessor instructions.  */
19644  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19645  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19646  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19647  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19648  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19649  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19650  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
19651
19652 #undef  ARM_VARIANT
19653 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
19654
19655   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19656   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19657
19658 #undef  ARM_VARIANT
19659 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
19660 #undef  THUMB_VARIANT
19661 #define THUMB_VARIANT  & arm_ext_msr
19662
19663  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
19664  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
19665
19666 #undef  ARM_VARIANT
19667 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
19668 #undef  THUMB_VARIANT
19669 #define THUMB_VARIANT  & arm_ext_v6t2
19670
19671  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19672   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19673  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19674   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19675  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19676   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19677  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19678   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19679
19680 #undef  ARM_VARIANT
19681 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
19682 #undef  THUMB_VARIANT
19683 #define THUMB_VARIANT  & arm_ext_v4t
19684
19685  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19686  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19687  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19688  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19689  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19690  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19691
19692 #undef  ARM_VARIANT
19693 #define ARM_VARIANT  & arm_ext_v4t_5
19694
19695   /* ARM Architecture 4T.  */
19696   /* Note: bx (and blx) are required on V5, even if the processor does
19697      not support Thumb.  */
19698  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
19699
19700 #undef  ARM_VARIANT
19701 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
19702 #undef  THUMB_VARIANT
19703 #define THUMB_VARIANT  & arm_ext_v5t
19704
19705   /* Note: blx has 2 variants; the .value coded here is for
19706      BLX(2).  Only this variant has conditional execution.  */
19707  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
19708  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
19709
19710 #undef  THUMB_VARIANT
19711 #define THUMB_VARIANT  & arm_ext_v6t2
19712
19713  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
19714  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19715  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19716  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19717  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19718  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19719  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19720  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19721
19722 #undef  ARM_VARIANT
19723 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
19724 #undef  THUMB_VARIANT
19725 #define THUMB_VARIANT  & arm_ext_v5exp
19726
19727  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19728  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19729  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19730  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19731
19732  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19733  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19734
19735  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19736  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19737  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19738  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19739
19740  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19741  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19742  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19743  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19744
19745  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19746  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19747
19748  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19749  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19750  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19751  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19752
19753 #undef  ARM_VARIANT
19754 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
19755 #undef  THUMB_VARIANT
19756 #define THUMB_VARIANT  & arm_ext_v6t2
19757
19758  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
19759  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
19760      ldrd, t_ldstd),
19761  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
19762                                        ADDRGLDRS), ldrd, t_ldstd),
19763
19764  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19765  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19766
19767 #undef  ARM_VARIANT
19768 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
19769
19770  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
19771
19772 #undef  ARM_VARIANT
19773 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
19774 #undef  THUMB_VARIANT
19775 #define THUMB_VARIANT  & arm_ext_v6
19776
19777  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19778  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19779  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19780  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19781  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19782  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19783  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19784  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19785  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19786  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
19787
19788 #undef  THUMB_VARIANT
19789 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19790
19791  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
19792  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19793                                       strex,  t_strex),
19794 #undef  THUMB_VARIANT
19795 #define THUMB_VARIANT  & arm_ext_v6t2
19796
19797  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19798  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19799
19800  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
19801  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
19802
19803 /*  ARM V6 not included in V7M.  */
19804 #undef  THUMB_VARIANT
19805 #define THUMB_VARIANT  & arm_ext_v6_notm
19806  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19807  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19808   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
19809   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
19810  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19811  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19812   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
19813  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19814   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
19815  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19816  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19817  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19818   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
19819   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
19820   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
19821   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
19822  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19823  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19824  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
19825
19826 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
19827 #undef  THUMB_VARIANT
19828 #define THUMB_VARIANT  & arm_ext_v6_dsp
19829  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
19830  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
19831  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19832  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19833  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19834  /* Old name for QASX.  */
19835  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19836  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19837  /* Old name for QSAX.  */
19838  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19839  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19840  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19841  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19842  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19843  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19844  /* Old name for SASX.  */
19845  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19846  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19847  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19848  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19849  /* Old name for SHASX.  */
19850  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19851  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19852  /* Old name for SHSAX.  */
19853  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19854  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19855  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19856  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19857  /* Old name for SSAX.  */
19858  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19859  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19860  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19861  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19862  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19863  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19864  /* Old name for UASX.  */
19865  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19866  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19867  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19868  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19869  /* Old name for UHASX.  */
19870  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19871  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19872  /* Old name for UHSAX.  */
19873  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19874  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19875  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19876  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19877  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19878  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19879  /* Old name for UQASX.  */
19880  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19881  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19882  /* Old name for UQSAX.  */
19883  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19884  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19885  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19886  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19887  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19888  /* Old name for USAX.  */
19889  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19890  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19891  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19892  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19893  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19894  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19895  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19896  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19897  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19898  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19899  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19900  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19901  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19902  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19903  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19904  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19905  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19906  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19907  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19908  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19909  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19910  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19911  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19912  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19913  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19914  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19915  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19916  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19917  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19918  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
19919  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
19920  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
19921  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
19922  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
19923
19924 #undef  ARM_VARIANT
19925 #define ARM_VARIANT   & arm_ext_v6k
19926 #undef  THUMB_VARIANT
19927 #define THUMB_VARIANT & arm_ext_v6k
19928
19929  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
19930  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
19931  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
19932  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
19933
19934 #undef  THUMB_VARIANT
19935 #define THUMB_VARIANT  & arm_ext_v6_notm
19936  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
19937                                       ldrexd, t_ldrexd),
19938  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
19939                                        RRnpcb), strexd, t_strexd),
19940
19941 #undef  THUMB_VARIANT
19942 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19943  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
19944      rd_rn,  rd_rn),
19945  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
19946      rd_rn,  rd_rn),
19947  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19948      strex, t_strexbh),
19949  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19950      strex, t_strexbh),
19951  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
19952
19953 #undef  ARM_VARIANT
19954 #define ARM_VARIANT    & arm_ext_sec
19955 #undef  THUMB_VARIANT
19956 #define THUMB_VARIANT  & arm_ext_sec
19957
19958  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
19959
19960 #undef  ARM_VARIANT
19961 #define ARM_VARIANT    & arm_ext_virt
19962 #undef  THUMB_VARIANT
19963 #define THUMB_VARIANT    & arm_ext_virt
19964
19965  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
19966  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
19967
19968 #undef  ARM_VARIANT
19969 #define ARM_VARIANT    & arm_ext_pan
19970 #undef  THUMB_VARIANT
19971 #define THUMB_VARIANT  & arm_ext_pan
19972
19973  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
19974
19975 #undef  ARM_VARIANT
19976 #define ARM_VARIANT    & arm_ext_v6t2
19977 #undef  THUMB_VARIANT
19978 #define THUMB_VARIANT  & arm_ext_v6t2
19979
19980  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
19981  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
19982  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19983  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19984
19985  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19986  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
19987
19988  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19989  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19990  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19991  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19992
19993 #undef  ARM_VARIANT
19994 #define ARM_VARIANT    & arm_ext_v3
19995 #undef  THUMB_VARIANT
19996 #define THUMB_VARIANT  & arm_ext_v6t2
19997
19998  TUE("csdb",    320f014, f3af8014, 0, (), noargs, t_csdb),
19999
20000 #undef  ARM_VARIANT
20001 #define ARM_VARIANT    & arm_ext_v6t2
20002 #undef  THUMB_VARIANT
20003 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
20004  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
20005  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
20006
20007  /* Thumb-only instructions.  */
20008 #undef  ARM_VARIANT
20009 #define ARM_VARIANT NULL
20010   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
20011   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
20012
20013  /* ARM does not really have an IT instruction, so always allow it.
20014     The opcode is copied from Thumb in order to allow warnings in
20015     -mimplicit-it=[never | arm] modes.  */
20016 #undef  ARM_VARIANT
20017 #define ARM_VARIANT  & arm_ext_v1
20018 #undef  THUMB_VARIANT
20019 #define THUMB_VARIANT  & arm_ext_v6t2
20020
20021  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
20022  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
20023  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
20024  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
20025  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
20026  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
20027  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
20028  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
20029  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
20030  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
20031  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
20032  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
20033  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
20034  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
20035  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
20036  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
20037  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
20038  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
20039
20040  /* Thumb2 only instructions.  */
20041 #undef  ARM_VARIANT
20042 #define ARM_VARIANT  NULL
20043
20044  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
20045  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
20046  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
20047  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
20048  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
20049  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
20050
20051  /* Hardware division instructions.  */
20052 #undef  ARM_VARIANT
20053 #define ARM_VARIANT    & arm_ext_adiv
20054 #undef  THUMB_VARIANT
20055 #define THUMB_VARIANT  & arm_ext_div
20056
20057  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
20058  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
20059
20060  /* ARM V6M/V7 instructions.  */
20061 #undef  ARM_VARIANT
20062 #define ARM_VARIANT    & arm_ext_barrier
20063 #undef  THUMB_VARIANT
20064 #define THUMB_VARIANT  & arm_ext_barrier
20065
20066  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
20067  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
20068  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
20069
20070  /* ARM V7 instructions.  */
20071 #undef  ARM_VARIANT
20072 #define ARM_VARIANT    & arm_ext_v7
20073 #undef  THUMB_VARIANT
20074 #define THUMB_VARIANT  & arm_ext_v7
20075
20076  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
20077  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
20078
20079 #undef  ARM_VARIANT
20080 #define ARM_VARIANT    & arm_ext_mp
20081 #undef  THUMB_VARIANT
20082 #define THUMB_VARIANT  & arm_ext_mp
20083
20084  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
20085
20086  /* AArchv8 instructions.  */
20087 #undef  ARM_VARIANT
20088 #define ARM_VARIANT   & arm_ext_v8
20089
20090 /* Instructions shared between armv8-a and armv8-m.  */
20091 #undef  THUMB_VARIANT
20092 #define THUMB_VARIANT & arm_ext_atomics
20093
20094  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20095  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20096  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20097  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20098  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20099  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
20100  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20101  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
20102  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
20103  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
20104                                                         stlex,  t_stlex),
20105  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
20106                                                         stlex, t_stlex),
20107  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
20108                                                         stlex, t_stlex),
20109 #undef  THUMB_VARIANT
20110 #define THUMB_VARIANT & arm_ext_v8
20111
20112  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
20113  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
20114  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
20115                                                         ldrexd, t_ldrexd),
20116  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
20117                                                         strexd, t_strexd),
20118  /* ARMv8 T32 only.  */
20119 #undef  ARM_VARIANT
20120 #define ARM_VARIANT  NULL
20121  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
20122  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
20123  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
20124
20125   /* FP for ARMv8.  */
20126 #undef  ARM_VARIANT
20127 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
20128 #undef  THUMB_VARIANT
20129 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
20130
20131   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
20132   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
20133   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
20134   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
20135   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
20136   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
20137   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
20138   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
20139   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
20140   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
20141   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
20142   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
20143   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
20144   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
20145   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
20146   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
20147   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
20148
20149   /* Crypto v1 extensions.  */
20150 #undef  ARM_VARIANT
20151 #define ARM_VARIANT & fpu_crypto_ext_armv8
20152 #undef  THUMB_VARIANT
20153 #define THUMB_VARIANT & fpu_crypto_ext_armv8
20154
20155   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
20156   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
20157   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
20158   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
20159   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
20160   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
20161   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
20162   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
20163   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
20164   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
20165   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
20166   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
20167   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
20168   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
20169
20170 #undef  ARM_VARIANT
20171 #define ARM_VARIANT   & crc_ext_armv8
20172 #undef  THUMB_VARIANT
20173 #define THUMB_VARIANT & crc_ext_armv8
20174   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
20175   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
20176   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
20177   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
20178   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
20179   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
20180
20181  /* ARMv8.2 RAS extension.  */
20182 #undef  ARM_VARIANT
20183 #define ARM_VARIANT   & arm_ext_ras
20184 #undef  THUMB_VARIANT
20185 #define THUMB_VARIANT & arm_ext_ras
20186  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
20187
20188 #undef  ARM_VARIANT
20189 #define ARM_VARIANT   & arm_ext_v8_3
20190 #undef  THUMB_VARIANT
20191 #define THUMB_VARIANT & arm_ext_v8_3
20192  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
20193  NUF (vcmla, 0, 4, (RNDQ, RNDQ, RNDQ_RNSC, EXPi), vcmla),
20194  NUF (vcadd, 0, 4, (RNDQ, RNDQ, RNDQ, EXPi), vcadd),
20195
20196 #undef  ARM_VARIANT
20197 #define ARM_VARIANT   & fpu_neon_ext_dotprod
20198 #undef  THUMB_VARIANT
20199 #define THUMB_VARIANT & fpu_neon_ext_dotprod
20200  NUF (vsdot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_s),
20201  NUF (vudot, d00, 3, (RNDQ, RNDQ, RNDQ_RNSC), neon_dotproduct_u),
20202
20203 #undef  ARM_VARIANT
20204 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
20205 #undef  THUMB_VARIANT
20206 #define THUMB_VARIANT NULL
20207
20208  cCE("wfs",     e200110, 1, (RR),            rd),
20209  cCE("rfs",     e300110, 1, (RR),            rd),
20210  cCE("wfc",     e400110, 1, (RR),            rd),
20211  cCE("rfc",     e500110, 1, (RR),            rd),
20212
20213  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20214  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20215  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20216  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20217
20218  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20219  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20220  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20221  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
20222
20223  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
20224  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
20225  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
20226  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
20227  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
20228  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
20229  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
20230  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
20231  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
20232  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
20233  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
20234  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
20235
20236  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
20237  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
20238  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
20239  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
20240  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
20241  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
20242  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
20243  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
20244  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
20245  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
20246  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
20247  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
20248
20249  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
20250  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
20251  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
20252  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
20253  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
20254  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
20255  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
20256  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
20257  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
20258  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
20259  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
20260  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
20261
20262  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
20263  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
20264  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
20265  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
20266  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
20267  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
20268  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
20269  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
20270  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
20271  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
20272  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
20273  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
20274
20275  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
20276  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
20277  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
20278  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
20279  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
20280  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
20281  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
20282  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
20283  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
20284  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
20285  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
20286  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
20287
20288  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
20289  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
20290  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
20291  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
20292  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
20293  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
20294  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
20295  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
20296  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
20297  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
20298  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
20299  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
20300
20301  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
20302  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
20303  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
20304  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
20305  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
20306  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
20307  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
20308  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
20309  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
20310  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
20311  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
20312  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
20313
20314  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
20315  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
20316  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
20317  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
20318  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
20319  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
20320  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
20321  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
20322  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
20323  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
20324  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
20325  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
20326
20327  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
20328  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
20329  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
20330  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
20331  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
20332  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
20333  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
20334  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
20335  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
20336  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
20337  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
20338  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
20339
20340  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
20341  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
20342  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
20343  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
20344  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
20345  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
20346  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
20347  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
20348  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
20349  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
20350  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
20351  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
20352
20353  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
20354  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
20355  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
20356  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
20357  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
20358  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
20359  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
20360  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
20361  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
20362  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
20363  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
20364  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
20365
20366  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
20367  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
20368  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
20369  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
20370  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
20371  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
20372  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
20373  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
20374  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
20375  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
20376  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
20377  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
20378
20379  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
20380  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
20381  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
20382  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
20383  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
20384  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
20385  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
20386  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
20387  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
20388  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
20389  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
20390  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
20391
20392  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
20393  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
20394  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
20395  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
20396  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
20397  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
20398  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
20399  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
20400  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
20401  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
20402  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
20403  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
20404
20405  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
20406  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
20407  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
20408  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
20409  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
20410  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
20411  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
20412  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
20413  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
20414  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
20415  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
20416  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
20417
20418  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
20419  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
20420  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
20421  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
20422  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
20423  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
20424  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
20425  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
20426  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
20427  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
20428  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
20429  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
20430
20431  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
20432  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
20433  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
20434  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
20435  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
20436  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20437  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20438  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20439  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
20440  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
20441  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
20442  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
20443
20444  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
20445  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
20446  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
20447  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
20448  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
20449  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20450  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20451  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20452  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
20453  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
20454  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
20455  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
20456
20457  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
20458  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
20459  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
20460  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
20461  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
20462  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20463  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20464  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20465  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
20466  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
20467  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
20468  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
20469
20470  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
20471  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
20472  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
20473  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
20474  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
20475  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20476  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20477  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20478  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
20479  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
20480  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
20481  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
20482
20483  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
20484  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
20485  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
20486  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
20487  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
20488  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20489  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20490  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20491  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
20492  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
20493  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
20494  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
20495
20496  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
20497  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
20498  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
20499  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
20500  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
20501  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20502  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20503  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20504  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
20505  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
20506  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
20507  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
20508
20509  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
20510  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
20511  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
20512  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
20513  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
20514  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20515  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20516  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20517  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
20518  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
20519  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
20520  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
20521
20522  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
20523  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
20524  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
20525  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
20526  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
20527  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20528  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20529  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20530  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
20531  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
20532  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
20533  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
20534
20535  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
20536  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
20537  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
20538  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
20539  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
20540  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20541  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20542  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20543  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
20544  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
20545  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
20546  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
20547
20548  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
20549  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
20550  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
20551  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
20552  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
20553  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20554  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20555  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20556  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
20557  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
20558  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
20559  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
20560
20561  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20562  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20563  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20564  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20565  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20566  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20567  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20568  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20569  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20570  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20571  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20572  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20573
20574  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20575  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20576  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20577  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20578  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20579  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20580  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20581  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20582  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20583  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20584  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20585  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20586
20587  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20588  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20589  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20590  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20591  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20592  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20593  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20594  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20595  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20596  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20597  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20598  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20599
20600  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
20601  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
20602  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
20603  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
20604
20605  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
20606  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
20607  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
20608  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
20609  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
20610  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
20611  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
20612  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
20613  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
20614  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
20615  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
20616  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
20617
20618   /* The implementation of the FIX instruction is broken on some
20619      assemblers, in that it accepts a precision specifier as well as a
20620      rounding specifier, despite the fact that this is meaningless.
20621      To be more compatible, we accept it as well, though of course it
20622      does not set any bits.  */
20623  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
20624  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
20625  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
20626  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
20627  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
20628  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
20629  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
20630  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
20631  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
20632  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
20633  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
20634  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
20635  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
20636
20637   /* Instructions that were new with the real FPA, call them V2.  */
20638 #undef  ARM_VARIANT
20639 #define ARM_VARIANT  & fpu_fpa_ext_v2
20640
20641  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20642  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20643  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20644  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20645  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20646  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20647
20648 #undef  ARM_VARIANT
20649 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
20650
20651   /* Moves and type conversions.  */
20652  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
20653  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
20654  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
20655  cCE("fmstat",  ef1fa10, 0, (),               noargs),
20656  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
20657  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
20658  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20659  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
20660  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20661  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20662  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20663  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20664  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
20665  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
20666
20667   /* Memory operations.  */
20668  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20669  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20670  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20671  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20672  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20673  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20674  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20675  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20676  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20677  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20678  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20679  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20680  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20681  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20682  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20683  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20684  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20685  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20686
20687   /* Monadic operations.  */
20688  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20689  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
20690  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20691
20692   /* Dyadic operations.  */
20693  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20694  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20695  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20696  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20697  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20698  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20699  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20700  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20701  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20702
20703   /* Comparisons.  */
20704  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
20705  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
20706  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20707  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
20708
20709  /* Double precision load/store are still present on single precision
20710     implementations.  */
20711  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20712  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20713  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20714  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20715  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20716  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20717  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20718  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20719  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20720  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20721
20722 #undef  ARM_VARIANT
20723 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
20724
20725   /* Moves and type conversions.  */
20726  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20727  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20728  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20729  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20730  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20731  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20732  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20733  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20734  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20735  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20736  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20737  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20738  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20739
20740   /* Monadic operations.  */
20741  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20742  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20743  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20744
20745   /* Dyadic operations.  */
20746  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20747  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20748  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20749  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20750  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20751  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20752  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20753  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20754  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20755
20756   /* Comparisons.  */
20757  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20758  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
20759  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20760  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
20761
20762 #undef  ARM_VARIANT
20763 #define ARM_VARIANT  & fpu_vfp_ext_v2
20764
20765  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
20766  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
20767  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
20768  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
20769
20770 /* Instructions which may belong to either the Neon or VFP instruction sets.
20771    Individual encoder functions perform additional architecture checks.  */
20772 #undef  ARM_VARIANT
20773 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
20774 #undef  THUMB_VARIANT
20775 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
20776
20777   /* These mnemonics are unique to VFP.  */
20778  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
20779  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
20780  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20781  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20782  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20783  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20784  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20785  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
20786  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
20787  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
20788
20789   /* Mnemonics shared by Neon and VFP.  */
20790  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
20791  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20792  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20793
20794  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20795  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20796
20797  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20798  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20799
20800  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20801  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20802  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20803  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20804  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20805  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20806  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20807  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20808
20809  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
20810  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
20811  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
20812  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
20813
20814
20815   /* NOTE: All VMOV encoding is special-cased!  */
20816  NCE(vmov,      0,       1, (VMOV), neon_mov),
20817  NCE(vmovq,     0,       1, (VMOV), neon_mov),
20818
20819 #undef  ARM_VARIANT
20820 #define ARM_VARIANT    & arm_ext_fp16
20821 #undef  THUMB_VARIANT
20822 #define THUMB_VARIANT  & arm_ext_fp16
20823  /* New instructions added from v8.2, allowing the extraction and insertion of
20824     the upper 16 bits of a 32-bit vector register.  */
20825  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
20826  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
20827
20828  /* New backported fma/fms instructions optional in v8.2.  */
20829  NCE (vfmal, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmal),
20830  NCE (vfmsl, 810, 3, (RNDQ, RNSD, RNSD_RNSC), neon_vfmsl),
20831
20832 #undef  THUMB_VARIANT
20833 #define THUMB_VARIANT  & fpu_neon_ext_v1
20834 #undef  ARM_VARIANT
20835 #define ARM_VARIANT    & fpu_neon_ext_v1
20836
20837   /* Data processing with three registers of the same length.  */
20838   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
20839  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
20840  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
20841  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20842  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20843  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20844  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20845  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20846  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20847   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
20848  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20849  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20850  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20851  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20852  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20853  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20854  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20855  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20856   /* If not immediate, fall back to neon_dyadic_i64_su.
20857      shl_imm should accept I8 I16 I32 I64,
20858      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
20859  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
20860  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
20861  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
20862  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
20863   /* Logic ops, types optional & ignored.  */
20864  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20865  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20866  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20867  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20868  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20869  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20870  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20871  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20872  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
20873  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
20874   /* Bitfield ops, untyped.  */
20875  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20876  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20877  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20878  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20879  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20880  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20881   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
20882  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20883  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20884  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20885  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20886  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20887  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20888   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
20889      back to neon_dyadic_if_su.  */
20890  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20891  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20892  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20893  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20894  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20895  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20896  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20897  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20898   /* Comparison. Type I8 I16 I32 F32.  */
20899  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
20900  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
20901   /* As above, D registers only.  */
20902  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20903  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20904   /* Int and float variants, signedness unimportant.  */
20905  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20906  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20907  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
20908   /* Add/sub take types I8 I16 I32 I64 F32.  */
20909  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20910  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20911   /* vtst takes sizes 8, 16, 32.  */
20912  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
20913  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
20914   /* VMUL takes I8 I16 I32 F32 P8.  */
20915  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
20916   /* VQD{R}MULH takes S16 S32.  */
20917  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20918  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20919  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20920  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20921  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20922  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20923  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20924  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20925  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20926  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20927  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20928  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20929  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20930  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20931  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20932  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20933  /* ARM v8.1 extension.  */
20934  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20935  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20936  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20937  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20938
20939   /* Two address, int/float. Types S8 S16 S32 F32.  */
20940  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
20941  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
20942
20943   /* Data processing with two registers and a shift amount.  */
20944   /* Right shifts, and variants with rounding.
20945      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
20946  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20947  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20948  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20949  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20950  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20951  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20952  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20953  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20954   /* Shift and insert. Sizes accepted 8 16 32 64.  */
20955  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
20956  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
20957  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
20958  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
20959   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
20960  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
20961  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
20962   /* Right shift immediate, saturating & narrowing, with rounding variants.
20963      Types accepted S16 S32 S64 U16 U32 U64.  */
20964  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20965  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20966   /* As above, unsigned. Types accepted S16 S32 S64.  */
20967  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20968  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20969   /* Right shift narrowing. Types accepted I16 I32 I64.  */
20970  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20971  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20972   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
20973  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
20974   /* CVT with optional immediate for fixed-point variant.  */
20975  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
20976
20977  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
20978  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
20979
20980   /* Data processing, three registers of different lengths.  */
20981   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
20982  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
20983  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
20984  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
20985  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
20986   /* If not scalar, fall back to neon_dyadic_long.
20987      Vector types as above, scalar types S16 S32 U16 U32.  */
20988  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20989  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20990   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
20991  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20992  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20993   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
20994  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20995  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20996  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20997  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20998   /* Saturating doubling multiplies. Types S16 S32.  */
20999  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21000  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21001  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
21002   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
21003      S16 S32 U16 U32.  */
21004  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
21005
21006   /* Extract. Size 8.  */
21007  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
21008  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
21009
21010   /* Two registers, miscellaneous.  */
21011   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
21012  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
21013  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
21014  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
21015  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
21016  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
21017  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
21018   /* Vector replicate. Sizes 8 16 32.  */
21019  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
21020  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
21021   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
21022  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
21023   /* VMOVN. Types I16 I32 I64.  */
21024  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
21025   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
21026  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
21027   /* VQMOVUN. Types S16 S32 S64.  */
21028  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
21029   /* VZIP / VUZP. Sizes 8 16 32.  */
21030  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
21031  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
21032  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
21033  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
21034   /* VQABS / VQNEG. Types S8 S16 S32.  */
21035  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
21036  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
21037  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
21038  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
21039   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
21040  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
21041  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
21042  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
21043  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
21044   /* Reciprocal estimates.  Types U32 F16 F32.  */
21045  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
21046  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
21047  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
21048  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
21049   /* VCLS. Types S8 S16 S32.  */
21050  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
21051  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
21052   /* VCLZ. Types I8 I16 I32.  */
21053  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
21054  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
21055   /* VCNT. Size 8.  */
21056  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
21057  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
21058   /* Two address, untyped.  */
21059  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
21060  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
21061   /* VTRN. Sizes 8 16 32.  */
21062  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
21063  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
21064
21065   /* Table lookup. Size 8.  */
21066  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
21067  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
21068
21069 #undef  THUMB_VARIANT
21070 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
21071 #undef  ARM_VARIANT
21072 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
21073
21074   /* Neon element/structure load/store.  */
21075  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21076  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21077  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21078  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21079  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21080  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21081  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21082  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
21083
21084 #undef  THUMB_VARIANT
21085 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
21086 #undef  ARM_VARIANT
21087 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
21088  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
21089  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21090  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21091  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21092  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21093  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21094  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21095  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
21096  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
21097
21098 #undef  THUMB_VARIANT
21099 #define THUMB_VARIANT  & fpu_vfp_ext_v3
21100 #undef  ARM_VARIANT
21101 #define ARM_VARIANT    & fpu_vfp_ext_v3
21102
21103  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
21104  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21105  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21106  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21107  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21108  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21109  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21110  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
21111  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
21112
21113 #undef  ARM_VARIANT
21114 #define ARM_VARIANT    & fpu_vfp_ext_fma
21115 #undef  THUMB_VARIANT
21116 #define THUMB_VARIANT  & fpu_vfp_ext_fma
21117  /* Mnemonics shared by Neon and VFP.  These are included in the
21118     VFP FMA variant; NEON and VFP FMA always includes the NEON
21119     FMA instructions.  */
21120  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
21121  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
21122  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
21123     the v form should always be used.  */
21124  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21125  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
21126  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21127  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
21128  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21129  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
21130
21131 #undef THUMB_VARIANT
21132 #undef  ARM_VARIANT
21133 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
21134
21135  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21136  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21137  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21138  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21139  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21140  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
21141  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
21142  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
21143
21144 #undef  ARM_VARIANT
21145 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
21146
21147  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
21148  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
21149  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
21150  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
21151  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
21152  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
21153  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
21154  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
21155  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
21156  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21157  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21158  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21159  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21160  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21161  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
21162  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21163  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21164  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
21165  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
21166  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
21167  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21168  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21169  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21170  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21171  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21172  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
21173  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
21174  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
21175  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
21176  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
21177  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
21178  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
21179  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
21180  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
21181  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
21182  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
21183  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
21184  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21185  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21186  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21187  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21188  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21189  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21190  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21191  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21192  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21193  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
21194  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21195  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21196  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21197  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21198  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21199  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21200  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21201  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21202  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21203  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21204  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21205  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21206  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21207  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21208  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21209  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21210  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21211  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21212  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21213  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21214  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21215  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
21216  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
21217  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21218  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21219  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21220  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21221  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21222  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21223  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21224  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21225  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21226  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21227  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21228  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21229  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21230  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21231  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21232  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21233  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21234  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21235  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
21236  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21237  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21238  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21239  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21240  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21241  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21242  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21243  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21244  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21245  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21246  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21247  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21248  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21249  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21250  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21251  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21252  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21253  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21254  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21255  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21256  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21257  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
21258  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21259  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21260  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21261  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21262  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21263  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21264  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21265  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21266  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21267  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21268  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21269  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21270  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21271  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21272  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21273  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21274  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
21275  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
21276  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21277  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
21278  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
21279  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
21280  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21281  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21282  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21283  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21284  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21285  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21286  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21287  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21288  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21289  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
21290  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
21291  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
21292  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
21293  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
21294  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
21295  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21296  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21297  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21298  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
21299  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
21300  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
21301  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
21302  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
21303  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
21304  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21305  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21306  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
21307  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21308  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
21309
21310 #undef  ARM_VARIANT
21311 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
21312
21313  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
21314  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
21315  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
21316  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
21317  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
21318  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
21319  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21320  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21321  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21322  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21323  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21324  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21325  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21326  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21327  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21328  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21329  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21330  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21331  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21332  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21333  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
21334  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21335  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21336  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21337  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21338  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21339  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21340  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21341  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21342  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21343  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21344  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21345  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21346  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21347  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21348  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21349  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21350  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21351  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21352  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21353  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21354  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21355  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21356  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21357  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21358  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21359  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21360  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21361  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21362  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21363  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21364  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21365  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21366  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21367  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21368  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21369  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21370
21371 #undef  ARM_VARIANT
21372 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
21373
21374  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21375  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21376  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21377  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21378  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21379  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21380  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21381  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21382  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
21383  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
21384  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
21385  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
21386  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
21387  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
21388  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
21389  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
21390  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
21391  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
21392  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
21393  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
21394  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
21395  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
21396  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
21397  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
21398  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
21399  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
21400  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
21401  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
21402  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
21403  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
21404  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
21405  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
21406  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
21407  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
21408  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
21409  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
21410  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
21411  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
21412  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
21413  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
21414  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
21415  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
21416  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
21417  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
21418  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
21419  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
21420  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
21421  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
21422  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
21423  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
21424  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
21425  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
21426  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
21427  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
21428  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
21429  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21430  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
21431  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21432  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
21433  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
21434  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
21435  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
21436  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
21437  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
21438  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21439  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21440  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21441  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21442  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21443  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21444  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21445  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21446  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21447  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21448  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21449  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21450
21451  /* ARMv8-M instructions.  */
21452 #undef  ARM_VARIANT
21453 #define ARM_VARIANT NULL
21454 #undef  THUMB_VARIANT
21455 #define THUMB_VARIANT & arm_ext_v8m
21456  TUE("sg", 0, e97fe97f, 0, (), 0, noargs),
21457  TUE("blxns", 0, 4784, 1, (RRnpc), 0, t_blx),
21458  TUE("bxns", 0, 4704, 1, (RRnpc), 0, t_bx),
21459  TUE("tt", 0, e840f000, 2, (RRnpc, RRnpc), 0, tt),
21460  TUE("ttt", 0, e840f040, 2, (RRnpc, RRnpc), 0, tt),
21461  TUE("tta", 0, e840f080, 2, (RRnpc, RRnpc), 0, tt),
21462  TUE("ttat", 0, e840f0c0, 2, (RRnpc, RRnpc), 0, tt),
21463
21464  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
21465     instructions behave as nop if no VFP is present.  */
21466 #undef  THUMB_VARIANT
21467 #define THUMB_VARIANT & arm_ext_v8m_main
21468  TUEc("vlldm",  0,       ec300a00, 1, (RRnpc),  rn),
21469  TUEc("vlstm",  0,       ec200a00, 1, (RRnpc),  rn),
21470 };
21471 #undef ARM_VARIANT
21472 #undef THUMB_VARIANT
21473 #undef TCE
21474 #undef TUE
21475 #undef TUF
21476 #undef TCC
21477 #undef cCE
21478 #undef cCL
21479 #undef C3E
21480 #undef CE
21481 #undef CM
21482 #undef UE
21483 #undef UF
21484 #undef UT
21485 #undef NUF
21486 #undef nUF
21487 #undef NCE
21488 #undef nCE
21489 #undef OPS0
21490 #undef OPS1
21491 #undef OPS2
21492 #undef OPS3
21493 #undef OPS4
21494 #undef OPS5
21495 #undef OPS6
21496 #undef do_0
21497 \f
21498 /* MD interface: bits in the object file.  */
21499
21500 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
21501    for use in the a.out file, and stores them in the array pointed to by buf.
21502    This knows about the endian-ness of the target machine and does
21503    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
21504    2 (short) and 4 (long)  Floating numbers are put out as a series of
21505    LITTLENUMS (shorts, here at least).  */
21506
21507 void
21508 md_number_to_chars (char * buf, valueT val, int n)
21509 {
21510   if (target_big_endian)
21511     number_to_chars_bigendian (buf, val, n);
21512   else
21513     number_to_chars_littleendian (buf, val, n);
21514 }
21515
21516 static valueT
21517 md_chars_to_number (char * buf, int n)
21518 {
21519   valueT result = 0;
21520   unsigned char * where = (unsigned char *) buf;
21521
21522   if (target_big_endian)
21523     {
21524       while (n--)
21525         {
21526           result <<= 8;
21527           result |= (*where++ & 255);
21528         }
21529     }
21530   else
21531     {
21532       while (n--)
21533         {
21534           result <<= 8;
21535           result |= (where[n] & 255);
21536         }
21537     }
21538
21539   return result;
21540 }
21541
21542 /* MD interface: Sections.  */
21543
21544 /* Calculate the maximum variable size (i.e., excluding fr_fix)
21545    that an rs_machine_dependent frag may reach.  */
21546
21547 unsigned int
21548 arm_frag_max_var (fragS *fragp)
21549 {
21550   /* We only use rs_machine_dependent for variable-size Thumb instructions,
21551      which are either THUMB_SIZE (2) or INSN_SIZE (4).
21552
21553      Note that we generate relaxable instructions even for cases that don't
21554      really need it, like an immediate that's a trivial constant.  So we're
21555      overestimating the instruction size for some of those cases.  Rather
21556      than putting more intelligence here, it would probably be better to
21557      avoid generating a relaxation frag in the first place when it can be
21558      determined up front that a short instruction will suffice.  */
21559
21560   gas_assert (fragp->fr_type == rs_machine_dependent);
21561   return INSN_SIZE;
21562 }
21563
21564 /* Estimate the size of a frag before relaxing.  Assume everything fits in
21565    2 bytes.  */
21566
21567 int
21568 md_estimate_size_before_relax (fragS * fragp,
21569                                segT    segtype ATTRIBUTE_UNUSED)
21570 {
21571   fragp->fr_var = 2;
21572   return 2;
21573 }
21574
21575 /* Convert a machine dependent frag.  */
21576
21577 void
21578 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
21579 {
21580   unsigned long insn;
21581   unsigned long old_op;
21582   char *buf;
21583   expressionS exp;
21584   fixS *fixp;
21585   int reloc_type;
21586   int pc_rel;
21587   int opcode;
21588
21589   buf = fragp->fr_literal + fragp->fr_fix;
21590
21591   old_op = bfd_get_16(abfd, buf);
21592   if (fragp->fr_symbol)
21593     {
21594       exp.X_op = O_symbol;
21595       exp.X_add_symbol = fragp->fr_symbol;
21596     }
21597   else
21598     {
21599       exp.X_op = O_constant;
21600     }
21601   exp.X_add_number = fragp->fr_offset;
21602   opcode = fragp->fr_subtype;
21603   switch (opcode)
21604     {
21605     case T_MNEM_ldr_pc:
21606     case T_MNEM_ldr_pc2:
21607     case T_MNEM_ldr_sp:
21608     case T_MNEM_str_sp:
21609     case T_MNEM_ldr:
21610     case T_MNEM_ldrb:
21611     case T_MNEM_ldrh:
21612     case T_MNEM_str:
21613     case T_MNEM_strb:
21614     case T_MNEM_strh:
21615       if (fragp->fr_var == 4)
21616         {
21617           insn = THUMB_OP32 (opcode);
21618           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
21619             {
21620               insn |= (old_op & 0x700) << 4;
21621             }
21622           else
21623             {
21624               insn |= (old_op & 7) << 12;
21625               insn |= (old_op & 0x38) << 13;
21626             }
21627           insn |= 0x00000c00;
21628           put_thumb32_insn (buf, insn);
21629           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
21630         }
21631       else
21632         {
21633           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
21634         }
21635       pc_rel = (opcode == T_MNEM_ldr_pc2);
21636       break;
21637     case T_MNEM_adr:
21638       if (fragp->fr_var == 4)
21639         {
21640           insn = THUMB_OP32 (opcode);
21641           insn |= (old_op & 0xf0) << 4;
21642           put_thumb32_insn (buf, insn);
21643           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
21644         }
21645       else
21646         {
21647           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21648           exp.X_add_number -= 4;
21649         }
21650       pc_rel = 1;
21651       break;
21652     case T_MNEM_mov:
21653     case T_MNEM_movs:
21654     case T_MNEM_cmp:
21655     case T_MNEM_cmn:
21656       if (fragp->fr_var == 4)
21657         {
21658           int r0off = (opcode == T_MNEM_mov
21659                        || opcode == T_MNEM_movs) ? 0 : 8;
21660           insn = THUMB_OP32 (opcode);
21661           insn = (insn & 0xe1ffffff) | 0x10000000;
21662           insn |= (old_op & 0x700) << r0off;
21663           put_thumb32_insn (buf, insn);
21664           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21665         }
21666       else
21667         {
21668           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
21669         }
21670       pc_rel = 0;
21671       break;
21672     case T_MNEM_b:
21673       if (fragp->fr_var == 4)
21674         {
21675           insn = THUMB_OP32(opcode);
21676           put_thumb32_insn (buf, insn);
21677           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
21678         }
21679       else
21680         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
21681       pc_rel = 1;
21682       break;
21683     case T_MNEM_bcond:
21684       if (fragp->fr_var == 4)
21685         {
21686           insn = THUMB_OP32(opcode);
21687           insn |= (old_op & 0xf00) << 14;
21688           put_thumb32_insn (buf, insn);
21689           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
21690         }
21691       else
21692         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
21693       pc_rel = 1;
21694       break;
21695     case T_MNEM_add_sp:
21696     case T_MNEM_add_pc:
21697     case T_MNEM_inc_sp:
21698     case T_MNEM_dec_sp:
21699       if (fragp->fr_var == 4)
21700         {
21701           /* ??? Choose between add and addw.  */
21702           insn = THUMB_OP32 (opcode);
21703           insn |= (old_op & 0xf0) << 4;
21704           put_thumb32_insn (buf, insn);
21705           if (opcode == T_MNEM_add_pc)
21706             reloc_type = BFD_RELOC_ARM_T32_IMM12;
21707           else
21708             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21709         }
21710       else
21711         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21712       pc_rel = 0;
21713       break;
21714
21715     case T_MNEM_addi:
21716     case T_MNEM_addis:
21717     case T_MNEM_subi:
21718     case T_MNEM_subis:
21719       if (fragp->fr_var == 4)
21720         {
21721           insn = THUMB_OP32 (opcode);
21722           insn |= (old_op & 0xf0) << 4;
21723           insn |= (old_op & 0xf) << 16;
21724           put_thumb32_insn (buf, insn);
21725           if (insn & (1 << 20))
21726             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21727           else
21728             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21729         }
21730       else
21731         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21732       pc_rel = 0;
21733       break;
21734     default:
21735       abort ();
21736     }
21737   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
21738                       (enum bfd_reloc_code_real) reloc_type);
21739   fixp->fx_file = fragp->fr_file;
21740   fixp->fx_line = fragp->fr_line;
21741   fragp->fr_fix += fragp->fr_var;
21742
21743   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
21744   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
21745       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
21746     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
21747 }
21748
21749 /* Return the size of a relaxable immediate operand instruction.
21750    SHIFT and SIZE specify the form of the allowable immediate.  */
21751 static int
21752 relax_immediate (fragS *fragp, int size, int shift)
21753 {
21754   offsetT offset;
21755   offsetT mask;
21756   offsetT low;
21757
21758   /* ??? Should be able to do better than this.  */
21759   if (fragp->fr_symbol)
21760     return 4;
21761
21762   low = (1 << shift) - 1;
21763   mask = (1 << (shift + size)) - (1 << shift);
21764   offset = fragp->fr_offset;
21765   /* Force misaligned offsets to 32-bit variant.  */
21766   if (offset & low)
21767     return 4;
21768   if (offset & ~mask)
21769     return 4;
21770   return 2;
21771 }
21772
21773 /* Get the address of a symbol during relaxation.  */
21774 static addressT
21775 relaxed_symbol_addr (fragS *fragp, long stretch)
21776 {
21777   fragS *sym_frag;
21778   addressT addr;
21779   symbolS *sym;
21780
21781   sym = fragp->fr_symbol;
21782   sym_frag = symbol_get_frag (sym);
21783   know (S_GET_SEGMENT (sym) != absolute_section
21784         || sym_frag == &zero_address_frag);
21785   addr = S_GET_VALUE (sym) + fragp->fr_offset;
21786
21787   /* If frag has yet to be reached on this pass, assume it will
21788      move by STRETCH just as we did.  If this is not so, it will
21789      be because some frag between grows, and that will force
21790      another pass.  */
21791
21792   if (stretch != 0
21793       && sym_frag->relax_marker != fragp->relax_marker)
21794     {
21795       fragS *f;
21796
21797       /* Adjust stretch for any alignment frag.  Note that if have
21798          been expanding the earlier code, the symbol may be
21799          defined in what appears to be an earlier frag.  FIXME:
21800          This doesn't handle the fr_subtype field, which specifies
21801          a maximum number of bytes to skip when doing an
21802          alignment.  */
21803       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
21804         {
21805           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
21806             {
21807               if (stretch < 0)
21808                 stretch = - ((- stretch)
21809                              & ~ ((1 << (int) f->fr_offset) - 1));
21810               else
21811                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
21812               if (stretch == 0)
21813                 break;
21814             }
21815         }
21816       if (f != NULL)
21817         addr += stretch;
21818     }
21819
21820   return addr;
21821 }
21822
21823 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
21824    load.  */
21825 static int
21826 relax_adr (fragS *fragp, asection *sec, long stretch)
21827 {
21828   addressT addr;
21829   offsetT val;
21830
21831   /* Assume worst case for symbols not known to be in the same section.  */
21832   if (fragp->fr_symbol == NULL
21833       || !S_IS_DEFINED (fragp->fr_symbol)
21834       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21835       || S_IS_WEAK (fragp->fr_symbol))
21836     return 4;
21837
21838   val = relaxed_symbol_addr (fragp, stretch);
21839   addr = fragp->fr_address + fragp->fr_fix;
21840   addr = (addr + 4) & ~3;
21841   /* Force misaligned targets to 32-bit variant.  */
21842   if (val & 3)
21843     return 4;
21844   val -= addr;
21845   if (val < 0 || val > 1020)
21846     return 4;
21847   return 2;
21848 }
21849
21850 /* Return the size of a relaxable add/sub immediate instruction.  */
21851 static int
21852 relax_addsub (fragS *fragp, asection *sec)
21853 {
21854   char *buf;
21855   int op;
21856
21857   buf = fragp->fr_literal + fragp->fr_fix;
21858   op = bfd_get_16(sec->owner, buf);
21859   if ((op & 0xf) == ((op >> 4) & 0xf))
21860     return relax_immediate (fragp, 8, 0);
21861   else
21862     return relax_immediate (fragp, 3, 0);
21863 }
21864
21865 /* Return TRUE iff the definition of symbol S could be pre-empted
21866    (overridden) at link or load time.  */
21867 static bfd_boolean
21868 symbol_preemptible (symbolS *s)
21869 {
21870   /* Weak symbols can always be pre-empted.  */
21871   if (S_IS_WEAK (s))
21872     return TRUE;
21873
21874   /* Non-global symbols cannot be pre-empted. */
21875   if (! S_IS_EXTERNAL (s))
21876     return FALSE;
21877
21878 #ifdef OBJ_ELF
21879   /* In ELF, a global symbol can be marked protected, or private.  In that
21880      case it can't be pre-empted (other definitions in the same link unit
21881      would violate the ODR).  */
21882   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
21883     return FALSE;
21884 #endif
21885
21886   /* Other global symbols might be pre-empted.  */
21887   return TRUE;
21888 }
21889
21890 /* Return the size of a relaxable branch instruction.  BITS is the
21891    size of the offset field in the narrow instruction.  */
21892
21893 static int
21894 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
21895 {
21896   addressT addr;
21897   offsetT val;
21898   offsetT limit;
21899
21900   /* Assume worst case for symbols not known to be in the same section.  */
21901   if (!S_IS_DEFINED (fragp->fr_symbol)
21902       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21903       || S_IS_WEAK (fragp->fr_symbol))
21904     return 4;
21905
21906 #ifdef OBJ_ELF
21907   /* A branch to a function in ARM state will require interworking.  */
21908   if (S_IS_DEFINED (fragp->fr_symbol)
21909       && ARM_IS_FUNC (fragp->fr_symbol))
21910       return 4;
21911 #endif
21912
21913   if (symbol_preemptible (fragp->fr_symbol))
21914     return 4;
21915
21916   val = relaxed_symbol_addr (fragp, stretch);
21917   addr = fragp->fr_address + fragp->fr_fix + 4;
21918   val -= addr;
21919
21920   /* Offset is a signed value *2 */
21921   limit = 1 << bits;
21922   if (val >= limit || val < -limit)
21923     return 4;
21924   return 2;
21925 }
21926
21927
21928 /* Relax a machine dependent frag.  This returns the amount by which
21929    the current size of the frag should change.  */
21930
21931 int
21932 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
21933 {
21934   int oldsize;
21935   int newsize;
21936
21937   oldsize = fragp->fr_var;
21938   switch (fragp->fr_subtype)
21939     {
21940     case T_MNEM_ldr_pc2:
21941       newsize = relax_adr (fragp, sec, stretch);
21942       break;
21943     case T_MNEM_ldr_pc:
21944     case T_MNEM_ldr_sp:
21945     case T_MNEM_str_sp:
21946       newsize = relax_immediate (fragp, 8, 2);
21947       break;
21948     case T_MNEM_ldr:
21949     case T_MNEM_str:
21950       newsize = relax_immediate (fragp, 5, 2);
21951       break;
21952     case T_MNEM_ldrh:
21953     case T_MNEM_strh:
21954       newsize = relax_immediate (fragp, 5, 1);
21955       break;
21956     case T_MNEM_ldrb:
21957     case T_MNEM_strb:
21958       newsize = relax_immediate (fragp, 5, 0);
21959       break;
21960     case T_MNEM_adr:
21961       newsize = relax_adr (fragp, sec, stretch);
21962       break;
21963     case T_MNEM_mov:
21964     case T_MNEM_movs:
21965     case T_MNEM_cmp:
21966     case T_MNEM_cmn:
21967       newsize = relax_immediate (fragp, 8, 0);
21968       break;
21969     case T_MNEM_b:
21970       newsize = relax_branch (fragp, sec, 11, stretch);
21971       break;
21972     case T_MNEM_bcond:
21973       newsize = relax_branch (fragp, sec, 8, stretch);
21974       break;
21975     case T_MNEM_add_sp:
21976     case T_MNEM_add_pc:
21977       newsize = relax_immediate (fragp, 8, 2);
21978       break;
21979     case T_MNEM_inc_sp:
21980     case T_MNEM_dec_sp:
21981       newsize = relax_immediate (fragp, 7, 2);
21982       break;
21983     case T_MNEM_addi:
21984     case T_MNEM_addis:
21985     case T_MNEM_subi:
21986     case T_MNEM_subis:
21987       newsize = relax_addsub (fragp, sec);
21988       break;
21989     default:
21990       abort ();
21991     }
21992
21993   fragp->fr_var = newsize;
21994   /* Freeze wide instructions that are at or before the same location as
21995      in the previous pass.  This avoids infinite loops.
21996      Don't freeze them unconditionally because targets may be artificially
21997      misaligned by the expansion of preceding frags.  */
21998   if (stretch <= 0 && newsize > 2)
21999     {
22000       md_convert_frag (sec->owner, sec, fragp);
22001       frag_wane (fragp);
22002     }
22003
22004   return newsize - oldsize;
22005 }
22006
22007 /* Round up a section size to the appropriate boundary.  */
22008
22009 valueT
22010 md_section_align (segT   segment ATTRIBUTE_UNUSED,
22011                   valueT size)
22012 {
22013 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
22014   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
22015     {
22016       /* For a.out, force the section size to be aligned.  If we don't do
22017          this, BFD will align it for us, but it will not write out the
22018          final bytes of the section.  This may be a bug in BFD, but it is
22019          easier to fix it here since that is how the other a.out targets
22020          work.  */
22021       int align;
22022
22023       align = bfd_get_section_alignment (stdoutput, segment);
22024       size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
22025     }
22026 #endif
22027
22028   return size;
22029 }
22030
22031 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
22032    of an rs_align_code fragment.  */
22033
22034 void
22035 arm_handle_align (fragS * fragP)
22036 {
22037   static unsigned char const arm_noop[2][2][4] =
22038     {
22039       {  /* ARMv1 */
22040         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
22041         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
22042       },
22043       {  /* ARMv6k */
22044         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
22045         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
22046       },
22047     };
22048   static unsigned char const thumb_noop[2][2][2] =
22049     {
22050       {  /* Thumb-1 */
22051         {0xc0, 0x46},  /* LE */
22052         {0x46, 0xc0},  /* BE */
22053       },
22054       {  /* Thumb-2 */
22055         {0x00, 0xbf},  /* LE */
22056         {0xbf, 0x00}   /* BE */
22057       }
22058     };
22059   static unsigned char const wide_thumb_noop[2][4] =
22060     {  /* Wide Thumb-2 */
22061       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
22062       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
22063     };
22064
22065   unsigned bytes, fix, noop_size;
22066   char * p;
22067   const unsigned char * noop;
22068   const unsigned char *narrow_noop = NULL;
22069 #ifdef OBJ_ELF
22070   enum mstate state;
22071 #endif
22072
22073   if (fragP->fr_type != rs_align_code)
22074     return;
22075
22076   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
22077   p = fragP->fr_literal + fragP->fr_fix;
22078   fix = 0;
22079
22080   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
22081     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
22082
22083   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
22084
22085   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
22086     {
22087       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
22088                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
22089         {
22090           narrow_noop = thumb_noop[1][target_big_endian];
22091           noop = wide_thumb_noop[target_big_endian];
22092         }
22093       else
22094         noop = thumb_noop[0][target_big_endian];
22095       noop_size = 2;
22096 #ifdef OBJ_ELF
22097       state = MAP_THUMB;
22098 #endif
22099     }
22100   else
22101     {
22102       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
22103                                            ? selected_cpu : arm_arch_none,
22104                                            arm_ext_v6k) != 0]
22105                      [target_big_endian];
22106       noop_size = 4;
22107 #ifdef OBJ_ELF
22108       state = MAP_ARM;
22109 #endif
22110     }
22111
22112   fragP->fr_var = noop_size;
22113
22114   if (bytes & (noop_size - 1))
22115     {
22116       fix = bytes & (noop_size - 1);
22117 #ifdef OBJ_ELF
22118       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
22119 #endif
22120       memset (p, 0, fix);
22121       p += fix;
22122       bytes -= fix;
22123     }
22124
22125   if (narrow_noop)
22126     {
22127       if (bytes & noop_size)
22128         {
22129           /* Insert a narrow noop.  */
22130           memcpy (p, narrow_noop, noop_size);
22131           p += noop_size;
22132           bytes -= noop_size;
22133           fix += noop_size;
22134         }
22135
22136       /* Use wide noops for the remainder */
22137       noop_size = 4;
22138     }
22139
22140   while (bytes >= noop_size)
22141     {
22142       memcpy (p, noop, noop_size);
22143       p += noop_size;
22144       bytes -= noop_size;
22145       fix += noop_size;
22146     }
22147
22148   fragP->fr_fix += fix;
22149 }
22150
22151 /* Called from md_do_align.  Used to create an alignment
22152    frag in a code section.  */
22153
22154 void
22155 arm_frag_align_code (int n, int max)
22156 {
22157   char * p;
22158
22159   /* We assume that there will never be a requirement
22160      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
22161   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
22162     {
22163       char err_msg[128];
22164
22165       sprintf (err_msg,
22166         _("alignments greater than %d bytes not supported in .text sections."),
22167         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
22168       as_fatal ("%s", err_msg);
22169     }
22170
22171   p = frag_var (rs_align_code,
22172                 MAX_MEM_FOR_RS_ALIGN_CODE,
22173                 1,
22174                 (relax_substateT) max,
22175                 (symbolS *) NULL,
22176                 (offsetT) n,
22177                 (char *) NULL);
22178   *p = 0;
22179 }
22180
22181 /* Perform target specific initialisation of a frag.
22182    Note - despite the name this initialisation is not done when the frag
22183    is created, but only when its type is assigned.  A frag can be created
22184    and used a long time before its type is set, so beware of assuming that
22185    this initialisation is performed first.  */
22186
22187 #ifndef OBJ_ELF
22188 void
22189 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
22190 {
22191   /* Record whether this frag is in an ARM or a THUMB area.  */
22192   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
22193 }
22194
22195 #else /* OBJ_ELF is defined.  */
22196 void
22197 arm_init_frag (fragS * fragP, int max_chars)
22198 {
22199   bfd_boolean frag_thumb_mode;
22200
22201   /* If the current ARM vs THUMB mode has not already
22202      been recorded into this frag then do so now.  */
22203   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
22204     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
22205
22206   /* PR 21809: Do not set a mapping state for debug sections
22207      - it just confuses other tools.  */
22208   if (bfd_get_section_flags (NULL, now_seg) & SEC_DEBUGGING)
22209     return;
22210
22211   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
22212
22213   /* Record a mapping symbol for alignment frags.  We will delete this
22214      later if the alignment ends up empty.  */
22215   switch (fragP->fr_type)
22216     {
22217     case rs_align:
22218     case rs_align_test:
22219     case rs_fill:
22220       mapping_state_2 (MAP_DATA, max_chars);
22221       break;
22222     case rs_align_code:
22223       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
22224       break;
22225     default:
22226       break;
22227     }
22228 }
22229
22230 /* When we change sections we need to issue a new mapping symbol.  */
22231
22232 void
22233 arm_elf_change_section (void)
22234 {
22235   /* Link an unlinked unwind index table section to the .text section.  */
22236   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
22237       && elf_linked_to_section (now_seg) == NULL)
22238     elf_linked_to_section (now_seg) = text_section;
22239 }
22240
22241 int
22242 arm_elf_section_type (const char * str, size_t len)
22243 {
22244   if (len == 5 && strncmp (str, "exidx", 5) == 0)
22245     return SHT_ARM_EXIDX;
22246
22247   return -1;
22248 }
22249 \f
22250 /* Code to deal with unwinding tables.  */
22251
22252 static void add_unwind_adjustsp (offsetT);
22253
22254 /* Generate any deferred unwind frame offset.  */
22255
22256 static void
22257 flush_pending_unwind (void)
22258 {
22259   offsetT offset;
22260
22261   offset = unwind.pending_offset;
22262   unwind.pending_offset = 0;
22263   if (offset != 0)
22264     add_unwind_adjustsp (offset);
22265 }
22266
22267 /* Add an opcode to this list for this function.  Two-byte opcodes should
22268    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
22269    order.  */
22270
22271 static void
22272 add_unwind_opcode (valueT op, int length)
22273 {
22274   /* Add any deferred stack adjustment.  */
22275   if (unwind.pending_offset)
22276     flush_pending_unwind ();
22277
22278   unwind.sp_restored = 0;
22279
22280   if (unwind.opcode_count + length > unwind.opcode_alloc)
22281     {
22282       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
22283       if (unwind.opcodes)
22284         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
22285                                      unwind.opcode_alloc);
22286       else
22287         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
22288     }
22289   while (length > 0)
22290     {
22291       length--;
22292       unwind.opcodes[unwind.opcode_count] = op & 0xff;
22293       op >>= 8;
22294       unwind.opcode_count++;
22295     }
22296 }
22297
22298 /* Add unwind opcodes to adjust the stack pointer.  */
22299
22300 static void
22301 add_unwind_adjustsp (offsetT offset)
22302 {
22303   valueT op;
22304
22305   if (offset > 0x200)
22306     {
22307       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
22308       char bytes[5];
22309       int n;
22310       valueT o;
22311
22312       /* Long form: 0xb2, uleb128.  */
22313       /* This might not fit in a word so add the individual bytes,
22314          remembering the list is built in reverse order.  */
22315       o = (valueT) ((offset - 0x204) >> 2);
22316       if (o == 0)
22317         add_unwind_opcode (0, 1);
22318
22319       /* Calculate the uleb128 encoding of the offset.  */
22320       n = 0;
22321       while (o)
22322         {
22323           bytes[n] = o & 0x7f;
22324           o >>= 7;
22325           if (o)
22326             bytes[n] |= 0x80;
22327           n++;
22328         }
22329       /* Add the insn.  */
22330       for (; n; n--)
22331         add_unwind_opcode (bytes[n - 1], 1);
22332       add_unwind_opcode (0xb2, 1);
22333     }
22334   else if (offset > 0x100)
22335     {
22336       /* Two short opcodes.  */
22337       add_unwind_opcode (0x3f, 1);
22338       op = (offset - 0x104) >> 2;
22339       add_unwind_opcode (op, 1);
22340     }
22341   else if (offset > 0)
22342     {
22343       /* Short opcode.  */
22344       op = (offset - 4) >> 2;
22345       add_unwind_opcode (op, 1);
22346     }
22347   else if (offset < 0)
22348     {
22349       offset = -offset;
22350       while (offset > 0x100)
22351         {
22352           add_unwind_opcode (0x7f, 1);
22353           offset -= 0x100;
22354         }
22355       op = ((offset - 4) >> 2) | 0x40;
22356       add_unwind_opcode (op, 1);
22357     }
22358 }
22359
22360 /* Finish the list of unwind opcodes for this function.  */
22361
22362 static void
22363 finish_unwind_opcodes (void)
22364 {
22365   valueT op;
22366
22367   if (unwind.fp_used)
22368     {
22369       /* Adjust sp as necessary.  */
22370       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
22371       flush_pending_unwind ();
22372
22373       /* After restoring sp from the frame pointer.  */
22374       op = 0x90 | unwind.fp_reg;
22375       add_unwind_opcode (op, 1);
22376     }
22377   else
22378     flush_pending_unwind ();
22379 }
22380
22381
22382 /* Start an exception table entry.  If idx is nonzero this is an index table
22383    entry.  */
22384
22385 static void
22386 start_unwind_section (const segT text_seg, int idx)
22387 {
22388   const char * text_name;
22389   const char * prefix;
22390   const char * prefix_once;
22391   const char * group_name;
22392   char * sec_name;
22393   int type;
22394   int flags;
22395   int linkonce;
22396
22397   if (idx)
22398     {
22399       prefix = ELF_STRING_ARM_unwind;
22400       prefix_once = ELF_STRING_ARM_unwind_once;
22401       type = SHT_ARM_EXIDX;
22402     }
22403   else
22404     {
22405       prefix = ELF_STRING_ARM_unwind_info;
22406       prefix_once = ELF_STRING_ARM_unwind_info_once;
22407       type = SHT_PROGBITS;
22408     }
22409
22410   text_name = segment_name (text_seg);
22411   if (streq (text_name, ".text"))
22412     text_name = "";
22413
22414   if (strncmp (text_name, ".gnu.linkonce.t.",
22415                strlen (".gnu.linkonce.t.")) == 0)
22416     {
22417       prefix = prefix_once;
22418       text_name += strlen (".gnu.linkonce.t.");
22419     }
22420
22421   sec_name = concat (prefix, text_name, (char *) NULL);
22422
22423   flags = SHF_ALLOC;
22424   linkonce = 0;
22425   group_name = 0;
22426
22427   /* Handle COMDAT group.  */
22428   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
22429     {
22430       group_name = elf_group_name (text_seg);
22431       if (group_name == NULL)
22432         {
22433           as_bad (_("Group section `%s' has no group signature"),
22434                   segment_name (text_seg));
22435           ignore_rest_of_line ();
22436           return;
22437         }
22438       flags |= SHF_GROUP;
22439       linkonce = 1;
22440     }
22441
22442   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
22443                           linkonce, 0);
22444
22445   /* Set the section link for index tables.  */
22446   if (idx)
22447     elf_linked_to_section (now_seg) = text_seg;
22448 }
22449
22450
22451 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
22452    personality routine data.  Returns zero, or the index table value for
22453    an inline entry.  */
22454
22455 static valueT
22456 create_unwind_entry (int have_data)
22457 {
22458   int size;
22459   addressT where;
22460   char *ptr;
22461   /* The current word of data.  */
22462   valueT data;
22463   /* The number of bytes left in this word.  */
22464   int n;
22465
22466   finish_unwind_opcodes ();
22467
22468   /* Remember the current text section.  */
22469   unwind.saved_seg = now_seg;
22470   unwind.saved_subseg = now_subseg;
22471
22472   start_unwind_section (now_seg, 0);
22473
22474   if (unwind.personality_routine == NULL)
22475     {
22476       if (unwind.personality_index == -2)
22477         {
22478           if (have_data)
22479             as_bad (_("handlerdata in cantunwind frame"));
22480           return 1; /* EXIDX_CANTUNWIND.  */
22481         }
22482
22483       /* Use a default personality routine if none is specified.  */
22484       if (unwind.personality_index == -1)
22485         {
22486           if (unwind.opcode_count > 3)
22487             unwind.personality_index = 1;
22488           else
22489             unwind.personality_index = 0;
22490         }
22491
22492       /* Space for the personality routine entry.  */
22493       if (unwind.personality_index == 0)
22494         {
22495           if (unwind.opcode_count > 3)
22496             as_bad (_("too many unwind opcodes for personality routine 0"));
22497
22498           if (!have_data)
22499             {
22500               /* All the data is inline in the index table.  */
22501               data = 0x80;
22502               n = 3;
22503               while (unwind.opcode_count > 0)
22504                 {
22505                   unwind.opcode_count--;
22506                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22507                   n--;
22508                 }
22509
22510               /* Pad with "finish" opcodes.  */
22511               while (n--)
22512                 data = (data << 8) | 0xb0;
22513
22514               return data;
22515             }
22516           size = 0;
22517         }
22518       else
22519         /* We get two opcodes "free" in the first word.  */
22520         size = unwind.opcode_count - 2;
22521     }
22522   else
22523     {
22524       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
22525       if (unwind.personality_index != -1)
22526         {
22527           as_bad (_("attempt to recreate an unwind entry"));
22528           return 1;
22529         }
22530
22531       /* An extra byte is required for the opcode count.        */
22532       size = unwind.opcode_count + 1;
22533     }
22534
22535   size = (size + 3) >> 2;
22536   if (size > 0xff)
22537     as_bad (_("too many unwind opcodes"));
22538
22539   frag_align (2, 0, 0);
22540   record_alignment (now_seg, 2);
22541   unwind.table_entry = expr_build_dot ();
22542
22543   /* Allocate the table entry.  */
22544   ptr = frag_more ((size << 2) + 4);
22545   /* PR 13449: Zero the table entries in case some of them are not used.  */
22546   memset (ptr, 0, (size << 2) + 4);
22547   where = frag_now_fix () - ((size << 2) + 4);
22548
22549   switch (unwind.personality_index)
22550     {
22551     case -1:
22552       /* ??? Should this be a PLT generating relocation?  */
22553       /* Custom personality routine.  */
22554       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
22555                BFD_RELOC_ARM_PREL31);
22556
22557       where += 4;
22558       ptr += 4;
22559
22560       /* Set the first byte to the number of additional words.  */
22561       data = size > 0 ? size - 1 : 0;
22562       n = 3;
22563       break;
22564
22565     /* ABI defined personality routines.  */
22566     case 0:
22567       /* Three opcodes bytes are packed into the first word.  */
22568       data = 0x80;
22569       n = 3;
22570       break;
22571
22572     case 1:
22573     case 2:
22574       /* The size and first two opcode bytes go in the first word.  */
22575       data = ((0x80 + unwind.personality_index) << 8) | size;
22576       n = 2;
22577       break;
22578
22579     default:
22580       /* Should never happen.  */
22581       abort ();
22582     }
22583
22584   /* Pack the opcodes into words (MSB first), reversing the list at the same
22585      time.  */
22586   while (unwind.opcode_count > 0)
22587     {
22588       if (n == 0)
22589         {
22590           md_number_to_chars (ptr, data, 4);
22591           ptr += 4;
22592           n = 4;
22593           data = 0;
22594         }
22595       unwind.opcode_count--;
22596       n--;
22597       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22598     }
22599
22600   /* Finish off the last word.  */
22601   if (n < 4)
22602     {
22603       /* Pad with "finish" opcodes.  */
22604       while (n--)
22605         data = (data << 8) | 0xb0;
22606
22607       md_number_to_chars (ptr, data, 4);
22608     }
22609
22610   if (!have_data)
22611     {
22612       /* Add an empty descriptor if there is no user-specified data.   */
22613       ptr = frag_more (4);
22614       md_number_to_chars (ptr, 0, 4);
22615     }
22616
22617   return 0;
22618 }
22619
22620
22621 /* Initialize the DWARF-2 unwind information for this procedure.  */
22622
22623 void
22624 tc_arm_frame_initial_instructions (void)
22625 {
22626   cfi_add_CFA_def_cfa (REG_SP, 0);
22627 }
22628 #endif /* OBJ_ELF */
22629
22630 /* Convert REGNAME to a DWARF-2 register number.  */
22631
22632 int
22633 tc_arm_regname_to_dw2regnum (char *regname)
22634 {
22635   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
22636   if (reg != FAIL)
22637     return reg;
22638
22639   /* PR 16694: Allow VFP registers as well.  */
22640   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
22641   if (reg != FAIL)
22642     return 64 + reg;
22643
22644   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
22645   if (reg != FAIL)
22646     return reg + 256;
22647
22648   return FAIL;
22649 }
22650
22651 #ifdef TE_PE
22652 void
22653 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
22654 {
22655   expressionS exp;
22656
22657   exp.X_op = O_secrel;
22658   exp.X_add_symbol = symbol;
22659   exp.X_add_number = 0;
22660   emit_expr (&exp, size);
22661 }
22662 #endif
22663
22664 /* MD interface: Symbol and relocation handling.  */
22665
22666 /* Return the address within the segment that a PC-relative fixup is
22667    relative to.  For ARM, PC-relative fixups applied to instructions
22668    are generally relative to the location of the fixup plus 8 bytes.
22669    Thumb branches are offset by 4, and Thumb loads relative to PC
22670    require special handling.  */
22671
22672 long
22673 md_pcrel_from_section (fixS * fixP, segT seg)
22674 {
22675   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
22676
22677   /* If this is pc-relative and we are going to emit a relocation
22678      then we just want to put out any pipeline compensation that the linker
22679      will need.  Otherwise we want to use the calculated base.
22680      For WinCE we skip the bias for externals as well, since this
22681      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
22682   if (fixP->fx_pcrel
22683       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
22684           || (arm_force_relocation (fixP)
22685 #ifdef TE_WINCE
22686               && !S_IS_EXTERNAL (fixP->fx_addsy)
22687 #endif
22688               )))
22689     base = 0;
22690
22691
22692   switch (fixP->fx_r_type)
22693     {
22694       /* PC relative addressing on the Thumb is slightly odd as the
22695          bottom two bits of the PC are forced to zero for the
22696          calculation.  This happens *after* application of the
22697          pipeline offset.  However, Thumb adrl already adjusts for
22698          this, so we need not do it again.  */
22699     case BFD_RELOC_ARM_THUMB_ADD:
22700       return base & ~3;
22701
22702     case BFD_RELOC_ARM_THUMB_OFFSET:
22703     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22704     case BFD_RELOC_ARM_T32_ADD_PC12:
22705     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22706       return (base + 4) & ~3;
22707
22708       /* Thumb branches are simply offset by +4.  */
22709     case BFD_RELOC_THUMB_PCREL_BRANCH7:
22710     case BFD_RELOC_THUMB_PCREL_BRANCH9:
22711     case BFD_RELOC_THUMB_PCREL_BRANCH12:
22712     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22713     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22714       return base + 4;
22715
22716     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22717       if (fixP->fx_addsy
22718           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22719           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22720           && ARM_IS_FUNC (fixP->fx_addsy)
22721           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22722         base = fixP->fx_where + fixP->fx_frag->fr_address;
22723        return base + 4;
22724
22725       /* BLX is like branches above, but forces the low two bits of PC to
22726          zero.  */
22727     case BFD_RELOC_THUMB_PCREL_BLX:
22728       if (fixP->fx_addsy
22729           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22730           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22731           && THUMB_IS_FUNC (fixP->fx_addsy)
22732           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22733         base = fixP->fx_where + fixP->fx_frag->fr_address;
22734       return (base + 4) & ~3;
22735
22736       /* ARM mode branches are offset by +8.  However, the Windows CE
22737          loader expects the relocation not to take this into account.  */
22738     case BFD_RELOC_ARM_PCREL_BLX:
22739       if (fixP->fx_addsy
22740           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22741           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22742           && ARM_IS_FUNC (fixP->fx_addsy)
22743           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22744         base = fixP->fx_where + fixP->fx_frag->fr_address;
22745       return base + 8;
22746
22747     case BFD_RELOC_ARM_PCREL_CALL:
22748       if (fixP->fx_addsy
22749           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22750           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22751           && THUMB_IS_FUNC (fixP->fx_addsy)
22752           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22753         base = fixP->fx_where + fixP->fx_frag->fr_address;
22754       return base + 8;
22755
22756     case BFD_RELOC_ARM_PCREL_BRANCH:
22757     case BFD_RELOC_ARM_PCREL_JUMP:
22758     case BFD_RELOC_ARM_PLT32:
22759 #ifdef TE_WINCE
22760       /* When handling fixups immediately, because we have already
22761          discovered the value of a symbol, or the address of the frag involved
22762          we must account for the offset by +8, as the OS loader will never see the reloc.
22763          see fixup_segment() in write.c
22764          The S_IS_EXTERNAL test handles the case of global symbols.
22765          Those need the calculated base, not just the pipe compensation the linker will need.  */
22766       if (fixP->fx_pcrel
22767           && fixP->fx_addsy != NULL
22768           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22769           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
22770         return base + 8;
22771       return base;
22772 #else
22773       return base + 8;
22774 #endif
22775
22776
22777       /* ARM mode loads relative to PC are also offset by +8.  Unlike
22778          branches, the Windows CE loader *does* expect the relocation
22779          to take this into account.  */
22780     case BFD_RELOC_ARM_OFFSET_IMM:
22781     case BFD_RELOC_ARM_OFFSET_IMM8:
22782     case BFD_RELOC_ARM_HWLITERAL:
22783     case BFD_RELOC_ARM_LITERAL:
22784     case BFD_RELOC_ARM_CP_OFF_IMM:
22785       return base + 8;
22786
22787
22788       /* Other PC-relative relocations are un-offset.  */
22789     default:
22790       return base;
22791     }
22792 }
22793
22794 static bfd_boolean flag_warn_syms = TRUE;
22795
22796 bfd_boolean
22797 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
22798 {
22799   /* PR 18347 - Warn if the user attempts to create a symbol with the same
22800      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
22801      does mean that the resulting code might be very confusing to the reader.
22802      Also this warning can be triggered if the user omits an operand before
22803      an immediate address, eg:
22804
22805        LDR =foo
22806
22807      GAS treats this as an assignment of the value of the symbol foo to a
22808      symbol LDR, and so (without this code) it will not issue any kind of
22809      warning or error message.
22810
22811      Note - ARM instructions are case-insensitive but the strings in the hash
22812      table are all stored in lower case, so we must first ensure that name is
22813      lower case too.  */
22814   if (flag_warn_syms && arm_ops_hsh)
22815     {
22816       char * nbuf = strdup (name);
22817       char * p;
22818
22819       for (p = nbuf; *p; p++)
22820         *p = TOLOWER (*p);
22821       if (hash_find (arm_ops_hsh, nbuf) != NULL)
22822         {
22823           static struct hash_control * already_warned = NULL;
22824
22825           if (already_warned == NULL)
22826             already_warned = hash_new ();
22827           /* Only warn about the symbol once.  To keep the code
22828              simple we let hash_insert do the lookup for us.  */
22829           if (hash_insert (already_warned, name, NULL) == NULL)
22830             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
22831         }
22832       else
22833         free (nbuf);
22834     }
22835
22836   return FALSE;
22837 }
22838
22839 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
22840    Otherwise we have no need to default values of symbols.  */
22841
22842 symbolS *
22843 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
22844 {
22845 #ifdef OBJ_ELF
22846   if (name[0] == '_' && name[1] == 'G'
22847       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
22848     {
22849       if (!GOT_symbol)
22850         {
22851           if (symbol_find (name))
22852             as_bad (_("GOT already in the symbol table"));
22853
22854           GOT_symbol = symbol_new (name, undefined_section,
22855                                    (valueT) 0, & zero_address_frag);
22856         }
22857
22858       return GOT_symbol;
22859     }
22860 #endif
22861
22862   return NULL;
22863 }
22864
22865 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
22866    computed as two separate immediate values, added together.  We
22867    already know that this value cannot be computed by just one ARM
22868    instruction.  */
22869
22870 static unsigned int
22871 validate_immediate_twopart (unsigned int   val,
22872                             unsigned int * highpart)
22873 {
22874   unsigned int a;
22875   unsigned int i;
22876
22877   for (i = 0; i < 32; i += 2)
22878     if (((a = rotate_left (val, i)) & 0xff) != 0)
22879       {
22880         if (a & 0xff00)
22881           {
22882             if (a & ~ 0xffff)
22883               continue;
22884             * highpart = (a  >> 8) | ((i + 24) << 7);
22885           }
22886         else if (a & 0xff0000)
22887           {
22888             if (a & 0xff000000)
22889               continue;
22890             * highpart = (a >> 16) | ((i + 16) << 7);
22891           }
22892         else
22893           {
22894             gas_assert (a & 0xff000000);
22895             * highpart = (a >> 24) | ((i + 8) << 7);
22896           }
22897
22898         return (a & 0xff) | (i << 7);
22899       }
22900
22901   return FAIL;
22902 }
22903
22904 static int
22905 validate_offset_imm (unsigned int val, int hwse)
22906 {
22907   if ((hwse && val > 255) || val > 4095)
22908     return FAIL;
22909   return val;
22910 }
22911
22912 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
22913    negative immediate constant by altering the instruction.  A bit of
22914    a hack really.
22915         MOV <-> MVN
22916         AND <-> BIC
22917         ADC <-> SBC
22918         by inverting the second operand, and
22919         ADD <-> SUB
22920         CMP <-> CMN
22921         by negating the second operand.  */
22922
22923 static int
22924 negate_data_op (unsigned long * instruction,
22925                 unsigned long   value)
22926 {
22927   int op, new_inst;
22928   unsigned long negated, inverted;
22929
22930   negated = encode_arm_immediate (-value);
22931   inverted = encode_arm_immediate (~value);
22932
22933   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
22934   switch (op)
22935     {
22936       /* First negates.  */
22937     case OPCODE_SUB:             /* ADD <-> SUB  */
22938       new_inst = OPCODE_ADD;
22939       value = negated;
22940       break;
22941
22942     case OPCODE_ADD:
22943       new_inst = OPCODE_SUB;
22944       value = negated;
22945       break;
22946
22947     case OPCODE_CMP:             /* CMP <-> CMN  */
22948       new_inst = OPCODE_CMN;
22949       value = negated;
22950       break;
22951
22952     case OPCODE_CMN:
22953       new_inst = OPCODE_CMP;
22954       value = negated;
22955       break;
22956
22957       /* Now Inverted ops.  */
22958     case OPCODE_MOV:             /* MOV <-> MVN  */
22959       new_inst = OPCODE_MVN;
22960       value = inverted;
22961       break;
22962
22963     case OPCODE_MVN:
22964       new_inst = OPCODE_MOV;
22965       value = inverted;
22966       break;
22967
22968     case OPCODE_AND:             /* AND <-> BIC  */
22969       new_inst = OPCODE_BIC;
22970       value = inverted;
22971       break;
22972
22973     case OPCODE_BIC:
22974       new_inst = OPCODE_AND;
22975       value = inverted;
22976       break;
22977
22978     case OPCODE_ADC:              /* ADC <-> SBC  */
22979       new_inst = OPCODE_SBC;
22980       value = inverted;
22981       break;
22982
22983     case OPCODE_SBC:
22984       new_inst = OPCODE_ADC;
22985       value = inverted;
22986       break;
22987
22988       /* We cannot do anything.  */
22989     default:
22990       return FAIL;
22991     }
22992
22993   if (value == (unsigned) FAIL)
22994     return FAIL;
22995
22996   *instruction &= OPCODE_MASK;
22997   *instruction |= new_inst << DATA_OP_SHIFT;
22998   return value;
22999 }
23000
23001 /* Like negate_data_op, but for Thumb-2.   */
23002
23003 static unsigned int
23004 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
23005 {
23006   int op, new_inst;
23007   int rd;
23008   unsigned int negated, inverted;
23009
23010   negated = encode_thumb32_immediate (-value);
23011   inverted = encode_thumb32_immediate (~value);
23012
23013   rd = (*instruction >> 8) & 0xf;
23014   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
23015   switch (op)
23016     {
23017       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
23018     case T2_OPCODE_SUB:
23019       new_inst = T2_OPCODE_ADD;
23020       value = negated;
23021       break;
23022
23023     case T2_OPCODE_ADD:
23024       new_inst = T2_OPCODE_SUB;
23025       value = negated;
23026       break;
23027
23028       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
23029     case T2_OPCODE_ORR:
23030       new_inst = T2_OPCODE_ORN;
23031       value = inverted;
23032       break;
23033
23034     case T2_OPCODE_ORN:
23035       new_inst = T2_OPCODE_ORR;
23036       value = inverted;
23037       break;
23038
23039       /* AND <-> BIC.  TST has no inverted equivalent.  */
23040     case T2_OPCODE_AND:
23041       new_inst = T2_OPCODE_BIC;
23042       if (rd == 15)
23043         value = FAIL;
23044       else
23045         value = inverted;
23046       break;
23047
23048     case T2_OPCODE_BIC:
23049       new_inst = T2_OPCODE_AND;
23050       value = inverted;
23051       break;
23052
23053       /* ADC <-> SBC  */
23054     case T2_OPCODE_ADC:
23055       new_inst = T2_OPCODE_SBC;
23056       value = inverted;
23057       break;
23058
23059     case T2_OPCODE_SBC:
23060       new_inst = T2_OPCODE_ADC;
23061       value = inverted;
23062       break;
23063
23064       /* We cannot do anything.  */
23065     default:
23066       return FAIL;
23067     }
23068
23069   if (value == (unsigned int)FAIL)
23070     return FAIL;
23071
23072   *instruction &= T2_OPCODE_MASK;
23073   *instruction |= new_inst << T2_DATA_OP_SHIFT;
23074   return value;
23075 }
23076
23077 /* Read a 32-bit thumb instruction from buf.  */
23078
23079 static unsigned long
23080 get_thumb32_insn (char * buf)
23081 {
23082   unsigned long insn;
23083   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
23084   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23085
23086   return insn;
23087 }
23088
23089 /* We usually want to set the low bit on the address of thumb function
23090    symbols.  In particular .word foo - . should have the low bit set.
23091    Generic code tries to fold the difference of two symbols to
23092    a constant.  Prevent this and force a relocation when the first symbols
23093    is a thumb function.  */
23094
23095 bfd_boolean
23096 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
23097 {
23098   if (op == O_subtract
23099       && l->X_op == O_symbol
23100       && r->X_op == O_symbol
23101       && THUMB_IS_FUNC (l->X_add_symbol))
23102     {
23103       l->X_op = O_subtract;
23104       l->X_op_symbol = r->X_add_symbol;
23105       l->X_add_number -= r->X_add_number;
23106       return TRUE;
23107     }
23108
23109   /* Process as normal.  */
23110   return FALSE;
23111 }
23112
23113 /* Encode Thumb2 unconditional branches and calls. The encoding
23114    for the 2 are identical for the immediate values.  */
23115
23116 static void
23117 encode_thumb2_b_bl_offset (char * buf, offsetT value)
23118 {
23119 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
23120   offsetT newval;
23121   offsetT newval2;
23122   addressT S, I1, I2, lo, hi;
23123
23124   S = (value >> 24) & 0x01;
23125   I1 = (value >> 23) & 0x01;
23126   I2 = (value >> 22) & 0x01;
23127   hi = (value >> 12) & 0x3ff;
23128   lo = (value >> 1) & 0x7ff;
23129   newval   = md_chars_to_number (buf, THUMB_SIZE);
23130   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23131   newval  |= (S << 10) | hi;
23132   newval2 &=  ~T2I1I2MASK;
23133   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
23134   md_number_to_chars (buf, newval, THUMB_SIZE);
23135   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23136 }
23137
23138 void
23139 md_apply_fix (fixS *    fixP,
23140                valueT * valP,
23141                segT     seg)
23142 {
23143   offsetT        value = * valP;
23144   offsetT        newval;
23145   unsigned int   newimm;
23146   unsigned long  temp;
23147   int            sign;
23148   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
23149
23150   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
23151
23152   /* Note whether this will delete the relocation.  */
23153
23154   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
23155     fixP->fx_done = 1;
23156
23157   /* On a 64-bit host, silently truncate 'value' to 32 bits for
23158      consistency with the behaviour on 32-bit hosts.  Remember value
23159      for emit_reloc.  */
23160   value &= 0xffffffff;
23161   value ^= 0x80000000;
23162   value -= 0x80000000;
23163
23164   *valP = value;
23165   fixP->fx_addnumber = value;
23166
23167   /* Same treatment for fixP->fx_offset.  */
23168   fixP->fx_offset &= 0xffffffff;
23169   fixP->fx_offset ^= 0x80000000;
23170   fixP->fx_offset -= 0x80000000;
23171
23172   switch (fixP->fx_r_type)
23173     {
23174     case BFD_RELOC_NONE:
23175       /* This will need to go in the object file.  */
23176       fixP->fx_done = 0;
23177       break;
23178
23179     case BFD_RELOC_ARM_IMMEDIATE:
23180       /* We claim that this fixup has been processed here,
23181          even if in fact we generate an error because we do
23182          not have a reloc for it, so tc_gen_reloc will reject it.  */
23183       fixP->fx_done = 1;
23184
23185       if (fixP->fx_addsy)
23186         {
23187           const char *msg = 0;
23188
23189           if (! S_IS_DEFINED (fixP->fx_addsy))
23190             msg = _("undefined symbol %s used as an immediate value");
23191           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
23192             msg = _("symbol %s is in a different section");
23193           else if (S_IS_WEAK (fixP->fx_addsy))
23194             msg = _("symbol %s is weak and may be overridden later");
23195
23196           if (msg)
23197             {
23198               as_bad_where (fixP->fx_file, fixP->fx_line,
23199                             msg, S_GET_NAME (fixP->fx_addsy));
23200               break;
23201             }
23202         }
23203
23204       temp = md_chars_to_number (buf, INSN_SIZE);
23205
23206       /* If the offset is negative, we should use encoding A2 for ADR.  */
23207       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
23208         newimm = negate_data_op (&temp, value);
23209       else
23210         {
23211           newimm = encode_arm_immediate (value);
23212
23213           /* If the instruction will fail, see if we can fix things up by
23214              changing the opcode.  */
23215           if (newimm == (unsigned int) FAIL)
23216             newimm = negate_data_op (&temp, value);
23217           /* MOV accepts both ARM modified immediate (A1 encoding) and
23218              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
23219              When disassembling, MOV is preferred when there is no encoding
23220              overlap.  */
23221           if (newimm == (unsigned int) FAIL
23222               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
23223               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
23224               && !((temp >> SBIT_SHIFT) & 0x1)
23225               && value >= 0 && value <= 0xffff)
23226             {
23227               /* Clear bits[23:20] to change encoding from A1 to A2.  */
23228               temp &= 0xff0fffff;
23229               /* Encoding high 4bits imm.  Code below will encode the remaining
23230                  low 12bits.  */
23231               temp |= (value & 0x0000f000) << 4;
23232               newimm = value & 0x00000fff;
23233             }
23234         }
23235
23236       if (newimm == (unsigned int) FAIL)
23237         {
23238           as_bad_where (fixP->fx_file, fixP->fx_line,
23239                         _("invalid constant (%lx) after fixup"),
23240                         (unsigned long) value);
23241           break;
23242         }
23243
23244       newimm |= (temp & 0xfffff000);
23245       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
23246       break;
23247
23248     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
23249       {
23250         unsigned int highpart = 0;
23251         unsigned int newinsn  = 0xe1a00000; /* nop.  */
23252
23253         if (fixP->fx_addsy)
23254           {
23255             const char *msg = 0;
23256
23257             if (! S_IS_DEFINED (fixP->fx_addsy))
23258               msg = _("undefined symbol %s used as an immediate value");
23259             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
23260               msg = _("symbol %s is in a different section");
23261             else if (S_IS_WEAK (fixP->fx_addsy))
23262               msg = _("symbol %s is weak and may be overridden later");
23263
23264             if (msg)
23265               {
23266                 as_bad_where (fixP->fx_file, fixP->fx_line,
23267                               msg, S_GET_NAME (fixP->fx_addsy));
23268                 break;
23269               }
23270           }
23271
23272         newimm = encode_arm_immediate (value);
23273         temp = md_chars_to_number (buf, INSN_SIZE);
23274
23275         /* If the instruction will fail, see if we can fix things up by
23276            changing the opcode.  */
23277         if (newimm == (unsigned int) FAIL
23278             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
23279           {
23280             /* No ?  OK - try using two ADD instructions to generate
23281                the value.  */
23282             newimm = validate_immediate_twopart (value, & highpart);
23283
23284             /* Yes - then make sure that the second instruction is
23285                also an add.  */
23286             if (newimm != (unsigned int) FAIL)
23287               newinsn = temp;
23288             /* Still No ?  Try using a negated value.  */
23289             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
23290               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
23291             /* Otherwise - give up.  */
23292             else
23293               {
23294                 as_bad_where (fixP->fx_file, fixP->fx_line,
23295                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
23296                               (long) value);
23297                 break;
23298               }
23299
23300             /* Replace the first operand in the 2nd instruction (which
23301                is the PC) with the destination register.  We have
23302                already added in the PC in the first instruction and we
23303                do not want to do it again.  */
23304             newinsn &= ~ 0xf0000;
23305             newinsn |= ((newinsn & 0x0f000) << 4);
23306           }
23307
23308         newimm |= (temp & 0xfffff000);
23309         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
23310
23311         highpart |= (newinsn & 0xfffff000);
23312         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
23313       }
23314       break;
23315
23316     case BFD_RELOC_ARM_OFFSET_IMM:
23317       if (!fixP->fx_done && seg->use_rela_p)
23318         value = 0;
23319       /* Fall through.  */
23320
23321     case BFD_RELOC_ARM_LITERAL:
23322       sign = value > 0;
23323
23324       if (value < 0)
23325         value = - value;
23326
23327       if (validate_offset_imm (value, 0) == FAIL)
23328         {
23329           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
23330             as_bad_where (fixP->fx_file, fixP->fx_line,
23331                           _("invalid literal constant: pool needs to be closer"));
23332           else
23333             as_bad_where (fixP->fx_file, fixP->fx_line,
23334                           _("bad immediate value for offset (%ld)"),
23335                           (long) value);
23336           break;
23337         }
23338
23339       newval = md_chars_to_number (buf, INSN_SIZE);
23340       if (value == 0)
23341         newval &= 0xfffff000;
23342       else
23343         {
23344           newval &= 0xff7ff000;
23345           newval |= value | (sign ? INDEX_UP : 0);
23346         }
23347       md_number_to_chars (buf, newval, INSN_SIZE);
23348       break;
23349
23350     case BFD_RELOC_ARM_OFFSET_IMM8:
23351     case BFD_RELOC_ARM_HWLITERAL:
23352       sign = value > 0;
23353
23354       if (value < 0)
23355         value = - value;
23356
23357       if (validate_offset_imm (value, 1) == FAIL)
23358         {
23359           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
23360             as_bad_where (fixP->fx_file, fixP->fx_line,
23361                           _("invalid literal constant: pool needs to be closer"));
23362           else
23363             as_bad_where (fixP->fx_file, fixP->fx_line,
23364                           _("bad immediate value for 8-bit offset (%ld)"),
23365                           (long) value);
23366           break;
23367         }
23368
23369       newval = md_chars_to_number (buf, INSN_SIZE);
23370       if (value == 0)
23371         newval &= 0xfffff0f0;
23372       else
23373         {
23374           newval &= 0xff7ff0f0;
23375           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
23376         }
23377       md_number_to_chars (buf, newval, INSN_SIZE);
23378       break;
23379
23380     case BFD_RELOC_ARM_T32_OFFSET_U8:
23381       if (value < 0 || value > 1020 || value % 4 != 0)
23382         as_bad_where (fixP->fx_file, fixP->fx_line,
23383                       _("bad immediate value for offset (%ld)"), (long) value);
23384       value /= 4;
23385
23386       newval = md_chars_to_number (buf+2, THUMB_SIZE);
23387       newval |= value;
23388       md_number_to_chars (buf+2, newval, THUMB_SIZE);
23389       break;
23390
23391     case BFD_RELOC_ARM_T32_OFFSET_IMM:
23392       /* This is a complicated relocation used for all varieties of Thumb32
23393          load/store instruction with immediate offset:
23394
23395          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
23396                                                    *4, optional writeback(W)
23397                                                    (doubleword load/store)
23398
23399          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
23400          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
23401          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
23402          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
23403          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
23404
23405          Uppercase letters indicate bits that are already encoded at
23406          this point.  Lowercase letters are our problem.  For the
23407          second block of instructions, the secondary opcode nybble
23408          (bits 8..11) is present, and bit 23 is zero, even if this is
23409          a PC-relative operation.  */
23410       newval = md_chars_to_number (buf, THUMB_SIZE);
23411       newval <<= 16;
23412       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
23413
23414       if ((newval & 0xf0000000) == 0xe0000000)
23415         {
23416           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
23417           if (value >= 0)
23418             newval |= (1 << 23);
23419           else
23420             value = -value;
23421           if (value % 4 != 0)
23422             {
23423               as_bad_where (fixP->fx_file, fixP->fx_line,
23424                             _("offset not a multiple of 4"));
23425               break;
23426             }
23427           value /= 4;
23428           if (value > 0xff)
23429             {
23430               as_bad_where (fixP->fx_file, fixP->fx_line,
23431                             _("offset out of range"));
23432               break;
23433             }
23434           newval &= ~0xff;
23435         }
23436       else if ((newval & 0x000f0000) == 0x000f0000)
23437         {
23438           /* PC-relative, 12-bit offset.  */
23439           if (value >= 0)
23440             newval |= (1 << 23);
23441           else
23442             value = -value;
23443           if (value > 0xfff)
23444             {
23445               as_bad_where (fixP->fx_file, fixP->fx_line,
23446                             _("offset out of range"));
23447               break;
23448             }
23449           newval &= ~0xfff;
23450         }
23451       else if ((newval & 0x00000100) == 0x00000100)
23452         {
23453           /* Writeback: 8-bit, +/- offset.  */
23454           if (value >= 0)
23455             newval |= (1 << 9);
23456           else
23457             value = -value;
23458           if (value > 0xff)
23459             {
23460               as_bad_where (fixP->fx_file, fixP->fx_line,
23461                             _("offset out of range"));
23462               break;
23463             }
23464           newval &= ~0xff;
23465         }
23466       else if ((newval & 0x00000f00) == 0x00000e00)
23467         {
23468           /* T-instruction: positive 8-bit offset.  */
23469           if (value < 0 || value > 0xff)
23470             {
23471               as_bad_where (fixP->fx_file, fixP->fx_line,
23472                             _("offset out of range"));
23473               break;
23474             }
23475           newval &= ~0xff;
23476           newval |= value;
23477         }
23478       else
23479         {
23480           /* Positive 12-bit or negative 8-bit offset.  */
23481           int limit;
23482           if (value >= 0)
23483             {
23484               newval |= (1 << 23);
23485               limit = 0xfff;
23486             }
23487           else
23488             {
23489               value = -value;
23490               limit = 0xff;
23491             }
23492           if (value > limit)
23493             {
23494               as_bad_where (fixP->fx_file, fixP->fx_line,
23495                             _("offset out of range"));
23496               break;
23497             }
23498           newval &= ~limit;
23499         }
23500
23501       newval |= value;
23502       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
23503       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
23504       break;
23505
23506     case BFD_RELOC_ARM_SHIFT_IMM:
23507       newval = md_chars_to_number (buf, INSN_SIZE);
23508       if (((unsigned long) value) > 32
23509           || (value == 32
23510               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
23511         {
23512           as_bad_where (fixP->fx_file, fixP->fx_line,
23513                         _("shift expression is too large"));
23514           break;
23515         }
23516
23517       if (value == 0)
23518         /* Shifts of zero must be done as lsl.  */
23519         newval &= ~0x60;
23520       else if (value == 32)
23521         value = 0;
23522       newval &= 0xfffff07f;
23523       newval |= (value & 0x1f) << 7;
23524       md_number_to_chars (buf, newval, INSN_SIZE);
23525       break;
23526
23527     case BFD_RELOC_ARM_T32_IMMEDIATE:
23528     case BFD_RELOC_ARM_T32_ADD_IMM:
23529     case BFD_RELOC_ARM_T32_IMM12:
23530     case BFD_RELOC_ARM_T32_ADD_PC12:
23531       /* We claim that this fixup has been processed here,
23532          even if in fact we generate an error because we do
23533          not have a reloc for it, so tc_gen_reloc will reject it.  */
23534       fixP->fx_done = 1;
23535
23536       if (fixP->fx_addsy
23537           && ! S_IS_DEFINED (fixP->fx_addsy))
23538         {
23539           as_bad_where (fixP->fx_file, fixP->fx_line,
23540                         _("undefined symbol %s used as an immediate value"),
23541                         S_GET_NAME (fixP->fx_addsy));
23542           break;
23543         }
23544
23545       newval = md_chars_to_number (buf, THUMB_SIZE);
23546       newval <<= 16;
23547       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
23548
23549       newimm = FAIL;
23550       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23551            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
23552               Thumb2 modified immediate encoding (T2).  */
23553            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
23554           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23555         {
23556           newimm = encode_thumb32_immediate (value);
23557           if (newimm == (unsigned int) FAIL)
23558             newimm = thumb32_negate_data_op (&newval, value);
23559         }
23560       if (newimm == (unsigned int) FAIL)
23561         {
23562           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
23563             {
23564               /* Turn add/sum into addw/subw.  */
23565               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23566                 newval = (newval & 0xfeffffff) | 0x02000000;
23567               /* No flat 12-bit imm encoding for addsw/subsw.  */
23568               if ((newval & 0x00100000) == 0)
23569                 {
23570                   /* 12 bit immediate for addw/subw.  */
23571                   if (value < 0)
23572                     {
23573                       value = -value;
23574                       newval ^= 0x00a00000;
23575                     }
23576                   if (value > 0xfff)
23577                     newimm = (unsigned int) FAIL;
23578                   else
23579                     newimm = value;
23580                 }
23581             }
23582           else
23583             {
23584               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
23585                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
23586                  disassembling, MOV is preferred when there is no encoding
23587                  overlap.
23588                  NOTE: MOV is using ORR opcode under Thumb 2 mode.  */
23589               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
23590                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
23591                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
23592                   && value >= 0 && value <=0xffff)
23593                 {
23594                   /* Toggle bit[25] to change encoding from T2 to T3.  */
23595                   newval ^= 1 << 25;
23596                   /* Clear bits[19:16].  */
23597                   newval &= 0xfff0ffff;
23598                   /* Encoding high 4bits imm.  Code below will encode the
23599                      remaining low 12bits.  */
23600                   newval |= (value & 0x0000f000) << 4;
23601                   newimm = value & 0x00000fff;
23602                 }
23603             }
23604         }
23605
23606       if (newimm == (unsigned int)FAIL)
23607         {
23608           as_bad_where (fixP->fx_file, fixP->fx_line,
23609                         _("invalid constant (%lx) after fixup"),
23610                         (unsigned long) value);
23611           break;
23612         }
23613
23614       newval |= (newimm & 0x800) << 15;
23615       newval |= (newimm & 0x700) << 4;
23616       newval |= (newimm & 0x0ff);
23617
23618       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
23619       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
23620       break;
23621
23622     case BFD_RELOC_ARM_SMC:
23623       if (((unsigned long) value) > 0xffff)
23624         as_bad_where (fixP->fx_file, fixP->fx_line,
23625                       _("invalid smc expression"));
23626       newval = md_chars_to_number (buf, INSN_SIZE);
23627       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23628       md_number_to_chars (buf, newval, INSN_SIZE);
23629       break;
23630
23631     case BFD_RELOC_ARM_HVC:
23632       if (((unsigned long) value) > 0xffff)
23633         as_bad_where (fixP->fx_file, fixP->fx_line,
23634                       _("invalid hvc expression"));
23635       newval = md_chars_to_number (buf, INSN_SIZE);
23636       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23637       md_number_to_chars (buf, newval, INSN_SIZE);
23638       break;
23639
23640     case BFD_RELOC_ARM_SWI:
23641       if (fixP->tc_fix_data != 0)
23642         {
23643           if (((unsigned long) value) > 0xff)
23644             as_bad_where (fixP->fx_file, fixP->fx_line,
23645                           _("invalid swi expression"));
23646           newval = md_chars_to_number (buf, THUMB_SIZE);
23647           newval |= value;
23648           md_number_to_chars (buf, newval, THUMB_SIZE);
23649         }
23650       else
23651         {
23652           if (((unsigned long) value) > 0x00ffffff)
23653             as_bad_where (fixP->fx_file, fixP->fx_line,
23654                           _("invalid swi expression"));
23655           newval = md_chars_to_number (buf, INSN_SIZE);
23656           newval |= value;
23657           md_number_to_chars (buf, newval, INSN_SIZE);
23658         }
23659       break;
23660
23661     case BFD_RELOC_ARM_MULTI:
23662       if (((unsigned long) value) > 0xffff)
23663         as_bad_where (fixP->fx_file, fixP->fx_line,
23664                       _("invalid expression in load/store multiple"));
23665       newval = value | md_chars_to_number (buf, INSN_SIZE);
23666       md_number_to_chars (buf, newval, INSN_SIZE);
23667       break;
23668
23669 #ifdef OBJ_ELF
23670     case BFD_RELOC_ARM_PCREL_CALL:
23671
23672       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23673           && fixP->fx_addsy
23674           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23675           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23676           && THUMB_IS_FUNC (fixP->fx_addsy))
23677         /* Flip the bl to blx. This is a simple flip
23678            bit here because we generate PCREL_CALL for
23679            unconditional bls.  */
23680         {
23681           newval = md_chars_to_number (buf, INSN_SIZE);
23682           newval = newval | 0x10000000;
23683           md_number_to_chars (buf, newval, INSN_SIZE);
23684           temp = 1;
23685           fixP->fx_done = 1;
23686         }
23687       else
23688         temp = 3;
23689       goto arm_branch_common;
23690
23691     case BFD_RELOC_ARM_PCREL_JUMP:
23692       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23693           && fixP->fx_addsy
23694           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23695           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23696           && THUMB_IS_FUNC (fixP->fx_addsy))
23697         {
23698           /* This would map to a bl<cond>, b<cond>,
23699              b<always> to a Thumb function. We
23700              need to force a relocation for this particular
23701              case.  */
23702           newval = md_chars_to_number (buf, INSN_SIZE);
23703           fixP->fx_done = 0;
23704         }
23705       /* Fall through.  */
23706
23707     case BFD_RELOC_ARM_PLT32:
23708 #endif
23709     case BFD_RELOC_ARM_PCREL_BRANCH:
23710       temp = 3;
23711       goto arm_branch_common;
23712
23713     case BFD_RELOC_ARM_PCREL_BLX:
23714
23715       temp = 1;
23716       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23717           && fixP->fx_addsy
23718           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23719           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23720           && ARM_IS_FUNC (fixP->fx_addsy))
23721         {
23722           /* Flip the blx to a bl and warn.  */
23723           const char *name = S_GET_NAME (fixP->fx_addsy);
23724           newval = 0xeb000000;
23725           as_warn_where (fixP->fx_file, fixP->fx_line,
23726                          _("blx to '%s' an ARM ISA state function changed to bl"),
23727                           name);
23728           md_number_to_chars (buf, newval, INSN_SIZE);
23729           temp = 3;
23730           fixP->fx_done = 1;
23731         }
23732
23733 #ifdef OBJ_ELF
23734        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23735          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
23736 #endif
23737
23738     arm_branch_common:
23739       /* We are going to store value (shifted right by two) in the
23740          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
23741          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
23742          also be clear.  */
23743       if (value & temp)
23744         as_bad_where (fixP->fx_file, fixP->fx_line,
23745                       _("misaligned branch destination"));
23746       if ((value & (offsetT)0xfe000000) != (offsetT)0
23747           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
23748         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23749
23750       if (fixP->fx_done || !seg->use_rela_p)
23751         {
23752           newval = md_chars_to_number (buf, INSN_SIZE);
23753           newval |= (value >> 2) & 0x00ffffff;
23754           /* Set the H bit on BLX instructions.  */
23755           if (temp == 1)
23756             {
23757               if (value & 2)
23758                 newval |= 0x01000000;
23759               else
23760                 newval &= ~0x01000000;
23761             }
23762           md_number_to_chars (buf, newval, INSN_SIZE);
23763         }
23764       break;
23765
23766     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
23767       /* CBZ can only branch forward.  */
23768
23769       /* Attempts to use CBZ to branch to the next instruction
23770          (which, strictly speaking, are prohibited) will be turned into
23771          no-ops.
23772
23773          FIXME: It may be better to remove the instruction completely and
23774          perform relaxation.  */
23775       if (value == -2)
23776         {
23777           newval = md_chars_to_number (buf, THUMB_SIZE);
23778           newval = 0xbf00; /* NOP encoding T1 */
23779           md_number_to_chars (buf, newval, THUMB_SIZE);
23780         }
23781       else
23782         {
23783           if (value & ~0x7e)
23784             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23785
23786           if (fixP->fx_done || !seg->use_rela_p)
23787             {
23788               newval = md_chars_to_number (buf, THUMB_SIZE);
23789               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
23790               md_number_to_chars (buf, newval, THUMB_SIZE);
23791             }
23792         }
23793       break;
23794
23795     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
23796       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
23797         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23798
23799       if (fixP->fx_done || !seg->use_rela_p)
23800         {
23801           newval = md_chars_to_number (buf, THUMB_SIZE);
23802           newval |= (value & 0x1ff) >> 1;
23803           md_number_to_chars (buf, newval, THUMB_SIZE);
23804         }
23805       break;
23806
23807     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
23808       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
23809         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23810
23811       if (fixP->fx_done || !seg->use_rela_p)
23812         {
23813           newval = md_chars_to_number (buf, THUMB_SIZE);
23814           newval |= (value & 0xfff) >> 1;
23815           md_number_to_chars (buf, newval, THUMB_SIZE);
23816         }
23817       break;
23818
23819     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23820       if (fixP->fx_addsy
23821           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23822           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23823           && ARM_IS_FUNC (fixP->fx_addsy)
23824           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23825         {
23826           /* Force a relocation for a branch 20 bits wide.  */
23827           fixP->fx_done = 0;
23828         }
23829       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
23830         as_bad_where (fixP->fx_file, fixP->fx_line,
23831                       _("conditional branch out of range"));
23832
23833       if (fixP->fx_done || !seg->use_rela_p)
23834         {
23835           offsetT newval2;
23836           addressT S, J1, J2, lo, hi;
23837
23838           S  = (value & 0x00100000) >> 20;
23839           J2 = (value & 0x00080000) >> 19;
23840           J1 = (value & 0x00040000) >> 18;
23841           hi = (value & 0x0003f000) >> 12;
23842           lo = (value & 0x00000ffe) >> 1;
23843
23844           newval   = md_chars_to_number (buf, THUMB_SIZE);
23845           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23846           newval  |= (S << 10) | hi;
23847           newval2 |= (J1 << 13) | (J2 << 11) | lo;
23848           md_number_to_chars (buf, newval, THUMB_SIZE);
23849           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23850         }
23851       break;
23852
23853     case BFD_RELOC_THUMB_PCREL_BLX:
23854       /* If there is a blx from a thumb state function to
23855          another thumb function flip this to a bl and warn
23856          about it.  */
23857
23858       if (fixP->fx_addsy
23859           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23860           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23861           && THUMB_IS_FUNC (fixP->fx_addsy))
23862         {
23863           const char *name = S_GET_NAME (fixP->fx_addsy);
23864           as_warn_where (fixP->fx_file, fixP->fx_line,
23865                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
23866                          name);
23867           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23868           newval = newval | 0x1000;
23869           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23870           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23871           fixP->fx_done = 1;
23872         }
23873
23874
23875       goto thumb_bl_common;
23876
23877     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23878       /* A bl from Thumb state ISA to an internal ARM state function
23879          is converted to a blx.  */
23880       if (fixP->fx_addsy
23881           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23882           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23883           && ARM_IS_FUNC (fixP->fx_addsy)
23884           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23885         {
23886           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23887           newval = newval & ~0x1000;
23888           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23889           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
23890           fixP->fx_done = 1;
23891         }
23892
23893     thumb_bl_common:
23894
23895       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23896         /* For a BLX instruction, make sure that the relocation is rounded up
23897            to a word boundary.  This follows the semantics of the instruction
23898            which specifies that bit 1 of the target address will come from bit
23899            1 of the base address.  */
23900         value = (value + 3) & ~ 3;
23901
23902 #ifdef OBJ_ELF
23903        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
23904            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23905          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23906 #endif
23907
23908       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
23909         {
23910           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
23911             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23912           else if ((value & ~0x1ffffff)
23913                    && ((value & ~0x1ffffff) != ~0x1ffffff))
23914             as_bad_where (fixP->fx_file, fixP->fx_line,
23915                           _("Thumb2 branch out of range"));
23916         }
23917
23918       if (fixP->fx_done || !seg->use_rela_p)
23919         encode_thumb2_b_bl_offset (buf, value);
23920
23921       break;
23922
23923     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23924       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
23925         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23926
23927       if (fixP->fx_done || !seg->use_rela_p)
23928           encode_thumb2_b_bl_offset (buf, value);
23929
23930       break;
23931
23932     case BFD_RELOC_8:
23933       if (fixP->fx_done || !seg->use_rela_p)
23934         *buf = value;
23935       break;
23936
23937     case BFD_RELOC_16:
23938       if (fixP->fx_done || !seg->use_rela_p)
23939         md_number_to_chars (buf, value, 2);
23940       break;
23941
23942 #ifdef OBJ_ELF
23943     case BFD_RELOC_ARM_TLS_CALL:
23944     case BFD_RELOC_ARM_THM_TLS_CALL:
23945     case BFD_RELOC_ARM_TLS_DESCSEQ:
23946     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
23947     case BFD_RELOC_ARM_TLS_GOTDESC:
23948     case BFD_RELOC_ARM_TLS_GD32:
23949     case BFD_RELOC_ARM_TLS_LE32:
23950     case BFD_RELOC_ARM_TLS_IE32:
23951     case BFD_RELOC_ARM_TLS_LDM32:
23952     case BFD_RELOC_ARM_TLS_LDO32:
23953       S_SET_THREAD_LOCAL (fixP->fx_addsy);
23954       break;
23955
23956     case BFD_RELOC_ARM_GOT32:
23957     case BFD_RELOC_ARM_GOTOFF:
23958       break;
23959
23960     case BFD_RELOC_ARM_GOT_PREL:
23961       if (fixP->fx_done || !seg->use_rela_p)
23962         md_number_to_chars (buf, value, 4);
23963       break;
23964
23965     case BFD_RELOC_ARM_TARGET2:
23966       /* TARGET2 is not partial-inplace, so we need to write the
23967          addend here for REL targets, because it won't be written out
23968          during reloc processing later.  */
23969       if (fixP->fx_done || !seg->use_rela_p)
23970         md_number_to_chars (buf, fixP->fx_offset, 4);
23971       break;
23972 #endif
23973
23974     case BFD_RELOC_RVA:
23975     case BFD_RELOC_32:
23976     case BFD_RELOC_ARM_TARGET1:
23977     case BFD_RELOC_ARM_ROSEGREL32:
23978     case BFD_RELOC_ARM_SBREL32:
23979     case BFD_RELOC_32_PCREL:
23980 #ifdef TE_PE
23981     case BFD_RELOC_32_SECREL:
23982 #endif
23983       if (fixP->fx_done || !seg->use_rela_p)
23984 #ifdef TE_WINCE
23985         /* For WinCE we only do this for pcrel fixups.  */
23986         if (fixP->fx_done || fixP->fx_pcrel)
23987 #endif
23988           md_number_to_chars (buf, value, 4);
23989       break;
23990
23991 #ifdef OBJ_ELF
23992     case BFD_RELOC_ARM_PREL31:
23993       if (fixP->fx_done || !seg->use_rela_p)
23994         {
23995           newval = md_chars_to_number (buf, 4) & 0x80000000;
23996           if ((value ^ (value >> 1)) & 0x40000000)
23997             {
23998               as_bad_where (fixP->fx_file, fixP->fx_line,
23999                             _("rel31 relocation overflow"));
24000             }
24001           newval |= value & 0x7fffffff;
24002           md_number_to_chars (buf, newval, 4);
24003         }
24004       break;
24005 #endif
24006
24007     case BFD_RELOC_ARM_CP_OFF_IMM:
24008     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
24009       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
24010         newval = md_chars_to_number (buf, INSN_SIZE);
24011       else
24012         newval = get_thumb32_insn (buf);
24013       if ((newval & 0x0f200f00) == 0x0d000900)
24014         {
24015           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
24016              has permitted values that are multiples of 2, in the range 0
24017              to 510.  */
24018           if (value < -510 || value > 510 || (value & 1))
24019             as_bad_where (fixP->fx_file, fixP->fx_line,
24020                           _("co-processor offset out of range"));
24021         }
24022       else if (value < -1023 || value > 1023 || (value & 3))
24023         as_bad_where (fixP->fx_file, fixP->fx_line,
24024                       _("co-processor offset out of range"));
24025     cp_off_common:
24026       sign = value > 0;
24027       if (value < 0)
24028         value = -value;
24029       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24030           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
24031         newval = md_chars_to_number (buf, INSN_SIZE);
24032       else
24033         newval = get_thumb32_insn (buf);
24034       if (value == 0)
24035         newval &= 0xffffff00;
24036       else
24037         {
24038           newval &= 0xff7fff00;
24039           if ((newval & 0x0f200f00) == 0x0d000900)
24040             {
24041               /* This is a fp16 vstr/vldr.
24042
24043                  It requires the immediate offset in the instruction is shifted
24044                  left by 1 to be a half-word offset.
24045
24046                  Here, left shift by 1 first, and later right shift by 2
24047                  should get the right offset.  */
24048               value <<= 1;
24049             }
24050           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
24051         }
24052       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24053           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
24054         md_number_to_chars (buf, newval, INSN_SIZE);
24055       else
24056         put_thumb32_insn (buf, newval);
24057       break;
24058
24059     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
24060     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
24061       if (value < -255 || value > 255)
24062         as_bad_where (fixP->fx_file, fixP->fx_line,
24063                       _("co-processor offset out of range"));
24064       value *= 4;
24065       goto cp_off_common;
24066
24067     case BFD_RELOC_ARM_THUMB_OFFSET:
24068       newval = md_chars_to_number (buf, THUMB_SIZE);
24069       /* Exactly what ranges, and where the offset is inserted depends
24070          on the type of instruction, we can establish this from the
24071          top 4 bits.  */
24072       switch (newval >> 12)
24073         {
24074         case 4: /* PC load.  */
24075           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
24076              forced to zero for these loads; md_pcrel_from has already
24077              compensated for this.  */
24078           if (value & 3)
24079             as_bad_where (fixP->fx_file, fixP->fx_line,
24080                           _("invalid offset, target not word aligned (0x%08lX)"),
24081                           (((unsigned long) fixP->fx_frag->fr_address
24082                             + (unsigned long) fixP->fx_where) & ~3)
24083                           + (unsigned long) value);
24084
24085           if (value & ~0x3fc)
24086             as_bad_where (fixP->fx_file, fixP->fx_line,
24087                           _("invalid offset, value too big (0x%08lX)"),
24088                           (long) value);
24089
24090           newval |= value >> 2;
24091           break;
24092
24093         case 9: /* SP load/store.  */
24094           if (value & ~0x3fc)
24095             as_bad_where (fixP->fx_file, fixP->fx_line,
24096                           _("invalid offset, value too big (0x%08lX)"),
24097                           (long) value);
24098           newval |= value >> 2;
24099           break;
24100
24101         case 6: /* Word load/store.  */
24102           if (value & ~0x7c)
24103             as_bad_where (fixP->fx_file, fixP->fx_line,
24104                           _("invalid offset, value too big (0x%08lX)"),
24105                           (long) value);
24106           newval |= value << 4; /* 6 - 2.  */
24107           break;
24108
24109         case 7: /* Byte load/store.  */
24110           if (value & ~0x1f)
24111             as_bad_where (fixP->fx_file, fixP->fx_line,
24112                           _("invalid offset, value too big (0x%08lX)"),
24113                           (long) value);
24114           newval |= value << 6;
24115           break;
24116
24117         case 8: /* Halfword load/store.  */
24118           if (value & ~0x3e)
24119             as_bad_where (fixP->fx_file, fixP->fx_line,
24120                           _("invalid offset, value too big (0x%08lX)"),
24121                           (long) value);
24122           newval |= value << 5; /* 6 - 1.  */
24123           break;
24124
24125         default:
24126           as_bad_where (fixP->fx_file, fixP->fx_line,
24127                         "Unable to process relocation for thumb opcode: %lx",
24128                         (unsigned long) newval);
24129           break;
24130         }
24131       md_number_to_chars (buf, newval, THUMB_SIZE);
24132       break;
24133
24134     case BFD_RELOC_ARM_THUMB_ADD:
24135       /* This is a complicated relocation, since we use it for all of
24136          the following immediate relocations:
24137
24138             3bit ADD/SUB
24139             8bit ADD/SUB
24140             9bit ADD/SUB SP word-aligned
24141            10bit ADD PC/SP word-aligned
24142
24143          The type of instruction being processed is encoded in the
24144          instruction field:
24145
24146            0x8000  SUB
24147            0x00F0  Rd
24148            0x000F  Rs
24149       */
24150       newval = md_chars_to_number (buf, THUMB_SIZE);
24151       {
24152         int rd = (newval >> 4) & 0xf;
24153         int rs = newval & 0xf;
24154         int subtract = !!(newval & 0x8000);
24155
24156         /* Check for HI regs, only very restricted cases allowed:
24157            Adjusting SP, and using PC or SP to get an address.  */
24158         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
24159             || (rs > 7 && rs != REG_SP && rs != REG_PC))
24160           as_bad_where (fixP->fx_file, fixP->fx_line,
24161                         _("invalid Hi register with immediate"));
24162
24163         /* If value is negative, choose the opposite instruction.  */
24164         if (value < 0)
24165           {
24166             value = -value;
24167             subtract = !subtract;
24168             if (value < 0)
24169               as_bad_where (fixP->fx_file, fixP->fx_line,
24170                             _("immediate value out of range"));
24171           }
24172
24173         if (rd == REG_SP)
24174           {
24175             if (value & ~0x1fc)
24176               as_bad_where (fixP->fx_file, fixP->fx_line,
24177                             _("invalid immediate for stack address calculation"));
24178             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
24179             newval |= value >> 2;
24180           }
24181         else if (rs == REG_PC || rs == REG_SP)
24182           {
24183             /* PR gas/18541.  If the addition is for a defined symbol
24184                within range of an ADR instruction then accept it.  */
24185             if (subtract
24186                 && value == 4
24187                 && fixP->fx_addsy != NULL)
24188               {
24189                 subtract = 0;
24190
24191                 if (! S_IS_DEFINED (fixP->fx_addsy)
24192                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
24193                     || S_IS_WEAK (fixP->fx_addsy))
24194                   {
24195                     as_bad_where (fixP->fx_file, fixP->fx_line,
24196                                   _("address calculation needs a strongly defined nearby symbol"));
24197                   }
24198                 else
24199                   {
24200                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
24201
24202                     /* Round up to the next 4-byte boundary.  */
24203                     if (v & 3)
24204                       v = (v + 3) & ~ 3;
24205                     else
24206                       v += 4;
24207                     v = S_GET_VALUE (fixP->fx_addsy) - v;
24208
24209                     if (v & ~0x3fc)
24210                       {
24211                         as_bad_where (fixP->fx_file, fixP->fx_line,
24212                                       _("symbol too far away"));
24213                       }
24214                     else
24215                       {
24216                         fixP->fx_done = 1;
24217                         value = v;
24218                       }
24219                   }
24220               }
24221
24222             if (subtract || value & ~0x3fc)
24223               as_bad_where (fixP->fx_file, fixP->fx_line,
24224                             _("invalid immediate for address calculation (value = 0x%08lX)"),
24225                             (unsigned long) (subtract ? - value : value));
24226             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
24227             newval |= rd << 8;
24228             newval |= value >> 2;
24229           }
24230         else if (rs == rd)
24231           {
24232             if (value & ~0xff)
24233               as_bad_where (fixP->fx_file, fixP->fx_line,
24234                             _("immediate value out of range"));
24235             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
24236             newval |= (rd << 8) | value;
24237           }
24238         else
24239           {
24240             if (value & ~0x7)
24241               as_bad_where (fixP->fx_file, fixP->fx_line,
24242                             _("immediate value out of range"));
24243             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
24244             newval |= rd | (rs << 3) | (value << 6);
24245           }
24246       }
24247       md_number_to_chars (buf, newval, THUMB_SIZE);
24248       break;
24249
24250     case BFD_RELOC_ARM_THUMB_IMM:
24251       newval = md_chars_to_number (buf, THUMB_SIZE);
24252       if (value < 0 || value > 255)
24253         as_bad_where (fixP->fx_file, fixP->fx_line,
24254                       _("invalid immediate: %ld is out of range"),
24255                       (long) value);
24256       newval |= value;
24257       md_number_to_chars (buf, newval, THUMB_SIZE);
24258       break;
24259
24260     case BFD_RELOC_ARM_THUMB_SHIFT:
24261       /* 5bit shift value (0..32).  LSL cannot take 32.  */
24262       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
24263       temp = newval & 0xf800;
24264       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
24265         as_bad_where (fixP->fx_file, fixP->fx_line,
24266                       _("invalid shift value: %ld"), (long) value);
24267       /* Shifts of zero must be encoded as LSL.  */
24268       if (value == 0)
24269         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
24270       /* Shifts of 32 are encoded as zero.  */
24271       else if (value == 32)
24272         value = 0;
24273       newval |= value << 6;
24274       md_number_to_chars (buf, newval, THUMB_SIZE);
24275       break;
24276
24277     case BFD_RELOC_VTABLE_INHERIT:
24278     case BFD_RELOC_VTABLE_ENTRY:
24279       fixP->fx_done = 0;
24280       return;
24281
24282     case BFD_RELOC_ARM_MOVW:
24283     case BFD_RELOC_ARM_MOVT:
24284     case BFD_RELOC_ARM_THUMB_MOVW:
24285     case BFD_RELOC_ARM_THUMB_MOVT:
24286       if (fixP->fx_done || !seg->use_rela_p)
24287         {
24288           /* REL format relocations are limited to a 16-bit addend.  */
24289           if (!fixP->fx_done)
24290             {
24291               if (value < -0x8000 || value > 0x7fff)
24292                   as_bad_where (fixP->fx_file, fixP->fx_line,
24293                                 _("offset out of range"));
24294             }
24295           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
24296                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
24297             {
24298               value >>= 16;
24299             }
24300
24301           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
24302               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
24303             {
24304               newval = get_thumb32_insn (buf);
24305               newval &= 0xfbf08f00;
24306               newval |= (value & 0xf000) << 4;
24307               newval |= (value & 0x0800) << 15;
24308               newval |= (value & 0x0700) << 4;
24309               newval |= (value & 0x00ff);
24310               put_thumb32_insn (buf, newval);
24311             }
24312           else
24313             {
24314               newval = md_chars_to_number (buf, 4);
24315               newval &= 0xfff0f000;
24316               newval |= value & 0x0fff;
24317               newval |= (value & 0xf000) << 4;
24318               md_number_to_chars (buf, newval, 4);
24319             }
24320         }
24321       return;
24322
24323    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24324    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24325    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24326    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24327       gas_assert (!fixP->fx_done);
24328       {
24329         bfd_vma insn;
24330         bfd_boolean is_mov;
24331         bfd_vma encoded_addend = value;
24332
24333         /* Check that addend can be encoded in instruction.  */
24334         if (!seg->use_rela_p && (value < 0 || value > 255))
24335           as_bad_where (fixP->fx_file, fixP->fx_line,
24336                         _("the offset 0x%08lX is not representable"),
24337                         (unsigned long) encoded_addend);
24338
24339         /* Extract the instruction.  */
24340         insn = md_chars_to_number (buf, THUMB_SIZE);
24341         is_mov = (insn & 0xf800) == 0x2000;
24342
24343         /* Encode insn.  */
24344         if (is_mov)
24345           {
24346             if (!seg->use_rela_p)
24347               insn |= encoded_addend;
24348           }
24349         else
24350           {
24351             int rd, rs;
24352
24353             /* Extract the instruction.  */
24354              /* Encoding is the following
24355                 0x8000  SUB
24356                 0x00F0  Rd
24357                 0x000F  Rs
24358              */
24359              /* The following conditions must be true :
24360                 - ADD
24361                 - Rd == Rs
24362                 - Rd <= 7
24363              */
24364             rd = (insn >> 4) & 0xf;
24365             rs = insn & 0xf;
24366             if ((insn & 0x8000) || (rd != rs) || rd > 7)
24367               as_bad_where (fixP->fx_file, fixP->fx_line,
24368                         _("Unable to process relocation for thumb opcode: %lx"),
24369                         (unsigned long) insn);
24370
24371             /* Encode as ADD immediate8 thumb 1 code.  */
24372             insn = 0x3000 | (rd << 8);
24373
24374             /* Place the encoded addend into the first 8 bits of the
24375                instruction.  */
24376             if (!seg->use_rela_p)
24377               insn |= encoded_addend;
24378           }
24379
24380         /* Update the instruction.  */
24381         md_number_to_chars (buf, insn, THUMB_SIZE);
24382       }
24383       break;
24384
24385    case BFD_RELOC_ARM_ALU_PC_G0_NC:
24386    case BFD_RELOC_ARM_ALU_PC_G0:
24387    case BFD_RELOC_ARM_ALU_PC_G1_NC:
24388    case BFD_RELOC_ARM_ALU_PC_G1:
24389    case BFD_RELOC_ARM_ALU_PC_G2:
24390    case BFD_RELOC_ARM_ALU_SB_G0_NC:
24391    case BFD_RELOC_ARM_ALU_SB_G0:
24392    case BFD_RELOC_ARM_ALU_SB_G1_NC:
24393    case BFD_RELOC_ARM_ALU_SB_G1:
24394    case BFD_RELOC_ARM_ALU_SB_G2:
24395      gas_assert (!fixP->fx_done);
24396      if (!seg->use_rela_p)
24397        {
24398          bfd_vma insn;
24399          bfd_vma encoded_addend;
24400          bfd_vma addend_abs = abs (value);
24401
24402          /* Check that the absolute value of the addend can be
24403             expressed as an 8-bit constant plus a rotation.  */
24404          encoded_addend = encode_arm_immediate (addend_abs);
24405          if (encoded_addend == (unsigned int) FAIL)
24406            as_bad_where (fixP->fx_file, fixP->fx_line,
24407                          _("the offset 0x%08lX is not representable"),
24408                          (unsigned long) addend_abs);
24409
24410          /* Extract the instruction.  */
24411          insn = md_chars_to_number (buf, INSN_SIZE);
24412
24413          /* If the addend is positive, use an ADD instruction.
24414             Otherwise use a SUB.  Take care not to destroy the S bit.  */
24415          insn &= 0xff1fffff;
24416          if (value < 0)
24417            insn |= 1 << 22;
24418          else
24419            insn |= 1 << 23;
24420
24421          /* Place the encoded addend into the first 12 bits of the
24422             instruction.  */
24423          insn &= 0xfffff000;
24424          insn |= encoded_addend;
24425
24426          /* Update the instruction.  */
24427          md_number_to_chars (buf, insn, INSN_SIZE);
24428        }
24429      break;
24430
24431     case BFD_RELOC_ARM_LDR_PC_G0:
24432     case BFD_RELOC_ARM_LDR_PC_G1:
24433     case BFD_RELOC_ARM_LDR_PC_G2:
24434     case BFD_RELOC_ARM_LDR_SB_G0:
24435     case BFD_RELOC_ARM_LDR_SB_G1:
24436     case BFD_RELOC_ARM_LDR_SB_G2:
24437       gas_assert (!fixP->fx_done);
24438       if (!seg->use_rela_p)
24439         {
24440           bfd_vma insn;
24441           bfd_vma addend_abs = abs (value);
24442
24443           /* Check that the absolute value of the addend can be
24444              encoded in 12 bits.  */
24445           if (addend_abs >= 0x1000)
24446             as_bad_where (fixP->fx_file, fixP->fx_line,
24447                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
24448                           (unsigned long) addend_abs);
24449
24450           /* Extract the instruction.  */
24451           insn = md_chars_to_number (buf, INSN_SIZE);
24452
24453           /* If the addend is negative, clear bit 23 of the instruction.
24454              Otherwise set it.  */
24455           if (value < 0)
24456             insn &= ~(1 << 23);
24457           else
24458             insn |= 1 << 23;
24459
24460           /* Place the absolute value of the addend into the first 12 bits
24461              of the instruction.  */
24462           insn &= 0xfffff000;
24463           insn |= addend_abs;
24464
24465           /* Update the instruction.  */
24466           md_number_to_chars (buf, insn, INSN_SIZE);
24467         }
24468       break;
24469
24470     case BFD_RELOC_ARM_LDRS_PC_G0:
24471     case BFD_RELOC_ARM_LDRS_PC_G1:
24472     case BFD_RELOC_ARM_LDRS_PC_G2:
24473     case BFD_RELOC_ARM_LDRS_SB_G0:
24474     case BFD_RELOC_ARM_LDRS_SB_G1:
24475     case BFD_RELOC_ARM_LDRS_SB_G2:
24476       gas_assert (!fixP->fx_done);
24477       if (!seg->use_rela_p)
24478         {
24479           bfd_vma insn;
24480           bfd_vma addend_abs = abs (value);
24481
24482           /* Check that the absolute value of the addend can be
24483              encoded in 8 bits.  */
24484           if (addend_abs >= 0x100)
24485             as_bad_where (fixP->fx_file, fixP->fx_line,
24486                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
24487                           (unsigned long) addend_abs);
24488
24489           /* Extract the instruction.  */
24490           insn = md_chars_to_number (buf, INSN_SIZE);
24491
24492           /* If the addend is negative, clear bit 23 of the instruction.
24493              Otherwise set it.  */
24494           if (value < 0)
24495             insn &= ~(1 << 23);
24496           else
24497             insn |= 1 << 23;
24498
24499           /* Place the first four bits of the absolute value of the addend
24500              into the first 4 bits of the instruction, and the remaining
24501              four into bits 8 .. 11.  */
24502           insn &= 0xfffff0f0;
24503           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
24504
24505           /* Update the instruction.  */
24506           md_number_to_chars (buf, insn, INSN_SIZE);
24507         }
24508       break;
24509
24510     case BFD_RELOC_ARM_LDC_PC_G0:
24511     case BFD_RELOC_ARM_LDC_PC_G1:
24512     case BFD_RELOC_ARM_LDC_PC_G2:
24513     case BFD_RELOC_ARM_LDC_SB_G0:
24514     case BFD_RELOC_ARM_LDC_SB_G1:
24515     case BFD_RELOC_ARM_LDC_SB_G2:
24516       gas_assert (!fixP->fx_done);
24517       if (!seg->use_rela_p)
24518         {
24519           bfd_vma insn;
24520           bfd_vma addend_abs = abs (value);
24521
24522           /* Check that the absolute value of the addend is a multiple of
24523              four and, when divided by four, fits in 8 bits.  */
24524           if (addend_abs & 0x3)
24525             as_bad_where (fixP->fx_file, fixP->fx_line,
24526                           _("bad offset 0x%08lX (must be word-aligned)"),
24527                           (unsigned long) addend_abs);
24528
24529           if ((addend_abs >> 2) > 0xff)
24530             as_bad_where (fixP->fx_file, fixP->fx_line,
24531                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
24532                           (unsigned long) addend_abs);
24533
24534           /* Extract the instruction.  */
24535           insn = md_chars_to_number (buf, INSN_SIZE);
24536
24537           /* If the addend is negative, clear bit 23 of the instruction.
24538              Otherwise set it.  */
24539           if (value < 0)
24540             insn &= ~(1 << 23);
24541           else
24542             insn |= 1 << 23;
24543
24544           /* Place the addend (divided by four) into the first eight
24545              bits of the instruction.  */
24546           insn &= 0xfffffff0;
24547           insn |= addend_abs >> 2;
24548
24549           /* Update the instruction.  */
24550           md_number_to_chars (buf, insn, INSN_SIZE);
24551         }
24552       break;
24553
24554     case BFD_RELOC_ARM_V4BX:
24555       /* This will need to go in the object file.  */
24556       fixP->fx_done = 0;
24557       break;
24558
24559     case BFD_RELOC_UNUSED:
24560     default:
24561       as_bad_where (fixP->fx_file, fixP->fx_line,
24562                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
24563     }
24564 }
24565
24566 /* Translate internal representation of relocation info to BFD target
24567    format.  */
24568
24569 arelent *
24570 tc_gen_reloc (asection *section, fixS *fixp)
24571 {
24572   arelent * reloc;
24573   bfd_reloc_code_real_type code;
24574
24575   reloc = XNEW (arelent);
24576
24577   reloc->sym_ptr_ptr = XNEW (asymbol *);
24578   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
24579   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
24580
24581   if (fixp->fx_pcrel)
24582     {
24583       if (section->use_rela_p)
24584         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
24585       else
24586         fixp->fx_offset = reloc->address;
24587     }
24588   reloc->addend = fixp->fx_offset;
24589
24590   switch (fixp->fx_r_type)
24591     {
24592     case BFD_RELOC_8:
24593       if (fixp->fx_pcrel)
24594         {
24595           code = BFD_RELOC_8_PCREL;
24596           break;
24597         }
24598       /* Fall through.  */
24599
24600     case BFD_RELOC_16:
24601       if (fixp->fx_pcrel)
24602         {
24603           code = BFD_RELOC_16_PCREL;
24604           break;
24605         }
24606       /* Fall through.  */
24607
24608     case BFD_RELOC_32:
24609       if (fixp->fx_pcrel)
24610         {
24611           code = BFD_RELOC_32_PCREL;
24612           break;
24613         }
24614       /* Fall through.  */
24615
24616     case BFD_RELOC_ARM_MOVW:
24617       if (fixp->fx_pcrel)
24618         {
24619           code = BFD_RELOC_ARM_MOVW_PCREL;
24620           break;
24621         }
24622       /* Fall through.  */
24623
24624     case BFD_RELOC_ARM_MOVT:
24625       if (fixp->fx_pcrel)
24626         {
24627           code = BFD_RELOC_ARM_MOVT_PCREL;
24628           break;
24629         }
24630       /* Fall through.  */
24631
24632     case BFD_RELOC_ARM_THUMB_MOVW:
24633       if (fixp->fx_pcrel)
24634         {
24635           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
24636           break;
24637         }
24638       /* Fall through.  */
24639
24640     case BFD_RELOC_ARM_THUMB_MOVT:
24641       if (fixp->fx_pcrel)
24642         {
24643           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
24644           break;
24645         }
24646       /* Fall through.  */
24647
24648     case BFD_RELOC_NONE:
24649     case BFD_RELOC_ARM_PCREL_BRANCH:
24650     case BFD_RELOC_ARM_PCREL_BLX:
24651     case BFD_RELOC_RVA:
24652     case BFD_RELOC_THUMB_PCREL_BRANCH7:
24653     case BFD_RELOC_THUMB_PCREL_BRANCH9:
24654     case BFD_RELOC_THUMB_PCREL_BRANCH12:
24655     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24656     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24657     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24658     case BFD_RELOC_VTABLE_ENTRY:
24659     case BFD_RELOC_VTABLE_INHERIT:
24660 #ifdef TE_PE
24661     case BFD_RELOC_32_SECREL:
24662 #endif
24663       code = fixp->fx_r_type;
24664       break;
24665
24666     case BFD_RELOC_THUMB_PCREL_BLX:
24667 #ifdef OBJ_ELF
24668       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
24669         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
24670       else
24671 #endif
24672         code = BFD_RELOC_THUMB_PCREL_BLX;
24673       break;
24674
24675     case BFD_RELOC_ARM_LITERAL:
24676     case BFD_RELOC_ARM_HWLITERAL:
24677       /* If this is called then the a literal has
24678          been referenced across a section boundary.  */
24679       as_bad_where (fixp->fx_file, fixp->fx_line,
24680                     _("literal referenced across section boundary"));
24681       return NULL;
24682
24683 #ifdef OBJ_ELF
24684     case BFD_RELOC_ARM_TLS_CALL:
24685     case BFD_RELOC_ARM_THM_TLS_CALL:
24686     case BFD_RELOC_ARM_TLS_DESCSEQ:
24687     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
24688     case BFD_RELOC_ARM_GOT32:
24689     case BFD_RELOC_ARM_GOTOFF:
24690     case BFD_RELOC_ARM_GOT_PREL:
24691     case BFD_RELOC_ARM_PLT32:
24692     case BFD_RELOC_ARM_TARGET1:
24693     case BFD_RELOC_ARM_ROSEGREL32:
24694     case BFD_RELOC_ARM_SBREL32:
24695     case BFD_RELOC_ARM_PREL31:
24696     case BFD_RELOC_ARM_TARGET2:
24697     case BFD_RELOC_ARM_TLS_LDO32:
24698     case BFD_RELOC_ARM_PCREL_CALL:
24699     case BFD_RELOC_ARM_PCREL_JUMP:
24700     case BFD_RELOC_ARM_ALU_PC_G0_NC:
24701     case BFD_RELOC_ARM_ALU_PC_G0:
24702     case BFD_RELOC_ARM_ALU_PC_G1_NC:
24703     case BFD_RELOC_ARM_ALU_PC_G1:
24704     case BFD_RELOC_ARM_ALU_PC_G2:
24705     case BFD_RELOC_ARM_LDR_PC_G0:
24706     case BFD_RELOC_ARM_LDR_PC_G1:
24707     case BFD_RELOC_ARM_LDR_PC_G2:
24708     case BFD_RELOC_ARM_LDRS_PC_G0:
24709     case BFD_RELOC_ARM_LDRS_PC_G1:
24710     case BFD_RELOC_ARM_LDRS_PC_G2:
24711     case BFD_RELOC_ARM_LDC_PC_G0:
24712     case BFD_RELOC_ARM_LDC_PC_G1:
24713     case BFD_RELOC_ARM_LDC_PC_G2:
24714     case BFD_RELOC_ARM_ALU_SB_G0_NC:
24715     case BFD_RELOC_ARM_ALU_SB_G0:
24716     case BFD_RELOC_ARM_ALU_SB_G1_NC:
24717     case BFD_RELOC_ARM_ALU_SB_G1:
24718     case BFD_RELOC_ARM_ALU_SB_G2:
24719     case BFD_RELOC_ARM_LDR_SB_G0:
24720     case BFD_RELOC_ARM_LDR_SB_G1:
24721     case BFD_RELOC_ARM_LDR_SB_G2:
24722     case BFD_RELOC_ARM_LDRS_SB_G0:
24723     case BFD_RELOC_ARM_LDRS_SB_G1:
24724     case BFD_RELOC_ARM_LDRS_SB_G2:
24725     case BFD_RELOC_ARM_LDC_SB_G0:
24726     case BFD_RELOC_ARM_LDC_SB_G1:
24727     case BFD_RELOC_ARM_LDC_SB_G2:
24728     case BFD_RELOC_ARM_V4BX:
24729     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24730     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24731     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24732     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24733       code = fixp->fx_r_type;
24734       break;
24735
24736     case BFD_RELOC_ARM_TLS_GOTDESC:
24737     case BFD_RELOC_ARM_TLS_GD32:
24738     case BFD_RELOC_ARM_TLS_LE32:
24739     case BFD_RELOC_ARM_TLS_IE32:
24740     case BFD_RELOC_ARM_TLS_LDM32:
24741       /* BFD will include the symbol's address in the addend.
24742          But we don't want that, so subtract it out again here.  */
24743       if (!S_IS_COMMON (fixp->fx_addsy))
24744         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
24745       code = fixp->fx_r_type;
24746       break;
24747 #endif
24748
24749     case BFD_RELOC_ARM_IMMEDIATE:
24750       as_bad_where (fixp->fx_file, fixp->fx_line,
24751                     _("internal relocation (type: IMMEDIATE) not fixed up"));
24752       return NULL;
24753
24754     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
24755       as_bad_where (fixp->fx_file, fixp->fx_line,
24756                     _("ADRL used for a symbol not defined in the same file"));
24757       return NULL;
24758
24759     case BFD_RELOC_ARM_OFFSET_IMM:
24760       if (section->use_rela_p)
24761         {
24762           code = fixp->fx_r_type;
24763           break;
24764         }
24765
24766       if (fixp->fx_addsy != NULL
24767           && !S_IS_DEFINED (fixp->fx_addsy)
24768           && S_IS_LOCAL (fixp->fx_addsy))
24769         {
24770           as_bad_where (fixp->fx_file, fixp->fx_line,
24771                         _("undefined local label `%s'"),
24772                         S_GET_NAME (fixp->fx_addsy));
24773           return NULL;
24774         }
24775
24776       as_bad_where (fixp->fx_file, fixp->fx_line,
24777                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
24778       return NULL;
24779
24780     default:
24781       {
24782         const char * type;
24783
24784         switch (fixp->fx_r_type)
24785           {
24786           case BFD_RELOC_NONE:             type = "NONE";         break;
24787           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
24788           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
24789           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
24790           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
24791           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
24792           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
24793           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
24794           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
24795           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
24796           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
24797           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
24798           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
24799           default:                         type = _("<unknown>"); break;
24800           }
24801         as_bad_where (fixp->fx_file, fixp->fx_line,
24802                       _("cannot represent %s relocation in this object file format"),
24803                       type);
24804         return NULL;
24805       }
24806     }
24807
24808 #ifdef OBJ_ELF
24809   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
24810       && GOT_symbol
24811       && fixp->fx_addsy == GOT_symbol)
24812     {
24813       code = BFD_RELOC_ARM_GOTPC;
24814       reloc->addend = fixp->fx_offset = reloc->address;
24815     }
24816 #endif
24817
24818   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
24819
24820   if (reloc->howto == NULL)
24821     {
24822       as_bad_where (fixp->fx_file, fixp->fx_line,
24823                     _("cannot represent %s relocation in this object file format"),
24824                     bfd_get_reloc_code_name (code));
24825       return NULL;
24826     }
24827
24828   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
24829      vtable entry to be used in the relocation's section offset.  */
24830   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24831     reloc->address = fixp->fx_offset;
24832
24833   return reloc;
24834 }
24835
24836 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
24837
24838 void
24839 cons_fix_new_arm (fragS *       frag,
24840                   int           where,
24841                   int           size,
24842                   expressionS * exp,
24843                   bfd_reloc_code_real_type reloc)
24844 {
24845   int pcrel = 0;
24846
24847   /* Pick a reloc.
24848      FIXME: @@ Should look at CPU word size.  */
24849   switch (size)
24850     {
24851     case 1:
24852       reloc = BFD_RELOC_8;
24853       break;
24854     case 2:
24855       reloc = BFD_RELOC_16;
24856       break;
24857     case 4:
24858     default:
24859       reloc = BFD_RELOC_32;
24860       break;
24861     case 8:
24862       reloc = BFD_RELOC_64;
24863       break;
24864     }
24865
24866 #ifdef TE_PE
24867   if (exp->X_op == O_secrel)
24868   {
24869     exp->X_op = O_symbol;
24870     reloc = BFD_RELOC_32_SECREL;
24871   }
24872 #endif
24873
24874   fix_new_exp (frag, where, size, exp, pcrel, reloc);
24875 }
24876
24877 #if defined (OBJ_COFF)
24878 void
24879 arm_validate_fix (fixS * fixP)
24880 {
24881   /* If the destination of the branch is a defined symbol which does not have
24882      the THUMB_FUNC attribute, then we must be calling a function which has
24883      the (interfacearm) attribute.  We look for the Thumb entry point to that
24884      function and change the branch to refer to that function instead.  */
24885   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
24886       && fixP->fx_addsy != NULL
24887       && S_IS_DEFINED (fixP->fx_addsy)
24888       && ! THUMB_IS_FUNC (fixP->fx_addsy))
24889     {
24890       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
24891     }
24892 }
24893 #endif
24894
24895
24896 int
24897 arm_force_relocation (struct fix * fixp)
24898 {
24899 #if defined (OBJ_COFF) && defined (TE_PE)
24900   if (fixp->fx_r_type == BFD_RELOC_RVA)
24901     return 1;
24902 #endif
24903
24904   /* In case we have a call or a branch to a function in ARM ISA mode from
24905      a thumb function or vice-versa force the relocation. These relocations
24906      are cleared off for some cores that might have blx and simple transformations
24907      are possible.  */
24908
24909 #ifdef OBJ_ELF
24910   switch (fixp->fx_r_type)
24911     {
24912     case BFD_RELOC_ARM_PCREL_JUMP:
24913     case BFD_RELOC_ARM_PCREL_CALL:
24914     case BFD_RELOC_THUMB_PCREL_BLX:
24915       if (THUMB_IS_FUNC (fixp->fx_addsy))
24916         return 1;
24917       break;
24918
24919     case BFD_RELOC_ARM_PCREL_BLX:
24920     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24921     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24922     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24923       if (ARM_IS_FUNC (fixp->fx_addsy))
24924         return 1;
24925       break;
24926
24927     default:
24928       break;
24929     }
24930 #endif
24931
24932   /* Resolve these relocations even if the symbol is extern or weak.
24933      Technically this is probably wrong due to symbol preemption.
24934      In practice these relocations do not have enough range to be useful
24935      at dynamic link time, and some code (e.g. in the Linux kernel)
24936      expects these references to be resolved.  */
24937   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
24938       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
24939       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
24940       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
24941       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24942       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
24943       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
24944       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
24945       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
24946       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
24947       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
24948       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
24949       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
24950       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
24951     return 0;
24952
24953   /* Always leave these relocations for the linker.  */
24954   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
24955        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
24956       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
24957     return 1;
24958
24959   /* Always generate relocations against function symbols.  */
24960   if (fixp->fx_r_type == BFD_RELOC_32
24961       && fixp->fx_addsy
24962       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
24963     return 1;
24964
24965   return generic_force_reloc (fixp);
24966 }
24967
24968 #if defined (OBJ_ELF) || defined (OBJ_COFF)
24969 /* Relocations against function names must be left unadjusted,
24970    so that the linker can use this information to generate interworking
24971    stubs.  The MIPS version of this function
24972    also prevents relocations that are mips-16 specific, but I do not
24973    know why it does this.
24974
24975    FIXME:
24976    There is one other problem that ought to be addressed here, but
24977    which currently is not:  Taking the address of a label (rather
24978    than a function) and then later jumping to that address.  Such
24979    addresses also ought to have their bottom bit set (assuming that
24980    they reside in Thumb code), but at the moment they will not.  */
24981
24982 bfd_boolean
24983 arm_fix_adjustable (fixS * fixP)
24984 {
24985   if (fixP->fx_addsy == NULL)
24986     return 1;
24987
24988   /* Preserve relocations against symbols with function type.  */
24989   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
24990     return FALSE;
24991
24992   if (THUMB_IS_FUNC (fixP->fx_addsy)
24993       && fixP->fx_subsy == NULL)
24994     return FALSE;
24995
24996   /* We need the symbol name for the VTABLE entries.  */
24997   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
24998       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24999     return FALSE;
25000
25001   /* Don't allow symbols to be discarded on GOT related relocs.  */
25002   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
25003       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
25004       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
25005       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
25006       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
25007       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
25008       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
25009       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
25010       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
25011       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
25012       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
25013       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
25014       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
25015       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
25016     return FALSE;
25017
25018   /* Similarly for group relocations.  */
25019   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
25020        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
25021       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
25022     return FALSE;
25023
25024   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
25025   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
25026       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
25027       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
25028       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
25029       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
25030       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
25031       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
25032       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
25033     return FALSE;
25034
25035   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
25036      offsets, so keep these symbols.  */
25037   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
25038       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
25039     return FALSE;
25040
25041   return TRUE;
25042 }
25043 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
25044
25045 #ifdef OBJ_ELF
25046 const char *
25047 elf32_arm_target_format (void)
25048 {
25049 #ifdef TE_SYMBIAN
25050   return (target_big_endian
25051           ? "elf32-bigarm-symbian"
25052           : "elf32-littlearm-symbian");
25053 #elif defined (TE_VXWORKS)
25054   return (target_big_endian
25055           ? "elf32-bigarm-vxworks"
25056           : "elf32-littlearm-vxworks");
25057 #elif defined (TE_NACL)
25058   return (target_big_endian
25059           ? "elf32-bigarm-nacl"
25060           : "elf32-littlearm-nacl");
25061 #else
25062   if (target_big_endian)
25063     return "elf32-bigarm";
25064   else
25065     return "elf32-littlearm";
25066 #endif
25067 }
25068
25069 void
25070 armelf_frob_symbol (symbolS * symp,
25071                     int *     puntp)
25072 {
25073   elf_frob_symbol (symp, puntp);
25074 }
25075 #endif
25076
25077 /* MD interface: Finalization.  */
25078
25079 void
25080 arm_cleanup (void)
25081 {
25082   literal_pool * pool;
25083
25084   /* Ensure that all the IT blocks are properly closed.  */
25085   check_it_blocks_finished ();
25086
25087   for (pool = list_of_pools; pool; pool = pool->next)
25088     {
25089       /* Put it at the end of the relevant section.  */
25090       subseg_set (pool->section, pool->sub_section);
25091 #ifdef OBJ_ELF
25092       arm_elf_change_section ();
25093 #endif
25094       s_ltorg (0);
25095     }
25096 }
25097
25098 #ifdef OBJ_ELF
25099 /* Remove any excess mapping symbols generated for alignment frags in
25100    SEC.  We may have created a mapping symbol before a zero byte
25101    alignment; remove it if there's a mapping symbol after the
25102    alignment.  */
25103 static void
25104 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
25105                        void *dummy ATTRIBUTE_UNUSED)
25106 {
25107   segment_info_type *seginfo = seg_info (sec);
25108   fragS *fragp;
25109
25110   if (seginfo == NULL || seginfo->frchainP == NULL)
25111     return;
25112
25113   for (fragp = seginfo->frchainP->frch_root;
25114        fragp != NULL;
25115        fragp = fragp->fr_next)
25116     {
25117       symbolS *sym = fragp->tc_frag_data.last_map;
25118       fragS *next = fragp->fr_next;
25119
25120       /* Variable-sized frags have been converted to fixed size by
25121          this point.  But if this was variable-sized to start with,
25122          there will be a fixed-size frag after it.  So don't handle
25123          next == NULL.  */
25124       if (sym == NULL || next == NULL)
25125         continue;
25126
25127       if (S_GET_VALUE (sym) < next->fr_address)
25128         /* Not at the end of this frag.  */
25129         continue;
25130       know (S_GET_VALUE (sym) == next->fr_address);
25131
25132       do
25133         {
25134           if (next->tc_frag_data.first_map != NULL)
25135             {
25136               /* Next frag starts with a mapping symbol.  Discard this
25137                  one.  */
25138               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
25139               break;
25140             }
25141
25142           if (next->fr_next == NULL)
25143             {
25144               /* This mapping symbol is at the end of the section.  Discard
25145                  it.  */
25146               know (next->fr_fix == 0 && next->fr_var == 0);
25147               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
25148               break;
25149             }
25150
25151           /* As long as we have empty frags without any mapping symbols,
25152              keep looking.  */
25153           /* If the next frag is non-empty and does not start with a
25154              mapping symbol, then this mapping symbol is required.  */
25155           if (next->fr_address != next->fr_next->fr_address)
25156             break;
25157
25158           next = next->fr_next;
25159         }
25160       while (next != NULL);
25161     }
25162 }
25163 #endif
25164
25165 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
25166    ARM ones.  */
25167
25168 void
25169 arm_adjust_symtab (void)
25170 {
25171 #ifdef OBJ_COFF
25172   symbolS * sym;
25173
25174   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
25175     {
25176       if (ARM_IS_THUMB (sym))
25177         {
25178           if (THUMB_IS_FUNC (sym))
25179             {
25180               /* Mark the symbol as a Thumb function.  */
25181               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
25182                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
25183                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
25184
25185               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
25186                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
25187               else
25188                 as_bad (_("%s: unexpected function type: %d"),
25189                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
25190             }
25191           else switch (S_GET_STORAGE_CLASS (sym))
25192             {
25193             case C_EXT:
25194               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
25195               break;
25196             case C_STAT:
25197               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
25198               break;
25199             case C_LABEL:
25200               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
25201               break;
25202             default:
25203               /* Do nothing.  */
25204               break;
25205             }
25206         }
25207
25208       if (ARM_IS_INTERWORK (sym))
25209         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
25210     }
25211 #endif
25212 #ifdef OBJ_ELF
25213   symbolS * sym;
25214   char      bind;
25215
25216   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
25217     {
25218       if (ARM_IS_THUMB (sym))
25219         {
25220           elf_symbol_type * elf_sym;
25221
25222           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
25223           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
25224
25225           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
25226                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
25227             {
25228               /* If it's a .thumb_func, declare it as so,
25229                  otherwise tag label as .code 16.  */
25230               if (THUMB_IS_FUNC (sym))
25231                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
25232                                          ST_BRANCH_TO_THUMB);
25233               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
25234                 elf_sym->internal_elf_sym.st_info =
25235                   ELF_ST_INFO (bind, STT_ARM_16BIT);
25236             }
25237         }
25238     }
25239
25240   /* Remove any overlapping mapping symbols generated by alignment frags.  */
25241   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
25242   /* Now do generic ELF adjustments.  */
25243   elf_adjust_symtab ();
25244 #endif
25245 }
25246
25247 /* MD interface: Initialization.  */
25248
25249 static void
25250 set_constant_flonums (void)
25251 {
25252   int i;
25253
25254   for (i = 0; i < NUM_FLOAT_VALS; i++)
25255     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
25256       abort ();
25257 }
25258
25259 /* Auto-select Thumb mode if it's the only available instruction set for the
25260    given architecture.  */
25261
25262 static void
25263 autoselect_thumb_from_cpu_variant (void)
25264 {
25265   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
25266     opcode_select (16);
25267 }
25268
25269 void
25270 md_begin (void)
25271 {
25272   unsigned mach;
25273   unsigned int i;
25274
25275   if (   (arm_ops_hsh = hash_new ()) == NULL
25276       || (arm_cond_hsh = hash_new ()) == NULL
25277       || (arm_shift_hsh = hash_new ()) == NULL
25278       || (arm_psr_hsh = hash_new ()) == NULL
25279       || (arm_v7m_psr_hsh = hash_new ()) == NULL
25280       || (arm_reg_hsh = hash_new ()) == NULL
25281       || (arm_reloc_hsh = hash_new ()) == NULL
25282       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
25283     as_fatal (_("virtual memory exhausted"));
25284
25285   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
25286     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
25287   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
25288     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
25289   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
25290     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
25291   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
25292     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
25293   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
25294     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
25295                  (void *) (v7m_psrs + i));
25296   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
25297     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
25298   for (i = 0;
25299        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
25300        i++)
25301     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
25302                  (void *) (barrier_opt_names + i));
25303 #ifdef OBJ_ELF
25304   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
25305     {
25306       struct reloc_entry * entry = reloc_names + i;
25307
25308       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
25309         /* This makes encode_branch() use the EABI versions of this relocation.  */
25310         entry->reloc = BFD_RELOC_UNUSED;
25311
25312       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
25313     }
25314 #endif
25315
25316   set_constant_flonums ();
25317
25318   /* Set the cpu variant based on the command-line options.  We prefer
25319      -mcpu= over -march= if both are set (as for GCC); and we prefer
25320      -mfpu= over any other way of setting the floating point unit.
25321      Use of legacy options with new options are faulted.  */
25322   if (legacy_cpu)
25323     {
25324       if (mcpu_cpu_opt || march_cpu_opt)
25325         as_bad (_("use of old and new-style options to set CPU type"));
25326
25327       mcpu_cpu_opt = legacy_cpu;
25328     }
25329   else if (!mcpu_cpu_opt)
25330     {
25331       mcpu_cpu_opt = march_cpu_opt;
25332       dyn_mcpu_ext_opt = dyn_march_ext_opt;
25333       /* Avoid double free in arm_md_end.  */
25334       dyn_march_ext_opt = NULL;
25335     }
25336
25337   if (legacy_fpu)
25338     {
25339       if (mfpu_opt)
25340         as_bad (_("use of old and new-style options to set FPU type"));
25341
25342       mfpu_opt = legacy_fpu;
25343     }
25344   else if (!mfpu_opt)
25345     {
25346 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
25347         || defined (TE_NetBSD) || defined (TE_VXWORKS))
25348       /* Some environments specify a default FPU.  If they don't, infer it
25349          from the processor.  */
25350       if (mcpu_fpu_opt)
25351         mfpu_opt = mcpu_fpu_opt;
25352       else
25353         mfpu_opt = march_fpu_opt;
25354 #else
25355       mfpu_opt = &fpu_default;
25356 #endif
25357     }
25358
25359   if (!mfpu_opt)
25360     {
25361       if (mcpu_cpu_opt != NULL)
25362         mfpu_opt = &fpu_default;
25363       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
25364         mfpu_opt = &fpu_arch_vfp_v2;
25365       else
25366         mfpu_opt = &fpu_arch_fpa;
25367     }
25368
25369 #ifdef CPU_DEFAULT
25370   if (!mcpu_cpu_opt)
25371     {
25372       mcpu_cpu_opt = &cpu_default;
25373       selected_cpu = cpu_default;
25374     }
25375   else if (dyn_mcpu_ext_opt)
25376     ARM_MERGE_FEATURE_SETS (selected_cpu, *mcpu_cpu_opt, *dyn_mcpu_ext_opt);
25377   else
25378     selected_cpu = *mcpu_cpu_opt;
25379 #else
25380   if (mcpu_cpu_opt && dyn_mcpu_ext_opt)
25381     ARM_MERGE_FEATURE_SETS (selected_cpu, *mcpu_cpu_opt, *dyn_mcpu_ext_opt);
25382   else if (mcpu_cpu_opt)
25383     selected_cpu = *mcpu_cpu_opt;
25384   else
25385     mcpu_cpu_opt = &arm_arch_any;
25386 #endif
25387
25388   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25389   if (dyn_mcpu_ext_opt)
25390     ARM_MERGE_FEATURE_SETS (cpu_variant, cpu_variant, *dyn_mcpu_ext_opt);
25391
25392   autoselect_thumb_from_cpu_variant ();
25393
25394   arm_arch_used = thumb_arch_used = arm_arch_none;
25395
25396 #if defined OBJ_COFF || defined OBJ_ELF
25397   {
25398     unsigned int flags = 0;
25399
25400 #if defined OBJ_ELF
25401     flags = meabi_flags;
25402
25403     switch (meabi_flags)
25404       {
25405       case EF_ARM_EABI_UNKNOWN:
25406 #endif
25407         /* Set the flags in the private structure.  */
25408         if (uses_apcs_26)      flags |= F_APCS26;
25409         if (support_interwork) flags |= F_INTERWORK;
25410         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
25411         if (pic_code)          flags |= F_PIC;
25412         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
25413           flags |= F_SOFT_FLOAT;
25414
25415         switch (mfloat_abi_opt)
25416           {
25417           case ARM_FLOAT_ABI_SOFT:
25418           case ARM_FLOAT_ABI_SOFTFP:
25419             flags |= F_SOFT_FLOAT;
25420             break;
25421
25422           case ARM_FLOAT_ABI_HARD:
25423             if (flags & F_SOFT_FLOAT)
25424               as_bad (_("hard-float conflicts with specified fpu"));
25425             break;
25426           }
25427
25428         /* Using pure-endian doubles (even if soft-float).      */
25429         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
25430           flags |= F_VFP_FLOAT;
25431
25432 #if defined OBJ_ELF
25433         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
25434             flags |= EF_ARM_MAVERICK_FLOAT;
25435         break;
25436
25437       case EF_ARM_EABI_VER4:
25438       case EF_ARM_EABI_VER5:
25439         /* No additional flags to set.  */
25440         break;
25441
25442       default:
25443         abort ();
25444       }
25445 #endif
25446     bfd_set_private_flags (stdoutput, flags);
25447
25448     /* We have run out flags in the COFF header to encode the
25449        status of ATPCS support, so instead we create a dummy,
25450        empty, debug section called .arm.atpcs.  */
25451     if (atpcs)
25452       {
25453         asection * sec;
25454
25455         sec = bfd_make_section (stdoutput, ".arm.atpcs");
25456
25457         if (sec != NULL)
25458           {
25459             bfd_set_section_flags
25460               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
25461             bfd_set_section_size (stdoutput, sec, 0);
25462             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
25463           }
25464       }
25465   }
25466 #endif
25467
25468   /* Record the CPU type as well.  */
25469   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
25470     mach = bfd_mach_arm_iWMMXt2;
25471   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
25472     mach = bfd_mach_arm_iWMMXt;
25473   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
25474     mach = bfd_mach_arm_XScale;
25475   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
25476     mach = bfd_mach_arm_ep9312;
25477   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
25478     mach = bfd_mach_arm_5TE;
25479   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
25480     {
25481       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25482         mach = bfd_mach_arm_5T;
25483       else
25484         mach = bfd_mach_arm_5;
25485     }
25486   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
25487     {
25488       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25489         mach = bfd_mach_arm_4T;
25490       else
25491         mach = bfd_mach_arm_4;
25492     }
25493   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
25494     mach = bfd_mach_arm_3M;
25495   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
25496     mach = bfd_mach_arm_3;
25497   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
25498     mach = bfd_mach_arm_2a;
25499   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
25500     mach = bfd_mach_arm_2;
25501   else
25502     mach = bfd_mach_arm_unknown;
25503
25504   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
25505 }
25506
25507 /* Command line processing.  */
25508
25509 /* md_parse_option
25510       Invocation line includes a switch not recognized by the base assembler.
25511       See if it's a processor-specific option.
25512
25513       This routine is somewhat complicated by the need for backwards
25514       compatibility (since older releases of gcc can't be changed).
25515       The new options try to make the interface as compatible as
25516       possible with GCC.
25517
25518       New options (supported) are:
25519
25520               -mcpu=<cpu name>           Assemble for selected processor
25521               -march=<architecture name> Assemble for selected architecture
25522               -mfpu=<fpu architecture>   Assemble for selected FPU.
25523               -EB/-mbig-endian           Big-endian
25524               -EL/-mlittle-endian        Little-endian
25525               -k                         Generate PIC code
25526               -mthumb                    Start in Thumb mode
25527               -mthumb-interwork          Code supports ARM/Thumb interworking
25528
25529               -m[no-]warn-deprecated     Warn about deprecated features
25530               -m[no-]warn-syms           Warn when symbols match instructions
25531
25532       For now we will also provide support for:
25533
25534               -mapcs-32                  32-bit Program counter
25535               -mapcs-26                  26-bit Program counter
25536               -macps-float               Floats passed in FP registers
25537               -mapcs-reentrant           Reentrant code
25538               -matpcs
25539       (sometime these will probably be replaced with -mapcs=<list of options>
25540       and -matpcs=<list of options>)
25541
25542       The remaining options are only supported for back-wards compatibility.
25543       Cpu variants, the arm part is optional:
25544               -m[arm]1                Currently not supported.
25545               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
25546               -m[arm]3                Arm 3 processor
25547               -m[arm]6[xx],           Arm 6 processors
25548               -m[arm]7[xx][t][[d]m]   Arm 7 processors
25549               -m[arm]8[10]            Arm 8 processors
25550               -m[arm]9[20][tdmi]      Arm 9 processors
25551               -mstrongarm[110[0]]     StrongARM processors
25552               -mxscale                XScale processors
25553               -m[arm]v[2345[t[e]]]    Arm architectures
25554               -mall                   All (except the ARM1)
25555       FP variants:
25556               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
25557               -mfpe-old               (No float load/store multiples)
25558               -mvfpxd                 VFP Single precision
25559               -mvfp                   All VFP
25560               -mno-fpu                Disable all floating point instructions
25561
25562       The following CPU names are recognized:
25563               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
25564               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
25565               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
25566               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
25567               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
25568               arm10t arm10e, arm1020t, arm1020e, arm10200e,
25569               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
25570
25571       */
25572
25573 const char * md_shortopts = "m:k";
25574
25575 #ifdef ARM_BI_ENDIAN
25576 #define OPTION_EB (OPTION_MD_BASE + 0)
25577 #define OPTION_EL (OPTION_MD_BASE + 1)
25578 #else
25579 #if TARGET_BYTES_BIG_ENDIAN
25580 #define OPTION_EB (OPTION_MD_BASE + 0)
25581 #else
25582 #define OPTION_EL (OPTION_MD_BASE + 1)
25583 #endif
25584 #endif
25585 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
25586
25587 struct option md_longopts[] =
25588 {
25589 #ifdef OPTION_EB
25590   {"EB", no_argument, NULL, OPTION_EB},
25591 #endif
25592 #ifdef OPTION_EL
25593   {"EL", no_argument, NULL, OPTION_EL},
25594 #endif
25595   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
25596   {NULL, no_argument, NULL, 0}
25597 };
25598
25599 size_t md_longopts_size = sizeof (md_longopts);
25600
25601 struct arm_option_table
25602 {
25603   const char *  option;         /* Option name to match.  */
25604   const char *  help;           /* Help information.  */
25605   int *         var;            /* Variable to change.  */
25606   int           value;          /* What to change it to.  */
25607   const char *  deprecated;     /* If non-null, print this message.  */
25608 };
25609
25610 struct arm_option_table arm_opts[] =
25611 {
25612   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
25613   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
25614   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
25615    &support_interwork, 1, NULL},
25616   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
25617   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
25618   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
25619    1, NULL},
25620   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
25621   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
25622   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
25623   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
25624    NULL},
25625
25626   /* These are recognized by the assembler, but have no affect on code.  */
25627   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
25628   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
25629
25630   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
25631   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
25632    &warn_on_deprecated, 0, NULL},
25633   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
25634   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
25635   {NULL, NULL, NULL, 0, NULL}
25636 };
25637
25638 struct arm_legacy_option_table
25639 {
25640   const char *              option;             /* Option name to match.  */
25641   const arm_feature_set **  var;                /* Variable to change.  */
25642   const arm_feature_set     value;              /* What to change it to.  */
25643   const char *              deprecated;         /* If non-null, print this message.  */
25644 };
25645
25646 const struct arm_legacy_option_table arm_legacy_opts[] =
25647 {
25648   /* DON'T add any new processors to this list -- we want the whole list
25649      to go away...  Add them to the processors table instead.  */
25650   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25651   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25652   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25653   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25654   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25655   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25656   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25657   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25658   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25659   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25660   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25661   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25662   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25663   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25664   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25665   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25666   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25667   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25668   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25669   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25670   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25671   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25672   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25673   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25674   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25675   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25676   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25677   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25678   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25679   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25680   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25681   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25682   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25683   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25684   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25685   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25686   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25687   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25688   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25689   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25690   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25691   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25692   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25693   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25694   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25695   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25696   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25697   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25698   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25699   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25700   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25701   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25702   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25703   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25704   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25705   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25706   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25707   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25708   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25709   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25710   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25711   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25712   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25713   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25714   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25715   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25716   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25717   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25718   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
25719   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
25720    N_("use -mcpu=strongarm110")},
25721   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
25722    N_("use -mcpu=strongarm1100")},
25723   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
25724    N_("use -mcpu=strongarm1110")},
25725   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
25726   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
25727   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
25728
25729   /* Architecture variants -- don't add any more to this list either.  */
25730   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25731   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25732   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25733   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25734   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25735   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25736   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25737   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25738   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25739   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25740   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25741   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25742   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25743   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25744   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25745   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25746   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25747   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25748
25749   /* Floating point variants -- don't add any more to this list either.  */
25750   {"mfpe-old",   &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
25751   {"mfpa10",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
25752   {"mfpa11",     &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
25753   {"mno-fpu",    &legacy_fpu, ARM_ARCH_NONE,
25754    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
25755
25756   {NULL, NULL, ARM_ARCH_NONE, NULL}
25757 };
25758
25759 struct arm_cpu_option_table
25760 {
25761   const char *           name;
25762   size_t                 name_len;
25763   const arm_feature_set  value;
25764   const arm_feature_set  ext;
25765   /* For some CPUs we assume an FPU unless the user explicitly sets
25766      -mfpu=...  */
25767   const arm_feature_set  default_fpu;
25768   /* The canonical name of the CPU, or NULL to use NAME converted to upper
25769      case.  */
25770   const char *           canonical_name;
25771 };
25772
25773 /* This list should, at a minimum, contain all the cpu names
25774    recognized by GCC.  */
25775 #define ARM_CPU_OPT(N, CN, V, E, DF) { N, sizeof (N) - 1, V, E, DF, CN }
25776
25777 static const struct arm_cpu_option_table arm_cpus[] =
25778 {
25779   ARM_CPU_OPT ("all",             NULL,                ARM_ANY,
25780                ARM_ARCH_NONE,
25781                FPU_ARCH_FPA),
25782   ARM_CPU_OPT ("arm1",            NULL,                ARM_ARCH_V1,
25783                ARM_ARCH_NONE,
25784                FPU_ARCH_FPA),
25785   ARM_CPU_OPT ("arm2",            NULL,                ARM_ARCH_V2,
25786                ARM_ARCH_NONE,
25787                FPU_ARCH_FPA),
25788   ARM_CPU_OPT ("arm250",          NULL,                ARM_ARCH_V2S,
25789                ARM_ARCH_NONE,
25790                FPU_ARCH_FPA),
25791   ARM_CPU_OPT ("arm3",            NULL,                ARM_ARCH_V2S,
25792                ARM_ARCH_NONE,
25793                FPU_ARCH_FPA),
25794   ARM_CPU_OPT ("arm6",            NULL,                ARM_ARCH_V3,
25795                ARM_ARCH_NONE,
25796                FPU_ARCH_FPA),
25797   ARM_CPU_OPT ("arm60",           NULL,                ARM_ARCH_V3,
25798                ARM_ARCH_NONE,
25799                FPU_ARCH_FPA),
25800   ARM_CPU_OPT ("arm600",          NULL,                ARM_ARCH_V3,
25801                ARM_ARCH_NONE,
25802                FPU_ARCH_FPA),
25803   ARM_CPU_OPT ("arm610",          NULL,                ARM_ARCH_V3,
25804                ARM_ARCH_NONE,
25805                FPU_ARCH_FPA),
25806   ARM_CPU_OPT ("arm620",          NULL,                ARM_ARCH_V3,
25807                ARM_ARCH_NONE,
25808                FPU_ARCH_FPA),
25809   ARM_CPU_OPT ("arm7",            NULL,                ARM_ARCH_V3,
25810                ARM_ARCH_NONE,
25811                FPU_ARCH_FPA),
25812   ARM_CPU_OPT ("arm7m",           NULL,                ARM_ARCH_V3M,
25813                ARM_ARCH_NONE,
25814                FPU_ARCH_FPA),
25815   ARM_CPU_OPT ("arm7d",           NULL,                ARM_ARCH_V3,
25816                ARM_ARCH_NONE,
25817                FPU_ARCH_FPA),
25818   ARM_CPU_OPT ("arm7dm",          NULL,                ARM_ARCH_V3M,
25819                ARM_ARCH_NONE,
25820                FPU_ARCH_FPA),
25821   ARM_CPU_OPT ("arm7di",          NULL,                ARM_ARCH_V3,
25822                ARM_ARCH_NONE,
25823                FPU_ARCH_FPA),
25824   ARM_CPU_OPT ("arm7dmi",         NULL,                ARM_ARCH_V3M,
25825                ARM_ARCH_NONE,
25826                FPU_ARCH_FPA),
25827   ARM_CPU_OPT ("arm70",           NULL,                ARM_ARCH_V3,
25828                ARM_ARCH_NONE,
25829                FPU_ARCH_FPA),
25830   ARM_CPU_OPT ("arm700",          NULL,                ARM_ARCH_V3,
25831                ARM_ARCH_NONE,
25832                FPU_ARCH_FPA),
25833   ARM_CPU_OPT ("arm700i",         NULL,                ARM_ARCH_V3,
25834                ARM_ARCH_NONE,
25835                FPU_ARCH_FPA),
25836   ARM_CPU_OPT ("arm710",          NULL,                ARM_ARCH_V3,
25837                ARM_ARCH_NONE,
25838                FPU_ARCH_FPA),
25839   ARM_CPU_OPT ("arm710t",         NULL,                ARM_ARCH_V4T,
25840                ARM_ARCH_NONE,
25841                FPU_ARCH_FPA),
25842   ARM_CPU_OPT ("arm720",          NULL,                ARM_ARCH_V3,
25843                ARM_ARCH_NONE,
25844                FPU_ARCH_FPA),
25845   ARM_CPU_OPT ("arm720t",         NULL,                ARM_ARCH_V4T,
25846                ARM_ARCH_NONE,
25847                FPU_ARCH_FPA),
25848   ARM_CPU_OPT ("arm740t",         NULL,                ARM_ARCH_V4T,
25849                ARM_ARCH_NONE,
25850                FPU_ARCH_FPA),
25851   ARM_CPU_OPT ("arm710c",         NULL,                ARM_ARCH_V3,
25852                ARM_ARCH_NONE,
25853                FPU_ARCH_FPA),
25854   ARM_CPU_OPT ("arm7100",         NULL,                ARM_ARCH_V3,
25855                ARM_ARCH_NONE,
25856                FPU_ARCH_FPA),
25857   ARM_CPU_OPT ("arm7500",         NULL,                ARM_ARCH_V3,
25858                ARM_ARCH_NONE,
25859                FPU_ARCH_FPA),
25860   ARM_CPU_OPT ("arm7500fe",       NULL,                ARM_ARCH_V3,
25861                ARM_ARCH_NONE,
25862                FPU_ARCH_FPA),
25863   ARM_CPU_OPT ("arm7t",           NULL,                ARM_ARCH_V4T,
25864                ARM_ARCH_NONE,
25865                FPU_ARCH_FPA),
25866   ARM_CPU_OPT ("arm7tdmi",        NULL,                ARM_ARCH_V4T,
25867                ARM_ARCH_NONE,
25868                FPU_ARCH_FPA),
25869   ARM_CPU_OPT ("arm7tdmi-s",      NULL,                ARM_ARCH_V4T,
25870                ARM_ARCH_NONE,
25871                FPU_ARCH_FPA),
25872   ARM_CPU_OPT ("arm8",            NULL,                ARM_ARCH_V4,
25873                ARM_ARCH_NONE,
25874                FPU_ARCH_FPA),
25875   ARM_CPU_OPT ("arm810",          NULL,                ARM_ARCH_V4,
25876                ARM_ARCH_NONE,
25877                FPU_ARCH_FPA),
25878   ARM_CPU_OPT ("strongarm",       NULL,                ARM_ARCH_V4,
25879                ARM_ARCH_NONE,
25880                FPU_ARCH_FPA),
25881   ARM_CPU_OPT ("strongarm1",      NULL,                ARM_ARCH_V4,
25882                ARM_ARCH_NONE,
25883                FPU_ARCH_FPA),
25884   ARM_CPU_OPT ("strongarm110",    NULL,                ARM_ARCH_V4,
25885                ARM_ARCH_NONE,
25886                FPU_ARCH_FPA),
25887   ARM_CPU_OPT ("strongarm1100",   NULL,                ARM_ARCH_V4,
25888                ARM_ARCH_NONE,
25889                FPU_ARCH_FPA),
25890   ARM_CPU_OPT ("strongarm1110",   NULL,                ARM_ARCH_V4,
25891                ARM_ARCH_NONE,
25892                FPU_ARCH_FPA),
25893   ARM_CPU_OPT ("arm9",            NULL,                ARM_ARCH_V4T,
25894                ARM_ARCH_NONE,
25895                FPU_ARCH_FPA),
25896   ARM_CPU_OPT ("arm920",          "ARM920T",           ARM_ARCH_V4T,
25897                ARM_ARCH_NONE,
25898                FPU_ARCH_FPA),
25899   ARM_CPU_OPT ("arm920t",         NULL,                ARM_ARCH_V4T,
25900                ARM_ARCH_NONE,
25901                FPU_ARCH_FPA),
25902   ARM_CPU_OPT ("arm922t",         NULL,                ARM_ARCH_V4T,
25903                ARM_ARCH_NONE,
25904                FPU_ARCH_FPA),
25905   ARM_CPU_OPT ("arm940t",         NULL,                ARM_ARCH_V4T,
25906                ARM_ARCH_NONE,
25907                FPU_ARCH_FPA),
25908   ARM_CPU_OPT ("arm9tdmi",        NULL,                ARM_ARCH_V4T,
25909                ARM_ARCH_NONE,
25910                FPU_ARCH_FPA),
25911   ARM_CPU_OPT ("fa526",           NULL,                ARM_ARCH_V4,
25912                ARM_ARCH_NONE,
25913                FPU_ARCH_FPA),
25914   ARM_CPU_OPT ("fa626",           NULL,                ARM_ARCH_V4,
25915                ARM_ARCH_NONE,
25916                FPU_ARCH_FPA),
25917
25918   /* For V5 or later processors we default to using VFP; but the user
25919      should really set the FPU type explicitly.  */
25920   ARM_CPU_OPT ("arm9e-r0",        NULL,                ARM_ARCH_V5TExP,
25921                ARM_ARCH_NONE,
25922                FPU_ARCH_VFP_V2),
25923   ARM_CPU_OPT ("arm9e",           NULL,                ARM_ARCH_V5TE,
25924                ARM_ARCH_NONE,
25925                FPU_ARCH_VFP_V2),
25926   ARM_CPU_OPT ("arm926ej",        "ARM926EJ-S",        ARM_ARCH_V5TEJ,
25927                ARM_ARCH_NONE,
25928                FPU_ARCH_VFP_V2),
25929   ARM_CPU_OPT ("arm926ejs",       "ARM926EJ-S",        ARM_ARCH_V5TEJ,
25930                ARM_ARCH_NONE,
25931                FPU_ARCH_VFP_V2),
25932   ARM_CPU_OPT ("arm926ej-s",      NULL,                ARM_ARCH_V5TEJ,
25933                ARM_ARCH_NONE,
25934                FPU_ARCH_VFP_V2),
25935   ARM_CPU_OPT ("arm946e-r0",      NULL,                ARM_ARCH_V5TExP,
25936                ARM_ARCH_NONE,
25937                FPU_ARCH_VFP_V2),
25938   ARM_CPU_OPT ("arm946e",         "ARM946E-S",         ARM_ARCH_V5TE,
25939                ARM_ARCH_NONE,
25940                FPU_ARCH_VFP_V2),
25941   ARM_CPU_OPT ("arm946e-s",       NULL,                ARM_ARCH_V5TE,
25942                ARM_ARCH_NONE,
25943                FPU_ARCH_VFP_V2),
25944   ARM_CPU_OPT ("arm966e-r0",      NULL,                ARM_ARCH_V5TExP,
25945                ARM_ARCH_NONE,
25946                FPU_ARCH_VFP_V2),
25947   ARM_CPU_OPT ("arm966e",         "ARM966E-S",         ARM_ARCH_V5TE,
25948                ARM_ARCH_NONE,
25949                FPU_ARCH_VFP_V2),
25950   ARM_CPU_OPT ("arm966e-s",       NULL,                ARM_ARCH_V5TE,
25951                ARM_ARCH_NONE,
25952                FPU_ARCH_VFP_V2),
25953   ARM_CPU_OPT ("arm968e-s",       NULL,                ARM_ARCH_V5TE,
25954                ARM_ARCH_NONE,
25955                FPU_ARCH_VFP_V2),
25956   ARM_CPU_OPT ("arm10t",          NULL,                ARM_ARCH_V5T,
25957                ARM_ARCH_NONE,
25958                FPU_ARCH_VFP_V1),
25959   ARM_CPU_OPT ("arm10tdmi",       NULL,                ARM_ARCH_V5T,
25960                ARM_ARCH_NONE,
25961                FPU_ARCH_VFP_V1),
25962   ARM_CPU_OPT ("arm10e",          NULL,                ARM_ARCH_V5TE,
25963                ARM_ARCH_NONE,
25964                FPU_ARCH_VFP_V2),
25965   ARM_CPU_OPT ("arm1020",         "ARM1020E",          ARM_ARCH_V5TE,
25966                ARM_ARCH_NONE,
25967                FPU_ARCH_VFP_V2),
25968   ARM_CPU_OPT ("arm1020t",        NULL,                ARM_ARCH_V5T,
25969                ARM_ARCH_NONE,
25970                FPU_ARCH_VFP_V1),
25971   ARM_CPU_OPT ("arm1020e",        NULL,                ARM_ARCH_V5TE,
25972                ARM_ARCH_NONE,
25973                FPU_ARCH_VFP_V2),
25974   ARM_CPU_OPT ("arm1022e",        NULL,                ARM_ARCH_V5TE,
25975                ARM_ARCH_NONE,
25976                FPU_ARCH_VFP_V2),
25977   ARM_CPU_OPT ("arm1026ejs",      "ARM1026EJ-S",       ARM_ARCH_V5TEJ,
25978                ARM_ARCH_NONE,
25979                FPU_ARCH_VFP_V2),
25980   ARM_CPU_OPT ("arm1026ej-s",     NULL,                ARM_ARCH_V5TEJ,
25981                ARM_ARCH_NONE,
25982                FPU_ARCH_VFP_V2),
25983   ARM_CPU_OPT ("fa606te",         NULL,                ARM_ARCH_V5TE,
25984                ARM_ARCH_NONE,
25985                FPU_ARCH_VFP_V2),
25986   ARM_CPU_OPT ("fa616te",         NULL,                ARM_ARCH_V5TE,
25987                ARM_ARCH_NONE,
25988                FPU_ARCH_VFP_V2),
25989   ARM_CPU_OPT ("fa626te",         NULL,                ARM_ARCH_V5TE,
25990                ARM_ARCH_NONE,
25991                FPU_ARCH_VFP_V2),
25992   ARM_CPU_OPT ("fmp626",          NULL,                ARM_ARCH_V5TE,
25993                ARM_ARCH_NONE,
25994                FPU_ARCH_VFP_V2),
25995   ARM_CPU_OPT ("fa726te",         NULL,                ARM_ARCH_V5TE,
25996                ARM_ARCH_NONE,
25997                FPU_ARCH_VFP_V2),
25998   ARM_CPU_OPT ("arm1136js",       "ARM1136J-S",        ARM_ARCH_V6,
25999                ARM_ARCH_NONE,
26000                FPU_NONE),
26001   ARM_CPU_OPT ("arm1136j-s",      NULL,                ARM_ARCH_V6,
26002                ARM_ARCH_NONE,
26003                FPU_NONE),
26004   ARM_CPU_OPT ("arm1136jfs",      "ARM1136JF-S",       ARM_ARCH_V6,
26005                ARM_ARCH_NONE,
26006                FPU_ARCH_VFP_V2),
26007   ARM_CPU_OPT ("arm1136jf-s",     NULL,                ARM_ARCH_V6,
26008                ARM_ARCH_NONE,
26009                FPU_ARCH_VFP_V2),
26010   ARM_CPU_OPT ("mpcore",          "MPCore",            ARM_ARCH_V6K,
26011                ARM_ARCH_NONE,
26012                FPU_ARCH_VFP_V2),
26013   ARM_CPU_OPT ("mpcorenovfp",     "MPCore",            ARM_ARCH_V6K,
26014                ARM_ARCH_NONE,
26015                FPU_NONE),
26016   ARM_CPU_OPT ("arm1156t2-s",     NULL,                ARM_ARCH_V6T2,
26017                ARM_ARCH_NONE,
26018                FPU_NONE),
26019   ARM_CPU_OPT ("arm1156t2f-s",    NULL,                ARM_ARCH_V6T2,
26020                ARM_ARCH_NONE,
26021                FPU_ARCH_VFP_V2),
26022   ARM_CPU_OPT ("arm1176jz-s",     NULL,                ARM_ARCH_V6KZ,
26023                ARM_ARCH_NONE,
26024                FPU_NONE),
26025   ARM_CPU_OPT ("arm1176jzf-s",    NULL,                ARM_ARCH_V6KZ,
26026                ARM_ARCH_NONE,
26027                FPU_ARCH_VFP_V2),
26028   ARM_CPU_OPT ("cortex-a5",       "Cortex-A5",         ARM_ARCH_V7A,
26029                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26030                FPU_NONE),
26031   ARM_CPU_OPT ("cortex-a7",       "Cortex-A7",         ARM_ARCH_V7VE,
26032                ARM_ARCH_NONE,
26033                FPU_ARCH_NEON_VFP_V4),
26034   ARM_CPU_OPT ("cortex-a8",       "Cortex-A8",         ARM_ARCH_V7A,
26035                ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26036                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
26037   ARM_CPU_OPT ("cortex-a9",       "Cortex-A9",         ARM_ARCH_V7A,
26038                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26039                ARM_FEATURE_COPROC (FPU_VFP_V3 | FPU_NEON_EXT_V1)),
26040   ARM_CPU_OPT ("cortex-a12",      "Cortex-A12",        ARM_ARCH_V7VE,
26041                ARM_ARCH_NONE,
26042                FPU_ARCH_NEON_VFP_V4),
26043   ARM_CPU_OPT ("cortex-a15",      "Cortex-A15",        ARM_ARCH_V7VE,
26044                ARM_ARCH_NONE,
26045                FPU_ARCH_NEON_VFP_V4),
26046   ARM_CPU_OPT ("cortex-a17",      "Cortex-A17",        ARM_ARCH_V7VE,
26047                ARM_ARCH_NONE,
26048                FPU_ARCH_NEON_VFP_V4),
26049   ARM_CPU_OPT ("cortex-a32",      "Cortex-A32",        ARM_ARCH_V8A,
26050                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26051                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26052   ARM_CPU_OPT ("cortex-a35",      "Cortex-A35",        ARM_ARCH_V8A,
26053                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26054                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26055   ARM_CPU_OPT ("cortex-a53",      "Cortex-A53",        ARM_ARCH_V8A,
26056                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26057                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26058   ARM_CPU_OPT ("cortex-a55",    "Cortex-A55",          ARM_ARCH_V8_2A,
26059                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26060                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26061   ARM_CPU_OPT ("cortex-a57",      "Cortex-A57",        ARM_ARCH_V8A,
26062                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26063                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26064   ARM_CPU_OPT ("cortex-a72",      "Cortex-A72",        ARM_ARCH_V8A,
26065               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26066               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26067   ARM_CPU_OPT ("cortex-a73",      "Cortex-A73",        ARM_ARCH_V8A,
26068               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26069               FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26070   ARM_CPU_OPT ("cortex-a75",    "Cortex-A75",          ARM_ARCH_V8_2A,
26071                ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26072                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_DOTPROD),
26073   ARM_CPU_OPT ("cortex-r4",       "Cortex-R4",         ARM_ARCH_V7R,
26074                ARM_ARCH_NONE,
26075                FPU_NONE),
26076   ARM_CPU_OPT ("cortex-r4f",      "Cortex-R4F",        ARM_ARCH_V7R,
26077                ARM_ARCH_NONE,
26078                FPU_ARCH_VFP_V3D16),
26079   ARM_CPU_OPT ("cortex-r5",       "Cortex-R5",         ARM_ARCH_V7R,
26080                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26081                FPU_NONE),
26082   ARM_CPU_OPT ("cortex-r7",       "Cortex-R7",         ARM_ARCH_V7R,
26083                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26084                FPU_ARCH_VFP_V3D16),
26085   ARM_CPU_OPT ("cortex-r8",       "Cortex-R8",         ARM_ARCH_V7R,
26086                ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV),
26087                FPU_ARCH_VFP_V3D16),
26088   ARM_CPU_OPT ("cortex-r52",      "Cortex-R52",        ARM_ARCH_V8R,
26089               ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26090               FPU_ARCH_NEON_VFP_ARMV8),
26091   ARM_CPU_OPT ("cortex-m33",      "Cortex-M33",        ARM_ARCH_V8M_MAIN,
26092                ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26093                FPU_NONE),
26094   ARM_CPU_OPT ("cortex-m23",      "Cortex-M23",        ARM_ARCH_V8M_BASE,
26095                ARM_ARCH_NONE,
26096                FPU_NONE),
26097   ARM_CPU_OPT ("cortex-m7",       "Cortex-M7",         ARM_ARCH_V7EM,
26098                ARM_ARCH_NONE,
26099                FPU_NONE),
26100   ARM_CPU_OPT ("cortex-m4",       "Cortex-M4",         ARM_ARCH_V7EM,
26101                ARM_ARCH_NONE,
26102                FPU_NONE),
26103   ARM_CPU_OPT ("cortex-m3",       "Cortex-M3",         ARM_ARCH_V7M,
26104                ARM_ARCH_NONE,
26105                FPU_NONE),
26106   ARM_CPU_OPT ("cortex-m1",       "Cortex-M1",         ARM_ARCH_V6SM,
26107                ARM_ARCH_NONE,
26108                FPU_NONE),
26109   ARM_CPU_OPT ("cortex-m0",       "Cortex-M0",         ARM_ARCH_V6SM,
26110                ARM_ARCH_NONE,
26111                FPU_NONE),
26112   ARM_CPU_OPT ("cortex-m0plus",   "Cortex-M0+",        ARM_ARCH_V6SM,
26113                ARM_ARCH_NONE,
26114                FPU_NONE),
26115   ARM_CPU_OPT ("exynos-m1",       "Samsung Exynos M1", ARM_ARCH_V8A,
26116                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26117                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26118
26119   /* ??? XSCALE is really an architecture.  */
26120   ARM_CPU_OPT ("xscale",          NULL,                ARM_ARCH_XSCALE,
26121                ARM_ARCH_NONE,
26122                FPU_ARCH_VFP_V2),
26123
26124   /* ??? iwmmxt is not a processor.  */
26125   ARM_CPU_OPT ("iwmmxt",          NULL,                ARM_ARCH_IWMMXT,
26126                ARM_ARCH_NONE,
26127                FPU_ARCH_VFP_V2),
26128   ARM_CPU_OPT ("iwmmxt2",         NULL,                ARM_ARCH_IWMMXT2,
26129                ARM_ARCH_NONE,
26130                FPU_ARCH_VFP_V2),
26131   ARM_CPU_OPT ("i80200",          NULL,                ARM_ARCH_XSCALE,
26132                ARM_ARCH_NONE,
26133                FPU_ARCH_VFP_V2),
26134
26135   /* Maverick.  */
26136   ARM_CPU_OPT ("ep9312",          "ARM920T",
26137                ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
26138                ARM_ARCH_NONE, FPU_ARCH_MAVERICK),
26139
26140   /* Marvell processors.  */
26141   ARM_CPU_OPT ("marvell-pj4",     NULL,                ARM_ARCH_V7A,
26142                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26143                FPU_ARCH_VFP_V3D16),
26144   ARM_CPU_OPT ("marvell-whitney", NULL,                ARM_ARCH_V7A,
26145                ARM_FEATURE_CORE_LOW (ARM_EXT_MP | ARM_EXT_SEC),
26146                FPU_ARCH_NEON_VFP_V4),
26147
26148   /* APM X-Gene family.  */
26149   ARM_CPU_OPT ("xgene1",          "APM X-Gene 1",      ARM_ARCH_V8A,
26150                ARM_ARCH_NONE,
26151                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26152   ARM_CPU_OPT ("xgene2",          "APM X-Gene 2",      ARM_ARCH_V8A,
26153                ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26154                FPU_ARCH_CRYPTO_NEON_VFP_ARMV8),
26155
26156   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
26157 };
26158 #undef ARM_CPU_OPT
26159
26160 struct arm_arch_option_table
26161 {
26162   const char *           name;
26163   size_t                 name_len;
26164   const arm_feature_set  value;
26165   const arm_feature_set  default_fpu;
26166 };
26167
26168 /* This list should, at a minimum, contain all the architecture names
26169    recognized by GCC.  */
26170 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
26171
26172 static const struct arm_arch_option_table arm_archs[] =
26173 {
26174   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
26175   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
26176   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
26177   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
26178   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
26179   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
26180   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
26181   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
26182   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
26183   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
26184   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
26185   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
26186   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
26187   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
26188   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
26189   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
26190   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
26191   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
26192   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
26193   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
26194   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
26195   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
26196      kept to preserve existing behaviour.  */
26197   ARM_ARCH_OPT ("armv6kz",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
26198   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
26199   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
26200   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
26201   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
26202   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
26203      kept to preserve existing behaviour.  */
26204   ARM_ARCH_OPT ("armv6kzt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
26205   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
26206   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
26207   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
26208   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
26209   /* The official spelling of the ARMv7 profile variants is the dashed form.
26210      Accept the non-dashed form for compatibility with old toolchains.  */
26211   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
26212   ARM_ARCH_OPT ("armv7ve",      ARM_ARCH_V7VE,   FPU_ARCH_VFP),
26213   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
26214   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
26215   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
26216   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
26217   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
26218   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
26219   ARM_ARCH_OPT ("armv8-m.base", ARM_ARCH_V8M_BASE, FPU_ARCH_VFP),
26220   ARM_ARCH_OPT ("armv8-m.main", ARM_ARCH_V8M_MAIN, FPU_ARCH_VFP),
26221   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
26222   ARM_ARCH_OPT ("armv8.1-a",    ARM_ARCH_V8_1A,  FPU_ARCH_VFP),
26223   ARM_ARCH_OPT ("armv8.2-a",    ARM_ARCH_V8_2A,  FPU_ARCH_VFP),
26224   ARM_ARCH_OPT ("armv8.3-a",    ARM_ARCH_V8_3A,  FPU_ARCH_VFP),
26225   ARM_ARCH_OPT ("armv8-r",      ARM_ARCH_V8R,    FPU_ARCH_VFP),
26226   ARM_ARCH_OPT ("armv8.4-a",    ARM_ARCH_V8_4A,  FPU_ARCH_VFP),
26227   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
26228   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
26229   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
26230   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
26231 };
26232 #undef ARM_ARCH_OPT
26233
26234 /* ISA extensions in the co-processor and main instruction set space.  */
26235
26236 struct arm_option_extension_value_table
26237 {
26238   const char *           name;
26239   size_t                 name_len;
26240   const arm_feature_set  merge_value;
26241   const arm_feature_set  clear_value;
26242   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
26243      indicates that an extension is available for all architectures while
26244      ARM_ANY marks an empty entry.  */
26245   const arm_feature_set  allowed_archs[2];
26246 };
26247
26248 /* The following table must be in alphabetical order with a NULL last entry.  */
26249
26250 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
26251 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
26252
26253 static const struct arm_option_extension_value_table arm_extensions[] =
26254 {
26255   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
26256                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26257   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
26258                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
26259                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26260   ARM_EXT_OPT ("dotprod", FPU_ARCH_DOTPROD_NEON_VFP_ARMV8,
26261                           ARM_FEATURE_COPROC (FPU_NEON_EXT_DOTPROD),
26262                           ARM_ARCH_V8_2A),
26263   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26264                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
26265                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
26266   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
26267                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26268   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26269                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
26270                         ARM_ARCH_V8_2A),
26271   ARM_EXT_OPT ("fp16fml",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
26272                                                   | ARM_EXT2_FP16_FML),
26273                            ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST
26274                                                   | ARM_EXT2_FP16_FML),
26275                            ARM_ARCH_V8_2A),
26276   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
26277                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
26278                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
26279                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
26280   /* Duplicate entry for the purpose of allowing ARMv7 to match in presence of
26281      Thumb divide instruction.  Due to this having the same name as the
26282      previous entry, this will be ignored when doing command-line parsing and
26283      only considered by build attribute selection code.  */
26284   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
26285                         ARM_FEATURE_CORE_LOW (ARM_EXT_DIV),
26286                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7)),
26287   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
26288                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
26289   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
26290                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
26291   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
26292                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
26293   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
26294                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
26295                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
26296                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
26297   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
26298                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
26299                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
26300   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
26301                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
26302                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26303   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
26304                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
26305                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26306   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
26307                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
26308                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8A)),
26309   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26310                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
26311                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
26312                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
26313   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
26314                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
26315                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
26316   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
26317                                      | ARM_EXT_DIV),
26318                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
26319                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
26320   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
26321                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
26322   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
26323 };
26324 #undef ARM_EXT_OPT
26325
26326 /* ISA floating-point and Advanced SIMD extensions.  */
26327 struct arm_option_fpu_value_table
26328 {
26329   const char *           name;
26330   const arm_feature_set  value;
26331 };
26332
26333 /* This list should, at a minimum, contain all the fpu names
26334    recognized by GCC.  */
26335 static const struct arm_option_fpu_value_table arm_fpus[] =
26336 {
26337   {"softfpa",           FPU_NONE},
26338   {"fpe",               FPU_ARCH_FPE},
26339   {"fpe2",              FPU_ARCH_FPE},
26340   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
26341   {"fpa",               FPU_ARCH_FPA},
26342   {"fpa10",             FPU_ARCH_FPA},
26343   {"fpa11",             FPU_ARCH_FPA},
26344   {"arm7500fe",         FPU_ARCH_FPA},
26345   {"softvfp",           FPU_ARCH_VFP},
26346   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
26347   {"vfp",               FPU_ARCH_VFP_V2},
26348   {"vfp9",              FPU_ARCH_VFP_V2},
26349   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
26350   {"vfp10",             FPU_ARCH_VFP_V2},
26351   {"vfp10-r0",          FPU_ARCH_VFP_V1},
26352   {"vfpxd",             FPU_ARCH_VFP_V1xD},
26353   {"vfpv2",             FPU_ARCH_VFP_V2},
26354   {"vfpv3",             FPU_ARCH_VFP_V3},
26355   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
26356   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
26357   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
26358   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
26359   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
26360   {"arm1020t",          FPU_ARCH_VFP_V1},
26361   {"arm1020e",          FPU_ARCH_VFP_V2},
26362   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
26363   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
26364   {"maverick",          FPU_ARCH_MAVERICK},
26365   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
26366   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
26367   {"neon-fp16",         FPU_ARCH_NEON_FP16},
26368   {"vfpv4",             FPU_ARCH_VFP_V4},
26369   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
26370   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
26371   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
26372   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
26373   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
26374   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
26375   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
26376   {"crypto-neon-fp-armv8",
26377                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
26378   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
26379   {"crypto-neon-fp-armv8.1",
26380                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
26381   {NULL,                ARM_ARCH_NONE}
26382 };
26383
26384 struct arm_option_value_table
26385 {
26386   const char *name;
26387   long value;
26388 };
26389
26390 static const struct arm_option_value_table arm_float_abis[] =
26391 {
26392   {"hard",      ARM_FLOAT_ABI_HARD},
26393   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
26394   {"soft",      ARM_FLOAT_ABI_SOFT},
26395   {NULL,        0}
26396 };
26397
26398 #ifdef OBJ_ELF
26399 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
26400 static const struct arm_option_value_table arm_eabis[] =
26401 {
26402   {"gnu",       EF_ARM_EABI_UNKNOWN},
26403   {"4",         EF_ARM_EABI_VER4},
26404   {"5",         EF_ARM_EABI_VER5},
26405   {NULL,        0}
26406 };
26407 #endif
26408
26409 struct arm_long_option_table
26410 {
26411   const char * option;                  /* Substring to match.  */
26412   const char * help;                    /* Help information.  */
26413   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
26414   const char * deprecated;              /* If non-null, print this message.  */
26415 };
26416
26417 static bfd_boolean
26418 arm_parse_extension (const char *str, const arm_feature_set *opt_set,
26419                      arm_feature_set **ext_set_p)
26420 {
26421   /* We insist on extensions being specified in alphabetical order, and with
26422      extensions being added before being removed.  We achieve this by having
26423      the global ARM_EXTENSIONS table in alphabetical order, and using the
26424      ADDING_VALUE variable to indicate whether we are adding an extension (1)
26425      or removing it (0) and only allowing it to change in the order
26426      -1 -> 1 -> 0.  */
26427   const struct arm_option_extension_value_table * opt = NULL;
26428   const arm_feature_set arm_any = ARM_ANY;
26429   int adding_value = -1;
26430
26431   if (!*ext_set_p)
26432     {
26433       *ext_set_p = XNEW (arm_feature_set);
26434       **ext_set_p = arm_arch_none;
26435     }
26436
26437   while (str != NULL && *str != 0)
26438     {
26439       const char *ext;
26440       size_t len;
26441
26442       if (*str != '+')
26443         {
26444           as_bad (_("invalid architectural extension"));
26445           return FALSE;
26446         }
26447
26448       str++;
26449       ext = strchr (str, '+');
26450
26451       if (ext != NULL)
26452         len = ext - str;
26453       else
26454         len = strlen (str);
26455
26456       if (len >= 2 && strncmp (str, "no", 2) == 0)
26457         {
26458           if (adding_value != 0)
26459             {
26460               adding_value = 0;
26461               opt = arm_extensions;
26462             }
26463
26464           len -= 2;
26465           str += 2;
26466         }
26467       else if (len > 0)
26468         {
26469           if (adding_value == -1)
26470             {
26471               adding_value = 1;
26472               opt = arm_extensions;
26473             }
26474           else if (adding_value != 1)
26475             {
26476               as_bad (_("must specify extensions to add before specifying "
26477                         "those to remove"));
26478               return FALSE;
26479             }
26480         }
26481
26482       if (len == 0)
26483         {
26484           as_bad (_("missing architectural extension"));
26485           return FALSE;
26486         }
26487
26488       gas_assert (adding_value != -1);
26489       gas_assert (opt != NULL);
26490
26491       /* Scan over the options table trying to find an exact match. */
26492       for (; opt->name != NULL; opt++)
26493         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26494           {
26495             int i, nb_allowed_archs =
26496               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
26497             /* Check we can apply the extension to this architecture.  */
26498             for (i = 0; i < nb_allowed_archs; i++)
26499               {
26500                 /* Empty entry.  */
26501                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
26502                   continue;
26503                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *opt_set))
26504                   break;
26505               }
26506             if (i == nb_allowed_archs)
26507               {
26508                 as_bad (_("extension does not apply to the base architecture"));
26509                 return FALSE;
26510               }
26511
26512             /* Add or remove the extension.  */
26513             if (adding_value)
26514               ARM_MERGE_FEATURE_SETS (**ext_set_p, **ext_set_p,
26515                                       opt->merge_value);
26516             else
26517               ARM_CLEAR_FEATURE (**ext_set_p, **ext_set_p, opt->clear_value);
26518
26519             /* Allowing Thumb division instructions for ARMv7 in autodetection
26520                rely on this break so that duplicate extensions (extensions
26521                with the same name as a previous extension in the list) are not
26522                considered for command-line parsing.  */
26523             break;
26524           }
26525
26526       if (opt->name == NULL)
26527         {
26528           /* Did we fail to find an extension because it wasn't specified in
26529              alphabetical order, or because it does not exist?  */
26530
26531           for (opt = arm_extensions; opt->name != NULL; opt++)
26532             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26533               break;
26534
26535           if (opt->name == NULL)
26536             as_bad (_("unknown architectural extension `%s'"), str);
26537           else
26538             as_bad (_("architectural extensions must be specified in "
26539                       "alphabetical order"));
26540
26541           return FALSE;
26542         }
26543       else
26544         {
26545           /* We should skip the extension we've just matched the next time
26546              round.  */
26547           opt++;
26548         }
26549
26550       str = ext;
26551     };
26552
26553   return TRUE;
26554 }
26555
26556 static bfd_boolean
26557 arm_parse_cpu (const char *str)
26558 {
26559   const struct arm_cpu_option_table *opt;
26560   const char *ext = strchr (str, '+');
26561   size_t len;
26562
26563   if (ext != NULL)
26564     len = ext - str;
26565   else
26566     len = strlen (str);
26567
26568   if (len == 0)
26569     {
26570       as_bad (_("missing cpu name `%s'"), str);
26571       return FALSE;
26572     }
26573
26574   for (opt = arm_cpus; opt->name != NULL; opt++)
26575     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26576       {
26577         mcpu_cpu_opt = &opt->value;
26578         if (!dyn_mcpu_ext_opt)
26579           dyn_mcpu_ext_opt = XNEW (arm_feature_set);
26580         *dyn_mcpu_ext_opt = opt->ext;
26581         mcpu_fpu_opt = &opt->default_fpu;
26582         if (opt->canonical_name)
26583           {
26584             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
26585             strcpy (selected_cpu_name, opt->canonical_name);
26586           }
26587         else
26588           {
26589             size_t i;
26590
26591             if (len >= sizeof selected_cpu_name)
26592               len = (sizeof selected_cpu_name) - 1;
26593
26594             for (i = 0; i < len; i++)
26595               selected_cpu_name[i] = TOUPPER (opt->name[i]);
26596             selected_cpu_name[i] = 0;
26597           }
26598
26599         if (ext != NULL)
26600           return arm_parse_extension (ext, mcpu_cpu_opt, &dyn_mcpu_ext_opt);
26601
26602         return TRUE;
26603       }
26604
26605   as_bad (_("unknown cpu `%s'"), str);
26606   return FALSE;
26607 }
26608
26609 static bfd_boolean
26610 arm_parse_arch (const char *str)
26611 {
26612   const struct arm_arch_option_table *opt;
26613   const char *ext = strchr (str, '+');
26614   size_t len;
26615
26616   if (ext != NULL)
26617     len = ext - str;
26618   else
26619     len = strlen (str);
26620
26621   if (len == 0)
26622     {
26623       as_bad (_("missing architecture name `%s'"), str);
26624       return FALSE;
26625     }
26626
26627   for (opt = arm_archs; opt->name != NULL; opt++)
26628     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26629       {
26630         march_cpu_opt = &opt->value;
26631         march_fpu_opt = &opt->default_fpu;
26632         strcpy (selected_cpu_name, opt->name);
26633
26634         if (ext != NULL)
26635           return arm_parse_extension (ext, march_cpu_opt, &dyn_march_ext_opt);
26636
26637         return TRUE;
26638       }
26639
26640   as_bad (_("unknown architecture `%s'\n"), str);
26641   return FALSE;
26642 }
26643
26644 static bfd_boolean
26645 arm_parse_fpu (const char * str)
26646 {
26647   const struct arm_option_fpu_value_table * opt;
26648
26649   for (opt = arm_fpus; opt->name != NULL; opt++)
26650     if (streq (opt->name, str))
26651       {
26652         mfpu_opt = &opt->value;
26653         return TRUE;
26654       }
26655
26656   as_bad (_("unknown floating point format `%s'\n"), str);
26657   return FALSE;
26658 }
26659
26660 static bfd_boolean
26661 arm_parse_float_abi (const char * str)
26662 {
26663   const struct arm_option_value_table * opt;
26664
26665   for (opt = arm_float_abis; opt->name != NULL; opt++)
26666     if (streq (opt->name, str))
26667       {
26668         mfloat_abi_opt = opt->value;
26669         return TRUE;
26670       }
26671
26672   as_bad (_("unknown floating point abi `%s'\n"), str);
26673   return FALSE;
26674 }
26675
26676 #ifdef OBJ_ELF
26677 static bfd_boolean
26678 arm_parse_eabi (const char * str)
26679 {
26680   const struct arm_option_value_table *opt;
26681
26682   for (opt = arm_eabis; opt->name != NULL; opt++)
26683     if (streq (opt->name, str))
26684       {
26685         meabi_flags = opt->value;
26686         return TRUE;
26687       }
26688   as_bad (_("unknown EABI `%s'\n"), str);
26689   return FALSE;
26690 }
26691 #endif
26692
26693 static bfd_boolean
26694 arm_parse_it_mode (const char * str)
26695 {
26696   bfd_boolean ret = TRUE;
26697
26698   if (streq ("arm", str))
26699     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
26700   else if (streq ("thumb", str))
26701     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
26702   else if (streq ("always", str))
26703     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
26704   else if (streq ("never", str))
26705     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
26706   else
26707     {
26708       as_bad (_("unknown implicit IT mode `%s', should be "\
26709                 "arm, thumb, always, or never."), str);
26710       ret = FALSE;
26711     }
26712
26713   return ret;
26714 }
26715
26716 static bfd_boolean
26717 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
26718 {
26719   codecomposer_syntax = TRUE;
26720   arm_comment_chars[0] = ';';
26721   arm_line_separator_chars[0] = 0;
26722   return TRUE;
26723 }
26724
26725 struct arm_long_option_table arm_long_opts[] =
26726 {
26727   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
26728    arm_parse_cpu, NULL},
26729   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
26730    arm_parse_arch, NULL},
26731   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
26732    arm_parse_fpu, NULL},
26733   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
26734    arm_parse_float_abi, NULL},
26735 #ifdef OBJ_ELF
26736   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
26737    arm_parse_eabi, NULL},
26738 #endif
26739   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
26740    arm_parse_it_mode, NULL},
26741   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
26742    arm_ccs_mode, NULL},
26743   {NULL, NULL, 0, NULL}
26744 };
26745
26746 int
26747 md_parse_option (int c, const char * arg)
26748 {
26749   struct arm_option_table *opt;
26750   const struct arm_legacy_option_table *fopt;
26751   struct arm_long_option_table *lopt;
26752
26753   switch (c)
26754     {
26755 #ifdef OPTION_EB
26756     case OPTION_EB:
26757       target_big_endian = 1;
26758       break;
26759 #endif
26760
26761 #ifdef OPTION_EL
26762     case OPTION_EL:
26763       target_big_endian = 0;
26764       break;
26765 #endif
26766
26767     case OPTION_FIX_V4BX:
26768       fix_v4bx = TRUE;
26769       break;
26770
26771     case 'a':
26772       /* Listing option.  Just ignore these, we don't support additional
26773          ones.  */
26774       return 0;
26775
26776     default:
26777       for (opt = arm_opts; opt->option != NULL; opt++)
26778         {
26779           if (c == opt->option[0]
26780               && ((arg == NULL && opt->option[1] == 0)
26781                   || streq (arg, opt->option + 1)))
26782             {
26783               /* If the option is deprecated, tell the user.  */
26784               if (warn_on_deprecated && opt->deprecated != NULL)
26785                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26786                            arg ? arg : "", _(opt->deprecated));
26787
26788               if (opt->var != NULL)
26789                 *opt->var = opt->value;
26790
26791               return 1;
26792             }
26793         }
26794
26795       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
26796         {
26797           if (c == fopt->option[0]
26798               && ((arg == NULL && fopt->option[1] == 0)
26799                   || streq (arg, fopt->option + 1)))
26800             {
26801               /* If the option is deprecated, tell the user.  */
26802               if (warn_on_deprecated && fopt->deprecated != NULL)
26803                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26804                            arg ? arg : "", _(fopt->deprecated));
26805
26806               if (fopt->var != NULL)
26807                 *fopt->var = &fopt->value;
26808
26809               return 1;
26810             }
26811         }
26812
26813       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26814         {
26815           /* These options are expected to have an argument.  */
26816           if (c == lopt->option[0]
26817               && arg != NULL
26818               && strncmp (arg, lopt->option + 1,
26819                           strlen (lopt->option + 1)) == 0)
26820             {
26821               /* If the option is deprecated, tell the user.  */
26822               if (warn_on_deprecated && lopt->deprecated != NULL)
26823                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
26824                            _(lopt->deprecated));
26825
26826               /* Call the sup-option parser.  */
26827               return lopt->func (arg + strlen (lopt->option) - 1);
26828             }
26829         }
26830
26831       return 0;
26832     }
26833
26834   return 1;
26835 }
26836
26837 void
26838 md_show_usage (FILE * fp)
26839 {
26840   struct arm_option_table *opt;
26841   struct arm_long_option_table *lopt;
26842
26843   fprintf (fp, _(" ARM-specific assembler options:\n"));
26844
26845   for (opt = arm_opts; opt->option != NULL; opt++)
26846     if (opt->help != NULL)
26847       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
26848
26849   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26850     if (lopt->help != NULL)
26851       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
26852
26853 #ifdef OPTION_EB
26854   fprintf (fp, _("\
26855   -EB                     assemble code for a big-endian cpu\n"));
26856 #endif
26857
26858 #ifdef OPTION_EL
26859   fprintf (fp, _("\
26860   -EL                     assemble code for a little-endian cpu\n"));
26861 #endif
26862
26863   fprintf (fp, _("\
26864   --fix-v4bx              Allow BX in ARMv4 code\n"));
26865 }
26866
26867 #ifdef OBJ_ELF
26868
26869 typedef struct
26870 {
26871   int val;
26872   arm_feature_set flags;
26873 } cpu_arch_ver_table;
26874
26875 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
26876    chronologically for architectures, with an exception for ARMv6-M and
26877    ARMv6S-M due to legacy reasons.  No new architecture should have a
26878    special case.  This allows for build attribute selection results to be
26879    stable when new architectures are added.  */
26880 static const cpu_arch_ver_table cpu_arch_ver[] =
26881 {
26882     {0, ARM_ARCH_V1},
26883     {0, ARM_ARCH_V2},
26884     {0, ARM_ARCH_V2S},
26885     {0, ARM_ARCH_V3},
26886     {0, ARM_ARCH_V3M},
26887     {1, ARM_ARCH_V4xM},
26888     {1, ARM_ARCH_V4},
26889     {2, ARM_ARCH_V4TxM},
26890     {2, ARM_ARCH_V4T},
26891     {3, ARM_ARCH_V5xM},
26892     {3, ARM_ARCH_V5},
26893     {3, ARM_ARCH_V5TxM},
26894     {3, ARM_ARCH_V5T},
26895     {4, ARM_ARCH_V5TExP},
26896     {4, ARM_ARCH_V5TE},
26897     {5, ARM_ARCH_V5TEJ},
26898     {6, ARM_ARCH_V6},
26899     {7, ARM_ARCH_V6Z},
26900     {7, ARM_ARCH_V6KZ},
26901     {9, ARM_ARCH_V6K},
26902     {8, ARM_ARCH_V6T2},
26903     {8, ARM_ARCH_V6KT2},
26904     {8, ARM_ARCH_V6ZT2},
26905     {8, ARM_ARCH_V6KZT2},
26906
26907     /* When assembling a file with only ARMv6-M or ARMv6S-M instruction, GNU as
26908        always selected build attributes to match those of ARMv6-M
26909        (resp. ARMv6S-M).  However, due to these architectures being a strict
26910        subset of ARMv7-M in terms of instructions available, ARMv7-M attributes
26911        would be selected when fully respecting chronology of architectures.
26912        It is thus necessary to make a special case of ARMv6-M and ARMv6S-M and
26913        move them before ARMv7 architectures.  */
26914     {11, ARM_ARCH_V6M},
26915     {12, ARM_ARCH_V6SM},
26916
26917     {10, ARM_ARCH_V7},
26918     {10, ARM_ARCH_V7A},
26919     {10, ARM_ARCH_V7R},
26920     {10, ARM_ARCH_V7M},
26921     {10, ARM_ARCH_V7VE},
26922     {13, ARM_ARCH_V7EM},
26923     {14, ARM_ARCH_V8A},
26924     {14, ARM_ARCH_V8_1A},
26925     {14, ARM_ARCH_V8_2A},
26926     {14, ARM_ARCH_V8_3A},
26927     {16, ARM_ARCH_V8M_BASE},
26928     {17, ARM_ARCH_V8M_MAIN},
26929     {15, ARM_ARCH_V8R},
26930     {16, ARM_ARCH_V8_4A},
26931     {-1, ARM_ARCH_NONE}
26932 };
26933
26934 /* Set an attribute if it has not already been set by the user.  */
26935
26936 static void
26937 aeabi_set_attribute_int (int tag, int value)
26938 {
26939   if (tag < 1
26940       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26941       || !attributes_set_explicitly[tag])
26942     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
26943 }
26944
26945 static void
26946 aeabi_set_attribute_string (int tag, const char *value)
26947 {
26948   if (tag < 1
26949       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26950       || !attributes_set_explicitly[tag])
26951     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
26952 }
26953
26954 /* Return whether features in the *NEEDED feature set are available via
26955    extensions for the architecture whose feature set is *ARCH_FSET.  */
26956
26957 static bfd_boolean
26958 have_ext_for_needed_feat_p (const arm_feature_set *arch_fset,
26959                             const arm_feature_set *needed)
26960 {
26961   int i, nb_allowed_archs;
26962   arm_feature_set ext_fset;
26963   const struct arm_option_extension_value_table *opt;
26964
26965   ext_fset = arm_arch_none;
26966   for (opt = arm_extensions; opt->name != NULL; opt++)
26967     {
26968       /* Extension does not provide any feature we need.  */
26969       if (!ARM_CPU_HAS_FEATURE (*needed, opt->merge_value))
26970         continue;
26971
26972       nb_allowed_archs =
26973         sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
26974       for (i = 0; i < nb_allowed_archs; i++)
26975         {
26976           /* Empty entry.  */
26977           if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_arch_any))
26978             break;
26979
26980           /* Extension is available, add it.  */
26981           if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *arch_fset))
26982             ARM_MERGE_FEATURE_SETS (ext_fset, ext_fset, opt->merge_value);
26983         }
26984     }
26985
26986   /* Can we enable all features in *needed?  */
26987   return ARM_FSET_CPU_SUBSET (*needed, ext_fset);
26988 }
26989
26990 /* Select value for Tag_CPU_arch and Tag_CPU_arch_profile build attributes for
26991    a given architecture feature set *ARCH_EXT_FSET including extension feature
26992    set *EXT_FSET.  Selection logic used depend on EXACT_MATCH:
26993    - if true, check for an exact match of the architecture modulo extensions;
26994    - otherwise, select build attribute value of the first superset
26995      architecture released so that results remains stable when new architectures
26996      are added.
26997    For -march/-mcpu=all the build attribute value of the most featureful
26998    architecture is returned.  Tag_CPU_arch_profile result is returned in
26999    PROFILE.  */
27000
27001 static int
27002 get_aeabi_cpu_arch_from_fset (const arm_feature_set *arch_ext_fset,
27003                               const arm_feature_set *ext_fset,
27004                               char *profile, int exact_match)
27005 {
27006   arm_feature_set arch_fset;
27007   const cpu_arch_ver_table *p_ver, *p_ver_ret = NULL;
27008
27009   /* Select most featureful architecture with all its extensions if building
27010      for -march=all as the feature sets used to set build attributes.  */
27011   if (ARM_FEATURE_EQUAL (*arch_ext_fset, arm_arch_any))
27012     {
27013       /* Force revisiting of decision for each new architecture.  */
27014       gas_assert (MAX_TAG_CPU_ARCH <= TAG_CPU_ARCH_V8M_MAIN);
27015       *profile = 'A';
27016       return TAG_CPU_ARCH_V8;
27017     }
27018
27019   ARM_CLEAR_FEATURE (arch_fset, *arch_ext_fset, *ext_fset);
27020
27021   for (p_ver = cpu_arch_ver; p_ver->val != -1; p_ver++)
27022     {
27023       arm_feature_set known_arch_fset;
27024
27025       ARM_CLEAR_FEATURE (known_arch_fset, p_ver->flags, fpu_any);
27026       if (exact_match)
27027         {
27028           /* Base architecture match user-specified architecture and
27029              extensions, eg. ARMv6S-M matching -march=armv6-m+os.  */
27030           if (ARM_FEATURE_EQUAL (*arch_ext_fset, known_arch_fset))
27031             {
27032               p_ver_ret = p_ver;
27033               goto found;
27034             }
27035           /* Base architecture match user-specified architecture only
27036              (eg. ARMv6-M in the same case as above).  Record it in case we
27037              find a match with above condition.  */
27038           else if (p_ver_ret == NULL
27039                    && ARM_FEATURE_EQUAL (arch_fset, known_arch_fset))
27040             p_ver_ret = p_ver;
27041         }
27042       else
27043         {
27044
27045           /* Architecture has all features wanted.  */
27046           if (ARM_FSET_CPU_SUBSET (arch_fset, known_arch_fset))
27047             {
27048               arm_feature_set added_fset;
27049
27050               /* Compute features added by this architecture over the one
27051                  recorded in p_ver_ret.  */
27052               if (p_ver_ret != NULL)
27053                 ARM_CLEAR_FEATURE (added_fset, known_arch_fset,
27054                                    p_ver_ret->flags);
27055               /* First architecture that match incl. with extensions, or the
27056                  only difference in features over the recorded match is
27057                  features that were optional and are now mandatory.  */
27058               if (p_ver_ret == NULL
27059                   || ARM_FSET_CPU_SUBSET (added_fset, arch_fset))
27060                 {
27061                   p_ver_ret = p_ver;
27062                   goto found;
27063                 }
27064             }
27065           else if (p_ver_ret == NULL)
27066             {
27067               arm_feature_set needed_ext_fset;
27068
27069               ARM_CLEAR_FEATURE (needed_ext_fset, arch_fset, known_arch_fset);
27070
27071               /* Architecture has all features needed when using some
27072                  extensions.  Record it and continue searching in case there
27073                  exist an architecture providing all needed features without
27074                  the need for extensions (eg. ARMv6S-M Vs ARMv6-M with
27075                  OS extension).  */
27076               if (have_ext_for_needed_feat_p (&known_arch_fset,
27077                                               &needed_ext_fset))
27078                 p_ver_ret = p_ver;
27079             }
27080         }
27081     }
27082
27083   if (p_ver_ret == NULL)
27084     return -1;
27085
27086 found:
27087   /* Tag_CPU_arch_profile.  */
27088   if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7a)
27089       || ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8)
27090       || (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_atomics)
27091           && !ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v8m_m_only)))
27092     *profile = 'A';
27093   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_v7r))
27094     *profile = 'R';
27095   else if (ARM_CPU_HAS_FEATURE (p_ver_ret->flags, arm_ext_m))
27096     *profile = 'M';
27097   else
27098     *profile = '\0';
27099   return p_ver_ret->val;
27100 }
27101
27102 /* Set the public EABI object attributes.  */
27103
27104 static void
27105 aeabi_set_public_attributes (void)
27106 {
27107   char profile;
27108   int arch = -1;
27109   int virt_sec = 0;
27110   int fp16_optional = 0;
27111   int skip_exact_match = 0;
27112   arm_feature_set flags, flags_arch, flags_ext;
27113
27114   /* Autodetection mode, choose the architecture based the instructions
27115      actually used.  */
27116   if (no_cpu_selected ())
27117     {
27118       ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
27119
27120       if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
27121         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
27122
27123       if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
27124         ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
27125
27126       /* Code run during relaxation relies on selected_cpu being set.  */
27127       selected_cpu = flags;
27128     }
27129   /* Otherwise, choose the architecture based on the capabilities of the
27130      requested cpu.  */
27131   else
27132     flags = selected_cpu;
27133   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
27134
27135   /* Allow the user to override the reported architecture.  */
27136   if (object_arch)
27137     {
27138       ARM_CLEAR_FEATURE (flags_arch, *object_arch, fpu_any);
27139       flags_ext = arm_arch_none;
27140     }
27141   else
27142     {
27143       ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
27144       flags_ext = dyn_mcpu_ext_opt ? *dyn_mcpu_ext_opt : arm_arch_none;
27145       skip_exact_match = ARM_FEATURE_EQUAL (selected_cpu, arm_arch_any);
27146     }
27147
27148   /* When this function is run again after relaxation has happened there is no
27149      way to determine whether an architecture or CPU was specified by the user:
27150      - selected_cpu is set above for relaxation to work;
27151      - march_cpu_opt is not set if only -mcpu or .cpu is used;
27152      - mcpu_cpu_opt is set to arm_arch_any for autodetection.
27153      Therefore, if not in -march=all case we first try an exact match and fall
27154      back to autodetection.  */
27155   if (!skip_exact_match)
27156     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 1);
27157   if (arch == -1)
27158     arch = get_aeabi_cpu_arch_from_fset (&flags_arch, &flags_ext, &profile, 0);
27159   if (arch == -1)
27160     as_bad (_("no architecture contains all the instructions used\n"));
27161
27162   /* Tag_CPU_name.  */
27163   if (selected_cpu_name[0])
27164     {
27165       char *q;
27166
27167       q = selected_cpu_name;
27168       if (strncmp (q, "armv", 4) == 0)
27169         {
27170           int i;
27171
27172           q += 4;
27173           for (i = 0; q[i]; i++)
27174             q[i] = TOUPPER (q[i]);
27175         }
27176       aeabi_set_attribute_string (Tag_CPU_name, q);
27177     }
27178
27179   /* Tag_CPU_arch.  */
27180   aeabi_set_attribute_int (Tag_CPU_arch, arch);
27181
27182   /* Tag_CPU_arch_profile.  */
27183   if (profile != '\0')
27184     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
27185
27186   /* Tag_DSP_extension.  */
27187   if (dyn_mcpu_ext_opt && ARM_CPU_HAS_FEATURE (*dyn_mcpu_ext_opt, arm_ext_dsp))
27188     aeabi_set_attribute_int (Tag_DSP_extension, 1);
27189
27190   ARM_CLEAR_FEATURE (flags_arch, flags, fpu_any);
27191   /* Tag_ARM_ISA_use.  */
27192   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
27193       || ARM_FEATURE_ZERO (flags_arch))
27194     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
27195
27196   /* Tag_THUMB_ISA_use.  */
27197   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
27198       || ARM_FEATURE_ZERO (flags_arch))
27199     {
27200       int thumb_isa_use;
27201
27202       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
27203           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
27204         thumb_isa_use = 3;
27205       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
27206         thumb_isa_use = 2;
27207       else
27208         thumb_isa_use = 1;
27209       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
27210     }
27211
27212   /* Tag_VFP_arch.  */
27213   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
27214     aeabi_set_attribute_int (Tag_VFP_arch,
27215                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
27216                              ? 7 : 8);
27217   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
27218     aeabi_set_attribute_int (Tag_VFP_arch,
27219                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
27220                              ? 5 : 6);
27221   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
27222     {
27223       fp16_optional = 1;
27224       aeabi_set_attribute_int (Tag_VFP_arch, 3);
27225     }
27226   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
27227     {
27228       aeabi_set_attribute_int (Tag_VFP_arch, 4);
27229       fp16_optional = 1;
27230     }
27231   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
27232     aeabi_set_attribute_int (Tag_VFP_arch, 2);
27233   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
27234            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
27235     aeabi_set_attribute_int (Tag_VFP_arch, 1);
27236
27237   /* Tag_ABI_HardFP_use.  */
27238   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
27239       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
27240     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
27241
27242   /* Tag_WMMX_arch.  */
27243   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
27244     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
27245   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
27246     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
27247
27248   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
27249   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
27250     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
27251   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
27252     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
27253   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
27254     {
27255       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
27256         {
27257           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
27258         }
27259       else
27260         {
27261           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
27262           fp16_optional = 1;
27263         }
27264     }
27265
27266   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
27267   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
27268     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
27269
27270   /* Tag_DIV_use.
27271
27272      We set Tag_DIV_use to two when integer divide instructions have been used
27273      in ARM state, or when Thumb integer divide instructions have been used,
27274      but we have no architecture profile set, nor have we any ARM instructions.
27275
27276      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
27277      by the base architecture.
27278
27279      For new architectures we will have to check these tests.  */
27280   gas_assert (arch <= TAG_CPU_ARCH_V8M_MAIN);
27281   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
27282       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
27283     aeabi_set_attribute_int (Tag_DIV_use, 0);
27284   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
27285            || (profile == '\0'
27286                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
27287                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
27288     aeabi_set_attribute_int (Tag_DIV_use, 2);
27289
27290   /* Tag_MP_extension_use.  */
27291   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
27292     aeabi_set_attribute_int (Tag_MPextension_use, 1);
27293
27294   /* Tag Virtualization_use.  */
27295   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
27296     virt_sec |= 1;
27297   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
27298     virt_sec |= 2;
27299   if (virt_sec != 0)
27300     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
27301 }
27302
27303 /* Post relaxation hook.  Recompute ARM attributes now that relaxation is
27304    finished and free extension feature bits which will not be used anymore.  */
27305
27306 void
27307 arm_md_post_relax (void)
27308 {
27309   aeabi_set_public_attributes ();
27310   XDELETE (dyn_mcpu_ext_opt);
27311   dyn_mcpu_ext_opt = NULL;
27312   XDELETE (dyn_march_ext_opt);
27313   dyn_march_ext_opt = NULL;
27314 }
27315
27316 /* Add the default contents for the .ARM.attributes section.  */
27317
27318 void
27319 arm_md_end (void)
27320 {
27321   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
27322     return;
27323
27324   aeabi_set_public_attributes ();
27325 }
27326 #endif /* OBJ_ELF */
27327
27328 /* Parse a .cpu directive.  */
27329
27330 static void
27331 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
27332 {
27333   const struct arm_cpu_option_table *opt;
27334   char *name;
27335   char saved_char;
27336
27337   name = input_line_pointer;
27338   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27339     input_line_pointer++;
27340   saved_char = *input_line_pointer;
27341   *input_line_pointer = 0;
27342
27343   /* Skip the first "all" entry.  */
27344   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
27345     if (streq (opt->name, name))
27346       {
27347         mcpu_cpu_opt = &opt->value;
27348         if (!dyn_mcpu_ext_opt)
27349           dyn_mcpu_ext_opt = XNEW (arm_feature_set);
27350         *dyn_mcpu_ext_opt = opt->ext;
27351         ARM_MERGE_FEATURE_SETS (selected_cpu, *mcpu_cpu_opt, *dyn_mcpu_ext_opt);
27352         if (opt->canonical_name)
27353           strcpy (selected_cpu_name, opt->canonical_name);
27354         else
27355           {
27356             int i;
27357             for (i = 0; opt->name[i]; i++)
27358               selected_cpu_name[i] = TOUPPER (opt->name[i]);
27359
27360             selected_cpu_name[i] = 0;
27361           }
27362         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
27363         if (dyn_mcpu_ext_opt)
27364           ARM_MERGE_FEATURE_SETS (cpu_variant, cpu_variant, *dyn_mcpu_ext_opt);
27365         *input_line_pointer = saved_char;
27366         demand_empty_rest_of_line ();
27367         return;
27368       }
27369   as_bad (_("unknown cpu `%s'"), name);
27370   *input_line_pointer = saved_char;
27371   ignore_rest_of_line ();
27372 }
27373
27374 /* Parse a .arch directive.  */
27375
27376 static void
27377 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
27378 {
27379   const struct arm_arch_option_table *opt;
27380   char saved_char;
27381   char *name;
27382
27383   name = input_line_pointer;
27384   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27385     input_line_pointer++;
27386   saved_char = *input_line_pointer;
27387   *input_line_pointer = 0;
27388
27389   /* Skip the first "all" entry.  */
27390   for (opt = arm_archs + 1; opt->name != NULL; opt++)
27391     if (streq (opt->name, name))
27392       {
27393         mcpu_cpu_opt = &opt->value;
27394         XDELETE (dyn_mcpu_ext_opt);
27395         dyn_mcpu_ext_opt = NULL;
27396         selected_cpu = *mcpu_cpu_opt;
27397         strcpy (selected_cpu_name, opt->name);
27398         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, *mfpu_opt);
27399         *input_line_pointer = saved_char;
27400         demand_empty_rest_of_line ();
27401         return;
27402       }
27403
27404   as_bad (_("unknown architecture `%s'\n"), name);
27405   *input_line_pointer = saved_char;
27406   ignore_rest_of_line ();
27407 }
27408
27409 /* Parse a .object_arch directive.  */
27410
27411 static void
27412 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
27413 {
27414   const struct arm_arch_option_table *opt;
27415   char saved_char;
27416   char *name;
27417
27418   name = input_line_pointer;
27419   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27420     input_line_pointer++;
27421   saved_char = *input_line_pointer;
27422   *input_line_pointer = 0;
27423
27424   /* Skip the first "all" entry.  */
27425   for (opt = arm_archs + 1; opt->name != NULL; opt++)
27426     if (streq (opt->name, name))
27427       {
27428         object_arch = &opt->value;
27429         *input_line_pointer = saved_char;
27430         demand_empty_rest_of_line ();
27431         return;
27432       }
27433
27434   as_bad (_("unknown architecture `%s'\n"), name);
27435   *input_line_pointer = saved_char;
27436   ignore_rest_of_line ();
27437 }
27438
27439 /* Parse a .arch_extension directive.  */
27440
27441 static void
27442 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
27443 {
27444   const struct arm_option_extension_value_table *opt;
27445   const arm_feature_set arm_any = ARM_ANY;
27446   char saved_char;
27447   char *name;
27448   int adding_value = 1;
27449
27450   name = input_line_pointer;
27451   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27452     input_line_pointer++;
27453   saved_char = *input_line_pointer;
27454   *input_line_pointer = 0;
27455
27456   if (strlen (name) >= 2
27457       && strncmp (name, "no", 2) == 0)
27458     {
27459       adding_value = 0;
27460       name += 2;
27461     }
27462
27463   for (opt = arm_extensions; opt->name != NULL; opt++)
27464     if (streq (opt->name, name))
27465       {
27466         int i, nb_allowed_archs =
27467           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
27468         for (i = 0; i < nb_allowed_archs; i++)
27469           {
27470             /* Empty entry.  */
27471             if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
27472               continue;
27473             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *mcpu_cpu_opt))
27474               break;
27475           }
27476
27477         if (i == nb_allowed_archs)
27478           {
27479             as_bad (_("architectural extension `%s' is not allowed for the "
27480                       "current base architecture"), name);
27481             break;
27482           }
27483
27484         if (!dyn_mcpu_ext_opt)
27485           {
27486             dyn_mcpu_ext_opt = XNEW (arm_feature_set);
27487             *dyn_mcpu_ext_opt = arm_arch_none;
27488           }
27489         if (adding_value)
27490           ARM_MERGE_FEATURE_SETS (*dyn_mcpu_ext_opt, *dyn_mcpu_ext_opt,
27491                                   opt->merge_value);
27492         else
27493           ARM_CLEAR_FEATURE (*dyn_mcpu_ext_opt, *dyn_mcpu_ext_opt,
27494                              opt->clear_value);
27495
27496         ARM_MERGE_FEATURE_SETS (selected_cpu, *mcpu_cpu_opt, *dyn_mcpu_ext_opt);
27497         ARM_MERGE_FEATURE_SETS (cpu_variant, selected_cpu, *mfpu_opt);
27498         *input_line_pointer = saved_char;
27499         demand_empty_rest_of_line ();
27500         /* Allowing Thumb division instructions for ARMv7 in autodetection rely
27501            on this return so that duplicate extensions (extensions with the
27502            same name as a previous extension in the list) are not considered
27503            for command-line parsing.  */
27504         return;
27505       }
27506
27507   if (opt->name == NULL)
27508     as_bad (_("unknown architecture extension `%s'\n"), name);
27509
27510   *input_line_pointer = saved_char;
27511   ignore_rest_of_line ();
27512 }
27513
27514 /* Parse a .fpu directive.  */
27515
27516 static void
27517 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
27518 {
27519   const struct arm_option_fpu_value_table *opt;
27520   char saved_char;
27521   char *name;
27522
27523   name = input_line_pointer;
27524   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
27525     input_line_pointer++;
27526   saved_char = *input_line_pointer;
27527   *input_line_pointer = 0;
27528
27529   for (opt = arm_fpus; opt->name != NULL; opt++)
27530     if (streq (opt->name, name))
27531       {
27532         mfpu_opt = &opt->value;
27533         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
27534         if (dyn_mcpu_ext_opt)
27535           ARM_MERGE_FEATURE_SETS (cpu_variant, cpu_variant, *dyn_mcpu_ext_opt);
27536         *input_line_pointer = saved_char;
27537         demand_empty_rest_of_line ();
27538         return;
27539       }
27540
27541   as_bad (_("unknown floating point format `%s'\n"), name);
27542   *input_line_pointer = saved_char;
27543   ignore_rest_of_line ();
27544 }
27545
27546 /* Copy symbol information.  */
27547
27548 void
27549 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
27550 {
27551   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
27552 }
27553
27554 #ifdef OBJ_ELF
27555 /* Given a symbolic attribute NAME, return the proper integer value.
27556    Returns -1 if the attribute is not known.  */
27557
27558 int
27559 arm_convert_symbolic_attribute (const char *name)
27560 {
27561   static const struct
27562   {
27563     const char * name;
27564     const int    tag;
27565   }
27566   attribute_table[] =
27567     {
27568       /* When you modify this table you should
27569          also modify the list in doc/c-arm.texi.  */
27570 #define T(tag) {#tag, tag}
27571       T (Tag_CPU_raw_name),
27572       T (Tag_CPU_name),
27573       T (Tag_CPU_arch),
27574       T (Tag_CPU_arch_profile),
27575       T (Tag_ARM_ISA_use),
27576       T (Tag_THUMB_ISA_use),
27577       T (Tag_FP_arch),
27578       T (Tag_VFP_arch),
27579       T (Tag_WMMX_arch),
27580       T (Tag_Advanced_SIMD_arch),
27581       T (Tag_PCS_config),
27582       T (Tag_ABI_PCS_R9_use),
27583       T (Tag_ABI_PCS_RW_data),
27584       T (Tag_ABI_PCS_RO_data),
27585       T (Tag_ABI_PCS_GOT_use),
27586       T (Tag_ABI_PCS_wchar_t),
27587       T (Tag_ABI_FP_rounding),
27588       T (Tag_ABI_FP_denormal),
27589       T (Tag_ABI_FP_exceptions),
27590       T (Tag_ABI_FP_user_exceptions),
27591       T (Tag_ABI_FP_number_model),
27592       T (Tag_ABI_align_needed),
27593       T (Tag_ABI_align8_needed),
27594       T (Tag_ABI_align_preserved),
27595       T (Tag_ABI_align8_preserved),
27596       T (Tag_ABI_enum_size),
27597       T (Tag_ABI_HardFP_use),
27598       T (Tag_ABI_VFP_args),
27599       T (Tag_ABI_WMMX_args),
27600       T (Tag_ABI_optimization_goals),
27601       T (Tag_ABI_FP_optimization_goals),
27602       T (Tag_compatibility),
27603       T (Tag_CPU_unaligned_access),
27604       T (Tag_FP_HP_extension),
27605       T (Tag_VFP_HP_extension),
27606       T (Tag_ABI_FP_16bit_format),
27607       T (Tag_MPextension_use),
27608       T (Tag_DIV_use),
27609       T (Tag_nodefaults),
27610       T (Tag_also_compatible_with),
27611       T (Tag_conformance),
27612       T (Tag_T2EE_use),
27613       T (Tag_Virtualization_use),
27614       T (Tag_DSP_extension),
27615       /* We deliberately do not include Tag_MPextension_use_legacy.  */
27616 #undef T
27617     };
27618   unsigned int i;
27619
27620   if (name == NULL)
27621     return -1;
27622
27623   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
27624     if (streq (name, attribute_table[i].name))
27625       return attribute_table[i].tag;
27626
27627   return -1;
27628 }
27629
27630 /* Apply sym value for relocations only in the case that they are for
27631    local symbols in the same segment as the fixup and you have the
27632    respective architectural feature for blx and simple switches.  */
27633
27634 int
27635 arm_apply_sym_value (struct fix * fixP, segT this_seg)
27636 {
27637   if (fixP->fx_addsy
27638       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
27639       /* PR 17444: If the local symbol is in a different section then a reloc
27640          will always be generated for it, so applying the symbol value now
27641          will result in a double offset being stored in the relocation.  */
27642       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
27643       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
27644     {
27645       switch (fixP->fx_r_type)
27646         {
27647         case BFD_RELOC_ARM_PCREL_BLX:
27648         case BFD_RELOC_THUMB_PCREL_BRANCH23:
27649           if (ARM_IS_FUNC (fixP->fx_addsy))
27650             return 1;
27651           break;
27652
27653         case BFD_RELOC_ARM_PCREL_CALL:
27654         case BFD_RELOC_THUMB_PCREL_BLX:
27655           if (THUMB_IS_FUNC (fixP->fx_addsy))
27656             return 1;
27657           break;
27658
27659         default:
27660           break;
27661         }
27662
27663     }
27664   return 0;
27665 }
27666 #endif /* OBJ_ELF */