[GAS/ARM] Fix expansion of ldr pseudo instruction
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994-2017 Free Software Foundation, Inc.
3    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
4         Modified by David Taylor (dtaylor@armltd.co.uk)
5         Cirrus coprocessor mods by Aldy Hernandez (aldyh@redhat.com)
6         Cirrus coprocessor fixes by Petko Manolov (petkan@nucleusys.com)
7         Cirrus coprocessor fixes by Vladimir Ivanov (vladitx@nucleusys.com)
8
9    This file is part of GAS, the GNU Assembler.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24    02110-1301, USA.  */
25
26 #include "as.h"
27 #include <limits.h>
28 #include <stdarg.h>
29 #define  NO_RELOC 0
30 #include "safe-ctype.h"
31 #include "subsegs.h"
32 #include "obstack.h"
33 #include "libiberty.h"
34 #include "opcode/arm.h"
35
36 #ifdef OBJ_ELF
37 #include "elf/arm.h"
38 #include "dw2gencfi.h"
39 #endif
40
41 #include "dwarf2dbg.h"
42
43 #ifdef OBJ_ELF
44 /* Must be at least the size of the largest unwind opcode (currently two).  */
45 #define ARM_OPCODE_CHUNK_SIZE 8
46
47 /* This structure holds the unwinding state.  */
48
49 static struct
50 {
51   symbolS *       proc_start;
52   symbolS *       table_entry;
53   symbolS *       personality_routine;
54   int             personality_index;
55   /* The segment containing the function.  */
56   segT            saved_seg;
57   subsegT         saved_subseg;
58   /* Opcodes generated from this function.  */
59   unsigned char * opcodes;
60   int             opcode_count;
61   int             opcode_alloc;
62   /* The number of bytes pushed to the stack.  */
63   offsetT         frame_size;
64   /* We don't add stack adjustment opcodes immediately so that we can merge
65      multiple adjustments.  We can also omit the final adjustment
66      when using a frame pointer.  */
67   offsetT         pending_offset;
68   /* These two fields are set by both unwind_movsp and unwind_setfp.  They
69      hold the reg+offset to use when restoring sp from a frame pointer.  */
70   offsetT         fp_offset;
71   int             fp_reg;
72   /* Nonzero if an unwind_setfp directive has been seen.  */
73   unsigned        fp_used:1;
74   /* Nonzero if the last opcode restores sp from fp_reg.  */
75   unsigned        sp_restored:1;
76 } unwind;
77
78 #endif /* OBJ_ELF */
79
80 /* Results from operand parsing worker functions.  */
81
82 typedef enum
83 {
84   PARSE_OPERAND_SUCCESS,
85   PARSE_OPERAND_FAIL,
86   PARSE_OPERAND_FAIL_NO_BACKTRACK
87 } parse_operand_result;
88
89 enum arm_float_abi
90 {
91   ARM_FLOAT_ABI_HARD,
92   ARM_FLOAT_ABI_SOFTFP,
93   ARM_FLOAT_ABI_SOFT
94 };
95
96 /* Types of processor to assemble for.  */
97 #ifndef CPU_DEFAULT
98 /* The code that was here used to select a default CPU depending on compiler
99    pre-defines which were only present when doing native builds, thus
100    changing gas' default behaviour depending upon the build host.
101
102    If you have a target that requires a default CPU option then the you
103    should define CPU_DEFAULT here.  */
104 #endif
105
106 #ifndef FPU_DEFAULT
107 # ifdef TE_LINUX
108 #  define FPU_DEFAULT FPU_ARCH_FPA
109 # elif defined (TE_NetBSD)
110 #  ifdef OBJ_ELF
111 #   define FPU_DEFAULT FPU_ARCH_VFP     /* Soft-float, but VFP order.  */
112 #  else
113     /* Legacy a.out format.  */
114 #   define FPU_DEFAULT FPU_ARCH_FPA     /* Soft-float, but FPA order.  */
115 #  endif
116 # elif defined (TE_VXWORKS)
117 #  define FPU_DEFAULT FPU_ARCH_VFP      /* Soft-float, VFP order.  */
118 # else
119    /* For backwards compatibility, default to FPA.  */
120 #  define FPU_DEFAULT FPU_ARCH_FPA
121 # endif
122 #endif /* ifndef FPU_DEFAULT */
123
124 #define streq(a, b)           (strcmp (a, b) == 0)
125
126 static arm_feature_set cpu_variant;
127 static arm_feature_set arm_arch_used;
128 static arm_feature_set thumb_arch_used;
129
130 /* Flags stored in private area of BFD structure.  */
131 static int uses_apcs_26      = FALSE;
132 static int atpcs             = FALSE;
133 static int support_interwork = FALSE;
134 static int uses_apcs_float   = FALSE;
135 static int pic_code          = FALSE;
136 static int fix_v4bx          = FALSE;
137 /* Warn on using deprecated features.  */
138 static int warn_on_deprecated = TRUE;
139
140 /* Understand CodeComposer Studio assembly syntax.  */
141 bfd_boolean codecomposer_syntax = FALSE;
142
143 /* Variables that we set while parsing command-line options.  Once all
144    options have been read we re-process these values to set the real
145    assembly flags.  */
146 static const arm_feature_set *legacy_cpu = NULL;
147 static const arm_feature_set *legacy_fpu = NULL;
148
149 static const arm_feature_set *mcpu_cpu_opt = NULL;
150 static const arm_feature_set *mcpu_fpu_opt = NULL;
151 static const arm_feature_set *march_cpu_opt = NULL;
152 static const arm_feature_set *march_fpu_opt = NULL;
153 static const arm_feature_set *mfpu_opt = NULL;
154 static const arm_feature_set *object_arch = NULL;
155
156 /* Constants for known architecture features.  */
157 static const arm_feature_set fpu_default = FPU_DEFAULT;
158 static const arm_feature_set fpu_arch_vfp_v1 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V1;
159 static const arm_feature_set fpu_arch_vfp_v2 = FPU_ARCH_VFP_V2;
160 static const arm_feature_set fpu_arch_vfp_v3 ATTRIBUTE_UNUSED = FPU_ARCH_VFP_V3;
161 static const arm_feature_set fpu_arch_neon_v1 ATTRIBUTE_UNUSED = FPU_ARCH_NEON_V1;
162 static const arm_feature_set fpu_arch_fpa = FPU_ARCH_FPA;
163 static const arm_feature_set fpu_any_hard = FPU_ANY_HARD;
164 #ifdef OBJ_ELF
165 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
166 #endif
167 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
168
169 #ifdef CPU_DEFAULT
170 static const arm_feature_set cpu_default = CPU_DEFAULT;
171 #endif
172
173 static const arm_feature_set arm_ext_v1 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
174 static const arm_feature_set arm_ext_v2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
175 static const arm_feature_set arm_ext_v2s = ARM_FEATURE_CORE_LOW (ARM_EXT_V2S);
176 static const arm_feature_set arm_ext_v3 = ARM_FEATURE_CORE_LOW (ARM_EXT_V3);
177 static const arm_feature_set arm_ext_v3m = ARM_FEATURE_CORE_LOW (ARM_EXT_V3M);
178 static const arm_feature_set arm_ext_v4 = ARM_FEATURE_CORE_LOW (ARM_EXT_V4);
179 static const arm_feature_set arm_ext_v4t = ARM_FEATURE_CORE_LOW (ARM_EXT_V4T);
180 static const arm_feature_set arm_ext_v5 = ARM_FEATURE_CORE_LOW (ARM_EXT_V5);
181 static const arm_feature_set arm_ext_v4t_5 =
182   ARM_FEATURE_CORE_LOW (ARM_EXT_V4T | ARM_EXT_V5);
183 static const arm_feature_set arm_ext_v5t = ARM_FEATURE_CORE_LOW (ARM_EXT_V5T);
184 static const arm_feature_set arm_ext_v5e = ARM_FEATURE_CORE_LOW (ARM_EXT_V5E);
185 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP);
186 static const arm_feature_set arm_ext_v5j = ARM_FEATURE_CORE_LOW (ARM_EXT_V5J);
187 static const arm_feature_set arm_ext_v6 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6);
188 static const arm_feature_set arm_ext_v6k = ARM_FEATURE_CORE_LOW (ARM_EXT_V6K);
189 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6T2);
190 static const arm_feature_set arm_ext_v6m = ARM_FEATURE_CORE_LOW (ARM_EXT_V6M);
191 static const arm_feature_set arm_ext_v6_notm =
192   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_NOTM);
193 static const arm_feature_set arm_ext_v6_dsp =
194   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_DSP);
195 static const arm_feature_set arm_ext_barrier =
196   ARM_FEATURE_CORE_LOW (ARM_EXT_BARRIER);
197 static const arm_feature_set arm_ext_msr =
198   ARM_FEATURE_CORE_LOW (ARM_EXT_THUMB_MSR);
199 static const arm_feature_set arm_ext_div = ARM_FEATURE_CORE_LOW (ARM_EXT_DIV);
200 static const arm_feature_set arm_ext_v7 = ARM_FEATURE_CORE_LOW (ARM_EXT_V7);
201 static const arm_feature_set arm_ext_v7a = ARM_FEATURE_CORE_LOW (ARM_EXT_V7A);
202 static const arm_feature_set arm_ext_v7r = ARM_FEATURE_CORE_LOW (ARM_EXT_V7R);
203 #ifdef OBJ_ELF
204 static const arm_feature_set arm_ext_v7m = ARM_FEATURE_CORE_LOW (ARM_EXT_V7M);
205 #endif
206 static const arm_feature_set arm_ext_v8 = ARM_FEATURE_CORE_LOW (ARM_EXT_V8);
207 static const arm_feature_set arm_ext_m =
208   ARM_FEATURE_CORE (ARM_EXT_V6M | ARM_EXT_OS | ARM_EXT_V7M,
209                     ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
210 static const arm_feature_set arm_ext_mp = ARM_FEATURE_CORE_LOW (ARM_EXT_MP);
211 static const arm_feature_set arm_ext_sec = ARM_FEATURE_CORE_LOW (ARM_EXT_SEC);
212 static const arm_feature_set arm_ext_os = ARM_FEATURE_CORE_LOW (ARM_EXT_OS);
213 static const arm_feature_set arm_ext_adiv = ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV);
214 static const arm_feature_set arm_ext_virt = ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT);
215 static const arm_feature_set arm_ext_pan = ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN);
216 static const arm_feature_set arm_ext_v8m = ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M);
217 static const arm_feature_set arm_ext_v8m_main =
218   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M_MAIN);
219 /* Instructions in ARMv8-M only found in M profile architectures.  */
220 static const arm_feature_set arm_ext_v8m_m_only =
221   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8M | ARM_EXT2_V8M_MAIN);
222 static const arm_feature_set arm_ext_v6t2_v8m =
223   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V6T2_V8M);
224 /* Instructions shared between ARMv8-A and ARMv8-M.  */
225 static const arm_feature_set arm_ext_atomics =
226   ARM_FEATURE_CORE_HIGH (ARM_EXT2_ATOMICS);
227 #ifdef OBJ_ELF
228 /* DSP instructions Tag_DSP_extension refers to.  */
229 static const arm_feature_set arm_ext_dsp =
230   ARM_FEATURE_CORE_LOW (ARM_EXT_V5E | ARM_EXT_V5ExP | ARM_EXT_V6_DSP);
231 #endif
232 static const arm_feature_set arm_ext_ras =
233   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS);
234 /* FP16 instructions.  */
235 static const arm_feature_set arm_ext_fp16 =
236   ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST);
237 static const arm_feature_set arm_ext_v8_3 =
238   ARM_FEATURE_CORE_HIGH (ARM_EXT2_V8_3A);
239
240 static const arm_feature_set arm_arch_any = ARM_ANY;
241 static const arm_feature_set arm_arch_full ATTRIBUTE_UNUSED = ARM_FEATURE (-1, -1, -1);
242 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
243 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
244 #ifdef OBJ_ELF
245 static const arm_feature_set arm_arch_v6m_only = ARM_ARCH_V6M_ONLY;
246 #endif
247
248 static const arm_feature_set arm_cext_iwmmxt2 =
249   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
250 static const arm_feature_set arm_cext_iwmmxt =
251   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
252 static const arm_feature_set arm_cext_xscale =
253   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
254 static const arm_feature_set arm_cext_maverick =
255   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
256 static const arm_feature_set fpu_fpa_ext_v1 =
257   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
258 static const arm_feature_set fpu_fpa_ext_v2 =
259   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
260 static const arm_feature_set fpu_vfp_ext_v1xd =
261   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
262 static const arm_feature_set fpu_vfp_ext_v1 =
263   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
264 static const arm_feature_set fpu_vfp_ext_v2 =
265   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
266 static const arm_feature_set fpu_vfp_ext_v3xd =
267   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
268 static const arm_feature_set fpu_vfp_ext_v3 =
269   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
270 static const arm_feature_set fpu_vfp_ext_d32 =
271   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
272 static const arm_feature_set fpu_neon_ext_v1 =
273   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
274 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
275   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
276 #ifdef OBJ_ELF
277 static const arm_feature_set fpu_vfp_fp16 =
278   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
279 static const arm_feature_set fpu_neon_ext_fma =
280   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
281 #endif
282 static const arm_feature_set fpu_vfp_ext_fma =
283   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
284 static const arm_feature_set fpu_vfp_ext_armv8 =
285   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
286 static const arm_feature_set fpu_vfp_ext_armv8xd =
287   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
288 static const arm_feature_set fpu_neon_ext_armv8 =
289   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
290 static const arm_feature_set fpu_crypto_ext_armv8 =
291   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
292 static const arm_feature_set crc_ext_armv8 =
293   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
294 static const arm_feature_set fpu_neon_ext_v8_1 =
295   ARM_FEATURE_COPROC (FPU_NEON_EXT_RDMA);
296
297 static int mfloat_abi_opt = -1;
298 /* Record user cpu selection for object attributes.  */
299 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
300 /* Must be long enough to hold any of the names in arm_cpus.  */
301 static char selected_cpu_name[20];
302
303 extern FLONUM_TYPE generic_floating_point_number;
304
305 /* Return if no cpu was selected on command-line.  */
306 static bfd_boolean
307 no_cpu_selected (void)
308 {
309   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
310 }
311
312 #ifdef OBJ_ELF
313 # ifdef EABI_DEFAULT
314 static int meabi_flags = EABI_DEFAULT;
315 # else
316 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
317 # endif
318
319 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
320
321 bfd_boolean
322 arm_is_eabi (void)
323 {
324   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
325 }
326 #endif
327
328 #ifdef OBJ_ELF
329 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
330 symbolS * GOT_symbol;
331 #endif
332
333 /* 0: assemble for ARM,
334    1: assemble for Thumb,
335    2: assemble for Thumb even though target CPU does not support thumb
336       instructions.  */
337 static int thumb_mode = 0;
338 /* A value distinct from the possible values for thumb_mode that we
339    can use to record whether thumb_mode has been copied into the
340    tc_frag_data field of a frag.  */
341 #define MODE_RECORDED (1 << 4)
342
343 /* Specifies the intrinsic IT insn behavior mode.  */
344 enum implicit_it_mode
345 {
346   IMPLICIT_IT_MODE_NEVER  = 0x00,
347   IMPLICIT_IT_MODE_ARM    = 0x01,
348   IMPLICIT_IT_MODE_THUMB  = 0x02,
349   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
350 };
351 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
352
353 /* If unified_syntax is true, we are processing the new unified
354    ARM/Thumb syntax.  Important differences from the old ARM mode:
355
356      - Immediate operands do not require a # prefix.
357      - Conditional affixes always appear at the end of the
358        instruction.  (For backward compatibility, those instructions
359        that formerly had them in the middle, continue to accept them
360        there.)
361      - The IT instruction may appear, and if it does is validated
362        against subsequent conditional affixes.  It does not generate
363        machine code.
364
365    Important differences from the old Thumb mode:
366
367      - Immediate operands do not require a # prefix.
368      - Most of the V6T2 instructions are only available in unified mode.
369      - The .N and .W suffixes are recognized and honored (it is an error
370        if they cannot be honored).
371      - All instructions set the flags if and only if they have an 's' affix.
372      - Conditional affixes may be used.  They are validated against
373        preceding IT instructions.  Unlike ARM mode, you cannot use a
374        conditional affix except in the scope of an IT instruction.  */
375
376 static bfd_boolean unified_syntax = FALSE;
377
378 /* An immediate operand can start with #, and ld*, st*, pld operands
379    can contain [ and ].  We need to tell APP not to elide whitespace
380    before a [, which can appear as the first operand for pld.
381    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
382 const char arm_symbol_chars[] = "#[]{}";
383
384 enum neon_el_type
385 {
386   NT_invtype,
387   NT_untyped,
388   NT_integer,
389   NT_float,
390   NT_poly,
391   NT_signed,
392   NT_unsigned
393 };
394
395 struct neon_type_el
396 {
397   enum neon_el_type type;
398   unsigned size;
399 };
400
401 #define NEON_MAX_TYPE_ELS 4
402
403 struct neon_type
404 {
405   struct neon_type_el el[NEON_MAX_TYPE_ELS];
406   unsigned elems;
407 };
408
409 enum it_instruction_type
410 {
411    OUTSIDE_IT_INSN,
412    INSIDE_IT_INSN,
413    INSIDE_IT_LAST_INSN,
414    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
415                               if inside, should be the last one.  */
416    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
417                               i.e. BKPT and NOP.  */
418    IT_INSN                 /* The IT insn has been parsed.  */
419 };
420
421 /* The maximum number of operands we need.  */
422 #define ARM_IT_MAX_OPERANDS 6
423
424 struct arm_it
425 {
426   const char *  error;
427   unsigned long instruction;
428   int           size;
429   int           size_req;
430   int           cond;
431   /* "uncond_value" is set to the value in place of the conditional field in
432      unconditional versions of the instruction, or -1 if nothing is
433      appropriate.  */
434   int           uncond_value;
435   struct neon_type vectype;
436   /* This does not indicate an actual NEON instruction, only that
437      the mnemonic accepts neon-style type suffixes.  */
438   int           is_neon;
439   /* Set to the opcode if the instruction needs relaxation.
440      Zero if the instruction is not relaxed.  */
441   unsigned long relax;
442   struct
443   {
444     bfd_reloc_code_real_type type;
445     expressionS              exp;
446     int                      pc_rel;
447   } reloc;
448
449   enum it_instruction_type it_insn_type;
450
451   struct
452   {
453     unsigned reg;
454     signed int imm;
455     struct neon_type_el vectype;
456     unsigned present    : 1;  /* Operand present.  */
457     unsigned isreg      : 1;  /* Operand was a register.  */
458     unsigned immisreg   : 1;  /* .imm field is a second register.  */
459     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
460     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
461     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
462     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
463        instructions. This allows us to disambiguate ARM <-> vector insns.  */
464     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
465     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
466     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
467     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
468     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
469     unsigned writeback  : 1;  /* Operand has trailing !  */
470     unsigned preind     : 1;  /* Preindexed address.  */
471     unsigned postind    : 1;  /* Postindexed address.  */
472     unsigned negative   : 1;  /* Index register was negated.  */
473     unsigned shifted    : 1;  /* Shift applied to operation.  */
474     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
475   } operands[ARM_IT_MAX_OPERANDS];
476 };
477
478 static struct arm_it inst;
479
480 #define NUM_FLOAT_VALS 8
481
482 const char * fp_const[] =
483 {
484   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
485 };
486
487 /* Number of littlenums required to hold an extended precision number.  */
488 #define MAX_LITTLENUMS 6
489
490 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
491
492 #define FAIL    (-1)
493 #define SUCCESS (0)
494
495 #define SUFF_S 1
496 #define SUFF_D 2
497 #define SUFF_E 3
498 #define SUFF_P 4
499
500 #define CP_T_X   0x00008000
501 #define CP_T_Y   0x00400000
502
503 #define CONDS_BIT        0x00100000
504 #define LOAD_BIT         0x00100000
505
506 #define DOUBLE_LOAD_FLAG 0x00000001
507
508 struct asm_cond
509 {
510   const char *   template_name;
511   unsigned long  value;
512 };
513
514 #define COND_ALWAYS 0xE
515
516 struct asm_psr
517 {
518   const char *   template_name;
519   unsigned long  field;
520 };
521
522 struct asm_barrier_opt
523 {
524   const char *    template_name;
525   unsigned long   value;
526   const arm_feature_set arch;
527 };
528
529 /* The bit that distinguishes CPSR and SPSR.  */
530 #define SPSR_BIT   (1 << 22)
531
532 /* The individual PSR flag bits.  */
533 #define PSR_c   (1 << 16)
534 #define PSR_x   (1 << 17)
535 #define PSR_s   (1 << 18)
536 #define PSR_f   (1 << 19)
537
538 struct reloc_entry
539 {
540   const char *                    name;
541   bfd_reloc_code_real_type  reloc;
542 };
543
544 enum vfp_reg_pos
545 {
546   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
547   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
548 };
549
550 enum vfp_ldstm_type
551 {
552   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
553 };
554
555 /* Bits for DEFINED field in neon_typed_alias.  */
556 #define NTA_HASTYPE  1
557 #define NTA_HASINDEX 2
558
559 struct neon_typed_alias
560 {
561   unsigned char        defined;
562   unsigned char        index;
563   struct neon_type_el  eltype;
564 };
565
566 /* ARM register categories.  This includes coprocessor numbers and various
567    architecture extensions' registers.  */
568 enum arm_reg_type
569 {
570   REG_TYPE_RN,
571   REG_TYPE_CP,
572   REG_TYPE_CN,
573   REG_TYPE_FN,
574   REG_TYPE_VFS,
575   REG_TYPE_VFD,
576   REG_TYPE_NQ,
577   REG_TYPE_VFSD,
578   REG_TYPE_NDQ,
579   REG_TYPE_NSDQ,
580   REG_TYPE_VFC,
581   REG_TYPE_MVF,
582   REG_TYPE_MVD,
583   REG_TYPE_MVFX,
584   REG_TYPE_MVDX,
585   REG_TYPE_MVAX,
586   REG_TYPE_DSPSC,
587   REG_TYPE_MMXWR,
588   REG_TYPE_MMXWC,
589   REG_TYPE_MMXWCG,
590   REG_TYPE_XSCALE,
591   REG_TYPE_RNB
592 };
593
594 /* Structure for a hash table entry for a register.
595    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
596    information which states whether a vector type or index is specified (for a
597    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
598 struct reg_entry
599 {
600   const char *               name;
601   unsigned int               number;
602   unsigned char              type;
603   unsigned char              builtin;
604   struct neon_typed_alias *  neon;
605 };
606
607 /* Diagnostics used when we don't get a register of the expected type.  */
608 const char * const reg_expected_msgs[] =
609 {
610   N_("ARM register expected"),
611   N_("bad or missing co-processor number"),
612   N_("co-processor register expected"),
613   N_("FPA register expected"),
614   N_("VFP single precision register expected"),
615   N_("VFP/Neon double precision register expected"),
616   N_("Neon quad precision register expected"),
617   N_("VFP single or double precision register expected"),
618   N_("Neon double or quad precision register expected"),
619   N_("VFP single, double or Neon quad precision register expected"),
620   N_("VFP system register expected"),
621   N_("Maverick MVF register expected"),
622   N_("Maverick MVD register expected"),
623   N_("Maverick MVFX register expected"),
624   N_("Maverick MVDX register expected"),
625   N_("Maverick MVAX register expected"),
626   N_("Maverick DSPSC register expected"),
627   N_("iWMMXt data register expected"),
628   N_("iWMMXt control register expected"),
629   N_("iWMMXt scalar register expected"),
630   N_("XScale accumulator register expected"),
631 };
632
633 /* Some well known registers that we refer to directly elsewhere.  */
634 #define REG_R12 12
635 #define REG_SP  13
636 #define REG_LR  14
637 #define REG_PC  15
638
639 /* ARM instructions take 4bytes in the object file, Thumb instructions
640    take 2:  */
641 #define INSN_SIZE       4
642
643 struct asm_opcode
644 {
645   /* Basic string to match.  */
646   const char * template_name;
647
648   /* Parameters to instruction.  */
649   unsigned int operands[8];
650
651   /* Conditional tag - see opcode_lookup.  */
652   unsigned int tag : 4;
653
654   /* Basic instruction code.  */
655   unsigned int avalue : 28;
656
657   /* Thumb-format instruction code.  */
658   unsigned int tvalue;
659
660   /* Which architecture variant provides this instruction.  */
661   const arm_feature_set * avariant;
662   const arm_feature_set * tvariant;
663
664   /* Function to call to encode instruction in ARM format.  */
665   void (* aencode) (void);
666
667   /* Function to call to encode instruction in Thumb format.  */
668   void (* tencode) (void);
669 };
670
671 /* Defines for various bits that we will want to toggle.  */
672 #define INST_IMMEDIATE  0x02000000
673 #define OFFSET_REG      0x02000000
674 #define HWOFFSET_IMM    0x00400000
675 #define SHIFT_BY_REG    0x00000010
676 #define PRE_INDEX       0x01000000
677 #define INDEX_UP        0x00800000
678 #define WRITE_BACK      0x00200000
679 #define LDM_TYPE_2_OR_3 0x00400000
680 #define CPSI_MMOD       0x00020000
681
682 #define LITERAL_MASK    0xf000f000
683 #define OPCODE_MASK     0xfe1fffff
684 #define V4_STR_BIT      0x00000020
685 #define VLDR_VMOV_SAME  0x0040f000
686
687 #define T2_SUBS_PC_LR   0xf3de8f00
688
689 #define DATA_OP_SHIFT   21
690 #define SBIT_SHIFT      20
691
692 #define T2_OPCODE_MASK  0xfe1fffff
693 #define T2_DATA_OP_SHIFT 21
694 #define T2_SBIT_SHIFT    20
695
696 #define A_COND_MASK         0xf0000000
697 #define A_PUSH_POP_OP_MASK  0x0fff0000
698
699 /* Opcodes for pushing/poping registers to/from the stack.  */
700 #define A1_OPCODE_PUSH    0x092d0000
701 #define A2_OPCODE_PUSH    0x052d0004
702 #define A2_OPCODE_POP     0x049d0004
703
704 /* Codes to distinguish the arithmetic instructions.  */
705 #define OPCODE_AND      0
706 #define OPCODE_EOR      1
707 #define OPCODE_SUB      2
708 #define OPCODE_RSB      3
709 #define OPCODE_ADD      4
710 #define OPCODE_ADC      5
711 #define OPCODE_SBC      6
712 #define OPCODE_RSC      7
713 #define OPCODE_TST      8
714 #define OPCODE_TEQ      9
715 #define OPCODE_CMP      10
716 #define OPCODE_CMN      11
717 #define OPCODE_ORR      12
718 #define OPCODE_MOV      13
719 #define OPCODE_BIC      14
720 #define OPCODE_MVN      15
721
722 #define T2_OPCODE_AND   0
723 #define T2_OPCODE_BIC   1
724 #define T2_OPCODE_ORR   2
725 #define T2_OPCODE_ORN   3
726 #define T2_OPCODE_EOR   4
727 #define T2_OPCODE_ADD   8
728 #define T2_OPCODE_ADC   10
729 #define T2_OPCODE_SBC   11
730 #define T2_OPCODE_SUB   13
731 #define T2_OPCODE_RSB   14
732
733 #define T_OPCODE_MUL 0x4340
734 #define T_OPCODE_TST 0x4200
735 #define T_OPCODE_CMN 0x42c0
736 #define T_OPCODE_NEG 0x4240
737 #define T_OPCODE_MVN 0x43c0
738
739 #define T_OPCODE_ADD_R3 0x1800
740 #define T_OPCODE_SUB_R3 0x1a00
741 #define T_OPCODE_ADD_HI 0x4400
742 #define T_OPCODE_ADD_ST 0xb000
743 #define T_OPCODE_SUB_ST 0xb080
744 #define T_OPCODE_ADD_SP 0xa800
745 #define T_OPCODE_ADD_PC 0xa000
746 #define T_OPCODE_ADD_I8 0x3000
747 #define T_OPCODE_SUB_I8 0x3800
748 #define T_OPCODE_ADD_I3 0x1c00
749 #define T_OPCODE_SUB_I3 0x1e00
750
751 #define T_OPCODE_ASR_R  0x4100
752 #define T_OPCODE_LSL_R  0x4080
753 #define T_OPCODE_LSR_R  0x40c0
754 #define T_OPCODE_ROR_R  0x41c0
755 #define T_OPCODE_ASR_I  0x1000
756 #define T_OPCODE_LSL_I  0x0000
757 #define T_OPCODE_LSR_I  0x0800
758
759 #define T_OPCODE_MOV_I8 0x2000
760 #define T_OPCODE_CMP_I8 0x2800
761 #define T_OPCODE_CMP_LR 0x4280
762 #define T_OPCODE_MOV_HR 0x4600
763 #define T_OPCODE_CMP_HR 0x4500
764
765 #define T_OPCODE_LDR_PC 0x4800
766 #define T_OPCODE_LDR_SP 0x9800
767 #define T_OPCODE_STR_SP 0x9000
768 #define T_OPCODE_LDR_IW 0x6800
769 #define T_OPCODE_STR_IW 0x6000
770 #define T_OPCODE_LDR_IH 0x8800
771 #define T_OPCODE_STR_IH 0x8000
772 #define T_OPCODE_LDR_IB 0x7800
773 #define T_OPCODE_STR_IB 0x7000
774 #define T_OPCODE_LDR_RW 0x5800
775 #define T_OPCODE_STR_RW 0x5000
776 #define T_OPCODE_LDR_RH 0x5a00
777 #define T_OPCODE_STR_RH 0x5200
778 #define T_OPCODE_LDR_RB 0x5c00
779 #define T_OPCODE_STR_RB 0x5400
780
781 #define T_OPCODE_PUSH   0xb400
782 #define T_OPCODE_POP    0xbc00
783
784 #define T_OPCODE_BRANCH 0xe000
785
786 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
787 #define THUMB_PP_PC_LR 0x0100
788 #define THUMB_LOAD_BIT 0x0800
789 #define THUMB2_LOAD_BIT 0x00100000
790
791 #define BAD_ARGS        _("bad arguments to instruction")
792 #define BAD_SP          _("r13 not allowed here")
793 #define BAD_PC          _("r15 not allowed here")
794 #define BAD_COND        _("instruction cannot be conditional")
795 #define BAD_OVERLAP     _("registers may not be the same")
796 #define BAD_HIREG       _("lo register required")
797 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
798 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
799 #define BAD_BRANCH      _("branch must be last instruction in IT block")
800 #define BAD_NOT_IT      _("instruction not allowed in IT block")
801 #define BAD_FPU         _("selected FPU does not support instruction")
802 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
803 #define BAD_IT_COND     _("incorrect condition in IT block")
804 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
805 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
806 #define BAD_PC_ADDRESSING \
807         _("cannot use register index with PC-relative addressing")
808 #define BAD_PC_WRITEBACK \
809         _("cannot use writeback with PC-relative addressing")
810 #define BAD_RANGE       _("branch out of range")
811 #define BAD_FP16        _("selected processor does not support fp16 instruction")
812 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
813 #define THUMB1_RELOC_ONLY  _("relocation valid in thumb1 code only")
814
815 static struct hash_control * arm_ops_hsh;
816 static struct hash_control * arm_cond_hsh;
817 static struct hash_control * arm_shift_hsh;
818 static struct hash_control * arm_psr_hsh;
819 static struct hash_control * arm_v7m_psr_hsh;
820 static struct hash_control * arm_reg_hsh;
821 static struct hash_control * arm_reloc_hsh;
822 static struct hash_control * arm_barrier_opt_hsh;
823
824 /* Stuff needed to resolve the label ambiguity
825    As:
826      ...
827      label:   <insn>
828    may differ from:
829      ...
830      label:
831               <insn>  */
832
833 symbolS *  last_label_seen;
834 static int label_is_thumb_function_name = FALSE;
835
836 /* Literal pool structure.  Held on a per-section
837    and per-sub-section basis.  */
838
839 #define MAX_LITERAL_POOL_SIZE 1024
840 typedef struct literal_pool
841 {
842   expressionS            literals [MAX_LITERAL_POOL_SIZE];
843   unsigned int           next_free_entry;
844   unsigned int           id;
845   symbolS *              symbol;
846   segT                   section;
847   subsegT                sub_section;
848 #ifdef OBJ_ELF
849   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
850 #endif
851   struct literal_pool *  next;
852   unsigned int           alignment;
853 } literal_pool;
854
855 /* Pointer to a linked list of literal pools.  */
856 literal_pool * list_of_pools = NULL;
857
858 typedef enum asmfunc_states
859 {
860   OUTSIDE_ASMFUNC,
861   WAITING_ASMFUNC_NAME,
862   WAITING_ENDASMFUNC
863 } asmfunc_states;
864
865 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
866
867 #ifdef OBJ_ELF
868 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
869 #else
870 static struct current_it now_it;
871 #endif
872
873 static inline int
874 now_it_compatible (int cond)
875 {
876   return (cond & ~1) == (now_it.cc & ~1);
877 }
878
879 static inline int
880 conditional_insn (void)
881 {
882   return inst.cond != COND_ALWAYS;
883 }
884
885 static int in_it_block (void);
886
887 static int handle_it_state (void);
888
889 static void force_automatic_it_block_close (void);
890
891 static void it_fsm_post_encode (void);
892
893 #define set_it_insn_type(type)                  \
894   do                                            \
895     {                                           \
896       inst.it_insn_type = type;                 \
897       if (handle_it_state () == FAIL)           \
898         return;                                 \
899     }                                           \
900   while (0)
901
902 #define set_it_insn_type_nonvoid(type, failret) \
903   do                                            \
904     {                                           \
905       inst.it_insn_type = type;                 \
906       if (handle_it_state () == FAIL)           \
907         return failret;                         \
908     }                                           \
909   while(0)
910
911 #define set_it_insn_type_last()                         \
912   do                                                    \
913     {                                                   \
914       if (inst.cond == COND_ALWAYS)                     \
915         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
916       else                                              \
917         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
918     }                                                   \
919   while (0)
920
921 /* Pure syntax.  */
922
923 /* This array holds the chars that always start a comment.  If the
924    pre-processor is disabled, these aren't very useful.  */
925 char arm_comment_chars[] = "@";
926
927 /* This array holds the chars that only start a comment at the beginning of
928    a line.  If the line seems to have the form '# 123 filename'
929    .line and .file directives will appear in the pre-processed output.  */
930 /* Note that input_file.c hand checks for '#' at the beginning of the
931    first line of the input file.  This is because the compiler outputs
932    #NO_APP at the beginning of its output.  */
933 /* Also note that comments like this one will always work.  */
934 const char line_comment_chars[] = "#";
935
936 char arm_line_separator_chars[] = ";";
937
938 /* Chars that can be used to separate mant
939    from exp in floating point numbers.  */
940 const char EXP_CHARS[] = "eE";
941
942 /* Chars that mean this number is a floating point constant.  */
943 /* As in 0f12.456  */
944 /* or    0d1.2345e12  */
945
946 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
947
948 /* Prefix characters that indicate the start of an immediate
949    value.  */
950 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
951
952 /* Separator character handling.  */
953
954 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
955
956 static inline int
957 skip_past_char (char ** str, char c)
958 {
959   /* PR gas/14987: Allow for whitespace before the expected character.  */
960   skip_whitespace (*str);
961
962   if (**str == c)
963     {
964       (*str)++;
965       return SUCCESS;
966     }
967   else
968     return FAIL;
969 }
970
971 #define skip_past_comma(str) skip_past_char (str, ',')
972
973 /* Arithmetic expressions (possibly involving symbols).  */
974
975 /* Return TRUE if anything in the expression is a bignum.  */
976
977 static int
978 walk_no_bignums (symbolS * sp)
979 {
980   if (symbol_get_value_expression (sp)->X_op == O_big)
981     return 1;
982
983   if (symbol_get_value_expression (sp)->X_add_symbol)
984     {
985       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
986               || (symbol_get_value_expression (sp)->X_op_symbol
987                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
988     }
989
990   return 0;
991 }
992
993 static int in_my_get_expression = 0;
994
995 /* Third argument to my_get_expression.  */
996 #define GE_NO_PREFIX 0
997 #define GE_IMM_PREFIX 1
998 #define GE_OPT_PREFIX 2
999 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
1000    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
1001 #define GE_OPT_PREFIX_BIG 3
1002
1003 static int
1004 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
1005 {
1006   char * save_in;
1007   segT   seg;
1008
1009   /* In unified syntax, all prefixes are optional.  */
1010   if (unified_syntax)
1011     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
1012                   : GE_OPT_PREFIX;
1013
1014   switch (prefix_mode)
1015     {
1016     case GE_NO_PREFIX: break;
1017     case GE_IMM_PREFIX:
1018       if (!is_immediate_prefix (**str))
1019         {
1020           inst.error = _("immediate expression requires a # prefix");
1021           return FAIL;
1022         }
1023       (*str)++;
1024       break;
1025     case GE_OPT_PREFIX:
1026     case GE_OPT_PREFIX_BIG:
1027       if (is_immediate_prefix (**str))
1028         (*str)++;
1029       break;
1030     default: abort ();
1031     }
1032
1033   memset (ep, 0, sizeof (expressionS));
1034
1035   save_in = input_line_pointer;
1036   input_line_pointer = *str;
1037   in_my_get_expression = 1;
1038   seg = expression (ep);
1039   in_my_get_expression = 0;
1040
1041   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1042     {
1043       /* We found a bad or missing expression in md_operand().  */
1044       *str = input_line_pointer;
1045       input_line_pointer = save_in;
1046       if (inst.error == NULL)
1047         inst.error = (ep->X_op == O_absent
1048                       ? _("missing expression") :_("bad expression"));
1049       return 1;
1050     }
1051
1052 #ifdef OBJ_AOUT
1053   if (seg != absolute_section
1054       && seg != text_section
1055       && seg != data_section
1056       && seg != bss_section
1057       && seg != undefined_section)
1058     {
1059       inst.error = _("bad segment");
1060       *str = input_line_pointer;
1061       input_line_pointer = save_in;
1062       return 1;
1063     }
1064 #else
1065   (void) seg;
1066 #endif
1067
1068   /* Get rid of any bignums now, so that we don't generate an error for which
1069      we can't establish a line number later on.  Big numbers are never valid
1070      in instructions, which is where this routine is always called.  */
1071   if (prefix_mode != GE_OPT_PREFIX_BIG
1072       && (ep->X_op == O_big
1073           || (ep->X_add_symbol
1074               && (walk_no_bignums (ep->X_add_symbol)
1075                   || (ep->X_op_symbol
1076                       && walk_no_bignums (ep->X_op_symbol))))))
1077     {
1078       inst.error = _("invalid constant");
1079       *str = input_line_pointer;
1080       input_line_pointer = save_in;
1081       return 1;
1082     }
1083
1084   *str = input_line_pointer;
1085   input_line_pointer = save_in;
1086   return 0;
1087 }
1088
1089 /* Turn a string in input_line_pointer into a floating point constant
1090    of type TYPE, and store the appropriate bytes in *LITP.  The number
1091    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1092    returned, or NULL on OK.
1093
1094    Note that fp constants aren't represent in the normal way on the ARM.
1095    In big endian mode, things are as expected.  However, in little endian
1096    mode fp constants are big-endian word-wise, and little-endian byte-wise
1097    within the words.  For example, (double) 1.1 in big endian mode is
1098    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1099    the byte sequence 99 99 f1 3f 9a 99 99 99.
1100
1101    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1102
1103 const char *
1104 md_atof (int type, char * litP, int * sizeP)
1105 {
1106   int prec;
1107   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1108   char *t;
1109   int i;
1110
1111   switch (type)
1112     {
1113     case 'f':
1114     case 'F':
1115     case 's':
1116     case 'S':
1117       prec = 2;
1118       break;
1119
1120     case 'd':
1121     case 'D':
1122     case 'r':
1123     case 'R':
1124       prec = 4;
1125       break;
1126
1127     case 'x':
1128     case 'X':
1129       prec = 5;
1130       break;
1131
1132     case 'p':
1133     case 'P':
1134       prec = 5;
1135       break;
1136
1137     default:
1138       *sizeP = 0;
1139       return _("Unrecognized or unsupported floating point constant");
1140     }
1141
1142   t = atof_ieee (input_line_pointer, type, words);
1143   if (t)
1144     input_line_pointer = t;
1145   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1146
1147   if (target_big_endian)
1148     {
1149       for (i = 0; i < prec; i++)
1150         {
1151           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1152           litP += sizeof (LITTLENUM_TYPE);
1153         }
1154     }
1155   else
1156     {
1157       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1158         for (i = prec - 1; i >= 0; i--)
1159           {
1160             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1161             litP += sizeof (LITTLENUM_TYPE);
1162           }
1163       else
1164         /* For a 4 byte float the order of elements in `words' is 1 0.
1165            For an 8 byte float the order is 1 0 3 2.  */
1166         for (i = 0; i < prec; i += 2)
1167           {
1168             md_number_to_chars (litP, (valueT) words[i + 1],
1169                                 sizeof (LITTLENUM_TYPE));
1170             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1171                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1172             litP += 2 * sizeof (LITTLENUM_TYPE);
1173           }
1174     }
1175
1176   return NULL;
1177 }
1178
1179 /* We handle all bad expressions here, so that we can report the faulty
1180    instruction in the error message.  */
1181 void
1182 md_operand (expressionS * exp)
1183 {
1184   if (in_my_get_expression)
1185     exp->X_op = O_illegal;
1186 }
1187
1188 /* Immediate values.  */
1189
1190 /* Generic immediate-value read function for use in directives.
1191    Accepts anything that 'expression' can fold to a constant.
1192    *val receives the number.  */
1193 #ifdef OBJ_ELF
1194 static int
1195 immediate_for_directive (int *val)
1196 {
1197   expressionS exp;
1198   exp.X_op = O_illegal;
1199
1200   if (is_immediate_prefix (*input_line_pointer))
1201     {
1202       input_line_pointer++;
1203       expression (&exp);
1204     }
1205
1206   if (exp.X_op != O_constant)
1207     {
1208       as_bad (_("expected #constant"));
1209       ignore_rest_of_line ();
1210       return FAIL;
1211     }
1212   *val = exp.X_add_number;
1213   return SUCCESS;
1214 }
1215 #endif
1216
1217 /* Register parsing.  */
1218
1219 /* Generic register parser.  CCP points to what should be the
1220    beginning of a register name.  If it is indeed a valid register
1221    name, advance CCP over it and return the reg_entry structure;
1222    otherwise return NULL.  Does not issue diagnostics.  */
1223
1224 static struct reg_entry *
1225 arm_reg_parse_multi (char **ccp)
1226 {
1227   char *start = *ccp;
1228   char *p;
1229   struct reg_entry *reg;
1230
1231   skip_whitespace (start);
1232
1233 #ifdef REGISTER_PREFIX
1234   if (*start != REGISTER_PREFIX)
1235     return NULL;
1236   start++;
1237 #endif
1238 #ifdef OPTIONAL_REGISTER_PREFIX
1239   if (*start == OPTIONAL_REGISTER_PREFIX)
1240     start++;
1241 #endif
1242
1243   p = start;
1244   if (!ISALPHA (*p) || !is_name_beginner (*p))
1245     return NULL;
1246
1247   do
1248     p++;
1249   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1250
1251   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1252
1253   if (!reg)
1254     return NULL;
1255
1256   *ccp = p;
1257   return reg;
1258 }
1259
1260 static int
1261 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1262                     enum arm_reg_type type)
1263 {
1264   /* Alternative syntaxes are accepted for a few register classes.  */
1265   switch (type)
1266     {
1267     case REG_TYPE_MVF:
1268     case REG_TYPE_MVD:
1269     case REG_TYPE_MVFX:
1270     case REG_TYPE_MVDX:
1271       /* Generic coprocessor register names are allowed for these.  */
1272       if (reg && reg->type == REG_TYPE_CN)
1273         return reg->number;
1274       break;
1275
1276     case REG_TYPE_CP:
1277       /* For backward compatibility, a bare number is valid here.  */
1278       {
1279         unsigned long processor = strtoul (start, ccp, 10);
1280         if (*ccp != start && processor <= 15)
1281           return processor;
1282       }
1283       /* Fall through.  */
1284
1285     case REG_TYPE_MMXWC:
1286       /* WC includes WCG.  ??? I'm not sure this is true for all
1287          instructions that take WC registers.  */
1288       if (reg && reg->type == REG_TYPE_MMXWCG)
1289         return reg->number;
1290       break;
1291
1292     default:
1293       break;
1294     }
1295
1296   return FAIL;
1297 }
1298
1299 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1300    return value is the register number or FAIL.  */
1301
1302 static int
1303 arm_reg_parse (char **ccp, enum arm_reg_type type)
1304 {
1305   char *start = *ccp;
1306   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1307   int ret;
1308
1309   /* Do not allow a scalar (reg+index) to parse as a register.  */
1310   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1311     return FAIL;
1312
1313   if (reg && reg->type == type)
1314     return reg->number;
1315
1316   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1317     return ret;
1318
1319   *ccp = start;
1320   return FAIL;
1321 }
1322
1323 /* Parse a Neon type specifier. *STR should point at the leading '.'
1324    character. Does no verification at this stage that the type fits the opcode
1325    properly. E.g.,
1326
1327      .i32.i32.s16
1328      .s32.f32
1329      .u16
1330
1331    Can all be legally parsed by this function.
1332
1333    Fills in neon_type struct pointer with parsed information, and updates STR
1334    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1335    type, FAIL if not.  */
1336
1337 static int
1338 parse_neon_type (struct neon_type *type, char **str)
1339 {
1340   char *ptr = *str;
1341
1342   if (type)
1343     type->elems = 0;
1344
1345   while (type->elems < NEON_MAX_TYPE_ELS)
1346     {
1347       enum neon_el_type thistype = NT_untyped;
1348       unsigned thissize = -1u;
1349
1350       if (*ptr != '.')
1351         break;
1352
1353       ptr++;
1354
1355       /* Just a size without an explicit type.  */
1356       if (ISDIGIT (*ptr))
1357         goto parsesize;
1358
1359       switch (TOLOWER (*ptr))
1360         {
1361         case 'i': thistype = NT_integer; break;
1362         case 'f': thistype = NT_float; break;
1363         case 'p': thistype = NT_poly; break;
1364         case 's': thistype = NT_signed; break;
1365         case 'u': thistype = NT_unsigned; break;
1366         case 'd':
1367           thistype = NT_float;
1368           thissize = 64;
1369           ptr++;
1370           goto done;
1371         default:
1372           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1373           return FAIL;
1374         }
1375
1376       ptr++;
1377
1378       /* .f is an abbreviation for .f32.  */
1379       if (thistype == NT_float && !ISDIGIT (*ptr))
1380         thissize = 32;
1381       else
1382         {
1383         parsesize:
1384           thissize = strtoul (ptr, &ptr, 10);
1385
1386           if (thissize != 8 && thissize != 16 && thissize != 32
1387               && thissize != 64)
1388             {
1389               as_bad (_("bad size %d in type specifier"), thissize);
1390               return FAIL;
1391             }
1392         }
1393
1394       done:
1395       if (type)
1396         {
1397           type->el[type->elems].type = thistype;
1398           type->el[type->elems].size = thissize;
1399           type->elems++;
1400         }
1401     }
1402
1403   /* Empty/missing type is not a successful parse.  */
1404   if (type->elems == 0)
1405     return FAIL;
1406
1407   *str = ptr;
1408
1409   return SUCCESS;
1410 }
1411
1412 /* Errors may be set multiple times during parsing or bit encoding
1413    (particularly in the Neon bits), but usually the earliest error which is set
1414    will be the most meaningful. Avoid overwriting it with later (cascading)
1415    errors by calling this function.  */
1416
1417 static void
1418 first_error (const char *err)
1419 {
1420   if (!inst.error)
1421     inst.error = err;
1422 }
1423
1424 /* Parse a single type, e.g. ".s32", leading period included.  */
1425 static int
1426 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1427 {
1428   char *str = *ccp;
1429   struct neon_type optype;
1430
1431   if (*str == '.')
1432     {
1433       if (parse_neon_type (&optype, &str) == SUCCESS)
1434         {
1435           if (optype.elems == 1)
1436             *vectype = optype.el[0];
1437           else
1438             {
1439               first_error (_("only one type should be specified for operand"));
1440               return FAIL;
1441             }
1442         }
1443       else
1444         {
1445           first_error (_("vector type expected"));
1446           return FAIL;
1447         }
1448     }
1449   else
1450     return FAIL;
1451
1452   *ccp = str;
1453
1454   return SUCCESS;
1455 }
1456
1457 /* Special meanings for indices (which have a range of 0-7), which will fit into
1458    a 4-bit integer.  */
1459
1460 #define NEON_ALL_LANES          15
1461 #define NEON_INTERLEAVE_LANES   14
1462
1463 /* Parse either a register or a scalar, with an optional type. Return the
1464    register number, and optionally fill in the actual type of the register
1465    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1466    type/index information in *TYPEINFO.  */
1467
1468 static int
1469 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1470                            enum arm_reg_type *rtype,
1471                            struct neon_typed_alias *typeinfo)
1472 {
1473   char *str = *ccp;
1474   struct reg_entry *reg = arm_reg_parse_multi (&str);
1475   struct neon_typed_alias atype;
1476   struct neon_type_el parsetype;
1477
1478   atype.defined = 0;
1479   atype.index = -1;
1480   atype.eltype.type = NT_invtype;
1481   atype.eltype.size = -1;
1482
1483   /* Try alternate syntax for some types of register. Note these are mutually
1484      exclusive with the Neon syntax extensions.  */
1485   if (reg == NULL)
1486     {
1487       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1488       if (altreg != FAIL)
1489         *ccp = str;
1490       if (typeinfo)
1491         *typeinfo = atype;
1492       return altreg;
1493     }
1494
1495   /* Undo polymorphism when a set of register types may be accepted.  */
1496   if ((type == REG_TYPE_NDQ
1497        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1498       || (type == REG_TYPE_VFSD
1499           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1500       || (type == REG_TYPE_NSDQ
1501           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1502               || reg->type == REG_TYPE_NQ))
1503       || (type == REG_TYPE_MMXWC
1504           && (reg->type == REG_TYPE_MMXWCG)))
1505     type = (enum arm_reg_type) reg->type;
1506
1507   if (type != reg->type)
1508     return FAIL;
1509
1510   if (reg->neon)
1511     atype = *reg->neon;
1512
1513   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1514     {
1515       if ((atype.defined & NTA_HASTYPE) != 0)
1516         {
1517           first_error (_("can't redefine type for operand"));
1518           return FAIL;
1519         }
1520       atype.defined |= NTA_HASTYPE;
1521       atype.eltype = parsetype;
1522     }
1523
1524   if (skip_past_char (&str, '[') == SUCCESS)
1525     {
1526       if (type != REG_TYPE_VFD)
1527         {
1528           first_error (_("only D registers may be indexed"));
1529           return FAIL;
1530         }
1531
1532       if ((atype.defined & NTA_HASINDEX) != 0)
1533         {
1534           first_error (_("can't change index for operand"));
1535           return FAIL;
1536         }
1537
1538       atype.defined |= NTA_HASINDEX;
1539
1540       if (skip_past_char (&str, ']') == SUCCESS)
1541         atype.index = NEON_ALL_LANES;
1542       else
1543         {
1544           expressionS exp;
1545
1546           my_get_expression (&exp, &str, GE_NO_PREFIX);
1547
1548           if (exp.X_op != O_constant)
1549             {
1550               first_error (_("constant expression required"));
1551               return FAIL;
1552             }
1553
1554           if (skip_past_char (&str, ']') == FAIL)
1555             return FAIL;
1556
1557           atype.index = exp.X_add_number;
1558         }
1559     }
1560
1561   if (typeinfo)
1562     *typeinfo = atype;
1563
1564   if (rtype)
1565     *rtype = type;
1566
1567   *ccp = str;
1568
1569   return reg->number;
1570 }
1571
1572 /* Like arm_reg_parse, but allow allow the following extra features:
1573     - If RTYPE is non-zero, return the (possibly restricted) type of the
1574       register (e.g. Neon double or quad reg when either has been requested).
1575     - If this is a Neon vector type with additional type information, fill
1576       in the struct pointed to by VECTYPE (if non-NULL).
1577    This function will fault on encountering a scalar.  */
1578
1579 static int
1580 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1581                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1582 {
1583   struct neon_typed_alias atype;
1584   char *str = *ccp;
1585   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1586
1587   if (reg == FAIL)
1588     return FAIL;
1589
1590   /* Do not allow regname(... to parse as a register.  */
1591   if (*str == '(')
1592     return FAIL;
1593
1594   /* Do not allow a scalar (reg+index) to parse as a register.  */
1595   if ((atype.defined & NTA_HASINDEX) != 0)
1596     {
1597       first_error (_("register operand expected, but got scalar"));
1598       return FAIL;
1599     }
1600
1601   if (vectype)
1602     *vectype = atype.eltype;
1603
1604   *ccp = str;
1605
1606   return reg;
1607 }
1608
1609 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1610 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1611
1612 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1613    have enough information to be able to do a good job bounds-checking. So, we
1614    just do easy checks here, and do further checks later.  */
1615
1616 static int
1617 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1618 {
1619   int reg;
1620   char *str = *ccp;
1621   struct neon_typed_alias atype;
1622
1623   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1624
1625   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1626     return FAIL;
1627
1628   if (atype.index == NEON_ALL_LANES)
1629     {
1630       first_error (_("scalar must have an index"));
1631       return FAIL;
1632     }
1633   else if (atype.index >= 64 / elsize)
1634     {
1635       first_error (_("scalar index out of range"));
1636       return FAIL;
1637     }
1638
1639   if (type)
1640     *type = atype.eltype;
1641
1642   *ccp = str;
1643
1644   return reg * 16 + atype.index;
1645 }
1646
1647 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1648
1649 static long
1650 parse_reg_list (char ** strp)
1651 {
1652   char * str = * strp;
1653   long   range = 0;
1654   int    another_range;
1655
1656   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1657   do
1658     {
1659       skip_whitespace (str);
1660
1661       another_range = 0;
1662
1663       if (*str == '{')
1664         {
1665           int in_range = 0;
1666           int cur_reg = -1;
1667
1668           str++;
1669           do
1670             {
1671               int reg;
1672
1673               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1674                 {
1675                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1676                   return FAIL;
1677                 }
1678
1679               if (in_range)
1680                 {
1681                   int i;
1682
1683                   if (reg <= cur_reg)
1684                     {
1685                       first_error (_("bad range in register list"));
1686                       return FAIL;
1687                     }
1688
1689                   for (i = cur_reg + 1; i < reg; i++)
1690                     {
1691                       if (range & (1 << i))
1692                         as_tsktsk
1693                           (_("Warning: duplicated register (r%d) in register list"),
1694                            i);
1695                       else
1696                         range |= 1 << i;
1697                     }
1698                   in_range = 0;
1699                 }
1700
1701               if (range & (1 << reg))
1702                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1703                            reg);
1704               else if (reg <= cur_reg)
1705                 as_tsktsk (_("Warning: register range not in ascending order"));
1706
1707               range |= 1 << reg;
1708               cur_reg = reg;
1709             }
1710           while (skip_past_comma (&str) != FAIL
1711                  || (in_range = 1, *str++ == '-'));
1712           str--;
1713
1714           if (skip_past_char (&str, '}') == FAIL)
1715             {
1716               first_error (_("missing `}'"));
1717               return FAIL;
1718             }
1719         }
1720       else
1721         {
1722           expressionS exp;
1723
1724           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1725             return FAIL;
1726
1727           if (exp.X_op == O_constant)
1728             {
1729               if (exp.X_add_number
1730                   != (exp.X_add_number & 0x0000ffff))
1731                 {
1732                   inst.error = _("invalid register mask");
1733                   return FAIL;
1734                 }
1735
1736               if ((range & exp.X_add_number) != 0)
1737                 {
1738                   int regno = range & exp.X_add_number;
1739
1740                   regno &= -regno;
1741                   regno = (1 << regno) - 1;
1742                   as_tsktsk
1743                     (_("Warning: duplicated register (r%d) in register list"),
1744                      regno);
1745                 }
1746
1747               range |= exp.X_add_number;
1748             }
1749           else
1750             {
1751               if (inst.reloc.type != 0)
1752                 {
1753                   inst.error = _("expression too complex");
1754                   return FAIL;
1755                 }
1756
1757               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1758               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1759               inst.reloc.pc_rel = 0;
1760             }
1761         }
1762
1763       if (*str == '|' || *str == '+')
1764         {
1765           str++;
1766           another_range = 1;
1767         }
1768     }
1769   while (another_range);
1770
1771   *strp = str;
1772   return range;
1773 }
1774
1775 /* Types of registers in a list.  */
1776
1777 enum reg_list_els
1778 {
1779   REGLIST_VFP_S,
1780   REGLIST_VFP_D,
1781   REGLIST_NEON_D
1782 };
1783
1784 /* Parse a VFP register list.  If the string is invalid return FAIL.
1785    Otherwise return the number of registers, and set PBASE to the first
1786    register.  Parses registers of type ETYPE.
1787    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1788      - Q registers can be used to specify pairs of D registers
1789      - { } can be omitted from around a singleton register list
1790          FIXME: This is not implemented, as it would require backtracking in
1791          some cases, e.g.:
1792            vtbl.8 d3,d4,d5
1793          This could be done (the meaning isn't really ambiguous), but doesn't
1794          fit in well with the current parsing framework.
1795      - 32 D registers may be used (also true for VFPv3).
1796    FIXME: Types are ignored in these register lists, which is probably a
1797    bug.  */
1798
1799 static int
1800 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1801 {
1802   char *str = *ccp;
1803   int base_reg;
1804   int new_base;
1805   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1806   int max_regs = 0;
1807   int count = 0;
1808   int warned = 0;
1809   unsigned long mask = 0;
1810   int i;
1811
1812   if (skip_past_char (&str, '{') == FAIL)
1813     {
1814       inst.error = _("expecting {");
1815       return FAIL;
1816     }
1817
1818   switch (etype)
1819     {
1820     case REGLIST_VFP_S:
1821       regtype = REG_TYPE_VFS;
1822       max_regs = 32;
1823       break;
1824
1825     case REGLIST_VFP_D:
1826       regtype = REG_TYPE_VFD;
1827       break;
1828
1829     case REGLIST_NEON_D:
1830       regtype = REG_TYPE_NDQ;
1831       break;
1832     }
1833
1834   if (etype != REGLIST_VFP_S)
1835     {
1836       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1837       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1838         {
1839           max_regs = 32;
1840           if (thumb_mode)
1841             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1842                                     fpu_vfp_ext_d32);
1843           else
1844             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1845                                     fpu_vfp_ext_d32);
1846         }
1847       else
1848         max_regs = 16;
1849     }
1850
1851   base_reg = max_regs;
1852
1853   do
1854     {
1855       int setmask = 1, addregs = 1;
1856
1857       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1858
1859       if (new_base == FAIL)
1860         {
1861           first_error (_(reg_expected_msgs[regtype]));
1862           return FAIL;
1863         }
1864
1865       if (new_base >= max_regs)
1866         {
1867           first_error (_("register out of range in list"));
1868           return FAIL;
1869         }
1870
1871       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1872       if (regtype == REG_TYPE_NQ)
1873         {
1874           setmask = 3;
1875           addregs = 2;
1876         }
1877
1878       if (new_base < base_reg)
1879         base_reg = new_base;
1880
1881       if (mask & (setmask << new_base))
1882         {
1883           first_error (_("invalid register list"));
1884           return FAIL;
1885         }
1886
1887       if ((mask >> new_base) != 0 && ! warned)
1888         {
1889           as_tsktsk (_("register list not in ascending order"));
1890           warned = 1;
1891         }
1892
1893       mask |= setmask << new_base;
1894       count += addregs;
1895
1896       if (*str == '-') /* We have the start of a range expression */
1897         {
1898           int high_range;
1899
1900           str++;
1901
1902           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1903               == FAIL)
1904             {
1905               inst.error = gettext (reg_expected_msgs[regtype]);
1906               return FAIL;
1907             }
1908
1909           if (high_range >= max_regs)
1910             {
1911               first_error (_("register out of range in list"));
1912               return FAIL;
1913             }
1914
1915           if (regtype == REG_TYPE_NQ)
1916             high_range = high_range + 1;
1917
1918           if (high_range <= new_base)
1919             {
1920               inst.error = _("register range not in ascending order");
1921               return FAIL;
1922             }
1923
1924           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1925             {
1926               if (mask & (setmask << new_base))
1927                 {
1928                   inst.error = _("invalid register list");
1929                   return FAIL;
1930                 }
1931
1932               mask |= setmask << new_base;
1933               count += addregs;
1934             }
1935         }
1936     }
1937   while (skip_past_comma (&str) != FAIL);
1938
1939   str++;
1940
1941   /* Sanity check -- should have raised a parse error above.  */
1942   if (count == 0 || count > max_regs)
1943     abort ();
1944
1945   *pbase = base_reg;
1946
1947   /* Final test -- the registers must be consecutive.  */
1948   mask >>= base_reg;
1949   for (i = 0; i < count; i++)
1950     {
1951       if ((mask & (1u << i)) == 0)
1952         {
1953           inst.error = _("non-contiguous register range");
1954           return FAIL;
1955         }
1956     }
1957
1958   *ccp = str;
1959
1960   return count;
1961 }
1962
1963 /* True if two alias types are the same.  */
1964
1965 static bfd_boolean
1966 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1967 {
1968   if (!a && !b)
1969     return TRUE;
1970
1971   if (!a || !b)
1972     return FALSE;
1973
1974   if (a->defined != b->defined)
1975     return FALSE;
1976
1977   if ((a->defined & NTA_HASTYPE) != 0
1978       && (a->eltype.type != b->eltype.type
1979           || a->eltype.size != b->eltype.size))
1980     return FALSE;
1981
1982   if ((a->defined & NTA_HASINDEX) != 0
1983       && (a->index != b->index))
1984     return FALSE;
1985
1986   return TRUE;
1987 }
1988
1989 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1990    The base register is put in *PBASE.
1991    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1992    the return value.
1993    The register stride (minus one) is put in bit 4 of the return value.
1994    Bits [6:5] encode the list length (minus one).
1995    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1996
1997 #define NEON_LANE(X)            ((X) & 0xf)
1998 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1999 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
2000
2001 static int
2002 parse_neon_el_struct_list (char **str, unsigned *pbase,
2003                            struct neon_type_el *eltype)
2004 {
2005   char *ptr = *str;
2006   int base_reg = -1;
2007   int reg_incr = -1;
2008   int count = 0;
2009   int lane = -1;
2010   int leading_brace = 0;
2011   enum arm_reg_type rtype = REG_TYPE_NDQ;
2012   const char *const incr_error = _("register stride must be 1 or 2");
2013   const char *const type_error = _("mismatched element/structure types in list");
2014   struct neon_typed_alias firsttype;
2015   firsttype.defined = 0;
2016   firsttype.eltype.type = NT_invtype;
2017   firsttype.eltype.size = -1;
2018   firsttype.index = -1;
2019
2020   if (skip_past_char (&ptr, '{') == SUCCESS)
2021     leading_brace = 1;
2022
2023   do
2024     {
2025       struct neon_typed_alias atype;
2026       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
2027
2028       if (getreg == FAIL)
2029         {
2030           first_error (_(reg_expected_msgs[rtype]));
2031           return FAIL;
2032         }
2033
2034       if (base_reg == -1)
2035         {
2036           base_reg = getreg;
2037           if (rtype == REG_TYPE_NQ)
2038             {
2039               reg_incr = 1;
2040             }
2041           firsttype = atype;
2042         }
2043       else if (reg_incr == -1)
2044         {
2045           reg_incr = getreg - base_reg;
2046           if (reg_incr < 1 || reg_incr > 2)
2047             {
2048               first_error (_(incr_error));
2049               return FAIL;
2050             }
2051         }
2052       else if (getreg != base_reg + reg_incr * count)
2053         {
2054           first_error (_(incr_error));
2055           return FAIL;
2056         }
2057
2058       if (! neon_alias_types_same (&atype, &firsttype))
2059         {
2060           first_error (_(type_error));
2061           return FAIL;
2062         }
2063
2064       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2065          modes.  */
2066       if (ptr[0] == '-')
2067         {
2068           struct neon_typed_alias htype;
2069           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2070           if (lane == -1)
2071             lane = NEON_INTERLEAVE_LANES;
2072           else if (lane != NEON_INTERLEAVE_LANES)
2073             {
2074               first_error (_(type_error));
2075               return FAIL;
2076             }
2077           if (reg_incr == -1)
2078             reg_incr = 1;
2079           else if (reg_incr != 1)
2080             {
2081               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2082               return FAIL;
2083             }
2084           ptr++;
2085           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2086           if (hireg == FAIL)
2087             {
2088               first_error (_(reg_expected_msgs[rtype]));
2089               return FAIL;
2090             }
2091           if (! neon_alias_types_same (&htype, &firsttype))
2092             {
2093               first_error (_(type_error));
2094               return FAIL;
2095             }
2096           count += hireg + dregs - getreg;
2097           continue;
2098         }
2099
2100       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2101       if (rtype == REG_TYPE_NQ)
2102         {
2103           count += 2;
2104           continue;
2105         }
2106
2107       if ((atype.defined & NTA_HASINDEX) != 0)
2108         {
2109           if (lane == -1)
2110             lane = atype.index;
2111           else if (lane != atype.index)
2112             {
2113               first_error (_(type_error));
2114               return FAIL;
2115             }
2116         }
2117       else if (lane == -1)
2118         lane = NEON_INTERLEAVE_LANES;
2119       else if (lane != NEON_INTERLEAVE_LANES)
2120         {
2121           first_error (_(type_error));
2122           return FAIL;
2123         }
2124       count++;
2125     }
2126   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2127
2128   /* No lane set by [x]. We must be interleaving structures.  */
2129   if (lane == -1)
2130     lane = NEON_INTERLEAVE_LANES;
2131
2132   /* Sanity check.  */
2133   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2134       || (count > 1 && reg_incr == -1))
2135     {
2136       first_error (_("error parsing element/structure list"));
2137       return FAIL;
2138     }
2139
2140   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2141     {
2142       first_error (_("expected }"));
2143       return FAIL;
2144     }
2145
2146   if (reg_incr == -1)
2147     reg_incr = 1;
2148
2149   if (eltype)
2150     *eltype = firsttype.eltype;
2151
2152   *pbase = base_reg;
2153   *str = ptr;
2154
2155   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2156 }
2157
2158 /* Parse an explicit relocation suffix on an expression.  This is
2159    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2160    arm_reloc_hsh contains no entries, so this function can only
2161    succeed if there is no () after the word.  Returns -1 on error,
2162    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2163
2164 static int
2165 parse_reloc (char **str)
2166 {
2167   struct reloc_entry *r;
2168   char *p, *q;
2169
2170   if (**str != '(')
2171     return BFD_RELOC_UNUSED;
2172
2173   p = *str + 1;
2174   q = p;
2175
2176   while (*q && *q != ')' && *q != ',')
2177     q++;
2178   if (*q != ')')
2179     return -1;
2180
2181   if ((r = (struct reloc_entry *)
2182        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2183     return -1;
2184
2185   *str = q + 1;
2186   return r->reloc;
2187 }
2188
2189 /* Directives: register aliases.  */
2190
2191 static struct reg_entry *
2192 insert_reg_alias (char *str, unsigned number, int type)
2193 {
2194   struct reg_entry *new_reg;
2195   const char *name;
2196
2197   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2198     {
2199       if (new_reg->builtin)
2200         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2201
2202       /* Only warn about a redefinition if it's not defined as the
2203          same register.  */
2204       else if (new_reg->number != number || new_reg->type != type)
2205         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2206
2207       return NULL;
2208     }
2209
2210   name = xstrdup (str);
2211   new_reg = XNEW (struct reg_entry);
2212
2213   new_reg->name = name;
2214   new_reg->number = number;
2215   new_reg->type = type;
2216   new_reg->builtin = FALSE;
2217   new_reg->neon = NULL;
2218
2219   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2220     abort ();
2221
2222   return new_reg;
2223 }
2224
2225 static void
2226 insert_neon_reg_alias (char *str, int number, int type,
2227                        struct neon_typed_alias *atype)
2228 {
2229   struct reg_entry *reg = insert_reg_alias (str, number, type);
2230
2231   if (!reg)
2232     {
2233       first_error (_("attempt to redefine typed alias"));
2234       return;
2235     }
2236
2237   if (atype)
2238     {
2239       reg->neon = XNEW (struct neon_typed_alias);
2240       *reg->neon = *atype;
2241     }
2242 }
2243
2244 /* Look for the .req directive.  This is of the form:
2245
2246         new_register_name .req existing_register_name
2247
2248    If we find one, or if it looks sufficiently like one that we want to
2249    handle any error here, return TRUE.  Otherwise return FALSE.  */
2250
2251 static bfd_boolean
2252 create_register_alias (char * newname, char *p)
2253 {
2254   struct reg_entry *old;
2255   char *oldname, *nbuf;
2256   size_t nlen;
2257
2258   /* The input scrubber ensures that whitespace after the mnemonic is
2259      collapsed to single spaces.  */
2260   oldname = p;
2261   if (strncmp (oldname, " .req ", 6) != 0)
2262     return FALSE;
2263
2264   oldname += 6;
2265   if (*oldname == '\0')
2266     return FALSE;
2267
2268   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2269   if (!old)
2270     {
2271       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2272       return TRUE;
2273     }
2274
2275   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2276      the desired alias name, and p points to its end.  If not, then
2277      the desired alias name is in the global original_case_string.  */
2278 #ifdef TC_CASE_SENSITIVE
2279   nlen = p - newname;
2280 #else
2281   newname = original_case_string;
2282   nlen = strlen (newname);
2283 #endif
2284
2285   nbuf = xmemdup0 (newname, nlen);
2286
2287   /* Create aliases under the new name as stated; an all-lowercase
2288      version of the new name; and an all-uppercase version of the new
2289      name.  */
2290   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2291     {
2292       for (p = nbuf; *p; p++)
2293         *p = TOUPPER (*p);
2294
2295       if (strncmp (nbuf, newname, nlen))
2296         {
2297           /* If this attempt to create an additional alias fails, do not bother
2298              trying to create the all-lower case alias.  We will fail and issue
2299              a second, duplicate error message.  This situation arises when the
2300              programmer does something like:
2301                foo .req r0
2302                Foo .req r1
2303              The second .req creates the "Foo" alias but then fails to create
2304              the artificial FOO alias because it has already been created by the
2305              first .req.  */
2306           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2307             {
2308               free (nbuf);
2309               return TRUE;
2310             }
2311         }
2312
2313       for (p = nbuf; *p; p++)
2314         *p = TOLOWER (*p);
2315
2316       if (strncmp (nbuf, newname, nlen))
2317         insert_reg_alias (nbuf, old->number, old->type);
2318     }
2319
2320   free (nbuf);
2321   return TRUE;
2322 }
2323
2324 /* Create a Neon typed/indexed register alias using directives, e.g.:
2325      X .dn d5.s32[1]
2326      Y .qn 6.s16
2327      Z .dn d7
2328      T .dn Z[0]
2329    These typed registers can be used instead of the types specified after the
2330    Neon mnemonic, so long as all operands given have types. Types can also be
2331    specified directly, e.g.:
2332      vadd d0.s32, d1.s32, d2.s32  */
2333
2334 static bfd_boolean
2335 create_neon_reg_alias (char *newname, char *p)
2336 {
2337   enum arm_reg_type basetype;
2338   struct reg_entry *basereg;
2339   struct reg_entry mybasereg;
2340   struct neon_type ntype;
2341   struct neon_typed_alias typeinfo;
2342   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2343   int namelen;
2344
2345   typeinfo.defined = 0;
2346   typeinfo.eltype.type = NT_invtype;
2347   typeinfo.eltype.size = -1;
2348   typeinfo.index = -1;
2349
2350   nameend = p;
2351
2352   if (strncmp (p, " .dn ", 5) == 0)
2353     basetype = REG_TYPE_VFD;
2354   else if (strncmp (p, " .qn ", 5) == 0)
2355     basetype = REG_TYPE_NQ;
2356   else
2357     return FALSE;
2358
2359   p += 5;
2360
2361   if (*p == '\0')
2362     return FALSE;
2363
2364   basereg = arm_reg_parse_multi (&p);
2365
2366   if (basereg && basereg->type != basetype)
2367     {
2368       as_bad (_("bad type for register"));
2369       return FALSE;
2370     }
2371
2372   if (basereg == NULL)
2373     {
2374       expressionS exp;
2375       /* Try parsing as an integer.  */
2376       my_get_expression (&exp, &p, GE_NO_PREFIX);
2377       if (exp.X_op != O_constant)
2378         {
2379           as_bad (_("expression must be constant"));
2380           return FALSE;
2381         }
2382       basereg = &mybasereg;
2383       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2384                                                   : exp.X_add_number;
2385       basereg->neon = 0;
2386     }
2387
2388   if (basereg->neon)
2389     typeinfo = *basereg->neon;
2390
2391   if (parse_neon_type (&ntype, &p) == SUCCESS)
2392     {
2393       /* We got a type.  */
2394       if (typeinfo.defined & NTA_HASTYPE)
2395         {
2396           as_bad (_("can't redefine the type of a register alias"));
2397           return FALSE;
2398         }
2399
2400       typeinfo.defined |= NTA_HASTYPE;
2401       if (ntype.elems != 1)
2402         {
2403           as_bad (_("you must specify a single type only"));
2404           return FALSE;
2405         }
2406       typeinfo.eltype = ntype.el[0];
2407     }
2408
2409   if (skip_past_char (&p, '[') == SUCCESS)
2410     {
2411       expressionS exp;
2412       /* We got a scalar index.  */
2413
2414       if (typeinfo.defined & NTA_HASINDEX)
2415         {
2416           as_bad (_("can't redefine the index of a scalar alias"));
2417           return FALSE;
2418         }
2419
2420       my_get_expression (&exp, &p, GE_NO_PREFIX);
2421
2422       if (exp.X_op != O_constant)
2423         {
2424           as_bad (_("scalar index must be constant"));
2425           return FALSE;
2426         }
2427
2428       typeinfo.defined |= NTA_HASINDEX;
2429       typeinfo.index = exp.X_add_number;
2430
2431       if (skip_past_char (&p, ']') == FAIL)
2432         {
2433           as_bad (_("expecting ]"));
2434           return FALSE;
2435         }
2436     }
2437
2438   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2439      the desired alias name, and p points to its end.  If not, then
2440      the desired alias name is in the global original_case_string.  */
2441 #ifdef TC_CASE_SENSITIVE
2442   namelen = nameend - newname;
2443 #else
2444   newname = original_case_string;
2445   namelen = strlen (newname);
2446 #endif
2447
2448   namebuf = xmemdup0 (newname, namelen);
2449
2450   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2451                          typeinfo.defined != 0 ? &typeinfo : NULL);
2452
2453   /* Insert name in all uppercase.  */
2454   for (p = namebuf; *p; p++)
2455     *p = TOUPPER (*p);
2456
2457   if (strncmp (namebuf, newname, namelen))
2458     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2459                            typeinfo.defined != 0 ? &typeinfo : NULL);
2460
2461   /* Insert name in all lowercase.  */
2462   for (p = namebuf; *p; p++)
2463     *p = TOLOWER (*p);
2464
2465   if (strncmp (namebuf, newname, namelen))
2466     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2467                            typeinfo.defined != 0 ? &typeinfo : NULL);
2468
2469   free (namebuf);
2470   return TRUE;
2471 }
2472
2473 /* Should never be called, as .req goes between the alias and the
2474    register name, not at the beginning of the line.  */
2475
2476 static void
2477 s_req (int a ATTRIBUTE_UNUSED)
2478 {
2479   as_bad (_("invalid syntax for .req directive"));
2480 }
2481
2482 static void
2483 s_dn (int a ATTRIBUTE_UNUSED)
2484 {
2485   as_bad (_("invalid syntax for .dn directive"));
2486 }
2487
2488 static void
2489 s_qn (int a ATTRIBUTE_UNUSED)
2490 {
2491   as_bad (_("invalid syntax for .qn directive"));
2492 }
2493
2494 /* The .unreq directive deletes an alias which was previously defined
2495    by .req.  For example:
2496
2497        my_alias .req r11
2498        .unreq my_alias    */
2499
2500 static void
2501 s_unreq (int a ATTRIBUTE_UNUSED)
2502 {
2503   char * name;
2504   char saved_char;
2505
2506   name = input_line_pointer;
2507
2508   while (*input_line_pointer != 0
2509          && *input_line_pointer != ' '
2510          && *input_line_pointer != '\n')
2511     ++input_line_pointer;
2512
2513   saved_char = *input_line_pointer;
2514   *input_line_pointer = 0;
2515
2516   if (!*name)
2517     as_bad (_("invalid syntax for .unreq directive"));
2518   else
2519     {
2520       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2521                                                               name);
2522
2523       if (!reg)
2524         as_bad (_("unknown register alias '%s'"), name);
2525       else if (reg->builtin)
2526         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2527                  name);
2528       else
2529         {
2530           char * p;
2531           char * nbuf;
2532
2533           hash_delete (arm_reg_hsh, name, FALSE);
2534           free ((char *) reg->name);
2535           if (reg->neon)
2536             free (reg->neon);
2537           free (reg);
2538
2539           /* Also locate the all upper case and all lower case versions.
2540              Do not complain if we cannot find one or the other as it
2541              was probably deleted above.  */
2542
2543           nbuf = strdup (name);
2544           for (p = nbuf; *p; p++)
2545             *p = TOUPPER (*p);
2546           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2547           if (reg)
2548             {
2549               hash_delete (arm_reg_hsh, nbuf, FALSE);
2550               free ((char *) reg->name);
2551               if (reg->neon)
2552                 free (reg->neon);
2553               free (reg);
2554             }
2555
2556           for (p = nbuf; *p; p++)
2557             *p = TOLOWER (*p);
2558           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2559           if (reg)
2560             {
2561               hash_delete (arm_reg_hsh, nbuf, FALSE);
2562               free ((char *) reg->name);
2563               if (reg->neon)
2564                 free (reg->neon);
2565               free (reg);
2566             }
2567
2568           free (nbuf);
2569         }
2570     }
2571
2572   *input_line_pointer = saved_char;
2573   demand_empty_rest_of_line ();
2574 }
2575
2576 /* Directives: Instruction set selection.  */
2577
2578 #ifdef OBJ_ELF
2579 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2580    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2581    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2582    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2583
2584 /* Create a new mapping symbol for the transition to STATE.  */
2585
2586 static void
2587 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2588 {
2589   symbolS * symbolP;
2590   const char * symname;
2591   int type;
2592
2593   switch (state)
2594     {
2595     case MAP_DATA:
2596       symname = "$d";
2597       type = BSF_NO_FLAGS;
2598       break;
2599     case MAP_ARM:
2600       symname = "$a";
2601       type = BSF_NO_FLAGS;
2602       break;
2603     case MAP_THUMB:
2604       symname = "$t";
2605       type = BSF_NO_FLAGS;
2606       break;
2607     default:
2608       abort ();
2609     }
2610
2611   symbolP = symbol_new (symname, now_seg, value, frag);
2612   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2613
2614   switch (state)
2615     {
2616     case MAP_ARM:
2617       THUMB_SET_FUNC (symbolP, 0);
2618       ARM_SET_THUMB (symbolP, 0);
2619       ARM_SET_INTERWORK (symbolP, support_interwork);
2620       break;
2621
2622     case MAP_THUMB:
2623       THUMB_SET_FUNC (symbolP, 1);
2624       ARM_SET_THUMB (symbolP, 1);
2625       ARM_SET_INTERWORK (symbolP, support_interwork);
2626       break;
2627
2628     case MAP_DATA:
2629     default:
2630       break;
2631     }
2632
2633   /* Save the mapping symbols for future reference.  Also check that
2634      we do not place two mapping symbols at the same offset within a
2635      frag.  We'll handle overlap between frags in
2636      check_mapping_symbols.
2637
2638      If .fill or other data filling directive generates zero sized data,
2639      the mapping symbol for the following code will have the same value
2640      as the one generated for the data filling directive.  In this case,
2641      we replace the old symbol with the new one at the same address.  */
2642   if (value == 0)
2643     {
2644       if (frag->tc_frag_data.first_map != NULL)
2645         {
2646           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2647           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2648         }
2649       frag->tc_frag_data.first_map = symbolP;
2650     }
2651   if (frag->tc_frag_data.last_map != NULL)
2652     {
2653       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2654       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2655         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2656     }
2657   frag->tc_frag_data.last_map = symbolP;
2658 }
2659
2660 /* We must sometimes convert a region marked as code to data during
2661    code alignment, if an odd number of bytes have to be padded.  The
2662    code mapping symbol is pushed to an aligned address.  */
2663
2664 static void
2665 insert_data_mapping_symbol (enum mstate state,
2666                             valueT value, fragS *frag, offsetT bytes)
2667 {
2668   /* If there was already a mapping symbol, remove it.  */
2669   if (frag->tc_frag_data.last_map != NULL
2670       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2671     {
2672       symbolS *symp = frag->tc_frag_data.last_map;
2673
2674       if (value == 0)
2675         {
2676           know (frag->tc_frag_data.first_map == symp);
2677           frag->tc_frag_data.first_map = NULL;
2678         }
2679       frag->tc_frag_data.last_map = NULL;
2680       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2681     }
2682
2683   make_mapping_symbol (MAP_DATA, value, frag);
2684   make_mapping_symbol (state, value + bytes, frag);
2685 }
2686
2687 static void mapping_state_2 (enum mstate state, int max_chars);
2688
2689 /* Set the mapping state to STATE.  Only call this when about to
2690    emit some STATE bytes to the file.  */
2691
2692 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2693 void
2694 mapping_state (enum mstate state)
2695 {
2696   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2697
2698   if (mapstate == state)
2699     /* The mapping symbol has already been emitted.
2700        There is nothing else to do.  */
2701     return;
2702
2703   if (state == MAP_ARM || state == MAP_THUMB)
2704     /*  PR gas/12931
2705         All ARM instructions require 4-byte alignment.
2706         (Almost) all Thumb instructions require 2-byte alignment.
2707
2708         When emitting instructions into any section, mark the section
2709         appropriately.
2710
2711         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2712         but themselves require 2-byte alignment; this applies to some
2713         PC- relative forms.  However, these cases will involve implicit
2714         literal pool generation or an explicit .align >=2, both of
2715         which will cause the section to me marked with sufficient
2716         alignment.  Thus, we don't handle those cases here.  */
2717     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2718
2719   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2720     /* This case will be evaluated later.  */
2721     return;
2722
2723   mapping_state_2 (state, 0);
2724 }
2725
2726 /* Same as mapping_state, but MAX_CHARS bytes have already been
2727    allocated.  Put the mapping symbol that far back.  */
2728
2729 static void
2730 mapping_state_2 (enum mstate state, int max_chars)
2731 {
2732   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2733
2734   if (!SEG_NORMAL (now_seg))
2735     return;
2736
2737   if (mapstate == state)
2738     /* The mapping symbol has already been emitted.
2739        There is nothing else to do.  */
2740     return;
2741
2742   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2743           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2744     {
2745       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2746       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2747
2748       if (add_symbol)
2749         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2750     }
2751
2752   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2753   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2754 }
2755 #undef TRANSITION
2756 #else
2757 #define mapping_state(x) ((void)0)
2758 #define mapping_state_2(x, y) ((void)0)
2759 #endif
2760
2761 /* Find the real, Thumb encoded start of a Thumb function.  */
2762
2763 #ifdef OBJ_COFF
2764 static symbolS *
2765 find_real_start (symbolS * symbolP)
2766 {
2767   char *       real_start;
2768   const char * name = S_GET_NAME (symbolP);
2769   symbolS *    new_target;
2770
2771   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2772 #define STUB_NAME ".real_start_of"
2773
2774   if (name == NULL)
2775     abort ();
2776
2777   /* The compiler may generate BL instructions to local labels because
2778      it needs to perform a branch to a far away location. These labels
2779      do not have a corresponding ".real_start_of" label.  We check
2780      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2781      the ".real_start_of" convention for nonlocal branches.  */
2782   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2783     return symbolP;
2784
2785   real_start = concat (STUB_NAME, name, NULL);
2786   new_target = symbol_find (real_start);
2787   free (real_start);
2788
2789   if (new_target == NULL)
2790     {
2791       as_warn (_("Failed to find real start of function: %s\n"), name);
2792       new_target = symbolP;
2793     }
2794
2795   return new_target;
2796 }
2797 #endif
2798
2799 static void
2800 opcode_select (int width)
2801 {
2802   switch (width)
2803     {
2804     case 16:
2805       if (! thumb_mode)
2806         {
2807           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2808             as_bad (_("selected processor does not support THUMB opcodes"));
2809
2810           thumb_mode = 1;
2811           /* No need to force the alignment, since we will have been
2812              coming from ARM mode, which is word-aligned.  */
2813           record_alignment (now_seg, 1);
2814         }
2815       break;
2816
2817     case 32:
2818       if (thumb_mode)
2819         {
2820           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2821             as_bad (_("selected processor does not support ARM opcodes"));
2822
2823           thumb_mode = 0;
2824
2825           if (!need_pass_2)
2826             frag_align (2, 0, 0);
2827
2828           record_alignment (now_seg, 1);
2829         }
2830       break;
2831
2832     default:
2833       as_bad (_("invalid instruction size selected (%d)"), width);
2834     }
2835 }
2836
2837 static void
2838 s_arm (int ignore ATTRIBUTE_UNUSED)
2839 {
2840   opcode_select (32);
2841   demand_empty_rest_of_line ();
2842 }
2843
2844 static void
2845 s_thumb (int ignore ATTRIBUTE_UNUSED)
2846 {
2847   opcode_select (16);
2848   demand_empty_rest_of_line ();
2849 }
2850
2851 static void
2852 s_code (int unused ATTRIBUTE_UNUSED)
2853 {
2854   int temp;
2855
2856   temp = get_absolute_expression ();
2857   switch (temp)
2858     {
2859     case 16:
2860     case 32:
2861       opcode_select (temp);
2862       break;
2863
2864     default:
2865       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2866     }
2867 }
2868
2869 static void
2870 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2871 {
2872   /* If we are not already in thumb mode go into it, EVEN if
2873      the target processor does not support thumb instructions.
2874      This is used by gcc/config/arm/lib1funcs.asm for example
2875      to compile interworking support functions even if the
2876      target processor should not support interworking.  */
2877   if (! thumb_mode)
2878     {
2879       thumb_mode = 2;
2880       record_alignment (now_seg, 1);
2881     }
2882
2883   demand_empty_rest_of_line ();
2884 }
2885
2886 static void
2887 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2888 {
2889   s_thumb (0);
2890
2891   /* The following label is the name/address of the start of a Thumb function.
2892      We need to know this for the interworking support.  */
2893   label_is_thumb_function_name = TRUE;
2894 }
2895
2896 /* Perform a .set directive, but also mark the alias as
2897    being a thumb function.  */
2898
2899 static void
2900 s_thumb_set (int equiv)
2901 {
2902   /* XXX the following is a duplicate of the code for s_set() in read.c
2903      We cannot just call that code as we need to get at the symbol that
2904      is created.  */
2905   char *    name;
2906   char      delim;
2907   char *    end_name;
2908   symbolS * symbolP;
2909
2910   /* Especial apologies for the random logic:
2911      This just grew, and could be parsed much more simply!
2912      Dean - in haste.  */
2913   delim     = get_symbol_name (& name);
2914   end_name  = input_line_pointer;
2915   (void) restore_line_pointer (delim);
2916
2917   if (*input_line_pointer != ',')
2918     {
2919       *end_name = 0;
2920       as_bad (_("expected comma after name \"%s\""), name);
2921       *end_name = delim;
2922       ignore_rest_of_line ();
2923       return;
2924     }
2925
2926   input_line_pointer++;
2927   *end_name = 0;
2928
2929   if (name[0] == '.' && name[1] == '\0')
2930     {
2931       /* XXX - this should not happen to .thumb_set.  */
2932       abort ();
2933     }
2934
2935   if ((symbolP = symbol_find (name)) == NULL
2936       && (symbolP = md_undefined_symbol (name)) == NULL)
2937     {
2938 #ifndef NO_LISTING
2939       /* When doing symbol listings, play games with dummy fragments living
2940          outside the normal fragment chain to record the file and line info
2941          for this symbol.  */
2942       if (listing & LISTING_SYMBOLS)
2943         {
2944           extern struct list_info_struct * listing_tail;
2945           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2946
2947           memset (dummy_frag, 0, sizeof (fragS));
2948           dummy_frag->fr_type = rs_fill;
2949           dummy_frag->line = listing_tail;
2950           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2951           dummy_frag->fr_symbol = symbolP;
2952         }
2953       else
2954 #endif
2955         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2956
2957 #ifdef OBJ_COFF
2958       /* "set" symbols are local unless otherwise specified.  */
2959       SF_SET_LOCAL (symbolP);
2960 #endif /* OBJ_COFF  */
2961     }                           /* Make a new symbol.  */
2962
2963   symbol_table_insert (symbolP);
2964
2965   * end_name = delim;
2966
2967   if (equiv
2968       && S_IS_DEFINED (symbolP)
2969       && S_GET_SEGMENT (symbolP) != reg_section)
2970     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2971
2972   pseudo_set (symbolP);
2973
2974   demand_empty_rest_of_line ();
2975
2976   /* XXX Now we come to the Thumb specific bit of code.  */
2977
2978   THUMB_SET_FUNC (symbolP, 1);
2979   ARM_SET_THUMB (symbolP, 1);
2980 #if defined OBJ_ELF || defined OBJ_COFF
2981   ARM_SET_INTERWORK (symbolP, support_interwork);
2982 #endif
2983 }
2984
2985 /* Directives: Mode selection.  */
2986
2987 /* .syntax [unified|divided] - choose the new unified syntax
2988    (same for Arm and Thumb encoding, modulo slight differences in what
2989    can be represented) or the old divergent syntax for each mode.  */
2990 static void
2991 s_syntax (int unused ATTRIBUTE_UNUSED)
2992 {
2993   char *name, delim;
2994
2995   delim = get_symbol_name (& name);
2996
2997   if (!strcasecmp (name, "unified"))
2998     unified_syntax = TRUE;
2999   else if (!strcasecmp (name, "divided"))
3000     unified_syntax = FALSE;
3001   else
3002     {
3003       as_bad (_("unrecognized syntax mode \"%s\""), name);
3004       return;
3005     }
3006   (void) restore_line_pointer (delim);
3007   demand_empty_rest_of_line ();
3008 }
3009
3010 /* Directives: sectioning and alignment.  */
3011
3012 static void
3013 s_bss (int ignore ATTRIBUTE_UNUSED)
3014 {
3015   /* We don't support putting frags in the BSS segment, we fake it by
3016      marking in_bss, then looking at s_skip for clues.  */
3017   subseg_set (bss_section, 0);
3018   demand_empty_rest_of_line ();
3019
3020 #ifdef md_elf_section_change_hook
3021   md_elf_section_change_hook ();
3022 #endif
3023 }
3024
3025 static void
3026 s_even (int ignore ATTRIBUTE_UNUSED)
3027 {
3028   /* Never make frag if expect extra pass.  */
3029   if (!need_pass_2)
3030     frag_align (1, 0, 0);
3031
3032   record_alignment (now_seg, 1);
3033
3034   demand_empty_rest_of_line ();
3035 }
3036
3037 /* Directives: CodeComposer Studio.  */
3038
3039 /*  .ref  (for CodeComposer Studio syntax only).  */
3040 static void
3041 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3042 {
3043   if (codecomposer_syntax)
3044     ignore_rest_of_line ();
3045   else
3046     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3047 }
3048
3049 /*  If name is not NULL, then it is used for marking the beginning of a
3050     function, whereas if it is NULL then it means the function end.  */
3051 static void
3052 asmfunc_debug (const char * name)
3053 {
3054   static const char * last_name = NULL;
3055
3056   if (name != NULL)
3057     {
3058       gas_assert (last_name == NULL);
3059       last_name = name;
3060
3061       if (debug_type == DEBUG_STABS)
3062          stabs_generate_asm_func (name, name);
3063     }
3064   else
3065     {
3066       gas_assert (last_name != NULL);
3067
3068       if (debug_type == DEBUG_STABS)
3069         stabs_generate_asm_endfunc (last_name, last_name);
3070
3071       last_name = NULL;
3072     }
3073 }
3074
3075 static void
3076 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3077 {
3078   if (codecomposer_syntax)
3079     {
3080       switch (asmfunc_state)
3081         {
3082         case OUTSIDE_ASMFUNC:
3083           asmfunc_state = WAITING_ASMFUNC_NAME;
3084           break;
3085
3086         case WAITING_ASMFUNC_NAME:
3087           as_bad (_(".asmfunc repeated."));
3088           break;
3089
3090         case WAITING_ENDASMFUNC:
3091           as_bad (_(".asmfunc without function."));
3092           break;
3093         }
3094       demand_empty_rest_of_line ();
3095     }
3096   else
3097     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3098 }
3099
3100 static void
3101 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3102 {
3103   if (codecomposer_syntax)
3104     {
3105       switch (asmfunc_state)
3106         {
3107         case OUTSIDE_ASMFUNC:
3108           as_bad (_(".endasmfunc without a .asmfunc."));
3109           break;
3110
3111         case WAITING_ASMFUNC_NAME:
3112           as_bad (_(".endasmfunc without function."));
3113           break;
3114
3115         case WAITING_ENDASMFUNC:
3116           asmfunc_state = OUTSIDE_ASMFUNC;
3117           asmfunc_debug (NULL);
3118           break;
3119         }
3120       demand_empty_rest_of_line ();
3121     }
3122   else
3123     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3124 }
3125
3126 static void
3127 s_ccs_def (int name)
3128 {
3129   if (codecomposer_syntax)
3130     s_globl (name);
3131   else
3132     as_bad (_(".def pseudo-op only available with -mccs flag."));
3133 }
3134
3135 /* Directives: Literal pools.  */
3136
3137 static literal_pool *
3138 find_literal_pool (void)
3139 {
3140   literal_pool * pool;
3141
3142   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3143     {
3144       if (pool->section == now_seg
3145           && pool->sub_section == now_subseg)
3146         break;
3147     }
3148
3149   return pool;
3150 }
3151
3152 static literal_pool *
3153 find_or_make_literal_pool (void)
3154 {
3155   /* Next literal pool ID number.  */
3156   static unsigned int latest_pool_num = 1;
3157   literal_pool *      pool;
3158
3159   pool = find_literal_pool ();
3160
3161   if (pool == NULL)
3162     {
3163       /* Create a new pool.  */
3164       pool = XNEW (literal_pool);
3165       if (! pool)
3166         return NULL;
3167
3168       pool->next_free_entry = 0;
3169       pool->section         = now_seg;
3170       pool->sub_section     = now_subseg;
3171       pool->next            = list_of_pools;
3172       pool->symbol          = NULL;
3173       pool->alignment       = 2;
3174
3175       /* Add it to the list.  */
3176       list_of_pools = pool;
3177     }
3178
3179   /* New pools, and emptied pools, will have a NULL symbol.  */
3180   if (pool->symbol == NULL)
3181     {
3182       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3183                                     (valueT) 0, &zero_address_frag);
3184       pool->id = latest_pool_num ++;
3185     }
3186
3187   /* Done.  */
3188   return pool;
3189 }
3190
3191 /* Add the literal in the global 'inst'
3192    structure to the relevant literal pool.  */
3193
3194 static int
3195 add_to_lit_pool (unsigned int nbytes)
3196 {
3197 #define PADDING_SLOT 0x1
3198 #define LIT_ENTRY_SIZE_MASK 0xFF
3199   literal_pool * pool;
3200   unsigned int entry, pool_size = 0;
3201   bfd_boolean padding_slot_p = FALSE;
3202   unsigned imm1 = 0;
3203   unsigned imm2 = 0;
3204
3205   if (nbytes == 8)
3206     {
3207       imm1 = inst.operands[1].imm;
3208       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3209                : inst.reloc.exp.X_unsigned ? 0
3210                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3211       if (target_big_endian)
3212         {
3213           imm1 = imm2;
3214           imm2 = inst.operands[1].imm;
3215         }
3216     }
3217
3218   pool = find_or_make_literal_pool ();
3219
3220   /* Check if this literal value is already in the pool.  */
3221   for (entry = 0; entry < pool->next_free_entry; entry ++)
3222     {
3223       if (nbytes == 4)
3224         {
3225           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3226               && (inst.reloc.exp.X_op == O_constant)
3227               && (pool->literals[entry].X_add_number
3228                   == inst.reloc.exp.X_add_number)
3229               && (pool->literals[entry].X_md == nbytes)
3230               && (pool->literals[entry].X_unsigned
3231                   == inst.reloc.exp.X_unsigned))
3232             break;
3233
3234           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3235               && (inst.reloc.exp.X_op == O_symbol)
3236               && (pool->literals[entry].X_add_number
3237                   == inst.reloc.exp.X_add_number)
3238               && (pool->literals[entry].X_add_symbol
3239                   == inst.reloc.exp.X_add_symbol)
3240               && (pool->literals[entry].X_op_symbol
3241                   == inst.reloc.exp.X_op_symbol)
3242               && (pool->literals[entry].X_md == nbytes))
3243             break;
3244         }
3245       else if ((nbytes == 8)
3246                && !(pool_size & 0x7)
3247                && ((entry + 1) != pool->next_free_entry)
3248                && (pool->literals[entry].X_op == O_constant)
3249                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3250                && (pool->literals[entry].X_unsigned
3251                    == inst.reloc.exp.X_unsigned)
3252                && (pool->literals[entry + 1].X_op == O_constant)
3253                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3254                && (pool->literals[entry + 1].X_unsigned
3255                    == inst.reloc.exp.X_unsigned))
3256         break;
3257
3258       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3259       if (padding_slot_p && (nbytes == 4))
3260         break;
3261
3262       pool_size += 4;
3263     }
3264
3265   /* Do we need to create a new entry?  */
3266   if (entry == pool->next_free_entry)
3267     {
3268       if (entry >= MAX_LITERAL_POOL_SIZE)
3269         {
3270           inst.error = _("literal pool overflow");
3271           return FAIL;
3272         }
3273
3274       if (nbytes == 8)
3275         {
3276           /* For 8-byte entries, we align to an 8-byte boundary,
3277              and split it into two 4-byte entries, because on 32-bit
3278              host, 8-byte constants are treated as big num, thus
3279              saved in "generic_bignum" which will be overwritten
3280              by later assignments.
3281
3282              We also need to make sure there is enough space for
3283              the split.
3284
3285              We also check to make sure the literal operand is a
3286              constant number.  */
3287           if (!(inst.reloc.exp.X_op == O_constant
3288                 || inst.reloc.exp.X_op == O_big))
3289             {
3290               inst.error = _("invalid type for literal pool");
3291               return FAIL;
3292             }
3293           else if (pool_size & 0x7)
3294             {
3295               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3296                 {
3297                   inst.error = _("literal pool overflow");
3298                   return FAIL;
3299                 }
3300
3301               pool->literals[entry] = inst.reloc.exp;
3302               pool->literals[entry].X_op = O_constant;
3303               pool->literals[entry].X_add_number = 0;
3304               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3305               pool->next_free_entry += 1;
3306               pool_size += 4;
3307             }
3308           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3309             {
3310               inst.error = _("literal pool overflow");
3311               return FAIL;
3312             }
3313
3314           pool->literals[entry] = inst.reloc.exp;
3315           pool->literals[entry].X_op = O_constant;
3316           pool->literals[entry].X_add_number = imm1;
3317           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3318           pool->literals[entry++].X_md = 4;
3319           pool->literals[entry] = inst.reloc.exp;
3320           pool->literals[entry].X_op = O_constant;
3321           pool->literals[entry].X_add_number = imm2;
3322           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3323           pool->literals[entry].X_md = 4;
3324           pool->alignment = 3;
3325           pool->next_free_entry += 1;
3326         }
3327       else
3328         {
3329           pool->literals[entry] = inst.reloc.exp;
3330           pool->literals[entry].X_md = 4;
3331         }
3332
3333 #ifdef OBJ_ELF
3334       /* PR ld/12974: Record the location of the first source line to reference
3335          this entry in the literal pool.  If it turns out during linking that the
3336          symbol does not exist we will be able to give an accurate line number for
3337          the (first use of the) missing reference.  */
3338       if (debug_type == DEBUG_DWARF2)
3339         dwarf2_where (pool->locs + entry);
3340 #endif
3341       pool->next_free_entry += 1;
3342     }
3343   else if (padding_slot_p)
3344     {
3345       pool->literals[entry] = inst.reloc.exp;
3346       pool->literals[entry].X_md = nbytes;
3347     }
3348
3349   inst.reloc.exp.X_op         = O_symbol;
3350   inst.reloc.exp.X_add_number = pool_size;
3351   inst.reloc.exp.X_add_symbol = pool->symbol;
3352
3353   return SUCCESS;
3354 }
3355
3356 bfd_boolean
3357 tc_start_label_without_colon (void)
3358 {
3359   bfd_boolean ret = TRUE;
3360
3361   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3362     {
3363       const char *label = input_line_pointer;
3364
3365       while (!is_end_of_line[(int) label[-1]])
3366         --label;
3367
3368       if (*label == '.')
3369         {
3370           as_bad (_("Invalid label '%s'"), label);
3371           ret = FALSE;
3372         }
3373
3374       asmfunc_debug (label);
3375
3376       asmfunc_state = WAITING_ENDASMFUNC;
3377     }
3378
3379   return ret;
3380 }
3381
3382 /* Can't use symbol_new here, so have to create a symbol and then at
3383    a later date assign it a value. That's what these functions do.  */
3384
3385 static void
3386 symbol_locate (symbolS *    symbolP,
3387                const char * name,       /* It is copied, the caller can modify.  */
3388                segT         segment,    /* Segment identifier (SEG_<something>).  */
3389                valueT       valu,       /* Symbol value.  */
3390                fragS *      frag)       /* Associated fragment.  */
3391 {
3392   size_t name_length;
3393   char * preserved_copy_of_name;
3394
3395   name_length = strlen (name) + 1;   /* +1 for \0.  */
3396   obstack_grow (&notes, name, name_length);
3397   preserved_copy_of_name = (char *) obstack_finish (&notes);
3398
3399 #ifdef tc_canonicalize_symbol_name
3400   preserved_copy_of_name =
3401     tc_canonicalize_symbol_name (preserved_copy_of_name);
3402 #endif
3403
3404   S_SET_NAME (symbolP, preserved_copy_of_name);
3405
3406   S_SET_SEGMENT (symbolP, segment);
3407   S_SET_VALUE (symbolP, valu);
3408   symbol_clear_list_pointers (symbolP);
3409
3410   symbol_set_frag (symbolP, frag);
3411
3412   /* Link to end of symbol chain.  */
3413   {
3414     extern int symbol_table_frozen;
3415
3416     if (symbol_table_frozen)
3417       abort ();
3418   }
3419
3420   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3421
3422   obj_symbol_new_hook (symbolP);
3423
3424 #ifdef tc_symbol_new_hook
3425   tc_symbol_new_hook (symbolP);
3426 #endif
3427
3428 #ifdef DEBUG_SYMS
3429   verify_symbol_chain (symbol_rootP, symbol_lastP);
3430 #endif /* DEBUG_SYMS  */
3431 }
3432
3433 static void
3434 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3435 {
3436   unsigned int entry;
3437   literal_pool * pool;
3438   char sym_name[20];
3439
3440   pool = find_literal_pool ();
3441   if (pool == NULL
3442       || pool->symbol == NULL
3443       || pool->next_free_entry == 0)
3444     return;
3445
3446   /* Align pool as you have word accesses.
3447      Only make a frag if we have to.  */
3448   if (!need_pass_2)
3449     frag_align (pool->alignment, 0, 0);
3450
3451   record_alignment (now_seg, 2);
3452
3453 #ifdef OBJ_ELF
3454   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3455   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3456 #endif
3457   sprintf (sym_name, "$$lit_\002%x", pool->id);
3458
3459   symbol_locate (pool->symbol, sym_name, now_seg,
3460                  (valueT) frag_now_fix (), frag_now);
3461   symbol_table_insert (pool->symbol);
3462
3463   ARM_SET_THUMB (pool->symbol, thumb_mode);
3464
3465 #if defined OBJ_COFF || defined OBJ_ELF
3466   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3467 #endif
3468
3469   for (entry = 0; entry < pool->next_free_entry; entry ++)
3470     {
3471 #ifdef OBJ_ELF
3472       if (debug_type == DEBUG_DWARF2)
3473         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3474 #endif
3475       /* First output the expression in the instruction to the pool.  */
3476       emit_expr (&(pool->literals[entry]),
3477                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3478     }
3479
3480   /* Mark the pool as empty.  */
3481   pool->next_free_entry = 0;
3482   pool->symbol = NULL;
3483 }
3484
3485 #ifdef OBJ_ELF
3486 /* Forward declarations for functions below, in the MD interface
3487    section.  */
3488 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3489 static valueT create_unwind_entry (int);
3490 static void start_unwind_section (const segT, int);
3491 static void add_unwind_opcode (valueT, int);
3492 static void flush_pending_unwind (void);
3493
3494 /* Directives: Data.  */
3495
3496 static void
3497 s_arm_elf_cons (int nbytes)
3498 {
3499   expressionS exp;
3500
3501 #ifdef md_flush_pending_output
3502   md_flush_pending_output ();
3503 #endif
3504
3505   if (is_it_end_of_statement ())
3506     {
3507       demand_empty_rest_of_line ();
3508       return;
3509     }
3510
3511 #ifdef md_cons_align
3512   md_cons_align (nbytes);
3513 #endif
3514
3515   mapping_state (MAP_DATA);
3516   do
3517     {
3518       int reloc;
3519       char *base = input_line_pointer;
3520
3521       expression (& exp);
3522
3523       if (exp.X_op != O_symbol)
3524         emit_expr (&exp, (unsigned int) nbytes);
3525       else
3526         {
3527           char *before_reloc = input_line_pointer;
3528           reloc = parse_reloc (&input_line_pointer);
3529           if (reloc == -1)
3530             {
3531               as_bad (_("unrecognized relocation suffix"));
3532               ignore_rest_of_line ();
3533               return;
3534             }
3535           else if (reloc == BFD_RELOC_UNUSED)
3536             emit_expr (&exp, (unsigned int) nbytes);
3537           else
3538             {
3539               reloc_howto_type *howto = (reloc_howto_type *)
3540                   bfd_reloc_type_lookup (stdoutput,
3541                                          (bfd_reloc_code_real_type) reloc);
3542               int size = bfd_get_reloc_size (howto);
3543
3544               if (reloc == BFD_RELOC_ARM_PLT32)
3545                 {
3546                   as_bad (_("(plt) is only valid on branch targets"));
3547                   reloc = BFD_RELOC_UNUSED;
3548                   size = 0;
3549                 }
3550
3551               if (size > nbytes)
3552                 as_bad (_("%s relocations do not fit in %d bytes"),
3553                         howto->name, nbytes);
3554               else
3555                 {
3556                   /* We've parsed an expression stopping at O_symbol.
3557                      But there may be more expression left now that we
3558                      have parsed the relocation marker.  Parse it again.
3559                      XXX Surely there is a cleaner way to do this.  */
3560                   char *p = input_line_pointer;
3561                   int offset;
3562                   char *save_buf = XNEWVEC (char, input_line_pointer - base);
3563
3564                   memcpy (save_buf, base, input_line_pointer - base);
3565                   memmove (base + (input_line_pointer - before_reloc),
3566                            base, before_reloc - base);
3567
3568                   input_line_pointer = base + (input_line_pointer-before_reloc);
3569                   expression (&exp);
3570                   memcpy (base, save_buf, p - base);
3571
3572                   offset = nbytes - size;
3573                   p = frag_more (nbytes);
3574                   memset (p, 0, nbytes);
3575                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3576                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3577                   free (save_buf);
3578                 }
3579             }
3580         }
3581     }
3582   while (*input_line_pointer++ == ',');
3583
3584   /* Put terminator back into stream.  */
3585   input_line_pointer --;
3586   demand_empty_rest_of_line ();
3587 }
3588
3589 /* Emit an expression containing a 32-bit thumb instruction.
3590    Implementation based on put_thumb32_insn.  */
3591
3592 static void
3593 emit_thumb32_expr (expressionS * exp)
3594 {
3595   expressionS exp_high = *exp;
3596
3597   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3598   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3599   exp->X_add_number &= 0xffff;
3600   emit_expr (exp, (unsigned int) THUMB_SIZE);
3601 }
3602
3603 /*  Guess the instruction size based on the opcode.  */
3604
3605 static int
3606 thumb_insn_size (int opcode)
3607 {
3608   if ((unsigned int) opcode < 0xe800u)
3609     return 2;
3610   else if ((unsigned int) opcode >= 0xe8000000u)
3611     return 4;
3612   else
3613     return 0;
3614 }
3615
3616 static bfd_boolean
3617 emit_insn (expressionS *exp, int nbytes)
3618 {
3619   int size = 0;
3620
3621   if (exp->X_op == O_constant)
3622     {
3623       size = nbytes;
3624
3625       if (size == 0)
3626         size = thumb_insn_size (exp->X_add_number);
3627
3628       if (size != 0)
3629         {
3630           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3631             {
3632               as_bad (_(".inst.n operand too big. "\
3633                         "Use .inst.w instead"));
3634               size = 0;
3635             }
3636           else
3637             {
3638               if (now_it.state == AUTOMATIC_IT_BLOCK)
3639                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3640               else
3641                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3642
3643               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3644                 emit_thumb32_expr (exp);
3645               else
3646                 emit_expr (exp, (unsigned int) size);
3647
3648               it_fsm_post_encode ();
3649             }
3650         }
3651       else
3652         as_bad (_("cannot determine Thumb instruction size. "   \
3653                   "Use .inst.n/.inst.w instead"));
3654     }
3655   else
3656     as_bad (_("constant expression required"));
3657
3658   return (size != 0);
3659 }
3660
3661 /* Like s_arm_elf_cons but do not use md_cons_align and
3662    set the mapping state to MAP_ARM/MAP_THUMB.  */
3663
3664 static void
3665 s_arm_elf_inst (int nbytes)
3666 {
3667   if (is_it_end_of_statement ())
3668     {
3669       demand_empty_rest_of_line ();
3670       return;
3671     }
3672
3673   /* Calling mapping_state () here will not change ARM/THUMB,
3674      but will ensure not to be in DATA state.  */
3675
3676   if (thumb_mode)
3677     mapping_state (MAP_THUMB);
3678   else
3679     {
3680       if (nbytes != 0)
3681         {
3682           as_bad (_("width suffixes are invalid in ARM mode"));
3683           ignore_rest_of_line ();
3684           return;
3685         }
3686
3687       nbytes = 4;
3688
3689       mapping_state (MAP_ARM);
3690     }
3691
3692   do
3693     {
3694       expressionS exp;
3695
3696       expression (& exp);
3697
3698       if (! emit_insn (& exp, nbytes))
3699         {
3700           ignore_rest_of_line ();
3701           return;
3702         }
3703     }
3704   while (*input_line_pointer++ == ',');
3705
3706   /* Put terminator back into stream.  */
3707   input_line_pointer --;
3708   demand_empty_rest_of_line ();
3709 }
3710
3711 /* Parse a .rel31 directive.  */
3712
3713 static void
3714 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3715 {
3716   expressionS exp;
3717   char *p;
3718   valueT highbit;
3719
3720   highbit = 0;
3721   if (*input_line_pointer == '1')
3722     highbit = 0x80000000;
3723   else if (*input_line_pointer != '0')
3724     as_bad (_("expected 0 or 1"));
3725
3726   input_line_pointer++;
3727   if (*input_line_pointer != ',')
3728     as_bad (_("missing comma"));
3729   input_line_pointer++;
3730
3731 #ifdef md_flush_pending_output
3732   md_flush_pending_output ();
3733 #endif
3734
3735 #ifdef md_cons_align
3736   md_cons_align (4);
3737 #endif
3738
3739   mapping_state (MAP_DATA);
3740
3741   expression (&exp);
3742
3743   p = frag_more (4);
3744   md_number_to_chars (p, highbit, 4);
3745   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3746                BFD_RELOC_ARM_PREL31);
3747
3748   demand_empty_rest_of_line ();
3749 }
3750
3751 /* Directives: AEABI stack-unwind tables.  */
3752
3753 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3754
3755 static void
3756 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3757 {
3758   demand_empty_rest_of_line ();
3759   if (unwind.proc_start)
3760     {
3761       as_bad (_("duplicate .fnstart directive"));
3762       return;
3763     }
3764
3765   /* Mark the start of the function.  */
3766   unwind.proc_start = expr_build_dot ();
3767
3768   /* Reset the rest of the unwind info.  */
3769   unwind.opcode_count = 0;
3770   unwind.table_entry = NULL;
3771   unwind.personality_routine = NULL;
3772   unwind.personality_index = -1;
3773   unwind.frame_size = 0;
3774   unwind.fp_offset = 0;
3775   unwind.fp_reg = REG_SP;
3776   unwind.fp_used = 0;
3777   unwind.sp_restored = 0;
3778 }
3779
3780
3781 /* Parse a handlerdata directive.  Creates the exception handling table entry
3782    for the function.  */
3783
3784 static void
3785 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3786 {
3787   demand_empty_rest_of_line ();
3788   if (!unwind.proc_start)
3789     as_bad (MISSING_FNSTART);
3790
3791   if (unwind.table_entry)
3792     as_bad (_("duplicate .handlerdata directive"));
3793
3794   create_unwind_entry (1);
3795 }
3796
3797 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3798
3799 static void
3800 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3801 {
3802   long where;
3803   char *ptr;
3804   valueT val;
3805   unsigned int marked_pr_dependency;
3806
3807   demand_empty_rest_of_line ();
3808
3809   if (!unwind.proc_start)
3810     {
3811       as_bad (_(".fnend directive without .fnstart"));
3812       return;
3813     }
3814
3815   /* Add eh table entry.  */
3816   if (unwind.table_entry == NULL)
3817     val = create_unwind_entry (0);
3818   else
3819     val = 0;
3820
3821   /* Add index table entry.  This is two words.  */
3822   start_unwind_section (unwind.saved_seg, 1);
3823   frag_align (2, 0, 0);
3824   record_alignment (now_seg, 2);
3825
3826   ptr = frag_more (8);
3827   memset (ptr, 0, 8);
3828   where = frag_now_fix () - 8;
3829
3830   /* Self relative offset of the function start.  */
3831   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3832            BFD_RELOC_ARM_PREL31);
3833
3834   /* Indicate dependency on EHABI-defined personality routines to the
3835      linker, if it hasn't been done already.  */
3836   marked_pr_dependency
3837     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3838   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3839       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3840     {
3841       static const char *const name[] =
3842         {
3843           "__aeabi_unwind_cpp_pr0",
3844           "__aeabi_unwind_cpp_pr1",
3845           "__aeabi_unwind_cpp_pr2"
3846         };
3847       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3848       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3849       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3850         |= 1 << unwind.personality_index;
3851     }
3852
3853   if (val)
3854     /* Inline exception table entry.  */
3855     md_number_to_chars (ptr + 4, val, 4);
3856   else
3857     /* Self relative offset of the table entry.  */
3858     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3859              BFD_RELOC_ARM_PREL31);
3860
3861   /* Restore the original section.  */
3862   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3863
3864   unwind.proc_start = NULL;
3865 }
3866
3867
3868 /* Parse an unwind_cantunwind directive.  */
3869
3870 static void
3871 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3872 {
3873   demand_empty_rest_of_line ();
3874   if (!unwind.proc_start)
3875     as_bad (MISSING_FNSTART);
3876
3877   if (unwind.personality_routine || unwind.personality_index != -1)
3878     as_bad (_("personality routine specified for cantunwind frame"));
3879
3880   unwind.personality_index = -2;
3881 }
3882
3883
3884 /* Parse a personalityindex directive.  */
3885
3886 static void
3887 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3888 {
3889   expressionS exp;
3890
3891   if (!unwind.proc_start)
3892     as_bad (MISSING_FNSTART);
3893
3894   if (unwind.personality_routine || unwind.personality_index != -1)
3895     as_bad (_("duplicate .personalityindex directive"));
3896
3897   expression (&exp);
3898
3899   if (exp.X_op != O_constant
3900       || exp.X_add_number < 0 || exp.X_add_number > 15)
3901     {
3902       as_bad (_("bad personality routine number"));
3903       ignore_rest_of_line ();
3904       return;
3905     }
3906
3907   unwind.personality_index = exp.X_add_number;
3908
3909   demand_empty_rest_of_line ();
3910 }
3911
3912
3913 /* Parse a personality directive.  */
3914
3915 static void
3916 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3917 {
3918   char *name, *p, c;
3919
3920   if (!unwind.proc_start)
3921     as_bad (MISSING_FNSTART);
3922
3923   if (unwind.personality_routine || unwind.personality_index != -1)
3924     as_bad (_("duplicate .personality directive"));
3925
3926   c = get_symbol_name (& name);
3927   p = input_line_pointer;
3928   if (c == '"')
3929     ++ input_line_pointer;
3930   unwind.personality_routine = symbol_find_or_make (name);
3931   *p = c;
3932   demand_empty_rest_of_line ();
3933 }
3934
3935
3936 /* Parse a directive saving core registers.  */
3937
3938 static void
3939 s_arm_unwind_save_core (void)
3940 {
3941   valueT op;
3942   long range;
3943   int n;
3944
3945   range = parse_reg_list (&input_line_pointer);
3946   if (range == FAIL)
3947     {
3948       as_bad (_("expected register list"));
3949       ignore_rest_of_line ();
3950       return;
3951     }
3952
3953   demand_empty_rest_of_line ();
3954
3955   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3956      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3957      ip because it is clobbered by calls.  */
3958   if (unwind.sp_restored && unwind.fp_reg == 12
3959       && (range & 0x3000) == 0x1000)
3960     {
3961       unwind.opcode_count--;
3962       unwind.sp_restored = 0;
3963       range = (range | 0x2000) & ~0x1000;
3964       unwind.pending_offset = 0;
3965     }
3966
3967   /* Pop r4-r15.  */
3968   if (range & 0xfff0)
3969     {
3970       /* See if we can use the short opcodes.  These pop a block of up to 8
3971          registers starting with r4, plus maybe r14.  */
3972       for (n = 0; n < 8; n++)
3973         {
3974           /* Break at the first non-saved register.      */
3975           if ((range & (1 << (n + 4))) == 0)
3976             break;
3977         }
3978       /* See if there are any other bits set.  */
3979       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3980         {
3981           /* Use the long form.  */
3982           op = 0x8000 | ((range >> 4) & 0xfff);
3983           add_unwind_opcode (op, 2);
3984         }
3985       else
3986         {
3987           /* Use the short form.  */
3988           if (range & 0x4000)
3989             op = 0xa8; /* Pop r14.      */
3990           else
3991             op = 0xa0; /* Do not pop r14.  */
3992           op |= (n - 1);
3993           add_unwind_opcode (op, 1);
3994         }
3995     }
3996
3997   /* Pop r0-r3.  */
3998   if (range & 0xf)
3999     {
4000       op = 0xb100 | (range & 0xf);
4001       add_unwind_opcode (op, 2);
4002     }
4003
4004   /* Record the number of bytes pushed.  */
4005   for (n = 0; n < 16; n++)
4006     {
4007       if (range & (1 << n))
4008         unwind.frame_size += 4;
4009     }
4010 }
4011
4012
4013 /* Parse a directive saving FPA registers.  */
4014
4015 static void
4016 s_arm_unwind_save_fpa (int reg)
4017 {
4018   expressionS exp;
4019   int num_regs;
4020   valueT op;
4021
4022   /* Get Number of registers to transfer.  */
4023   if (skip_past_comma (&input_line_pointer) != FAIL)
4024     expression (&exp);
4025   else
4026     exp.X_op = O_illegal;
4027
4028   if (exp.X_op != O_constant)
4029     {
4030       as_bad (_("expected , <constant>"));
4031       ignore_rest_of_line ();
4032       return;
4033     }
4034
4035   num_regs = exp.X_add_number;
4036
4037   if (num_regs < 1 || num_regs > 4)
4038     {
4039       as_bad (_("number of registers must be in the range [1:4]"));
4040       ignore_rest_of_line ();
4041       return;
4042     }
4043
4044   demand_empty_rest_of_line ();
4045
4046   if (reg == 4)
4047     {
4048       /* Short form.  */
4049       op = 0xb4 | (num_regs - 1);
4050       add_unwind_opcode (op, 1);
4051     }
4052   else
4053     {
4054       /* Long form.  */
4055       op = 0xc800 | (reg << 4) | (num_regs - 1);
4056       add_unwind_opcode (op, 2);
4057     }
4058   unwind.frame_size += num_regs * 12;
4059 }
4060
4061
4062 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4063
4064 static void
4065 s_arm_unwind_save_vfp_armv6 (void)
4066 {
4067   int count;
4068   unsigned int start;
4069   valueT op;
4070   int num_vfpv3_regs = 0;
4071   int num_regs_below_16;
4072
4073   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
4074   if (count == FAIL)
4075     {
4076       as_bad (_("expected register list"));
4077       ignore_rest_of_line ();
4078       return;
4079     }
4080
4081   demand_empty_rest_of_line ();
4082
4083   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4084      than FSTMX/FLDMX-style ones).  */
4085
4086   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4087   if (start >= 16)
4088     num_vfpv3_regs = count;
4089   else if (start + count > 16)
4090     num_vfpv3_regs = start + count - 16;
4091
4092   if (num_vfpv3_regs > 0)
4093     {
4094       int start_offset = start > 16 ? start - 16 : 0;
4095       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4096       add_unwind_opcode (op, 2);
4097     }
4098
4099   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4100   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4101   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4102   if (num_regs_below_16 > 0)
4103     {
4104       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4105       add_unwind_opcode (op, 2);
4106     }
4107
4108   unwind.frame_size += count * 8;
4109 }
4110
4111
4112 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4113
4114 static void
4115 s_arm_unwind_save_vfp (void)
4116 {
4117   int count;
4118   unsigned int reg;
4119   valueT op;
4120
4121   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4122   if (count == FAIL)
4123     {
4124       as_bad (_("expected register list"));
4125       ignore_rest_of_line ();
4126       return;
4127     }
4128
4129   demand_empty_rest_of_line ();
4130
4131   if (reg == 8)
4132     {
4133       /* Short form.  */
4134       op = 0xb8 | (count - 1);
4135       add_unwind_opcode (op, 1);
4136     }
4137   else
4138     {
4139       /* Long form.  */
4140       op = 0xb300 | (reg << 4) | (count - 1);
4141       add_unwind_opcode (op, 2);
4142     }
4143   unwind.frame_size += count * 8 + 4;
4144 }
4145
4146
4147 /* Parse a directive saving iWMMXt data registers.  */
4148
4149 static void
4150 s_arm_unwind_save_mmxwr (void)
4151 {
4152   int reg;
4153   int hi_reg;
4154   int i;
4155   unsigned mask = 0;
4156   valueT op;
4157
4158   if (*input_line_pointer == '{')
4159     input_line_pointer++;
4160
4161   do
4162     {
4163       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4164
4165       if (reg == FAIL)
4166         {
4167           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4168           goto error;
4169         }
4170
4171       if (mask >> reg)
4172         as_tsktsk (_("register list not in ascending order"));
4173       mask |= 1 << reg;
4174
4175       if (*input_line_pointer == '-')
4176         {
4177           input_line_pointer++;
4178           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4179           if (hi_reg == FAIL)
4180             {
4181               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4182               goto error;
4183             }
4184           else if (reg >= hi_reg)
4185             {
4186               as_bad (_("bad register range"));
4187               goto error;
4188             }
4189           for (; reg < hi_reg; reg++)
4190             mask |= 1 << reg;
4191         }
4192     }
4193   while (skip_past_comma (&input_line_pointer) != FAIL);
4194
4195   skip_past_char (&input_line_pointer, '}');
4196
4197   demand_empty_rest_of_line ();
4198
4199   /* Generate any deferred opcodes because we're going to be looking at
4200      the list.  */
4201   flush_pending_unwind ();
4202
4203   for (i = 0; i < 16; i++)
4204     {
4205       if (mask & (1 << i))
4206         unwind.frame_size += 8;
4207     }
4208
4209   /* Attempt to combine with a previous opcode.  We do this because gcc
4210      likes to output separate unwind directives for a single block of
4211      registers.  */
4212   if (unwind.opcode_count > 0)
4213     {
4214       i = unwind.opcodes[unwind.opcode_count - 1];
4215       if ((i & 0xf8) == 0xc0)
4216         {
4217           i &= 7;
4218           /* Only merge if the blocks are contiguous.  */
4219           if (i < 6)
4220             {
4221               if ((mask & 0xfe00) == (1 << 9))
4222                 {
4223                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4224                   unwind.opcode_count--;
4225                 }
4226             }
4227           else if (i == 6 && unwind.opcode_count >= 2)
4228             {
4229               i = unwind.opcodes[unwind.opcode_count - 2];
4230               reg = i >> 4;
4231               i &= 0xf;
4232
4233               op = 0xffff << (reg - 1);
4234               if (reg > 0
4235                   && ((mask & op) == (1u << (reg - 1))))
4236                 {
4237                   op = (1 << (reg + i + 1)) - 1;
4238                   op &= ~((1 << reg) - 1);
4239                   mask |= op;
4240                   unwind.opcode_count -= 2;
4241                 }
4242             }
4243         }
4244     }
4245
4246   hi_reg = 15;
4247   /* We want to generate opcodes in the order the registers have been
4248      saved, ie. descending order.  */
4249   for (reg = 15; reg >= -1; reg--)
4250     {
4251       /* Save registers in blocks.  */
4252       if (reg < 0
4253           || !(mask & (1 << reg)))
4254         {
4255           /* We found an unsaved reg.  Generate opcodes to save the
4256              preceding block.   */
4257           if (reg != hi_reg)
4258             {
4259               if (reg == 9)
4260                 {
4261                   /* Short form.  */
4262                   op = 0xc0 | (hi_reg - 10);
4263                   add_unwind_opcode (op, 1);
4264                 }
4265               else
4266                 {
4267                   /* Long form.  */
4268                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4269                   add_unwind_opcode (op, 2);
4270                 }
4271             }
4272           hi_reg = reg - 1;
4273         }
4274     }
4275
4276   return;
4277 error:
4278   ignore_rest_of_line ();
4279 }
4280
4281 static void
4282 s_arm_unwind_save_mmxwcg (void)
4283 {
4284   int reg;
4285   int hi_reg;
4286   unsigned mask = 0;
4287   valueT op;
4288
4289   if (*input_line_pointer == '{')
4290     input_line_pointer++;
4291
4292   skip_whitespace (input_line_pointer);
4293
4294   do
4295     {
4296       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4297
4298       if (reg == FAIL)
4299         {
4300           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4301           goto error;
4302         }
4303
4304       reg -= 8;
4305       if (mask >> reg)
4306         as_tsktsk (_("register list not in ascending order"));
4307       mask |= 1 << reg;
4308
4309       if (*input_line_pointer == '-')
4310         {
4311           input_line_pointer++;
4312           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4313           if (hi_reg == FAIL)
4314             {
4315               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4316               goto error;
4317             }
4318           else if (reg >= hi_reg)
4319             {
4320               as_bad (_("bad register range"));
4321               goto error;
4322             }
4323           for (; reg < hi_reg; reg++)
4324             mask |= 1 << reg;
4325         }
4326     }
4327   while (skip_past_comma (&input_line_pointer) != FAIL);
4328
4329   skip_past_char (&input_line_pointer, '}');
4330
4331   demand_empty_rest_of_line ();
4332
4333   /* Generate any deferred opcodes because we're going to be looking at
4334      the list.  */
4335   flush_pending_unwind ();
4336
4337   for (reg = 0; reg < 16; reg++)
4338     {
4339       if (mask & (1 << reg))
4340         unwind.frame_size += 4;
4341     }
4342   op = 0xc700 | mask;
4343   add_unwind_opcode (op, 2);
4344   return;
4345 error:
4346   ignore_rest_of_line ();
4347 }
4348
4349
4350 /* Parse an unwind_save directive.
4351    If the argument is non-zero, this is a .vsave directive.  */
4352
4353 static void
4354 s_arm_unwind_save (int arch_v6)
4355 {
4356   char *peek;
4357   struct reg_entry *reg;
4358   bfd_boolean had_brace = FALSE;
4359
4360   if (!unwind.proc_start)
4361     as_bad (MISSING_FNSTART);
4362
4363   /* Figure out what sort of save we have.  */
4364   peek = input_line_pointer;
4365
4366   if (*peek == '{')
4367     {
4368       had_brace = TRUE;
4369       peek++;
4370     }
4371
4372   reg = arm_reg_parse_multi (&peek);
4373
4374   if (!reg)
4375     {
4376       as_bad (_("register expected"));
4377       ignore_rest_of_line ();
4378       return;
4379     }
4380
4381   switch (reg->type)
4382     {
4383     case REG_TYPE_FN:
4384       if (had_brace)
4385         {
4386           as_bad (_("FPA .unwind_save does not take a register list"));
4387           ignore_rest_of_line ();
4388           return;
4389         }
4390       input_line_pointer = peek;
4391       s_arm_unwind_save_fpa (reg->number);
4392       return;
4393
4394     case REG_TYPE_RN:
4395       s_arm_unwind_save_core ();
4396       return;
4397
4398     case REG_TYPE_VFD:
4399       if (arch_v6)
4400         s_arm_unwind_save_vfp_armv6 ();
4401       else
4402         s_arm_unwind_save_vfp ();
4403       return;
4404
4405     case REG_TYPE_MMXWR:
4406       s_arm_unwind_save_mmxwr ();
4407       return;
4408
4409     case REG_TYPE_MMXWCG:
4410       s_arm_unwind_save_mmxwcg ();
4411       return;
4412
4413     default:
4414       as_bad (_(".unwind_save does not support this kind of register"));
4415       ignore_rest_of_line ();
4416     }
4417 }
4418
4419
4420 /* Parse an unwind_movsp directive.  */
4421
4422 static void
4423 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4424 {
4425   int reg;
4426   valueT op;
4427   int offset;
4428
4429   if (!unwind.proc_start)
4430     as_bad (MISSING_FNSTART);
4431
4432   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4433   if (reg == FAIL)
4434     {
4435       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4436       ignore_rest_of_line ();
4437       return;
4438     }
4439
4440   /* Optional constant.  */
4441   if (skip_past_comma (&input_line_pointer) != FAIL)
4442     {
4443       if (immediate_for_directive (&offset) == FAIL)
4444         return;
4445     }
4446   else
4447     offset = 0;
4448
4449   demand_empty_rest_of_line ();
4450
4451   if (reg == REG_SP || reg == REG_PC)
4452     {
4453       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4454       return;
4455     }
4456
4457   if (unwind.fp_reg != REG_SP)
4458     as_bad (_("unexpected .unwind_movsp directive"));
4459
4460   /* Generate opcode to restore the value.  */
4461   op = 0x90 | reg;
4462   add_unwind_opcode (op, 1);
4463
4464   /* Record the information for later.  */
4465   unwind.fp_reg = reg;
4466   unwind.fp_offset = unwind.frame_size - offset;
4467   unwind.sp_restored = 1;
4468 }
4469
4470 /* Parse an unwind_pad directive.  */
4471
4472 static void
4473 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4474 {
4475   int offset;
4476
4477   if (!unwind.proc_start)
4478     as_bad (MISSING_FNSTART);
4479
4480   if (immediate_for_directive (&offset) == FAIL)
4481     return;
4482
4483   if (offset & 3)
4484     {
4485       as_bad (_("stack increment must be multiple of 4"));
4486       ignore_rest_of_line ();
4487       return;
4488     }
4489
4490   /* Don't generate any opcodes, just record the details for later.  */
4491   unwind.frame_size += offset;
4492   unwind.pending_offset += offset;
4493
4494   demand_empty_rest_of_line ();
4495 }
4496
4497 /* Parse an unwind_setfp directive.  */
4498
4499 static void
4500 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4501 {
4502   int sp_reg;
4503   int fp_reg;
4504   int offset;
4505
4506   if (!unwind.proc_start)
4507     as_bad (MISSING_FNSTART);
4508
4509   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4510   if (skip_past_comma (&input_line_pointer) == FAIL)
4511     sp_reg = FAIL;
4512   else
4513     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4514
4515   if (fp_reg == FAIL || sp_reg == FAIL)
4516     {
4517       as_bad (_("expected <reg>, <reg>"));
4518       ignore_rest_of_line ();
4519       return;
4520     }
4521
4522   /* Optional constant.  */
4523   if (skip_past_comma (&input_line_pointer) != FAIL)
4524     {
4525       if (immediate_for_directive (&offset) == FAIL)
4526         return;
4527     }
4528   else
4529     offset = 0;
4530
4531   demand_empty_rest_of_line ();
4532
4533   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4534     {
4535       as_bad (_("register must be either sp or set by a previous"
4536                 "unwind_movsp directive"));
4537       return;
4538     }
4539
4540   /* Don't generate any opcodes, just record the information for later.  */
4541   unwind.fp_reg = fp_reg;
4542   unwind.fp_used = 1;
4543   if (sp_reg == REG_SP)
4544     unwind.fp_offset = unwind.frame_size - offset;
4545   else
4546     unwind.fp_offset -= offset;
4547 }
4548
4549 /* Parse an unwind_raw directive.  */
4550
4551 static void
4552 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4553 {
4554   expressionS exp;
4555   /* This is an arbitrary limit.         */
4556   unsigned char op[16];
4557   int count;
4558
4559   if (!unwind.proc_start)
4560     as_bad (MISSING_FNSTART);
4561
4562   expression (&exp);
4563   if (exp.X_op == O_constant
4564       && skip_past_comma (&input_line_pointer) != FAIL)
4565     {
4566       unwind.frame_size += exp.X_add_number;
4567       expression (&exp);
4568     }
4569   else
4570     exp.X_op = O_illegal;
4571
4572   if (exp.X_op != O_constant)
4573     {
4574       as_bad (_("expected <offset>, <opcode>"));
4575       ignore_rest_of_line ();
4576       return;
4577     }
4578
4579   count = 0;
4580
4581   /* Parse the opcode.  */
4582   for (;;)
4583     {
4584       if (count >= 16)
4585         {
4586           as_bad (_("unwind opcode too long"));
4587           ignore_rest_of_line ();
4588         }
4589       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4590         {
4591           as_bad (_("invalid unwind opcode"));
4592           ignore_rest_of_line ();
4593           return;
4594         }
4595       op[count++] = exp.X_add_number;
4596
4597       /* Parse the next byte.  */
4598       if (skip_past_comma (&input_line_pointer) == FAIL)
4599         break;
4600
4601       expression (&exp);
4602     }
4603
4604   /* Add the opcode bytes in reverse order.  */
4605   while (count--)
4606     add_unwind_opcode (op[count], 1);
4607
4608   demand_empty_rest_of_line ();
4609 }
4610
4611
4612 /* Parse a .eabi_attribute directive.  */
4613
4614 static void
4615 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4616 {
4617   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4618
4619   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4620     attributes_set_explicitly[tag] = 1;
4621 }
4622
4623 /* Emit a tls fix for the symbol.  */
4624
4625 static void
4626 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4627 {
4628   char *p;
4629   expressionS exp;
4630 #ifdef md_flush_pending_output
4631   md_flush_pending_output ();
4632 #endif
4633
4634 #ifdef md_cons_align
4635   md_cons_align (4);
4636 #endif
4637
4638   /* Since we're just labelling the code, there's no need to define a
4639      mapping symbol.  */
4640   expression (&exp);
4641   p = obstack_next_free (&frchain_now->frch_obstack);
4642   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4643                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4644                : BFD_RELOC_ARM_TLS_DESCSEQ);
4645 }
4646 #endif /* OBJ_ELF */
4647
4648 static void s_arm_arch (int);
4649 static void s_arm_object_arch (int);
4650 static void s_arm_cpu (int);
4651 static void s_arm_fpu (int);
4652 static void s_arm_arch_extension (int);
4653
4654 #ifdef TE_PE
4655
4656 static void
4657 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4658 {
4659   expressionS exp;
4660
4661   do
4662     {
4663       expression (&exp);
4664       if (exp.X_op == O_symbol)
4665         exp.X_op = O_secrel;
4666
4667       emit_expr (&exp, 4);
4668     }
4669   while (*input_line_pointer++ == ',');
4670
4671   input_line_pointer--;
4672   demand_empty_rest_of_line ();
4673 }
4674 #endif /* TE_PE */
4675
4676 /* This table describes all the machine specific pseudo-ops the assembler
4677    has to support.  The fields are:
4678      pseudo-op name without dot
4679      function to call to execute this pseudo-op
4680      Integer arg to pass to the function.  */
4681
4682 const pseudo_typeS md_pseudo_table[] =
4683 {
4684   /* Never called because '.req' does not start a line.  */
4685   { "req",         s_req,         0 },
4686   /* Following two are likewise never called.  */
4687   { "dn",          s_dn,          0 },
4688   { "qn",          s_qn,          0 },
4689   { "unreq",       s_unreq,       0 },
4690   { "bss",         s_bss,         0 },
4691   { "align",       s_align_ptwo,  2 },
4692   { "arm",         s_arm,         0 },
4693   { "thumb",       s_thumb,       0 },
4694   { "code",        s_code,        0 },
4695   { "force_thumb", s_force_thumb, 0 },
4696   { "thumb_func",  s_thumb_func,  0 },
4697   { "thumb_set",   s_thumb_set,   0 },
4698   { "even",        s_even,        0 },
4699   { "ltorg",       s_ltorg,       0 },
4700   { "pool",        s_ltorg,       0 },
4701   { "syntax",      s_syntax,      0 },
4702   { "cpu",         s_arm_cpu,     0 },
4703   { "arch",        s_arm_arch,    0 },
4704   { "object_arch", s_arm_object_arch,   0 },
4705   { "fpu",         s_arm_fpu,     0 },
4706   { "arch_extension", s_arm_arch_extension, 0 },
4707 #ifdef OBJ_ELF
4708   { "word",             s_arm_elf_cons, 4 },
4709   { "long",             s_arm_elf_cons, 4 },
4710   { "inst.n",           s_arm_elf_inst, 2 },
4711   { "inst.w",           s_arm_elf_inst, 4 },
4712   { "inst",             s_arm_elf_inst, 0 },
4713   { "rel31",            s_arm_rel31,      0 },
4714   { "fnstart",          s_arm_unwind_fnstart,   0 },
4715   { "fnend",            s_arm_unwind_fnend,     0 },
4716   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4717   { "personality",      s_arm_unwind_personality, 0 },
4718   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4719   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4720   { "save",             s_arm_unwind_save,      0 },
4721   { "vsave",            s_arm_unwind_save,      1 },
4722   { "movsp",            s_arm_unwind_movsp,     0 },
4723   { "pad",              s_arm_unwind_pad,       0 },
4724   { "setfp",            s_arm_unwind_setfp,     0 },
4725   { "unwind_raw",       s_arm_unwind_raw,       0 },
4726   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4727   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4728 #else
4729   { "word",        cons, 4},
4730
4731   /* These are used for dwarf.  */
4732   {"2byte", cons, 2},
4733   {"4byte", cons, 4},
4734   {"8byte", cons, 8},
4735   /* These are used for dwarf2.  */
4736   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4737   { "loc",  dwarf2_directive_loc,  0 },
4738   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4739 #endif
4740   { "extend",      float_cons, 'x' },
4741   { "ldouble",     float_cons, 'x' },
4742   { "packed",      float_cons, 'p' },
4743 #ifdef TE_PE
4744   {"secrel32", pe_directive_secrel, 0},
4745 #endif
4746
4747   /* These are for compatibility with CodeComposer Studio.  */
4748   {"ref",          s_ccs_ref,        0},
4749   {"def",          s_ccs_def,        0},
4750   {"asmfunc",      s_ccs_asmfunc,    0},
4751   {"endasmfunc",   s_ccs_endasmfunc, 0},
4752
4753   { 0, 0, 0 }
4754 };
4755 \f
4756 /* Parser functions used exclusively in instruction operands.  */
4757
4758 /* Generic immediate-value read function for use in insn parsing.
4759    STR points to the beginning of the immediate (the leading #);
4760    VAL receives the value; if the value is outside [MIN, MAX]
4761    issue an error.  PREFIX_OPT is true if the immediate prefix is
4762    optional.  */
4763
4764 static int
4765 parse_immediate (char **str, int *val, int min, int max,
4766                  bfd_boolean prefix_opt)
4767 {
4768   expressionS exp;
4769   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4770   if (exp.X_op != O_constant)
4771     {
4772       inst.error = _("constant expression required");
4773       return FAIL;
4774     }
4775
4776   if (exp.X_add_number < min || exp.X_add_number > max)
4777     {
4778       inst.error = _("immediate value out of range");
4779       return FAIL;
4780     }
4781
4782   *val = exp.X_add_number;
4783   return SUCCESS;
4784 }
4785
4786 /* Less-generic immediate-value read function with the possibility of loading a
4787    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4788    instructions. Puts the result directly in inst.operands[i].  */
4789
4790 static int
4791 parse_big_immediate (char **str, int i, expressionS *in_exp,
4792                      bfd_boolean allow_symbol_p)
4793 {
4794   expressionS exp;
4795   expressionS *exp_p = in_exp ? in_exp : &exp;
4796   char *ptr = *str;
4797
4798   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4799
4800   if (exp_p->X_op == O_constant)
4801     {
4802       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4803       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4804          O_constant.  We have to be careful not to break compilation for
4805          32-bit X_add_number, though.  */
4806       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4807         {
4808           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4809           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4810                                   & 0xffffffff);
4811           inst.operands[i].regisimm = 1;
4812         }
4813     }
4814   else if (exp_p->X_op == O_big
4815            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4816     {
4817       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4818
4819       /* Bignums have their least significant bits in
4820          generic_bignum[0]. Make sure we put 32 bits in imm and
4821          32 bits in reg,  in a (hopefully) portable way.  */
4822       gas_assert (parts != 0);
4823
4824       /* Make sure that the number is not too big.
4825          PR 11972: Bignums can now be sign-extended to the
4826          size of a .octa so check that the out of range bits
4827          are all zero or all one.  */
4828       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4829         {
4830           LITTLENUM_TYPE m = -1;
4831
4832           if (generic_bignum[parts * 2] != 0
4833               && generic_bignum[parts * 2] != m)
4834             return FAIL;
4835
4836           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4837             if (generic_bignum[j] != generic_bignum[j-1])
4838               return FAIL;
4839         }
4840
4841       inst.operands[i].imm = 0;
4842       for (j = 0; j < parts; j++, idx++)
4843         inst.operands[i].imm |= generic_bignum[idx]
4844                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4845       inst.operands[i].reg = 0;
4846       for (j = 0; j < parts; j++, idx++)
4847         inst.operands[i].reg |= generic_bignum[idx]
4848                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4849       inst.operands[i].regisimm = 1;
4850     }
4851   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4852     return FAIL;
4853
4854   *str = ptr;
4855
4856   return SUCCESS;
4857 }
4858
4859 /* Returns the pseudo-register number of an FPA immediate constant,
4860    or FAIL if there isn't a valid constant here.  */
4861
4862 static int
4863 parse_fpa_immediate (char ** str)
4864 {
4865   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4866   char *         save_in;
4867   expressionS    exp;
4868   int            i;
4869   int            j;
4870
4871   /* First try and match exact strings, this is to guarantee
4872      that some formats will work even for cross assembly.  */
4873
4874   for (i = 0; fp_const[i]; i++)
4875     {
4876       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4877         {
4878           char *start = *str;
4879
4880           *str += strlen (fp_const[i]);
4881           if (is_end_of_line[(unsigned char) **str])
4882             return i + 8;
4883           *str = start;
4884         }
4885     }
4886
4887   /* Just because we didn't get a match doesn't mean that the constant
4888      isn't valid, just that it is in a format that we don't
4889      automatically recognize.  Try parsing it with the standard
4890      expression routines.  */
4891
4892   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4893
4894   /* Look for a raw floating point number.  */
4895   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4896       && is_end_of_line[(unsigned char) *save_in])
4897     {
4898       for (i = 0; i < NUM_FLOAT_VALS; i++)
4899         {
4900           for (j = 0; j < MAX_LITTLENUMS; j++)
4901             {
4902               if (words[j] != fp_values[i][j])
4903                 break;
4904             }
4905
4906           if (j == MAX_LITTLENUMS)
4907             {
4908               *str = save_in;
4909               return i + 8;
4910             }
4911         }
4912     }
4913
4914   /* Try and parse a more complex expression, this will probably fail
4915      unless the code uses a floating point prefix (eg "0f").  */
4916   save_in = input_line_pointer;
4917   input_line_pointer = *str;
4918   if (expression (&exp) == absolute_section
4919       && exp.X_op == O_big
4920       && exp.X_add_number < 0)
4921     {
4922       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4923          Ditto for 15.  */
4924 #define X_PRECISION 5
4925 #define E_PRECISION 15L
4926       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
4927         {
4928           for (i = 0; i < NUM_FLOAT_VALS; i++)
4929             {
4930               for (j = 0; j < MAX_LITTLENUMS; j++)
4931                 {
4932                   if (words[j] != fp_values[i][j])
4933                     break;
4934                 }
4935
4936               if (j == MAX_LITTLENUMS)
4937                 {
4938                   *str = input_line_pointer;
4939                   input_line_pointer = save_in;
4940                   return i + 8;
4941                 }
4942             }
4943         }
4944     }
4945
4946   *str = input_line_pointer;
4947   input_line_pointer = save_in;
4948   inst.error = _("invalid FPA immediate expression");
4949   return FAIL;
4950 }
4951
4952 /* Returns 1 if a number has "quarter-precision" float format
4953    0baBbbbbbc defgh000 00000000 00000000.  */
4954
4955 static int
4956 is_quarter_float (unsigned imm)
4957 {
4958   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4959   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4960 }
4961
4962
4963 /* Detect the presence of a floating point or integer zero constant,
4964    i.e. #0.0 or #0.  */
4965
4966 static bfd_boolean
4967 parse_ifimm_zero (char **in)
4968 {
4969   int error_code;
4970
4971   if (!is_immediate_prefix (**in))
4972     {
4973       /* In unified syntax, all prefixes are optional.  */
4974       if (!unified_syntax)
4975         return FALSE;
4976     }
4977   else
4978     ++*in;
4979
4980   /* Accept #0x0 as a synonym for #0.  */
4981   if (strncmp (*in, "0x", 2) == 0)
4982     {
4983       int val;
4984       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
4985         return FALSE;
4986       return TRUE;
4987     }
4988
4989   error_code = atof_generic (in, ".", EXP_CHARS,
4990                              &generic_floating_point_number);
4991
4992   if (!error_code
4993       && generic_floating_point_number.sign == '+'
4994       && (generic_floating_point_number.low
4995           > generic_floating_point_number.leader))
4996     return TRUE;
4997
4998   return FALSE;
4999 }
5000
5001 /* Parse an 8-bit "quarter-precision" floating point number of the form:
5002    0baBbbbbbc defgh000 00000000 00000000.
5003    The zero and minus-zero cases need special handling, since they can't be
5004    encoded in the "quarter-precision" float format, but can nonetheless be
5005    loaded as integer constants.  */
5006
5007 static unsigned
5008 parse_qfloat_immediate (char **ccp, int *immed)
5009 {
5010   char *str = *ccp;
5011   char *fpnum;
5012   LITTLENUM_TYPE words[MAX_LITTLENUMS];
5013   int found_fpchar = 0;
5014
5015   skip_past_char (&str, '#');
5016
5017   /* We must not accidentally parse an integer as a floating-point number. Make
5018      sure that the value we parse is not an integer by checking for special
5019      characters '.' or 'e'.
5020      FIXME: This is a horrible hack, but doing better is tricky because type
5021      information isn't in a very usable state at parse time.  */
5022   fpnum = str;
5023   skip_whitespace (fpnum);
5024
5025   if (strncmp (fpnum, "0x", 2) == 0)
5026     return FAIL;
5027   else
5028     {
5029       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
5030         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
5031           {
5032             found_fpchar = 1;
5033             break;
5034           }
5035
5036       if (!found_fpchar)
5037         return FAIL;
5038     }
5039
5040   if ((str = atof_ieee (str, 's', words)) != NULL)
5041     {
5042       unsigned fpword = 0;
5043       int i;
5044
5045       /* Our FP word must be 32 bits (single-precision FP).  */
5046       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
5047         {
5048           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5049           fpword |= words[i];
5050         }
5051
5052       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5053         *immed = fpword;
5054       else
5055         return FAIL;
5056
5057       *ccp = str;
5058
5059       return SUCCESS;
5060     }
5061
5062   return FAIL;
5063 }
5064
5065 /* Shift operands.  */
5066 enum shift_kind
5067 {
5068   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5069 };
5070
5071 struct asm_shift_name
5072 {
5073   const char      *name;
5074   enum shift_kind  kind;
5075 };
5076
5077 /* Third argument to parse_shift.  */
5078 enum parse_shift_mode
5079 {
5080   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5081   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5082   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5083   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5084   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5085 };
5086
5087 /* Parse a <shift> specifier on an ARM data processing instruction.
5088    This has three forms:
5089
5090      (LSL|LSR|ASL|ASR|ROR) Rs
5091      (LSL|LSR|ASL|ASR|ROR) #imm
5092      RRX
5093
5094    Note that ASL is assimilated to LSL in the instruction encoding, and
5095    RRX to ROR #0 (which cannot be written as such).  */
5096
5097 static int
5098 parse_shift (char **str, int i, enum parse_shift_mode mode)
5099 {
5100   const struct asm_shift_name *shift_name;
5101   enum shift_kind shift;
5102   char *s = *str;
5103   char *p = s;
5104   int reg;
5105
5106   for (p = *str; ISALPHA (*p); p++)
5107     ;
5108
5109   if (p == *str)
5110     {
5111       inst.error = _("shift expression expected");
5112       return FAIL;
5113     }
5114
5115   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5116                                                             p - *str);
5117
5118   if (shift_name == NULL)
5119     {
5120       inst.error = _("shift expression expected");
5121       return FAIL;
5122     }
5123
5124   shift = shift_name->kind;
5125
5126   switch (mode)
5127     {
5128     case NO_SHIFT_RESTRICT:
5129     case SHIFT_IMMEDIATE:   break;
5130
5131     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5132       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5133         {
5134           inst.error = _("'LSL' or 'ASR' required");
5135           return FAIL;
5136         }
5137       break;
5138
5139     case SHIFT_LSL_IMMEDIATE:
5140       if (shift != SHIFT_LSL)
5141         {
5142           inst.error = _("'LSL' required");
5143           return FAIL;
5144         }
5145       break;
5146
5147     case SHIFT_ASR_IMMEDIATE:
5148       if (shift != SHIFT_ASR)
5149         {
5150           inst.error = _("'ASR' required");
5151           return FAIL;
5152         }
5153       break;
5154
5155     default: abort ();
5156     }
5157
5158   if (shift != SHIFT_RRX)
5159     {
5160       /* Whitespace can appear here if the next thing is a bare digit.  */
5161       skip_whitespace (p);
5162
5163       if (mode == NO_SHIFT_RESTRICT
5164           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5165         {
5166           inst.operands[i].imm = reg;
5167           inst.operands[i].immisreg = 1;
5168         }
5169       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5170         return FAIL;
5171     }
5172   inst.operands[i].shift_kind = shift;
5173   inst.operands[i].shifted = 1;
5174   *str = p;
5175   return SUCCESS;
5176 }
5177
5178 /* Parse a <shifter_operand> for an ARM data processing instruction:
5179
5180       #<immediate>
5181       #<immediate>, <rotate>
5182       <Rm>
5183       <Rm>, <shift>
5184
5185    where <shift> is defined by parse_shift above, and <rotate> is a
5186    multiple of 2 between 0 and 30.  Validation of immediate operands
5187    is deferred to md_apply_fix.  */
5188
5189 static int
5190 parse_shifter_operand (char **str, int i)
5191 {
5192   int value;
5193   expressionS exp;
5194
5195   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5196     {
5197       inst.operands[i].reg = value;
5198       inst.operands[i].isreg = 1;
5199
5200       /* parse_shift will override this if appropriate */
5201       inst.reloc.exp.X_op = O_constant;
5202       inst.reloc.exp.X_add_number = 0;
5203
5204       if (skip_past_comma (str) == FAIL)
5205         return SUCCESS;
5206
5207       /* Shift operation on register.  */
5208       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5209     }
5210
5211   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5212     return FAIL;
5213
5214   if (skip_past_comma (str) == SUCCESS)
5215     {
5216       /* #x, y -- ie explicit rotation by Y.  */
5217       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5218         return FAIL;
5219
5220       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5221         {
5222           inst.error = _("constant expression expected");
5223           return FAIL;
5224         }
5225
5226       value = exp.X_add_number;
5227       if (value < 0 || value > 30 || value % 2 != 0)
5228         {
5229           inst.error = _("invalid rotation");
5230           return FAIL;
5231         }
5232       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5233         {
5234           inst.error = _("invalid constant");
5235           return FAIL;
5236         }
5237
5238       /* Encode as specified.  */
5239       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5240       return SUCCESS;
5241     }
5242
5243   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5244   inst.reloc.pc_rel = 0;
5245   return SUCCESS;
5246 }
5247
5248 /* Group relocation information.  Each entry in the table contains the
5249    textual name of the relocation as may appear in assembler source
5250    and must end with a colon.
5251    Along with this textual name are the relocation codes to be used if
5252    the corresponding instruction is an ALU instruction (ADD or SUB only),
5253    an LDR, an LDRS, or an LDC.  */
5254
5255 struct group_reloc_table_entry
5256 {
5257   const char *name;
5258   int alu_code;
5259   int ldr_code;
5260   int ldrs_code;
5261   int ldc_code;
5262 };
5263
5264 typedef enum
5265 {
5266   /* Varieties of non-ALU group relocation.  */
5267
5268   GROUP_LDR,
5269   GROUP_LDRS,
5270   GROUP_LDC
5271 } group_reloc_type;
5272
5273 static struct group_reloc_table_entry group_reloc_table[] =
5274   { /* Program counter relative: */
5275     { "pc_g0_nc",
5276       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5277       0,                                /* LDR */
5278       0,                                /* LDRS */
5279       0 },                              /* LDC */
5280     { "pc_g0",
5281       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5282       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5283       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5284       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5285     { "pc_g1_nc",
5286       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5287       0,                                /* LDR */
5288       0,                                /* LDRS */
5289       0 },                              /* LDC */
5290     { "pc_g1",
5291       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5292       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5293       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5294       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5295     { "pc_g2",
5296       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5297       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5298       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5299       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5300     /* Section base relative */
5301     { "sb_g0_nc",
5302       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5303       0,                                /* LDR */
5304       0,                                /* LDRS */
5305       0 },                              /* LDC */
5306     { "sb_g0",
5307       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5308       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5309       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5310       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5311     { "sb_g1_nc",
5312       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5313       0,                                /* LDR */
5314       0,                                /* LDRS */
5315       0 },                              /* LDC */
5316     { "sb_g1",
5317       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5318       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5319       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5320       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5321     { "sb_g2",
5322       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5323       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5324       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5325       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5326     /* Absolute thumb alu relocations.  */
5327     { "lower0_7",
5328       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5329       0,                                /* LDR.  */
5330       0,                                /* LDRS.  */
5331       0 },                              /* LDC.  */
5332     { "lower8_15",
5333       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5334       0,                                /* LDR.  */
5335       0,                                /* LDRS.  */
5336       0 },                              /* LDC.  */
5337     { "upper0_7",
5338       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5339       0,                                /* LDR.  */
5340       0,                                /* LDRS.  */
5341       0 },                              /* LDC.  */
5342     { "upper8_15",
5343       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5344       0,                                /* LDR.  */
5345       0,                                /* LDRS.  */
5346       0 } };                            /* LDC.  */
5347
5348 /* Given the address of a pointer pointing to the textual name of a group
5349    relocation as may appear in assembler source, attempt to find its details
5350    in group_reloc_table.  The pointer will be updated to the character after
5351    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5352    otherwise.  On success, *entry will be updated to point at the relevant
5353    group_reloc_table entry. */
5354
5355 static int
5356 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5357 {
5358   unsigned int i;
5359   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5360     {
5361       int length = strlen (group_reloc_table[i].name);
5362
5363       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5364           && (*str)[length] == ':')
5365         {
5366           *out = &group_reloc_table[i];
5367           *str += (length + 1);
5368           return SUCCESS;
5369         }
5370     }
5371
5372   return FAIL;
5373 }
5374
5375 /* Parse a <shifter_operand> for an ARM data processing instruction
5376    (as for parse_shifter_operand) where group relocations are allowed:
5377
5378       #<immediate>
5379       #<immediate>, <rotate>
5380       #:<group_reloc>:<expression>
5381       <Rm>
5382       <Rm>, <shift>
5383
5384    where <group_reloc> is one of the strings defined in group_reloc_table.
5385    The hashes are optional.
5386
5387    Everything else is as for parse_shifter_operand.  */
5388
5389 static parse_operand_result
5390 parse_shifter_operand_group_reloc (char **str, int i)
5391 {
5392   /* Determine if we have the sequence of characters #: or just :
5393      coming next.  If we do, then we check for a group relocation.
5394      If we don't, punt the whole lot to parse_shifter_operand.  */
5395
5396   if (((*str)[0] == '#' && (*str)[1] == ':')
5397       || (*str)[0] == ':')
5398     {
5399       struct group_reloc_table_entry *entry;
5400
5401       if ((*str)[0] == '#')
5402         (*str) += 2;
5403       else
5404         (*str)++;
5405
5406       /* Try to parse a group relocation.  Anything else is an error.  */
5407       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5408         {
5409           inst.error = _("unknown group relocation");
5410           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5411         }
5412
5413       /* We now have the group relocation table entry corresponding to
5414          the name in the assembler source.  Next, we parse the expression.  */
5415       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5416         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5417
5418       /* Record the relocation type (always the ALU variant here).  */
5419       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5420       gas_assert (inst.reloc.type != 0);
5421
5422       return PARSE_OPERAND_SUCCESS;
5423     }
5424   else
5425     return parse_shifter_operand (str, i) == SUCCESS
5426            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5427
5428   /* Never reached.  */
5429 }
5430
5431 /* Parse a Neon alignment expression.  Information is written to
5432    inst.operands[i].  We assume the initial ':' has been skipped.
5433
5434    align        .imm = align << 8, .immisalign=1, .preind=0  */
5435 static parse_operand_result
5436 parse_neon_alignment (char **str, int i)
5437 {
5438   char *p = *str;
5439   expressionS exp;
5440
5441   my_get_expression (&exp, &p, GE_NO_PREFIX);
5442
5443   if (exp.X_op != O_constant)
5444     {
5445       inst.error = _("alignment must be constant");
5446       return PARSE_OPERAND_FAIL;
5447     }
5448
5449   inst.operands[i].imm = exp.X_add_number << 8;
5450   inst.operands[i].immisalign = 1;
5451   /* Alignments are not pre-indexes.  */
5452   inst.operands[i].preind = 0;
5453
5454   *str = p;
5455   return PARSE_OPERAND_SUCCESS;
5456 }
5457
5458 /* Parse all forms of an ARM address expression.  Information is written
5459    to inst.operands[i] and/or inst.reloc.
5460
5461    Preindexed addressing (.preind=1):
5462
5463    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5464    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5465    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5466                        .shift_kind=shift .reloc.exp=shift_imm
5467
5468    These three may have a trailing ! which causes .writeback to be set also.
5469
5470    Postindexed addressing (.postind=1, .writeback=1):
5471
5472    [Rn], #offset       .reg=Rn .reloc.exp=offset
5473    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5474    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5475                        .shift_kind=shift .reloc.exp=shift_imm
5476
5477    Unindexed addressing (.preind=0, .postind=0):
5478
5479    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5480
5481    Other:
5482
5483    [Rn]{!}             shorthand for [Rn,#0]{!}
5484    =immediate          .isreg=0 .reloc.exp=immediate
5485    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5486
5487   It is the caller's responsibility to check for addressing modes not
5488   supported by the instruction, and to set inst.reloc.type.  */
5489
5490 static parse_operand_result
5491 parse_address_main (char **str, int i, int group_relocations,
5492                     group_reloc_type group_type)
5493 {
5494   char *p = *str;
5495   int reg;
5496
5497   if (skip_past_char (&p, '[') == FAIL)
5498     {
5499       if (skip_past_char (&p, '=') == FAIL)
5500         {
5501           /* Bare address - translate to PC-relative offset.  */
5502           inst.reloc.pc_rel = 1;
5503           inst.operands[i].reg = REG_PC;
5504           inst.operands[i].isreg = 1;
5505           inst.operands[i].preind = 1;
5506
5507           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5508             return PARSE_OPERAND_FAIL;
5509         }
5510       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5511                                     /*allow_symbol_p=*/TRUE))
5512         return PARSE_OPERAND_FAIL;
5513
5514       *str = p;
5515       return PARSE_OPERAND_SUCCESS;
5516     }
5517
5518   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5519   skip_whitespace (p);
5520
5521   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5522     {
5523       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5524       return PARSE_OPERAND_FAIL;
5525     }
5526   inst.operands[i].reg = reg;
5527   inst.operands[i].isreg = 1;
5528
5529   if (skip_past_comma (&p) == SUCCESS)
5530     {
5531       inst.operands[i].preind = 1;
5532
5533       if (*p == '+') p++;
5534       else if (*p == '-') p++, inst.operands[i].negative = 1;
5535
5536       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5537         {
5538           inst.operands[i].imm = reg;
5539           inst.operands[i].immisreg = 1;
5540
5541           if (skip_past_comma (&p) == SUCCESS)
5542             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5543               return PARSE_OPERAND_FAIL;
5544         }
5545       else if (skip_past_char (&p, ':') == SUCCESS)
5546         {
5547           /* FIXME: '@' should be used here, but it's filtered out by generic
5548              code before we get to see it here. This may be subject to
5549              change.  */
5550           parse_operand_result result = parse_neon_alignment (&p, i);
5551
5552           if (result != PARSE_OPERAND_SUCCESS)
5553             return result;
5554         }
5555       else
5556         {
5557           if (inst.operands[i].negative)
5558             {
5559               inst.operands[i].negative = 0;
5560               p--;
5561             }
5562
5563           if (group_relocations
5564               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5565             {
5566               struct group_reloc_table_entry *entry;
5567
5568               /* Skip over the #: or : sequence.  */
5569               if (*p == '#')
5570                 p += 2;
5571               else
5572                 p++;
5573
5574               /* Try to parse a group relocation.  Anything else is an
5575                  error.  */
5576               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5577                 {
5578                   inst.error = _("unknown group relocation");
5579                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5580                 }
5581
5582               /* We now have the group relocation table entry corresponding to
5583                  the name in the assembler source.  Next, we parse the
5584                  expression.  */
5585               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5586                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5587
5588               /* Record the relocation type.  */
5589               switch (group_type)
5590                 {
5591                   case GROUP_LDR:
5592                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5593                     break;
5594
5595                   case GROUP_LDRS:
5596                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5597                     break;
5598
5599                   case GROUP_LDC:
5600                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5601                     break;
5602
5603                   default:
5604                     gas_assert (0);
5605                 }
5606
5607               if (inst.reloc.type == 0)
5608                 {
5609                   inst.error = _("this group relocation is not allowed on this instruction");
5610                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5611                 }
5612             }
5613           else
5614             {
5615               char *q = p;
5616               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5617                 return PARSE_OPERAND_FAIL;
5618               /* If the offset is 0, find out if it's a +0 or -0.  */
5619               if (inst.reloc.exp.X_op == O_constant
5620                   && inst.reloc.exp.X_add_number == 0)
5621                 {
5622                   skip_whitespace (q);
5623                   if (*q == '#')
5624                     {
5625                       q++;
5626                       skip_whitespace (q);
5627                     }
5628                   if (*q == '-')
5629                     inst.operands[i].negative = 1;
5630                 }
5631             }
5632         }
5633     }
5634   else if (skip_past_char (&p, ':') == SUCCESS)
5635     {
5636       /* FIXME: '@' should be used here, but it's filtered out by generic code
5637          before we get to see it here. This may be subject to change.  */
5638       parse_operand_result result = parse_neon_alignment (&p, i);
5639
5640       if (result != PARSE_OPERAND_SUCCESS)
5641         return result;
5642     }
5643
5644   if (skip_past_char (&p, ']') == FAIL)
5645     {
5646       inst.error = _("']' expected");
5647       return PARSE_OPERAND_FAIL;
5648     }
5649
5650   if (skip_past_char (&p, '!') == SUCCESS)
5651     inst.operands[i].writeback = 1;
5652
5653   else if (skip_past_comma (&p) == SUCCESS)
5654     {
5655       if (skip_past_char (&p, '{') == SUCCESS)
5656         {
5657           /* [Rn], {expr} - unindexed, with option */
5658           if (parse_immediate (&p, &inst.operands[i].imm,
5659                                0, 255, TRUE) == FAIL)
5660             return PARSE_OPERAND_FAIL;
5661
5662           if (skip_past_char (&p, '}') == FAIL)
5663             {
5664               inst.error = _("'}' expected at end of 'option' field");
5665               return PARSE_OPERAND_FAIL;
5666             }
5667           if (inst.operands[i].preind)
5668             {
5669               inst.error = _("cannot combine index with option");
5670               return PARSE_OPERAND_FAIL;
5671             }
5672           *str = p;
5673           return PARSE_OPERAND_SUCCESS;
5674         }
5675       else
5676         {
5677           inst.operands[i].postind = 1;
5678           inst.operands[i].writeback = 1;
5679
5680           if (inst.operands[i].preind)
5681             {
5682               inst.error = _("cannot combine pre- and post-indexing");
5683               return PARSE_OPERAND_FAIL;
5684             }
5685
5686           if (*p == '+') p++;
5687           else if (*p == '-') p++, inst.operands[i].negative = 1;
5688
5689           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5690             {
5691               /* We might be using the immediate for alignment already. If we
5692                  are, OR the register number into the low-order bits.  */
5693               if (inst.operands[i].immisalign)
5694                 inst.operands[i].imm |= reg;
5695               else
5696                 inst.operands[i].imm = reg;
5697               inst.operands[i].immisreg = 1;
5698
5699               if (skip_past_comma (&p) == SUCCESS)
5700                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5701                   return PARSE_OPERAND_FAIL;
5702             }
5703           else
5704             {
5705               char *q = p;
5706               if (inst.operands[i].negative)
5707                 {
5708                   inst.operands[i].negative = 0;
5709                   p--;
5710                 }
5711               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5712                 return PARSE_OPERAND_FAIL;
5713               /* If the offset is 0, find out if it's a +0 or -0.  */
5714               if (inst.reloc.exp.X_op == O_constant
5715                   && inst.reloc.exp.X_add_number == 0)
5716                 {
5717                   skip_whitespace (q);
5718                   if (*q == '#')
5719                     {
5720                       q++;
5721                       skip_whitespace (q);
5722                     }
5723                   if (*q == '-')
5724                     inst.operands[i].negative = 1;
5725                 }
5726             }
5727         }
5728     }
5729
5730   /* If at this point neither .preind nor .postind is set, we have a
5731      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5732   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5733     {
5734       inst.operands[i].preind = 1;
5735       inst.reloc.exp.X_op = O_constant;
5736       inst.reloc.exp.X_add_number = 0;
5737     }
5738   *str = p;
5739   return PARSE_OPERAND_SUCCESS;
5740 }
5741
5742 static int
5743 parse_address (char **str, int i)
5744 {
5745   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5746          ? SUCCESS : FAIL;
5747 }
5748
5749 static parse_operand_result
5750 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5751 {
5752   return parse_address_main (str, i, 1, type);
5753 }
5754
5755 /* Parse an operand for a MOVW or MOVT instruction.  */
5756 static int
5757 parse_half (char **str)
5758 {
5759   char * p;
5760
5761   p = *str;
5762   skip_past_char (&p, '#');
5763   if (strncasecmp (p, ":lower16:", 9) == 0)
5764     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5765   else if (strncasecmp (p, ":upper16:", 9) == 0)
5766     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5767
5768   if (inst.reloc.type != BFD_RELOC_UNUSED)
5769     {
5770       p += 9;
5771       skip_whitespace (p);
5772     }
5773
5774   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5775     return FAIL;
5776
5777   if (inst.reloc.type == BFD_RELOC_UNUSED)
5778     {
5779       if (inst.reloc.exp.X_op != O_constant)
5780         {
5781           inst.error = _("constant expression expected");
5782           return FAIL;
5783         }
5784       if (inst.reloc.exp.X_add_number < 0
5785           || inst.reloc.exp.X_add_number > 0xffff)
5786         {
5787           inst.error = _("immediate value out of range");
5788           return FAIL;
5789         }
5790     }
5791   *str = p;
5792   return SUCCESS;
5793 }
5794
5795 /* Miscellaneous. */
5796
5797 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5798    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5799 static int
5800 parse_psr (char **str, bfd_boolean lhs)
5801 {
5802   char *p;
5803   unsigned long psr_field;
5804   const struct asm_psr *psr;
5805   char *start;
5806   bfd_boolean is_apsr = FALSE;
5807   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5808
5809   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5810      be TRUE, but we want to ignore it in this case as we are building for any
5811      CPU type, including non-m variants.  */
5812   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5813     m_profile = FALSE;
5814
5815   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5816      feature for ease of use and backwards compatibility.  */
5817   p = *str;
5818   if (strncasecmp (p, "SPSR", 4) == 0)
5819     {
5820       if (m_profile)
5821         goto unsupported_psr;
5822
5823       psr_field = SPSR_BIT;
5824     }
5825   else if (strncasecmp (p, "CPSR", 4) == 0)
5826     {
5827       if (m_profile)
5828         goto unsupported_psr;
5829
5830       psr_field = 0;
5831     }
5832   else if (strncasecmp (p, "APSR", 4) == 0)
5833     {
5834       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5835          and ARMv7-R architecture CPUs.  */
5836       is_apsr = TRUE;
5837       psr_field = 0;
5838     }
5839   else if (m_profile)
5840     {
5841       start = p;
5842       do
5843         p++;
5844       while (ISALNUM (*p) || *p == '_');
5845
5846       if (strncasecmp (start, "iapsr", 5) == 0
5847           || strncasecmp (start, "eapsr", 5) == 0
5848           || strncasecmp (start, "xpsr", 4) == 0
5849           || strncasecmp (start, "psr", 3) == 0)
5850         p = start + strcspn (start, "rR") + 1;
5851
5852       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5853                                                   p - start);
5854
5855       if (!psr)
5856         return FAIL;
5857
5858       /* If APSR is being written, a bitfield may be specified.  Note that
5859          APSR itself is handled above.  */
5860       if (psr->field <= 3)
5861         {
5862           psr_field = psr->field;
5863           is_apsr = TRUE;
5864           goto check_suffix;
5865         }
5866
5867       *str = p;
5868       /* M-profile MSR instructions have the mask field set to "10", except
5869          *PSR variants which modify APSR, which may use a different mask (and
5870          have been handled already).  Do that by setting the PSR_f field
5871          here.  */
5872       return psr->field | (lhs ? PSR_f : 0);
5873     }
5874   else
5875     goto unsupported_psr;
5876
5877   p += 4;
5878 check_suffix:
5879   if (*p == '_')
5880     {
5881       /* A suffix follows.  */
5882       p++;
5883       start = p;
5884
5885       do
5886         p++;
5887       while (ISALNUM (*p) || *p == '_');
5888
5889       if (is_apsr)
5890         {
5891           /* APSR uses a notation for bits, rather than fields.  */
5892           unsigned int nzcvq_bits = 0;
5893           unsigned int g_bit = 0;
5894           char *bit;
5895
5896           for (bit = start; bit != p; bit++)
5897             {
5898               switch (TOLOWER (*bit))
5899                 {
5900                 case 'n':
5901                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5902                   break;
5903
5904                 case 'z':
5905                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5906                   break;
5907
5908                 case 'c':
5909                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5910                   break;
5911
5912                 case 'v':
5913                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5914                   break;
5915
5916                 case 'q':
5917                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5918                   break;
5919
5920                 case 'g':
5921                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5922                   break;
5923
5924                 default:
5925                   inst.error = _("unexpected bit specified after APSR");
5926                   return FAIL;
5927                 }
5928             }
5929
5930           if (nzcvq_bits == 0x1f)
5931             psr_field |= PSR_f;
5932
5933           if (g_bit == 0x1)
5934             {
5935               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5936                 {
5937                   inst.error = _("selected processor does not "
5938                                  "support DSP extension");
5939                   return FAIL;
5940                 }
5941
5942               psr_field |= PSR_s;
5943             }
5944
5945           if ((nzcvq_bits & 0x20) != 0
5946               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5947               || (g_bit & 0x2) != 0)
5948             {
5949               inst.error = _("bad bitmask specified after APSR");
5950               return FAIL;
5951             }
5952         }
5953       else
5954         {
5955           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5956                                                       p - start);
5957           if (!psr)
5958             goto error;
5959
5960           psr_field |= psr->field;
5961         }
5962     }
5963   else
5964     {
5965       if (ISALNUM (*p))
5966         goto error;    /* Garbage after "[CS]PSR".  */
5967
5968       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5969          is deprecated, but allow it anyway.  */
5970       if (is_apsr && lhs)
5971         {
5972           psr_field |= PSR_f;
5973           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5974                        "deprecated"));
5975         }
5976       else if (!m_profile)
5977         /* These bits are never right for M-profile devices: don't set them
5978            (only code paths which read/write APSR reach here).  */
5979         psr_field |= (PSR_c | PSR_f);
5980     }
5981   *str = p;
5982   return psr_field;
5983
5984  unsupported_psr:
5985   inst.error = _("selected processor does not support requested special "
5986                  "purpose register");
5987   return FAIL;
5988
5989  error:
5990   inst.error = _("flag for {c}psr instruction expected");
5991   return FAIL;
5992 }
5993
5994 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5995    value suitable for splatting into the AIF field of the instruction.  */
5996
5997 static int
5998 parse_cps_flags (char **str)
5999 {
6000   int val = 0;
6001   int saw_a_flag = 0;
6002   char *s = *str;
6003
6004   for (;;)
6005     switch (*s++)
6006       {
6007       case '\0': case ',':
6008         goto done;
6009
6010       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
6011       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
6012       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
6013
6014       default:
6015         inst.error = _("unrecognized CPS flag");
6016         return FAIL;
6017       }
6018
6019  done:
6020   if (saw_a_flag == 0)
6021     {
6022       inst.error = _("missing CPS flags");
6023       return FAIL;
6024     }
6025
6026   *str = s - 1;
6027   return val;
6028 }
6029
6030 /* Parse an endian specifier ("BE" or "LE", case insensitive);
6031    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
6032
6033 static int
6034 parse_endian_specifier (char **str)
6035 {
6036   int little_endian;
6037   char *s = *str;
6038
6039   if (strncasecmp (s, "BE", 2))
6040     little_endian = 0;
6041   else if (strncasecmp (s, "LE", 2))
6042     little_endian = 1;
6043   else
6044     {
6045       inst.error = _("valid endian specifiers are be or le");
6046       return FAIL;
6047     }
6048
6049   if (ISALNUM (s[2]) || s[2] == '_')
6050     {
6051       inst.error = _("valid endian specifiers are be or le");
6052       return FAIL;
6053     }
6054
6055   *str = s + 2;
6056   return little_endian;
6057 }
6058
6059 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6060    value suitable for poking into the rotate field of an sxt or sxta
6061    instruction, or FAIL on error.  */
6062
6063 static int
6064 parse_ror (char **str)
6065 {
6066   int rot;
6067   char *s = *str;
6068
6069   if (strncasecmp (s, "ROR", 3) == 0)
6070     s += 3;
6071   else
6072     {
6073       inst.error = _("missing rotation field after comma");
6074       return FAIL;
6075     }
6076
6077   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6078     return FAIL;
6079
6080   switch (rot)
6081     {
6082     case  0: *str = s; return 0x0;
6083     case  8: *str = s; return 0x1;
6084     case 16: *str = s; return 0x2;
6085     case 24: *str = s; return 0x3;
6086
6087     default:
6088       inst.error = _("rotation can only be 0, 8, 16, or 24");
6089       return FAIL;
6090     }
6091 }
6092
6093 /* Parse a conditional code (from conds[] below).  The value returned is in the
6094    range 0 .. 14, or FAIL.  */
6095 static int
6096 parse_cond (char **str)
6097 {
6098   char *q;
6099   const struct asm_cond *c;
6100   int n;
6101   /* Condition codes are always 2 characters, so matching up to
6102      3 characters is sufficient.  */
6103   char cond[3];
6104
6105   q = *str;
6106   n = 0;
6107   while (ISALPHA (*q) && n < 3)
6108     {
6109       cond[n] = TOLOWER (*q);
6110       q++;
6111       n++;
6112     }
6113
6114   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6115   if (!c)
6116     {
6117       inst.error = _("condition required");
6118       return FAIL;
6119     }
6120
6121   *str = q;
6122   return c->value;
6123 }
6124
6125 /* Record a use of the given feature.  */
6126 static void
6127 record_feature_use (const arm_feature_set *feature)
6128 {
6129   if (thumb_mode)
6130     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6131   else
6132     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6133 }
6134
6135 /* If the given feature available in the selected CPU, mark it as used.
6136    Returns TRUE iff feature is available.  */
6137 static bfd_boolean
6138 mark_feature_used (const arm_feature_set *feature)
6139 {
6140   /* Ensure the option is valid on the current architecture.  */
6141   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6142     return FALSE;
6143
6144   /* Add the appropriate architecture feature for the barrier option used.
6145      */
6146   record_feature_use (feature);
6147
6148   return TRUE;
6149 }
6150
6151 /* Parse an option for a barrier instruction.  Returns the encoding for the
6152    option, or FAIL.  */
6153 static int
6154 parse_barrier (char **str)
6155 {
6156   char *p, *q;
6157   const struct asm_barrier_opt *o;
6158
6159   p = q = *str;
6160   while (ISALPHA (*q))
6161     q++;
6162
6163   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6164                                                     q - p);
6165   if (!o)
6166     return FAIL;
6167
6168   if (!mark_feature_used (&o->arch))
6169     return FAIL;
6170
6171   *str = q;
6172   return o->value;
6173 }
6174
6175 /* Parse the operands of a table branch instruction.  Similar to a memory
6176    operand.  */
6177 static int
6178 parse_tb (char **str)
6179 {
6180   char * p = *str;
6181   int reg;
6182
6183   if (skip_past_char (&p, '[') == FAIL)
6184     {
6185       inst.error = _("'[' expected");
6186       return FAIL;
6187     }
6188
6189   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6190     {
6191       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6192       return FAIL;
6193     }
6194   inst.operands[0].reg = reg;
6195
6196   if (skip_past_comma (&p) == FAIL)
6197     {
6198       inst.error = _("',' expected");
6199       return FAIL;
6200     }
6201
6202   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6203     {
6204       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6205       return FAIL;
6206     }
6207   inst.operands[0].imm = reg;
6208
6209   if (skip_past_comma (&p) == SUCCESS)
6210     {
6211       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6212         return FAIL;
6213       if (inst.reloc.exp.X_add_number != 1)
6214         {
6215           inst.error = _("invalid shift");
6216           return FAIL;
6217         }
6218       inst.operands[0].shifted = 1;
6219     }
6220
6221   if (skip_past_char (&p, ']') == FAIL)
6222     {
6223       inst.error = _("']' expected");
6224       return FAIL;
6225     }
6226   *str = p;
6227   return SUCCESS;
6228 }
6229
6230 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6231    information on the types the operands can take and how they are encoded.
6232    Up to four operands may be read; this function handles setting the
6233    ".present" field for each read operand itself.
6234    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6235    else returns FAIL.  */
6236
6237 static int
6238 parse_neon_mov (char **str, int *which_operand)
6239 {
6240   int i = *which_operand, val;
6241   enum arm_reg_type rtype;
6242   char *ptr = *str;
6243   struct neon_type_el optype;
6244
6245   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6246     {
6247       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6248       inst.operands[i].reg = val;
6249       inst.operands[i].isscalar = 1;
6250       inst.operands[i].vectype = optype;
6251       inst.operands[i++].present = 1;
6252
6253       if (skip_past_comma (&ptr) == FAIL)
6254         goto wanted_comma;
6255
6256       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6257         goto wanted_arm;
6258
6259       inst.operands[i].reg = val;
6260       inst.operands[i].isreg = 1;
6261       inst.operands[i].present = 1;
6262     }
6263   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6264            != FAIL)
6265     {
6266       /* Cases 0, 1, 2, 3, 5 (D only).  */
6267       if (skip_past_comma (&ptr) == FAIL)
6268         goto wanted_comma;
6269
6270       inst.operands[i].reg = val;
6271       inst.operands[i].isreg = 1;
6272       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6273       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6274       inst.operands[i].isvec = 1;
6275       inst.operands[i].vectype = optype;
6276       inst.operands[i++].present = 1;
6277
6278       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6279         {
6280           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6281              Case 13: VMOV <Sd>, <Rm>  */
6282           inst.operands[i].reg = val;
6283           inst.operands[i].isreg = 1;
6284           inst.operands[i].present = 1;
6285
6286           if (rtype == REG_TYPE_NQ)
6287             {
6288               first_error (_("can't use Neon quad register here"));
6289               return FAIL;
6290             }
6291           else if (rtype != REG_TYPE_VFS)
6292             {
6293               i++;
6294               if (skip_past_comma (&ptr) == FAIL)
6295                 goto wanted_comma;
6296               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6297                 goto wanted_arm;
6298               inst.operands[i].reg = val;
6299               inst.operands[i].isreg = 1;
6300               inst.operands[i].present = 1;
6301             }
6302         }
6303       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6304                                            &optype)) != FAIL)
6305         {
6306           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6307              Case 1: VMOV<c><q> <Dd>, <Dm>
6308              Case 8: VMOV.F32 <Sd>, <Sm>
6309              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6310
6311           inst.operands[i].reg = val;
6312           inst.operands[i].isreg = 1;
6313           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6314           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6315           inst.operands[i].isvec = 1;
6316           inst.operands[i].vectype = optype;
6317           inst.operands[i].present = 1;
6318
6319           if (skip_past_comma (&ptr) == SUCCESS)
6320             {
6321               /* Case 15.  */
6322               i++;
6323
6324               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6325                 goto wanted_arm;
6326
6327               inst.operands[i].reg = val;
6328               inst.operands[i].isreg = 1;
6329               inst.operands[i++].present = 1;
6330
6331               if (skip_past_comma (&ptr) == FAIL)
6332                 goto wanted_comma;
6333
6334               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6335                 goto wanted_arm;
6336
6337               inst.operands[i].reg = val;
6338               inst.operands[i].isreg = 1;
6339               inst.operands[i].present = 1;
6340             }
6341         }
6342       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6343           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6344              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6345              Case 10: VMOV.F32 <Sd>, #<imm>
6346              Case 11: VMOV.F64 <Dd>, #<imm>  */
6347         inst.operands[i].immisfloat = 1;
6348       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6349                == SUCCESS)
6350           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6351              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6352         ;
6353       else
6354         {
6355           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6356           return FAIL;
6357         }
6358     }
6359   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6360     {
6361       /* Cases 6, 7.  */
6362       inst.operands[i].reg = val;
6363       inst.operands[i].isreg = 1;
6364       inst.operands[i++].present = 1;
6365
6366       if (skip_past_comma (&ptr) == FAIL)
6367         goto wanted_comma;
6368
6369       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6370         {
6371           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6372           inst.operands[i].reg = val;
6373           inst.operands[i].isscalar = 1;
6374           inst.operands[i].present = 1;
6375           inst.operands[i].vectype = optype;
6376         }
6377       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6378         {
6379           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6380           inst.operands[i].reg = val;
6381           inst.operands[i].isreg = 1;
6382           inst.operands[i++].present = 1;
6383
6384           if (skip_past_comma (&ptr) == FAIL)
6385             goto wanted_comma;
6386
6387           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6388               == FAIL)
6389             {
6390               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6391               return FAIL;
6392             }
6393
6394           inst.operands[i].reg = val;
6395           inst.operands[i].isreg = 1;
6396           inst.operands[i].isvec = 1;
6397           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6398           inst.operands[i].vectype = optype;
6399           inst.operands[i].present = 1;
6400
6401           if (rtype == REG_TYPE_VFS)
6402             {
6403               /* Case 14.  */
6404               i++;
6405               if (skip_past_comma (&ptr) == FAIL)
6406                 goto wanted_comma;
6407               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6408                                               &optype)) == FAIL)
6409                 {
6410                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6411                   return FAIL;
6412                 }
6413               inst.operands[i].reg = val;
6414               inst.operands[i].isreg = 1;
6415               inst.operands[i].isvec = 1;
6416               inst.operands[i].issingle = 1;
6417               inst.operands[i].vectype = optype;
6418               inst.operands[i].present = 1;
6419             }
6420         }
6421       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6422                != FAIL)
6423         {
6424           /* Case 13.  */
6425           inst.operands[i].reg = val;
6426           inst.operands[i].isreg = 1;
6427           inst.operands[i].isvec = 1;
6428           inst.operands[i].issingle = 1;
6429           inst.operands[i].vectype = optype;
6430           inst.operands[i].present = 1;
6431         }
6432     }
6433   else
6434     {
6435       first_error (_("parse error"));
6436       return FAIL;
6437     }
6438
6439   /* Successfully parsed the operands. Update args.  */
6440   *which_operand = i;
6441   *str = ptr;
6442   return SUCCESS;
6443
6444  wanted_comma:
6445   first_error (_("expected comma"));
6446   return FAIL;
6447
6448  wanted_arm:
6449   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6450   return FAIL;
6451 }
6452
6453 /* Use this macro when the operand constraints are different
6454    for ARM and THUMB (e.g. ldrd).  */
6455 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6456         ((arm_operand) | ((thumb_operand) << 16))
6457
6458 /* Matcher codes for parse_operands.  */
6459 enum operand_parse_code
6460 {
6461   OP_stop,      /* end of line */
6462
6463   OP_RR,        /* ARM register */
6464   OP_RRnpc,     /* ARM register, not r15 */
6465   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6466   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6467   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6468                    optional trailing ! */
6469   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6470   OP_RCP,       /* Coprocessor number */
6471   OP_RCN,       /* Coprocessor register */
6472   OP_RF,        /* FPA register */
6473   OP_RVS,       /* VFP single precision register */
6474   OP_RVD,       /* VFP double precision register (0..15) */
6475   OP_RND,       /* Neon double precision register (0..31) */
6476   OP_RNQ,       /* Neon quad precision register */
6477   OP_RVSD,      /* VFP single or double precision register */
6478   OP_RNDQ,      /* Neon double or quad precision register */
6479   OP_RNSDQ,     /* Neon single, double or quad precision register */
6480   OP_RNSC,      /* Neon scalar D[X] */
6481   OP_RVC,       /* VFP control register */
6482   OP_RMF,       /* Maverick F register */
6483   OP_RMD,       /* Maverick D register */
6484   OP_RMFX,      /* Maverick FX register */
6485   OP_RMDX,      /* Maverick DX register */
6486   OP_RMAX,      /* Maverick AX register */
6487   OP_RMDS,      /* Maverick DSPSC register */
6488   OP_RIWR,      /* iWMMXt wR register */
6489   OP_RIWC,      /* iWMMXt wC register */
6490   OP_RIWG,      /* iWMMXt wCG register */
6491   OP_RXA,       /* XScale accumulator register */
6492
6493   OP_REGLST,    /* ARM register list */
6494   OP_VRSLST,    /* VFP single-precision register list */
6495   OP_VRDLST,    /* VFP double-precision register list */
6496   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6497   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6498   OP_NSTRLST,   /* Neon element/structure list */
6499
6500   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6501   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6502   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6503   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6504   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6505   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6506   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6507   OP_VMOV,      /* Neon VMOV operands.  */
6508   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6509   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6510   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6511
6512   OP_I0,        /* immediate zero */
6513   OP_I7,        /* immediate value 0 .. 7 */
6514   OP_I15,       /*                 0 .. 15 */
6515   OP_I16,       /*                 1 .. 16 */
6516   OP_I16z,      /*                 0 .. 16 */
6517   OP_I31,       /*                 0 .. 31 */
6518   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6519   OP_I32,       /*                 1 .. 32 */
6520   OP_I32z,      /*                 0 .. 32 */
6521   OP_I63,       /*                 0 .. 63 */
6522   OP_I63s,      /*               -64 .. 63 */
6523   OP_I64,       /*                 1 .. 64 */
6524   OP_I64z,      /*                 0 .. 64 */
6525   OP_I255,      /*                 0 .. 255 */
6526
6527   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6528   OP_I7b,       /*                             0 .. 7 */
6529   OP_I15b,      /*                             0 .. 15 */
6530   OP_I31b,      /*                             0 .. 31 */
6531
6532   OP_SH,        /* shifter operand */
6533   OP_SHG,       /* shifter operand with possible group relocation */
6534   OP_ADDR,      /* Memory address expression (any mode) */
6535   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6536   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6537   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6538   OP_EXP,       /* arbitrary expression */
6539   OP_EXPi,      /* same, with optional immediate prefix */
6540   OP_EXPr,      /* same, with optional relocation suffix */
6541   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6542   OP_IROT1,     /* VCADD rotate immediate: 90, 270.  */
6543   OP_IROT2,     /* VCMLA rotate immediate: 0, 90, 180, 270.  */
6544
6545   OP_CPSF,      /* CPS flags */
6546   OP_ENDI,      /* Endianness specifier */
6547   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6548   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6549   OP_COND,      /* conditional code */
6550   OP_TB,        /* Table branch.  */
6551
6552   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6553
6554   OP_RRnpc_I0,  /* ARM register or literal 0 */
6555   OP_RR_EXr,    /* ARM register or expression with opt. reloc stuff. */
6556   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6557   OP_RF_IF,     /* FPA register or immediate */
6558   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6559   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6560
6561   /* Optional operands.  */
6562   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6563   OP_oI31b,      /*                             0 .. 31 */
6564   OP_oI32b,      /*                             1 .. 32 */
6565   OP_oI32z,      /*                             0 .. 32 */
6566   OP_oIffffb,    /*                             0 .. 65535 */
6567   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6568
6569   OP_oRR,        /* ARM register */
6570   OP_oRRnpc,     /* ARM register, not the PC */
6571   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6572   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6573   OP_oRND,       /* Optional Neon double precision register */
6574   OP_oRNQ,       /* Optional Neon quad precision register */
6575   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6576   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6577   OP_oSHll,      /* LSL immediate */
6578   OP_oSHar,      /* ASR immediate */
6579   OP_oSHllar,    /* LSL or ASR immediate */
6580   OP_oROR,       /* ROR 0/8/16/24 */
6581   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6582
6583   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6584   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6585   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6586   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6587
6588   OP_FIRST_OPTIONAL = OP_oI7b
6589 };
6590
6591 /* Generic instruction operand parser.  This does no encoding and no
6592    semantic validation; it merely squirrels values away in the inst
6593    structure.  Returns SUCCESS or FAIL depending on whether the
6594    specified grammar matched.  */
6595 static int
6596 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6597 {
6598   unsigned const int *upat = pattern;
6599   char *backtrack_pos = 0;
6600   const char *backtrack_error = 0;
6601   int i, val = 0, backtrack_index = 0;
6602   enum arm_reg_type rtype;
6603   parse_operand_result result;
6604   unsigned int op_parse_code;
6605
6606 #define po_char_or_fail(chr)                    \
6607   do                                            \
6608     {                                           \
6609       if (skip_past_char (&str, chr) == FAIL)   \
6610         goto bad_args;                          \
6611     }                                           \
6612   while (0)
6613
6614 #define po_reg_or_fail(regtype)                                 \
6615   do                                                            \
6616     {                                                           \
6617       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6618                                  & inst.operands[i].vectype);   \
6619       if (val == FAIL)                                          \
6620         {                                                       \
6621           first_error (_(reg_expected_msgs[regtype]));          \
6622           goto failure;                                         \
6623         }                                                       \
6624       inst.operands[i].reg = val;                               \
6625       inst.operands[i].isreg = 1;                               \
6626       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6627       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6628       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6629                              || rtype == REG_TYPE_VFD           \
6630                              || rtype == REG_TYPE_NQ);          \
6631     }                                                           \
6632   while (0)
6633
6634 #define po_reg_or_goto(regtype, label)                          \
6635   do                                                            \
6636     {                                                           \
6637       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6638                                  & inst.operands[i].vectype);   \
6639       if (val == FAIL)                                          \
6640         goto label;                                             \
6641                                                                 \
6642       inst.operands[i].reg = val;                               \
6643       inst.operands[i].isreg = 1;                               \
6644       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6645       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6646       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6647                              || rtype == REG_TYPE_VFD           \
6648                              || rtype == REG_TYPE_NQ);          \
6649     }                                                           \
6650   while (0)
6651
6652 #define po_imm_or_fail(min, max, popt)                          \
6653   do                                                            \
6654     {                                                           \
6655       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6656         goto failure;                                           \
6657       inst.operands[i].imm = val;                               \
6658     }                                                           \
6659   while (0)
6660
6661 #define po_scalar_or_goto(elsz, label)                                  \
6662   do                                                                    \
6663     {                                                                   \
6664       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6665       if (val == FAIL)                                                  \
6666         goto label;                                                     \
6667       inst.operands[i].reg = val;                                       \
6668       inst.operands[i].isscalar = 1;                                    \
6669     }                                                                   \
6670   while (0)
6671
6672 #define po_misc_or_fail(expr)                   \
6673   do                                            \
6674     {                                           \
6675       if (expr)                                 \
6676         goto failure;                           \
6677     }                                           \
6678   while (0)
6679
6680 #define po_misc_or_fail_no_backtrack(expr)              \
6681   do                                                    \
6682     {                                                   \
6683       result = expr;                                    \
6684       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6685         backtrack_pos = 0;                              \
6686       if (result != PARSE_OPERAND_SUCCESS)              \
6687         goto failure;                                   \
6688     }                                                   \
6689   while (0)
6690
6691 #define po_barrier_or_imm(str)                             \
6692   do                                                       \
6693     {                                                      \
6694       val = parse_barrier (&str);                          \
6695       if (val == FAIL && ! ISALPHA (*str))                 \
6696         goto immediate;                                    \
6697       if (val == FAIL                                      \
6698           /* ISB can only take SY as an option.  */        \
6699           || ((inst.instruction & 0xf0) == 0x60            \
6700                && val != 0xf))                             \
6701         {                                                  \
6702            inst.error = _("invalid barrier type");         \
6703            backtrack_pos = 0;                              \
6704            goto failure;                                   \
6705         }                                                  \
6706     }                                                      \
6707   while (0)
6708
6709   skip_whitespace (str);
6710
6711   for (i = 0; upat[i] != OP_stop; i++)
6712     {
6713       op_parse_code = upat[i];
6714       if (op_parse_code >= 1<<16)
6715         op_parse_code = thumb ? (op_parse_code >> 16)
6716                                 : (op_parse_code & ((1<<16)-1));
6717
6718       if (op_parse_code >= OP_FIRST_OPTIONAL)
6719         {
6720           /* Remember where we are in case we need to backtrack.  */
6721           gas_assert (!backtrack_pos);
6722           backtrack_pos = str;
6723           backtrack_error = inst.error;
6724           backtrack_index = i;
6725         }
6726
6727       if (i > 0 && (i > 1 || inst.operands[0].present))
6728         po_char_or_fail (',');
6729
6730       switch (op_parse_code)
6731         {
6732           /* Registers */
6733         case OP_oRRnpc:
6734         case OP_oRRnpcsp:
6735         case OP_RRnpc:
6736         case OP_RRnpcsp:
6737         case OP_oRR:
6738         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6739         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6740         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6741         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6742         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6743         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6744         case OP_oRND:
6745         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6746         case OP_RVC:
6747           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6748           break;
6749           /* Also accept generic coprocessor regs for unknown registers.  */
6750           coproc_reg:
6751           po_reg_or_fail (REG_TYPE_CN);
6752           break;
6753         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6754         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6755         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6756         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6757         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6758         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6759         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6760         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6761         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6762         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6763         case OP_oRNQ:
6764         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6765         case OP_oRNDQ:
6766         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6767         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6768         case OP_oRNSDQ:
6769         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6770
6771         /* Neon scalar. Using an element size of 8 means that some invalid
6772            scalars are accepted here, so deal with those in later code.  */
6773         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6774
6775         case OP_RNDQ_I0:
6776           {
6777             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6778             break;
6779             try_imm0:
6780             po_imm_or_fail (0, 0, TRUE);
6781           }
6782           break;
6783
6784         case OP_RVSD_I0:
6785           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6786           break;
6787
6788         case OP_RSVD_FI0:
6789           {
6790             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6791             break;
6792             try_ifimm0:
6793             if (parse_ifimm_zero (&str))
6794               inst.operands[i].imm = 0;
6795             else
6796             {
6797               inst.error
6798                 = _("only floating point zero is allowed as immediate value");
6799               goto failure;
6800             }
6801           }
6802           break;
6803
6804         case OP_RR_RNSC:
6805           {
6806             po_scalar_or_goto (8, try_rr);
6807             break;
6808             try_rr:
6809             po_reg_or_fail (REG_TYPE_RN);
6810           }
6811           break;
6812
6813         case OP_RNSDQ_RNSC:
6814           {
6815             po_scalar_or_goto (8, try_nsdq);
6816             break;
6817             try_nsdq:
6818             po_reg_or_fail (REG_TYPE_NSDQ);
6819           }
6820           break;
6821
6822         case OP_RNDQ_RNSC:
6823           {
6824             po_scalar_or_goto (8, try_ndq);
6825             break;
6826             try_ndq:
6827             po_reg_or_fail (REG_TYPE_NDQ);
6828           }
6829           break;
6830
6831         case OP_RND_RNSC:
6832           {
6833             po_scalar_or_goto (8, try_vfd);
6834             break;
6835             try_vfd:
6836             po_reg_or_fail (REG_TYPE_VFD);
6837           }
6838           break;
6839
6840         case OP_VMOV:
6841           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6842              not careful then bad things might happen.  */
6843           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6844           break;
6845
6846         case OP_RNDQ_Ibig:
6847           {
6848             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6849             break;
6850             try_immbig:
6851             /* There's a possibility of getting a 64-bit immediate here, so
6852                we need special handling.  */
6853             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6854                 == FAIL)
6855               {
6856                 inst.error = _("immediate value is out of range");
6857                 goto failure;
6858               }
6859           }
6860           break;
6861
6862         case OP_RNDQ_I63b:
6863           {
6864             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6865             break;
6866             try_shimm:
6867             po_imm_or_fail (0, 63, TRUE);
6868           }
6869           break;
6870
6871         case OP_RRnpcb:
6872           po_char_or_fail ('[');
6873           po_reg_or_fail  (REG_TYPE_RN);
6874           po_char_or_fail (']');
6875           break;
6876
6877         case OP_RRnpctw:
6878         case OP_RRw:
6879         case OP_oRRw:
6880           po_reg_or_fail (REG_TYPE_RN);
6881           if (skip_past_char (&str, '!') == SUCCESS)
6882             inst.operands[i].writeback = 1;
6883           break;
6884
6885           /* Immediates */
6886         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6887         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6888         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6889         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6890         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6891         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6892         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6893         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6894         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6895         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6896         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6897         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6898
6899         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6900         case OP_oI7b:
6901         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6902         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6903         case OP_oI31b:
6904         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6905         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6906         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6907         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6908
6909           /* Immediate variants */
6910         case OP_oI255c:
6911           po_char_or_fail ('{');
6912           po_imm_or_fail (0, 255, TRUE);
6913           po_char_or_fail ('}');
6914           break;
6915
6916         case OP_I31w:
6917           /* The expression parser chokes on a trailing !, so we have
6918              to find it first and zap it.  */
6919           {
6920             char *s = str;
6921             while (*s && *s != ',')
6922               s++;
6923             if (s[-1] == '!')
6924               {
6925                 s[-1] = '\0';
6926                 inst.operands[i].writeback = 1;
6927               }
6928             po_imm_or_fail (0, 31, TRUE);
6929             if (str == s - 1)
6930               str = s;
6931           }
6932           break;
6933
6934           /* Expressions */
6935         case OP_EXPi:   EXPi:
6936           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6937                                               GE_OPT_PREFIX));
6938           break;
6939
6940         case OP_EXP:
6941           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6942                                               GE_NO_PREFIX));
6943           break;
6944
6945         case OP_EXPr:   EXPr:
6946           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6947                                               GE_NO_PREFIX));
6948           if (inst.reloc.exp.X_op == O_symbol)
6949             {
6950               val = parse_reloc (&str);
6951               if (val == -1)
6952                 {
6953                   inst.error = _("unrecognized relocation suffix");
6954                   goto failure;
6955                 }
6956               else if (val != BFD_RELOC_UNUSED)
6957                 {
6958                   inst.operands[i].imm = val;
6959                   inst.operands[i].hasreloc = 1;
6960                 }
6961             }
6962           break;
6963
6964           /* Operand for MOVW or MOVT.  */
6965         case OP_HALF:
6966           po_misc_or_fail (parse_half (&str));
6967           break;
6968
6969           /* Register or expression.  */
6970         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6971         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6972
6973           /* Register or immediate.  */
6974         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6975         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6976
6977         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6978         IF:
6979           if (!is_immediate_prefix (*str))
6980             goto bad_args;
6981           str++;
6982           val = parse_fpa_immediate (&str);
6983           if (val == FAIL)
6984             goto failure;
6985           /* FPA immediates are encoded as registers 8-15.
6986              parse_fpa_immediate has already applied the offset.  */
6987           inst.operands[i].reg = val;
6988           inst.operands[i].isreg = 1;
6989           break;
6990
6991         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6992         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6993
6994           /* Two kinds of register.  */
6995         case OP_RIWR_RIWC:
6996           {
6997             struct reg_entry *rege = arm_reg_parse_multi (&str);
6998             if (!rege
6999                 || (rege->type != REG_TYPE_MMXWR
7000                     && rege->type != REG_TYPE_MMXWC
7001                     && rege->type != REG_TYPE_MMXWCG))
7002               {
7003                 inst.error = _("iWMMXt data or control register expected");
7004                 goto failure;
7005               }
7006             inst.operands[i].reg = rege->number;
7007             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
7008           }
7009           break;
7010
7011         case OP_RIWC_RIWG:
7012           {
7013             struct reg_entry *rege = arm_reg_parse_multi (&str);
7014             if (!rege
7015                 || (rege->type != REG_TYPE_MMXWC
7016                     && rege->type != REG_TYPE_MMXWCG))
7017               {
7018                 inst.error = _("iWMMXt control register expected");
7019                 goto failure;
7020               }
7021             inst.operands[i].reg = rege->number;
7022             inst.operands[i].isreg = 1;
7023           }
7024           break;
7025
7026           /* Misc */
7027         case OP_CPSF:    val = parse_cps_flags (&str);          break;
7028         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
7029         case OP_oROR:    val = parse_ror (&str);                break;
7030         case OP_COND:    val = parse_cond (&str);               break;
7031         case OP_oBARRIER_I15:
7032           po_barrier_or_imm (str); break;
7033           immediate:
7034           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
7035             goto failure;
7036           break;
7037
7038         case OP_wPSR:
7039         case OP_rPSR:
7040           po_reg_or_goto (REG_TYPE_RNB, try_psr);
7041           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
7042             {
7043               inst.error = _("Banked registers are not available with this "
7044                              "architecture.");
7045               goto failure;
7046             }
7047           break;
7048           try_psr:
7049           val = parse_psr (&str, op_parse_code == OP_wPSR);
7050           break;
7051
7052         case OP_APSR_RR:
7053           po_reg_or_goto (REG_TYPE_RN, try_apsr);
7054           break;
7055           try_apsr:
7056           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
7057              instruction).  */
7058           if (strncasecmp (str, "APSR_", 5) == 0)
7059             {
7060               unsigned found = 0;
7061               str += 5;
7062               while (found < 15)
7063                 switch (*str++)
7064                   {
7065                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7066                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7067                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7068                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7069                   default: found = 16;
7070                   }
7071               if (found != 15)
7072                 goto failure;
7073               inst.operands[i].isvec = 1;
7074               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7075               inst.operands[i].reg = REG_PC;
7076             }
7077           else
7078             goto failure;
7079           break;
7080
7081         case OP_TB:
7082           po_misc_or_fail (parse_tb (&str));
7083           break;
7084
7085           /* Register lists.  */
7086         case OP_REGLST:
7087           val = parse_reg_list (&str);
7088           if (*str == '^')
7089             {
7090               inst.operands[i].writeback = 1;
7091               str++;
7092             }
7093           break;
7094
7095         case OP_VRSLST:
7096           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7097           break;
7098
7099         case OP_VRDLST:
7100           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7101           break;
7102
7103         case OP_VRSDLST:
7104           /* Allow Q registers too.  */
7105           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7106                                     REGLIST_NEON_D);
7107           if (val == FAIL)
7108             {
7109               inst.error = NULL;
7110               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7111                                         REGLIST_VFP_S);
7112               inst.operands[i].issingle = 1;
7113             }
7114           break;
7115
7116         case OP_NRDLST:
7117           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7118                                     REGLIST_NEON_D);
7119           break;
7120
7121         case OP_NSTRLST:
7122           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7123                                            &inst.operands[i].vectype);
7124           break;
7125
7126           /* Addressing modes */
7127         case OP_ADDR:
7128           po_misc_or_fail (parse_address (&str, i));
7129           break;
7130
7131         case OP_ADDRGLDR:
7132           po_misc_or_fail_no_backtrack (
7133             parse_address_group_reloc (&str, i, GROUP_LDR));
7134           break;
7135
7136         case OP_ADDRGLDRS:
7137           po_misc_or_fail_no_backtrack (
7138             parse_address_group_reloc (&str, i, GROUP_LDRS));
7139           break;
7140
7141         case OP_ADDRGLDC:
7142           po_misc_or_fail_no_backtrack (
7143             parse_address_group_reloc (&str, i, GROUP_LDC));
7144           break;
7145
7146         case OP_SH:
7147           po_misc_or_fail (parse_shifter_operand (&str, i));
7148           break;
7149
7150         case OP_SHG:
7151           po_misc_or_fail_no_backtrack (
7152             parse_shifter_operand_group_reloc (&str, i));
7153           break;
7154
7155         case OP_oSHll:
7156           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7157           break;
7158
7159         case OP_oSHar:
7160           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7161           break;
7162
7163         case OP_oSHllar:
7164           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7165           break;
7166
7167         default:
7168           as_fatal (_("unhandled operand code %d"), op_parse_code);
7169         }
7170
7171       /* Various value-based sanity checks and shared operations.  We
7172          do not signal immediate failures for the register constraints;
7173          this allows a syntax error to take precedence.  */
7174       switch (op_parse_code)
7175         {
7176         case OP_oRRnpc:
7177         case OP_RRnpc:
7178         case OP_RRnpcb:
7179         case OP_RRw:
7180         case OP_oRRw:
7181         case OP_RRnpc_I0:
7182           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7183             inst.error = BAD_PC;
7184           break;
7185
7186         case OP_oRRnpcsp:
7187         case OP_RRnpcsp:
7188           if (inst.operands[i].isreg)
7189             {
7190               if (inst.operands[i].reg == REG_PC)
7191                 inst.error = BAD_PC;
7192               else if (inst.operands[i].reg == REG_SP)
7193                 inst.error = BAD_SP;
7194             }
7195           break;
7196
7197         case OP_RRnpctw:
7198           if (inst.operands[i].isreg
7199               && inst.operands[i].reg == REG_PC
7200               && (inst.operands[i].writeback || thumb))
7201             inst.error = BAD_PC;
7202           break;
7203
7204         case OP_CPSF:
7205         case OP_ENDI:
7206         case OP_oROR:
7207         case OP_wPSR:
7208         case OP_rPSR:
7209         case OP_COND:
7210         case OP_oBARRIER_I15:
7211         case OP_REGLST:
7212         case OP_VRSLST:
7213         case OP_VRDLST:
7214         case OP_VRSDLST:
7215         case OP_NRDLST:
7216         case OP_NSTRLST:
7217           if (val == FAIL)
7218             goto failure;
7219           inst.operands[i].imm = val;
7220           break;
7221
7222         default:
7223           break;
7224         }
7225
7226       /* If we get here, this operand was successfully parsed.  */
7227       inst.operands[i].present = 1;
7228       continue;
7229
7230     bad_args:
7231       inst.error = BAD_ARGS;
7232
7233     failure:
7234       if (!backtrack_pos)
7235         {
7236           /* The parse routine should already have set inst.error, but set a
7237              default here just in case.  */
7238           if (!inst.error)
7239             inst.error = _("syntax error");
7240           return FAIL;
7241         }
7242
7243       /* Do not backtrack over a trailing optional argument that
7244          absorbed some text.  We will only fail again, with the
7245          'garbage following instruction' error message, which is
7246          probably less helpful than the current one.  */
7247       if (backtrack_index == i && backtrack_pos != str
7248           && upat[i+1] == OP_stop)
7249         {
7250           if (!inst.error)
7251             inst.error = _("syntax error");
7252           return FAIL;
7253         }
7254
7255       /* Try again, skipping the optional argument at backtrack_pos.  */
7256       str = backtrack_pos;
7257       inst.error = backtrack_error;
7258       inst.operands[backtrack_index].present = 0;
7259       i = backtrack_index;
7260       backtrack_pos = 0;
7261     }
7262
7263   /* Check that we have parsed all the arguments.  */
7264   if (*str != '\0' && !inst.error)
7265     inst.error = _("garbage following instruction");
7266
7267   return inst.error ? FAIL : SUCCESS;
7268 }
7269
7270 #undef po_char_or_fail
7271 #undef po_reg_or_fail
7272 #undef po_reg_or_goto
7273 #undef po_imm_or_fail
7274 #undef po_scalar_or_fail
7275 #undef po_barrier_or_imm
7276
7277 /* Shorthand macro for instruction encoding functions issuing errors.  */
7278 #define constraint(expr, err)                   \
7279   do                                            \
7280     {                                           \
7281       if (expr)                                 \
7282         {                                       \
7283           inst.error = err;                     \
7284           return;                               \
7285         }                                       \
7286     }                                           \
7287   while (0)
7288
7289 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7290    instructions are unpredictable if these registers are used.  This
7291    is the BadReg predicate in ARM's Thumb-2 documentation.  */
7292 #define reject_bad_reg(reg)                             \
7293   do                                                    \
7294    if (reg == REG_SP || reg == REG_PC)                  \
7295      {                                                  \
7296        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
7297        return;                                          \
7298      }                                                  \
7299   while (0)
7300
7301 /* If REG is R13 (the stack pointer), warn that its use is
7302    deprecated.  */
7303 #define warn_deprecated_sp(reg)                 \
7304   do                                            \
7305     if (warn_on_deprecated && reg == REG_SP)    \
7306        as_tsktsk (_("use of r13 is deprecated"));       \
7307   while (0)
7308
7309 /* Functions for operand encoding.  ARM, then Thumb.  */
7310
7311 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7312
7313 /* If the current inst is scalar ARMv8.2 fp16 instruction, do special encoding.
7314
7315    The only binary encoding difference is the Coprocessor number.  Coprocessor
7316    9 is used for half-precision calculations or conversions.  The format of the
7317    instruction is the same as the equivalent Coprocessor 10 instruction that
7318    exists for Single-Precision operation.  */
7319
7320 static void
7321 do_scalar_fp16_v82_encode (void)
7322 {
7323   if (inst.cond != COND_ALWAYS)
7324     as_warn (_("ARMv8.2 scalar fp16 instruction cannot be conditional,"
7325                " the behaviour is UNPREDICTABLE"));
7326   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
7327               _(BAD_FP16));
7328
7329   inst.instruction = (inst.instruction & 0xfffff0ff) | 0x900;
7330   mark_feature_used (&arm_ext_fp16);
7331 }
7332
7333 /* If VAL can be encoded in the immediate field of an ARM instruction,
7334    return the encoded form.  Otherwise, return FAIL.  */
7335
7336 static unsigned int
7337 encode_arm_immediate (unsigned int val)
7338 {
7339   unsigned int a, i;
7340
7341   if (val <= 0xff)
7342     return val;
7343
7344   for (i = 2; i < 32; i += 2)
7345     if ((a = rotate_left (val, i)) <= 0xff)
7346       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7347
7348   return FAIL;
7349 }
7350
7351 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7352    return the encoded form.  Otherwise, return FAIL.  */
7353 static unsigned int
7354 encode_thumb32_immediate (unsigned int val)
7355 {
7356   unsigned int a, i;
7357
7358   if (val <= 0xff)
7359     return val;
7360
7361   for (i = 1; i <= 24; i++)
7362     {
7363       a = val >> i;
7364       if ((val & ~(0xff << i)) == 0)
7365         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7366     }
7367
7368   a = val & 0xff;
7369   if (val == ((a << 16) | a))
7370     return 0x100 | a;
7371   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7372     return 0x300 | a;
7373
7374   a = val & 0xff00;
7375   if (val == ((a << 16) | a))
7376     return 0x200 | (a >> 8);
7377
7378   return FAIL;
7379 }
7380 /* Encode a VFP SP or DP register number into inst.instruction.  */
7381
7382 static void
7383 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7384 {
7385   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7386       && reg > 15)
7387     {
7388       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7389         {
7390           if (thumb_mode)
7391             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7392                                     fpu_vfp_ext_d32);
7393           else
7394             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7395                                     fpu_vfp_ext_d32);
7396         }
7397       else
7398         {
7399           first_error (_("D register out of range for selected VFP version"));
7400           return;
7401         }
7402     }
7403
7404   switch (pos)
7405     {
7406     case VFP_REG_Sd:
7407       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7408       break;
7409
7410     case VFP_REG_Sn:
7411       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7412       break;
7413
7414     case VFP_REG_Sm:
7415       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7416       break;
7417
7418     case VFP_REG_Dd:
7419       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7420       break;
7421
7422     case VFP_REG_Dn:
7423       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7424       break;
7425
7426     case VFP_REG_Dm:
7427       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7428       break;
7429
7430     default:
7431       abort ();
7432     }
7433 }
7434
7435 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7436    if any, is handled by md_apply_fix.   */
7437 static void
7438 encode_arm_shift (int i)
7439 {
7440   /* register-shifted register.  */
7441   if (inst.operands[i].immisreg)
7442     {
7443       int op_index;
7444       for (op_index = 0; op_index <= i; ++op_index)
7445         {
7446           /* Check the operand only when it's presented.  In pre-UAL syntax,
7447              if the destination register is the same as the first operand, two
7448              register form of the instruction can be used.  */
7449           if (inst.operands[op_index].present && inst.operands[op_index].isreg
7450               && inst.operands[op_index].reg == REG_PC)
7451             as_warn (UNPRED_REG ("r15"));
7452         }
7453
7454       if (inst.operands[i].imm == REG_PC)
7455         as_warn (UNPRED_REG ("r15"));
7456     }
7457
7458   if (inst.operands[i].shift_kind == SHIFT_RRX)
7459     inst.instruction |= SHIFT_ROR << 5;
7460   else
7461     {
7462       inst.instruction |= inst.operands[i].shift_kind << 5;
7463       if (inst.operands[i].immisreg)
7464         {
7465           inst.instruction |= SHIFT_BY_REG;
7466           inst.instruction |= inst.operands[i].imm << 8;
7467         }
7468       else
7469         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7470     }
7471 }
7472
7473 static void
7474 encode_arm_shifter_operand (int i)
7475 {
7476   if (inst.operands[i].isreg)
7477     {
7478       inst.instruction |= inst.operands[i].reg;
7479       encode_arm_shift (i);
7480     }
7481   else
7482     {
7483       inst.instruction |= INST_IMMEDIATE;
7484       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7485         inst.instruction |= inst.operands[i].imm;
7486     }
7487 }
7488
7489 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7490 static void
7491 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7492 {
7493   /* PR 14260:
7494      Generate an error if the operand is not a register.  */
7495   constraint (!inst.operands[i].isreg,
7496               _("Instruction does not support =N addresses"));
7497
7498   inst.instruction |= inst.operands[i].reg << 16;
7499
7500   if (inst.operands[i].preind)
7501     {
7502       if (is_t)
7503         {
7504           inst.error = _("instruction does not accept preindexed addressing");
7505           return;
7506         }
7507       inst.instruction |= PRE_INDEX;
7508       if (inst.operands[i].writeback)
7509         inst.instruction |= WRITE_BACK;
7510
7511     }
7512   else if (inst.operands[i].postind)
7513     {
7514       gas_assert (inst.operands[i].writeback);
7515       if (is_t)
7516         inst.instruction |= WRITE_BACK;
7517     }
7518   else /* unindexed - only for coprocessor */
7519     {
7520       inst.error = _("instruction does not accept unindexed addressing");
7521       return;
7522     }
7523
7524   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7525       && (((inst.instruction & 0x000f0000) >> 16)
7526           == ((inst.instruction & 0x0000f000) >> 12)))
7527     as_warn ((inst.instruction & LOAD_BIT)
7528              ? _("destination register same as write-back base")
7529              : _("source register same as write-back base"));
7530 }
7531
7532 /* inst.operands[i] was set up by parse_address.  Encode it into an
7533    ARM-format mode 2 load or store instruction.  If is_t is true,
7534    reject forms that cannot be used with a T instruction (i.e. not
7535    post-indexed).  */
7536 static void
7537 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7538 {
7539   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7540
7541   encode_arm_addr_mode_common (i, is_t);
7542
7543   if (inst.operands[i].immisreg)
7544     {
7545       constraint ((inst.operands[i].imm == REG_PC
7546                    || (is_pc && inst.operands[i].writeback)),
7547                   BAD_PC_ADDRESSING);
7548       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7549       inst.instruction |= inst.operands[i].imm;
7550       if (!inst.operands[i].negative)
7551         inst.instruction |= INDEX_UP;
7552       if (inst.operands[i].shifted)
7553         {
7554           if (inst.operands[i].shift_kind == SHIFT_RRX)
7555             inst.instruction |= SHIFT_ROR << 5;
7556           else
7557             {
7558               inst.instruction |= inst.operands[i].shift_kind << 5;
7559               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7560             }
7561         }
7562     }
7563   else /* immediate offset in inst.reloc */
7564     {
7565       if (is_pc && !inst.reloc.pc_rel)
7566         {
7567           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7568
7569           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7570              cannot use PC in addressing.
7571              PC cannot be used in writeback addressing, either.  */
7572           constraint ((is_t || inst.operands[i].writeback),
7573                       BAD_PC_ADDRESSING);
7574
7575           /* Use of PC in str is deprecated for ARMv7.  */
7576           if (warn_on_deprecated
7577               && !is_load
7578               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7579             as_tsktsk (_("use of PC in this instruction is deprecated"));
7580         }
7581
7582       if (inst.reloc.type == BFD_RELOC_UNUSED)
7583         {
7584           /* Prefer + for zero encoded value.  */
7585           if (!inst.operands[i].negative)
7586             inst.instruction |= INDEX_UP;
7587           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7588         }
7589     }
7590 }
7591
7592 /* inst.operands[i] was set up by parse_address.  Encode it into an
7593    ARM-format mode 3 load or store instruction.  Reject forms that
7594    cannot be used with such instructions.  If is_t is true, reject
7595    forms that cannot be used with a T instruction (i.e. not
7596    post-indexed).  */
7597 static void
7598 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7599 {
7600   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7601     {
7602       inst.error = _("instruction does not accept scaled register index");
7603       return;
7604     }
7605
7606   encode_arm_addr_mode_common (i, is_t);
7607
7608   if (inst.operands[i].immisreg)
7609     {
7610       constraint ((inst.operands[i].imm == REG_PC
7611                    || (is_t && inst.operands[i].reg == REG_PC)),
7612                   BAD_PC_ADDRESSING);
7613       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7614                   BAD_PC_WRITEBACK);
7615       inst.instruction |= inst.operands[i].imm;
7616       if (!inst.operands[i].negative)
7617         inst.instruction |= INDEX_UP;
7618     }
7619   else /* immediate offset in inst.reloc */
7620     {
7621       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7622                    && inst.operands[i].writeback),
7623                   BAD_PC_WRITEBACK);
7624       inst.instruction |= HWOFFSET_IMM;
7625       if (inst.reloc.type == BFD_RELOC_UNUSED)
7626         {
7627           /* Prefer + for zero encoded value.  */
7628           if (!inst.operands[i].negative)
7629             inst.instruction |= INDEX_UP;
7630
7631           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7632         }
7633     }
7634 }
7635
7636 /* Write immediate bits [7:0] to the following locations:
7637
7638   |28/24|23     19|18 16|15                    4|3     0|
7639   |  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|
7640
7641   This function is used by VMOV/VMVN/VORR/VBIC.  */
7642
7643 static void
7644 neon_write_immbits (unsigned immbits)
7645 {
7646   inst.instruction |= immbits & 0xf;
7647   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7648   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7649 }
7650
7651 /* Invert low-order SIZE bits of XHI:XLO.  */
7652
7653 static void
7654 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7655 {
7656   unsigned immlo = xlo ? *xlo : 0;
7657   unsigned immhi = xhi ? *xhi : 0;
7658
7659   switch (size)
7660     {
7661     case 8:
7662       immlo = (~immlo) & 0xff;
7663       break;
7664
7665     case 16:
7666       immlo = (~immlo) & 0xffff;
7667       break;
7668
7669     case 64:
7670       immhi = (~immhi) & 0xffffffff;
7671       /* fall through.  */
7672
7673     case 32:
7674       immlo = (~immlo) & 0xffffffff;
7675       break;
7676
7677     default:
7678       abort ();
7679     }
7680
7681   if (xlo)
7682     *xlo = immlo;
7683
7684   if (xhi)
7685     *xhi = immhi;
7686 }
7687
7688 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7689    A, B, C, D.  */
7690
7691 static int
7692 neon_bits_same_in_bytes (unsigned imm)
7693 {
7694   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7695          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7696          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7697          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7698 }
7699
7700 /* For immediate of above form, return 0bABCD.  */
7701
7702 static unsigned
7703 neon_squash_bits (unsigned imm)
7704 {
7705   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7706          | ((imm & 0x01000000) >> 21);
7707 }
7708
7709 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7710
7711 static unsigned
7712 neon_qfloat_bits (unsigned imm)
7713 {
7714   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7715 }
7716
7717 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7718    the instruction. *OP is passed as the initial value of the op field, and
7719    may be set to a different value depending on the constant (i.e.
7720    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7721    MVN).  If the immediate looks like a repeated pattern then also
7722    try smaller element sizes.  */
7723
7724 static int
7725 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7726                          unsigned *immbits, int *op, int size,
7727                          enum neon_el_type type)
7728 {
7729   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7730      float.  */
7731   if (type == NT_float && !float_p)
7732     return FAIL;
7733
7734   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7735     {
7736       if (size != 32 || *op == 1)
7737         return FAIL;
7738       *immbits = neon_qfloat_bits (immlo);
7739       return 0xf;
7740     }
7741
7742   if (size == 64)
7743     {
7744       if (neon_bits_same_in_bytes (immhi)
7745           && neon_bits_same_in_bytes (immlo))
7746         {
7747           if (*op == 1)
7748             return FAIL;
7749           *immbits = (neon_squash_bits (immhi) << 4)
7750                      | neon_squash_bits (immlo);
7751           *op = 1;
7752           return 0xe;
7753         }
7754
7755       if (immhi != immlo)
7756         return FAIL;
7757     }
7758
7759   if (size >= 32)
7760     {
7761       if (immlo == (immlo & 0x000000ff))
7762         {
7763           *immbits = immlo;
7764           return 0x0;
7765         }
7766       else if (immlo == (immlo & 0x0000ff00))
7767         {
7768           *immbits = immlo >> 8;
7769           return 0x2;
7770         }
7771       else if (immlo == (immlo & 0x00ff0000))
7772         {
7773           *immbits = immlo >> 16;
7774           return 0x4;
7775         }
7776       else if (immlo == (immlo & 0xff000000))
7777         {
7778           *immbits = immlo >> 24;
7779           return 0x6;
7780         }
7781       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7782         {
7783           *immbits = (immlo >> 8) & 0xff;
7784           return 0xc;
7785         }
7786       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7787         {
7788           *immbits = (immlo >> 16) & 0xff;
7789           return 0xd;
7790         }
7791
7792       if ((immlo & 0xffff) != (immlo >> 16))
7793         return FAIL;
7794       immlo &= 0xffff;
7795     }
7796
7797   if (size >= 16)
7798     {
7799       if (immlo == (immlo & 0x000000ff))
7800         {
7801           *immbits = immlo;
7802           return 0x8;
7803         }
7804       else if (immlo == (immlo & 0x0000ff00))
7805         {
7806           *immbits = immlo >> 8;
7807           return 0xa;
7808         }
7809
7810       if ((immlo & 0xff) != (immlo >> 8))
7811         return FAIL;
7812       immlo &= 0xff;
7813     }
7814
7815   if (immlo == (immlo & 0x000000ff))
7816     {
7817       /* Don't allow MVN with 8-bit immediate.  */
7818       if (*op == 1)
7819         return FAIL;
7820       *immbits = immlo;
7821       return 0xe;
7822     }
7823
7824   return FAIL;
7825 }
7826
7827 #if defined BFD_HOST_64_BIT
7828 /* Returns TRUE if double precision value V may be cast
7829    to single precision without loss of accuracy.  */
7830
7831 static bfd_boolean
7832 is_double_a_single (bfd_int64_t v)
7833 {
7834   int exp = (int)((v >> 52) & 0x7FF);
7835   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7836
7837   return (exp == 0 || exp == 0x7FF
7838           || (exp >= 1023 - 126 && exp <= 1023 + 127))
7839     && (mantissa & 0x1FFFFFFFl) == 0;
7840 }
7841
7842 /* Returns a double precision value casted to single precision
7843    (ignoring the least significant bits in exponent and mantissa).  */
7844
7845 static int
7846 double_to_single (bfd_int64_t v)
7847 {
7848   int sign = (int) ((v >> 63) & 1l);
7849   int exp = (int) ((v >> 52) & 0x7FF);
7850   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7851
7852   if (exp == 0x7FF)
7853     exp = 0xFF;
7854   else
7855     {
7856       exp = exp - 1023 + 127;
7857       if (exp >= 0xFF)
7858         {
7859           /* Infinity.  */
7860           exp = 0x7F;
7861           mantissa = 0;
7862         }
7863       else if (exp < 0)
7864         {
7865           /* No denormalized numbers.  */
7866           exp = 0;
7867           mantissa = 0;
7868         }
7869     }
7870   mantissa >>= 29;
7871   return (sign << 31) | (exp << 23) | mantissa;
7872 }
7873 #endif /* BFD_HOST_64_BIT */
7874
7875 enum lit_type
7876 {
7877   CONST_THUMB,
7878   CONST_ARM,
7879   CONST_VEC
7880 };
7881
7882 static void do_vfp_nsyn_opcode (const char *);
7883
7884 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7885    Determine whether it can be performed with a move instruction; if
7886    it can, convert inst.instruction to that move instruction and
7887    return TRUE; if it can't, convert inst.instruction to a literal-pool
7888    load and return FALSE.  If this is not a valid thing to do in the
7889    current context, set inst.error and return TRUE.
7890
7891    inst.operands[i] describes the destination register.  */
7892
7893 static bfd_boolean
7894 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7895 {
7896   unsigned long tbit;
7897   bfd_boolean thumb_p = (t == CONST_THUMB);
7898   bfd_boolean arm_p   = (t == CONST_ARM);
7899
7900   if (thumb_p)
7901     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7902   else
7903     tbit = LOAD_BIT;
7904
7905   if ((inst.instruction & tbit) == 0)
7906     {
7907       inst.error = _("invalid pseudo operation");
7908       return TRUE;
7909     }
7910
7911   if (inst.reloc.exp.X_op != O_constant
7912       && inst.reloc.exp.X_op != O_symbol
7913       && inst.reloc.exp.X_op != O_big)
7914     {
7915       inst.error = _("constant expression expected");
7916       return TRUE;
7917     }
7918
7919   if (inst.reloc.exp.X_op == O_constant
7920       || inst.reloc.exp.X_op == O_big)
7921     {
7922 #if defined BFD_HOST_64_BIT
7923       bfd_int64_t v;
7924 #else
7925       offsetT v;
7926 #endif
7927       if (inst.reloc.exp.X_op == O_big)
7928         {
7929           LITTLENUM_TYPE w[X_PRECISION];
7930           LITTLENUM_TYPE * l;
7931
7932           if (inst.reloc.exp.X_add_number == -1)
7933             {
7934               gen_to_words (w, X_PRECISION, E_PRECISION);
7935               l = w;
7936               /* FIXME: Should we check words w[2..5] ?  */
7937             }
7938           else
7939             l = generic_bignum;
7940
7941 #if defined BFD_HOST_64_BIT
7942           v =
7943             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
7944                   << LITTLENUM_NUMBER_OF_BITS)
7945                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
7946                 << LITTLENUM_NUMBER_OF_BITS)
7947                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
7948               << LITTLENUM_NUMBER_OF_BITS)
7949              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
7950 #else
7951           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
7952             |  (l[0] & LITTLENUM_MASK);
7953 #endif
7954         }
7955       else
7956         v = inst.reloc.exp.X_add_number;
7957
7958       if (!inst.operands[i].issingle)
7959         {
7960           if (thumb_p)
7961             {
7962               /* LDR should not use lead in a flag-setting instruction being
7963                  chosen so we do not check whether movs can be used.  */
7964
7965               if ((ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
7966                   || ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
7967                   && inst.operands[i].reg != 13
7968                   && inst.operands[i].reg != 15)
7969                 {
7970                   /* Check if on thumb2 it can be done with a mov.w, mvn or
7971                      movw instruction.  */
7972                   unsigned int newimm;
7973                   bfd_boolean isNegated;
7974
7975                   newimm = encode_thumb32_immediate (v);
7976                   if (newimm != (unsigned int) FAIL)
7977                     isNegated = FALSE;
7978                   else
7979                     {
7980                       newimm = encode_thumb32_immediate (~v);
7981                       if (newimm != (unsigned int) FAIL)
7982                         isNegated = TRUE;
7983                     }
7984
7985                   /* The number can be loaded with a mov.w or mvn
7986                      instruction.  */
7987                   if (newimm != (unsigned int) FAIL
7988                       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
7989                     {
7990                       inst.instruction = (0xf04f0000  /*  MOV.W.  */
7991                                           | (inst.operands[i].reg << 8));
7992                       /* Change to MOVN.  */
7993                       inst.instruction |= (isNegated ? 0x200000 : 0);
7994                       inst.instruction |= (newimm & 0x800) << 15;
7995                       inst.instruction |= (newimm & 0x700) << 4;
7996                       inst.instruction |= (newimm & 0x0ff);
7997                       return TRUE;
7998                     }
7999                   /* The number can be loaded with a movw instruction.  */
8000                   else if ((v & ~0xFFFF) == 0
8001                            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m))
8002                     {
8003                       int imm = v & 0xFFFF;
8004
8005                       inst.instruction = 0xf2400000;  /* MOVW.  */
8006                       inst.instruction |= (inst.operands[i].reg << 8);
8007                       inst.instruction |= (imm & 0xf000) << 4;
8008                       inst.instruction |= (imm & 0x0800) << 15;
8009                       inst.instruction |= (imm & 0x0700) << 4;
8010                       inst.instruction |= (imm & 0x00ff);
8011                       return TRUE;
8012                     }
8013                 }
8014             }
8015           else if (arm_p)
8016             {
8017               int value = encode_arm_immediate (v);
8018
8019               if (value != FAIL)
8020                 {
8021                   /* This can be done with a mov instruction.  */
8022                   inst.instruction &= LITERAL_MASK;
8023                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
8024                   inst.instruction |= value & 0xfff;
8025                   return TRUE;
8026                 }
8027
8028               value = encode_arm_immediate (~ v);
8029               if (value != FAIL)
8030                 {
8031                   /* This can be done with a mvn instruction.  */
8032                   inst.instruction &= LITERAL_MASK;
8033                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
8034                   inst.instruction |= value & 0xfff;
8035                   return TRUE;
8036                 }
8037             }
8038           else if (t == CONST_VEC && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1))
8039             {
8040               int op = 0;
8041               unsigned immbits = 0;
8042               unsigned immlo = inst.operands[1].imm;
8043               unsigned immhi = inst.operands[1].regisimm
8044                 ? inst.operands[1].reg
8045                 : inst.reloc.exp.X_unsigned
8046                 ? 0
8047                 : ((bfd_int64_t)((int) immlo)) >> 32;
8048               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8049                                                    &op, 64, NT_invtype);
8050
8051               if (cmode == FAIL)
8052                 {
8053                   neon_invert_size (&immlo, &immhi, 64);
8054                   op = !op;
8055                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
8056                                                    &op, 64, NT_invtype);
8057                 }
8058
8059               if (cmode != FAIL)
8060                 {
8061                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
8062                     | (1 << 23)
8063                     | (cmode << 8)
8064                     | (op << 5)
8065                     | (1 << 4);
8066
8067                   /* Fill other bits in vmov encoding for both thumb and arm.  */
8068                   if (thumb_mode)
8069                     inst.instruction |= (0x7U << 29) | (0xF << 24);
8070                   else
8071                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
8072                   neon_write_immbits (immbits);
8073                   return TRUE;
8074                 }
8075             }
8076         }
8077
8078       if (t == CONST_VEC)
8079         {
8080           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
8081           if (inst.operands[i].issingle
8082               && is_quarter_float (inst.operands[1].imm)
8083               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
8084             {
8085               inst.operands[1].imm =
8086                 neon_qfloat_bits (v);
8087               do_vfp_nsyn_opcode ("fconsts");
8088               return TRUE;
8089             }
8090
8091           /* If our host does not support a 64-bit type then we cannot perform
8092              the following optimization.  This mean that there will be a
8093              discrepancy between the output produced by an assembler built for
8094              a 32-bit-only host and the output produced from a 64-bit host, but
8095              this cannot be helped.  */
8096 #if defined BFD_HOST_64_BIT
8097           else if (!inst.operands[1].issingle
8098                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
8099             {
8100               if (is_double_a_single (v)
8101                   && is_quarter_float (double_to_single (v)))
8102                 {
8103                   inst.operands[1].imm =
8104                     neon_qfloat_bits (double_to_single (v));
8105                   do_vfp_nsyn_opcode ("fconstd");
8106                   return TRUE;
8107                 }
8108             }
8109 #endif
8110         }
8111     }
8112
8113   if (add_to_lit_pool ((!inst.operands[i].isvec
8114                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8115     return TRUE;
8116
8117   inst.operands[1].reg = REG_PC;
8118   inst.operands[1].isreg = 1;
8119   inst.operands[1].preind = 1;
8120   inst.reloc.pc_rel = 1;
8121   inst.reloc.type = (thumb_p
8122                      ? BFD_RELOC_ARM_THUMB_OFFSET
8123                      : (mode_3
8124                         ? BFD_RELOC_ARM_HWLITERAL
8125                         : BFD_RELOC_ARM_LITERAL));
8126   return FALSE;
8127 }
8128
8129 /* inst.operands[i] was set up by parse_address.  Encode it into an
8130    ARM-format instruction.  Reject all forms which cannot be encoded
8131    into a coprocessor load/store instruction.  If wb_ok is false,
8132    reject use of writeback; if unind_ok is false, reject use of
8133    unindexed addressing.  If reloc_override is not 0, use it instead
8134    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8135    (in which case it is preserved).  */
8136
8137 static int
8138 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8139 {
8140   if (!inst.operands[i].isreg)
8141     {
8142       /* PR 18256 */
8143       if (! inst.operands[0].isvec)
8144         {
8145           inst.error = _("invalid co-processor operand");
8146           return FAIL;
8147         }
8148       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8149         return SUCCESS;
8150     }
8151
8152   inst.instruction |= inst.operands[i].reg << 16;
8153
8154   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8155
8156   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8157     {
8158       gas_assert (!inst.operands[i].writeback);
8159       if (!unind_ok)
8160         {
8161           inst.error = _("instruction does not support unindexed addressing");
8162           return FAIL;
8163         }
8164       inst.instruction |= inst.operands[i].imm;
8165       inst.instruction |= INDEX_UP;
8166       return SUCCESS;
8167     }
8168
8169   if (inst.operands[i].preind)
8170     inst.instruction |= PRE_INDEX;
8171
8172   if (inst.operands[i].writeback)
8173     {
8174       if (inst.operands[i].reg == REG_PC)
8175         {
8176           inst.error = _("pc may not be used with write-back");
8177           return FAIL;
8178         }
8179       if (!wb_ok)
8180         {
8181           inst.error = _("instruction does not support writeback");
8182           return FAIL;
8183         }
8184       inst.instruction |= WRITE_BACK;
8185     }
8186
8187   if (reloc_override)
8188     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
8189   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
8190             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
8191            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
8192     {
8193       if (thumb_mode)
8194         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8195       else
8196         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
8197     }
8198
8199   /* Prefer + for zero encoded value.  */
8200   if (!inst.operands[i].negative)
8201     inst.instruction |= INDEX_UP;
8202
8203   return SUCCESS;
8204 }
8205
8206 /* Functions for instruction encoding, sorted by sub-architecture.
8207    First some generics; their names are taken from the conventional
8208    bit positions for register arguments in ARM format instructions.  */
8209
8210 static void
8211 do_noargs (void)
8212 {
8213 }
8214
8215 static void
8216 do_rd (void)
8217 {
8218   inst.instruction |= inst.operands[0].reg << 12;
8219 }
8220
8221 static void
8222 do_rn (void)
8223 {
8224   inst.instruction |= inst.operands[0].reg << 16;
8225 }
8226
8227 static void
8228 do_rd_rm (void)
8229 {
8230   inst.instruction |= inst.operands[0].reg << 12;
8231   inst.instruction |= inst.operands[1].reg;
8232 }
8233
8234 static void
8235 do_rm_rn (void)
8236 {
8237   inst.instruction |= inst.operands[0].reg;
8238   inst.instruction |= inst.operands[1].reg << 16;
8239 }
8240
8241 static void
8242 do_rd_rn (void)
8243 {
8244   inst.instruction |= inst.operands[0].reg << 12;
8245   inst.instruction |= inst.operands[1].reg << 16;
8246 }
8247
8248 static void
8249 do_rn_rd (void)
8250 {
8251   inst.instruction |= inst.operands[0].reg << 16;
8252   inst.instruction |= inst.operands[1].reg << 12;
8253 }
8254
8255 static void
8256 do_tt (void)
8257 {
8258   inst.instruction |= inst.operands[0].reg << 8;
8259   inst.instruction |= inst.operands[1].reg << 16;
8260 }
8261
8262 static bfd_boolean
8263 check_obsolete (const arm_feature_set *feature, const char *msg)
8264 {
8265   if (ARM_CPU_IS_ANY (cpu_variant))
8266     {
8267       as_tsktsk ("%s", msg);
8268       return TRUE;
8269     }
8270   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8271     {
8272       as_bad ("%s", msg);
8273       return TRUE;
8274     }
8275
8276   return FALSE;
8277 }
8278
8279 static void
8280 do_rd_rm_rn (void)
8281 {
8282   unsigned Rn = inst.operands[2].reg;
8283   /* Enforce restrictions on SWP instruction.  */
8284   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8285     {
8286       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8287                   _("Rn must not overlap other operands"));
8288
8289       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8290        */
8291       if (!check_obsolete (&arm_ext_v8,
8292                            _("swp{b} use is obsoleted for ARMv8 and later"))
8293           && warn_on_deprecated
8294           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8295         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8296     }
8297
8298   inst.instruction |= inst.operands[0].reg << 12;
8299   inst.instruction |= inst.operands[1].reg;
8300   inst.instruction |= Rn << 16;
8301 }
8302
8303 static void
8304 do_rd_rn_rm (void)
8305 {
8306   inst.instruction |= inst.operands[0].reg << 12;
8307   inst.instruction |= inst.operands[1].reg << 16;
8308   inst.instruction |= inst.operands[2].reg;
8309 }
8310
8311 static void
8312 do_rm_rd_rn (void)
8313 {
8314   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8315   constraint (((inst.reloc.exp.X_op != O_constant
8316                 && inst.reloc.exp.X_op != O_illegal)
8317                || inst.reloc.exp.X_add_number != 0),
8318               BAD_ADDR_MODE);
8319   inst.instruction |= inst.operands[0].reg;
8320   inst.instruction |= inst.operands[1].reg << 12;
8321   inst.instruction |= inst.operands[2].reg << 16;
8322 }
8323
8324 static void
8325 do_imm0 (void)
8326 {
8327   inst.instruction |= inst.operands[0].imm;
8328 }
8329
8330 static void
8331 do_rd_cpaddr (void)
8332 {
8333   inst.instruction |= inst.operands[0].reg << 12;
8334   encode_arm_cp_address (1, TRUE, TRUE, 0);
8335 }
8336
8337 /* ARM instructions, in alphabetical order by function name (except
8338    that wrapper functions appear immediately after the function they
8339    wrap).  */
8340
8341 /* This is a pseudo-op of the form "adr rd, label" to be converted
8342    into a relative address of the form "add rd, pc, #label-.-8".  */
8343
8344 static void
8345 do_adr (void)
8346 {
8347   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8348
8349   /* Frag hacking will turn this into a sub instruction if the offset turns
8350      out to be negative.  */
8351   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8352   inst.reloc.pc_rel = 1;
8353   inst.reloc.exp.X_add_number -= 8;
8354 }
8355
8356 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8357    into a relative address of the form:
8358    add rd, pc, #low(label-.-8)"
8359    add rd, rd, #high(label-.-8)"  */
8360
8361 static void
8362 do_adrl (void)
8363 {
8364   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8365
8366   /* Frag hacking will turn this into a sub instruction if the offset turns
8367      out to be negative.  */
8368   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8369   inst.reloc.pc_rel            = 1;
8370   inst.size                    = INSN_SIZE * 2;
8371   inst.reloc.exp.X_add_number -= 8;
8372 }
8373
8374 static void
8375 do_arit (void)
8376 {
8377   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
8378               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
8379               THUMB1_RELOC_ONLY);
8380   if (!inst.operands[1].present)
8381     inst.operands[1].reg = inst.operands[0].reg;
8382   inst.instruction |= inst.operands[0].reg << 12;
8383   inst.instruction |= inst.operands[1].reg << 16;
8384   encode_arm_shifter_operand (2);
8385 }
8386
8387 static void
8388 do_barrier (void)
8389 {
8390   if (inst.operands[0].present)
8391     inst.instruction |= inst.operands[0].imm;
8392   else
8393     inst.instruction |= 0xf;
8394 }
8395
8396 static void
8397 do_bfc (void)
8398 {
8399   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8400   constraint (msb > 32, _("bit-field extends past end of register"));
8401   /* The instruction encoding stores the LSB and MSB,
8402      not the LSB and width.  */
8403   inst.instruction |= inst.operands[0].reg << 12;
8404   inst.instruction |= inst.operands[1].imm << 7;
8405   inst.instruction |= (msb - 1) << 16;
8406 }
8407
8408 static void
8409 do_bfi (void)
8410 {
8411   unsigned int msb;
8412
8413   /* #0 in second position is alternative syntax for bfc, which is
8414      the same instruction but with REG_PC in the Rm field.  */
8415   if (!inst.operands[1].isreg)
8416     inst.operands[1].reg = REG_PC;
8417
8418   msb = inst.operands[2].imm + inst.operands[3].imm;
8419   constraint (msb > 32, _("bit-field extends past end of register"));
8420   /* The instruction encoding stores the LSB and MSB,
8421      not the LSB and width.  */
8422   inst.instruction |= inst.operands[0].reg << 12;
8423   inst.instruction |= inst.operands[1].reg;
8424   inst.instruction |= inst.operands[2].imm << 7;
8425   inst.instruction |= (msb - 1) << 16;
8426 }
8427
8428 static void
8429 do_bfx (void)
8430 {
8431   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8432               _("bit-field extends past end of register"));
8433   inst.instruction |= inst.operands[0].reg << 12;
8434   inst.instruction |= inst.operands[1].reg;
8435   inst.instruction |= inst.operands[2].imm << 7;
8436   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8437 }
8438
8439 /* ARM V5 breakpoint instruction (argument parse)
8440      BKPT <16 bit unsigned immediate>
8441      Instruction is not conditional.
8442         The bit pattern given in insns[] has the COND_ALWAYS condition,
8443         and it is an error if the caller tried to override that.  */
8444
8445 static void
8446 do_bkpt (void)
8447 {
8448   /* Top 12 of 16 bits to bits 19:8.  */
8449   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8450
8451   /* Bottom 4 of 16 bits to bits 3:0.  */
8452   inst.instruction |= inst.operands[0].imm & 0xf;
8453 }
8454
8455 static void
8456 encode_branch (int default_reloc)
8457 {
8458   if (inst.operands[0].hasreloc)
8459     {
8460       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8461                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8462                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8463       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8464         ? BFD_RELOC_ARM_PLT32
8465         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8466     }
8467   else
8468     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8469   inst.reloc.pc_rel = 1;
8470 }
8471
8472 static void
8473 do_branch (void)
8474 {
8475 #ifdef OBJ_ELF
8476   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8477     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8478   else
8479 #endif
8480     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8481 }
8482
8483 static void
8484 do_bl (void)
8485 {
8486 #ifdef OBJ_ELF
8487   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8488     {
8489       if (inst.cond == COND_ALWAYS)
8490         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8491       else
8492         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8493     }
8494   else
8495 #endif
8496     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8497 }
8498
8499 /* ARM V5 branch-link-exchange instruction (argument parse)
8500      BLX <target_addr>          ie BLX(1)
8501      BLX{<condition>} <Rm>      ie BLX(2)
8502    Unfortunately, there are two different opcodes for this mnemonic.
8503    So, the insns[].value is not used, and the code here zaps values
8504         into inst.instruction.
8505    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8506
8507 static void
8508 do_blx (void)
8509 {
8510   if (inst.operands[0].isreg)
8511     {
8512       /* Arg is a register; the opcode provided by insns[] is correct.
8513          It is not illegal to do "blx pc", just useless.  */
8514       if (inst.operands[0].reg == REG_PC)
8515         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8516
8517       inst.instruction |= inst.operands[0].reg;
8518     }
8519   else
8520     {
8521       /* Arg is an address; this instruction cannot be executed
8522          conditionally, and the opcode must be adjusted.
8523          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8524          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8525       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8526       inst.instruction = 0xfa000000;
8527       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8528     }
8529 }
8530
8531 static void
8532 do_bx (void)
8533 {
8534   bfd_boolean want_reloc;
8535
8536   if (inst.operands[0].reg == REG_PC)
8537     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8538
8539   inst.instruction |= inst.operands[0].reg;
8540   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8541      it is for ARMv4t or earlier.  */
8542   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8543   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
8544       want_reloc = TRUE;
8545
8546 #ifdef OBJ_ELF
8547   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8548 #endif
8549     want_reloc = FALSE;
8550
8551   if (want_reloc)
8552     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8553 }
8554
8555
8556 /* ARM v5TEJ.  Jump to Jazelle code.  */
8557
8558 static void
8559 do_bxj (void)
8560 {
8561   if (inst.operands[0].reg == REG_PC)
8562     as_tsktsk (_("use of r15 in bxj is not really useful"));
8563
8564   inst.instruction |= inst.operands[0].reg;
8565 }
8566
8567 /* Co-processor data operation:
8568       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8569       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8570 static void
8571 do_cdp (void)
8572 {
8573   inst.instruction |= inst.operands[0].reg << 8;
8574   inst.instruction |= inst.operands[1].imm << 20;
8575   inst.instruction |= inst.operands[2].reg << 12;
8576   inst.instruction |= inst.operands[3].reg << 16;
8577   inst.instruction |= inst.operands[4].reg;
8578   inst.instruction |= inst.operands[5].imm << 5;
8579 }
8580
8581 static void
8582 do_cmp (void)
8583 {
8584   inst.instruction |= inst.operands[0].reg << 16;
8585   encode_arm_shifter_operand (1);
8586 }
8587
8588 /* Transfer between coprocessor and ARM registers.
8589    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8590    MRC2
8591    MCR{cond}
8592    MCR2
8593
8594    No special properties.  */
8595
8596 struct deprecated_coproc_regs_s
8597 {
8598   unsigned cp;
8599   int opc1;
8600   unsigned crn;
8601   unsigned crm;
8602   int opc2;
8603   arm_feature_set deprecated;
8604   arm_feature_set obsoleted;
8605   const char *dep_msg;
8606   const char *obs_msg;
8607 };
8608
8609 #define DEPR_ACCESS_V8 \
8610   N_("This coprocessor register access is deprecated in ARMv8")
8611
8612 /* Table of all deprecated coprocessor registers.  */
8613 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8614 {
8615     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8616      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8617      DEPR_ACCESS_V8, NULL},
8618     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8619      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8620      DEPR_ACCESS_V8, NULL},
8621     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8622      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8623      DEPR_ACCESS_V8, NULL},
8624     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8625      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8626      DEPR_ACCESS_V8, NULL},
8627     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8628      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8629      DEPR_ACCESS_V8, NULL},
8630 };
8631
8632 #undef DEPR_ACCESS_V8
8633
8634 static const size_t deprecated_coproc_reg_count =
8635   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8636
8637 static void
8638 do_co_reg (void)
8639 {
8640   unsigned Rd;
8641   size_t i;
8642
8643   Rd = inst.operands[2].reg;
8644   if (thumb_mode)
8645     {
8646       if (inst.instruction == 0xee000010
8647           || inst.instruction == 0xfe000010)
8648         /* MCR, MCR2  */
8649         reject_bad_reg (Rd);
8650       else
8651         /* MRC, MRC2  */
8652         constraint (Rd == REG_SP, BAD_SP);
8653     }
8654   else
8655     {
8656       /* MCR */
8657       if (inst.instruction == 0xe000010)
8658         constraint (Rd == REG_PC, BAD_PC);
8659     }
8660
8661     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8662       {
8663         const struct deprecated_coproc_regs_s *r =
8664           deprecated_coproc_regs + i;
8665
8666         if (inst.operands[0].reg == r->cp
8667             && inst.operands[1].imm == r->opc1
8668             && inst.operands[3].reg == r->crn
8669             && inst.operands[4].reg == r->crm
8670             && inst.operands[5].imm == r->opc2)
8671           {
8672             if (! ARM_CPU_IS_ANY (cpu_variant)
8673                 && warn_on_deprecated
8674                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8675               as_tsktsk ("%s", r->dep_msg);
8676           }
8677       }
8678
8679   inst.instruction |= inst.operands[0].reg << 8;
8680   inst.instruction |= inst.operands[1].imm << 21;
8681   inst.instruction |= Rd << 12;
8682   inst.instruction |= inst.operands[3].reg << 16;
8683   inst.instruction |= inst.operands[4].reg;
8684   inst.instruction |= inst.operands[5].imm << 5;
8685 }
8686
8687 /* Transfer between coprocessor register and pair of ARM registers.
8688    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8689    MCRR2
8690    MRRC{cond}
8691    MRRC2
8692
8693    Two XScale instructions are special cases of these:
8694
8695      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8696      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8697
8698    Result unpredictable if Rd or Rn is R15.  */
8699
8700 static void
8701 do_co_reg2c (void)
8702 {
8703   unsigned Rd, Rn;
8704
8705   Rd = inst.operands[2].reg;
8706   Rn = inst.operands[3].reg;
8707
8708   if (thumb_mode)
8709     {
8710       reject_bad_reg (Rd);
8711       reject_bad_reg (Rn);
8712     }
8713   else
8714     {
8715       constraint (Rd == REG_PC, BAD_PC);
8716       constraint (Rn == REG_PC, BAD_PC);
8717     }
8718
8719   /* Only check the MRRC{2} variants.  */
8720   if ((inst.instruction & 0x0FF00000) == 0x0C500000)
8721     {
8722        /* If Rd == Rn, error that the operation is
8723           unpredictable (example MRRC p3,#1,r1,r1,c4).  */
8724        constraint (Rd == Rn, BAD_OVERLAP);
8725     }
8726
8727   inst.instruction |= inst.operands[0].reg << 8;
8728   inst.instruction |= inst.operands[1].imm << 4;
8729   inst.instruction |= Rd << 12;
8730   inst.instruction |= Rn << 16;
8731   inst.instruction |= inst.operands[4].reg;
8732 }
8733
8734 static void
8735 do_cpsi (void)
8736 {
8737   inst.instruction |= inst.operands[0].imm << 6;
8738   if (inst.operands[1].present)
8739     {
8740       inst.instruction |= CPSI_MMOD;
8741       inst.instruction |= inst.operands[1].imm;
8742     }
8743 }
8744
8745 static void
8746 do_dbg (void)
8747 {
8748   inst.instruction |= inst.operands[0].imm;
8749 }
8750
8751 static void
8752 do_div (void)
8753 {
8754   unsigned Rd, Rn, Rm;
8755
8756   Rd = inst.operands[0].reg;
8757   Rn = (inst.operands[1].present
8758         ? inst.operands[1].reg : Rd);
8759   Rm = inst.operands[2].reg;
8760
8761   constraint ((Rd == REG_PC), BAD_PC);
8762   constraint ((Rn == REG_PC), BAD_PC);
8763   constraint ((Rm == REG_PC), BAD_PC);
8764
8765   inst.instruction |= Rd << 16;
8766   inst.instruction |= Rn << 0;
8767   inst.instruction |= Rm << 8;
8768 }
8769
8770 static void
8771 do_it (void)
8772 {
8773   /* There is no IT instruction in ARM mode.  We
8774      process it to do the validation as if in
8775      thumb mode, just in case the code gets
8776      assembled for thumb using the unified syntax.  */
8777
8778   inst.size = 0;
8779   if (unified_syntax)
8780     {
8781       set_it_insn_type (IT_INSN);
8782       now_it.mask = (inst.instruction & 0xf) | 0x10;
8783       now_it.cc = inst.operands[0].imm;
8784     }
8785 }
8786
8787 /* If there is only one register in the register list,
8788    then return its register number.  Otherwise return -1.  */
8789 static int
8790 only_one_reg_in_list (int range)
8791 {
8792   int i = ffs (range) - 1;
8793   return (i > 15 || range != (1 << i)) ? -1 : i;
8794 }
8795
8796 static void
8797 encode_ldmstm(int from_push_pop_mnem)
8798 {
8799   int base_reg = inst.operands[0].reg;
8800   int range = inst.operands[1].imm;
8801   int one_reg;
8802
8803   inst.instruction |= base_reg << 16;
8804   inst.instruction |= range;
8805
8806   if (inst.operands[1].writeback)
8807     inst.instruction |= LDM_TYPE_2_OR_3;
8808
8809   if (inst.operands[0].writeback)
8810     {
8811       inst.instruction |= WRITE_BACK;
8812       /* Check for unpredictable uses of writeback.  */
8813       if (inst.instruction & LOAD_BIT)
8814         {
8815           /* Not allowed in LDM type 2.  */
8816           if ((inst.instruction & LDM_TYPE_2_OR_3)
8817               && ((range & (1 << REG_PC)) == 0))
8818             as_warn (_("writeback of base register is UNPREDICTABLE"));
8819           /* Only allowed if base reg not in list for other types.  */
8820           else if (range & (1 << base_reg))
8821             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8822         }
8823       else /* STM.  */
8824         {
8825           /* Not allowed for type 2.  */
8826           if (inst.instruction & LDM_TYPE_2_OR_3)
8827             as_warn (_("writeback of base register is UNPREDICTABLE"));
8828           /* Only allowed if base reg not in list, or first in list.  */
8829           else if ((range & (1 << base_reg))
8830                    && (range & ((1 << base_reg) - 1)))
8831             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8832         }
8833     }
8834
8835   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8836   one_reg = only_one_reg_in_list (range);
8837   if (from_push_pop_mnem && one_reg >= 0)
8838     {
8839       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8840
8841       inst.instruction &= A_COND_MASK;
8842       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8843       inst.instruction |= one_reg << 12;
8844     }
8845 }
8846
8847 static void
8848 do_ldmstm (void)
8849 {
8850   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8851 }
8852
8853 /* ARMv5TE load-consecutive (argument parse)
8854    Mode is like LDRH.
8855
8856      LDRccD R, mode
8857      STRccD R, mode.  */
8858
8859 static void
8860 do_ldrd (void)
8861 {
8862   constraint (inst.operands[0].reg % 2 != 0,
8863               _("first transfer register must be even"));
8864   constraint (inst.operands[1].present
8865               && inst.operands[1].reg != inst.operands[0].reg + 1,
8866               _("can only transfer two consecutive registers"));
8867   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8868   constraint (!inst.operands[2].isreg, _("'[' expected"));
8869
8870   if (!inst.operands[1].present)
8871     inst.operands[1].reg = inst.operands[0].reg + 1;
8872
8873   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8874      register and the first register written; we have to diagnose
8875      overlap between the base and the second register written here.  */
8876
8877   if (inst.operands[2].reg == inst.operands[1].reg
8878       && (inst.operands[2].writeback || inst.operands[2].postind))
8879     as_warn (_("base register written back, and overlaps "
8880                "second transfer register"));
8881
8882   if (!(inst.instruction & V4_STR_BIT))
8883     {
8884       /* For an index-register load, the index register must not overlap the
8885         destination (even if not write-back).  */
8886       if (inst.operands[2].immisreg
8887               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8888               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8889         as_warn (_("index register overlaps transfer register"));
8890     }
8891   inst.instruction |= inst.operands[0].reg << 12;
8892   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8893 }
8894
8895 static void
8896 do_ldrex (void)
8897 {
8898   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8899               || inst.operands[1].postind || inst.operands[1].writeback
8900               || inst.operands[1].immisreg || inst.operands[1].shifted
8901               || inst.operands[1].negative
8902               /* This can arise if the programmer has written
8903                    strex rN, rM, foo
8904                  or if they have mistakenly used a register name as the last
8905                  operand,  eg:
8906                    strex rN, rM, rX
8907                  It is very difficult to distinguish between these two cases
8908                  because "rX" might actually be a label. ie the register
8909                  name has been occluded by a symbol of the same name. So we
8910                  just generate a general 'bad addressing mode' type error
8911                  message and leave it up to the programmer to discover the
8912                  true cause and fix their mistake.  */
8913               || (inst.operands[1].reg == REG_PC),
8914               BAD_ADDR_MODE);
8915
8916   constraint (inst.reloc.exp.X_op != O_constant
8917               || inst.reloc.exp.X_add_number != 0,
8918               _("offset must be zero in ARM encoding"));
8919
8920   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8921
8922   inst.instruction |= inst.operands[0].reg << 12;
8923   inst.instruction |= inst.operands[1].reg << 16;
8924   inst.reloc.type = BFD_RELOC_UNUSED;
8925 }
8926
8927 static void
8928 do_ldrexd (void)
8929 {
8930   constraint (inst.operands[0].reg % 2 != 0,
8931               _("even register required"));
8932   constraint (inst.operands[1].present
8933               && inst.operands[1].reg != inst.operands[0].reg + 1,
8934               _("can only load two consecutive registers"));
8935   /* If op 1 were present and equal to PC, this function wouldn't
8936      have been called in the first place.  */
8937   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8938
8939   inst.instruction |= inst.operands[0].reg << 12;
8940   inst.instruction |= inst.operands[2].reg << 16;
8941 }
8942
8943 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
8944    which is not a multiple of four is UNPREDICTABLE.  */
8945 static void
8946 check_ldr_r15_aligned (void)
8947 {
8948   constraint (!(inst.operands[1].immisreg)
8949               && (inst.operands[0].reg == REG_PC
8950               && inst.operands[1].reg == REG_PC
8951               && (inst.reloc.exp.X_add_number & 0x3)),
8952               _("ldr to register 15 must be 4-byte alligned"));
8953 }
8954
8955 static void
8956 do_ldst (void)
8957 {
8958   inst.instruction |= inst.operands[0].reg << 12;
8959   if (!inst.operands[1].isreg)
8960     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
8961       return;
8962   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
8963   check_ldr_r15_aligned ();
8964 }
8965
8966 static void
8967 do_ldstt (void)
8968 {
8969   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8970      reject [Rn,...].  */
8971   if (inst.operands[1].preind)
8972     {
8973       constraint (inst.reloc.exp.X_op != O_constant
8974                   || inst.reloc.exp.X_add_number != 0,
8975                   _("this instruction requires a post-indexed address"));
8976
8977       inst.operands[1].preind = 0;
8978       inst.operands[1].postind = 1;
8979       inst.operands[1].writeback = 1;
8980     }
8981   inst.instruction |= inst.operands[0].reg << 12;
8982   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8983 }
8984
8985 /* Halfword and signed-byte load/store operations.  */
8986
8987 static void
8988 do_ldstv4 (void)
8989 {
8990   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8991   inst.instruction |= inst.operands[0].reg << 12;
8992   if (!inst.operands[1].isreg)
8993     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
8994       return;
8995   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8996 }
8997
8998 static void
8999 do_ldsttv4 (void)
9000 {
9001   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
9002      reject [Rn,...].  */
9003   if (inst.operands[1].preind)
9004     {
9005       constraint (inst.reloc.exp.X_op != O_constant
9006                   || inst.reloc.exp.X_add_number != 0,
9007                   _("this instruction requires a post-indexed address"));
9008
9009       inst.operands[1].preind = 0;
9010       inst.operands[1].postind = 1;
9011       inst.operands[1].writeback = 1;
9012     }
9013   inst.instruction |= inst.operands[0].reg << 12;
9014   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
9015 }
9016
9017 /* Co-processor register load/store.
9018    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
9019 static void
9020 do_lstc (void)
9021 {
9022   inst.instruction |= inst.operands[0].reg << 8;
9023   inst.instruction |= inst.operands[1].reg << 12;
9024   encode_arm_cp_address (2, TRUE, TRUE, 0);
9025 }
9026
9027 static void
9028 do_mlas (void)
9029 {
9030   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
9031   if (inst.operands[0].reg == inst.operands[1].reg
9032       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
9033       && !(inst.instruction & 0x00400000))
9034     as_tsktsk (_("Rd and Rm should be different in mla"));
9035
9036   inst.instruction |= inst.operands[0].reg << 16;
9037   inst.instruction |= inst.operands[1].reg;
9038   inst.instruction |= inst.operands[2].reg << 8;
9039   inst.instruction |= inst.operands[3].reg << 12;
9040 }
9041
9042 static void
9043 do_mov (void)
9044 {
9045   constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
9046               && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
9047               THUMB1_RELOC_ONLY);
9048   inst.instruction |= inst.operands[0].reg << 12;
9049   encode_arm_shifter_operand (1);
9050 }
9051
9052 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
9053 static void
9054 do_mov16 (void)
9055 {
9056   bfd_vma imm;
9057   bfd_boolean top;
9058
9059   top = (inst.instruction & 0x00400000) != 0;
9060   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
9061               _(":lower16: not allowed in this instruction"));
9062   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
9063               _(":upper16: not allowed in this instruction"));
9064   inst.instruction |= inst.operands[0].reg << 12;
9065   if (inst.reloc.type == BFD_RELOC_UNUSED)
9066     {
9067       imm = inst.reloc.exp.X_add_number;
9068       /* The value is in two pieces: 0:11, 16:19.  */
9069       inst.instruction |= (imm & 0x00000fff);
9070       inst.instruction |= (imm & 0x0000f000) << 4;
9071     }
9072 }
9073
9074 static int
9075 do_vfp_nsyn_mrs (void)
9076 {
9077   if (inst.operands[0].isvec)
9078     {
9079       if (inst.operands[1].reg != 1)
9080         first_error (_("operand 1 must be FPSCR"));
9081       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
9082       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
9083       do_vfp_nsyn_opcode ("fmstat");
9084     }
9085   else if (inst.operands[1].isvec)
9086     do_vfp_nsyn_opcode ("fmrx");
9087   else
9088     return FAIL;
9089
9090   return SUCCESS;
9091 }
9092
9093 static int
9094 do_vfp_nsyn_msr (void)
9095 {
9096   if (inst.operands[0].isvec)
9097     do_vfp_nsyn_opcode ("fmxr");
9098   else
9099     return FAIL;
9100
9101   return SUCCESS;
9102 }
9103
9104 static void
9105 do_vmrs (void)
9106 {
9107   unsigned Rt = inst.operands[0].reg;
9108
9109   if (thumb_mode && Rt == REG_SP)
9110     {
9111       inst.error = BAD_SP;
9112       return;
9113     }
9114
9115   /* APSR_ sets isvec. All other refs to PC are illegal.  */
9116   if (!inst.operands[0].isvec && Rt == REG_PC)
9117     {
9118       inst.error = BAD_PC;
9119       return;
9120     }
9121
9122   /* If we get through parsing the register name, we just insert the number
9123      generated into the instruction without further validation.  */
9124   inst.instruction |= (inst.operands[1].reg << 16);
9125   inst.instruction |= (Rt << 12);
9126 }
9127
9128 static void
9129 do_vmsr (void)
9130 {
9131   unsigned Rt = inst.operands[1].reg;
9132
9133   if (thumb_mode)
9134     reject_bad_reg (Rt);
9135   else if (Rt == REG_PC)
9136     {
9137       inst.error = BAD_PC;
9138       return;
9139     }
9140
9141   /* If we get through parsing the register name, we just insert the number
9142      generated into the instruction without further validation.  */
9143   inst.instruction |= (inst.operands[0].reg << 16);
9144   inst.instruction |= (Rt << 12);
9145 }
9146
9147 static void
9148 do_mrs (void)
9149 {
9150   unsigned br;
9151
9152   if (do_vfp_nsyn_mrs () == SUCCESS)
9153     return;
9154
9155   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9156   inst.instruction |= inst.operands[0].reg << 12;
9157
9158   if (inst.operands[1].isreg)
9159     {
9160       br = inst.operands[1].reg;
9161       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
9162         as_bad (_("bad register for mrs"));
9163     }
9164   else
9165     {
9166       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9167       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9168                   != (PSR_c|PSR_f),
9169                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9170       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9171     }
9172
9173   inst.instruction |= br;
9174 }
9175
9176 /* Two possible forms:
9177       "{C|S}PSR_<field>, Rm",
9178       "{C|S}PSR_f, #expression".  */
9179
9180 static void
9181 do_msr (void)
9182 {
9183   if (do_vfp_nsyn_msr () == SUCCESS)
9184     return;
9185
9186   inst.instruction |= inst.operands[0].imm;
9187   if (inst.operands[1].isreg)
9188     inst.instruction |= inst.operands[1].reg;
9189   else
9190     {
9191       inst.instruction |= INST_IMMEDIATE;
9192       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
9193       inst.reloc.pc_rel = 0;
9194     }
9195 }
9196
9197 static void
9198 do_mul (void)
9199 {
9200   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9201
9202   if (!inst.operands[2].present)
9203     inst.operands[2].reg = inst.operands[0].reg;
9204   inst.instruction |= inst.operands[0].reg << 16;
9205   inst.instruction |= inst.operands[1].reg;
9206   inst.instruction |= inst.operands[2].reg << 8;
9207
9208   if (inst.operands[0].reg == inst.operands[1].reg
9209       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9210     as_tsktsk (_("Rd and Rm should be different in mul"));
9211 }
9212
9213 /* Long Multiply Parser
9214    UMULL RdLo, RdHi, Rm, Rs
9215    SMULL RdLo, RdHi, Rm, Rs
9216    UMLAL RdLo, RdHi, Rm, Rs
9217    SMLAL RdLo, RdHi, Rm, Rs.  */
9218
9219 static void
9220 do_mull (void)
9221 {
9222   inst.instruction |= inst.operands[0].reg << 12;
9223   inst.instruction |= inst.operands[1].reg << 16;
9224   inst.instruction |= inst.operands[2].reg;
9225   inst.instruction |= inst.operands[3].reg << 8;
9226
9227   /* rdhi and rdlo must be different.  */
9228   if (inst.operands[0].reg == inst.operands[1].reg)
9229     as_tsktsk (_("rdhi and rdlo must be different"));
9230
9231   /* rdhi, rdlo and rm must all be different before armv6.  */
9232   if ((inst.operands[0].reg == inst.operands[2].reg
9233       || inst.operands[1].reg == inst.operands[2].reg)
9234       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9235     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9236 }
9237
9238 static void
9239 do_nop (void)
9240 {
9241   if (inst.operands[0].present
9242       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9243     {
9244       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9245       inst.instruction &= 0xf0000000;
9246       inst.instruction |= 0x0320f000;
9247       if (inst.operands[0].present)
9248         inst.instruction |= inst.operands[0].imm;
9249     }
9250 }
9251
9252 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9253    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9254    Condition defaults to COND_ALWAYS.
9255    Error if Rd, Rn or Rm are R15.  */
9256
9257 static void
9258 do_pkhbt (void)
9259 {
9260   inst.instruction |= inst.operands[0].reg << 12;
9261   inst.instruction |= inst.operands[1].reg << 16;
9262   inst.instruction |= inst.operands[2].reg;
9263   if (inst.operands[3].present)
9264     encode_arm_shift (3);
9265 }
9266
9267 /* ARM V6 PKHTB (Argument Parse).  */
9268
9269 static void
9270 do_pkhtb (void)
9271 {
9272   if (!inst.operands[3].present)
9273     {
9274       /* If the shift specifier is omitted, turn the instruction
9275          into pkhbt rd, rm, rn. */
9276       inst.instruction &= 0xfff00010;
9277       inst.instruction |= inst.operands[0].reg << 12;
9278       inst.instruction |= inst.operands[1].reg;
9279       inst.instruction |= inst.operands[2].reg << 16;
9280     }
9281   else
9282     {
9283       inst.instruction |= inst.operands[0].reg << 12;
9284       inst.instruction |= inst.operands[1].reg << 16;
9285       inst.instruction |= inst.operands[2].reg;
9286       encode_arm_shift (3);
9287     }
9288 }
9289
9290 /* ARMv5TE: Preload-Cache
9291    MP Extensions: Preload for write
9292
9293     PLD(W) <addr_mode>
9294
9295   Syntactically, like LDR with B=1, W=0, L=1.  */
9296
9297 static void
9298 do_pld (void)
9299 {
9300   constraint (!inst.operands[0].isreg,
9301               _("'[' expected after PLD mnemonic"));
9302   constraint (inst.operands[0].postind,
9303               _("post-indexed expression used in preload instruction"));
9304   constraint (inst.operands[0].writeback,
9305               _("writeback used in preload instruction"));
9306   constraint (!inst.operands[0].preind,
9307               _("unindexed addressing used in preload instruction"));
9308   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9309 }
9310
9311 /* ARMv7: PLI <addr_mode>  */
9312 static void
9313 do_pli (void)
9314 {
9315   constraint (!inst.operands[0].isreg,
9316               _("'[' expected after PLI mnemonic"));
9317   constraint (inst.operands[0].postind,
9318               _("post-indexed expression used in preload instruction"));
9319   constraint (inst.operands[0].writeback,
9320               _("writeback used in preload instruction"));
9321   constraint (!inst.operands[0].preind,
9322               _("unindexed addressing used in preload instruction"));
9323   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9324   inst.instruction &= ~PRE_INDEX;
9325 }
9326
9327 static void
9328 do_push_pop (void)
9329 {
9330   constraint (inst.operands[0].writeback,
9331               _("push/pop do not support {reglist}^"));
9332   inst.operands[1] = inst.operands[0];
9333   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9334   inst.operands[0].isreg = 1;
9335   inst.operands[0].writeback = 1;
9336   inst.operands[0].reg = REG_SP;
9337   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9338 }
9339
9340 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9341    word at the specified address and the following word
9342    respectively.
9343    Unconditionally executed.
9344    Error if Rn is R15.  */
9345
9346 static void
9347 do_rfe (void)
9348 {
9349   inst.instruction |= inst.operands[0].reg << 16;
9350   if (inst.operands[0].writeback)
9351     inst.instruction |= WRITE_BACK;
9352 }
9353
9354 /* ARM V6 ssat (argument parse).  */
9355
9356 static void
9357 do_ssat (void)
9358 {
9359   inst.instruction |= inst.operands[0].reg << 12;
9360   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9361   inst.instruction |= inst.operands[2].reg;
9362
9363   if (inst.operands[3].present)
9364     encode_arm_shift (3);
9365 }
9366
9367 /* ARM V6 usat (argument parse).  */
9368
9369 static void
9370 do_usat (void)
9371 {
9372   inst.instruction |= inst.operands[0].reg << 12;
9373   inst.instruction |= inst.operands[1].imm << 16;
9374   inst.instruction |= inst.operands[2].reg;
9375
9376   if (inst.operands[3].present)
9377     encode_arm_shift (3);
9378 }
9379
9380 /* ARM V6 ssat16 (argument parse).  */
9381
9382 static void
9383 do_ssat16 (void)
9384 {
9385   inst.instruction |= inst.operands[0].reg << 12;
9386   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9387   inst.instruction |= inst.operands[2].reg;
9388 }
9389
9390 static void
9391 do_usat16 (void)
9392 {
9393   inst.instruction |= inst.operands[0].reg << 12;
9394   inst.instruction |= inst.operands[1].imm << 16;
9395   inst.instruction |= inst.operands[2].reg;
9396 }
9397
9398 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9399    preserving the other bits.
9400
9401    setend <endian_specifier>, where <endian_specifier> is either
9402    BE or LE.  */
9403
9404 static void
9405 do_setend (void)
9406 {
9407   if (warn_on_deprecated
9408       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9409       as_tsktsk (_("setend use is deprecated for ARMv8"));
9410
9411   if (inst.operands[0].imm)
9412     inst.instruction |= 0x200;
9413 }
9414
9415 static void
9416 do_shift (void)
9417 {
9418   unsigned int Rm = (inst.operands[1].present
9419                      ? inst.operands[1].reg
9420                      : inst.operands[0].reg);
9421
9422   inst.instruction |= inst.operands[0].reg << 12;
9423   inst.instruction |= Rm;
9424   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9425     {
9426       inst.instruction |= inst.operands[2].reg << 8;
9427       inst.instruction |= SHIFT_BY_REG;
9428       /* PR 12854: Error on extraneous shifts.  */
9429       constraint (inst.operands[2].shifted,
9430                   _("extraneous shift as part of operand to shift insn"));
9431     }
9432   else
9433     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9434 }
9435
9436 static void
9437 do_smc (void)
9438 {
9439   inst.reloc.type = BFD_RELOC_ARM_SMC;
9440   inst.reloc.pc_rel = 0;
9441 }
9442
9443 static void
9444 do_hvc (void)
9445 {
9446   inst.reloc.type = BFD_RELOC_ARM_HVC;
9447   inst.reloc.pc_rel = 0;
9448 }
9449
9450 static void
9451 do_swi (void)
9452 {
9453   inst.reloc.type = BFD_RELOC_ARM_SWI;
9454   inst.reloc.pc_rel = 0;
9455 }
9456
9457 static void
9458 do_setpan (void)
9459 {
9460   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9461               _("selected processor does not support SETPAN instruction"));
9462
9463   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9464 }
9465
9466 static void
9467 do_t_setpan (void)
9468 {
9469   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9470               _("selected processor does not support SETPAN instruction"));
9471
9472   inst.instruction |= (inst.operands[0].imm << 3);
9473 }
9474
9475 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9476    SMLAxy{cond} Rd,Rm,Rs,Rn
9477    SMLAWy{cond} Rd,Rm,Rs,Rn
9478    Error if any register is R15.  */
9479
9480 static void
9481 do_smla (void)
9482 {
9483   inst.instruction |= inst.operands[0].reg << 16;
9484   inst.instruction |= inst.operands[1].reg;
9485   inst.instruction |= inst.operands[2].reg << 8;
9486   inst.instruction |= inst.operands[3].reg << 12;
9487 }
9488
9489 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9490    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9491    Error if any register is R15.
9492    Warning if Rdlo == Rdhi.  */
9493
9494 static void
9495 do_smlal (void)
9496 {
9497   inst.instruction |= inst.operands[0].reg << 12;
9498   inst.instruction |= inst.operands[1].reg << 16;
9499   inst.instruction |= inst.operands[2].reg;
9500   inst.instruction |= inst.operands[3].reg << 8;
9501
9502   if (inst.operands[0].reg == inst.operands[1].reg)
9503     as_tsktsk (_("rdhi and rdlo must be different"));
9504 }
9505
9506 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9507    SMULxy{cond} Rd,Rm,Rs
9508    Error if any register is R15.  */
9509
9510 static void
9511 do_smul (void)
9512 {
9513   inst.instruction |= inst.operands[0].reg << 16;
9514   inst.instruction |= inst.operands[1].reg;
9515   inst.instruction |= inst.operands[2].reg << 8;
9516 }
9517
9518 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9519    the same for both ARM and Thumb-2.  */
9520
9521 static void
9522 do_srs (void)
9523 {
9524   int reg;
9525
9526   if (inst.operands[0].present)
9527     {
9528       reg = inst.operands[0].reg;
9529       constraint (reg != REG_SP, _("SRS base register must be r13"));
9530     }
9531   else
9532     reg = REG_SP;
9533
9534   inst.instruction |= reg << 16;
9535   inst.instruction |= inst.operands[1].imm;
9536   if (inst.operands[0].writeback || inst.operands[1].writeback)
9537     inst.instruction |= WRITE_BACK;
9538 }
9539
9540 /* ARM V6 strex (argument parse).  */
9541
9542 static void
9543 do_strex (void)
9544 {
9545   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9546               || inst.operands[2].postind || inst.operands[2].writeback
9547               || inst.operands[2].immisreg || inst.operands[2].shifted
9548               || inst.operands[2].negative
9549               /* See comment in do_ldrex().  */
9550               || (inst.operands[2].reg == REG_PC),
9551               BAD_ADDR_MODE);
9552
9553   constraint (inst.operands[0].reg == inst.operands[1].reg
9554               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9555
9556   constraint (inst.reloc.exp.X_op != O_constant
9557               || inst.reloc.exp.X_add_number != 0,
9558               _("offset must be zero in ARM encoding"));
9559
9560   inst.instruction |= inst.operands[0].reg << 12;
9561   inst.instruction |= inst.operands[1].reg;
9562   inst.instruction |= inst.operands[2].reg << 16;
9563   inst.reloc.type = BFD_RELOC_UNUSED;
9564 }
9565
9566 static void
9567 do_t_strexbh (void)
9568 {
9569   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9570               || inst.operands[2].postind || inst.operands[2].writeback
9571               || inst.operands[2].immisreg || inst.operands[2].shifted
9572               || inst.operands[2].negative,
9573               BAD_ADDR_MODE);
9574
9575   constraint (inst.operands[0].reg == inst.operands[1].reg
9576               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9577
9578   do_rm_rd_rn ();
9579 }
9580
9581 static void
9582 do_strexd (void)
9583 {
9584   constraint (inst.operands[1].reg % 2 != 0,
9585               _("even register required"));
9586   constraint (inst.operands[2].present
9587               && inst.operands[2].reg != inst.operands[1].reg + 1,
9588               _("can only store two consecutive registers"));
9589   /* If op 2 were present and equal to PC, this function wouldn't
9590      have been called in the first place.  */
9591   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9592
9593   constraint (inst.operands[0].reg == inst.operands[1].reg
9594               || inst.operands[0].reg == inst.operands[1].reg + 1
9595               || inst.operands[0].reg == inst.operands[3].reg,
9596               BAD_OVERLAP);
9597
9598   inst.instruction |= inst.operands[0].reg << 12;
9599   inst.instruction |= inst.operands[1].reg;
9600   inst.instruction |= inst.operands[3].reg << 16;
9601 }
9602
9603 /* ARM V8 STRL.  */
9604 static void
9605 do_stlex (void)
9606 {
9607   constraint (inst.operands[0].reg == inst.operands[1].reg
9608               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9609
9610   do_rd_rm_rn ();
9611 }
9612
9613 static void
9614 do_t_stlex (void)
9615 {
9616   constraint (inst.operands[0].reg == inst.operands[1].reg
9617               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9618
9619   do_rm_rd_rn ();
9620 }
9621
9622 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9623    extends it to 32-bits, and adds the result to a value in another
9624    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9625    before extracting the 16-bit value.
9626    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9627    Condition defaults to COND_ALWAYS.
9628    Error if any register uses R15.  */
9629
9630 static void
9631 do_sxtah (void)
9632 {
9633   inst.instruction |= inst.operands[0].reg << 12;
9634   inst.instruction |= inst.operands[1].reg << 16;
9635   inst.instruction |= inst.operands[2].reg;
9636   inst.instruction |= inst.operands[3].imm << 10;
9637 }
9638
9639 /* ARM V6 SXTH.
9640
9641    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9642    Condition defaults to COND_ALWAYS.
9643    Error if any register uses R15.  */
9644
9645 static void
9646 do_sxth (void)
9647 {
9648   inst.instruction |= inst.operands[0].reg << 12;
9649   inst.instruction |= inst.operands[1].reg;
9650   inst.instruction |= inst.operands[2].imm << 10;
9651 }
9652 \f
9653 /* VFP instructions.  In a logical order: SP variant first, monad
9654    before dyad, arithmetic then move then load/store.  */
9655
9656 static void
9657 do_vfp_sp_monadic (void)
9658 {
9659   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9660   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9661 }
9662
9663 static void
9664 do_vfp_sp_dyadic (void)
9665 {
9666   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9667   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9668   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9669 }
9670
9671 static void
9672 do_vfp_sp_compare_z (void)
9673 {
9674   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9675 }
9676
9677 static void
9678 do_vfp_dp_sp_cvt (void)
9679 {
9680   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9681   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9682 }
9683
9684 static void
9685 do_vfp_sp_dp_cvt (void)
9686 {
9687   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9688   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9689 }
9690
9691 static void
9692 do_vfp_reg_from_sp (void)
9693 {
9694   inst.instruction |= inst.operands[0].reg << 12;
9695   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9696 }
9697
9698 static void
9699 do_vfp_reg2_from_sp2 (void)
9700 {
9701   constraint (inst.operands[2].imm != 2,
9702               _("only two consecutive VFP SP registers allowed here"));
9703   inst.instruction |= inst.operands[0].reg << 12;
9704   inst.instruction |= inst.operands[1].reg << 16;
9705   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9706 }
9707
9708 static void
9709 do_vfp_sp_from_reg (void)
9710 {
9711   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9712   inst.instruction |= inst.operands[1].reg << 12;
9713 }
9714
9715 static void
9716 do_vfp_sp2_from_reg2 (void)
9717 {
9718   constraint (inst.operands[0].imm != 2,
9719               _("only two consecutive VFP SP registers allowed here"));
9720   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9721   inst.instruction |= inst.operands[1].reg << 12;
9722   inst.instruction |= inst.operands[2].reg << 16;
9723 }
9724
9725 static void
9726 do_vfp_sp_ldst (void)
9727 {
9728   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9729   encode_arm_cp_address (1, FALSE, TRUE, 0);
9730 }
9731
9732 static void
9733 do_vfp_dp_ldst (void)
9734 {
9735   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9736   encode_arm_cp_address (1, FALSE, TRUE, 0);
9737 }
9738
9739
9740 static void
9741 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9742 {
9743   if (inst.operands[0].writeback)
9744     inst.instruction |= WRITE_BACK;
9745   else
9746     constraint (ldstm_type != VFP_LDSTMIA,
9747                 _("this addressing mode requires base-register writeback"));
9748   inst.instruction |= inst.operands[0].reg << 16;
9749   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9750   inst.instruction |= inst.operands[1].imm;
9751 }
9752
9753 static void
9754 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9755 {
9756   int count;
9757
9758   if (inst.operands[0].writeback)
9759     inst.instruction |= WRITE_BACK;
9760   else
9761     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9762                 _("this addressing mode requires base-register writeback"));
9763
9764   inst.instruction |= inst.operands[0].reg << 16;
9765   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9766
9767   count = inst.operands[1].imm << 1;
9768   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9769     count += 1;
9770
9771   inst.instruction |= count;
9772 }
9773
9774 static void
9775 do_vfp_sp_ldstmia (void)
9776 {
9777   vfp_sp_ldstm (VFP_LDSTMIA);
9778 }
9779
9780 static void
9781 do_vfp_sp_ldstmdb (void)
9782 {
9783   vfp_sp_ldstm (VFP_LDSTMDB);
9784 }
9785
9786 static void
9787 do_vfp_dp_ldstmia (void)
9788 {
9789   vfp_dp_ldstm (VFP_LDSTMIA);
9790 }
9791
9792 static void
9793 do_vfp_dp_ldstmdb (void)
9794 {
9795   vfp_dp_ldstm (VFP_LDSTMDB);
9796 }
9797
9798 static void
9799 do_vfp_xp_ldstmia (void)
9800 {
9801   vfp_dp_ldstm (VFP_LDSTMIAX);
9802 }
9803
9804 static void
9805 do_vfp_xp_ldstmdb (void)
9806 {
9807   vfp_dp_ldstm (VFP_LDSTMDBX);
9808 }
9809
9810 static void
9811 do_vfp_dp_rd_rm (void)
9812 {
9813   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9814   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9815 }
9816
9817 static void
9818 do_vfp_dp_rn_rd (void)
9819 {
9820   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9821   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9822 }
9823
9824 static void
9825 do_vfp_dp_rd_rn (void)
9826 {
9827   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9828   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9829 }
9830
9831 static void
9832 do_vfp_dp_rd_rn_rm (void)
9833 {
9834   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9835   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9836   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9837 }
9838
9839 static void
9840 do_vfp_dp_rd (void)
9841 {
9842   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9843 }
9844
9845 static void
9846 do_vfp_dp_rm_rd_rn (void)
9847 {
9848   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9849   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9850   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9851 }
9852
9853 /* VFPv3 instructions.  */
9854 static void
9855 do_vfp_sp_const (void)
9856 {
9857   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9858   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9859   inst.instruction |= (inst.operands[1].imm & 0x0f);
9860 }
9861
9862 static void
9863 do_vfp_dp_const (void)
9864 {
9865   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9866   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9867   inst.instruction |= (inst.operands[1].imm & 0x0f);
9868 }
9869
9870 static void
9871 vfp_conv (int srcsize)
9872 {
9873   int immbits = srcsize - inst.operands[1].imm;
9874
9875   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9876     {
9877       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9878          i.e. immbits must be in range 0 - 16.  */
9879       inst.error = _("immediate value out of range, expected range [0, 16]");
9880       return;
9881     }
9882   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9883     {
9884       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9885          i.e. immbits must be in range 0 - 31.  */
9886       inst.error = _("immediate value out of range, expected range [1, 32]");
9887       return;
9888     }
9889
9890   inst.instruction |= (immbits & 1) << 5;
9891   inst.instruction |= (immbits >> 1);
9892 }
9893
9894 static void
9895 do_vfp_sp_conv_16 (void)
9896 {
9897   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9898   vfp_conv (16);
9899 }
9900
9901 static void
9902 do_vfp_dp_conv_16 (void)
9903 {
9904   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9905   vfp_conv (16);
9906 }
9907
9908 static void
9909 do_vfp_sp_conv_32 (void)
9910 {
9911   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9912   vfp_conv (32);
9913 }
9914
9915 static void
9916 do_vfp_dp_conv_32 (void)
9917 {
9918   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9919   vfp_conv (32);
9920 }
9921 \f
9922 /* FPA instructions.  Also in a logical order.  */
9923
9924 static void
9925 do_fpa_cmp (void)
9926 {
9927   inst.instruction |= inst.operands[0].reg << 16;
9928   inst.instruction |= inst.operands[1].reg;
9929 }
9930
9931 static void
9932 do_fpa_ldmstm (void)
9933 {
9934   inst.instruction |= inst.operands[0].reg << 12;
9935   switch (inst.operands[1].imm)
9936     {
9937     case 1: inst.instruction |= CP_T_X;          break;
9938     case 2: inst.instruction |= CP_T_Y;          break;
9939     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
9940     case 4:                                      break;
9941     default: abort ();
9942     }
9943
9944   if (inst.instruction & (PRE_INDEX | INDEX_UP))
9945     {
9946       /* The instruction specified "ea" or "fd", so we can only accept
9947          [Rn]{!}.  The instruction does not really support stacking or
9948          unstacking, so we have to emulate these by setting appropriate
9949          bits and offsets.  */
9950       constraint (inst.reloc.exp.X_op != O_constant
9951                   || inst.reloc.exp.X_add_number != 0,
9952                   _("this instruction does not support indexing"));
9953
9954       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
9955         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
9956
9957       if (!(inst.instruction & INDEX_UP))
9958         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
9959
9960       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
9961         {
9962           inst.operands[2].preind = 0;
9963           inst.operands[2].postind = 1;
9964         }
9965     }
9966
9967   encode_arm_cp_address (2, TRUE, TRUE, 0);
9968 }
9969 \f
9970 /* iWMMXt instructions: strictly in alphabetical order.  */
9971
9972 static void
9973 do_iwmmxt_tandorc (void)
9974 {
9975   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
9976 }
9977
9978 static void
9979 do_iwmmxt_textrc (void)
9980 {
9981   inst.instruction |= inst.operands[0].reg << 12;
9982   inst.instruction |= inst.operands[1].imm;
9983 }
9984
9985 static void
9986 do_iwmmxt_textrm (void)
9987 {
9988   inst.instruction |= inst.operands[0].reg << 12;
9989   inst.instruction |= inst.operands[1].reg << 16;
9990   inst.instruction |= inst.operands[2].imm;
9991 }
9992
9993 static void
9994 do_iwmmxt_tinsr (void)
9995 {
9996   inst.instruction |= inst.operands[0].reg << 16;
9997   inst.instruction |= inst.operands[1].reg << 12;
9998   inst.instruction |= inst.operands[2].imm;
9999 }
10000
10001 static void
10002 do_iwmmxt_tmia (void)
10003 {
10004   inst.instruction |= inst.operands[0].reg << 5;
10005   inst.instruction |= inst.operands[1].reg;
10006   inst.instruction |= inst.operands[2].reg << 12;
10007 }
10008
10009 static void
10010 do_iwmmxt_waligni (void)
10011 {
10012   inst.instruction |= inst.operands[0].reg << 12;
10013   inst.instruction |= inst.operands[1].reg << 16;
10014   inst.instruction |= inst.operands[2].reg;
10015   inst.instruction |= inst.operands[3].imm << 20;
10016 }
10017
10018 static void
10019 do_iwmmxt_wmerge (void)
10020 {
10021   inst.instruction |= inst.operands[0].reg << 12;
10022   inst.instruction |= inst.operands[1].reg << 16;
10023   inst.instruction |= inst.operands[2].reg;
10024   inst.instruction |= inst.operands[3].imm << 21;
10025 }
10026
10027 static void
10028 do_iwmmxt_wmov (void)
10029 {
10030   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
10031   inst.instruction |= inst.operands[0].reg << 12;
10032   inst.instruction |= inst.operands[1].reg << 16;
10033   inst.instruction |= inst.operands[1].reg;
10034 }
10035
10036 static void
10037 do_iwmmxt_wldstbh (void)
10038 {
10039   int reloc;
10040   inst.instruction |= inst.operands[0].reg << 12;
10041   if (thumb_mode)
10042     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
10043   else
10044     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
10045   encode_arm_cp_address (1, TRUE, FALSE, reloc);
10046 }
10047
10048 static void
10049 do_iwmmxt_wldstw (void)
10050 {
10051   /* RIWR_RIWC clears .isreg for a control register.  */
10052   if (!inst.operands[0].isreg)
10053     {
10054       constraint (inst.cond != COND_ALWAYS, BAD_COND);
10055       inst.instruction |= 0xf0000000;
10056     }
10057
10058   inst.instruction |= inst.operands[0].reg << 12;
10059   encode_arm_cp_address (1, TRUE, TRUE, 0);
10060 }
10061
10062 static void
10063 do_iwmmxt_wldstd (void)
10064 {
10065   inst.instruction |= inst.operands[0].reg << 12;
10066   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
10067       && inst.operands[1].immisreg)
10068     {
10069       inst.instruction &= ~0x1a000ff;
10070       inst.instruction |= (0xfU << 28);
10071       if (inst.operands[1].preind)
10072         inst.instruction |= PRE_INDEX;
10073       if (!inst.operands[1].negative)
10074         inst.instruction |= INDEX_UP;
10075       if (inst.operands[1].writeback)
10076         inst.instruction |= WRITE_BACK;
10077       inst.instruction |= inst.operands[1].reg << 16;
10078       inst.instruction |= inst.reloc.exp.X_add_number << 4;
10079       inst.instruction |= inst.operands[1].imm;
10080     }
10081   else
10082     encode_arm_cp_address (1, TRUE, FALSE, 0);
10083 }
10084
10085 static void
10086 do_iwmmxt_wshufh (void)
10087 {
10088   inst.instruction |= inst.operands[0].reg << 12;
10089   inst.instruction |= inst.operands[1].reg << 16;
10090   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
10091   inst.instruction |= (inst.operands[2].imm & 0x0f);
10092 }
10093
10094 static void
10095 do_iwmmxt_wzero (void)
10096 {
10097   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
10098   inst.instruction |= inst.operands[0].reg;
10099   inst.instruction |= inst.operands[0].reg << 12;
10100   inst.instruction |= inst.operands[0].reg << 16;
10101 }
10102
10103 static void
10104 do_iwmmxt_wrwrwr_or_imm5 (void)
10105 {
10106   if (inst.operands[2].isreg)
10107     do_rd_rn_rm ();
10108   else {
10109     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
10110                 _("immediate operand requires iWMMXt2"));
10111     do_rd_rn ();
10112     if (inst.operands[2].imm == 0)
10113       {
10114         switch ((inst.instruction >> 20) & 0xf)
10115           {
10116           case 4:
10117           case 5:
10118           case 6:
10119           case 7:
10120             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
10121             inst.operands[2].imm = 16;
10122             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
10123             break;
10124           case 8:
10125           case 9:
10126           case 10:
10127           case 11:
10128             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10129             inst.operands[2].imm = 32;
10130             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10131             break;
10132           case 12:
10133           case 13:
10134           case 14:
10135           case 15:
10136             {
10137               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10138               unsigned long wrn;
10139               wrn = (inst.instruction >> 16) & 0xf;
10140               inst.instruction &= 0xff0fff0f;
10141               inst.instruction |= wrn;
10142               /* Bail out here; the instruction is now assembled.  */
10143               return;
10144             }
10145           }
10146       }
10147     /* Map 32 -> 0, etc.  */
10148     inst.operands[2].imm &= 0x1f;
10149     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10150   }
10151 }
10152 \f
10153 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10154    operations first, then control, shift, and load/store.  */
10155
10156 /* Insns like "foo X,Y,Z".  */
10157
10158 static void
10159 do_mav_triple (void)
10160 {
10161   inst.instruction |= inst.operands[0].reg << 16;
10162   inst.instruction |= inst.operands[1].reg;
10163   inst.instruction |= inst.operands[2].reg << 12;
10164 }
10165
10166 /* Insns like "foo W,X,Y,Z".
10167     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10168
10169 static void
10170 do_mav_quad (void)
10171 {
10172   inst.instruction |= inst.operands[0].reg << 5;
10173   inst.instruction |= inst.operands[1].reg << 12;
10174   inst.instruction |= inst.operands[2].reg << 16;
10175   inst.instruction |= inst.operands[3].reg;
10176 }
10177
10178 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10179 static void
10180 do_mav_dspsc (void)
10181 {
10182   inst.instruction |= inst.operands[1].reg << 12;
10183 }
10184
10185 /* Maverick shift immediate instructions.
10186    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10187    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10188
10189 static void
10190 do_mav_shift (void)
10191 {
10192   int imm = inst.operands[2].imm;
10193
10194   inst.instruction |= inst.operands[0].reg << 12;
10195   inst.instruction |= inst.operands[1].reg << 16;
10196
10197   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10198      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10199      Bit 4 should be 0.  */
10200   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10201
10202   inst.instruction |= imm;
10203 }
10204 \f
10205 /* XScale instructions.  Also sorted arithmetic before move.  */
10206
10207 /* Xscale multiply-accumulate (argument parse)
10208      MIAcc   acc0,Rm,Rs
10209      MIAPHcc acc0,Rm,Rs
10210      MIAxycc acc0,Rm,Rs.  */
10211
10212 static void
10213 do_xsc_mia (void)
10214 {
10215   inst.instruction |= inst.operands[1].reg;
10216   inst.instruction |= inst.operands[2].reg << 12;
10217 }
10218
10219 /* Xscale move-accumulator-register (argument parse)
10220
10221      MARcc   acc0,RdLo,RdHi.  */
10222
10223 static void
10224 do_xsc_mar (void)
10225 {
10226   inst.instruction |= inst.operands[1].reg << 12;
10227   inst.instruction |= inst.operands[2].reg << 16;
10228 }
10229
10230 /* Xscale move-register-accumulator (argument parse)
10231
10232      MRAcc   RdLo,RdHi,acc0.  */
10233
10234 static void
10235 do_xsc_mra (void)
10236 {
10237   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10238   inst.instruction |= inst.operands[0].reg << 12;
10239   inst.instruction |= inst.operands[1].reg << 16;
10240 }
10241 \f
10242 /* Encoding functions relevant only to Thumb.  */
10243
10244 /* inst.operands[i] is a shifted-register operand; encode
10245    it into inst.instruction in the format used by Thumb32.  */
10246
10247 static void
10248 encode_thumb32_shifted_operand (int i)
10249 {
10250   unsigned int value = inst.reloc.exp.X_add_number;
10251   unsigned int shift = inst.operands[i].shift_kind;
10252
10253   constraint (inst.operands[i].immisreg,
10254               _("shift by register not allowed in thumb mode"));
10255   inst.instruction |= inst.operands[i].reg;
10256   if (shift == SHIFT_RRX)
10257     inst.instruction |= SHIFT_ROR << 4;
10258   else
10259     {
10260       constraint (inst.reloc.exp.X_op != O_constant,
10261                   _("expression too complex"));
10262
10263       constraint (value > 32
10264                   || (value == 32 && (shift == SHIFT_LSL
10265                                       || shift == SHIFT_ROR)),
10266                   _("shift expression is too large"));
10267
10268       if (value == 0)
10269         shift = SHIFT_LSL;
10270       else if (value == 32)
10271         value = 0;
10272
10273       inst.instruction |= shift << 4;
10274       inst.instruction |= (value & 0x1c) << 10;
10275       inst.instruction |= (value & 0x03) << 6;
10276     }
10277 }
10278
10279
10280 /* inst.operands[i] was set up by parse_address.  Encode it into a
10281    Thumb32 format load or store instruction.  Reject forms that cannot
10282    be used with such instructions.  If is_t is true, reject forms that
10283    cannot be used with a T instruction; if is_d is true, reject forms
10284    that cannot be used with a D instruction.  If it is a store insn,
10285    reject PC in Rn.  */
10286
10287 static void
10288 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10289 {
10290   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10291
10292   constraint (!inst.operands[i].isreg,
10293               _("Instruction does not support =N addresses"));
10294
10295   inst.instruction |= inst.operands[i].reg << 16;
10296   if (inst.operands[i].immisreg)
10297     {
10298       constraint (is_pc, BAD_PC_ADDRESSING);
10299       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10300       constraint (inst.operands[i].negative,
10301                   _("Thumb does not support negative register indexing"));
10302       constraint (inst.operands[i].postind,
10303                   _("Thumb does not support register post-indexing"));
10304       constraint (inst.operands[i].writeback,
10305                   _("Thumb does not support register indexing with writeback"));
10306       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10307                   _("Thumb supports only LSL in shifted register indexing"));
10308
10309       inst.instruction |= inst.operands[i].imm;
10310       if (inst.operands[i].shifted)
10311         {
10312           constraint (inst.reloc.exp.X_op != O_constant,
10313                       _("expression too complex"));
10314           constraint (inst.reloc.exp.X_add_number < 0
10315                       || inst.reloc.exp.X_add_number > 3,
10316                       _("shift out of range"));
10317           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10318         }
10319       inst.reloc.type = BFD_RELOC_UNUSED;
10320     }
10321   else if (inst.operands[i].preind)
10322     {
10323       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10324       constraint (is_t && inst.operands[i].writeback,
10325                   _("cannot use writeback with this instruction"));
10326       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10327                   BAD_PC_ADDRESSING);
10328
10329       if (is_d)
10330         {
10331           inst.instruction |= 0x01000000;
10332           if (inst.operands[i].writeback)
10333             inst.instruction |= 0x00200000;
10334         }
10335       else
10336         {
10337           inst.instruction |= 0x00000c00;
10338           if (inst.operands[i].writeback)
10339             inst.instruction |= 0x00000100;
10340         }
10341       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10342     }
10343   else if (inst.operands[i].postind)
10344     {
10345       gas_assert (inst.operands[i].writeback);
10346       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10347       constraint (is_t, _("cannot use post-indexing with this instruction"));
10348
10349       if (is_d)
10350         inst.instruction |= 0x00200000;
10351       else
10352         inst.instruction |= 0x00000900;
10353       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10354     }
10355   else /* unindexed - only for coprocessor */
10356     inst.error = _("instruction does not accept unindexed addressing");
10357 }
10358
10359 /* Table of Thumb instructions which exist in both 16- and 32-bit
10360    encodings (the latter only in post-V6T2 cores).  The index is the
10361    value used in the insns table below.  When there is more than one
10362    possible 16-bit encoding for the instruction, this table always
10363    holds variant (1).
10364    Also contains several pseudo-instructions used during relaxation.  */
10365 #define T16_32_TAB                              \
10366   X(_adc,   4140, eb400000),                    \
10367   X(_adcs,  4140, eb500000),                    \
10368   X(_add,   1c00, eb000000),                    \
10369   X(_adds,  1c00, eb100000),                    \
10370   X(_addi,  0000, f1000000),                    \
10371   X(_addis, 0000, f1100000),                    \
10372   X(_add_pc,000f, f20f0000),                    \
10373   X(_add_sp,000d, f10d0000),                    \
10374   X(_adr,   000f, f20f0000),                    \
10375   X(_and,   4000, ea000000),                    \
10376   X(_ands,  4000, ea100000),                    \
10377   X(_asr,   1000, fa40f000),                    \
10378   X(_asrs,  1000, fa50f000),                    \
10379   X(_b,     e000, f000b000),                    \
10380   X(_bcond, d000, f0008000),                    \
10381   X(_bic,   4380, ea200000),                    \
10382   X(_bics,  4380, ea300000),                    \
10383   X(_cmn,   42c0, eb100f00),                    \
10384   X(_cmp,   2800, ebb00f00),                    \
10385   X(_cpsie, b660, f3af8400),                    \
10386   X(_cpsid, b670, f3af8600),                    \
10387   X(_cpy,   4600, ea4f0000),                    \
10388   X(_dec_sp,80dd, f1ad0d00),                    \
10389   X(_eor,   4040, ea800000),                    \
10390   X(_eors,  4040, ea900000),                    \
10391   X(_inc_sp,00dd, f10d0d00),                    \
10392   X(_ldmia, c800, e8900000),                    \
10393   X(_ldr,   6800, f8500000),                    \
10394   X(_ldrb,  7800, f8100000),                    \
10395   X(_ldrh,  8800, f8300000),                    \
10396   X(_ldrsb, 5600, f9100000),                    \
10397   X(_ldrsh, 5e00, f9300000),                    \
10398   X(_ldr_pc,4800, f85f0000),                    \
10399   X(_ldr_pc2,4800, f85f0000),                   \
10400   X(_ldr_sp,9800, f85d0000),                    \
10401   X(_lsl,   0000, fa00f000),                    \
10402   X(_lsls,  0000, fa10f000),                    \
10403   X(_lsr,   0800, fa20f000),                    \
10404   X(_lsrs,  0800, fa30f000),                    \
10405   X(_mov,   2000, ea4f0000),                    \
10406   X(_movs,  2000, ea5f0000),                    \
10407   X(_mul,   4340, fb00f000),                     \
10408   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10409   X(_mvn,   43c0, ea6f0000),                    \
10410   X(_mvns,  43c0, ea7f0000),                    \
10411   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10412   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10413   X(_orr,   4300, ea400000),                    \
10414   X(_orrs,  4300, ea500000),                    \
10415   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10416   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10417   X(_rev,   ba00, fa90f080),                    \
10418   X(_rev16, ba40, fa90f090),                    \
10419   X(_revsh, bac0, fa90f0b0),                    \
10420   X(_ror,   41c0, fa60f000),                    \
10421   X(_rors,  41c0, fa70f000),                    \
10422   X(_sbc,   4180, eb600000),                    \
10423   X(_sbcs,  4180, eb700000),                    \
10424   X(_stmia, c000, e8800000),                    \
10425   X(_str,   6000, f8400000),                    \
10426   X(_strb,  7000, f8000000),                    \
10427   X(_strh,  8000, f8200000),                    \
10428   X(_str_sp,9000, f84d0000),                    \
10429   X(_sub,   1e00, eba00000),                    \
10430   X(_subs,  1e00, ebb00000),                    \
10431   X(_subi,  8000, f1a00000),                    \
10432   X(_subis, 8000, f1b00000),                    \
10433   X(_sxtb,  b240, fa4ff080),                    \
10434   X(_sxth,  b200, fa0ff080),                    \
10435   X(_tst,   4200, ea100f00),                    \
10436   X(_uxtb,  b2c0, fa5ff080),                    \
10437   X(_uxth,  b280, fa1ff080),                    \
10438   X(_nop,   bf00, f3af8000),                    \
10439   X(_yield, bf10, f3af8001),                    \
10440   X(_wfe,   bf20, f3af8002),                    \
10441   X(_wfi,   bf30, f3af8003),                    \
10442   X(_sev,   bf40, f3af8004),                    \
10443   X(_sevl,  bf50, f3af8005),                    \
10444   X(_udf,   de00, f7f0a000)
10445
10446 /* To catch errors in encoding functions, the codes are all offset by
10447    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10448    as 16-bit instructions.  */
10449 #define X(a,b,c) T_MNEM##a
10450 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10451 #undef X
10452
10453 #define X(a,b,c) 0x##b
10454 static const unsigned short thumb_op16[] = { T16_32_TAB };
10455 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10456 #undef X
10457
10458 #define X(a,b,c) 0x##c
10459 static const unsigned int thumb_op32[] = { T16_32_TAB };
10460 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10461 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10462 #undef X
10463 #undef T16_32_TAB
10464
10465 /* Thumb instruction encoders, in alphabetical order.  */
10466
10467 /* ADDW or SUBW.  */
10468
10469 static void
10470 do_t_add_sub_w (void)
10471 {
10472   int Rd, Rn;
10473
10474   Rd = inst.operands[0].reg;
10475   Rn = inst.operands[1].reg;
10476
10477   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10478      is the SP-{plus,minus}-immediate form of the instruction.  */
10479   if (Rn == REG_SP)
10480     constraint (Rd == REG_PC, BAD_PC);
10481   else
10482     reject_bad_reg (Rd);
10483
10484   inst.instruction |= (Rn << 16) | (Rd << 8);
10485   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10486 }
10487
10488 /* Parse an add or subtract instruction.  We get here with inst.instruction
10489    equaling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10490
10491 static void
10492 do_t_add_sub (void)
10493 {
10494   int Rd, Rs, Rn;
10495
10496   Rd = inst.operands[0].reg;
10497   Rs = (inst.operands[1].present
10498         ? inst.operands[1].reg    /* Rd, Rs, foo */
10499         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10500
10501   if (Rd == REG_PC)
10502     set_it_insn_type_last ();
10503
10504   if (unified_syntax)
10505     {
10506       bfd_boolean flags;
10507       bfd_boolean narrow;
10508       int opcode;
10509
10510       flags = (inst.instruction == T_MNEM_adds
10511                || inst.instruction == T_MNEM_subs);
10512       if (flags)
10513         narrow = !in_it_block ();
10514       else
10515         narrow = in_it_block ();
10516       if (!inst.operands[2].isreg)
10517         {
10518           int add;
10519
10520           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10521
10522           add = (inst.instruction == T_MNEM_add
10523                  || inst.instruction == T_MNEM_adds);
10524           opcode = 0;
10525           if (inst.size_req != 4)
10526             {
10527               /* Attempt to use a narrow opcode, with relaxation if
10528                  appropriate.  */
10529               if (Rd == REG_SP && Rs == REG_SP && !flags)
10530                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10531               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10532                 opcode = T_MNEM_add_sp;
10533               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10534                 opcode = T_MNEM_add_pc;
10535               else if (Rd <= 7 && Rs <= 7 && narrow)
10536                 {
10537                   if (flags)
10538                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10539                   else
10540                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10541                 }
10542               if (opcode)
10543                 {
10544                   inst.instruction = THUMB_OP16(opcode);
10545                   inst.instruction |= (Rd << 4) | Rs;
10546                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10547                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
10548                   {
10549                     if (inst.size_req == 2)
10550                       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10551                     else
10552                       inst.relax = opcode;
10553                   }
10554                 }
10555               else
10556                 constraint (inst.size_req == 2, BAD_HIREG);
10557             }
10558           if (inst.size_req == 4
10559               || (inst.size_req != 2 && !opcode))
10560             {
10561               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10562                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
10563                           THUMB1_RELOC_ONLY);
10564               if (Rd == REG_PC)
10565                 {
10566                   constraint (add, BAD_PC);
10567                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10568                              _("only SUBS PC, LR, #const allowed"));
10569                   constraint (inst.reloc.exp.X_op != O_constant,
10570                               _("expression too complex"));
10571                   constraint (inst.reloc.exp.X_add_number < 0
10572                               || inst.reloc.exp.X_add_number > 0xff,
10573                              _("immediate value out of range"));
10574                   inst.instruction = T2_SUBS_PC_LR
10575                                      | inst.reloc.exp.X_add_number;
10576                   inst.reloc.type = BFD_RELOC_UNUSED;
10577                   return;
10578                 }
10579               else if (Rs == REG_PC)
10580                 {
10581                   /* Always use addw/subw.  */
10582                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10583                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10584                 }
10585               else
10586                 {
10587                   inst.instruction = THUMB_OP32 (inst.instruction);
10588                   inst.instruction = (inst.instruction & 0xe1ffffff)
10589                                      | 0x10000000;
10590                   if (flags)
10591                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10592                   else
10593                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10594                 }
10595               inst.instruction |= Rd << 8;
10596               inst.instruction |= Rs << 16;
10597             }
10598         }
10599       else
10600         {
10601           unsigned int value = inst.reloc.exp.X_add_number;
10602           unsigned int shift = inst.operands[2].shift_kind;
10603
10604           Rn = inst.operands[2].reg;
10605           /* See if we can do this with a 16-bit instruction.  */
10606           if (!inst.operands[2].shifted && inst.size_req != 4)
10607             {
10608               if (Rd > 7 || Rs > 7 || Rn > 7)
10609                 narrow = FALSE;
10610
10611               if (narrow)
10612                 {
10613                   inst.instruction = ((inst.instruction == T_MNEM_adds
10614                                        || inst.instruction == T_MNEM_add)
10615                                       ? T_OPCODE_ADD_R3
10616                                       : T_OPCODE_SUB_R3);
10617                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10618                   return;
10619                 }
10620
10621               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10622                 {
10623                   /* Thumb-1 cores (except v6-M) require at least one high
10624                      register in a narrow non flag setting add.  */
10625                   if (Rd > 7 || Rn > 7
10626                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10627                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10628                     {
10629                       if (Rd == Rn)
10630                         {
10631                           Rn = Rs;
10632                           Rs = Rd;
10633                         }
10634                       inst.instruction = T_OPCODE_ADD_HI;
10635                       inst.instruction |= (Rd & 8) << 4;
10636                       inst.instruction |= (Rd & 7);
10637                       inst.instruction |= Rn << 3;
10638                       return;
10639                     }
10640                 }
10641             }
10642
10643           constraint (Rd == REG_PC, BAD_PC);
10644           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10645           constraint (Rs == REG_PC, BAD_PC);
10646           reject_bad_reg (Rn);
10647
10648           /* If we get here, it can't be done in 16 bits.  */
10649           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10650                       _("shift must be constant"));
10651           inst.instruction = THUMB_OP32 (inst.instruction);
10652           inst.instruction |= Rd << 8;
10653           inst.instruction |= Rs << 16;
10654           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10655                       _("shift value over 3 not allowed in thumb mode"));
10656           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10657                       _("only LSL shift allowed in thumb mode"));
10658           encode_thumb32_shifted_operand (2);
10659         }
10660     }
10661   else
10662     {
10663       constraint (inst.instruction == T_MNEM_adds
10664                   || inst.instruction == T_MNEM_subs,
10665                   BAD_THUMB32);
10666
10667       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10668         {
10669           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10670                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10671                       BAD_HIREG);
10672
10673           inst.instruction = (inst.instruction == T_MNEM_add
10674                               ? 0x0000 : 0x8000);
10675           inst.instruction |= (Rd << 4) | Rs;
10676           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10677           return;
10678         }
10679
10680       Rn = inst.operands[2].reg;
10681       constraint (inst.operands[2].shifted, _("unshifted register required"));
10682
10683       /* We now have Rd, Rs, and Rn set to registers.  */
10684       if (Rd > 7 || Rs > 7 || Rn > 7)
10685         {
10686           /* Can't do this for SUB.      */
10687           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10688           inst.instruction = T_OPCODE_ADD_HI;
10689           inst.instruction |= (Rd & 8) << 4;
10690           inst.instruction |= (Rd & 7);
10691           if (Rs == Rd)
10692             inst.instruction |= Rn << 3;
10693           else if (Rn == Rd)
10694             inst.instruction |= Rs << 3;
10695           else
10696             constraint (1, _("dest must overlap one source register"));
10697         }
10698       else
10699         {
10700           inst.instruction = (inst.instruction == T_MNEM_add
10701                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10702           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10703         }
10704     }
10705 }
10706
10707 static void
10708 do_t_adr (void)
10709 {
10710   unsigned Rd;
10711
10712   Rd = inst.operands[0].reg;
10713   reject_bad_reg (Rd);
10714
10715   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10716     {
10717       /* Defer to section relaxation.  */
10718       inst.relax = inst.instruction;
10719       inst.instruction = THUMB_OP16 (inst.instruction);
10720       inst.instruction |= Rd << 4;
10721     }
10722   else if (unified_syntax && inst.size_req != 2)
10723     {
10724       /* Generate a 32-bit opcode.  */
10725       inst.instruction = THUMB_OP32 (inst.instruction);
10726       inst.instruction |= Rd << 8;
10727       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10728       inst.reloc.pc_rel = 1;
10729     }
10730   else
10731     {
10732       /* Generate a 16-bit opcode.  */
10733       inst.instruction = THUMB_OP16 (inst.instruction);
10734       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10735       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10736       inst.reloc.pc_rel = 1;
10737
10738       inst.instruction |= Rd << 4;
10739     }
10740 }
10741
10742 /* Arithmetic instructions for which there is just one 16-bit
10743    instruction encoding, and it allows only two low registers.
10744    For maximal compatibility with ARM syntax, we allow three register
10745    operands even when Thumb-32 instructions are not available, as long
10746    as the first two are identical.  For instance, both "sbc r0,r1" and
10747    "sbc r0,r0,r1" are allowed.  */
10748 static void
10749 do_t_arit3 (void)
10750 {
10751   int Rd, Rs, Rn;
10752
10753   Rd = inst.operands[0].reg;
10754   Rs = (inst.operands[1].present
10755         ? inst.operands[1].reg    /* Rd, Rs, foo */
10756         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10757   Rn = inst.operands[2].reg;
10758
10759   reject_bad_reg (Rd);
10760   reject_bad_reg (Rs);
10761   if (inst.operands[2].isreg)
10762     reject_bad_reg (Rn);
10763
10764   if (unified_syntax)
10765     {
10766       if (!inst.operands[2].isreg)
10767         {
10768           /* For an immediate, we always generate a 32-bit opcode;
10769              section relaxation will shrink it later if possible.  */
10770           inst.instruction = THUMB_OP32 (inst.instruction);
10771           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10772           inst.instruction |= Rd << 8;
10773           inst.instruction |= Rs << 16;
10774           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10775         }
10776       else
10777         {
10778           bfd_boolean narrow;
10779
10780           /* See if we can do this with a 16-bit instruction.  */
10781           if (THUMB_SETS_FLAGS (inst.instruction))
10782             narrow = !in_it_block ();
10783           else
10784             narrow = in_it_block ();
10785
10786           if (Rd > 7 || Rn > 7 || Rs > 7)
10787             narrow = FALSE;
10788           if (inst.operands[2].shifted)
10789             narrow = FALSE;
10790           if (inst.size_req == 4)
10791             narrow = FALSE;
10792
10793           if (narrow
10794               && Rd == Rs)
10795             {
10796               inst.instruction = THUMB_OP16 (inst.instruction);
10797               inst.instruction |= Rd;
10798               inst.instruction |= Rn << 3;
10799               return;
10800             }
10801
10802           /* If we get here, it can't be done in 16 bits.  */
10803           constraint (inst.operands[2].shifted
10804                       && inst.operands[2].immisreg,
10805                       _("shift must be constant"));
10806           inst.instruction = THUMB_OP32 (inst.instruction);
10807           inst.instruction |= Rd << 8;
10808           inst.instruction |= Rs << 16;
10809           encode_thumb32_shifted_operand (2);
10810         }
10811     }
10812   else
10813     {
10814       /* On its face this is a lie - the instruction does set the
10815          flags.  However, the only supported mnemonic in this mode
10816          says it doesn't.  */
10817       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10818
10819       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10820                   _("unshifted register required"));
10821       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10822       constraint (Rd != Rs,
10823                   _("dest and source1 must be the same register"));
10824
10825       inst.instruction = THUMB_OP16 (inst.instruction);
10826       inst.instruction |= Rd;
10827       inst.instruction |= Rn << 3;
10828     }
10829 }
10830
10831 /* Similarly, but for instructions where the arithmetic operation is
10832    commutative, so we can allow either of them to be different from
10833    the destination operand in a 16-bit instruction.  For instance, all
10834    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10835    accepted.  */
10836 static void
10837 do_t_arit3c (void)
10838 {
10839   int Rd, Rs, Rn;
10840
10841   Rd = inst.operands[0].reg;
10842   Rs = (inst.operands[1].present
10843         ? inst.operands[1].reg    /* Rd, Rs, foo */
10844         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10845   Rn = inst.operands[2].reg;
10846
10847   reject_bad_reg (Rd);
10848   reject_bad_reg (Rs);
10849   if (inst.operands[2].isreg)
10850     reject_bad_reg (Rn);
10851
10852   if (unified_syntax)
10853     {
10854       if (!inst.operands[2].isreg)
10855         {
10856           /* For an immediate, we always generate a 32-bit opcode;
10857              section relaxation will shrink it later if possible.  */
10858           inst.instruction = THUMB_OP32 (inst.instruction);
10859           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10860           inst.instruction |= Rd << 8;
10861           inst.instruction |= Rs << 16;
10862           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10863         }
10864       else
10865         {
10866           bfd_boolean narrow;
10867
10868           /* See if we can do this with a 16-bit instruction.  */
10869           if (THUMB_SETS_FLAGS (inst.instruction))
10870             narrow = !in_it_block ();
10871           else
10872             narrow = in_it_block ();
10873
10874           if (Rd > 7 || Rn > 7 || Rs > 7)
10875             narrow = FALSE;
10876           if (inst.operands[2].shifted)
10877             narrow = FALSE;
10878           if (inst.size_req == 4)
10879             narrow = FALSE;
10880
10881           if (narrow)
10882             {
10883               if (Rd == Rs)
10884                 {
10885                   inst.instruction = THUMB_OP16 (inst.instruction);
10886                   inst.instruction |= Rd;
10887                   inst.instruction |= Rn << 3;
10888                   return;
10889                 }
10890               if (Rd == Rn)
10891                 {
10892                   inst.instruction = THUMB_OP16 (inst.instruction);
10893                   inst.instruction |= Rd;
10894                   inst.instruction |= Rs << 3;
10895                   return;
10896                 }
10897             }
10898
10899           /* If we get here, it can't be done in 16 bits.  */
10900           constraint (inst.operands[2].shifted
10901                       && inst.operands[2].immisreg,
10902                       _("shift must be constant"));
10903           inst.instruction = THUMB_OP32 (inst.instruction);
10904           inst.instruction |= Rd << 8;
10905           inst.instruction |= Rs << 16;
10906           encode_thumb32_shifted_operand (2);
10907         }
10908     }
10909   else
10910     {
10911       /* On its face this is a lie - the instruction does set the
10912          flags.  However, the only supported mnemonic in this mode
10913          says it doesn't.  */
10914       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10915
10916       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10917                   _("unshifted register required"));
10918       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10919
10920       inst.instruction = THUMB_OP16 (inst.instruction);
10921       inst.instruction |= Rd;
10922
10923       if (Rd == Rs)
10924         inst.instruction |= Rn << 3;
10925       else if (Rd == Rn)
10926         inst.instruction |= Rs << 3;
10927       else
10928         constraint (1, _("dest must overlap one source register"));
10929     }
10930 }
10931
10932 static void
10933 do_t_bfc (void)
10934 {
10935   unsigned Rd;
10936   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
10937   constraint (msb > 32, _("bit-field extends past end of register"));
10938   /* The instruction encoding stores the LSB and MSB,
10939      not the LSB and width.  */
10940   Rd = inst.operands[0].reg;
10941   reject_bad_reg (Rd);
10942   inst.instruction |= Rd << 8;
10943   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
10944   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
10945   inst.instruction |= msb - 1;
10946 }
10947
10948 static void
10949 do_t_bfi (void)
10950 {
10951   int Rd, Rn;
10952   unsigned int msb;
10953
10954   Rd = inst.operands[0].reg;
10955   reject_bad_reg (Rd);
10956
10957   /* #0 in second position is alternative syntax for bfc, which is
10958      the same instruction but with REG_PC in the Rm field.  */
10959   if (!inst.operands[1].isreg)
10960     Rn = REG_PC;
10961   else
10962     {
10963       Rn = inst.operands[1].reg;
10964       reject_bad_reg (Rn);
10965     }
10966
10967   msb = inst.operands[2].imm + inst.operands[3].imm;
10968   constraint (msb > 32, _("bit-field extends past end of register"));
10969   /* The instruction encoding stores the LSB and MSB,
10970      not the LSB and width.  */
10971   inst.instruction |= Rd << 8;
10972   inst.instruction |= Rn << 16;
10973   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10974   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10975   inst.instruction |= msb - 1;
10976 }
10977
10978 static void
10979 do_t_bfx (void)
10980 {
10981   unsigned Rd, Rn;
10982
10983   Rd = inst.operands[0].reg;
10984   Rn = inst.operands[1].reg;
10985
10986   reject_bad_reg (Rd);
10987   reject_bad_reg (Rn);
10988
10989   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
10990               _("bit-field extends past end of register"));
10991   inst.instruction |= Rd << 8;
10992   inst.instruction |= Rn << 16;
10993   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10994   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10995   inst.instruction |= inst.operands[3].imm - 1;
10996 }
10997
10998 /* ARM V5 Thumb BLX (argument parse)
10999         BLX <target_addr>       which is BLX(1)
11000         BLX <Rm>                which is BLX(2)
11001    Unfortunately, there are two different opcodes for this mnemonic.
11002    So, the insns[].value is not used, and the code here zaps values
11003         into inst.instruction.
11004
11005    ??? How to take advantage of the additional two bits of displacement
11006    available in Thumb32 mode?  Need new relocation?  */
11007
11008 static void
11009 do_t_blx (void)
11010 {
11011   set_it_insn_type_last ();
11012
11013   if (inst.operands[0].isreg)
11014     {
11015       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
11016       /* We have a register, so this is BLX(2).  */
11017       inst.instruction |= inst.operands[0].reg << 3;
11018     }
11019   else
11020     {
11021       /* No register.  This must be BLX(1).  */
11022       inst.instruction = 0xf000e800;
11023       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
11024     }
11025 }
11026
11027 static void
11028 do_t_branch (void)
11029 {
11030   int opcode;
11031   int cond;
11032   bfd_reloc_code_real_type reloc;
11033
11034   cond = inst.cond;
11035   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
11036
11037   if (in_it_block ())
11038     {
11039       /* Conditional branches inside IT blocks are encoded as unconditional
11040          branches.  */
11041       cond = COND_ALWAYS;
11042     }
11043   else
11044     cond = inst.cond;
11045
11046   if (cond != COND_ALWAYS)
11047     opcode = T_MNEM_bcond;
11048   else
11049     opcode = inst.instruction;
11050
11051   if (unified_syntax
11052       && (inst.size_req == 4
11053           || (inst.size_req != 2
11054               && (inst.operands[0].hasreloc
11055                   || inst.reloc.exp.X_op == O_constant))))
11056     {
11057       inst.instruction = THUMB_OP32(opcode);
11058       if (cond == COND_ALWAYS)
11059         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
11060       else
11061         {
11062           constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2),
11063                       _("selected architecture does not support "
11064                         "wide conditional branch instruction"));
11065
11066           gas_assert (cond != 0xF);
11067           inst.instruction |= cond << 22;
11068           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
11069         }
11070     }
11071   else
11072     {
11073       inst.instruction = THUMB_OP16(opcode);
11074       if (cond == COND_ALWAYS)
11075         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
11076       else
11077         {
11078           inst.instruction |= cond << 8;
11079           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
11080         }
11081       /* Allow section relaxation.  */
11082       if (unified_syntax && inst.size_req != 2)
11083         inst.relax = opcode;
11084     }
11085   inst.reloc.type = reloc;
11086   inst.reloc.pc_rel = 1;
11087 }
11088
11089 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
11090    between the two is the maximum immediate allowed - which is passed in
11091    RANGE.  */
11092 static void
11093 do_t_bkpt_hlt1 (int range)
11094 {
11095   constraint (inst.cond != COND_ALWAYS,
11096               _("instruction is always unconditional"));
11097   if (inst.operands[0].present)
11098     {
11099       constraint (inst.operands[0].imm > range,
11100                   _("immediate value out of range"));
11101       inst.instruction |= inst.operands[0].imm;
11102     }
11103
11104   set_it_insn_type (NEUTRAL_IT_INSN);
11105 }
11106
11107 static void
11108 do_t_hlt (void)
11109 {
11110   do_t_bkpt_hlt1 (63);
11111 }
11112
11113 static void
11114 do_t_bkpt (void)
11115 {
11116   do_t_bkpt_hlt1 (255);
11117 }
11118
11119 static void
11120 do_t_branch23 (void)
11121 {
11122   set_it_insn_type_last ();
11123   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
11124
11125   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
11126      this file.  We used to simply ignore the PLT reloc type here --
11127      the branch encoding is now needed to deal with TLSCALL relocs.
11128      So if we see a PLT reloc now, put it back to how it used to be to
11129      keep the preexisting behaviour.  */
11130   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
11131     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
11132
11133 #if defined(OBJ_COFF)
11134   /* If the destination of the branch is a defined symbol which does not have
11135      the THUMB_FUNC attribute, then we must be calling a function which has
11136      the (interfacearm) attribute.  We look for the Thumb entry point to that
11137      function and change the branch to refer to that function instead.  */
11138   if (   inst.reloc.exp.X_op == O_symbol
11139       && inst.reloc.exp.X_add_symbol != NULL
11140       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
11141       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
11142     inst.reloc.exp.X_add_symbol =
11143       find_real_start (inst.reloc.exp.X_add_symbol);
11144 #endif
11145 }
11146
11147 static void
11148 do_t_bx (void)
11149 {
11150   set_it_insn_type_last ();
11151   inst.instruction |= inst.operands[0].reg << 3;
11152   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11153      should cause the alignment to be checked once it is known.  This is
11154      because BX PC only works if the instruction is word aligned.  */
11155 }
11156
11157 static void
11158 do_t_bxj (void)
11159 {
11160   int Rm;
11161
11162   set_it_insn_type_last ();
11163   Rm = inst.operands[0].reg;
11164   reject_bad_reg (Rm);
11165   inst.instruction |= Rm << 16;
11166 }
11167
11168 static void
11169 do_t_clz (void)
11170 {
11171   unsigned Rd;
11172   unsigned Rm;
11173
11174   Rd = inst.operands[0].reg;
11175   Rm = inst.operands[1].reg;
11176
11177   reject_bad_reg (Rd);
11178   reject_bad_reg (Rm);
11179
11180   inst.instruction |= Rd << 8;
11181   inst.instruction |= Rm << 16;
11182   inst.instruction |= Rm;
11183 }
11184
11185 static void
11186 do_t_cps (void)
11187 {
11188   set_it_insn_type (OUTSIDE_IT_INSN);
11189   inst.instruction |= inst.operands[0].imm;
11190 }
11191
11192 static void
11193 do_t_cpsi (void)
11194 {
11195   set_it_insn_type (OUTSIDE_IT_INSN);
11196   if (unified_syntax
11197       && (inst.operands[1].present || inst.size_req == 4)
11198       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11199     {
11200       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11201       inst.instruction = 0xf3af8000;
11202       inst.instruction |= imod << 9;
11203       inst.instruction |= inst.operands[0].imm << 5;
11204       if (inst.operands[1].present)
11205         inst.instruction |= 0x100 | inst.operands[1].imm;
11206     }
11207   else
11208     {
11209       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11210                   && (inst.operands[0].imm & 4),
11211                   _("selected processor does not support 'A' form "
11212                     "of this instruction"));
11213       constraint (inst.operands[1].present || inst.size_req == 4,
11214                   _("Thumb does not support the 2-argument "
11215                     "form of this instruction"));
11216       inst.instruction |= inst.operands[0].imm;
11217     }
11218 }
11219
11220 /* THUMB CPY instruction (argument parse).  */
11221
11222 static void
11223 do_t_cpy (void)
11224 {
11225   if (inst.size_req == 4)
11226     {
11227       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11228       inst.instruction |= inst.operands[0].reg << 8;
11229       inst.instruction |= inst.operands[1].reg;
11230     }
11231   else
11232     {
11233       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11234       inst.instruction |= (inst.operands[0].reg & 0x7);
11235       inst.instruction |= inst.operands[1].reg << 3;
11236     }
11237 }
11238
11239 static void
11240 do_t_cbz (void)
11241 {
11242   set_it_insn_type (OUTSIDE_IT_INSN);
11243   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11244   inst.instruction |= inst.operands[0].reg;
11245   inst.reloc.pc_rel = 1;
11246   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11247 }
11248
11249 static void
11250 do_t_dbg (void)
11251 {
11252   inst.instruction |= inst.operands[0].imm;
11253 }
11254
11255 static void
11256 do_t_div (void)
11257 {
11258   unsigned Rd, Rn, Rm;
11259
11260   Rd = inst.operands[0].reg;
11261   Rn = (inst.operands[1].present
11262         ? inst.operands[1].reg : Rd);
11263   Rm = inst.operands[2].reg;
11264
11265   reject_bad_reg (Rd);
11266   reject_bad_reg (Rn);
11267   reject_bad_reg (Rm);
11268
11269   inst.instruction |= Rd << 8;
11270   inst.instruction |= Rn << 16;
11271   inst.instruction |= Rm;
11272 }
11273
11274 static void
11275 do_t_hint (void)
11276 {
11277   if (unified_syntax && inst.size_req == 4)
11278     inst.instruction = THUMB_OP32 (inst.instruction);
11279   else
11280     inst.instruction = THUMB_OP16 (inst.instruction);
11281 }
11282
11283 static void
11284 do_t_it (void)
11285 {
11286   unsigned int cond = inst.operands[0].imm;
11287
11288   set_it_insn_type (IT_INSN);
11289   now_it.mask = (inst.instruction & 0xf) | 0x10;
11290   now_it.cc = cond;
11291   now_it.warn_deprecated = FALSE;
11292
11293   /* If the condition is a negative condition, invert the mask.  */
11294   if ((cond & 0x1) == 0x0)
11295     {
11296       unsigned int mask = inst.instruction & 0x000f;
11297
11298       if ((mask & 0x7) == 0)
11299         {
11300           /* No conversion needed.  */
11301           now_it.block_length = 1;
11302         }
11303       else if ((mask & 0x3) == 0)
11304         {
11305           mask ^= 0x8;
11306           now_it.block_length = 2;
11307         }
11308       else if ((mask & 0x1) == 0)
11309         {
11310           mask ^= 0xC;
11311           now_it.block_length = 3;
11312         }
11313       else
11314         {
11315           mask ^= 0xE;
11316           now_it.block_length = 4;
11317         }
11318
11319       inst.instruction &= 0xfff0;
11320       inst.instruction |= mask;
11321     }
11322
11323   inst.instruction |= cond << 4;
11324 }
11325
11326 /* Helper function used for both push/pop and ldm/stm.  */
11327 static void
11328 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11329 {
11330   bfd_boolean load;
11331
11332   load = (inst.instruction & (1 << 20)) != 0;
11333
11334   if (mask & (1 << 13))
11335     inst.error =  _("SP not allowed in register list");
11336
11337   if ((mask & (1 << base)) != 0
11338       && writeback)
11339     inst.error = _("having the base register in the register list when "
11340                    "using write back is UNPREDICTABLE");
11341
11342   if (load)
11343     {
11344       if (mask & (1 << 15))
11345         {
11346           if (mask & (1 << 14))
11347             inst.error = _("LR and PC should not both be in register list");
11348           else
11349             set_it_insn_type_last ();
11350         }
11351     }
11352   else
11353     {
11354       if (mask & (1 << 15))
11355         inst.error = _("PC not allowed in register list");
11356     }
11357
11358   if ((mask & (mask - 1)) == 0)
11359     {
11360       /* Single register transfers implemented as str/ldr.  */
11361       if (writeback)
11362         {
11363           if (inst.instruction & (1 << 23))
11364             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11365           else
11366             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11367         }
11368       else
11369         {
11370           if (inst.instruction & (1 << 23))
11371             inst.instruction = 0x00800000; /* ia -> [base] */
11372           else
11373             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11374         }
11375
11376       inst.instruction |= 0xf8400000;
11377       if (load)
11378         inst.instruction |= 0x00100000;
11379
11380       mask = ffs (mask) - 1;
11381       mask <<= 12;
11382     }
11383   else if (writeback)
11384     inst.instruction |= WRITE_BACK;
11385
11386   inst.instruction |= mask;
11387   inst.instruction |= base << 16;
11388 }
11389
11390 static void
11391 do_t_ldmstm (void)
11392 {
11393   /* This really doesn't seem worth it.  */
11394   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11395               _("expression too complex"));
11396   constraint (inst.operands[1].writeback,
11397               _("Thumb load/store multiple does not support {reglist}^"));
11398
11399   if (unified_syntax)
11400     {
11401       bfd_boolean narrow;
11402       unsigned mask;
11403
11404       narrow = FALSE;
11405       /* See if we can use a 16-bit instruction.  */
11406       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11407           && inst.size_req != 4
11408           && !(inst.operands[1].imm & ~0xff))
11409         {
11410           mask = 1 << inst.operands[0].reg;
11411
11412           if (inst.operands[0].reg <= 7)
11413             {
11414               if (inst.instruction == T_MNEM_stmia
11415                   ? inst.operands[0].writeback
11416                   : (inst.operands[0].writeback
11417                      == !(inst.operands[1].imm & mask)))
11418                 {
11419                   if (inst.instruction == T_MNEM_stmia
11420                       && (inst.operands[1].imm & mask)
11421                       && (inst.operands[1].imm & (mask - 1)))
11422                     as_warn (_("value stored for r%d is UNKNOWN"),
11423                              inst.operands[0].reg);
11424
11425                   inst.instruction = THUMB_OP16 (inst.instruction);
11426                   inst.instruction |= inst.operands[0].reg << 8;
11427                   inst.instruction |= inst.operands[1].imm;
11428                   narrow = TRUE;
11429                 }
11430               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11431                 {
11432                   /* This means 1 register in reg list one of 3 situations:
11433                      1. Instruction is stmia, but without writeback.
11434                      2. lmdia without writeback, but with Rn not in
11435                         reglist.
11436                      3. ldmia with writeback, but with Rn in reglist.
11437                      Case 3 is UNPREDICTABLE behaviour, so we handle
11438                      case 1 and 2 which can be converted into a 16-bit
11439                      str or ldr. The SP cases are handled below.  */
11440                   unsigned long opcode;
11441                   /* First, record an error for Case 3.  */
11442                   if (inst.operands[1].imm & mask
11443                       && inst.operands[0].writeback)
11444                     inst.error =
11445                         _("having the base register in the register list when "
11446                           "using write back is UNPREDICTABLE");
11447
11448                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11449                                                              : T_MNEM_ldr);
11450                   inst.instruction = THUMB_OP16 (opcode);
11451                   inst.instruction |= inst.operands[0].reg << 3;
11452                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11453                   narrow = TRUE;
11454                 }
11455             }
11456           else if (inst.operands[0] .reg == REG_SP)
11457             {
11458               if (inst.operands[0].writeback)
11459                 {
11460                   inst.instruction =
11461                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11462                                     ? T_MNEM_push : T_MNEM_pop);
11463                   inst.instruction |= inst.operands[1].imm;
11464                   narrow = TRUE;
11465                 }
11466               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11467                 {
11468                   inst.instruction =
11469                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11470                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11471                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11472                   narrow = TRUE;
11473                 }
11474             }
11475         }
11476
11477       if (!narrow)
11478         {
11479           if (inst.instruction < 0xffff)
11480             inst.instruction = THUMB_OP32 (inst.instruction);
11481
11482           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11483                                 inst.operands[0].writeback);
11484         }
11485     }
11486   else
11487     {
11488       constraint (inst.operands[0].reg > 7
11489                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11490       constraint (inst.instruction != T_MNEM_ldmia
11491                   && inst.instruction != T_MNEM_stmia,
11492                   _("Thumb-2 instruction only valid in unified syntax"));
11493       if (inst.instruction == T_MNEM_stmia)
11494         {
11495           if (!inst.operands[0].writeback)
11496             as_warn (_("this instruction will write back the base register"));
11497           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11498               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11499             as_warn (_("value stored for r%d is UNKNOWN"),
11500                      inst.operands[0].reg);
11501         }
11502       else
11503         {
11504           if (!inst.operands[0].writeback
11505               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11506             as_warn (_("this instruction will write back the base register"));
11507           else if (inst.operands[0].writeback
11508                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11509             as_warn (_("this instruction will not write back the base register"));
11510         }
11511
11512       inst.instruction = THUMB_OP16 (inst.instruction);
11513       inst.instruction |= inst.operands[0].reg << 8;
11514       inst.instruction |= inst.operands[1].imm;
11515     }
11516 }
11517
11518 static void
11519 do_t_ldrex (void)
11520 {
11521   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11522               || inst.operands[1].postind || inst.operands[1].writeback
11523               || inst.operands[1].immisreg || inst.operands[1].shifted
11524               || inst.operands[1].negative,
11525               BAD_ADDR_MODE);
11526
11527   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11528
11529   inst.instruction |= inst.operands[0].reg << 12;
11530   inst.instruction |= inst.operands[1].reg << 16;
11531   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11532 }
11533
11534 static void
11535 do_t_ldrexd (void)
11536 {
11537   if (!inst.operands[1].present)
11538     {
11539       constraint (inst.operands[0].reg == REG_LR,
11540                   _("r14 not allowed as first register "
11541                     "when second register is omitted"));
11542       inst.operands[1].reg = inst.operands[0].reg + 1;
11543     }
11544   constraint (inst.operands[0].reg == inst.operands[1].reg,
11545               BAD_OVERLAP);
11546
11547   inst.instruction |= inst.operands[0].reg << 12;
11548   inst.instruction |= inst.operands[1].reg << 8;
11549   inst.instruction |= inst.operands[2].reg << 16;
11550 }
11551
11552 static void
11553 do_t_ldst (void)
11554 {
11555   unsigned long opcode;
11556   int Rn;
11557
11558   if (inst.operands[0].isreg
11559       && !inst.operands[0].preind
11560       && inst.operands[0].reg == REG_PC)
11561     set_it_insn_type_last ();
11562
11563   opcode = inst.instruction;
11564   if (unified_syntax)
11565     {
11566       if (!inst.operands[1].isreg)
11567         {
11568           if (opcode <= 0xffff)
11569             inst.instruction = THUMB_OP32 (opcode);
11570           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11571             return;
11572         }
11573       if (inst.operands[1].isreg
11574           && !inst.operands[1].writeback
11575           && !inst.operands[1].shifted && !inst.operands[1].postind
11576           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11577           && opcode <= 0xffff
11578           && inst.size_req != 4)
11579         {
11580           /* Insn may have a 16-bit form.  */
11581           Rn = inst.operands[1].reg;
11582           if (inst.operands[1].immisreg)
11583             {
11584               inst.instruction = THUMB_OP16 (opcode);
11585               /* [Rn, Rik] */
11586               if (Rn <= 7 && inst.operands[1].imm <= 7)
11587                 goto op16;
11588               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11589                 reject_bad_reg (inst.operands[1].imm);
11590             }
11591           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11592                     && opcode != T_MNEM_ldrsb)
11593                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11594                    || (Rn == REG_SP && opcode == T_MNEM_str))
11595             {
11596               /* [Rn, #const] */
11597               if (Rn > 7)
11598                 {
11599                   if (Rn == REG_PC)
11600                     {
11601                       if (inst.reloc.pc_rel)
11602                         opcode = T_MNEM_ldr_pc2;
11603                       else
11604                         opcode = T_MNEM_ldr_pc;
11605                     }
11606                   else
11607                     {
11608                       if (opcode == T_MNEM_ldr)
11609                         opcode = T_MNEM_ldr_sp;
11610                       else
11611                         opcode = T_MNEM_str_sp;
11612                     }
11613                   inst.instruction = inst.operands[0].reg << 8;
11614                 }
11615               else
11616                 {
11617                   inst.instruction = inst.operands[0].reg;
11618                   inst.instruction |= inst.operands[1].reg << 3;
11619                 }
11620               inst.instruction |= THUMB_OP16 (opcode);
11621               if (inst.size_req == 2)
11622                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11623               else
11624                 inst.relax = opcode;
11625               return;
11626             }
11627         }
11628       /* Definitely a 32-bit variant.  */
11629
11630       /* Warning for Erratum 752419.  */
11631       if (opcode == T_MNEM_ldr
11632           && inst.operands[0].reg == REG_SP
11633           && inst.operands[1].writeback == 1
11634           && !inst.operands[1].immisreg)
11635         {
11636           if (no_cpu_selected ()
11637               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11638                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11639                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11640             as_warn (_("This instruction may be unpredictable "
11641                        "if executed on M-profile cores "
11642                        "with interrupts enabled."));
11643         }
11644
11645       /* Do some validations regarding addressing modes.  */
11646       if (inst.operands[1].immisreg)
11647         reject_bad_reg (inst.operands[1].imm);
11648
11649       constraint (inst.operands[1].writeback == 1
11650                   && inst.operands[0].reg == inst.operands[1].reg,
11651                   BAD_OVERLAP);
11652
11653       inst.instruction = THUMB_OP32 (opcode);
11654       inst.instruction |= inst.operands[0].reg << 12;
11655       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11656       check_ldr_r15_aligned ();
11657       return;
11658     }
11659
11660   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11661
11662   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11663     {
11664       /* Only [Rn,Rm] is acceptable.  */
11665       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11666       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11667                   || inst.operands[1].postind || inst.operands[1].shifted
11668                   || inst.operands[1].negative,
11669                   _("Thumb does not support this addressing mode"));
11670       inst.instruction = THUMB_OP16 (inst.instruction);
11671       goto op16;
11672     }
11673
11674   inst.instruction = THUMB_OP16 (inst.instruction);
11675   if (!inst.operands[1].isreg)
11676     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11677       return;
11678
11679   constraint (!inst.operands[1].preind
11680               || inst.operands[1].shifted
11681               || inst.operands[1].writeback,
11682               _("Thumb does not support this addressing mode"));
11683   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11684     {
11685       constraint (inst.instruction & 0x0600,
11686                   _("byte or halfword not valid for base register"));
11687       constraint (inst.operands[1].reg == REG_PC
11688                   && !(inst.instruction & THUMB_LOAD_BIT),
11689                   _("r15 based store not allowed"));
11690       constraint (inst.operands[1].immisreg,
11691                   _("invalid base register for register offset"));
11692
11693       if (inst.operands[1].reg == REG_PC)
11694         inst.instruction = T_OPCODE_LDR_PC;
11695       else if (inst.instruction & THUMB_LOAD_BIT)
11696         inst.instruction = T_OPCODE_LDR_SP;
11697       else
11698         inst.instruction = T_OPCODE_STR_SP;
11699
11700       inst.instruction |= inst.operands[0].reg << 8;
11701       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11702       return;
11703     }
11704
11705   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11706   if (!inst.operands[1].immisreg)
11707     {
11708       /* Immediate offset.  */
11709       inst.instruction |= inst.operands[0].reg;
11710       inst.instruction |= inst.operands[1].reg << 3;
11711       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11712       return;
11713     }
11714
11715   /* Register offset.  */
11716   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11717   constraint (inst.operands[1].negative,
11718               _("Thumb does not support this addressing mode"));
11719
11720  op16:
11721   switch (inst.instruction)
11722     {
11723     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11724     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11725     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11726     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11727     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11728     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11729     case 0x5600 /* ldrsb */:
11730     case 0x5e00 /* ldrsh */: break;
11731     default: abort ();
11732     }
11733
11734   inst.instruction |= inst.operands[0].reg;
11735   inst.instruction |= inst.operands[1].reg << 3;
11736   inst.instruction |= inst.operands[1].imm << 6;
11737 }
11738
11739 static void
11740 do_t_ldstd (void)
11741 {
11742   if (!inst.operands[1].present)
11743     {
11744       inst.operands[1].reg = inst.operands[0].reg + 1;
11745       constraint (inst.operands[0].reg == REG_LR,
11746                   _("r14 not allowed here"));
11747       constraint (inst.operands[0].reg == REG_R12,
11748                   _("r12 not allowed here"));
11749     }
11750
11751   if (inst.operands[2].writeback
11752       && (inst.operands[0].reg == inst.operands[2].reg
11753       || inst.operands[1].reg == inst.operands[2].reg))
11754     as_warn (_("base register written back, and overlaps "
11755                "one of transfer registers"));
11756
11757   inst.instruction |= inst.operands[0].reg << 12;
11758   inst.instruction |= inst.operands[1].reg << 8;
11759   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11760 }
11761
11762 static void
11763 do_t_ldstt (void)
11764 {
11765   inst.instruction |= inst.operands[0].reg << 12;
11766   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11767 }
11768
11769 static void
11770 do_t_mla (void)
11771 {
11772   unsigned Rd, Rn, Rm, Ra;
11773
11774   Rd = inst.operands[0].reg;
11775   Rn = inst.operands[1].reg;
11776   Rm = inst.operands[2].reg;
11777   Ra = inst.operands[3].reg;
11778
11779   reject_bad_reg (Rd);
11780   reject_bad_reg (Rn);
11781   reject_bad_reg (Rm);
11782   reject_bad_reg (Ra);
11783
11784   inst.instruction |= Rd << 8;
11785   inst.instruction |= Rn << 16;
11786   inst.instruction |= Rm;
11787   inst.instruction |= Ra << 12;
11788 }
11789
11790 static void
11791 do_t_mlal (void)
11792 {
11793   unsigned RdLo, RdHi, Rn, Rm;
11794
11795   RdLo = inst.operands[0].reg;
11796   RdHi = inst.operands[1].reg;
11797   Rn = inst.operands[2].reg;
11798   Rm = inst.operands[3].reg;
11799
11800   reject_bad_reg (RdLo);
11801   reject_bad_reg (RdHi);
11802   reject_bad_reg (Rn);
11803   reject_bad_reg (Rm);
11804
11805   inst.instruction |= RdLo << 12;
11806   inst.instruction |= RdHi << 8;
11807   inst.instruction |= Rn << 16;
11808   inst.instruction |= Rm;
11809 }
11810
11811 static void
11812 do_t_mov_cmp (void)
11813 {
11814   unsigned Rn, Rm;
11815
11816   Rn = inst.operands[0].reg;
11817   Rm = inst.operands[1].reg;
11818
11819   if (Rn == REG_PC)
11820     set_it_insn_type_last ();
11821
11822   if (unified_syntax)
11823     {
11824       int r0off = (inst.instruction == T_MNEM_mov
11825                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11826       unsigned long opcode;
11827       bfd_boolean narrow;
11828       bfd_boolean low_regs;
11829
11830       low_regs = (Rn <= 7 && Rm <= 7);
11831       opcode = inst.instruction;
11832       if (in_it_block ())
11833         narrow = opcode != T_MNEM_movs;
11834       else
11835         narrow = opcode != T_MNEM_movs || low_regs;
11836       if (inst.size_req == 4
11837           || inst.operands[1].shifted)
11838         narrow = FALSE;
11839
11840       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11841       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11842           && !inst.operands[1].shifted
11843           && Rn == REG_PC
11844           && Rm == REG_LR)
11845         {
11846           inst.instruction = T2_SUBS_PC_LR;
11847           return;
11848         }
11849
11850       if (opcode == T_MNEM_cmp)
11851         {
11852           constraint (Rn == REG_PC, BAD_PC);
11853           if (narrow)
11854             {
11855               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11856                  but valid.  */
11857               warn_deprecated_sp (Rm);
11858               /* R15 was documented as a valid choice for Rm in ARMv6,
11859                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11860                  tools reject R15, so we do too.  */
11861               constraint (Rm == REG_PC, BAD_PC);
11862             }
11863           else
11864             reject_bad_reg (Rm);
11865         }
11866       else if (opcode == T_MNEM_mov
11867                || opcode == T_MNEM_movs)
11868         {
11869           if (inst.operands[1].isreg)
11870             {
11871               if (opcode == T_MNEM_movs)
11872                 {
11873                   reject_bad_reg (Rn);
11874                   reject_bad_reg (Rm);
11875                 }
11876               else if (narrow)
11877                 {
11878                   /* This is mov.n.  */
11879                   if ((Rn == REG_SP || Rn == REG_PC)
11880                       && (Rm == REG_SP || Rm == REG_PC))
11881                     {
11882                       as_tsktsk (_("Use of r%u as a source register is "
11883                                  "deprecated when r%u is the destination "
11884                                  "register."), Rm, Rn);
11885                     }
11886                 }
11887               else
11888                 {
11889                   /* This is mov.w.  */
11890                   constraint (Rn == REG_PC, BAD_PC);
11891                   constraint (Rm == REG_PC, BAD_PC);
11892                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11893                 }
11894             }
11895           else
11896             reject_bad_reg (Rn);
11897         }
11898
11899       if (!inst.operands[1].isreg)
11900         {
11901           /* Immediate operand.  */
11902           if (!in_it_block () && opcode == T_MNEM_mov)
11903             narrow = 0;
11904           if (low_regs && narrow)
11905             {
11906               inst.instruction = THUMB_OP16 (opcode);
11907               inst.instruction |= Rn << 8;
11908               if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11909                   || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
11910                 {
11911                   if (inst.size_req == 2)
11912                     inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11913                   else
11914                     inst.relax = opcode;
11915                 }
11916             }
11917           else
11918             {
11919               constraint (inst.reloc.type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11920                           && inst.reloc.type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC ,
11921                           THUMB1_RELOC_ONLY);
11922
11923               inst.instruction = THUMB_OP32 (inst.instruction);
11924               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11925               inst.instruction |= Rn << r0off;
11926               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11927             }
11928         }
11929       else if (inst.operands[1].shifted && inst.operands[1].immisreg
11930                && (inst.instruction == T_MNEM_mov
11931                    || inst.instruction == T_MNEM_movs))
11932         {
11933           /* Register shifts are encoded as separate shift instructions.  */
11934           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
11935
11936           if (in_it_block ())
11937             narrow = !flags;
11938           else
11939             narrow = flags;
11940
11941           if (inst.size_req == 4)
11942             narrow = FALSE;
11943
11944           if (!low_regs || inst.operands[1].imm > 7)
11945             narrow = FALSE;
11946
11947           if (Rn != Rm)
11948             narrow = FALSE;
11949
11950           switch (inst.operands[1].shift_kind)
11951             {
11952             case SHIFT_LSL:
11953               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
11954               break;
11955             case SHIFT_ASR:
11956               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
11957               break;
11958             case SHIFT_LSR:
11959               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
11960               break;
11961             case SHIFT_ROR:
11962               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
11963               break;
11964             default:
11965               abort ();
11966             }
11967
11968           inst.instruction = opcode;
11969           if (narrow)
11970             {
11971               inst.instruction |= Rn;
11972               inst.instruction |= inst.operands[1].imm << 3;
11973             }
11974           else
11975             {
11976               if (flags)
11977                 inst.instruction |= CONDS_BIT;
11978
11979               inst.instruction |= Rn << 8;
11980               inst.instruction |= Rm << 16;
11981               inst.instruction |= inst.operands[1].imm;
11982             }
11983         }
11984       else if (!narrow)
11985         {
11986           /* Some mov with immediate shift have narrow variants.
11987              Register shifts are handled above.  */
11988           if (low_regs && inst.operands[1].shifted
11989               && (inst.instruction == T_MNEM_mov
11990                   || inst.instruction == T_MNEM_movs))
11991             {
11992               if (in_it_block ())
11993                 narrow = (inst.instruction == T_MNEM_mov);
11994               else
11995                 narrow = (inst.instruction == T_MNEM_movs);
11996             }
11997
11998           if (narrow)
11999             {
12000               switch (inst.operands[1].shift_kind)
12001                 {
12002                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12003                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12004                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12005                 default: narrow = FALSE; break;
12006                 }
12007             }
12008
12009           if (narrow)
12010             {
12011               inst.instruction |= Rn;
12012               inst.instruction |= Rm << 3;
12013               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12014             }
12015           else
12016             {
12017               inst.instruction = THUMB_OP32 (inst.instruction);
12018               inst.instruction |= Rn << r0off;
12019               encode_thumb32_shifted_operand (1);
12020             }
12021         }
12022       else
12023         switch (inst.instruction)
12024           {
12025           case T_MNEM_mov:
12026             /* In v4t or v5t a move of two lowregs produces unpredictable
12027                results. Don't allow this.  */
12028             if (low_regs)
12029               {
12030                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
12031                             "MOV Rd, Rs with two low registers is not "
12032                             "permitted on this architecture");
12033                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
12034                                         arm_ext_v6);
12035               }
12036
12037             inst.instruction = T_OPCODE_MOV_HR;
12038             inst.instruction |= (Rn & 0x8) << 4;
12039             inst.instruction |= (Rn & 0x7);
12040             inst.instruction |= Rm << 3;
12041             break;
12042
12043           case T_MNEM_movs:
12044             /* We know we have low registers at this point.
12045                Generate LSLS Rd, Rs, #0.  */
12046             inst.instruction = T_OPCODE_LSL_I;
12047             inst.instruction |= Rn;
12048             inst.instruction |= Rm << 3;
12049             break;
12050
12051           case T_MNEM_cmp:
12052             if (low_regs)
12053               {
12054                 inst.instruction = T_OPCODE_CMP_LR;
12055                 inst.instruction |= Rn;
12056                 inst.instruction |= Rm << 3;
12057               }
12058             else
12059               {
12060                 inst.instruction = T_OPCODE_CMP_HR;
12061                 inst.instruction |= (Rn & 0x8) << 4;
12062                 inst.instruction |= (Rn & 0x7);
12063                 inst.instruction |= Rm << 3;
12064               }
12065             break;
12066           }
12067       return;
12068     }
12069
12070   inst.instruction = THUMB_OP16 (inst.instruction);
12071
12072   /* PR 10443: Do not silently ignore shifted operands.  */
12073   constraint (inst.operands[1].shifted,
12074               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
12075
12076   if (inst.operands[1].isreg)
12077     {
12078       if (Rn < 8 && Rm < 8)
12079         {
12080           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
12081              since a MOV instruction produces unpredictable results.  */
12082           if (inst.instruction == T_OPCODE_MOV_I8)
12083             inst.instruction = T_OPCODE_ADD_I3;
12084           else
12085             inst.instruction = T_OPCODE_CMP_LR;
12086
12087           inst.instruction |= Rn;
12088           inst.instruction |= Rm << 3;
12089         }
12090       else
12091         {
12092           if (inst.instruction == T_OPCODE_MOV_I8)
12093             inst.instruction = T_OPCODE_MOV_HR;
12094           else
12095             inst.instruction = T_OPCODE_CMP_HR;
12096           do_t_cpy ();
12097         }
12098     }
12099   else
12100     {
12101       constraint (Rn > 7,
12102                   _("only lo regs allowed with immediate"));
12103       inst.instruction |= Rn << 8;
12104       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
12105     }
12106 }
12107
12108 static void
12109 do_t_mov16 (void)
12110 {
12111   unsigned Rd;
12112   bfd_vma imm;
12113   bfd_boolean top;
12114
12115   top = (inst.instruction & 0x00800000) != 0;
12116   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
12117     {
12118       constraint (top, _(":lower16: not allowed in this instruction"));
12119       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
12120     }
12121   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
12122     {
12123       constraint (!top, _(":upper16: not allowed in this instruction"));
12124       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
12125     }
12126
12127   Rd = inst.operands[0].reg;
12128   reject_bad_reg (Rd);
12129
12130   inst.instruction |= Rd << 8;
12131   if (inst.reloc.type == BFD_RELOC_UNUSED)
12132     {
12133       imm = inst.reloc.exp.X_add_number;
12134       inst.instruction |= (imm & 0xf000) << 4;
12135       inst.instruction |= (imm & 0x0800) << 15;
12136       inst.instruction |= (imm & 0x0700) << 4;
12137       inst.instruction |= (imm & 0x00ff);
12138     }
12139 }
12140
12141 static void
12142 do_t_mvn_tst (void)
12143 {
12144   unsigned Rn, Rm;
12145
12146   Rn = inst.operands[0].reg;
12147   Rm = inst.operands[1].reg;
12148
12149   if (inst.instruction == T_MNEM_cmp
12150       || inst.instruction == T_MNEM_cmn)
12151     constraint (Rn == REG_PC, BAD_PC);
12152   else
12153     reject_bad_reg (Rn);
12154   reject_bad_reg (Rm);
12155
12156   if (unified_syntax)
12157     {
12158       int r0off = (inst.instruction == T_MNEM_mvn
12159                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12160       bfd_boolean narrow;
12161
12162       if (inst.size_req == 4
12163           || inst.instruction > 0xffff
12164           || inst.operands[1].shifted
12165           || Rn > 7 || Rm > 7)
12166         narrow = FALSE;
12167       else if (inst.instruction == T_MNEM_cmn
12168                || inst.instruction == T_MNEM_tst)
12169         narrow = TRUE;
12170       else if (THUMB_SETS_FLAGS (inst.instruction))
12171         narrow = !in_it_block ();
12172       else
12173         narrow = in_it_block ();
12174
12175       if (!inst.operands[1].isreg)
12176         {
12177           /* For an immediate, we always generate a 32-bit opcode;
12178              section relaxation will shrink it later if possible.  */
12179           if (inst.instruction < 0xffff)
12180             inst.instruction = THUMB_OP32 (inst.instruction);
12181           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12182           inst.instruction |= Rn << r0off;
12183           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12184         }
12185       else
12186         {
12187           /* See if we can do this with a 16-bit instruction.  */
12188           if (narrow)
12189             {
12190               inst.instruction = THUMB_OP16 (inst.instruction);
12191               inst.instruction |= Rn;
12192               inst.instruction |= Rm << 3;
12193             }
12194           else
12195             {
12196               constraint (inst.operands[1].shifted
12197                           && inst.operands[1].immisreg,
12198                           _("shift must be constant"));
12199               if (inst.instruction < 0xffff)
12200                 inst.instruction = THUMB_OP32 (inst.instruction);
12201               inst.instruction |= Rn << r0off;
12202               encode_thumb32_shifted_operand (1);
12203             }
12204         }
12205     }
12206   else
12207     {
12208       constraint (inst.instruction > 0xffff
12209                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12210       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12211                   _("unshifted register required"));
12212       constraint (Rn > 7 || Rm > 7,
12213                   BAD_HIREG);
12214
12215       inst.instruction = THUMB_OP16 (inst.instruction);
12216       inst.instruction |= Rn;
12217       inst.instruction |= Rm << 3;
12218     }
12219 }
12220
12221 static void
12222 do_t_mrs (void)
12223 {
12224   unsigned Rd;
12225
12226   if (do_vfp_nsyn_mrs () == SUCCESS)
12227     return;
12228
12229   Rd = inst.operands[0].reg;
12230   reject_bad_reg (Rd);
12231   inst.instruction |= Rd << 8;
12232
12233   if (inst.operands[1].isreg)
12234     {
12235       unsigned br = inst.operands[1].reg;
12236       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12237         as_bad (_("bad register for mrs"));
12238
12239       inst.instruction |= br & (0xf << 16);
12240       inst.instruction |= (br & 0x300) >> 4;
12241       inst.instruction |= (br & SPSR_BIT) >> 2;
12242     }
12243   else
12244     {
12245       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12246
12247       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12248         {
12249           /* PR gas/12698:  The constraint is only applied for m_profile.
12250              If the user has specified -march=all, we want to ignore it as
12251              we are building for any CPU type, including non-m variants.  */
12252           bfd_boolean m_profile =
12253             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12254           constraint ((flags != 0) && m_profile, _("selected processor does "
12255                                                    "not support requested special purpose register"));
12256         }
12257       else
12258         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12259            devices).  */
12260         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12261                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12262
12263       inst.instruction |= (flags & SPSR_BIT) >> 2;
12264       inst.instruction |= inst.operands[1].imm & 0xff;
12265       inst.instruction |= 0xf0000;
12266     }
12267 }
12268
12269 static void
12270 do_t_msr (void)
12271 {
12272   int flags;
12273   unsigned Rn;
12274
12275   if (do_vfp_nsyn_msr () == SUCCESS)
12276     return;
12277
12278   constraint (!inst.operands[1].isreg,
12279               _("Thumb encoding does not support an immediate here"));
12280
12281   if (inst.operands[0].isreg)
12282     flags = (int)(inst.operands[0].reg);
12283   else
12284     flags = inst.operands[0].imm;
12285
12286   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12287     {
12288       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12289
12290       /* PR gas/12698:  The constraint is only applied for m_profile.
12291          If the user has specified -march=all, we want to ignore it as
12292          we are building for any CPU type, including non-m variants.  */
12293       bfd_boolean m_profile =
12294         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12295       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12296            && (bits & ~(PSR_s | PSR_f)) != 0)
12297           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12298               && bits != PSR_f)) && m_profile,
12299           _("selected processor does not support requested special "
12300             "purpose register"));
12301     }
12302   else
12303      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12304                  "requested special purpose register"));
12305
12306   Rn = inst.operands[1].reg;
12307   reject_bad_reg (Rn);
12308
12309   inst.instruction |= (flags & SPSR_BIT) >> 2;
12310   inst.instruction |= (flags & 0xf0000) >> 8;
12311   inst.instruction |= (flags & 0x300) >> 4;
12312   inst.instruction |= (flags & 0xff);
12313   inst.instruction |= Rn << 16;
12314 }
12315
12316 static void
12317 do_t_mul (void)
12318 {
12319   bfd_boolean narrow;
12320   unsigned Rd, Rn, Rm;
12321
12322   if (!inst.operands[2].present)
12323     inst.operands[2].reg = inst.operands[0].reg;
12324
12325   Rd = inst.operands[0].reg;
12326   Rn = inst.operands[1].reg;
12327   Rm = inst.operands[2].reg;
12328
12329   if (unified_syntax)
12330     {
12331       if (inst.size_req == 4
12332           || (Rd != Rn
12333               && Rd != Rm)
12334           || Rn > 7
12335           || Rm > 7)
12336         narrow = FALSE;
12337       else if (inst.instruction == T_MNEM_muls)
12338         narrow = !in_it_block ();
12339       else
12340         narrow = in_it_block ();
12341     }
12342   else
12343     {
12344       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12345       constraint (Rn > 7 || Rm > 7,
12346                   BAD_HIREG);
12347       narrow = TRUE;
12348     }
12349
12350   if (narrow)
12351     {
12352       /* 16-bit MULS/Conditional MUL.  */
12353       inst.instruction = THUMB_OP16 (inst.instruction);
12354       inst.instruction |= Rd;
12355
12356       if (Rd == Rn)
12357         inst.instruction |= Rm << 3;
12358       else if (Rd == Rm)
12359         inst.instruction |= Rn << 3;
12360       else
12361         constraint (1, _("dest must overlap one source register"));
12362     }
12363   else
12364     {
12365       constraint (inst.instruction != T_MNEM_mul,
12366                   _("Thumb-2 MUL must not set flags"));
12367       /* 32-bit MUL.  */
12368       inst.instruction = THUMB_OP32 (inst.instruction);
12369       inst.instruction |= Rd << 8;
12370       inst.instruction |= Rn << 16;
12371       inst.instruction |= Rm << 0;
12372
12373       reject_bad_reg (Rd);
12374       reject_bad_reg (Rn);
12375       reject_bad_reg (Rm);
12376     }
12377 }
12378
12379 static void
12380 do_t_mull (void)
12381 {
12382   unsigned RdLo, RdHi, Rn, Rm;
12383
12384   RdLo = inst.operands[0].reg;
12385   RdHi = inst.operands[1].reg;
12386   Rn = inst.operands[2].reg;
12387   Rm = inst.operands[3].reg;
12388
12389   reject_bad_reg (RdLo);
12390   reject_bad_reg (RdHi);
12391   reject_bad_reg (Rn);
12392   reject_bad_reg (Rm);
12393
12394   inst.instruction |= RdLo << 12;
12395   inst.instruction |= RdHi << 8;
12396   inst.instruction |= Rn << 16;
12397   inst.instruction |= Rm;
12398
12399  if (RdLo == RdHi)
12400     as_tsktsk (_("rdhi and rdlo must be different"));
12401 }
12402
12403 static void
12404 do_t_nop (void)
12405 {
12406   set_it_insn_type (NEUTRAL_IT_INSN);
12407
12408   if (unified_syntax)
12409     {
12410       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12411         {
12412           inst.instruction = THUMB_OP32 (inst.instruction);
12413           inst.instruction |= inst.operands[0].imm;
12414         }
12415       else
12416         {
12417           /* PR9722: Check for Thumb2 availability before
12418              generating a thumb2 nop instruction.  */
12419           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12420             {
12421               inst.instruction = THUMB_OP16 (inst.instruction);
12422               inst.instruction |= inst.operands[0].imm << 4;
12423             }
12424           else
12425             inst.instruction = 0x46c0;
12426         }
12427     }
12428   else
12429     {
12430       constraint (inst.operands[0].present,
12431                   _("Thumb does not support NOP with hints"));
12432       inst.instruction = 0x46c0;
12433     }
12434 }
12435
12436 static void
12437 do_t_neg (void)
12438 {
12439   if (unified_syntax)
12440     {
12441       bfd_boolean narrow;
12442
12443       if (THUMB_SETS_FLAGS (inst.instruction))
12444         narrow = !in_it_block ();
12445       else
12446         narrow = in_it_block ();
12447       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12448         narrow = FALSE;
12449       if (inst.size_req == 4)
12450         narrow = FALSE;
12451
12452       if (!narrow)
12453         {
12454           inst.instruction = THUMB_OP32 (inst.instruction);
12455           inst.instruction |= inst.operands[0].reg << 8;
12456           inst.instruction |= inst.operands[1].reg << 16;
12457         }
12458       else
12459         {
12460           inst.instruction = THUMB_OP16 (inst.instruction);
12461           inst.instruction |= inst.operands[0].reg;
12462           inst.instruction |= inst.operands[1].reg << 3;
12463         }
12464     }
12465   else
12466     {
12467       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12468                   BAD_HIREG);
12469       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12470
12471       inst.instruction = THUMB_OP16 (inst.instruction);
12472       inst.instruction |= inst.operands[0].reg;
12473       inst.instruction |= inst.operands[1].reg << 3;
12474     }
12475 }
12476
12477 static void
12478 do_t_orn (void)
12479 {
12480   unsigned Rd, Rn;
12481
12482   Rd = inst.operands[0].reg;
12483   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12484
12485   reject_bad_reg (Rd);
12486   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12487   reject_bad_reg (Rn);
12488
12489   inst.instruction |= Rd << 8;
12490   inst.instruction |= Rn << 16;
12491
12492   if (!inst.operands[2].isreg)
12493     {
12494       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12495       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12496     }
12497   else
12498     {
12499       unsigned Rm;
12500
12501       Rm = inst.operands[2].reg;
12502       reject_bad_reg (Rm);
12503
12504       constraint (inst.operands[2].shifted
12505                   && inst.operands[2].immisreg,
12506                   _("shift must be constant"));
12507       encode_thumb32_shifted_operand (2);
12508     }
12509 }
12510
12511 static void
12512 do_t_pkhbt (void)
12513 {
12514   unsigned Rd, Rn, Rm;
12515
12516   Rd = inst.operands[0].reg;
12517   Rn = inst.operands[1].reg;
12518   Rm = inst.operands[2].reg;
12519
12520   reject_bad_reg (Rd);
12521   reject_bad_reg (Rn);
12522   reject_bad_reg (Rm);
12523
12524   inst.instruction |= Rd << 8;
12525   inst.instruction |= Rn << 16;
12526   inst.instruction |= Rm;
12527   if (inst.operands[3].present)
12528     {
12529       unsigned int val = inst.reloc.exp.X_add_number;
12530       constraint (inst.reloc.exp.X_op != O_constant,
12531                   _("expression too complex"));
12532       inst.instruction |= (val & 0x1c) << 10;
12533       inst.instruction |= (val & 0x03) << 6;
12534     }
12535 }
12536
12537 static void
12538 do_t_pkhtb (void)
12539 {
12540   if (!inst.operands[3].present)
12541     {
12542       unsigned Rtmp;
12543
12544       inst.instruction &= ~0x00000020;
12545
12546       /* PR 10168.  Swap the Rm and Rn registers.  */
12547       Rtmp = inst.operands[1].reg;
12548       inst.operands[1].reg = inst.operands[2].reg;
12549       inst.operands[2].reg = Rtmp;
12550     }
12551   do_t_pkhbt ();
12552 }
12553
12554 static void
12555 do_t_pld (void)
12556 {
12557   if (inst.operands[0].immisreg)
12558     reject_bad_reg (inst.operands[0].imm);
12559
12560   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12561 }
12562
12563 static void
12564 do_t_push_pop (void)
12565 {
12566   unsigned mask;
12567
12568   constraint (inst.operands[0].writeback,
12569               _("push/pop do not support {reglist}^"));
12570   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12571               _("expression too complex"));
12572
12573   mask = inst.operands[0].imm;
12574   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12575     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12576   else if (inst.size_req != 4
12577            && (mask & ~0xff) == (1U << (inst.instruction == T_MNEM_push
12578                                        ? REG_LR : REG_PC)))
12579     {
12580       inst.instruction = THUMB_OP16 (inst.instruction);
12581       inst.instruction |= THUMB_PP_PC_LR;
12582       inst.instruction |= mask & 0xff;
12583     }
12584   else if (unified_syntax)
12585     {
12586       inst.instruction = THUMB_OP32 (inst.instruction);
12587       encode_thumb2_ldmstm (13, mask, TRUE);
12588     }
12589   else
12590     {
12591       inst.error = _("invalid register list to push/pop instruction");
12592       return;
12593     }
12594 }
12595
12596 static void
12597 do_t_rbit (void)
12598 {
12599   unsigned Rd, Rm;
12600
12601   Rd = inst.operands[0].reg;
12602   Rm = inst.operands[1].reg;
12603
12604   reject_bad_reg (Rd);
12605   reject_bad_reg (Rm);
12606
12607   inst.instruction |= Rd << 8;
12608   inst.instruction |= Rm << 16;
12609   inst.instruction |= Rm;
12610 }
12611
12612 static void
12613 do_t_rev (void)
12614 {
12615   unsigned Rd, Rm;
12616
12617   Rd = inst.operands[0].reg;
12618   Rm = inst.operands[1].reg;
12619
12620   reject_bad_reg (Rd);
12621   reject_bad_reg (Rm);
12622
12623   if (Rd <= 7 && Rm <= 7
12624       && inst.size_req != 4)
12625     {
12626       inst.instruction = THUMB_OP16 (inst.instruction);
12627       inst.instruction |= Rd;
12628       inst.instruction |= Rm << 3;
12629     }
12630   else if (unified_syntax)
12631     {
12632       inst.instruction = THUMB_OP32 (inst.instruction);
12633       inst.instruction |= Rd << 8;
12634       inst.instruction |= Rm << 16;
12635       inst.instruction |= Rm;
12636     }
12637   else
12638     inst.error = BAD_HIREG;
12639 }
12640
12641 static void
12642 do_t_rrx (void)
12643 {
12644   unsigned Rd, Rm;
12645
12646   Rd = inst.operands[0].reg;
12647   Rm = inst.operands[1].reg;
12648
12649   reject_bad_reg (Rd);
12650   reject_bad_reg (Rm);
12651
12652   inst.instruction |= Rd << 8;
12653   inst.instruction |= Rm;
12654 }
12655
12656 static void
12657 do_t_rsb (void)
12658 {
12659   unsigned Rd, Rs;
12660
12661   Rd = inst.operands[0].reg;
12662   Rs = (inst.operands[1].present
12663         ? inst.operands[1].reg    /* Rd, Rs, foo */
12664         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12665
12666   reject_bad_reg (Rd);
12667   reject_bad_reg (Rs);
12668   if (inst.operands[2].isreg)
12669     reject_bad_reg (inst.operands[2].reg);
12670
12671   inst.instruction |= Rd << 8;
12672   inst.instruction |= Rs << 16;
12673   if (!inst.operands[2].isreg)
12674     {
12675       bfd_boolean narrow;
12676
12677       if ((inst.instruction & 0x00100000) != 0)
12678         narrow = !in_it_block ();
12679       else
12680         narrow = in_it_block ();
12681
12682       if (Rd > 7 || Rs > 7)
12683         narrow = FALSE;
12684
12685       if (inst.size_req == 4 || !unified_syntax)
12686         narrow = FALSE;
12687
12688       if (inst.reloc.exp.X_op != O_constant
12689           || inst.reloc.exp.X_add_number != 0)
12690         narrow = FALSE;
12691
12692       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12693          relaxation, but it doesn't seem worth the hassle.  */
12694       if (narrow)
12695         {
12696           inst.reloc.type = BFD_RELOC_UNUSED;
12697           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12698           inst.instruction |= Rs << 3;
12699           inst.instruction |= Rd;
12700         }
12701       else
12702         {
12703           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12704           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12705         }
12706     }
12707   else
12708     encode_thumb32_shifted_operand (2);
12709 }
12710
12711 static void
12712 do_t_setend (void)
12713 {
12714   if (warn_on_deprecated
12715       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12716       as_tsktsk (_("setend use is deprecated for ARMv8"));
12717
12718   set_it_insn_type (OUTSIDE_IT_INSN);
12719   if (inst.operands[0].imm)
12720     inst.instruction |= 0x8;
12721 }
12722
12723 static void
12724 do_t_shift (void)
12725 {
12726   if (!inst.operands[1].present)
12727     inst.operands[1].reg = inst.operands[0].reg;
12728
12729   if (unified_syntax)
12730     {
12731       bfd_boolean narrow;
12732       int shift_kind;
12733
12734       switch (inst.instruction)
12735         {
12736         case T_MNEM_asr:
12737         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12738         case T_MNEM_lsl:
12739         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12740         case T_MNEM_lsr:
12741         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12742         case T_MNEM_ror:
12743         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12744         default: abort ();
12745         }
12746
12747       if (THUMB_SETS_FLAGS (inst.instruction))
12748         narrow = !in_it_block ();
12749       else
12750         narrow = in_it_block ();
12751       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12752         narrow = FALSE;
12753       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12754         narrow = FALSE;
12755       if (inst.operands[2].isreg
12756           && (inst.operands[1].reg != inst.operands[0].reg
12757               || inst.operands[2].reg > 7))
12758         narrow = FALSE;
12759       if (inst.size_req == 4)
12760         narrow = FALSE;
12761
12762       reject_bad_reg (inst.operands[0].reg);
12763       reject_bad_reg (inst.operands[1].reg);
12764
12765       if (!narrow)
12766         {
12767           if (inst.operands[2].isreg)
12768             {
12769               reject_bad_reg (inst.operands[2].reg);
12770               inst.instruction = THUMB_OP32 (inst.instruction);
12771               inst.instruction |= inst.operands[0].reg << 8;
12772               inst.instruction |= inst.operands[1].reg << 16;
12773               inst.instruction |= inst.operands[2].reg;
12774
12775               /* PR 12854: Error on extraneous shifts.  */
12776               constraint (inst.operands[2].shifted,
12777                           _("extraneous shift as part of operand to shift insn"));
12778             }
12779           else
12780             {
12781               inst.operands[1].shifted = 1;
12782               inst.operands[1].shift_kind = shift_kind;
12783               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12784                                              ? T_MNEM_movs : T_MNEM_mov);
12785               inst.instruction |= inst.operands[0].reg << 8;
12786               encode_thumb32_shifted_operand (1);
12787               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12788               inst.reloc.type = BFD_RELOC_UNUSED;
12789             }
12790         }
12791       else
12792         {
12793           if (inst.operands[2].isreg)
12794             {
12795               switch (shift_kind)
12796                 {
12797                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12798                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12799                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12800                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12801                 default: abort ();
12802                 }
12803
12804               inst.instruction |= inst.operands[0].reg;
12805               inst.instruction |= inst.operands[2].reg << 3;
12806
12807               /* PR 12854: Error on extraneous shifts.  */
12808               constraint (inst.operands[2].shifted,
12809                           _("extraneous shift as part of operand to shift insn"));
12810             }
12811           else
12812             {
12813               switch (shift_kind)
12814                 {
12815                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12816                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12817                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12818                 default: abort ();
12819                 }
12820               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12821               inst.instruction |= inst.operands[0].reg;
12822               inst.instruction |= inst.operands[1].reg << 3;
12823             }
12824         }
12825     }
12826   else
12827     {
12828       constraint (inst.operands[0].reg > 7
12829                   || inst.operands[1].reg > 7, BAD_HIREG);
12830       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12831
12832       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12833         {
12834           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12835           constraint (inst.operands[0].reg != inst.operands[1].reg,
12836                       _("source1 and dest must be same register"));
12837
12838           switch (inst.instruction)
12839             {
12840             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12841             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12842             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12843             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12844             default: abort ();
12845             }
12846
12847           inst.instruction |= inst.operands[0].reg;
12848           inst.instruction |= inst.operands[2].reg << 3;
12849
12850           /* PR 12854: Error on extraneous shifts.  */
12851           constraint (inst.operands[2].shifted,
12852                       _("extraneous shift as part of operand to shift insn"));
12853         }
12854       else
12855         {
12856           switch (inst.instruction)
12857             {
12858             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12859             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12860             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12861             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12862             default: abort ();
12863             }
12864           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12865           inst.instruction |= inst.operands[0].reg;
12866           inst.instruction |= inst.operands[1].reg << 3;
12867         }
12868     }
12869 }
12870
12871 static void
12872 do_t_simd (void)
12873 {
12874   unsigned Rd, Rn, Rm;
12875
12876   Rd = inst.operands[0].reg;
12877   Rn = inst.operands[1].reg;
12878   Rm = inst.operands[2].reg;
12879
12880   reject_bad_reg (Rd);
12881   reject_bad_reg (Rn);
12882   reject_bad_reg (Rm);
12883
12884   inst.instruction |= Rd << 8;
12885   inst.instruction |= Rn << 16;
12886   inst.instruction |= Rm;
12887 }
12888
12889 static void
12890 do_t_simd2 (void)
12891 {
12892   unsigned Rd, Rn, Rm;
12893
12894   Rd = inst.operands[0].reg;
12895   Rm = inst.operands[1].reg;
12896   Rn = inst.operands[2].reg;
12897
12898   reject_bad_reg (Rd);
12899   reject_bad_reg (Rn);
12900   reject_bad_reg (Rm);
12901
12902   inst.instruction |= Rd << 8;
12903   inst.instruction |= Rn << 16;
12904   inst.instruction |= Rm;
12905 }
12906
12907 static void
12908 do_t_smc (void)
12909 {
12910   unsigned int value = inst.reloc.exp.X_add_number;
12911   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
12912               _("SMC is not permitted on this architecture"));
12913   constraint (inst.reloc.exp.X_op != O_constant,
12914               _("expression too complex"));
12915   inst.reloc.type = BFD_RELOC_UNUSED;
12916   inst.instruction |= (value & 0xf000) >> 12;
12917   inst.instruction |= (value & 0x0ff0);
12918   inst.instruction |= (value & 0x000f) << 16;
12919   /* PR gas/15623: SMC instructions must be last in an IT block.  */
12920   set_it_insn_type_last ();
12921 }
12922
12923 static void
12924 do_t_hvc (void)
12925 {
12926   unsigned int value = inst.reloc.exp.X_add_number;
12927
12928   inst.reloc.type = BFD_RELOC_UNUSED;
12929   inst.instruction |= (value & 0x0fff);
12930   inst.instruction |= (value & 0xf000) << 4;
12931 }
12932
12933 static void
12934 do_t_ssat_usat (int bias)
12935 {
12936   unsigned Rd, Rn;
12937
12938   Rd = inst.operands[0].reg;
12939   Rn = inst.operands[2].reg;
12940
12941   reject_bad_reg (Rd);
12942   reject_bad_reg (Rn);
12943
12944   inst.instruction |= Rd << 8;
12945   inst.instruction |= inst.operands[1].imm - bias;
12946   inst.instruction |= Rn << 16;
12947
12948   if (inst.operands[3].present)
12949     {
12950       offsetT shift_amount = inst.reloc.exp.X_add_number;
12951
12952       inst.reloc.type = BFD_RELOC_UNUSED;
12953
12954       constraint (inst.reloc.exp.X_op != O_constant,
12955                   _("expression too complex"));
12956
12957       if (shift_amount != 0)
12958         {
12959           constraint (shift_amount > 31,
12960                       _("shift expression is too large"));
12961
12962           if (inst.operands[3].shift_kind == SHIFT_ASR)
12963             inst.instruction |= 0x00200000;  /* sh bit.  */
12964
12965           inst.instruction |= (shift_amount & 0x1c) << 10;
12966           inst.instruction |= (shift_amount & 0x03) << 6;
12967         }
12968     }
12969 }
12970
12971 static void
12972 do_t_ssat (void)
12973 {
12974   do_t_ssat_usat (1);
12975 }
12976
12977 static void
12978 do_t_ssat16 (void)
12979 {
12980   unsigned Rd, Rn;
12981
12982   Rd = inst.operands[0].reg;
12983   Rn = inst.operands[2].reg;
12984
12985   reject_bad_reg (Rd);
12986   reject_bad_reg (Rn);
12987
12988   inst.instruction |= Rd << 8;
12989   inst.instruction |= inst.operands[1].imm - 1;
12990   inst.instruction |= Rn << 16;
12991 }
12992
12993 static void
12994 do_t_strex (void)
12995 {
12996   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
12997               || inst.operands[2].postind || inst.operands[2].writeback
12998               || inst.operands[2].immisreg || inst.operands[2].shifted
12999               || inst.operands[2].negative,
13000               BAD_ADDR_MODE);
13001
13002   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
13003
13004   inst.instruction |= inst.operands[0].reg << 8;
13005   inst.instruction |= inst.operands[1].reg << 12;
13006   inst.instruction |= inst.operands[2].reg << 16;
13007   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
13008 }
13009
13010 static void
13011 do_t_strexd (void)
13012 {
13013   if (!inst.operands[2].present)
13014     inst.operands[2].reg = inst.operands[1].reg + 1;
13015
13016   constraint (inst.operands[0].reg == inst.operands[1].reg
13017               || inst.operands[0].reg == inst.operands[2].reg
13018               || inst.operands[0].reg == inst.operands[3].reg,
13019               BAD_OVERLAP);
13020
13021   inst.instruction |= inst.operands[0].reg;
13022   inst.instruction |= inst.operands[1].reg << 12;
13023   inst.instruction |= inst.operands[2].reg << 8;
13024   inst.instruction |= inst.operands[3].reg << 16;
13025 }
13026
13027 static void
13028 do_t_sxtah (void)
13029 {
13030   unsigned Rd, Rn, Rm;
13031
13032   Rd = inst.operands[0].reg;
13033   Rn = inst.operands[1].reg;
13034   Rm = inst.operands[2].reg;
13035
13036   reject_bad_reg (Rd);
13037   reject_bad_reg (Rn);
13038   reject_bad_reg (Rm);
13039
13040   inst.instruction |= Rd << 8;
13041   inst.instruction |= Rn << 16;
13042   inst.instruction |= Rm;
13043   inst.instruction |= inst.operands[3].imm << 4;
13044 }
13045
13046 static void
13047 do_t_sxth (void)
13048 {
13049   unsigned Rd, Rm;
13050
13051   Rd = inst.operands[0].reg;
13052   Rm = inst.operands[1].reg;
13053
13054   reject_bad_reg (Rd);
13055   reject_bad_reg (Rm);
13056
13057   if (inst.instruction <= 0xffff
13058       && inst.size_req != 4
13059       && Rd <= 7 && Rm <= 7
13060       && (!inst.operands[2].present || inst.operands[2].imm == 0))
13061     {
13062       inst.instruction = THUMB_OP16 (inst.instruction);
13063       inst.instruction |= Rd;
13064       inst.instruction |= Rm << 3;
13065     }
13066   else if (unified_syntax)
13067     {
13068       if (inst.instruction <= 0xffff)
13069         inst.instruction = THUMB_OP32 (inst.instruction);
13070       inst.instruction |= Rd << 8;
13071       inst.instruction |= Rm;
13072       inst.instruction |= inst.operands[2].imm << 4;
13073     }
13074   else
13075     {
13076       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
13077                   _("Thumb encoding does not support rotation"));
13078       constraint (1, BAD_HIREG);
13079     }
13080 }
13081
13082 static void
13083 do_t_swi (void)
13084 {
13085   /* We have to do the following check manually as ARM_EXT_OS only applies
13086      to ARM_EXT_V6M.  */
13087   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
13088     {
13089       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
13090           /* This only applies to the v6m however, not later architectures.  */
13091           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
13092         as_bad (_("SVC is not permitted on this architecture"));
13093       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
13094     }
13095
13096   inst.reloc.type = BFD_RELOC_ARM_SWI;
13097 }
13098
13099 static void
13100 do_t_tb (void)
13101 {
13102   unsigned Rn, Rm;
13103   int half;
13104
13105   half = (inst.instruction & 0x10) != 0;
13106   set_it_insn_type_last ();
13107   constraint (inst.operands[0].immisreg,
13108               _("instruction requires register index"));
13109
13110   Rn = inst.operands[0].reg;
13111   Rm = inst.operands[0].imm;
13112
13113   constraint (Rn == REG_SP, BAD_SP);
13114   reject_bad_reg (Rm);
13115
13116   constraint (!half && inst.operands[0].shifted,
13117               _("instruction does not allow shifted index"));
13118   inst.instruction |= (Rn << 16) | Rm;
13119 }
13120
13121 static void
13122 do_t_udf (void)
13123 {
13124   if (!inst.operands[0].present)
13125     inst.operands[0].imm = 0;
13126
13127   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
13128     {
13129       constraint (inst.size_req == 2,
13130                   _("immediate value out of range"));
13131       inst.instruction = THUMB_OP32 (inst.instruction);
13132       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
13133       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
13134     }
13135   else
13136     {
13137       inst.instruction = THUMB_OP16 (inst.instruction);
13138       inst.instruction |= inst.operands[0].imm;
13139     }
13140
13141   set_it_insn_type (NEUTRAL_IT_INSN);
13142 }
13143
13144
13145 static void
13146 do_t_usat (void)
13147 {
13148   do_t_ssat_usat (0);
13149 }
13150
13151 static void
13152 do_t_usat16 (void)
13153 {
13154   unsigned Rd, Rn;
13155
13156   Rd = inst.operands[0].reg;
13157   Rn = inst.operands[2].reg;
13158
13159   reject_bad_reg (Rd);
13160   reject_bad_reg (Rn);
13161
13162   inst.instruction |= Rd << 8;
13163   inst.instruction |= inst.operands[1].imm;
13164   inst.instruction |= Rn << 16;
13165 }
13166
13167 /* Neon instruction encoder helpers.  */
13168
13169 /* Encodings for the different types for various Neon opcodes.  */
13170
13171 /* An "invalid" code for the following tables.  */
13172 #define N_INV -1u
13173
13174 struct neon_tab_entry
13175 {
13176   unsigned integer;
13177   unsigned float_or_poly;
13178   unsigned scalar_or_imm;
13179 };
13180
13181 /* Map overloaded Neon opcodes to their respective encodings.  */
13182 #define NEON_ENC_TAB                                    \
13183   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13184   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13185   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13186   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13187   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13188   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13189   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13190   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13191   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13192   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13193   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13194   /* Register variants of the following two instructions are encoded as
13195      vcge / vcgt with the operands reversed.  */        \
13196   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13197   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13198   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13199   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13200   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13201   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13202   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13203   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13204   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13205   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13206   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13207   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13208   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13209   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13210   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13211   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13212   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13213   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13214   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13215   X(vand,       0x0000110, N_INV,     0x0800030),       \
13216   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13217   X(veor,       0x1000110, N_INV,     N_INV),           \
13218   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13219   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13220   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13221   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13222   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13223   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13224   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13225   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13226   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13227   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13228   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13229   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13230   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13231   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13232   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13233   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13234   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13235   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13236   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13237   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13238   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13239   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13240   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13241   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13242   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13243   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13244   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13245   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13246   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13247   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13248   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13249   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13250   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13251   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13252   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13253   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13254   X(aes,        0x3b00300, N_INV,     N_INV),           \
13255   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13256   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13257   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13258
13259 enum neon_opc
13260 {
13261 #define X(OPC,I,F,S) N_MNEM_##OPC
13262 NEON_ENC_TAB
13263 #undef X
13264 };
13265
13266 static const struct neon_tab_entry neon_enc_tab[] =
13267 {
13268 #define X(OPC,I,F,S) { (I), (F), (S) }
13269 NEON_ENC_TAB
13270 #undef X
13271 };
13272
13273 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13274 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13275 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13276 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13277 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13278 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13279 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13280 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13281 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13282 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13283 #define NEON_ENC_SINGLE_(X) \
13284   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13285 #define NEON_ENC_DOUBLE_(X) \
13286   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13287 #define NEON_ENC_FPV8_(X) \
13288   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13289
13290 #define NEON_ENCODE(type, inst)                                 \
13291   do                                                            \
13292     {                                                           \
13293       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13294       inst.is_neon = 1;                                         \
13295     }                                                           \
13296   while (0)
13297
13298 #define check_neon_suffixes                                             \
13299   do                                                                    \
13300     {                                                                   \
13301       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13302         {                                                               \
13303           as_bad (_("invalid neon suffix for non neon instruction"));   \
13304           return;                                                       \
13305         }                                                               \
13306     }                                                                   \
13307   while (0)
13308
13309 /* Define shapes for instruction operands. The following mnemonic characters
13310    are used in this table:
13311
13312      F - VFP S<n> register
13313      D - Neon D<n> register
13314      Q - Neon Q<n> register
13315      I - Immediate
13316      S - Scalar
13317      R - ARM register
13318      L - D<n> register list
13319
13320    This table is used to generate various data:
13321      - enumerations of the form NS_DDR to be used as arguments to
13322        neon_select_shape.
13323      - a table classifying shapes into single, double, quad, mixed.
13324      - a table used to drive neon_select_shape.  */
13325
13326 #define NEON_SHAPE_DEF                  \
13327   X(3, (D, D, D), DOUBLE),              \
13328   X(3, (Q, Q, Q), QUAD),                \
13329   X(3, (D, D, I), DOUBLE),              \
13330   X(3, (Q, Q, I), QUAD),                \
13331   X(3, (D, D, S), DOUBLE),              \
13332   X(3, (Q, Q, S), QUAD),                \
13333   X(2, (D, D), DOUBLE),                 \
13334   X(2, (Q, Q), QUAD),                   \
13335   X(2, (D, S), DOUBLE),                 \
13336   X(2, (Q, S), QUAD),                   \
13337   X(2, (D, R), DOUBLE),                 \
13338   X(2, (Q, R), QUAD),                   \
13339   X(2, (D, I), DOUBLE),                 \
13340   X(2, (Q, I), QUAD),                   \
13341   X(3, (D, L, D), DOUBLE),              \
13342   X(2, (D, Q), MIXED),                  \
13343   X(2, (Q, D), MIXED),                  \
13344   X(3, (D, Q, I), MIXED),               \
13345   X(3, (Q, D, I), MIXED),               \
13346   X(3, (Q, D, D), MIXED),               \
13347   X(3, (D, Q, Q), MIXED),               \
13348   X(3, (Q, Q, D), MIXED),               \
13349   X(3, (Q, D, S), MIXED),               \
13350   X(3, (D, Q, S), MIXED),               \
13351   X(4, (D, D, D, I), DOUBLE),           \
13352   X(4, (Q, Q, Q, I), QUAD),             \
13353   X(4, (D, D, S, I), DOUBLE),           \
13354   X(4, (Q, Q, S, I), QUAD),             \
13355   X(2, (F, F), SINGLE),                 \
13356   X(3, (F, F, F), SINGLE),              \
13357   X(2, (F, I), SINGLE),                 \
13358   X(2, (F, D), MIXED),                  \
13359   X(2, (D, F), MIXED),                  \
13360   X(3, (F, F, I), MIXED),               \
13361   X(4, (R, R, F, F), SINGLE),           \
13362   X(4, (F, F, R, R), SINGLE),           \
13363   X(3, (D, R, R), DOUBLE),              \
13364   X(3, (R, R, D), DOUBLE),              \
13365   X(2, (S, R), SINGLE),                 \
13366   X(2, (R, S), SINGLE),                 \
13367   X(2, (F, R), SINGLE),                 \
13368   X(2, (R, F), SINGLE),                 \
13369 /* Half float shape supported so far.  */\
13370   X (2, (H, D), MIXED),                 \
13371   X (2, (D, H), MIXED),                 \
13372   X (2, (H, F), MIXED),                 \
13373   X (2, (F, H), MIXED),                 \
13374   X (2, (H, H), HALF),                  \
13375   X (2, (H, R), HALF),                  \
13376   X (2, (R, H), HALF),                  \
13377   X (2, (H, I), HALF),                  \
13378   X (3, (H, H, H), HALF),               \
13379   X (3, (H, F, I), MIXED),              \
13380   X (3, (F, H, I), MIXED)
13381
13382 #define S2(A,B)         NS_##A##B
13383 #define S3(A,B,C)       NS_##A##B##C
13384 #define S4(A,B,C,D)     NS_##A##B##C##D
13385
13386 #define X(N, L, C) S##N L
13387
13388 enum neon_shape
13389 {
13390   NEON_SHAPE_DEF,
13391   NS_NULL
13392 };
13393
13394 #undef X
13395 #undef S2
13396 #undef S3
13397 #undef S4
13398
13399 enum neon_shape_class
13400 {
13401   SC_HALF,
13402   SC_SINGLE,
13403   SC_DOUBLE,
13404   SC_QUAD,
13405   SC_MIXED
13406 };
13407
13408 #define X(N, L, C) SC_##C
13409
13410 static enum neon_shape_class neon_shape_class[] =
13411 {
13412   NEON_SHAPE_DEF
13413 };
13414
13415 #undef X
13416
13417 enum neon_shape_el
13418 {
13419   SE_H,
13420   SE_F,
13421   SE_D,
13422   SE_Q,
13423   SE_I,
13424   SE_S,
13425   SE_R,
13426   SE_L
13427 };
13428
13429 /* Register widths of above.  */
13430 static unsigned neon_shape_el_size[] =
13431 {
13432   16,
13433   32,
13434   64,
13435   128,
13436   0,
13437   32,
13438   32,
13439   0
13440 };
13441
13442 struct neon_shape_info
13443 {
13444   unsigned els;
13445   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13446 };
13447
13448 #define S2(A,B)         { SE_##A, SE_##B }
13449 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13450 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13451
13452 #define X(N, L, C) { N, S##N L }
13453
13454 static struct neon_shape_info neon_shape_tab[] =
13455 {
13456   NEON_SHAPE_DEF
13457 };
13458
13459 #undef X
13460 #undef S2
13461 #undef S3
13462 #undef S4
13463
13464 /* Bit masks used in type checking given instructions.
13465   'N_EQK' means the type must be the same as (or based on in some way) the key
13466    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13467    set, various other bits can be set as well in order to modify the meaning of
13468    the type constraint.  */
13469
13470 enum neon_type_mask
13471 {
13472   N_S8   = 0x0000001,
13473   N_S16  = 0x0000002,
13474   N_S32  = 0x0000004,
13475   N_S64  = 0x0000008,
13476   N_U8   = 0x0000010,
13477   N_U16  = 0x0000020,
13478   N_U32  = 0x0000040,
13479   N_U64  = 0x0000080,
13480   N_I8   = 0x0000100,
13481   N_I16  = 0x0000200,
13482   N_I32  = 0x0000400,
13483   N_I64  = 0x0000800,
13484   N_8    = 0x0001000,
13485   N_16   = 0x0002000,
13486   N_32   = 0x0004000,
13487   N_64   = 0x0008000,
13488   N_P8   = 0x0010000,
13489   N_P16  = 0x0020000,
13490   N_F16  = 0x0040000,
13491   N_F32  = 0x0080000,
13492   N_F64  = 0x0100000,
13493   N_P64  = 0x0200000,
13494   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13495   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13496   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13497   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13498   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13499   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13500   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13501   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13502   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13503   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13504   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13505   N_UTYP = 0,
13506   N_MAX_NONSPECIAL = N_P64
13507 };
13508
13509 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13510
13511 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13512 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13513 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13514 #define N_S_32     (N_S8 | N_S16 | N_S32)
13515 #define N_F_16_32  (N_F16 | N_F32)
13516 #define N_SUF_32   (N_SU_32 | N_F_16_32)
13517 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13518 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F16 | N_F32)
13519 #define N_F_ALL    (N_F16 | N_F32 | N_F64)
13520
13521 /* Pass this as the first type argument to neon_check_type to ignore types
13522    altogether.  */
13523 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13524
13525 /* Select a "shape" for the current instruction (describing register types or
13526    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13527    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13528    function of operand parsing, so this function doesn't need to be called.
13529    Shapes should be listed in order of decreasing length.  */
13530
13531 static enum neon_shape
13532 neon_select_shape (enum neon_shape shape, ...)
13533 {
13534   va_list ap;
13535   enum neon_shape first_shape = shape;
13536
13537   /* Fix missing optional operands. FIXME: we don't know at this point how
13538      many arguments we should have, so this makes the assumption that we have
13539      > 1. This is true of all current Neon opcodes, I think, but may not be
13540      true in the future.  */
13541   if (!inst.operands[1].present)
13542     inst.operands[1] = inst.operands[0];
13543
13544   va_start (ap, shape);
13545
13546   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13547     {
13548       unsigned j;
13549       int matches = 1;
13550
13551       for (j = 0; j < neon_shape_tab[shape].els; j++)
13552         {
13553           if (!inst.operands[j].present)
13554             {
13555               matches = 0;
13556               break;
13557             }
13558
13559           switch (neon_shape_tab[shape].el[j])
13560             {
13561               /* If a  .f16,  .16,  .u16,  .s16 type specifier is given over
13562                  a VFP single precision register operand, it's essentially
13563                  means only half of the register is used.
13564
13565                  If the type specifier is given after the mnemonics, the
13566                  information is stored in inst.vectype.  If the type specifier
13567                  is given after register operand, the information is stored
13568                  in inst.operands[].vectype.
13569
13570                  When there is only one type specifier, and all the register
13571                  operands are the same type of hardware register, the type
13572                  specifier applies to all register operands.
13573
13574                  If no type specifier is given, the shape is inferred from
13575                  operand information.
13576
13577                  for example:
13578                  vadd.f16 s0, s1, s2:           NS_HHH
13579                  vabs.f16 s0, s1:               NS_HH
13580                  vmov.f16 s0, r1:               NS_HR
13581                  vmov.f16 r0, s1:               NS_RH
13582                  vcvt.f16 r0, s1:               NS_RH
13583                  vcvt.f16.s32   s2, s2, #29:    NS_HFI
13584                  vcvt.f16.s32   s2, s2:         NS_HF
13585               */
13586             case SE_H:
13587               if (!(inst.operands[j].isreg
13588                     && inst.operands[j].isvec
13589                     && inst.operands[j].issingle
13590                     && !inst.operands[j].isquad
13591                     && ((inst.vectype.elems == 1
13592                          && inst.vectype.el[0].size == 16)
13593                         || (inst.vectype.elems > 1
13594                             && inst.vectype.el[j].size == 16)
13595                         || (inst.vectype.elems == 0
13596                             && inst.operands[j].vectype.type != NT_invtype
13597                             && inst.operands[j].vectype.size == 16))))
13598                 matches = 0;
13599               break;
13600
13601             case SE_F:
13602               if (!(inst.operands[j].isreg
13603                     && inst.operands[j].isvec
13604                     && inst.operands[j].issingle
13605                     && !inst.operands[j].isquad
13606                     && ((inst.vectype.elems == 1 && inst.vectype.el[0].size == 32)
13607                         || (inst.vectype.elems > 1 && inst.vectype.el[j].size == 32)
13608                         || (inst.vectype.elems == 0
13609                             && (inst.operands[j].vectype.size == 32
13610                                 || inst.operands[j].vectype.type == NT_invtype)))))
13611                 matches = 0;
13612               break;
13613
13614             case SE_D:
13615               if (!(inst.operands[j].isreg
13616                     && inst.operands[j].isvec
13617                     && !inst.operands[j].isquad
13618                     && !inst.operands[j].issingle))
13619                 matches = 0;
13620               break;
13621
13622             case SE_R:
13623               if (!(inst.operands[j].isreg
13624                     && !inst.operands[j].isvec))
13625                 matches = 0;
13626               break;
13627
13628             case SE_Q:
13629               if (!(inst.operands[j].isreg
13630                     && inst.operands[j].isvec
13631                     && inst.operands[j].isquad
13632                     && !inst.operands[j].issingle))
13633                 matches = 0;
13634               break;
13635
13636             case SE_I:
13637               if (!(!inst.operands[j].isreg
13638                     && !inst.operands[j].isscalar))
13639                 matches = 0;
13640               break;
13641
13642             case SE_S:
13643               if (!(!inst.operands[j].isreg
13644                     && inst.operands[j].isscalar))
13645                 matches = 0;
13646               break;
13647
13648             case SE_L:
13649               break;
13650             }
13651           if (!matches)
13652             break;
13653         }
13654       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13655         /* We've matched all the entries in the shape table, and we don't
13656            have any left over operands which have not been matched.  */
13657         break;
13658     }
13659
13660   va_end (ap);
13661
13662   if (shape == NS_NULL && first_shape != NS_NULL)
13663     first_error (_("invalid instruction shape"));
13664
13665   return shape;
13666 }
13667
13668 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13669    means the Q bit should be set).  */
13670
13671 static int
13672 neon_quad (enum neon_shape shape)
13673 {
13674   return neon_shape_class[shape] == SC_QUAD;
13675 }
13676
13677 static void
13678 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13679                        unsigned *g_size)
13680 {
13681   /* Allow modification to be made to types which are constrained to be
13682      based on the key element, based on bits set alongside N_EQK.  */
13683   if ((typebits & N_EQK) != 0)
13684     {
13685       if ((typebits & N_HLF) != 0)
13686         *g_size /= 2;
13687       else if ((typebits & N_DBL) != 0)
13688         *g_size *= 2;
13689       if ((typebits & N_SGN) != 0)
13690         *g_type = NT_signed;
13691       else if ((typebits & N_UNS) != 0)
13692         *g_type = NT_unsigned;
13693       else if ((typebits & N_INT) != 0)
13694         *g_type = NT_integer;
13695       else if ((typebits & N_FLT) != 0)
13696         *g_type = NT_float;
13697       else if ((typebits & N_SIZ) != 0)
13698         *g_type = NT_untyped;
13699     }
13700 }
13701
13702 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13703    operand type, i.e. the single type specified in a Neon instruction when it
13704    is the only one given.  */
13705
13706 static struct neon_type_el
13707 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13708 {
13709   struct neon_type_el dest = *key;
13710
13711   gas_assert ((thisarg & N_EQK) != 0);
13712
13713   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13714
13715   return dest;
13716 }
13717
13718 /* Convert Neon type and size into compact bitmask representation.  */
13719
13720 static enum neon_type_mask
13721 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13722 {
13723   switch (type)
13724     {
13725     case NT_untyped:
13726       switch (size)
13727         {
13728         case 8:  return N_8;
13729         case 16: return N_16;
13730         case 32: return N_32;
13731         case 64: return N_64;
13732         default: ;
13733         }
13734       break;
13735
13736     case NT_integer:
13737       switch (size)
13738         {
13739         case 8:  return N_I8;
13740         case 16: return N_I16;
13741         case 32: return N_I32;
13742         case 64: return N_I64;
13743         default: ;
13744         }
13745       break;
13746
13747     case NT_float:
13748       switch (size)
13749         {
13750         case 16: return N_F16;
13751         case 32: return N_F32;
13752         case 64: return N_F64;
13753         default: ;
13754         }
13755       break;
13756
13757     case NT_poly:
13758       switch (size)
13759         {
13760         case 8:  return N_P8;
13761         case 16: return N_P16;
13762         case 64: return N_P64;
13763         default: ;
13764         }
13765       break;
13766
13767     case NT_signed:
13768       switch (size)
13769         {
13770         case 8:  return N_S8;
13771         case 16: return N_S16;
13772         case 32: return N_S32;
13773         case 64: return N_S64;
13774         default: ;
13775         }
13776       break;
13777
13778     case NT_unsigned:
13779       switch (size)
13780         {
13781         case 8:  return N_U8;
13782         case 16: return N_U16;
13783         case 32: return N_U32;
13784         case 64: return N_U64;
13785         default: ;
13786         }
13787       break;
13788
13789     default: ;
13790     }
13791
13792   return N_UTYP;
13793 }
13794
13795 /* Convert compact Neon bitmask type representation to a type and size. Only
13796    handles the case where a single bit is set in the mask.  */
13797
13798 static int
13799 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13800                      enum neon_type_mask mask)
13801 {
13802   if ((mask & N_EQK) != 0)
13803     return FAIL;
13804
13805   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13806     *size = 8;
13807   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13808     *size = 16;
13809   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13810     *size = 32;
13811   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13812     *size = 64;
13813   else
13814     return FAIL;
13815
13816   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13817     *type = NT_signed;
13818   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13819     *type = NT_unsigned;
13820   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13821     *type = NT_integer;
13822   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13823     *type = NT_untyped;
13824   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13825     *type = NT_poly;
13826   else if ((mask & (N_F_ALL)) != 0)
13827     *type = NT_float;
13828   else
13829     return FAIL;
13830
13831   return SUCCESS;
13832 }
13833
13834 /* Modify a bitmask of allowed types. This is only needed for type
13835    relaxation.  */
13836
13837 static unsigned
13838 modify_types_allowed (unsigned allowed, unsigned mods)
13839 {
13840   unsigned size;
13841   enum neon_el_type type;
13842   unsigned destmask;
13843   int i;
13844
13845   destmask = 0;
13846
13847   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13848     {
13849       if (el_type_of_type_chk (&type, &size,
13850                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13851         {
13852           neon_modify_type_size (mods, &type, &size);
13853           destmask |= type_chk_of_el_type (type, size);
13854         }
13855     }
13856
13857   return destmask;
13858 }
13859
13860 /* Check type and return type classification.
13861    The manual states (paraphrase): If one datatype is given, it indicates the
13862    type given in:
13863     - the second operand, if there is one
13864     - the operand, if there is no second operand
13865     - the result, if there are no operands.
13866    This isn't quite good enough though, so we use a concept of a "key" datatype
13867    which is set on a per-instruction basis, which is the one which matters when
13868    only one data type is written.
13869    Note: this function has side-effects (e.g. filling in missing operands). All
13870    Neon instructions should call it before performing bit encoding.  */
13871
13872 static struct neon_type_el
13873 neon_check_type (unsigned els, enum neon_shape ns, ...)
13874 {
13875   va_list ap;
13876   unsigned i, pass, key_el = 0;
13877   unsigned types[NEON_MAX_TYPE_ELS];
13878   enum neon_el_type k_type = NT_invtype;
13879   unsigned k_size = -1u;
13880   struct neon_type_el badtype = {NT_invtype, -1};
13881   unsigned key_allowed = 0;
13882
13883   /* Optional registers in Neon instructions are always (not) in operand 1.
13884      Fill in the missing operand here, if it was omitted.  */
13885   if (els > 1 && !inst.operands[1].present)
13886     inst.operands[1] = inst.operands[0];
13887
13888   /* Suck up all the varargs.  */
13889   va_start (ap, ns);
13890   for (i = 0; i < els; i++)
13891     {
13892       unsigned thisarg = va_arg (ap, unsigned);
13893       if (thisarg == N_IGNORE_TYPE)
13894         {
13895           va_end (ap);
13896           return badtype;
13897         }
13898       types[i] = thisarg;
13899       if ((thisarg & N_KEY) != 0)
13900         key_el = i;
13901     }
13902   va_end (ap);
13903
13904   if (inst.vectype.elems > 0)
13905     for (i = 0; i < els; i++)
13906       if (inst.operands[i].vectype.type != NT_invtype)
13907         {
13908           first_error (_("types specified in both the mnemonic and operands"));
13909           return badtype;
13910         }
13911
13912   /* Duplicate inst.vectype elements here as necessary.
13913      FIXME: No idea if this is exactly the same as the ARM assembler,
13914      particularly when an insn takes one register and one non-register
13915      operand. */
13916   if (inst.vectype.elems == 1 && els > 1)
13917     {
13918       unsigned j;
13919       inst.vectype.elems = els;
13920       inst.vectype.el[key_el] = inst.vectype.el[0];
13921       for (j = 0; j < els; j++)
13922         if (j != key_el)
13923           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13924                                                   types[j]);
13925     }
13926   else if (inst.vectype.elems == 0 && els > 0)
13927     {
13928       unsigned j;
13929       /* No types were given after the mnemonic, so look for types specified
13930          after each operand. We allow some flexibility here; as long as the
13931          "key" operand has a type, we can infer the others.  */
13932       for (j = 0; j < els; j++)
13933         if (inst.operands[j].vectype.type != NT_invtype)
13934           inst.vectype.el[j] = inst.operands[j].vectype;
13935
13936       if (inst.operands[key_el].vectype.type != NT_invtype)
13937         {
13938           for (j = 0; j < els; j++)
13939             if (inst.operands[j].vectype.type == NT_invtype)
13940               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13941                                                       types[j]);
13942         }
13943       else
13944         {
13945           first_error (_("operand types can't be inferred"));
13946           return badtype;
13947         }
13948     }
13949   else if (inst.vectype.elems != els)
13950     {
13951       first_error (_("type specifier has the wrong number of parts"));
13952       return badtype;
13953     }
13954
13955   for (pass = 0; pass < 2; pass++)
13956     {
13957       for (i = 0; i < els; i++)
13958         {
13959           unsigned thisarg = types[i];
13960           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
13961             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
13962           enum neon_el_type g_type = inst.vectype.el[i].type;
13963           unsigned g_size = inst.vectype.el[i].size;
13964
13965           /* Decay more-specific signed & unsigned types to sign-insensitive
13966              integer types if sign-specific variants are unavailable.  */
13967           if ((g_type == NT_signed || g_type == NT_unsigned)
13968               && (types_allowed & N_SU_ALL) == 0)
13969             g_type = NT_integer;
13970
13971           /* If only untyped args are allowed, decay any more specific types to
13972              them. Some instructions only care about signs for some element
13973              sizes, so handle that properly.  */
13974           if (((types_allowed & N_UNT) == 0)
13975               && ((g_size == 8 && (types_allowed & N_8) != 0)
13976                   || (g_size == 16 && (types_allowed & N_16) != 0)
13977                   || (g_size == 32 && (types_allowed & N_32) != 0)
13978                   || (g_size == 64 && (types_allowed & N_64) != 0)))
13979             g_type = NT_untyped;
13980
13981           if (pass == 0)
13982             {
13983               if ((thisarg & N_KEY) != 0)
13984                 {
13985                   k_type = g_type;
13986                   k_size = g_size;
13987                   key_allowed = thisarg & ~N_KEY;
13988
13989                   /* Check architecture constraint on FP16 extension.  */
13990                   if (k_size == 16
13991                       && k_type == NT_float
13992                       && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
13993                     {
13994                       inst.error = _(BAD_FP16);
13995                       return badtype;
13996                     }
13997                 }
13998             }
13999           else
14000             {
14001               if ((thisarg & N_VFP) != 0)
14002                 {
14003                   enum neon_shape_el regshape;
14004                   unsigned regwidth, match;
14005
14006                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
14007                   if (ns == NS_NULL)
14008                     {
14009                       first_error (_("invalid instruction shape"));
14010                       return badtype;
14011                     }
14012                   regshape = neon_shape_tab[ns].el[i];
14013                   regwidth = neon_shape_el_size[regshape];
14014
14015                   /* In VFP mode, operands must match register widths. If we
14016                      have a key operand, use its width, else use the width of
14017                      the current operand.  */
14018                   if (k_size != -1u)
14019                     match = k_size;
14020                   else
14021                     match = g_size;
14022
14023                   /* FP16 will use a single precision register.  */
14024                   if (regwidth == 32 && match == 16)
14025                     {
14026                       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16))
14027                         match = regwidth;
14028                       else
14029                         {
14030                           inst.error = _(BAD_FP16);
14031                           return badtype;
14032                         }
14033                     }
14034
14035                   if (regwidth != match)
14036                     {
14037                       first_error (_("operand size must match register width"));
14038                       return badtype;
14039                     }
14040                 }
14041
14042               if ((thisarg & N_EQK) == 0)
14043                 {
14044                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
14045
14046                   if ((given_type & types_allowed) == 0)
14047                     {
14048                       first_error (_("bad type in Neon instruction"));
14049                       return badtype;
14050                     }
14051                 }
14052               else
14053                 {
14054                   enum neon_el_type mod_k_type = k_type;
14055                   unsigned mod_k_size = k_size;
14056                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
14057                   if (g_type != mod_k_type || g_size != mod_k_size)
14058                     {
14059                       first_error (_("inconsistent types in Neon instruction"));
14060                       return badtype;
14061                     }
14062                 }
14063             }
14064         }
14065     }
14066
14067   return inst.vectype.el[key_el];
14068 }
14069
14070 /* Neon-style VFP instruction forwarding.  */
14071
14072 /* Thumb VFP instructions have 0xE in the condition field.  */
14073
14074 static void
14075 do_vfp_cond_or_thumb (void)
14076 {
14077   inst.is_neon = 1;
14078
14079   if (thumb_mode)
14080     inst.instruction |= 0xe0000000;
14081   else
14082     inst.instruction |= inst.cond << 28;
14083 }
14084
14085 /* Look up and encode a simple mnemonic, for use as a helper function for the
14086    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
14087    etc.  It is assumed that operand parsing has already been done, and that the
14088    operands are in the form expected by the given opcode (this isn't necessarily
14089    the same as the form in which they were parsed, hence some massaging must
14090    take place before this function is called).
14091    Checks current arch version against that in the looked-up opcode.  */
14092
14093 static void
14094 do_vfp_nsyn_opcode (const char *opname)
14095 {
14096   const struct asm_opcode *opcode;
14097
14098   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
14099
14100   if (!opcode)
14101     abort ();
14102
14103   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
14104                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
14105               _(BAD_FPU));
14106
14107   inst.is_neon = 1;
14108
14109   if (thumb_mode)
14110     {
14111       inst.instruction = opcode->tvalue;
14112       opcode->tencode ();
14113     }
14114   else
14115     {
14116       inst.instruction = (inst.cond << 28) | opcode->avalue;
14117       opcode->aencode ();
14118     }
14119 }
14120
14121 static void
14122 do_vfp_nsyn_add_sub (enum neon_shape rs)
14123 {
14124   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
14125
14126   if (rs == NS_FFF || rs == NS_HHH)
14127     {
14128       if (is_add)
14129         do_vfp_nsyn_opcode ("fadds");
14130       else
14131         do_vfp_nsyn_opcode ("fsubs");
14132
14133       /* ARMv8.2 fp16 instruction.  */
14134       if (rs == NS_HHH)
14135         do_scalar_fp16_v82_encode ();
14136     }
14137   else
14138     {
14139       if (is_add)
14140         do_vfp_nsyn_opcode ("faddd");
14141       else
14142         do_vfp_nsyn_opcode ("fsubd");
14143     }
14144 }
14145
14146 /* Check operand types to see if this is a VFP instruction, and if so call
14147    PFN ().  */
14148
14149 static int
14150 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
14151 {
14152   enum neon_shape rs;
14153   struct neon_type_el et;
14154
14155   switch (args)
14156     {
14157     case 2:
14158       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14159       et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14160       break;
14161
14162     case 3:
14163       rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14164       et = neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14165                             N_F_ALL | N_KEY | N_VFP);
14166       break;
14167
14168     default:
14169       abort ();
14170     }
14171
14172   if (et.type != NT_invtype)
14173     {
14174       pfn (rs);
14175       return SUCCESS;
14176     }
14177
14178   inst.error = NULL;
14179   return FAIL;
14180 }
14181
14182 static void
14183 do_vfp_nsyn_mla_mls (enum neon_shape rs)
14184 {
14185   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
14186
14187   if (rs == NS_FFF || rs == NS_HHH)
14188     {
14189       if (is_mla)
14190         do_vfp_nsyn_opcode ("fmacs");
14191       else
14192         do_vfp_nsyn_opcode ("fnmacs");
14193
14194       /* ARMv8.2 fp16 instruction.  */
14195       if (rs == NS_HHH)
14196         do_scalar_fp16_v82_encode ();
14197     }
14198   else
14199     {
14200       if (is_mla)
14201         do_vfp_nsyn_opcode ("fmacd");
14202       else
14203         do_vfp_nsyn_opcode ("fnmacd");
14204     }
14205 }
14206
14207 static void
14208 do_vfp_nsyn_fma_fms (enum neon_shape rs)
14209 {
14210   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
14211
14212   if (rs == NS_FFF || rs == NS_HHH)
14213     {
14214       if (is_fma)
14215         do_vfp_nsyn_opcode ("ffmas");
14216       else
14217         do_vfp_nsyn_opcode ("ffnmas");
14218
14219       /* ARMv8.2 fp16 instruction.  */
14220       if (rs == NS_HHH)
14221         do_scalar_fp16_v82_encode ();
14222     }
14223   else
14224     {
14225       if (is_fma)
14226         do_vfp_nsyn_opcode ("ffmad");
14227       else
14228         do_vfp_nsyn_opcode ("ffnmad");
14229     }
14230 }
14231
14232 static void
14233 do_vfp_nsyn_mul (enum neon_shape rs)
14234 {
14235   if (rs == NS_FFF || rs == NS_HHH)
14236     {
14237       do_vfp_nsyn_opcode ("fmuls");
14238
14239       /* ARMv8.2 fp16 instruction.  */
14240       if (rs == NS_HHH)
14241         do_scalar_fp16_v82_encode ();
14242     }
14243   else
14244     do_vfp_nsyn_opcode ("fmuld");
14245 }
14246
14247 static void
14248 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14249 {
14250   int is_neg = (inst.instruction & 0x80) != 0;
14251   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_VFP | N_KEY);
14252
14253   if (rs == NS_FF || rs == NS_HH)
14254     {
14255       if (is_neg)
14256         do_vfp_nsyn_opcode ("fnegs");
14257       else
14258         do_vfp_nsyn_opcode ("fabss");
14259
14260       /* ARMv8.2 fp16 instruction.  */
14261       if (rs == NS_HH)
14262         do_scalar_fp16_v82_encode ();
14263     }
14264   else
14265     {
14266       if (is_neg)
14267         do_vfp_nsyn_opcode ("fnegd");
14268       else
14269         do_vfp_nsyn_opcode ("fabsd");
14270     }
14271 }
14272
14273 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14274    insns belong to Neon, and are handled elsewhere.  */
14275
14276 static void
14277 do_vfp_nsyn_ldm_stm (int is_dbmode)
14278 {
14279   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14280   if (is_ldm)
14281     {
14282       if (is_dbmode)
14283         do_vfp_nsyn_opcode ("fldmdbs");
14284       else
14285         do_vfp_nsyn_opcode ("fldmias");
14286     }
14287   else
14288     {
14289       if (is_dbmode)
14290         do_vfp_nsyn_opcode ("fstmdbs");
14291       else
14292         do_vfp_nsyn_opcode ("fstmias");
14293     }
14294 }
14295
14296 static void
14297 do_vfp_nsyn_sqrt (void)
14298 {
14299   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14300   neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14301
14302   if (rs == NS_FF || rs == NS_HH)
14303     {
14304       do_vfp_nsyn_opcode ("fsqrts");
14305
14306       /* ARMv8.2 fp16 instruction.  */
14307       if (rs == NS_HH)
14308         do_scalar_fp16_v82_encode ();
14309     }
14310   else
14311     do_vfp_nsyn_opcode ("fsqrtd");
14312 }
14313
14314 static void
14315 do_vfp_nsyn_div (void)
14316 {
14317   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14318   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14319                    N_F_ALL | N_KEY | N_VFP);
14320
14321   if (rs == NS_FFF || rs == NS_HHH)
14322     {
14323       do_vfp_nsyn_opcode ("fdivs");
14324
14325       /* ARMv8.2 fp16 instruction.  */
14326       if (rs == NS_HHH)
14327         do_scalar_fp16_v82_encode ();
14328     }
14329   else
14330     do_vfp_nsyn_opcode ("fdivd");
14331 }
14332
14333 static void
14334 do_vfp_nsyn_nmul (void)
14335 {
14336   enum neon_shape rs = neon_select_shape (NS_HHH, NS_FFF, NS_DDD, NS_NULL);
14337   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14338                    N_F_ALL | N_KEY | N_VFP);
14339
14340   if (rs == NS_FFF || rs == NS_HHH)
14341     {
14342       NEON_ENCODE (SINGLE, inst);
14343       do_vfp_sp_dyadic ();
14344
14345       /* ARMv8.2 fp16 instruction.  */
14346       if (rs == NS_HHH)
14347         do_scalar_fp16_v82_encode ();
14348     }
14349   else
14350     {
14351       NEON_ENCODE (DOUBLE, inst);
14352       do_vfp_dp_rd_rn_rm ();
14353     }
14354   do_vfp_cond_or_thumb ();
14355
14356 }
14357
14358 static void
14359 do_vfp_nsyn_cmp (void)
14360 {
14361   enum neon_shape rs;
14362   if (inst.operands[1].isreg)
14363     {
14364       rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_NULL);
14365       neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY | N_VFP);
14366
14367       if (rs == NS_FF || rs == NS_HH)
14368         {
14369           NEON_ENCODE (SINGLE, inst);
14370           do_vfp_sp_monadic ();
14371         }
14372       else
14373         {
14374           NEON_ENCODE (DOUBLE, inst);
14375           do_vfp_dp_rd_rm ();
14376         }
14377     }
14378   else
14379     {
14380       rs = neon_select_shape (NS_HI, NS_FI, NS_DI, NS_NULL);
14381       neon_check_type (2, rs, N_F_ALL | N_KEY | N_VFP, N_EQK);
14382
14383       switch (inst.instruction & 0x0fffffff)
14384         {
14385         case N_MNEM_vcmp:
14386           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
14387           break;
14388         case N_MNEM_vcmpe:
14389           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
14390           break;
14391         default:
14392           abort ();
14393         }
14394
14395       if (rs == NS_FI || rs == NS_HI)
14396         {
14397           NEON_ENCODE (SINGLE, inst);
14398           do_vfp_sp_compare_z ();
14399         }
14400       else
14401         {
14402           NEON_ENCODE (DOUBLE, inst);
14403           do_vfp_dp_rd ();
14404         }
14405     }
14406   do_vfp_cond_or_thumb ();
14407
14408   /* ARMv8.2 fp16 instruction.  */
14409   if (rs == NS_HI || rs == NS_HH)
14410     do_scalar_fp16_v82_encode ();
14411 }
14412
14413 static void
14414 nsyn_insert_sp (void)
14415 {
14416   inst.operands[1] = inst.operands[0];
14417   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
14418   inst.operands[0].reg = REG_SP;
14419   inst.operands[0].isreg = 1;
14420   inst.operands[0].writeback = 1;
14421   inst.operands[0].present = 1;
14422 }
14423
14424 static void
14425 do_vfp_nsyn_push (void)
14426 {
14427   nsyn_insert_sp ();
14428
14429   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14430               _("register list must contain at least 1 and at most 16 "
14431                 "registers"));
14432
14433   if (inst.operands[1].issingle)
14434     do_vfp_nsyn_opcode ("fstmdbs");
14435   else
14436     do_vfp_nsyn_opcode ("fstmdbd");
14437 }
14438
14439 static void
14440 do_vfp_nsyn_pop (void)
14441 {
14442   nsyn_insert_sp ();
14443
14444   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
14445               _("register list must contain at least 1 and at most 16 "
14446                 "registers"));
14447
14448   if (inst.operands[1].issingle)
14449     do_vfp_nsyn_opcode ("fldmias");
14450   else
14451     do_vfp_nsyn_opcode ("fldmiad");
14452 }
14453
14454 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14455    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14456
14457 static void
14458 neon_dp_fixup (struct arm_it* insn)
14459 {
14460   unsigned int i = insn->instruction;
14461   insn->is_neon = 1;
14462
14463   if (thumb_mode)
14464     {
14465       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14466       if (i & (1 << 24))
14467         i |= 1 << 28;
14468
14469       i &= ~(1 << 24);
14470
14471       i |= 0xef000000;
14472     }
14473   else
14474     i |= 0xf2000000;
14475
14476   insn->instruction = i;
14477 }
14478
14479 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14480    (0, 1, 2, 3).  */
14481
14482 static unsigned
14483 neon_logbits (unsigned x)
14484 {
14485   return ffs (x) - 4;
14486 }
14487
14488 #define LOW4(R) ((R) & 0xf)
14489 #define HI1(R) (((R) >> 4) & 1)
14490
14491 /* Encode insns with bit pattern:
14492
14493   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14494   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14495
14496   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14497   different meaning for some instruction.  */
14498
14499 static void
14500 neon_three_same (int isquad, int ubit, int size)
14501 {
14502   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14503   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14504   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14505   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14506   inst.instruction |= LOW4 (inst.operands[2].reg);
14507   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14508   inst.instruction |= (isquad != 0) << 6;
14509   inst.instruction |= (ubit != 0) << 24;
14510   if (size != -1)
14511     inst.instruction |= neon_logbits (size) << 20;
14512
14513   neon_dp_fixup (&inst);
14514 }
14515
14516 /* Encode instructions of the form:
14517
14518   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14519   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14520
14521   Don't write size if SIZE == -1.  */
14522
14523 static void
14524 neon_two_same (int qbit, int ubit, int size)
14525 {
14526   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14527   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14528   inst.instruction |= LOW4 (inst.operands[1].reg);
14529   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14530   inst.instruction |= (qbit != 0) << 6;
14531   inst.instruction |= (ubit != 0) << 24;
14532
14533   if (size != -1)
14534     inst.instruction |= neon_logbits (size) << 18;
14535
14536   neon_dp_fixup (&inst);
14537 }
14538
14539 /* Neon instruction encoders, in approximate order of appearance.  */
14540
14541 static void
14542 do_neon_dyadic_i_su (void)
14543 {
14544   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14545   struct neon_type_el et = neon_check_type (3, rs,
14546     N_EQK, N_EQK, N_SU_32 | N_KEY);
14547   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14548 }
14549
14550 static void
14551 do_neon_dyadic_i64_su (void)
14552 {
14553   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14554   struct neon_type_el et = neon_check_type (3, rs,
14555     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14556   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14557 }
14558
14559 static void
14560 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14561                 unsigned immbits)
14562 {
14563   unsigned size = et.size >> 3;
14564   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14565   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14566   inst.instruction |= LOW4 (inst.operands[1].reg);
14567   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14568   inst.instruction |= (isquad != 0) << 6;
14569   inst.instruction |= immbits << 16;
14570   inst.instruction |= (size >> 3) << 7;
14571   inst.instruction |= (size & 0x7) << 19;
14572   if (write_ubit)
14573     inst.instruction |= (uval != 0) << 24;
14574
14575   neon_dp_fixup (&inst);
14576 }
14577
14578 static void
14579 do_neon_shl_imm (void)
14580 {
14581   if (!inst.operands[2].isreg)
14582     {
14583       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14584       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14585       int imm = inst.operands[2].imm;
14586
14587       constraint (imm < 0 || (unsigned)imm >= et.size,
14588                   _("immediate out of range for shift"));
14589       NEON_ENCODE (IMMED, inst);
14590       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14591     }
14592   else
14593     {
14594       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14595       struct neon_type_el et = neon_check_type (3, rs,
14596         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14597       unsigned int tmp;
14598
14599       /* VSHL/VQSHL 3-register variants have syntax such as:
14600            vshl.xx Dd, Dm, Dn
14601          whereas other 3-register operations encoded by neon_three_same have
14602          syntax like:
14603            vadd.xx Dd, Dn, Dm
14604          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14605          here.  */
14606       tmp = inst.operands[2].reg;
14607       inst.operands[2].reg = inst.operands[1].reg;
14608       inst.operands[1].reg = tmp;
14609       NEON_ENCODE (INTEGER, inst);
14610       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14611     }
14612 }
14613
14614 static void
14615 do_neon_qshl_imm (void)
14616 {
14617   if (!inst.operands[2].isreg)
14618     {
14619       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14620       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14621       int imm = inst.operands[2].imm;
14622
14623       constraint (imm < 0 || (unsigned)imm >= et.size,
14624                   _("immediate out of range for shift"));
14625       NEON_ENCODE (IMMED, inst);
14626       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14627     }
14628   else
14629     {
14630       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14631       struct neon_type_el et = neon_check_type (3, rs,
14632         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14633       unsigned int tmp;
14634
14635       /* See note in do_neon_shl_imm.  */
14636       tmp = inst.operands[2].reg;
14637       inst.operands[2].reg = inst.operands[1].reg;
14638       inst.operands[1].reg = tmp;
14639       NEON_ENCODE (INTEGER, inst);
14640       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14641     }
14642 }
14643
14644 static void
14645 do_neon_rshl (void)
14646 {
14647   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14648   struct neon_type_el et = neon_check_type (3, rs,
14649     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14650   unsigned int tmp;
14651
14652   tmp = inst.operands[2].reg;
14653   inst.operands[2].reg = inst.operands[1].reg;
14654   inst.operands[1].reg = tmp;
14655   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14656 }
14657
14658 static int
14659 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14660 {
14661   /* Handle .I8 pseudo-instructions.  */
14662   if (size == 8)
14663     {
14664       /* Unfortunately, this will make everything apart from zero out-of-range.
14665          FIXME is this the intended semantics? There doesn't seem much point in
14666          accepting .I8 if so.  */
14667       immediate |= immediate << 8;
14668       size = 16;
14669     }
14670
14671   if (size >= 32)
14672     {
14673       if (immediate == (immediate & 0x000000ff))
14674         {
14675           *immbits = immediate;
14676           return 0x1;
14677         }
14678       else if (immediate == (immediate & 0x0000ff00))
14679         {
14680           *immbits = immediate >> 8;
14681           return 0x3;
14682         }
14683       else if (immediate == (immediate & 0x00ff0000))
14684         {
14685           *immbits = immediate >> 16;
14686           return 0x5;
14687         }
14688       else if (immediate == (immediate & 0xff000000))
14689         {
14690           *immbits = immediate >> 24;
14691           return 0x7;
14692         }
14693       if ((immediate & 0xffff) != (immediate >> 16))
14694         goto bad_immediate;
14695       immediate &= 0xffff;
14696     }
14697
14698   if (immediate == (immediate & 0x000000ff))
14699     {
14700       *immbits = immediate;
14701       return 0x9;
14702     }
14703   else if (immediate == (immediate & 0x0000ff00))
14704     {
14705       *immbits = immediate >> 8;
14706       return 0xb;
14707     }
14708
14709   bad_immediate:
14710   first_error (_("immediate value out of range"));
14711   return FAIL;
14712 }
14713
14714 static void
14715 do_neon_logic (void)
14716 {
14717   if (inst.operands[2].present && inst.operands[2].isreg)
14718     {
14719       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14720       neon_check_type (3, rs, N_IGNORE_TYPE);
14721       /* U bit and size field were set as part of the bitmask.  */
14722       NEON_ENCODE (INTEGER, inst);
14723       neon_three_same (neon_quad (rs), 0, -1);
14724     }
14725   else
14726     {
14727       const int three_ops_form = (inst.operands[2].present
14728                                   && !inst.operands[2].isreg);
14729       const int immoperand = (three_ops_form ? 2 : 1);
14730       enum neon_shape rs = (three_ops_form
14731                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14732                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14733       struct neon_type_el et = neon_check_type (2, rs,
14734         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14735       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14736       unsigned immbits;
14737       int cmode;
14738
14739       if (et.type == NT_invtype)
14740         return;
14741
14742       if (three_ops_form)
14743         constraint (inst.operands[0].reg != inst.operands[1].reg,
14744                     _("first and second operands shall be the same register"));
14745
14746       NEON_ENCODE (IMMED, inst);
14747
14748       immbits = inst.operands[immoperand].imm;
14749       if (et.size == 64)
14750         {
14751           /* .i64 is a pseudo-op, so the immediate must be a repeating
14752              pattern.  */
14753           if (immbits != (inst.operands[immoperand].regisimm ?
14754                           inst.operands[immoperand].reg : 0))
14755             {
14756               /* Set immbits to an invalid constant.  */
14757               immbits = 0xdeadbeef;
14758             }
14759         }
14760
14761       switch (opcode)
14762         {
14763         case N_MNEM_vbic:
14764           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14765           break;
14766
14767         case N_MNEM_vorr:
14768           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14769           break;
14770
14771         case N_MNEM_vand:
14772           /* Pseudo-instruction for VBIC.  */
14773           neon_invert_size (&immbits, 0, et.size);
14774           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14775           break;
14776
14777         case N_MNEM_vorn:
14778           /* Pseudo-instruction for VORR.  */
14779           neon_invert_size (&immbits, 0, et.size);
14780           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14781           break;
14782
14783         default:
14784           abort ();
14785         }
14786
14787       if (cmode == FAIL)
14788         return;
14789
14790       inst.instruction |= neon_quad (rs) << 6;
14791       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14792       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14793       inst.instruction |= cmode << 8;
14794       neon_write_immbits (immbits);
14795
14796       neon_dp_fixup (&inst);
14797     }
14798 }
14799
14800 static void
14801 do_neon_bitfield (void)
14802 {
14803   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14804   neon_check_type (3, rs, N_IGNORE_TYPE);
14805   neon_three_same (neon_quad (rs), 0, -1);
14806 }
14807
14808 static void
14809 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14810                   unsigned destbits)
14811 {
14812   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14813   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14814                                             types | N_KEY);
14815   if (et.type == NT_float)
14816     {
14817       NEON_ENCODE (FLOAT, inst);
14818       neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
14819     }
14820   else
14821     {
14822       NEON_ENCODE (INTEGER, inst);
14823       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14824     }
14825 }
14826
14827 static void
14828 do_neon_dyadic_if_su (void)
14829 {
14830   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14831 }
14832
14833 static void
14834 do_neon_dyadic_if_su_d (void)
14835 {
14836   /* This version only allow D registers, but that constraint is enforced during
14837      operand parsing so we don't need to do anything extra here.  */
14838   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14839 }
14840
14841 static void
14842 do_neon_dyadic_if_i_d (void)
14843 {
14844   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14845      affected if we specify unsigned args.  */
14846   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14847 }
14848
14849 enum vfp_or_neon_is_neon_bits
14850 {
14851   NEON_CHECK_CC = 1,
14852   NEON_CHECK_ARCH = 2,
14853   NEON_CHECK_ARCH8 = 4
14854 };
14855
14856 /* Call this function if an instruction which may have belonged to the VFP or
14857    Neon instruction sets, but turned out to be a Neon instruction (due to the
14858    operand types involved, etc.). We have to check and/or fix-up a couple of
14859    things:
14860
14861      - Make sure the user hasn't attempted to make a Neon instruction
14862        conditional.
14863      - Alter the value in the condition code field if necessary.
14864      - Make sure that the arch supports Neon instructions.
14865
14866    Which of these operations take place depends on bits from enum
14867    vfp_or_neon_is_neon_bits.
14868
14869    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14870    current instruction's condition is COND_ALWAYS, the condition field is
14871    changed to inst.uncond_value. This is necessary because instructions shared
14872    between VFP and Neon may be conditional for the VFP variants only, and the
14873    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14874
14875 static int
14876 vfp_or_neon_is_neon (unsigned check)
14877 {
14878   /* Conditions are always legal in Thumb mode (IT blocks).  */
14879   if (!thumb_mode && (check & NEON_CHECK_CC))
14880     {
14881       if (inst.cond != COND_ALWAYS)
14882         {
14883           first_error (_(BAD_COND));
14884           return FAIL;
14885         }
14886       if (inst.uncond_value != -1)
14887         inst.instruction |= inst.uncond_value << 28;
14888     }
14889
14890   if ((check & NEON_CHECK_ARCH)
14891       && !mark_feature_used (&fpu_neon_ext_v1))
14892     {
14893       first_error (_(BAD_FPU));
14894       return FAIL;
14895     }
14896
14897   if ((check & NEON_CHECK_ARCH8)
14898       && !mark_feature_used (&fpu_neon_ext_armv8))
14899     {
14900       first_error (_(BAD_FPU));
14901       return FAIL;
14902     }
14903
14904   return SUCCESS;
14905 }
14906
14907 static void
14908 do_neon_addsub_if_i (void)
14909 {
14910   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
14911     return;
14912
14913   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14914     return;
14915
14916   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14917      affected if we specify unsigned args.  */
14918   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
14919 }
14920
14921 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
14922    result to be:
14923      V<op> A,B     (A is operand 0, B is operand 2)
14924    to mean:
14925      V<op> A,B,A
14926    not:
14927      V<op> A,B,B
14928    so handle that case specially.  */
14929
14930 static void
14931 neon_exchange_operands (void)
14932 {
14933   if (inst.operands[1].present)
14934     {
14935       void *scratch = xmalloc (sizeof (inst.operands[0]));
14936
14937       /* Swap operands[1] and operands[2].  */
14938       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
14939       inst.operands[1] = inst.operands[2];
14940       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
14941       free (scratch);
14942     }
14943   else
14944     {
14945       inst.operands[1] = inst.operands[2];
14946       inst.operands[2] = inst.operands[0];
14947     }
14948 }
14949
14950 static void
14951 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
14952 {
14953   if (inst.operands[2].isreg)
14954     {
14955       if (invert)
14956         neon_exchange_operands ();
14957       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
14958     }
14959   else
14960     {
14961       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14962       struct neon_type_el et = neon_check_type (2, rs,
14963         N_EQK | N_SIZ, immtypes | N_KEY);
14964
14965       NEON_ENCODE (IMMED, inst);
14966       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14967       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14968       inst.instruction |= LOW4 (inst.operands[1].reg);
14969       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14970       inst.instruction |= neon_quad (rs) << 6;
14971       inst.instruction |= (et.type == NT_float) << 10;
14972       inst.instruction |= neon_logbits (et.size) << 18;
14973
14974       neon_dp_fixup (&inst);
14975     }
14976 }
14977
14978 static void
14979 do_neon_cmp (void)
14980 {
14981   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, FALSE);
14982 }
14983
14984 static void
14985 do_neon_cmp_inv (void)
14986 {
14987   neon_compare (N_SUF_32, N_S_32 | N_F_16_32, TRUE);
14988 }
14989
14990 static void
14991 do_neon_ceq (void)
14992 {
14993   neon_compare (N_IF_32, N_IF_32, FALSE);
14994 }
14995
14996 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
14997    scalars, which are encoded in 5 bits, M : Rm.
14998    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
14999    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
15000    index in M.  */
15001
15002 static unsigned
15003 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
15004 {
15005   unsigned regno = NEON_SCALAR_REG (scalar);
15006   unsigned elno = NEON_SCALAR_INDEX (scalar);
15007
15008   switch (elsize)
15009     {
15010     case 16:
15011       if (regno > 7 || elno > 3)
15012         goto bad_scalar;
15013       return regno | (elno << 3);
15014
15015     case 32:
15016       if (regno > 15 || elno > 1)
15017         goto bad_scalar;
15018       return regno | (elno << 4);
15019
15020     default:
15021     bad_scalar:
15022       first_error (_("scalar out of range for multiply instruction"));
15023     }
15024
15025   return 0;
15026 }
15027
15028 /* Encode multiply / multiply-accumulate scalar instructions.  */
15029
15030 static void
15031 neon_mul_mac (struct neon_type_el et, int ubit)
15032 {
15033   unsigned scalar;
15034
15035   /* Give a more helpful error message if we have an invalid type.  */
15036   if (et.type == NT_invtype)
15037     return;
15038
15039   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
15040   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15041   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15042   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15043   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15044   inst.instruction |= LOW4 (scalar);
15045   inst.instruction |= HI1 (scalar) << 5;
15046   inst.instruction |= (et.type == NT_float) << 8;
15047   inst.instruction |= neon_logbits (et.size) << 20;
15048   inst.instruction |= (ubit != 0) << 24;
15049
15050   neon_dp_fixup (&inst);
15051 }
15052
15053 static void
15054 do_neon_mac_maybe_scalar (void)
15055 {
15056   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
15057     return;
15058
15059   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15060     return;
15061
15062   if (inst.operands[2].isscalar)
15063     {
15064       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15065       struct neon_type_el et = neon_check_type (3, rs,
15066         N_EQK, N_EQK, N_I16 | N_I32 | N_F_16_32 | N_KEY);
15067       NEON_ENCODE (SCALAR, inst);
15068       neon_mul_mac (et, neon_quad (rs));
15069     }
15070   else
15071     {
15072       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
15073          affected if we specify unsigned args.  */
15074       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15075     }
15076 }
15077
15078 static void
15079 do_neon_fmac (void)
15080 {
15081   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
15082     return;
15083
15084   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15085     return;
15086
15087   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
15088 }
15089
15090 static void
15091 do_neon_tst (void)
15092 {
15093   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15094   struct neon_type_el et = neon_check_type (3, rs,
15095     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
15096   neon_three_same (neon_quad (rs), 0, et.size);
15097 }
15098
15099 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
15100    same types as the MAC equivalents. The polynomial type for this instruction
15101    is encoded the same as the integer type.  */
15102
15103 static void
15104 do_neon_mul (void)
15105 {
15106   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
15107     return;
15108
15109   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15110     return;
15111
15112   if (inst.operands[2].isscalar)
15113     do_neon_mac_maybe_scalar ();
15114   else
15115     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F16 | N_F32 | N_P8, 0);
15116 }
15117
15118 static void
15119 do_neon_qdmulh (void)
15120 {
15121   if (inst.operands[2].isscalar)
15122     {
15123       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15124       struct neon_type_el et = neon_check_type (3, rs,
15125         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15126       NEON_ENCODE (SCALAR, inst);
15127       neon_mul_mac (et, neon_quad (rs));
15128     }
15129   else
15130     {
15131       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15132       struct neon_type_el et = neon_check_type (3, rs,
15133         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15134       NEON_ENCODE (INTEGER, inst);
15135       /* The U bit (rounding) comes from bit mask.  */
15136       neon_three_same (neon_quad (rs), 0, et.size);
15137     }
15138 }
15139
15140 static void
15141 do_neon_qrdmlah (void)
15142 {
15143   /* Check we're on the correct architecture.  */
15144   if (!mark_feature_used (&fpu_neon_ext_armv8))
15145     inst.error =
15146       _("instruction form not available on this architecture.");
15147   else if (!mark_feature_used (&fpu_neon_ext_v8_1))
15148     {
15149       as_warn (_("this instruction implies use of ARMv8.1 AdvSIMD."));
15150       record_feature_use (&fpu_neon_ext_v8_1);
15151     }
15152
15153   if (inst.operands[2].isscalar)
15154     {
15155       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
15156       struct neon_type_el et = neon_check_type (3, rs,
15157         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15158       NEON_ENCODE (SCALAR, inst);
15159       neon_mul_mac (et, neon_quad (rs));
15160     }
15161   else
15162     {
15163       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15164       struct neon_type_el et = neon_check_type (3, rs,
15165         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
15166       NEON_ENCODE (INTEGER, inst);
15167       /* The U bit (rounding) comes from bit mask.  */
15168       neon_three_same (neon_quad (rs), 0, et.size);
15169     }
15170 }
15171
15172 static void
15173 do_neon_fcmp_absolute (void)
15174 {
15175   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15176   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15177                                             N_F_16_32 | N_KEY);
15178   /* Size field comes from bit mask.  */
15179   neon_three_same (neon_quad (rs), 1, et.size == 16 ? (int) et.size : -1);
15180 }
15181
15182 static void
15183 do_neon_fcmp_absolute_inv (void)
15184 {
15185   neon_exchange_operands ();
15186   do_neon_fcmp_absolute ();
15187 }
15188
15189 static void
15190 do_neon_step (void)
15191 {
15192   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
15193   struct neon_type_el et = neon_check_type (3, rs, N_EQK, N_EQK,
15194                                             N_F_16_32 | N_KEY);
15195   neon_three_same (neon_quad (rs), 0, et.size == 16 ? (int) et.size : -1);
15196 }
15197
15198 static void
15199 do_neon_abs_neg (void)
15200 {
15201   enum neon_shape rs;
15202   struct neon_type_el et;
15203
15204   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
15205     return;
15206
15207   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15208     return;
15209
15210   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15211   et = neon_check_type (2, rs, N_EQK, N_S_32 | N_F_16_32 | N_KEY);
15212
15213   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15214   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15215   inst.instruction |= LOW4 (inst.operands[1].reg);
15216   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15217   inst.instruction |= neon_quad (rs) << 6;
15218   inst.instruction |= (et.type == NT_float) << 10;
15219   inst.instruction |= neon_logbits (et.size) << 18;
15220
15221   neon_dp_fixup (&inst);
15222 }
15223
15224 static void
15225 do_neon_sli (void)
15226 {
15227   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15228   struct neon_type_el et = neon_check_type (2, rs,
15229     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15230   int imm = inst.operands[2].imm;
15231   constraint (imm < 0 || (unsigned)imm >= et.size,
15232               _("immediate out of range for insert"));
15233   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15234 }
15235
15236 static void
15237 do_neon_sri (void)
15238 {
15239   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15240   struct neon_type_el et = neon_check_type (2, rs,
15241     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15242   int imm = inst.operands[2].imm;
15243   constraint (imm < 1 || (unsigned)imm > et.size,
15244               _("immediate out of range for insert"));
15245   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
15246 }
15247
15248 static void
15249 do_neon_qshlu_imm (void)
15250 {
15251   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
15252   struct neon_type_el et = neon_check_type (2, rs,
15253     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
15254   int imm = inst.operands[2].imm;
15255   constraint (imm < 0 || (unsigned)imm >= et.size,
15256               _("immediate out of range for shift"));
15257   /* Only encodes the 'U present' variant of the instruction.
15258      In this case, signed types have OP (bit 8) set to 0.
15259      Unsigned types have OP set to 1.  */
15260   inst.instruction |= (et.type == NT_unsigned) << 8;
15261   /* The rest of the bits are the same as other immediate shifts.  */
15262   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
15263 }
15264
15265 static void
15266 do_neon_qmovn (void)
15267 {
15268   struct neon_type_el et = neon_check_type (2, NS_DQ,
15269     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15270   /* Saturating move where operands can be signed or unsigned, and the
15271      destination has the same signedness.  */
15272   NEON_ENCODE (INTEGER, inst);
15273   if (et.type == NT_unsigned)
15274     inst.instruction |= 0xc0;
15275   else
15276     inst.instruction |= 0x80;
15277   neon_two_same (0, 1, et.size / 2);
15278 }
15279
15280 static void
15281 do_neon_qmovun (void)
15282 {
15283   struct neon_type_el et = neon_check_type (2, NS_DQ,
15284     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15285   /* Saturating move with unsigned results. Operands must be signed.  */
15286   NEON_ENCODE (INTEGER, inst);
15287   neon_two_same (0, 1, et.size / 2);
15288 }
15289
15290 static void
15291 do_neon_rshift_sat_narrow (void)
15292 {
15293   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15294      or unsigned. If operands are unsigned, results must also be unsigned.  */
15295   struct neon_type_el et = neon_check_type (2, NS_DQI,
15296     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
15297   int imm = inst.operands[2].imm;
15298   /* This gets the bounds check, size encoding and immediate bits calculation
15299      right.  */
15300   et.size /= 2;
15301
15302   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
15303      VQMOVN.I<size> <Dd>, <Qm>.  */
15304   if (imm == 0)
15305     {
15306       inst.operands[2].present = 0;
15307       inst.instruction = N_MNEM_vqmovn;
15308       do_neon_qmovn ();
15309       return;
15310     }
15311
15312   constraint (imm < 1 || (unsigned)imm > et.size,
15313               _("immediate out of range"));
15314   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
15315 }
15316
15317 static void
15318 do_neon_rshift_sat_narrow_u (void)
15319 {
15320   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15321      or unsigned. If operands are unsigned, results must also be unsigned.  */
15322   struct neon_type_el et = neon_check_type (2, NS_DQI,
15323     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15324   int imm = inst.operands[2].imm;
15325   /* This gets the bounds check, size encoding and immediate bits calculation
15326      right.  */
15327   et.size /= 2;
15328
15329   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
15330      VQMOVUN.I<size> <Dd>, <Qm>.  */
15331   if (imm == 0)
15332     {
15333       inst.operands[2].present = 0;
15334       inst.instruction = N_MNEM_vqmovun;
15335       do_neon_qmovun ();
15336       return;
15337     }
15338
15339   constraint (imm < 1 || (unsigned)imm > et.size,
15340               _("immediate out of range"));
15341   /* FIXME: The manual is kind of unclear about what value U should have in
15342      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
15343      must be 1.  */
15344   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
15345 }
15346
15347 static void
15348 do_neon_movn (void)
15349 {
15350   struct neon_type_el et = neon_check_type (2, NS_DQ,
15351     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15352   NEON_ENCODE (INTEGER, inst);
15353   neon_two_same (0, 1, et.size / 2);
15354 }
15355
15356 static void
15357 do_neon_rshift_narrow (void)
15358 {
15359   struct neon_type_el et = neon_check_type (2, NS_DQI,
15360     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15361   int imm = inst.operands[2].imm;
15362   /* This gets the bounds check, size encoding and immediate bits calculation
15363      right.  */
15364   et.size /= 2;
15365
15366   /* If immediate is zero then we are a pseudo-instruction for
15367      VMOVN.I<size> <Dd>, <Qm>  */
15368   if (imm == 0)
15369     {
15370       inst.operands[2].present = 0;
15371       inst.instruction = N_MNEM_vmovn;
15372       do_neon_movn ();
15373       return;
15374     }
15375
15376   constraint (imm < 1 || (unsigned)imm > et.size,
15377               _("immediate out of range for narrowing operation"));
15378   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
15379 }
15380
15381 static void
15382 do_neon_shll (void)
15383 {
15384   /* FIXME: Type checking when lengthening.  */
15385   struct neon_type_el et = neon_check_type (2, NS_QDI,
15386     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
15387   unsigned imm = inst.operands[2].imm;
15388
15389   if (imm == et.size)
15390     {
15391       /* Maximum shift variant.  */
15392       NEON_ENCODE (INTEGER, inst);
15393       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15394       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15395       inst.instruction |= LOW4 (inst.operands[1].reg);
15396       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15397       inst.instruction |= neon_logbits (et.size) << 18;
15398
15399       neon_dp_fixup (&inst);
15400     }
15401   else
15402     {
15403       /* A more-specific type check for non-max versions.  */
15404       et = neon_check_type (2, NS_QDI,
15405         N_EQK | N_DBL, N_SU_32 | N_KEY);
15406       NEON_ENCODE (IMMED, inst);
15407       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
15408     }
15409 }
15410
15411 /* Check the various types for the VCVT instruction, and return which version
15412    the current instruction is.  */
15413
15414 #define CVT_FLAVOUR_VAR                                                       \
15415   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
15416   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
15417   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
15418   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
15419   /* Half-precision conversions.  */                                          \
15420   CVT_VAR (s16_f16, N_S16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15421   CVT_VAR (u16_f16, N_U16, N_F16 | N_KEY, whole_reg, NULL, NULL, NULL)        \
15422   CVT_VAR (f16_s16, N_F16 | N_KEY, N_S16, whole_reg, NULL, NULL, NULL)        \
15423   CVT_VAR (f16_u16, N_F16 | N_KEY, N_U16, whole_reg, NULL, NULL, NULL)        \
15424   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
15425   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
15426   /* New VCVT instructions introduced by ARMv8.2 fp16 extension.              \
15427      Compared with single/double precision variants, only the co-processor    \
15428      field is different, so the encoding flow is reused here.  */             \
15429   CVT_VAR (f16_s32, N_F16 | N_KEY, N_S32, N_VFP, "fsltos", "fsitos", NULL)    \
15430   CVT_VAR (f16_u32, N_F16 | N_KEY, N_U32, N_VFP, "fultos", "fuitos", NULL)    \
15431   CVT_VAR (u32_f16, N_U32, N_F16 | N_KEY, N_VFP, "ftouls", "ftouis", "ftouizs")\
15432   CVT_VAR (s32_f16, N_S32, N_F16 | N_KEY, N_VFP, "ftosls", "ftosis", "ftosizs")\
15433   /* VFP instructions.  */                                                    \
15434   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
15435   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
15436   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
15437   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
15438   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
15439   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
15440   /* VFP instructions with bitshift.  */                                      \
15441   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
15442   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
15443   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
15444   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
15445   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
15446   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
15447   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
15448   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
15449
15450 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
15451   neon_cvt_flavour_##C,
15452
15453 /* The different types of conversions we can do.  */
15454 enum neon_cvt_flavour
15455 {
15456   CVT_FLAVOUR_VAR
15457   neon_cvt_flavour_invalid,
15458   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
15459 };
15460
15461 #undef CVT_VAR
15462
15463 static enum neon_cvt_flavour
15464 get_neon_cvt_flavour (enum neon_shape rs)
15465 {
15466 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
15467   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
15468   if (et.type != NT_invtype)                            \
15469     {                                                   \
15470       inst.error = NULL;                                \
15471       return (neon_cvt_flavour_##C);                    \
15472     }
15473
15474   struct neon_type_el et;
15475   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15476                         || rs == NS_FF) ? N_VFP : 0;
15477   /* The instruction versions which take an immediate take one register
15478      argument, which is extended to the width of the full register. Thus the
15479      "source" and "destination" registers must have the same width.  Hack that
15480      here by making the size equal to the key (wider, in this case) operand.  */
15481   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15482
15483   CVT_FLAVOUR_VAR;
15484
15485   return neon_cvt_flavour_invalid;
15486 #undef CVT_VAR
15487 }
15488
15489 enum neon_cvt_mode
15490 {
15491   neon_cvt_mode_a,
15492   neon_cvt_mode_n,
15493   neon_cvt_mode_p,
15494   neon_cvt_mode_m,
15495   neon_cvt_mode_z,
15496   neon_cvt_mode_x,
15497   neon_cvt_mode_r
15498 };
15499
15500 /* Neon-syntax VFP conversions.  */
15501
15502 static void
15503 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15504 {
15505   const char *opname = 0;
15506
15507   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI
15508       || rs == NS_FHI || rs == NS_HFI)
15509     {
15510       /* Conversions with immediate bitshift.  */
15511       const char *enc[] =
15512         {
15513 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15514           CVT_FLAVOUR_VAR
15515           NULL
15516 #undef CVT_VAR
15517         };
15518
15519       if (flavour < (int) ARRAY_SIZE (enc))
15520         {
15521           opname = enc[flavour];
15522           constraint (inst.operands[0].reg != inst.operands[1].reg,
15523                       _("operands 0 and 1 must be the same register"));
15524           inst.operands[1] = inst.operands[2];
15525           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15526         }
15527     }
15528   else
15529     {
15530       /* Conversions without bitshift.  */
15531       const char *enc[] =
15532         {
15533 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15534           CVT_FLAVOUR_VAR
15535           NULL
15536 #undef CVT_VAR
15537         };
15538
15539       if (flavour < (int) ARRAY_SIZE (enc))
15540         opname = enc[flavour];
15541     }
15542
15543   if (opname)
15544     do_vfp_nsyn_opcode (opname);
15545
15546   /* ARMv8.2 fp16 VCVT instruction.  */
15547   if (flavour == neon_cvt_flavour_s32_f16
15548       || flavour == neon_cvt_flavour_u32_f16
15549       || flavour == neon_cvt_flavour_f16_u32
15550       || flavour == neon_cvt_flavour_f16_s32)
15551     do_scalar_fp16_v82_encode ();
15552 }
15553
15554 static void
15555 do_vfp_nsyn_cvtz (void)
15556 {
15557   enum neon_shape rs = neon_select_shape (NS_FH, NS_FF, NS_FD, NS_NULL);
15558   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15559   const char *enc[] =
15560     {
15561 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15562       CVT_FLAVOUR_VAR
15563       NULL
15564 #undef CVT_VAR
15565     };
15566
15567   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15568     do_vfp_nsyn_opcode (enc[flavour]);
15569 }
15570
15571 static void
15572 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15573                       enum neon_cvt_mode mode)
15574 {
15575   int sz, op;
15576   int rm;
15577
15578   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15579      D register operands.  */
15580   if (flavour == neon_cvt_flavour_s32_f64
15581       || flavour == neon_cvt_flavour_u32_f64)
15582     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15583                 _(BAD_FPU));
15584
15585   if (flavour == neon_cvt_flavour_s32_f16
15586       || flavour == neon_cvt_flavour_u32_f16)
15587     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16),
15588                 _(BAD_FP16));
15589
15590   set_it_insn_type (OUTSIDE_IT_INSN);
15591
15592   switch (flavour)
15593     {
15594     case neon_cvt_flavour_s32_f64:
15595       sz = 1;
15596       op = 1;
15597       break;
15598     case neon_cvt_flavour_s32_f32:
15599       sz = 0;
15600       op = 1;
15601       break;
15602     case neon_cvt_flavour_s32_f16:
15603       sz = 0;
15604       op = 1;
15605       break;
15606     case neon_cvt_flavour_u32_f64:
15607       sz = 1;
15608       op = 0;
15609       break;
15610     case neon_cvt_flavour_u32_f32:
15611       sz = 0;
15612       op = 0;
15613       break;
15614     case neon_cvt_flavour_u32_f16:
15615       sz = 0;
15616       op = 0;
15617       break;
15618     default:
15619       first_error (_("invalid instruction shape"));
15620       return;
15621     }
15622
15623   switch (mode)
15624     {
15625     case neon_cvt_mode_a: rm = 0; break;
15626     case neon_cvt_mode_n: rm = 1; break;
15627     case neon_cvt_mode_p: rm = 2; break;
15628     case neon_cvt_mode_m: rm = 3; break;
15629     default: first_error (_("invalid rounding mode")); return;
15630     }
15631
15632   NEON_ENCODE (FPV8, inst);
15633   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15634   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15635   inst.instruction |= sz << 8;
15636
15637   /* ARMv8.2 fp16 VCVT instruction.  */
15638   if (flavour == neon_cvt_flavour_s32_f16
15639       ||flavour == neon_cvt_flavour_u32_f16)
15640     do_scalar_fp16_v82_encode ();
15641   inst.instruction |= op << 7;
15642   inst.instruction |= rm << 16;
15643   inst.instruction |= 0xf0000000;
15644   inst.is_neon = TRUE;
15645 }
15646
15647 static void
15648 do_neon_cvt_1 (enum neon_cvt_mode mode)
15649 {
15650   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15651                                           NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ,
15652                                           NS_FH, NS_HF, NS_FHI, NS_HFI,
15653                                           NS_NULL);
15654   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15655
15656   if (flavour == neon_cvt_flavour_invalid)
15657     return;
15658
15659   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15660   if (mode == neon_cvt_mode_z
15661       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15662       && (flavour == neon_cvt_flavour_s16_f16
15663           || flavour == neon_cvt_flavour_u16_f16
15664           || flavour == neon_cvt_flavour_s32_f32
15665           || flavour == neon_cvt_flavour_u32_f32
15666           || flavour == neon_cvt_flavour_s32_f64
15667           || flavour == neon_cvt_flavour_u32_f64)
15668       && (rs == NS_FD || rs == NS_FF))
15669     {
15670       do_vfp_nsyn_cvtz ();
15671       return;
15672     }
15673
15674   /* ARMv8.2 fp16 VCVT conversions.  */
15675   if (mode == neon_cvt_mode_z
15676       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_fp16)
15677       && (flavour == neon_cvt_flavour_s32_f16
15678           || flavour == neon_cvt_flavour_u32_f16)
15679       && (rs == NS_FH))
15680     {
15681       do_vfp_nsyn_cvtz ();
15682       do_scalar_fp16_v82_encode ();
15683       return;
15684     }
15685
15686   /* VFP rather than Neon conversions.  */
15687   if (flavour >= neon_cvt_flavour_first_fp)
15688     {
15689       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15690         do_vfp_nsyn_cvt (rs, flavour);
15691       else
15692         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15693
15694       return;
15695     }
15696
15697   switch (rs)
15698     {
15699     case NS_DDI:
15700     case NS_QQI:
15701       {
15702         unsigned immbits;
15703         unsigned enctab[] = {0x0000100, 0x1000100, 0x0, 0x1000000,
15704                              0x0000100, 0x1000100, 0x0, 0x1000000};
15705
15706         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15707           return;
15708
15709         /* Fixed-point conversion with #0 immediate is encoded as an
15710            integer conversion.  */
15711         if (inst.operands[2].present && inst.operands[2].imm == 0)
15712           goto int_encode;
15713         NEON_ENCODE (IMMED, inst);
15714         if (flavour != neon_cvt_flavour_invalid)
15715           inst.instruction |= enctab[flavour];
15716         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15717         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15718         inst.instruction |= LOW4 (inst.operands[1].reg);
15719         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15720         inst.instruction |= neon_quad (rs) << 6;
15721         inst.instruction |= 1 << 21;
15722         if (flavour < neon_cvt_flavour_s16_f16)
15723           {
15724             inst.instruction |= 1 << 21;
15725             immbits = 32 - inst.operands[2].imm;
15726             inst.instruction |= immbits << 16;
15727           }
15728         else
15729           {
15730             inst.instruction |= 3 << 20;
15731             immbits = 16 - inst.operands[2].imm;
15732             inst.instruction |= immbits << 16;
15733             inst.instruction &= ~(1 << 9);
15734           }
15735
15736         neon_dp_fixup (&inst);
15737       }
15738       break;
15739
15740     case NS_DD:
15741     case NS_QQ:
15742       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15743         {
15744           NEON_ENCODE (FLOAT, inst);
15745           set_it_insn_type (OUTSIDE_IT_INSN);
15746
15747           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15748             return;
15749
15750           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15751           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15752           inst.instruction |= LOW4 (inst.operands[1].reg);
15753           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15754           inst.instruction |= neon_quad (rs) << 6;
15755           inst.instruction |= (flavour == neon_cvt_flavour_u16_f16
15756                                || flavour == neon_cvt_flavour_u32_f32) << 7;
15757           inst.instruction |= mode << 8;
15758           if (flavour == neon_cvt_flavour_u16_f16
15759               || flavour == neon_cvt_flavour_s16_f16)
15760             /* Mask off the original size bits and reencode them.  */
15761             inst.instruction = ((inst.instruction & 0xfff3ffff) | (1 << 18));
15762
15763           if (thumb_mode)
15764             inst.instruction |= 0xfc000000;
15765           else
15766             inst.instruction |= 0xf0000000;
15767         }
15768       else
15769         {
15770     int_encode:
15771           {
15772             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080,
15773                                   0x100, 0x180, 0x0, 0x080};
15774
15775             NEON_ENCODE (INTEGER, inst);
15776
15777             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15778               return;
15779
15780             if (flavour != neon_cvt_flavour_invalid)
15781               inst.instruction |= enctab[flavour];
15782
15783             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15784             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15785             inst.instruction |= LOW4 (inst.operands[1].reg);
15786             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15787             inst.instruction |= neon_quad (rs) << 6;
15788             if (flavour >= neon_cvt_flavour_s16_f16
15789                 && flavour <= neon_cvt_flavour_f16_u16)
15790               /* Half precision.  */
15791               inst.instruction |= 1 << 18;
15792             else
15793               inst.instruction |= 2 << 18;
15794
15795             neon_dp_fixup (&inst);
15796           }
15797         }
15798       break;
15799
15800     /* Half-precision conversions for Advanced SIMD -- neon.  */
15801     case NS_QD:
15802     case NS_DQ:
15803
15804       if ((rs == NS_DQ)
15805           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15806           {
15807             as_bad (_("operand size must match register width"));
15808             break;
15809           }
15810
15811       if ((rs == NS_QD)
15812           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15813           {
15814             as_bad (_("operand size must match register width"));
15815             break;
15816           }
15817
15818       if (rs == NS_DQ)
15819         inst.instruction = 0x3b60600;
15820       else
15821         inst.instruction = 0x3b60700;
15822
15823       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15824       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15825       inst.instruction |= LOW4 (inst.operands[1].reg);
15826       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15827       neon_dp_fixup (&inst);
15828       break;
15829
15830     default:
15831       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
15832       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15833         do_vfp_nsyn_cvt (rs, flavour);
15834       else
15835         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15836     }
15837 }
15838
15839 static void
15840 do_neon_cvtr (void)
15841 {
15842   do_neon_cvt_1 (neon_cvt_mode_x);
15843 }
15844
15845 static void
15846 do_neon_cvt (void)
15847 {
15848   do_neon_cvt_1 (neon_cvt_mode_z);
15849 }
15850
15851 static void
15852 do_neon_cvta (void)
15853 {
15854   do_neon_cvt_1 (neon_cvt_mode_a);
15855 }
15856
15857 static void
15858 do_neon_cvtn (void)
15859 {
15860   do_neon_cvt_1 (neon_cvt_mode_n);
15861 }
15862
15863 static void
15864 do_neon_cvtp (void)
15865 {
15866   do_neon_cvt_1 (neon_cvt_mode_p);
15867 }
15868
15869 static void
15870 do_neon_cvtm (void)
15871 {
15872   do_neon_cvt_1 (neon_cvt_mode_m);
15873 }
15874
15875 static void
15876 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15877 {
15878   if (is_double)
15879     mark_feature_used (&fpu_vfp_ext_armv8);
15880
15881   encode_arm_vfp_reg (inst.operands[0].reg,
15882                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
15883   encode_arm_vfp_reg (inst.operands[1].reg,
15884                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
15885   inst.instruction |= to ? 0x10000 : 0;
15886   inst.instruction |= t ? 0x80 : 0;
15887   inst.instruction |= is_double ? 0x100 : 0;
15888   do_vfp_cond_or_thumb ();
15889 }
15890
15891 static void
15892 do_neon_cvttb_1 (bfd_boolean t)
15893 {
15894   enum neon_shape rs = neon_select_shape (NS_HF, NS_HD, NS_FH, NS_FF, NS_FD,
15895                                           NS_DF, NS_DH, NS_NULL);
15896
15897   if (rs == NS_NULL)
15898     return;
15899   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
15900     {
15901       inst.error = NULL;
15902       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
15903     }
15904   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
15905     {
15906       inst.error = NULL;
15907       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
15908     }
15909   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
15910     {
15911       /* The VCVTB and VCVTT instructions with D-register operands
15912          don't work for SP only targets.  */
15913       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15914                   _(BAD_FPU));
15915
15916       inst.error = NULL;
15917       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
15918     }
15919   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
15920     {
15921       /* The VCVTB and VCVTT instructions with D-register operands
15922          don't work for SP only targets.  */
15923       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15924                   _(BAD_FPU));
15925
15926       inst.error = NULL;
15927       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
15928     }
15929   else
15930     return;
15931 }
15932
15933 static void
15934 do_neon_cvtb (void)
15935 {
15936   do_neon_cvttb_1 (FALSE);
15937 }
15938
15939
15940 static void
15941 do_neon_cvtt (void)
15942 {
15943   do_neon_cvttb_1 (TRUE);
15944 }
15945
15946 static void
15947 neon_move_immediate (void)
15948 {
15949   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
15950   struct neon_type_el et = neon_check_type (2, rs,
15951     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
15952   unsigned immlo, immhi = 0, immbits;
15953   int op, cmode, float_p;
15954
15955   constraint (et.type == NT_invtype,
15956               _("operand size must be specified for immediate VMOV"));
15957
15958   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
15959   op = (inst.instruction & (1 << 5)) != 0;
15960
15961   immlo = inst.operands[1].imm;
15962   if (inst.operands[1].regisimm)
15963     immhi = inst.operands[1].reg;
15964
15965   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
15966               _("immediate has bits set outside the operand size"));
15967
15968   float_p = inst.operands[1].immisfloat;
15969
15970   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
15971                                         et.size, et.type)) == FAIL)
15972     {
15973       /* Invert relevant bits only.  */
15974       neon_invert_size (&immlo, &immhi, et.size);
15975       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
15976          with one or the other; those cases are caught by
15977          neon_cmode_for_move_imm.  */
15978       op = !op;
15979       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
15980                                             &op, et.size, et.type)) == FAIL)
15981         {
15982           first_error (_("immediate out of range"));
15983           return;
15984         }
15985     }
15986
15987   inst.instruction &= ~(1 << 5);
15988   inst.instruction |= op << 5;
15989
15990   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15991   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15992   inst.instruction |= neon_quad (rs) << 6;
15993   inst.instruction |= cmode << 8;
15994
15995   neon_write_immbits (immbits);
15996 }
15997
15998 static void
15999 do_neon_mvn (void)
16000 {
16001   if (inst.operands[1].isreg)
16002     {
16003       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16004
16005       NEON_ENCODE (INTEGER, inst);
16006       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16007       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16008       inst.instruction |= LOW4 (inst.operands[1].reg);
16009       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16010       inst.instruction |= neon_quad (rs) << 6;
16011     }
16012   else
16013     {
16014       NEON_ENCODE (IMMED, inst);
16015       neon_move_immediate ();
16016     }
16017
16018   neon_dp_fixup (&inst);
16019 }
16020
16021 /* Encode instructions of form:
16022
16023   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
16024   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
16025
16026 static void
16027 neon_mixed_length (struct neon_type_el et, unsigned size)
16028 {
16029   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16030   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16031   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16032   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16033   inst.instruction |= LOW4 (inst.operands[2].reg);
16034   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16035   inst.instruction |= (et.type == NT_unsigned) << 24;
16036   inst.instruction |= neon_logbits (size) << 20;
16037
16038   neon_dp_fixup (&inst);
16039 }
16040
16041 static void
16042 do_neon_dyadic_long (void)
16043 {
16044   /* FIXME: Type checking for lengthening op.  */
16045   struct neon_type_el et = neon_check_type (3, NS_QDD,
16046     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
16047   neon_mixed_length (et, et.size);
16048 }
16049
16050 static void
16051 do_neon_abal (void)
16052 {
16053   struct neon_type_el et = neon_check_type (3, NS_QDD,
16054     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
16055   neon_mixed_length (et, et.size);
16056 }
16057
16058 static void
16059 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
16060 {
16061   if (inst.operands[2].isscalar)
16062     {
16063       struct neon_type_el et = neon_check_type (3, NS_QDS,
16064         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
16065       NEON_ENCODE (SCALAR, inst);
16066       neon_mul_mac (et, et.type == NT_unsigned);
16067     }
16068   else
16069     {
16070       struct neon_type_el et = neon_check_type (3, NS_QDD,
16071         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
16072       NEON_ENCODE (INTEGER, inst);
16073       neon_mixed_length (et, et.size);
16074     }
16075 }
16076
16077 static void
16078 do_neon_mac_maybe_scalar_long (void)
16079 {
16080   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
16081 }
16082
16083 static void
16084 do_neon_dyadic_wide (void)
16085 {
16086   struct neon_type_el et = neon_check_type (3, NS_QQD,
16087     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
16088   neon_mixed_length (et, et.size);
16089 }
16090
16091 static void
16092 do_neon_dyadic_narrow (void)
16093 {
16094   struct neon_type_el et = neon_check_type (3, NS_QDD,
16095     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
16096   /* Operand sign is unimportant, and the U bit is part of the opcode,
16097      so force the operand type to integer.  */
16098   et.type = NT_integer;
16099   neon_mixed_length (et, et.size / 2);
16100 }
16101
16102 static void
16103 do_neon_mul_sat_scalar_long (void)
16104 {
16105   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
16106 }
16107
16108 static void
16109 do_neon_vmull (void)
16110 {
16111   if (inst.operands[2].isscalar)
16112     do_neon_mac_maybe_scalar_long ();
16113   else
16114     {
16115       struct neon_type_el et = neon_check_type (3, NS_QDD,
16116         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
16117
16118       if (et.type == NT_poly)
16119         NEON_ENCODE (POLY, inst);
16120       else
16121         NEON_ENCODE (INTEGER, inst);
16122
16123       /* For polynomial encoding the U bit must be zero, and the size must
16124          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
16125          obviously, as 0b10).  */
16126       if (et.size == 64)
16127         {
16128           /* Check we're on the correct architecture.  */
16129           if (!mark_feature_used (&fpu_crypto_ext_armv8))
16130             inst.error =
16131               _("Instruction form not available on this architecture.");
16132
16133           et.size = 32;
16134         }
16135
16136       neon_mixed_length (et, et.size);
16137     }
16138 }
16139
16140 static void
16141 do_neon_ext (void)
16142 {
16143   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
16144   struct neon_type_el et = neon_check_type (3, rs,
16145     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
16146   unsigned imm = (inst.operands[3].imm * et.size) / 8;
16147
16148   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
16149               _("shift out of range"));
16150   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16151   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16152   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16153   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16154   inst.instruction |= LOW4 (inst.operands[2].reg);
16155   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16156   inst.instruction |= neon_quad (rs) << 6;
16157   inst.instruction |= imm << 8;
16158
16159   neon_dp_fixup (&inst);
16160 }
16161
16162 static void
16163 do_neon_rev (void)
16164 {
16165   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16166   struct neon_type_el et = neon_check_type (2, rs,
16167     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16168   unsigned op = (inst.instruction >> 7) & 3;
16169   /* N (width of reversed regions) is encoded as part of the bitmask. We
16170      extract it here to check the elements to be reversed are smaller.
16171      Otherwise we'd get a reserved instruction.  */
16172   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
16173   gas_assert (elsize != 0);
16174   constraint (et.size >= elsize,
16175               _("elements must be smaller than reversal region"));
16176   neon_two_same (neon_quad (rs), 1, et.size);
16177 }
16178
16179 static void
16180 do_neon_dup (void)
16181 {
16182   if (inst.operands[1].isscalar)
16183     {
16184       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
16185       struct neon_type_el et = neon_check_type (2, rs,
16186         N_EQK, N_8 | N_16 | N_32 | N_KEY);
16187       unsigned sizebits = et.size >> 3;
16188       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
16189       int logsize = neon_logbits (et.size);
16190       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
16191
16192       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
16193         return;
16194
16195       NEON_ENCODE (SCALAR, inst);
16196       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16197       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16198       inst.instruction |= LOW4 (dm);
16199       inst.instruction |= HI1 (dm) << 5;
16200       inst.instruction |= neon_quad (rs) << 6;
16201       inst.instruction |= x << 17;
16202       inst.instruction |= sizebits << 16;
16203
16204       neon_dp_fixup (&inst);
16205     }
16206   else
16207     {
16208       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
16209       struct neon_type_el et = neon_check_type (2, rs,
16210         N_8 | N_16 | N_32 | N_KEY, N_EQK);
16211       /* Duplicate ARM register to lanes of vector.  */
16212       NEON_ENCODE (ARMREG, inst);
16213       switch (et.size)
16214         {
16215         case 8:  inst.instruction |= 0x400000; break;
16216         case 16: inst.instruction |= 0x000020; break;
16217         case 32: inst.instruction |= 0x000000; break;
16218         default: break;
16219         }
16220       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16221       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
16222       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
16223       inst.instruction |= neon_quad (rs) << 21;
16224       /* The encoding for this instruction is identical for the ARM and Thumb
16225          variants, except for the condition field.  */
16226       do_vfp_cond_or_thumb ();
16227     }
16228 }
16229
16230 /* VMOV has particularly many variations. It can be one of:
16231      0. VMOV<c><q> <Qd>, <Qm>
16232      1. VMOV<c><q> <Dd>, <Dm>
16233    (Register operations, which are VORR with Rm = Rn.)
16234      2. VMOV<c><q>.<dt> <Qd>, #<imm>
16235      3. VMOV<c><q>.<dt> <Dd>, #<imm>
16236    (Immediate loads.)
16237      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
16238    (ARM register to scalar.)
16239      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
16240    (Two ARM registers to vector.)
16241      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
16242    (Scalar to ARM register.)
16243      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
16244    (Vector to two ARM registers.)
16245      8. VMOV.F32 <Sd>, <Sm>
16246      9. VMOV.F64 <Dd>, <Dm>
16247    (VFP register moves.)
16248     10. VMOV.F32 <Sd>, #imm
16249     11. VMOV.F64 <Dd>, #imm
16250    (VFP float immediate load.)
16251     12. VMOV <Rd>, <Sm>
16252    (VFP single to ARM reg.)
16253     13. VMOV <Sd>, <Rm>
16254    (ARM reg to VFP single.)
16255     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
16256    (Two ARM regs to two VFP singles.)
16257     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
16258    (Two VFP singles to two ARM regs.)
16259
16260    These cases can be disambiguated using neon_select_shape, except cases 1/9
16261    and 3/11 which depend on the operand type too.
16262
16263    All the encoded bits are hardcoded by this function.
16264
16265    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
16266    Cases 5, 7 may be used with VFPv2 and above.
16267
16268    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
16269    can specify a type where it doesn't make sense to, and is ignored).  */
16270
16271 static void
16272 do_neon_mov (void)
16273 {
16274   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
16275                                           NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR,
16276                                           NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
16277                                           NS_HR, NS_RH, NS_HI, NS_NULL);
16278   struct neon_type_el et;
16279   const char *ldconst = 0;
16280
16281   switch (rs)
16282     {
16283     case NS_DD:  /* case 1/9.  */
16284       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16285       /* It is not an error here if no type is given.  */
16286       inst.error = NULL;
16287       if (et.type == NT_float && et.size == 64)
16288         {
16289           do_vfp_nsyn_opcode ("fcpyd");
16290           break;
16291         }
16292       /* fall through.  */
16293
16294     case NS_QQ:  /* case 0/1.  */
16295       {
16296         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16297           return;
16298         /* The architecture manual I have doesn't explicitly state which
16299            value the U bit should have for register->register moves, but
16300            the equivalent VORR instruction has U = 0, so do that.  */
16301         inst.instruction = 0x0200110;
16302         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16303         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16304         inst.instruction |= LOW4 (inst.operands[1].reg);
16305         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16306         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16307         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16308         inst.instruction |= neon_quad (rs) << 6;
16309
16310         neon_dp_fixup (&inst);
16311       }
16312       break;
16313
16314     case NS_DI:  /* case 3/11.  */
16315       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
16316       inst.error = NULL;
16317       if (et.type == NT_float && et.size == 64)
16318         {
16319           /* case 11 (fconstd).  */
16320           ldconst = "fconstd";
16321           goto encode_fconstd;
16322         }
16323       /* fall through.  */
16324
16325     case NS_QI:  /* case 2/3.  */
16326       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
16327         return;
16328       inst.instruction = 0x0800010;
16329       neon_move_immediate ();
16330       neon_dp_fixup (&inst);
16331       break;
16332
16333     case NS_SR:  /* case 4.  */
16334       {
16335         unsigned bcdebits = 0;
16336         int logsize;
16337         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
16338         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
16339
16340         /* .<size> is optional here, defaulting to .32. */
16341         if (inst.vectype.elems == 0
16342             && inst.operands[0].vectype.type == NT_invtype
16343             && inst.operands[1].vectype.type == NT_invtype)
16344           {
16345             inst.vectype.el[0].type = NT_untyped;
16346             inst.vectype.el[0].size = 32;
16347             inst.vectype.elems = 1;
16348           }
16349
16350         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
16351         logsize = neon_logbits (et.size);
16352
16353         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16354                     _(BAD_FPU));
16355         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16356                     && et.size != 32, _(BAD_FPU));
16357         constraint (et.type == NT_invtype, _("bad type for scalar"));
16358         constraint (x >= 64 / et.size, _("scalar index out of range"));
16359
16360         switch (et.size)
16361           {
16362           case 8:  bcdebits = 0x8; break;
16363           case 16: bcdebits = 0x1; break;
16364           case 32: bcdebits = 0x0; break;
16365           default: ;
16366           }
16367
16368         bcdebits |= x << logsize;
16369
16370         inst.instruction = 0xe000b10;
16371         do_vfp_cond_or_thumb ();
16372         inst.instruction |= LOW4 (dn) << 16;
16373         inst.instruction |= HI1 (dn) << 7;
16374         inst.instruction |= inst.operands[1].reg << 12;
16375         inst.instruction |= (bcdebits & 3) << 5;
16376         inst.instruction |= (bcdebits >> 2) << 21;
16377       }
16378       break;
16379
16380     case NS_DRR:  /* case 5 (fmdrr).  */
16381       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16382                   _(BAD_FPU));
16383
16384       inst.instruction = 0xc400b10;
16385       do_vfp_cond_or_thumb ();
16386       inst.instruction |= LOW4 (inst.operands[0].reg);
16387       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
16388       inst.instruction |= inst.operands[1].reg << 12;
16389       inst.instruction |= inst.operands[2].reg << 16;
16390       break;
16391
16392     case NS_RS:  /* case 6.  */
16393       {
16394         unsigned logsize;
16395         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
16396         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
16397         unsigned abcdebits = 0;
16398
16399         /* .<dt> is optional here, defaulting to .32. */
16400         if (inst.vectype.elems == 0
16401             && inst.operands[0].vectype.type == NT_invtype
16402             && inst.operands[1].vectype.type == NT_invtype)
16403           {
16404             inst.vectype.el[0].type = NT_untyped;
16405             inst.vectype.el[0].size = 32;
16406             inst.vectype.elems = 1;
16407           }
16408
16409         et = neon_check_type (2, NS_NULL,
16410                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
16411         logsize = neon_logbits (et.size);
16412
16413         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16414                     _(BAD_FPU));
16415         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16416                     && et.size != 32, _(BAD_FPU));
16417         constraint (et.type == NT_invtype, _("bad type for scalar"));
16418         constraint (x >= 64 / et.size, _("scalar index out of range"));
16419
16420         switch (et.size)
16421           {
16422           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
16423           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
16424           case 32: abcdebits = 0x00; break;
16425           default: ;
16426           }
16427
16428         abcdebits |= x << logsize;
16429         inst.instruction = 0xe100b10;
16430         do_vfp_cond_or_thumb ();
16431         inst.instruction |= LOW4 (dn) << 16;
16432         inst.instruction |= HI1 (dn) << 7;
16433         inst.instruction |= inst.operands[0].reg << 12;
16434         inst.instruction |= (abcdebits & 3) << 5;
16435         inst.instruction |= (abcdebits >> 2) << 21;
16436       }
16437       break;
16438
16439     case NS_RRD:  /* case 7 (fmrrd).  */
16440       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16441                   _(BAD_FPU));
16442
16443       inst.instruction = 0xc500b10;
16444       do_vfp_cond_or_thumb ();
16445       inst.instruction |= inst.operands[0].reg << 12;
16446       inst.instruction |= inst.operands[1].reg << 16;
16447       inst.instruction |= LOW4 (inst.operands[2].reg);
16448       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16449       break;
16450
16451     case NS_FF:  /* case 8 (fcpys).  */
16452       do_vfp_nsyn_opcode ("fcpys");
16453       break;
16454
16455     case NS_HI:
16456     case NS_FI:  /* case 10 (fconsts).  */
16457       ldconst = "fconsts";
16458       encode_fconstd:
16459       if (is_quarter_float (inst.operands[1].imm))
16460         {
16461           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
16462           do_vfp_nsyn_opcode (ldconst);
16463
16464           /* ARMv8.2 fp16 vmov.f16 instruction.  */
16465           if (rs == NS_HI)
16466             do_scalar_fp16_v82_encode ();
16467         }
16468       else
16469         first_error (_("immediate out of range"));
16470       break;
16471
16472     case NS_RH:
16473     case NS_RF:  /* case 12 (fmrs).  */
16474       do_vfp_nsyn_opcode ("fmrs");
16475       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16476       if (rs == NS_RH)
16477         do_scalar_fp16_v82_encode ();
16478       break;
16479
16480     case NS_HR:
16481     case NS_FR:  /* case 13 (fmsr).  */
16482       do_vfp_nsyn_opcode ("fmsr");
16483       /* ARMv8.2 fp16 vmov.f16 instruction.  */
16484       if (rs == NS_HR)
16485         do_scalar_fp16_v82_encode ();
16486       break;
16487
16488     /* The encoders for the fmrrs and fmsrr instructions expect three operands
16489        (one of which is a list), but we have parsed four.  Do some fiddling to
16490        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
16491        expect.  */
16492     case NS_RRFF:  /* case 14 (fmrrs).  */
16493       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
16494                   _("VFP registers must be adjacent"));
16495       inst.operands[2].imm = 2;
16496       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16497       do_vfp_nsyn_opcode ("fmrrs");
16498       break;
16499
16500     case NS_FFRR:  /* case 15 (fmsrr).  */
16501       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
16502                   _("VFP registers must be adjacent"));
16503       inst.operands[1] = inst.operands[2];
16504       inst.operands[2] = inst.operands[3];
16505       inst.operands[0].imm = 2;
16506       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16507       do_vfp_nsyn_opcode ("fmsrr");
16508       break;
16509
16510     case NS_NULL:
16511       /* neon_select_shape has determined that the instruction
16512          shape is wrong and has already set the error message.  */
16513       break;
16514
16515     default:
16516       abort ();
16517     }
16518 }
16519
16520 static void
16521 do_neon_rshift_round_imm (void)
16522 {
16523   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16524   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16525   int imm = inst.operands[2].imm;
16526
16527   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
16528   if (imm == 0)
16529     {
16530       inst.operands[2].present = 0;
16531       do_neon_mov ();
16532       return;
16533     }
16534
16535   constraint (imm < 1 || (unsigned)imm > et.size,
16536               _("immediate out of range for shift"));
16537   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
16538                   et.size - imm);
16539 }
16540
16541 static void
16542 do_neon_movhf (void)
16543 {
16544   enum neon_shape rs = neon_select_shape (NS_HH, NS_NULL);
16545   constraint (rs != NS_HH, _("invalid suffix"));
16546
16547   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16548               _(BAD_FPU));
16549
16550   do_vfp_sp_monadic ();
16551
16552   inst.is_neon = 1;
16553   inst.instruction |= 0xf0000000;
16554 }
16555
16556 static void
16557 do_neon_movl (void)
16558 {
16559   struct neon_type_el et = neon_check_type (2, NS_QD,
16560     N_EQK | N_DBL, N_SU_32 | N_KEY);
16561   unsigned sizebits = et.size >> 3;
16562   inst.instruction |= sizebits << 19;
16563   neon_two_same (0, et.type == NT_unsigned, -1);
16564 }
16565
16566 static void
16567 do_neon_trn (void)
16568 {
16569   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16570   struct neon_type_el et = neon_check_type (2, rs,
16571     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16572   NEON_ENCODE (INTEGER, inst);
16573   neon_two_same (neon_quad (rs), 1, et.size);
16574 }
16575
16576 static void
16577 do_neon_zip_uzp (void)
16578 {
16579   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16580   struct neon_type_el et = neon_check_type (2, rs,
16581     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16582   if (rs == NS_DD && et.size == 32)
16583     {
16584       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16585       inst.instruction = N_MNEM_vtrn;
16586       do_neon_trn ();
16587       return;
16588     }
16589   neon_two_same (neon_quad (rs), 1, et.size);
16590 }
16591
16592 static void
16593 do_neon_sat_abs_neg (void)
16594 {
16595   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16596   struct neon_type_el et = neon_check_type (2, rs,
16597     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16598   neon_two_same (neon_quad (rs), 1, et.size);
16599 }
16600
16601 static void
16602 do_neon_pair_long (void)
16603 {
16604   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16605   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16606   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16607   inst.instruction |= (et.type == NT_unsigned) << 7;
16608   neon_two_same (neon_quad (rs), 1, et.size);
16609 }
16610
16611 static void
16612 do_neon_recip_est (void)
16613 {
16614   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16615   struct neon_type_el et = neon_check_type (2, rs,
16616     N_EQK | N_FLT, N_F_16_32 | N_U32 | N_KEY);
16617   inst.instruction |= (et.type == NT_float) << 8;
16618   neon_two_same (neon_quad (rs), 1, et.size);
16619 }
16620
16621 static void
16622 do_neon_cls (void)
16623 {
16624   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16625   struct neon_type_el et = neon_check_type (2, rs,
16626     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16627   neon_two_same (neon_quad (rs), 1, et.size);
16628 }
16629
16630 static void
16631 do_neon_clz (void)
16632 {
16633   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16634   struct neon_type_el et = neon_check_type (2, rs,
16635     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16636   neon_two_same (neon_quad (rs), 1, et.size);
16637 }
16638
16639 static void
16640 do_neon_cnt (void)
16641 {
16642   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16643   struct neon_type_el et = neon_check_type (2, rs,
16644     N_EQK | N_INT, N_8 | N_KEY);
16645   neon_two_same (neon_quad (rs), 1, et.size);
16646 }
16647
16648 static void
16649 do_neon_swp (void)
16650 {
16651   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16652   neon_two_same (neon_quad (rs), 1, -1);
16653 }
16654
16655 static void
16656 do_neon_tbl_tbx (void)
16657 {
16658   unsigned listlenbits;
16659   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16660
16661   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16662     {
16663       first_error (_("bad list length for table lookup"));
16664       return;
16665     }
16666
16667   listlenbits = inst.operands[1].imm - 1;
16668   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16669   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16670   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16671   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16672   inst.instruction |= LOW4 (inst.operands[2].reg);
16673   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16674   inst.instruction |= listlenbits << 8;
16675
16676   neon_dp_fixup (&inst);
16677 }
16678
16679 static void
16680 do_neon_ldm_stm (void)
16681 {
16682   /* P, U and L bits are part of bitmask.  */
16683   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16684   unsigned offsetbits = inst.operands[1].imm * 2;
16685
16686   if (inst.operands[1].issingle)
16687     {
16688       do_vfp_nsyn_ldm_stm (is_dbmode);
16689       return;
16690     }
16691
16692   constraint (is_dbmode && !inst.operands[0].writeback,
16693               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16694
16695   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16696               _("register list must contain at least 1 and at most 16 "
16697                 "registers"));
16698
16699   inst.instruction |= inst.operands[0].reg << 16;
16700   inst.instruction |= inst.operands[0].writeback << 21;
16701   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16702   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16703
16704   inst.instruction |= offsetbits;
16705
16706   do_vfp_cond_or_thumb ();
16707 }
16708
16709 static void
16710 do_neon_ldr_str (void)
16711 {
16712   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16713
16714   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16715      And is UNPREDICTABLE in thumb mode.  */
16716   if (!is_ldr
16717       && inst.operands[1].reg == REG_PC
16718       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16719     {
16720       if (thumb_mode)
16721         inst.error = _("Use of PC here is UNPREDICTABLE");
16722       else if (warn_on_deprecated)
16723         as_tsktsk (_("Use of PC here is deprecated"));
16724     }
16725
16726   if (inst.operands[0].issingle)
16727     {
16728       if (is_ldr)
16729         do_vfp_nsyn_opcode ("flds");
16730       else
16731         do_vfp_nsyn_opcode ("fsts");
16732
16733       /* ARMv8.2 vldr.16/vstr.16 instruction.  */
16734       if (inst.vectype.el[0].size == 16)
16735         do_scalar_fp16_v82_encode ();
16736     }
16737   else
16738     {
16739       if (is_ldr)
16740         do_vfp_nsyn_opcode ("fldd");
16741       else
16742         do_vfp_nsyn_opcode ("fstd");
16743     }
16744 }
16745
16746 /* "interleave" version also handles non-interleaving register VLD1/VST1
16747    instructions.  */
16748
16749 static void
16750 do_neon_ld_st_interleave (void)
16751 {
16752   struct neon_type_el et = neon_check_type (1, NS_NULL,
16753                                             N_8 | N_16 | N_32 | N_64);
16754   unsigned alignbits = 0;
16755   unsigned idx;
16756   /* The bits in this table go:
16757      0: register stride of one (0) or two (1)
16758      1,2: register list length, minus one (1, 2, 3, 4).
16759      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
16760      We use -1 for invalid entries.  */
16761   const int typetable[] =
16762     {
16763       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
16764        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
16765        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
16766        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
16767     };
16768   int typebits;
16769
16770   if (et.type == NT_invtype)
16771     return;
16772
16773   if (inst.operands[1].immisalign)
16774     switch (inst.operands[1].imm >> 8)
16775       {
16776       case 64: alignbits = 1; break;
16777       case 128:
16778         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
16779             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16780           goto bad_alignment;
16781         alignbits = 2;
16782         break;
16783       case 256:
16784         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16785           goto bad_alignment;
16786         alignbits = 3;
16787         break;
16788       default:
16789       bad_alignment:
16790         first_error (_("bad alignment"));
16791         return;
16792       }
16793
16794   inst.instruction |= alignbits << 4;
16795   inst.instruction |= neon_logbits (et.size) << 6;
16796
16797   /* Bits [4:6] of the immediate in a list specifier encode register stride
16798      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
16799      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
16800      up the right value for "type" in a table based on this value and the given
16801      list style, then stick it back.  */
16802   idx = ((inst.operands[0].imm >> 4) & 7)
16803         | (((inst.instruction >> 8) & 3) << 3);
16804
16805   typebits = typetable[idx];
16806
16807   constraint (typebits == -1, _("bad list type for instruction"));
16808   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
16809               _("bad element type for instruction"));
16810
16811   inst.instruction &= ~0xf00;
16812   inst.instruction |= typebits << 8;
16813 }
16814
16815 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
16816    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
16817    otherwise. The variable arguments are a list of pairs of legal (size, align)
16818    values, terminated with -1.  */
16819
16820 static int
16821 neon_alignment_bit (int size, int align, int *do_alignment, ...)
16822 {
16823   va_list ap;
16824   int result = FAIL, thissize, thisalign;
16825
16826   if (!inst.operands[1].immisalign)
16827     {
16828       *do_alignment = 0;
16829       return SUCCESS;
16830     }
16831
16832   va_start (ap, do_alignment);
16833
16834   do
16835     {
16836       thissize = va_arg (ap, int);
16837       if (thissize == -1)
16838         break;
16839       thisalign = va_arg (ap, int);
16840
16841       if (size == thissize && align == thisalign)
16842         result = SUCCESS;
16843     }
16844   while (result != SUCCESS);
16845
16846   va_end (ap);
16847
16848   if (result == SUCCESS)
16849     *do_alignment = 1;
16850   else
16851     first_error (_("unsupported alignment for instruction"));
16852
16853   return result;
16854 }
16855
16856 static void
16857 do_neon_ld_st_lane (void)
16858 {
16859   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16860   int align_good, do_alignment = 0;
16861   int logsize = neon_logbits (et.size);
16862   int align = inst.operands[1].imm >> 8;
16863   int n = (inst.instruction >> 8) & 3;
16864   int max_el = 64 / et.size;
16865
16866   if (et.type == NT_invtype)
16867     return;
16868
16869   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
16870               _("bad list length"));
16871   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
16872               _("scalar index out of range"));
16873   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
16874               && et.size == 8,
16875               _("stride of 2 unavailable when element size is 8"));
16876
16877   switch (n)
16878     {
16879     case 0:  /* VLD1 / VST1.  */
16880       align_good = neon_alignment_bit (et.size, align, &do_alignment, 16, 16,
16881                                        32, 32, -1);
16882       if (align_good == FAIL)
16883         return;
16884       if (do_alignment)
16885         {
16886           unsigned alignbits = 0;
16887           switch (et.size)
16888             {
16889             case 16: alignbits = 0x1; break;
16890             case 32: alignbits = 0x3; break;
16891             default: ;
16892             }
16893           inst.instruction |= alignbits << 4;
16894         }
16895       break;
16896
16897     case 1:  /* VLD2 / VST2.  */
16898       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 16,
16899                       16, 32, 32, 64, -1);
16900       if (align_good == FAIL)
16901         return;
16902       if (do_alignment)
16903         inst.instruction |= 1 << 4;
16904       break;
16905
16906     case 2:  /* VLD3 / VST3.  */
16907       constraint (inst.operands[1].immisalign,
16908                   _("can't use alignment with this instruction"));
16909       break;
16910
16911     case 3:  /* VLD4 / VST4.  */
16912       align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
16913                                        16, 64, 32, 64, 32, 128, -1);
16914       if (align_good == FAIL)
16915         return;
16916       if (do_alignment)
16917         {
16918           unsigned alignbits = 0;
16919           switch (et.size)
16920             {
16921             case 8:  alignbits = 0x1; break;
16922             case 16: alignbits = 0x1; break;
16923             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
16924             default: ;
16925             }
16926           inst.instruction |= alignbits << 4;
16927         }
16928       break;
16929
16930     default: ;
16931     }
16932
16933   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
16934   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16935     inst.instruction |= 1 << (4 + logsize);
16936
16937   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
16938   inst.instruction |= logsize << 10;
16939 }
16940
16941 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
16942
16943 static void
16944 do_neon_ld_dup (void)
16945 {
16946   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16947   int align_good, do_alignment = 0;
16948
16949   if (et.type == NT_invtype)
16950     return;
16951
16952   switch ((inst.instruction >> 8) & 3)
16953     {
16954     case 0:  /* VLD1.  */
16955       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
16956       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16957                                        &do_alignment, 16, 16, 32, 32, -1);
16958       if (align_good == FAIL)
16959         return;
16960       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
16961         {
16962         case 1: break;
16963         case 2: inst.instruction |= 1 << 5; break;
16964         default: first_error (_("bad list length")); return;
16965         }
16966       inst.instruction |= neon_logbits (et.size) << 6;
16967       break;
16968
16969     case 1:  /* VLD2.  */
16970       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16971                                        &do_alignment, 8, 16, 16, 32, 32, 64,
16972                                        -1);
16973       if (align_good == FAIL)
16974         return;
16975       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
16976                   _("bad list length"));
16977       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16978         inst.instruction |= 1 << 5;
16979       inst.instruction |= neon_logbits (et.size) << 6;
16980       break;
16981
16982     case 2:  /* VLD3.  */
16983       constraint (inst.operands[1].immisalign,
16984                   _("can't use alignment with this instruction"));
16985       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
16986                   _("bad list length"));
16987       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16988         inst.instruction |= 1 << 5;
16989       inst.instruction |= neon_logbits (et.size) << 6;
16990       break;
16991
16992     case 3:  /* VLD4.  */
16993       {
16994         int align = inst.operands[1].imm >> 8;
16995         align_good = neon_alignment_bit (et.size, align, &do_alignment, 8, 32,
16996                                          16, 64, 32, 64, 32, 128, -1);
16997         if (align_good == FAIL)
16998           return;
16999         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
17000                     _("bad list length"));
17001         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
17002           inst.instruction |= 1 << 5;
17003         if (et.size == 32 && align == 128)
17004           inst.instruction |= 0x3 << 6;
17005         else
17006           inst.instruction |= neon_logbits (et.size) << 6;
17007       }
17008       break;
17009
17010     default: ;
17011     }
17012
17013   inst.instruction |= do_alignment << 4;
17014 }
17015
17016 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
17017    apart from bits [11:4].  */
17018
17019 static void
17020 do_neon_ldx_stx (void)
17021 {
17022   if (inst.operands[1].isreg)
17023     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
17024
17025   switch (NEON_LANE (inst.operands[0].imm))
17026     {
17027     case NEON_INTERLEAVE_LANES:
17028       NEON_ENCODE (INTERLV, inst);
17029       do_neon_ld_st_interleave ();
17030       break;
17031
17032     case NEON_ALL_LANES:
17033       NEON_ENCODE (DUP, inst);
17034       if (inst.instruction == N_INV)
17035         {
17036           first_error ("only loads support such operands");
17037           break;
17038         }
17039       do_neon_ld_dup ();
17040       break;
17041
17042     default:
17043       NEON_ENCODE (LANE, inst);
17044       do_neon_ld_st_lane ();
17045     }
17046
17047   /* L bit comes from bit mask.  */
17048   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17049   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17050   inst.instruction |= inst.operands[1].reg << 16;
17051
17052   if (inst.operands[1].postind)
17053     {
17054       int postreg = inst.operands[1].imm & 0xf;
17055       constraint (!inst.operands[1].immisreg,
17056                   _("post-index must be a register"));
17057       constraint (postreg == 0xd || postreg == 0xf,
17058                   _("bad register for post-index"));
17059       inst.instruction |= postreg;
17060     }
17061   else
17062     {
17063       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
17064       constraint (inst.reloc.exp.X_op != O_constant
17065                   || inst.reloc.exp.X_add_number != 0,
17066                   BAD_ADDR_MODE);
17067
17068       if (inst.operands[1].writeback)
17069         {
17070           inst.instruction |= 0xd;
17071         }
17072       else
17073         inst.instruction |= 0xf;
17074     }
17075
17076   if (thumb_mode)
17077     inst.instruction |= 0xf9000000;
17078   else
17079     inst.instruction |= 0xf4000000;
17080 }
17081
17082 /* FP v8.  */
17083 static void
17084 do_vfp_nsyn_fpv8 (enum neon_shape rs)
17085 {
17086   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17087      D register operands.  */
17088   if (neon_shape_class[rs] == SC_DOUBLE)
17089     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17090                 _(BAD_FPU));
17091
17092   NEON_ENCODE (FPV8, inst);
17093
17094   if (rs == NS_FFF || rs == NS_HHH)
17095     {
17096       do_vfp_sp_dyadic ();
17097
17098       /* ARMv8.2 fp16 instruction.  */
17099       if (rs == NS_HHH)
17100         do_scalar_fp16_v82_encode ();
17101     }
17102   else
17103     do_vfp_dp_rd_rn_rm ();
17104
17105   if (rs == NS_DDD)
17106     inst.instruction |= 0x100;
17107
17108   inst.instruction |= 0xf0000000;
17109 }
17110
17111 static void
17112 do_vsel (void)
17113 {
17114   set_it_insn_type (OUTSIDE_IT_INSN);
17115
17116   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
17117     first_error (_("invalid instruction shape"));
17118 }
17119
17120 static void
17121 do_vmaxnm (void)
17122 {
17123   set_it_insn_type (OUTSIDE_IT_INSN);
17124
17125   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
17126     return;
17127
17128   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17129     return;
17130
17131   neon_dyadic_misc (NT_untyped, N_F_16_32, 0);
17132 }
17133
17134 static void
17135 do_vrint_1 (enum neon_cvt_mode mode)
17136 {
17137   enum neon_shape rs = neon_select_shape (NS_HH, NS_FF, NS_DD, NS_QQ, NS_NULL);
17138   struct neon_type_el et;
17139
17140   if (rs == NS_NULL)
17141     return;
17142
17143   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
17144      D register operands.  */
17145   if (neon_shape_class[rs] == SC_DOUBLE)
17146     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17147                 _(BAD_FPU));
17148
17149   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F_ALL | N_KEY
17150                         | N_VFP);
17151   if (et.type != NT_invtype)
17152     {
17153       /* VFP encodings.  */
17154       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
17155           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
17156         set_it_insn_type (OUTSIDE_IT_INSN);
17157
17158       NEON_ENCODE (FPV8, inst);
17159       if (rs == NS_FF || rs == NS_HH)
17160         do_vfp_sp_monadic ();
17161       else
17162         do_vfp_dp_rd_rm ();
17163
17164       switch (mode)
17165         {
17166         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
17167         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
17168         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
17169         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
17170         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
17171         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
17172         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
17173         default: abort ();
17174         }
17175
17176       inst.instruction |= (rs == NS_DD) << 8;
17177       do_vfp_cond_or_thumb ();
17178
17179       /* ARMv8.2 fp16 vrint instruction.  */
17180       if (rs == NS_HH)
17181       do_scalar_fp16_v82_encode ();
17182     }
17183   else
17184     {
17185       /* Neon encodings (or something broken...).  */
17186       inst.error = NULL;
17187       et = neon_check_type (2, rs, N_EQK, N_F_16_32 | N_KEY);
17188
17189       if (et.type == NT_invtype)
17190         return;
17191
17192       set_it_insn_type (OUTSIDE_IT_INSN);
17193       NEON_ENCODE (FLOAT, inst);
17194
17195       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
17196         return;
17197
17198       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17199       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17200       inst.instruction |= LOW4 (inst.operands[1].reg);
17201       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17202       inst.instruction |= neon_quad (rs) << 6;
17203       /* Mask off the original size bits and reencode them.  */
17204       inst.instruction = ((inst.instruction & 0xfff3ffff)
17205                           | neon_logbits (et.size) << 18);
17206
17207       switch (mode)
17208         {
17209         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
17210         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
17211         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
17212         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
17213         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
17214         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
17215         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
17216         default: abort ();
17217         }
17218
17219       if (thumb_mode)
17220         inst.instruction |= 0xfc000000;
17221       else
17222         inst.instruction |= 0xf0000000;
17223     }
17224 }
17225
17226 static void
17227 do_vrintx (void)
17228 {
17229   do_vrint_1 (neon_cvt_mode_x);
17230 }
17231
17232 static void
17233 do_vrintz (void)
17234 {
17235   do_vrint_1 (neon_cvt_mode_z);
17236 }
17237
17238 static void
17239 do_vrintr (void)
17240 {
17241   do_vrint_1 (neon_cvt_mode_r);
17242 }
17243
17244 static void
17245 do_vrinta (void)
17246 {
17247   do_vrint_1 (neon_cvt_mode_a);
17248 }
17249
17250 static void
17251 do_vrintn (void)
17252 {
17253   do_vrint_1 (neon_cvt_mode_n);
17254 }
17255
17256 static void
17257 do_vrintp (void)
17258 {
17259   do_vrint_1 (neon_cvt_mode_p);
17260 }
17261
17262 static void
17263 do_vrintm (void)
17264 {
17265   do_vrint_1 (neon_cvt_mode_m);
17266 }
17267
17268 static unsigned
17269 neon_scalar_for_vcmla (unsigned opnd, unsigned elsize)
17270 {
17271   unsigned regno = NEON_SCALAR_REG (opnd);
17272   unsigned elno = NEON_SCALAR_INDEX (opnd);
17273
17274   if (elsize == 16 && elno < 2 && regno < 16)
17275     return regno | (elno << 4);
17276   else if (elsize == 32 && elno == 0)
17277     return regno;
17278
17279   first_error (_("scalar out of range"));
17280   return 0;
17281 }
17282
17283 static void
17284 do_vcmla (void)
17285 {
17286   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17287               _(BAD_FPU));
17288   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17289   unsigned rot = inst.reloc.exp.X_add_number;
17290   constraint (rot != 0 && rot != 90 && rot != 180 && rot != 270,
17291               _("immediate out of range"));
17292   rot /= 90;
17293   if (inst.operands[2].isscalar)
17294     {
17295       enum neon_shape rs = neon_select_shape (NS_DDSI, NS_QQSI, NS_NULL);
17296       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17297                                        N_KEY | N_F16 | N_F32).size;
17298       unsigned m = neon_scalar_for_vcmla (inst.operands[2].reg, size);
17299       inst.is_neon = 1;
17300       inst.instruction = 0xfe000800;
17301       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17302       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17303       inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
17304       inst.instruction |= HI1 (inst.operands[1].reg) << 7;
17305       inst.instruction |= LOW4 (m);
17306       inst.instruction |= HI1 (m) << 5;
17307       inst.instruction |= neon_quad (rs) << 6;
17308       inst.instruction |= rot << 20;
17309       inst.instruction |= (size == 32) << 23;
17310     }
17311   else
17312     {
17313       enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17314       unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17315                                        N_KEY | N_F16 | N_F32).size;
17316       neon_three_same (neon_quad (rs), 0, -1);
17317       inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17318       inst.instruction |= 0xfc200800;
17319       inst.instruction |= rot << 23;
17320       inst.instruction |= (size == 32) << 20;
17321     }
17322 }
17323
17324 static void
17325 do_vcadd (void)
17326 {
17327   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_armv8),
17328               _(BAD_FPU));
17329   constraint (inst.reloc.exp.X_op != O_constant, _("expression too complex"));
17330   unsigned rot = inst.reloc.exp.X_add_number;
17331   constraint (rot != 90 && rot != 270, _("immediate out of range"));
17332   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
17333   unsigned size = neon_check_type (3, rs, N_EQK, N_EQK,
17334                                    N_KEY | N_F16 | N_F32).size;
17335   neon_three_same (neon_quad (rs), 0, -1);
17336   inst.instruction &= 0x00ffffff; /* Undo neon_dp_fixup.  */
17337   inst.instruction |= 0xfc800800;
17338   inst.instruction |= (rot == 270) << 24;
17339   inst.instruction |= (size == 32) << 20;
17340 }
17341
17342 /* Crypto v1 instructions.  */
17343 static void
17344 do_crypto_2op_1 (unsigned elttype, int op)
17345 {
17346   set_it_insn_type (OUTSIDE_IT_INSN);
17347
17348   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
17349       == NT_invtype)
17350     return;
17351
17352   inst.error = NULL;
17353
17354   NEON_ENCODE (INTEGER, inst);
17355   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
17356   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
17357   inst.instruction |= LOW4 (inst.operands[1].reg);
17358   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
17359   if (op != -1)
17360     inst.instruction |= op << 6;
17361
17362   if (thumb_mode)
17363     inst.instruction |= 0xfc000000;
17364   else
17365     inst.instruction |= 0xf0000000;
17366 }
17367
17368 static void
17369 do_crypto_3op_1 (int u, int op)
17370 {
17371   set_it_insn_type (OUTSIDE_IT_INSN);
17372
17373   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
17374                        N_32 | N_UNT | N_KEY).type == NT_invtype)
17375     return;
17376
17377   inst.error = NULL;
17378
17379   NEON_ENCODE (INTEGER, inst);
17380   neon_three_same (1, u, 8 << op);
17381 }
17382
17383 static void
17384 do_aese (void)
17385 {
17386   do_crypto_2op_1 (N_8, 0);
17387 }
17388
17389 static void
17390 do_aesd (void)
17391 {
17392   do_crypto_2op_1 (N_8, 1);
17393 }
17394
17395 static void
17396 do_aesmc (void)
17397 {
17398   do_crypto_2op_1 (N_8, 2);
17399 }
17400
17401 static void
17402 do_aesimc (void)
17403 {
17404   do_crypto_2op_1 (N_8, 3);
17405 }
17406
17407 static void
17408 do_sha1c (void)
17409 {
17410   do_crypto_3op_1 (0, 0);
17411 }
17412
17413 static void
17414 do_sha1p (void)
17415 {
17416   do_crypto_3op_1 (0, 1);
17417 }
17418
17419 static void
17420 do_sha1m (void)
17421 {
17422   do_crypto_3op_1 (0, 2);
17423 }
17424
17425 static void
17426 do_sha1su0 (void)
17427 {
17428   do_crypto_3op_1 (0, 3);
17429 }
17430
17431 static void
17432 do_sha256h (void)
17433 {
17434   do_crypto_3op_1 (1, 0);
17435 }
17436
17437 static void
17438 do_sha256h2 (void)
17439 {
17440   do_crypto_3op_1 (1, 1);
17441 }
17442
17443 static void
17444 do_sha256su1 (void)
17445 {
17446   do_crypto_3op_1 (1, 2);
17447 }
17448
17449 static void
17450 do_sha1h (void)
17451 {
17452   do_crypto_2op_1 (N_32, -1);
17453 }
17454
17455 static void
17456 do_sha1su1 (void)
17457 {
17458   do_crypto_2op_1 (N_32, 0);
17459 }
17460
17461 static void
17462 do_sha256su0 (void)
17463 {
17464   do_crypto_2op_1 (N_32, 1);
17465 }
17466
17467 static void
17468 do_crc32_1 (unsigned int poly, unsigned int sz)
17469 {
17470   unsigned int Rd = inst.operands[0].reg;
17471   unsigned int Rn = inst.operands[1].reg;
17472   unsigned int Rm = inst.operands[2].reg;
17473
17474   set_it_insn_type (OUTSIDE_IT_INSN);
17475   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
17476   inst.instruction |= LOW4 (Rn) << 16;
17477   inst.instruction |= LOW4 (Rm);
17478   inst.instruction |= sz << (thumb_mode ? 4 : 21);
17479   inst.instruction |= poly << (thumb_mode ? 20 : 9);
17480
17481   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
17482     as_warn (UNPRED_REG ("r15"));
17483   if (thumb_mode && (Rd == REG_SP || Rn == REG_SP || Rm == REG_SP))
17484     as_warn (UNPRED_REG ("r13"));
17485 }
17486
17487 static void
17488 do_crc32b (void)
17489 {
17490   do_crc32_1 (0, 0);
17491 }
17492
17493 static void
17494 do_crc32h (void)
17495 {
17496   do_crc32_1 (0, 1);
17497 }
17498
17499 static void
17500 do_crc32w (void)
17501 {
17502   do_crc32_1 (0, 2);
17503 }
17504
17505 static void
17506 do_crc32cb (void)
17507 {
17508   do_crc32_1 (1, 0);
17509 }
17510
17511 static void
17512 do_crc32ch (void)
17513 {
17514   do_crc32_1 (1, 1);
17515 }
17516
17517 static void
17518 do_crc32cw (void)
17519 {
17520   do_crc32_1 (1, 2);
17521 }
17522
17523 static void
17524 do_vjcvt (void)
17525 {
17526   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
17527               _(BAD_FPU));
17528   neon_check_type (2, NS_FD, N_S32, N_F64);
17529   do_vfp_sp_dp_cvt ();
17530   do_vfp_cond_or_thumb ();
17531 }
17532
17533 \f
17534 /* Overall per-instruction processing.  */
17535
17536 /* We need to be able to fix up arbitrary expressions in some statements.
17537    This is so that we can handle symbols that are an arbitrary distance from
17538    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
17539    which returns part of an address in a form which will be valid for
17540    a data instruction.  We do this by pushing the expression into a symbol
17541    in the expr_section, and creating a fix for that.  */
17542
17543 static void
17544 fix_new_arm (fragS *       frag,
17545              int           where,
17546              short int     size,
17547              expressionS * exp,
17548              int           pc_rel,
17549              int           reloc)
17550 {
17551   fixS *           new_fix;
17552
17553   switch (exp->X_op)
17554     {
17555     case O_constant:
17556       if (pc_rel)
17557         {
17558           /* Create an absolute valued symbol, so we have something to
17559              refer to in the object file.  Unfortunately for us, gas's
17560              generic expression parsing will already have folded out
17561              any use of .set foo/.type foo %function that may have
17562              been used to set type information of the target location,
17563              that's being specified symbolically.  We have to presume
17564              the user knows what they are doing.  */
17565           char name[16 + 8];
17566           symbolS *symbol;
17567
17568           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
17569
17570           symbol = symbol_find_or_make (name);
17571           S_SET_SEGMENT (symbol, absolute_section);
17572           symbol_set_frag (symbol, &zero_address_frag);
17573           S_SET_VALUE (symbol, exp->X_add_number);
17574           exp->X_op = O_symbol;
17575           exp->X_add_symbol = symbol;
17576           exp->X_add_number = 0;
17577         }
17578       /* FALLTHROUGH */
17579     case O_symbol:
17580     case O_add:
17581     case O_subtract:
17582       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
17583                              (enum bfd_reloc_code_real) reloc);
17584       break;
17585
17586     default:
17587       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
17588                                   pc_rel, (enum bfd_reloc_code_real) reloc);
17589       break;
17590     }
17591
17592   /* Mark whether the fix is to a THUMB instruction, or an ARM
17593      instruction.  */
17594   new_fix->tc_fix_data = thumb_mode;
17595 }
17596
17597 /* Create a frg for an instruction requiring relaxation.  */
17598 static void
17599 output_relax_insn (void)
17600 {
17601   char * to;
17602   symbolS *sym;
17603   int offset;
17604
17605   /* The size of the instruction is unknown, so tie the debug info to the
17606      start of the instruction.  */
17607   dwarf2_emit_insn (0);
17608
17609   switch (inst.reloc.exp.X_op)
17610     {
17611     case O_symbol:
17612       sym = inst.reloc.exp.X_add_symbol;
17613       offset = inst.reloc.exp.X_add_number;
17614       break;
17615     case O_constant:
17616       sym = NULL;
17617       offset = inst.reloc.exp.X_add_number;
17618       break;
17619     default:
17620       sym = make_expr_symbol (&inst.reloc.exp);
17621       offset = 0;
17622       break;
17623   }
17624   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
17625                  inst.relax, sym, offset, NULL/*offset, opcode*/);
17626   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
17627 }
17628
17629 /* Write a 32-bit thumb instruction to buf.  */
17630 static void
17631 put_thumb32_insn (char * buf, unsigned long insn)
17632 {
17633   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
17634   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
17635 }
17636
17637 static void
17638 output_inst (const char * str)
17639 {
17640   char * to = NULL;
17641
17642   if (inst.error)
17643     {
17644       as_bad ("%s -- `%s'", inst.error, str);
17645       return;
17646     }
17647   if (inst.relax)
17648     {
17649       output_relax_insn ();
17650       return;
17651     }
17652   if (inst.size == 0)
17653     return;
17654
17655   to = frag_more (inst.size);
17656   /* PR 9814: Record the thumb mode into the current frag so that we know
17657      what type of NOP padding to use, if necessary.  We override any previous
17658      setting so that if the mode has changed then the NOPS that we use will
17659      match the encoding of the last instruction in the frag.  */
17660   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
17661
17662   if (thumb_mode && (inst.size > THUMB_SIZE))
17663     {
17664       gas_assert (inst.size == (2 * THUMB_SIZE));
17665       put_thumb32_insn (to, inst.instruction);
17666     }
17667   else if (inst.size > INSN_SIZE)
17668     {
17669       gas_assert (inst.size == (2 * INSN_SIZE));
17670       md_number_to_chars (to, inst.instruction, INSN_SIZE);
17671       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
17672     }
17673   else
17674     md_number_to_chars (to, inst.instruction, inst.size);
17675
17676   if (inst.reloc.type != BFD_RELOC_UNUSED)
17677     fix_new_arm (frag_now, to - frag_now->fr_literal,
17678                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
17679                  inst.reloc.type);
17680
17681   dwarf2_emit_insn (inst.size);
17682 }
17683
17684 static char *
17685 output_it_inst (int cond, int mask, char * to)
17686 {
17687   unsigned long instruction = 0xbf00;
17688
17689   mask &= 0xf;
17690   instruction |= mask;
17691   instruction |= cond << 4;
17692
17693   if (to == NULL)
17694     {
17695       to = frag_more (2);
17696 #ifdef OBJ_ELF
17697       dwarf2_emit_insn (2);
17698 #endif
17699     }
17700
17701   md_number_to_chars (to, instruction, 2);
17702
17703   return to;
17704 }
17705
17706 /* Tag values used in struct asm_opcode's tag field.  */
17707 enum opcode_tag
17708 {
17709   OT_unconditional,     /* Instruction cannot be conditionalized.
17710                            The ARM condition field is still 0xE.  */
17711   OT_unconditionalF,    /* Instruction cannot be conditionalized
17712                            and carries 0xF in its ARM condition field.  */
17713   OT_csuffix,           /* Instruction takes a conditional suffix.  */
17714   OT_csuffixF,          /* Some forms of the instruction take a conditional
17715                            suffix, others place 0xF where the condition field
17716                            would be.  */
17717   OT_cinfix3,           /* Instruction takes a conditional infix,
17718                            beginning at character index 3.  (In
17719                            unified mode, it becomes a suffix.)  */
17720   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
17721                             tsts, cmps, cmns, and teqs. */
17722   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
17723                            character index 3, even in unified mode.  Used for
17724                            legacy instructions where suffix and infix forms
17725                            may be ambiguous.  */
17726   OT_csuf_or_in3,       /* Instruction takes either a conditional
17727                            suffix or an infix at character index 3.  */
17728   OT_odd_infix_unc,     /* This is the unconditional variant of an
17729                            instruction that takes a conditional infix
17730                            at an unusual position.  In unified mode,
17731                            this variant will accept a suffix.  */
17732   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
17733                            are the conditional variants of instructions that
17734                            take conditional infixes in unusual positions.
17735                            The infix appears at character index
17736                            (tag - OT_odd_infix_0).  These are not accepted
17737                            in unified mode.  */
17738 };
17739
17740 /* Subroutine of md_assemble, responsible for looking up the primary
17741    opcode from the mnemonic the user wrote.  STR points to the
17742    beginning of the mnemonic.
17743
17744    This is not simply a hash table lookup, because of conditional
17745    variants.  Most instructions have conditional variants, which are
17746    expressed with a _conditional affix_ to the mnemonic.  If we were
17747    to encode each conditional variant as a literal string in the opcode
17748    table, it would have approximately 20,000 entries.
17749
17750    Most mnemonics take this affix as a suffix, and in unified syntax,
17751    'most' is upgraded to 'all'.  However, in the divided syntax, some
17752    instructions take the affix as an infix, notably the s-variants of
17753    the arithmetic instructions.  Of those instructions, all but six
17754    have the infix appear after the third character of the mnemonic.
17755
17756    Accordingly, the algorithm for looking up primary opcodes given
17757    an identifier is:
17758
17759    1. Look up the identifier in the opcode table.
17760       If we find a match, go to step U.
17761
17762    2. Look up the last two characters of the identifier in the
17763       conditions table.  If we find a match, look up the first N-2
17764       characters of the identifier in the opcode table.  If we
17765       find a match, go to step CE.
17766
17767    3. Look up the fourth and fifth characters of the identifier in
17768       the conditions table.  If we find a match, extract those
17769       characters from the identifier, and look up the remaining
17770       characters in the opcode table.  If we find a match, go
17771       to step CM.
17772
17773    4. Fail.
17774
17775    U. Examine the tag field of the opcode structure, in case this is
17776       one of the six instructions with its conditional infix in an
17777       unusual place.  If it is, the tag tells us where to find the
17778       infix; look it up in the conditions table and set inst.cond
17779       accordingly.  Otherwise, this is an unconditional instruction.
17780       Again set inst.cond accordingly.  Return the opcode structure.
17781
17782   CE. Examine the tag field to make sure this is an instruction that
17783       should receive a conditional suffix.  If it is not, fail.
17784       Otherwise, set inst.cond from the suffix we already looked up,
17785       and return the opcode structure.
17786
17787   CM. Examine the tag field to make sure this is an instruction that
17788       should receive a conditional infix after the third character.
17789       If it is not, fail.  Otherwise, undo the edits to the current
17790       line of input and proceed as for case CE.  */
17791
17792 static const struct asm_opcode *
17793 opcode_lookup (char **str)
17794 {
17795   char *end, *base;
17796   char *affix;
17797   const struct asm_opcode *opcode;
17798   const struct asm_cond *cond;
17799   char save[2];
17800
17801   /* Scan up to the end of the mnemonic, which must end in white space,
17802      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
17803   for (base = end = *str; *end != '\0'; end++)
17804     if (*end == ' ' || *end == '.')
17805       break;
17806
17807   if (end == base)
17808     return NULL;
17809
17810   /* Handle a possible width suffix and/or Neon type suffix.  */
17811   if (end[0] == '.')
17812     {
17813       int offset = 2;
17814
17815       /* The .w and .n suffixes are only valid if the unified syntax is in
17816          use.  */
17817       if (unified_syntax && end[1] == 'w')
17818         inst.size_req = 4;
17819       else if (unified_syntax && end[1] == 'n')
17820         inst.size_req = 2;
17821       else
17822         offset = 0;
17823
17824       inst.vectype.elems = 0;
17825
17826       *str = end + offset;
17827
17828       if (end[offset] == '.')
17829         {
17830           /* See if we have a Neon type suffix (possible in either unified or
17831              non-unified ARM syntax mode).  */
17832           if (parse_neon_type (&inst.vectype, str) == FAIL)
17833             return NULL;
17834         }
17835       else if (end[offset] != '\0' && end[offset] != ' ')
17836         return NULL;
17837     }
17838   else
17839     *str = end;
17840
17841   /* Look for unaffixed or special-case affixed mnemonic.  */
17842   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17843                                                     end - base);
17844   if (opcode)
17845     {
17846       /* step U */
17847       if (opcode->tag < OT_odd_infix_0)
17848         {
17849           inst.cond = COND_ALWAYS;
17850           return opcode;
17851         }
17852
17853       if (warn_on_deprecated && unified_syntax)
17854         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17855       affix = base + (opcode->tag - OT_odd_infix_0);
17856       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17857       gas_assert (cond);
17858
17859       inst.cond = cond->value;
17860       return opcode;
17861     }
17862
17863   /* Cannot have a conditional suffix on a mnemonic of less than two
17864      characters.  */
17865   if (end - base < 3)
17866     return NULL;
17867
17868   /* Look for suffixed mnemonic.  */
17869   affix = end - 2;
17870   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17871   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17872                                                     affix - base);
17873   if (opcode && cond)
17874     {
17875       /* step CE */
17876       switch (opcode->tag)
17877         {
17878         case OT_cinfix3_legacy:
17879           /* Ignore conditional suffixes matched on infix only mnemonics.  */
17880           break;
17881
17882         case OT_cinfix3:
17883         case OT_cinfix3_deprecated:
17884         case OT_odd_infix_unc:
17885           if (!unified_syntax)
17886             return 0;
17887           /* Fall through.  */
17888
17889         case OT_csuffix:
17890         case OT_csuffixF:
17891         case OT_csuf_or_in3:
17892           inst.cond = cond->value;
17893           return opcode;
17894
17895         case OT_unconditional:
17896         case OT_unconditionalF:
17897           if (thumb_mode)
17898             inst.cond = cond->value;
17899           else
17900             {
17901               /* Delayed diagnostic.  */
17902               inst.error = BAD_COND;
17903               inst.cond = COND_ALWAYS;
17904             }
17905           return opcode;
17906
17907         default:
17908           return NULL;
17909         }
17910     }
17911
17912   /* Cannot have a usual-position infix on a mnemonic of less than
17913      six characters (five would be a suffix).  */
17914   if (end - base < 6)
17915     return NULL;
17916
17917   /* Look for infixed mnemonic in the usual position.  */
17918   affix = base + 3;
17919   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17920   if (!cond)
17921     return NULL;
17922
17923   memcpy (save, affix, 2);
17924   memmove (affix, affix + 2, (end - affix) - 2);
17925   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17926                                                     (end - base) - 2);
17927   memmove (affix + 2, affix, (end - affix) - 2);
17928   memcpy (affix, save, 2);
17929
17930   if (opcode
17931       && (opcode->tag == OT_cinfix3
17932           || opcode->tag == OT_cinfix3_deprecated
17933           || opcode->tag == OT_csuf_or_in3
17934           || opcode->tag == OT_cinfix3_legacy))
17935     {
17936       /* Step CM.  */
17937       if (warn_on_deprecated && unified_syntax
17938           && (opcode->tag == OT_cinfix3
17939               || opcode->tag == OT_cinfix3_deprecated))
17940         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17941
17942       inst.cond = cond->value;
17943       return opcode;
17944     }
17945
17946   return NULL;
17947 }
17948
17949 /* This function generates an initial IT instruction, leaving its block
17950    virtually open for the new instructions. Eventually,
17951    the mask will be updated by now_it_add_mask () each time
17952    a new instruction needs to be included in the IT block.
17953    Finally, the block is closed with close_automatic_it_block ().
17954    The block closure can be requested either from md_assemble (),
17955    a tencode (), or due to a label hook.  */
17956
17957 static void
17958 new_automatic_it_block (int cond)
17959 {
17960   now_it.state = AUTOMATIC_IT_BLOCK;
17961   now_it.mask = 0x18;
17962   now_it.cc = cond;
17963   now_it.block_length = 1;
17964   mapping_state (MAP_THUMB);
17965   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
17966   now_it.warn_deprecated = FALSE;
17967   now_it.insn_cond = TRUE;
17968 }
17969
17970 /* Close an automatic IT block.
17971    See comments in new_automatic_it_block ().  */
17972
17973 static void
17974 close_automatic_it_block (void)
17975 {
17976   now_it.mask = 0x10;
17977   now_it.block_length = 0;
17978 }
17979
17980 /* Update the mask of the current automatically-generated IT
17981    instruction. See comments in new_automatic_it_block ().  */
17982
17983 static void
17984 now_it_add_mask (int cond)
17985 {
17986 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
17987 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
17988                                               | ((bitvalue) << (nbit)))
17989   const int resulting_bit = (cond & 1);
17990
17991   now_it.mask &= 0xf;
17992   now_it.mask = SET_BIT_VALUE (now_it.mask,
17993                                    resulting_bit,
17994                                   (5 - now_it.block_length));
17995   now_it.mask = SET_BIT_VALUE (now_it.mask,
17996                                    1,
17997                                    ((5 - now_it.block_length) - 1) );
17998   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
17999
18000 #undef CLEAR_BIT
18001 #undef SET_BIT_VALUE
18002 }
18003
18004 /* The IT blocks handling machinery is accessed through the these functions:
18005      it_fsm_pre_encode ()               from md_assemble ()
18006      set_it_insn_type ()                optional, from the tencode functions
18007      set_it_insn_type_last ()           ditto
18008      in_it_block ()                     ditto
18009      it_fsm_post_encode ()              from md_assemble ()
18010      force_automatic_it_block_close ()  from label handling functions
18011
18012    Rationale:
18013      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
18014         initializing the IT insn type with a generic initial value depending
18015         on the inst.condition.
18016      2) During the tencode function, two things may happen:
18017         a) The tencode function overrides the IT insn type by
18018            calling either set_it_insn_type (type) or set_it_insn_type_last ().
18019         b) The tencode function queries the IT block state by
18020            calling in_it_block () (i.e. to determine narrow/not narrow mode).
18021
18022         Both set_it_insn_type and in_it_block run the internal FSM state
18023         handling function (handle_it_state), because: a) setting the IT insn
18024         type may incur in an invalid state (exiting the function),
18025         and b) querying the state requires the FSM to be updated.
18026         Specifically we want to avoid creating an IT block for conditional
18027         branches, so it_fsm_pre_encode is actually a guess and we can't
18028         determine whether an IT block is required until the tencode () routine
18029         has decided what type of instruction this actually it.
18030         Because of this, if set_it_insn_type and in_it_block have to be used,
18031         set_it_insn_type has to be called first.
18032
18033         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
18034         determines the insn IT type depending on the inst.cond code.
18035         When a tencode () routine encodes an instruction that can be
18036         either outside an IT block, or, in the case of being inside, has to be
18037         the last one, set_it_insn_type_last () will determine the proper
18038         IT instruction type based on the inst.cond code. Otherwise,
18039         set_it_insn_type can be called for overriding that logic or
18040         for covering other cases.
18041
18042         Calling handle_it_state () may not transition the IT block state to
18043         OUTSIDE_IT_BLOCK immediately, since the (current) state could be
18044         still queried. Instead, if the FSM determines that the state should
18045         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
18046         after the tencode () function: that's what it_fsm_post_encode () does.
18047
18048         Since in_it_block () calls the state handling function to get an
18049         updated state, an error may occur (due to invalid insns combination).
18050         In that case, inst.error is set.
18051         Therefore, inst.error has to be checked after the execution of
18052         the tencode () routine.
18053
18054      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
18055         any pending state change (if any) that didn't take place in
18056         handle_it_state () as explained above.  */
18057
18058 static void
18059 it_fsm_pre_encode (void)
18060 {
18061   if (inst.cond != COND_ALWAYS)
18062     inst.it_insn_type = INSIDE_IT_INSN;
18063   else
18064     inst.it_insn_type = OUTSIDE_IT_INSN;
18065
18066   now_it.state_handled = 0;
18067 }
18068
18069 /* IT state FSM handling function.  */
18070
18071 static int
18072 handle_it_state (void)
18073 {
18074   now_it.state_handled = 1;
18075   now_it.insn_cond = FALSE;
18076
18077   switch (now_it.state)
18078     {
18079     case OUTSIDE_IT_BLOCK:
18080       switch (inst.it_insn_type)
18081         {
18082         case OUTSIDE_IT_INSN:
18083           break;
18084
18085         case INSIDE_IT_INSN:
18086         case INSIDE_IT_LAST_INSN:
18087           if (thumb_mode == 0)
18088             {
18089               if (unified_syntax
18090                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
18091                 as_tsktsk (_("Warning: conditional outside an IT block"\
18092                              " for Thumb."));
18093             }
18094           else
18095             {
18096               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
18097                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
18098                 {
18099                   /* Automatically generate the IT instruction.  */
18100                   new_automatic_it_block (inst.cond);
18101                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
18102                     close_automatic_it_block ();
18103                 }
18104               else
18105                 {
18106                   inst.error = BAD_OUT_IT;
18107                   return FAIL;
18108                 }
18109             }
18110           break;
18111
18112         case IF_INSIDE_IT_LAST_INSN:
18113         case NEUTRAL_IT_INSN:
18114           break;
18115
18116         case IT_INSN:
18117           now_it.state = MANUAL_IT_BLOCK;
18118           now_it.block_length = 0;
18119           break;
18120         }
18121       break;
18122
18123     case AUTOMATIC_IT_BLOCK:
18124       /* Three things may happen now:
18125          a) We should increment current it block size;
18126          b) We should close current it block (closing insn or 4 insns);
18127          c) We should close current it block and start a new one (due
18128          to incompatible conditions or
18129          4 insns-length block reached).  */
18130
18131       switch (inst.it_insn_type)
18132         {
18133         case OUTSIDE_IT_INSN:
18134           /* The closure of the block shall happen immediately,
18135              so any in_it_block () call reports the block as closed.  */
18136           force_automatic_it_block_close ();
18137           break;
18138
18139         case INSIDE_IT_INSN:
18140         case INSIDE_IT_LAST_INSN:
18141         case IF_INSIDE_IT_LAST_INSN:
18142           now_it.block_length++;
18143
18144           if (now_it.block_length > 4
18145               || !now_it_compatible (inst.cond))
18146             {
18147               force_automatic_it_block_close ();
18148               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
18149                 new_automatic_it_block (inst.cond);
18150             }
18151           else
18152             {
18153               now_it.insn_cond = TRUE;
18154               now_it_add_mask (inst.cond);
18155             }
18156
18157           if (now_it.state == AUTOMATIC_IT_BLOCK
18158               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
18159                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
18160             close_automatic_it_block ();
18161           break;
18162
18163         case NEUTRAL_IT_INSN:
18164           now_it.block_length++;
18165           now_it.insn_cond = TRUE;
18166
18167           if (now_it.block_length > 4)
18168             force_automatic_it_block_close ();
18169           else
18170             now_it_add_mask (now_it.cc & 1);
18171           break;
18172
18173         case IT_INSN:
18174           close_automatic_it_block ();
18175           now_it.state = MANUAL_IT_BLOCK;
18176           break;
18177         }
18178       break;
18179
18180     case MANUAL_IT_BLOCK:
18181       {
18182         /* Check conditional suffixes.  */
18183         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
18184         int is_last;
18185         now_it.mask <<= 1;
18186         now_it.mask &= 0x1f;
18187         is_last = (now_it.mask == 0x10);
18188         now_it.insn_cond = TRUE;
18189
18190         switch (inst.it_insn_type)
18191           {
18192           case OUTSIDE_IT_INSN:
18193             inst.error = BAD_NOT_IT;
18194             return FAIL;
18195
18196           case INSIDE_IT_INSN:
18197             if (cond != inst.cond)
18198               {
18199                 inst.error = BAD_IT_COND;
18200                 return FAIL;
18201               }
18202             break;
18203
18204           case INSIDE_IT_LAST_INSN:
18205           case IF_INSIDE_IT_LAST_INSN:
18206             if (cond != inst.cond)
18207               {
18208                 inst.error = BAD_IT_COND;
18209                 return FAIL;
18210               }
18211             if (!is_last)
18212               {
18213                 inst.error = BAD_BRANCH;
18214                 return FAIL;
18215               }
18216             break;
18217
18218           case NEUTRAL_IT_INSN:
18219             /* The BKPT instruction is unconditional even in an IT block.  */
18220             break;
18221
18222           case IT_INSN:
18223             inst.error = BAD_IT_IT;
18224             return FAIL;
18225           }
18226       }
18227       break;
18228     }
18229
18230   return SUCCESS;
18231 }
18232
18233 struct depr_insn_mask
18234 {
18235   unsigned long pattern;
18236   unsigned long mask;
18237   const char* description;
18238 };
18239
18240 /* List of 16-bit instruction patterns deprecated in an IT block in
18241    ARMv8.  */
18242 static const struct depr_insn_mask depr_it_insns[] = {
18243   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
18244   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
18245   { 0xa000, 0xb800, N_("ADR") },
18246   { 0x4800, 0xf800, N_("Literal loads") },
18247   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
18248   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
18249   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
18250      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
18251   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
18252   { 0, 0, NULL }
18253 };
18254
18255 static void
18256 it_fsm_post_encode (void)
18257 {
18258   int is_last;
18259
18260   if (!now_it.state_handled)
18261     handle_it_state ();
18262
18263   if (now_it.insn_cond
18264       && !now_it.warn_deprecated
18265       && warn_on_deprecated
18266       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
18267     {
18268       if (inst.instruction >= 0x10000)
18269         {
18270           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
18271                      "deprecated in ARMv8"));
18272           now_it.warn_deprecated = TRUE;
18273         }
18274       else
18275         {
18276           const struct depr_insn_mask *p = depr_it_insns;
18277
18278           while (p->mask != 0)
18279             {
18280               if ((inst.instruction & p->mask) == p->pattern)
18281                 {
18282                   as_tsktsk (_("IT blocks containing 16-bit Thumb instructions "
18283                              "of the following class are deprecated in ARMv8: "
18284                              "%s"), p->description);
18285                   now_it.warn_deprecated = TRUE;
18286                   break;
18287                 }
18288
18289               ++p;
18290             }
18291         }
18292
18293       if (now_it.block_length > 1)
18294         {
18295           as_tsktsk (_("IT blocks containing more than one conditional "
18296                      "instruction are deprecated in ARMv8"));
18297           now_it.warn_deprecated = TRUE;
18298         }
18299     }
18300
18301   is_last = (now_it.mask == 0x10);
18302   if (is_last)
18303     {
18304       now_it.state = OUTSIDE_IT_BLOCK;
18305       now_it.mask = 0;
18306     }
18307 }
18308
18309 static void
18310 force_automatic_it_block_close (void)
18311 {
18312   if (now_it.state == AUTOMATIC_IT_BLOCK)
18313     {
18314       close_automatic_it_block ();
18315       now_it.state = OUTSIDE_IT_BLOCK;
18316       now_it.mask = 0;
18317     }
18318 }
18319
18320 static int
18321 in_it_block (void)
18322 {
18323   if (!now_it.state_handled)
18324     handle_it_state ();
18325
18326   return now_it.state != OUTSIDE_IT_BLOCK;
18327 }
18328
18329 /* Whether OPCODE only has T32 encoding.  Since this function is only used by
18330    t32_insn_ok, OPCODE enabled by v6t2 extension bit do not need to be listed
18331    here, hence the "known" in the function name.  */
18332
18333 static bfd_boolean
18334 known_t32_only_insn (const struct asm_opcode *opcode)
18335 {
18336   /* Original Thumb-1 wide instruction.  */
18337   if (opcode->tencode == do_t_blx
18338       || opcode->tencode == do_t_branch23
18339       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
18340       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier))
18341     return TRUE;
18342
18343   /* Wide-only instruction added to ARMv8-M Baseline.  */
18344   if (ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v8m_m_only)
18345       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_atomics)
18346       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_v6t2_v8m)
18347       || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_div))
18348     return TRUE;
18349
18350   return FALSE;
18351 }
18352
18353 /* Whether wide instruction variant can be used if available for a valid OPCODE
18354    in ARCH.  */
18355
18356 static bfd_boolean
18357 t32_insn_ok (arm_feature_set arch, const struct asm_opcode *opcode)
18358 {
18359   if (known_t32_only_insn (opcode))
18360     return TRUE;
18361
18362   /* Instruction with narrow and wide encoding added to ARMv8-M.  Availability
18363      of variant T3 of B.W is checked in do_t_branch.  */
18364   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18365       && opcode->tencode == do_t_branch)
18366     return TRUE;
18367
18368   /* MOV accepts T1/T3 encodings under Baseline, T3 encoding is 32bit.  */
18369   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v8m)
18370       && opcode->tencode == do_t_mov_cmp
18371       /* Make sure CMP instruction is not affected.  */
18372       && opcode->aencode == do_mov)
18373     return TRUE;
18374
18375   /* Wide instruction variants of all instructions with narrow *and* wide
18376      variants become available with ARMv6t2.  Other opcodes are either
18377      narrow-only or wide-only and are thus available if OPCODE is valid.  */
18378   if (ARM_CPU_HAS_FEATURE (arch, arm_ext_v6t2))
18379     return TRUE;
18380
18381   /* OPCODE with narrow only instruction variant or wide variant not
18382      available.  */
18383   return FALSE;
18384 }
18385
18386 void
18387 md_assemble (char *str)
18388 {
18389   char *p = str;
18390   const struct asm_opcode * opcode;
18391
18392   /* Align the previous label if needed.  */
18393   if (last_label_seen != NULL)
18394     {
18395       symbol_set_frag (last_label_seen, frag_now);
18396       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
18397       S_SET_SEGMENT (last_label_seen, now_seg);
18398     }
18399
18400   memset (&inst, '\0', sizeof (inst));
18401   inst.reloc.type = BFD_RELOC_UNUSED;
18402
18403   opcode = opcode_lookup (&p);
18404   if (!opcode)
18405     {
18406       /* It wasn't an instruction, but it might be a register alias of
18407          the form alias .req reg, or a Neon .dn/.qn directive.  */
18408       if (! create_register_alias (str, p)
18409           && ! create_neon_reg_alias (str, p))
18410         as_bad (_("bad instruction `%s'"), str);
18411
18412       return;
18413     }
18414
18415   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
18416     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
18417
18418   /* The value which unconditional instructions should have in place of the
18419      condition field.  */
18420   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
18421
18422   if (thumb_mode)
18423     {
18424       arm_feature_set variant;
18425
18426       variant = cpu_variant;
18427       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
18428       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
18429         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
18430       /* Check that this instruction is supported for this CPU.  */
18431       if (!opcode->tvariant
18432           || (thumb_mode == 1
18433               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
18434         {
18435           as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
18436           return;
18437         }
18438       if (inst.cond != COND_ALWAYS && !unified_syntax
18439           && opcode->tencode != do_t_branch)
18440         {
18441           as_bad (_("Thumb does not support conditional execution"));
18442           return;
18443         }
18444
18445       /* Two things are addressed here:
18446          1) Implicit require narrow instructions on Thumb-1.
18447             This avoids relaxation accidentally introducing Thumb-2
18448             instructions.
18449          2) Reject wide instructions in non Thumb-2 cores.
18450
18451          Only instructions with narrow and wide variants need to be handled
18452          but selecting all non wide-only instructions is easier.  */
18453       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2)
18454           && !t32_insn_ok (variant, opcode))
18455         {
18456           if (inst.size_req == 0)
18457             inst.size_req = 2;
18458           else if (inst.size_req == 4)
18459             {
18460               if (ARM_CPU_HAS_FEATURE (variant, arm_ext_v8m))
18461                 as_bad (_("selected processor does not support 32bit wide "
18462                           "variant of instruction `%s'"), str);
18463               else
18464                 as_bad (_("selected processor does not support `%s' in "
18465                           "Thumb-2 mode"), str);
18466               return;
18467             }
18468         }
18469
18470       inst.instruction = opcode->tvalue;
18471
18472       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
18473         {
18474           /* Prepare the it_insn_type for those encodings that don't set
18475              it.  */
18476           it_fsm_pre_encode ();
18477
18478           opcode->tencode ();
18479
18480           it_fsm_post_encode ();
18481         }
18482
18483       if (!(inst.error || inst.relax))
18484         {
18485           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
18486           inst.size = (inst.instruction > 0xffff ? 4 : 2);
18487           if (inst.size_req && inst.size_req != inst.size)
18488             {
18489               as_bad (_("cannot honor width suffix -- `%s'"), str);
18490               return;
18491             }
18492         }
18493
18494       /* Something has gone badly wrong if we try to relax a fixed size
18495          instruction.  */
18496       gas_assert (inst.size_req == 0 || !inst.relax);
18497
18498       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18499                               *opcode->tvariant);
18500       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
18501          set those bits when Thumb-2 32-bit instructions are seen.  The impact
18502          of relaxable instructions will be considered later after we finish all
18503          relaxation.  */
18504       if (ARM_FEATURE_CORE_EQUAL (cpu_variant, arm_arch_any))
18505         variant = arm_arch_none;
18506       else
18507         variant = cpu_variant;
18508       if (inst.size == 4 && !t32_insn_ok (variant, opcode))
18509         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
18510                                 arm_ext_v6t2);
18511
18512       check_neon_suffixes;
18513
18514       if (!inst.error)
18515         {
18516           mapping_state (MAP_THUMB);
18517         }
18518     }
18519   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
18520     {
18521       bfd_boolean is_bx;
18522
18523       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
18524       is_bx = (opcode->aencode == do_bx);
18525
18526       /* Check that this instruction is supported for this CPU.  */
18527       if (!(is_bx && fix_v4bx)
18528           && !(opcode->avariant &&
18529                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
18530         {
18531           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
18532           return;
18533         }
18534       if (inst.size_req)
18535         {
18536           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
18537           return;
18538         }
18539
18540       inst.instruction = opcode->avalue;
18541       if (opcode->tag == OT_unconditionalF)
18542         inst.instruction |= 0xFU << 28;
18543       else
18544         inst.instruction |= inst.cond << 28;
18545       inst.size = INSN_SIZE;
18546       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
18547         {
18548           it_fsm_pre_encode ();
18549           opcode->aencode ();
18550           it_fsm_post_encode ();
18551         }
18552       /* Arm mode bx is marked as both v4T and v5 because it's still required
18553          on a hypothetical non-thumb v5 core.  */
18554       if (is_bx)
18555         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
18556       else
18557         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
18558                                 *opcode->avariant);
18559
18560       check_neon_suffixes;
18561
18562       if (!inst.error)
18563         {
18564           mapping_state (MAP_ARM);
18565         }
18566     }
18567   else
18568     {
18569       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
18570                 "-- `%s'"), str);
18571       return;
18572     }
18573   output_inst (str);
18574 }
18575
18576 static void
18577 check_it_blocks_finished (void)
18578 {
18579 #ifdef OBJ_ELF
18580   asection *sect;
18581
18582   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
18583     if (seg_info (sect)->tc_segment_info_data.current_it.state
18584         == MANUAL_IT_BLOCK)
18585       {
18586         as_warn (_("section '%s' finished with an open IT block."),
18587                  sect->name);
18588       }
18589 #else
18590   if (now_it.state == MANUAL_IT_BLOCK)
18591     as_warn (_("file finished with an open IT block."));
18592 #endif
18593 }
18594
18595 /* Various frobbings of labels and their addresses.  */
18596
18597 void
18598 arm_start_line_hook (void)
18599 {
18600   last_label_seen = NULL;
18601 }
18602
18603 void
18604 arm_frob_label (symbolS * sym)
18605 {
18606   last_label_seen = sym;
18607
18608   ARM_SET_THUMB (sym, thumb_mode);
18609
18610 #if defined OBJ_COFF || defined OBJ_ELF
18611   ARM_SET_INTERWORK (sym, support_interwork);
18612 #endif
18613
18614   force_automatic_it_block_close ();
18615
18616   /* Note - do not allow local symbols (.Lxxx) to be labelled
18617      as Thumb functions.  This is because these labels, whilst
18618      they exist inside Thumb code, are not the entry points for
18619      possible ARM->Thumb calls.  Also, these labels can be used
18620      as part of a computed goto or switch statement.  eg gcc
18621      can generate code that looks like this:
18622
18623                 ldr  r2, [pc, .Laaa]
18624                 lsl  r3, r3, #2
18625                 ldr  r2, [r3, r2]
18626                 mov  pc, r2
18627
18628        .Lbbb:  .word .Lxxx
18629        .Lccc:  .word .Lyyy
18630        ..etc...
18631        .Laaa:   .word Lbbb
18632
18633      The first instruction loads the address of the jump table.
18634      The second instruction converts a table index into a byte offset.
18635      The third instruction gets the jump address out of the table.
18636      The fourth instruction performs the jump.
18637
18638      If the address stored at .Laaa is that of a symbol which has the
18639      Thumb_Func bit set, then the linker will arrange for this address
18640      to have the bottom bit set, which in turn would mean that the
18641      address computation performed by the third instruction would end
18642      up with the bottom bit set.  Since the ARM is capable of unaligned
18643      word loads, the instruction would then load the incorrect address
18644      out of the jump table, and chaos would ensue.  */
18645   if (label_is_thumb_function_name
18646       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
18647       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
18648     {
18649       /* When the address of a Thumb function is taken the bottom
18650          bit of that address should be set.  This will allow
18651          interworking between Arm and Thumb functions to work
18652          correctly.  */
18653
18654       THUMB_SET_FUNC (sym, 1);
18655
18656       label_is_thumb_function_name = FALSE;
18657     }
18658
18659   dwarf2_emit_label (sym);
18660 }
18661
18662 bfd_boolean
18663 arm_data_in_code (void)
18664 {
18665   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
18666     {
18667       *input_line_pointer = '/';
18668       input_line_pointer += 5;
18669       *input_line_pointer = 0;
18670       return TRUE;
18671     }
18672
18673   return FALSE;
18674 }
18675
18676 char *
18677 arm_canonicalize_symbol_name (char * name)
18678 {
18679   int len;
18680
18681   if (thumb_mode && (len = strlen (name)) > 5
18682       && streq (name + len - 5, "/data"))
18683     *(name + len - 5) = 0;
18684
18685   return name;
18686 }
18687 \f
18688 /* Table of all register names defined by default.  The user can
18689    define additional names with .req.  Note that all register names
18690    should appear in both upper and lowercase variants.  Some registers
18691    also have mixed-case names.  */
18692
18693 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
18694 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
18695 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
18696 #define REGSET(p,t) \
18697   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
18698   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
18699   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
18700   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
18701 #define REGSETH(p,t) \
18702   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
18703   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
18704   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
18705   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
18706 #define REGSET2(p,t) \
18707   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
18708   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
18709   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
18710   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
18711 #define SPLRBANK(base,bank,t) \
18712   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
18713   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
18714   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
18715   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
18716   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
18717   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
18718
18719 static const struct reg_entry reg_names[] =
18720 {
18721   /* ARM integer registers.  */
18722   REGSET(r, RN), REGSET(R, RN),
18723
18724   /* ATPCS synonyms.  */
18725   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
18726   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
18727   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
18728
18729   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
18730   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
18731   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
18732
18733   /* Well-known aliases.  */
18734   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
18735   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
18736
18737   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
18738   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
18739
18740   /* Coprocessor numbers.  */
18741   REGSET(p, CP), REGSET(P, CP),
18742
18743   /* Coprocessor register numbers.  The "cr" variants are for backward
18744      compatibility.  */
18745   REGSET(c,  CN), REGSET(C, CN),
18746   REGSET(cr, CN), REGSET(CR, CN),
18747
18748   /* ARM banked registers.  */
18749   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
18750   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
18751   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
18752   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
18753   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
18754   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
18755   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
18756
18757   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
18758   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
18759   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
18760   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
18761   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
18762   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
18763   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
18764   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
18765
18766   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
18767   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
18768   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
18769   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
18770   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
18771   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
18772   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
18773   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
18774   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
18775
18776   /* FPA registers.  */
18777   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
18778   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
18779
18780   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
18781   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
18782
18783   /* VFP SP registers.  */
18784   REGSET(s,VFS),  REGSET(S,VFS),
18785   REGSETH(s,VFS), REGSETH(S,VFS),
18786
18787   /* VFP DP Registers.  */
18788   REGSET(d,VFD),  REGSET(D,VFD),
18789   /* Extra Neon DP registers.  */
18790   REGSETH(d,VFD), REGSETH(D,VFD),
18791
18792   /* Neon QP registers.  */
18793   REGSET2(q,NQ),  REGSET2(Q,NQ),
18794
18795   /* VFP control registers.  */
18796   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
18797   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
18798   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
18799   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
18800   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
18801   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
18802
18803   /* Maverick DSP coprocessor registers.  */
18804   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
18805   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
18806
18807   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
18808   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
18809   REGDEF(dspsc,0,DSPSC),
18810
18811   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
18812   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
18813   REGDEF(DSPSC,0,DSPSC),
18814
18815   /* iWMMXt data registers - p0, c0-15.  */
18816   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
18817
18818   /* iWMMXt control registers - p1, c0-3.  */
18819   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
18820   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
18821   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
18822   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
18823
18824   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
18825   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
18826   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
18827   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
18828   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
18829
18830   /* XScale accumulator registers.  */
18831   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
18832 };
18833 #undef REGDEF
18834 #undef REGNUM
18835 #undef REGSET
18836
18837 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
18838    within psr_required_here.  */
18839 static const struct asm_psr psrs[] =
18840 {
18841   /* Backward compatibility notation.  Note that "all" is no longer
18842      truly all possible PSR bits.  */
18843   {"all",  PSR_c | PSR_f},
18844   {"flg",  PSR_f},
18845   {"ctl",  PSR_c},
18846
18847   /* Individual flags.  */
18848   {"f",    PSR_f},
18849   {"c",    PSR_c},
18850   {"x",    PSR_x},
18851   {"s",    PSR_s},
18852
18853   /* Combinations of flags.  */
18854   {"fs",   PSR_f | PSR_s},
18855   {"fx",   PSR_f | PSR_x},
18856   {"fc",   PSR_f | PSR_c},
18857   {"sf",   PSR_s | PSR_f},
18858   {"sx",   PSR_s | PSR_x},
18859   {"sc",   PSR_s | PSR_c},
18860   {"xf",   PSR_x | PSR_f},
18861   {"xs",   PSR_x | PSR_s},
18862   {"xc",   PSR_x | PSR_c},
18863   {"cf",   PSR_c | PSR_f},
18864   {"cs",   PSR_c | PSR_s},
18865   {"cx",   PSR_c | PSR_x},
18866   {"fsx",  PSR_f | PSR_s | PSR_x},
18867   {"fsc",  PSR_f | PSR_s | PSR_c},
18868   {"fxs",  PSR_f | PSR_x | PSR_s},
18869   {"fxc",  PSR_f | PSR_x | PSR_c},
18870   {"fcs",  PSR_f | PSR_c | PSR_s},
18871   {"fcx",  PSR_f | PSR_c | PSR_x},
18872   {"sfx",  PSR_s | PSR_f | PSR_x},
18873   {"sfc",  PSR_s | PSR_f | PSR_c},
18874   {"sxf",  PSR_s | PSR_x | PSR_f},
18875   {"sxc",  PSR_s | PSR_x | PSR_c},
18876   {"scf",  PSR_s | PSR_c | PSR_f},
18877   {"scx",  PSR_s | PSR_c | PSR_x},
18878   {"xfs",  PSR_x | PSR_f | PSR_s},
18879   {"xfc",  PSR_x | PSR_f | PSR_c},
18880   {"xsf",  PSR_x | PSR_s | PSR_f},
18881   {"xsc",  PSR_x | PSR_s | PSR_c},
18882   {"xcf",  PSR_x | PSR_c | PSR_f},
18883   {"xcs",  PSR_x | PSR_c | PSR_s},
18884   {"cfs",  PSR_c | PSR_f | PSR_s},
18885   {"cfx",  PSR_c | PSR_f | PSR_x},
18886   {"csf",  PSR_c | PSR_s | PSR_f},
18887   {"csx",  PSR_c | PSR_s | PSR_x},
18888   {"cxf",  PSR_c | PSR_x | PSR_f},
18889   {"cxs",  PSR_c | PSR_x | PSR_s},
18890   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
18891   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
18892   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
18893   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
18894   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
18895   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
18896   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
18897   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
18898   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
18899   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
18900   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
18901   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
18902   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
18903   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
18904   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
18905   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
18906   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
18907   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
18908   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
18909   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
18910   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
18911   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
18912   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
18913   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
18914 };
18915
18916 /* Table of V7M psr names.  */
18917 static const struct asm_psr v7m_psrs[] =
18918 {
18919   {"apsr",         0x0 }, {"APSR",         0x0 },
18920   {"iapsr",        0x1 }, {"IAPSR",        0x1 },
18921   {"eapsr",        0x2 }, {"EAPSR",        0x2 },
18922   {"psr",          0x3 }, {"PSR",          0x3 },
18923   {"xpsr",         0x3 }, {"XPSR",         0x3 }, {"xPSR",        3 },
18924   {"ipsr",         0x5 }, {"IPSR",         0x5 },
18925   {"epsr",         0x6 }, {"EPSR",         0x6 },
18926   {"iepsr",        0x7 }, {"IEPSR",        0x7 },
18927   {"msp",          0x8 }, {"MSP",          0x8 },
18928   {"psp",          0x9 }, {"PSP",          0x9 },
18929   {"msplim",       0xa }, {"MSPLIM",       0xa },
18930   {"psplim",       0xb }, {"PSPLIM",       0xb },
18931   {"primask",      0x10}, {"PRIMASK",      0x10},
18932   {"basepri",      0x11}, {"BASEPRI",      0x11},
18933   {"basepri_max",  0x12}, {"BASEPRI_MAX",  0x12},
18934   {"faultmask",    0x13}, {"FAULTMASK",    0x13},
18935   {"control",      0x14}, {"CONTROL",      0x14},
18936   {"msp_ns",       0x88}, {"MSP_NS",       0x88},
18937   {"psp_ns",       0x89}, {"PSP_NS",       0x89},
18938   {"msplim_ns",    0x8a}, {"MSPLIM_NS",    0x8a},
18939   {"psplim_ns",    0x8b}, {"PSPLIM_NS",    0x8b},
18940   {"primask_ns",   0x90}, {"PRIMASK_NS",   0x90},
18941   {"basepri_ns",   0x91}, {"BASEPRI_NS",   0x91},
18942   {"faultmask_ns", 0x93}, {"FAULTMASK_NS", 0x93},
18943   {"control_ns",   0x94}, {"CONTROL_NS",   0x94},
18944   {"sp_ns",        0x98}, {"SP_NS",        0x98 }
18945 };
18946
18947 /* Table of all shift-in-operand names.  */
18948 static const struct asm_shift_name shift_names [] =
18949 {
18950   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
18951   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
18952   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
18953   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
18954   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
18955   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
18956 };
18957
18958 /* Table of all explicit relocation names.  */
18959 #ifdef OBJ_ELF
18960 static struct reloc_entry reloc_names[] =
18961 {
18962   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
18963   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
18964   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
18965   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
18966   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
18967   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
18968   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
18969   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
18970   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
18971   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
18972   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
18973   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
18974   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
18975         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
18976   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
18977         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
18978   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
18979         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
18980 };
18981 #endif
18982
18983 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
18984 static const struct asm_cond conds[] =
18985 {
18986   {"eq", 0x0},
18987   {"ne", 0x1},
18988   {"cs", 0x2}, {"hs", 0x2},
18989   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
18990   {"mi", 0x4},
18991   {"pl", 0x5},
18992   {"vs", 0x6},
18993   {"vc", 0x7},
18994   {"hi", 0x8},
18995   {"ls", 0x9},
18996   {"ge", 0xa},
18997   {"lt", 0xb},
18998   {"gt", 0xc},
18999   {"le", 0xd},
19000   {"al", 0xe}
19001 };
19002
19003 #define UL_BARRIER(L,U,CODE,FEAT) \
19004   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
19005   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
19006
19007 static struct asm_barrier_opt barrier_opt_names[] =
19008 {
19009   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
19010   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
19011   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
19012   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
19013   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
19014   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
19015   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
19016   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
19017   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
19018   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
19019   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
19020   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
19021   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
19022   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
19023   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
19024   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
19025 };
19026
19027 #undef UL_BARRIER
19028
19029 /* Table of ARM-format instructions.    */
19030
19031 /* Macros for gluing together operand strings.  N.B. In all cases
19032    other than OPS0, the trailing OP_stop comes from default
19033    zero-initialization of the unspecified elements of the array.  */
19034 #define OPS0()            { OP_stop, }
19035 #define OPS1(a)           { OP_##a, }
19036 #define OPS2(a,b)         { OP_##a,OP_##b, }
19037 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
19038 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
19039 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
19040 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
19041
19042 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
19043    This is useful when mixing operands for ARM and THUMB, i.e. using the
19044    MIX_ARM_THUMB_OPERANDS macro.
19045    In order to use these macros, prefix the number of operands with _
19046    e.g. _3.  */
19047 #define OPS_1(a)           { a, }
19048 #define OPS_2(a,b)         { a,b, }
19049 #define OPS_3(a,b,c)       { a,b,c, }
19050 #define OPS_4(a,b,c,d)     { a,b,c,d, }
19051 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
19052 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
19053
19054 /* These macros abstract out the exact format of the mnemonic table and
19055    save some repeated characters.  */
19056
19057 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
19058 #define TxCE(mnem, op, top, nops, ops, ae, te) \
19059   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
19060     THUMB_VARIANT, do_##ae, do_##te }
19061
19062 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
19063    a T_MNEM_xyz enumerator.  */
19064 #define TCE(mnem, aop, top, nops, ops, ae, te) \
19065       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
19066 #define tCE(mnem, aop, top, nops, ops, ae, te) \
19067       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19068
19069 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
19070    infix after the third character.  */
19071 #define TxC3(mnem, op, top, nops, ops, ae, te) \
19072   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
19073     THUMB_VARIANT, do_##ae, do_##te }
19074 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
19075   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
19076     THUMB_VARIANT, do_##ae, do_##te }
19077 #define TC3(mnem, aop, top, nops, ops, ae, te) \
19078       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
19079 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
19080       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
19081 #define tC3(mnem, aop, top, nops, ops, ae, te) \
19082       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19083 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
19084       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
19085
19086 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
19087    field is still 0xE.  Many of the Thumb variants can be executed
19088    conditionally, so this is checked separately.  */
19089 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
19090   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19091     THUMB_VARIANT, do_##ae, do_##te }
19092
19093 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
19094    Used by mnemonics that have very minimal differences in the encoding for
19095    ARM and Thumb variants and can be handled in a common function.  */
19096 #define TUEc(mnem, op, top, nops, ops, en) \
19097   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
19098     THUMB_VARIANT, do_##en, do_##en }
19099
19100 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
19101    condition code field.  */
19102 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
19103   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
19104     THUMB_VARIANT, do_##ae, do_##te }
19105
19106 /* ARM-only variants of all the above.  */
19107 #define CE(mnem,  op, nops, ops, ae)    \
19108   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19109
19110 #define C3(mnem, op, nops, ops, ae)     \
19111   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19112
19113 /* Legacy mnemonics that always have conditional infix after the third
19114    character.  */
19115 #define CL(mnem, op, nops, ops, ae)     \
19116   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19117     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19118
19119 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
19120 #define cCE(mnem,  op, nops, ops, ae)   \
19121   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19122
19123 /* Legacy coprocessor instructions where conditional infix and conditional
19124    suffix are ambiguous.  For consistency this includes all FPA instructions,
19125    not just the potentially ambiguous ones.  */
19126 #define cCL(mnem, op, nops, ops, ae)    \
19127   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
19128     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19129
19130 /* Coprocessor, takes either a suffix or a position-3 infix
19131    (for an FPA corner case). */
19132 #define C3E(mnem, op, nops, ops, ae) \
19133   { mnem, OPS##nops ops, OT_csuf_or_in3, \
19134     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
19135
19136 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
19137   { m1 #m2 m3, OPS##nops ops, \
19138     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
19139     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
19140
19141 #define CM(m1, m2, op, nops, ops, ae)   \
19142   xCM_ (m1,   , m2, op, nops, ops, ae), \
19143   xCM_ (m1, eq, m2, op, nops, ops, ae), \
19144   xCM_ (m1, ne, m2, op, nops, ops, ae), \
19145   xCM_ (m1, cs, m2, op, nops, ops, ae), \
19146   xCM_ (m1, hs, m2, op, nops, ops, ae), \
19147   xCM_ (m1, cc, m2, op, nops, ops, ae), \
19148   xCM_ (m1, ul, m2, op, nops, ops, ae), \
19149   xCM_ (m1, lo, m2, op, nops, ops, ae), \
19150   xCM_ (m1, mi, m2, op, nops, ops, ae), \
19151   xCM_ (m1, pl, m2, op, nops, ops, ae), \
19152   xCM_ (m1, vs, m2, op, nops, ops, ae), \
19153   xCM_ (m1, vc, m2, op, nops, ops, ae), \
19154   xCM_ (m1, hi, m2, op, nops, ops, ae), \
19155   xCM_ (m1, ls, m2, op, nops, ops, ae), \
19156   xCM_ (m1, ge, m2, op, nops, ops, ae), \
19157   xCM_ (m1, lt, m2, op, nops, ops, ae), \
19158   xCM_ (m1, gt, m2, op, nops, ops, ae), \
19159   xCM_ (m1, le, m2, op, nops, ops, ae), \
19160   xCM_ (m1, al, m2, op, nops, ops, ae)
19161
19162 #define UE(mnem, op, nops, ops, ae)     \
19163   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19164
19165 #define UF(mnem, op, nops, ops, ae)     \
19166   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
19167
19168 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
19169    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
19170    use the same encoding function for each.  */
19171 #define NUF(mnem, op, nops, ops, enc)                                   \
19172   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
19173     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19174
19175 /* Neon data processing, version which indirects through neon_enc_tab for
19176    the various overloaded versions of opcodes.  */
19177 #define nUF(mnem, op, nops, ops, enc)                                   \
19178   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
19179     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19180
19181 /* Neon insn with conditional suffix for the ARM version, non-overloaded
19182    version.  */
19183 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
19184   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
19185     THUMB_VARIANT, do_##enc, do_##enc }
19186
19187 #define NCE(mnem, op, nops, ops, enc)                                   \
19188    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19189
19190 #define NCEF(mnem, op, nops, ops, enc)                                  \
19191     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19192
19193 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
19194 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
19195   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
19196     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
19197
19198 #define nCE(mnem, op, nops, ops, enc)                                   \
19199    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
19200
19201 #define nCEF(mnem, op, nops, ops, enc)                                  \
19202     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
19203
19204 #define do_0 0
19205
19206 static const struct asm_opcode insns[] =
19207 {
19208 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
19209 #define THUMB_VARIANT  & arm_ext_v4t
19210  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
19211  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
19212  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
19213  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
19214  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
19215  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
19216  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
19217  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
19218  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
19219  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
19220  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
19221  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
19222  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
19223  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
19224  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
19225  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
19226
19227  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
19228     for setting PSR flag bits.  They are obsolete in V6 and do not
19229     have Thumb equivalents. */
19230  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19231  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
19232   CL("tstp",    110f000,           2, (RR, SH),      cmp),
19233  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19234  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
19235   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
19236  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19237  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
19238   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
19239
19240  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
19241  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
19242  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
19243  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
19244
19245  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
19246  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19247  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
19248                                                                 OP_RRnpc),
19249                                         OP_ADDRGLDR),ldst, t_ldst),
19250  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
19251
19252  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19253  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19254  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19255  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19256  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19257  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
19258
19259  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
19260  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
19261  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
19262  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
19263
19264   /* Pseudo ops.  */
19265  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
19266   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
19267  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
19268  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
19269
19270   /* Thumb-compatibility pseudo ops.  */
19271  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
19272  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
19273  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
19274  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
19275  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
19276  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
19277  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
19278  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
19279  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
19280  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
19281  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
19282  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
19283
19284  /* These may simplify to neg.  */
19285  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
19286  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
19287
19288 #undef  THUMB_VARIANT
19289 #define THUMB_VARIANT  & arm_ext_v6
19290
19291  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
19292
19293  /* V1 instructions with no Thumb analogue prior to V6T2.  */
19294 #undef  THUMB_VARIANT
19295 #define THUMB_VARIANT  & arm_ext_v6t2
19296
19297  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19298  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
19299   CL("teqp",    130f000,           2, (RR, SH),      cmp),
19300
19301  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19302  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19303  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
19304  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
19305
19306  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19307  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19308
19309  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19310  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
19311
19312  /* V1 instructions with no Thumb analogue at all.  */
19313   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
19314   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
19315
19316   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
19317   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
19318   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
19319   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
19320   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
19321   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
19322   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
19323   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
19324
19325 #undef  ARM_VARIANT
19326 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
19327 #undef  THUMB_VARIANT
19328 #define THUMB_VARIANT  & arm_ext_v4t
19329
19330  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
19331  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
19332
19333 #undef  THUMB_VARIANT
19334 #define THUMB_VARIANT  & arm_ext_v6t2
19335
19336  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19337   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
19338
19339   /* Generic coprocessor instructions.  */
19340  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19341  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19342  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19343  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19344  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19345  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19346  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
19347
19348 #undef  ARM_VARIANT
19349 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
19350
19351   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19352   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
19353
19354 #undef  ARM_VARIANT
19355 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
19356 #undef  THUMB_VARIANT
19357 #define THUMB_VARIANT  & arm_ext_msr
19358
19359  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
19360  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
19361
19362 #undef  ARM_VARIANT
19363 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
19364 #undef  THUMB_VARIANT
19365 #define THUMB_VARIANT  & arm_ext_v6t2
19366
19367  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19368   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19369  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19370   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19371  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19372   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19373  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
19374   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
19375
19376 #undef  ARM_VARIANT
19377 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
19378 #undef  THUMB_VARIANT
19379 #define THUMB_VARIANT  & arm_ext_v4t
19380
19381  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19382  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19383  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19384  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19385  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19386  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
19387
19388 #undef  ARM_VARIANT
19389 #define ARM_VARIANT  & arm_ext_v4t_5
19390
19391   /* ARM Architecture 4T.  */
19392   /* Note: bx (and blx) are required on V5, even if the processor does
19393      not support Thumb.  */
19394  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
19395
19396 #undef  ARM_VARIANT
19397 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
19398 #undef  THUMB_VARIANT
19399 #define THUMB_VARIANT  & arm_ext_v5t
19400
19401   /* Note: blx has 2 variants; the .value coded here is for
19402      BLX(2).  Only this variant has conditional execution.  */
19403  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
19404  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
19405
19406 #undef  THUMB_VARIANT
19407 #define THUMB_VARIANT  & arm_ext_v6t2
19408
19409  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
19410  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19411  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19412  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
19413  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
19414  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
19415  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19416  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
19417
19418 #undef  ARM_VARIANT
19419 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
19420 #undef  THUMB_VARIANT
19421 #define THUMB_VARIANT  & arm_ext_v5exp
19422
19423  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19424  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19425  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19426  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19427
19428  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19429  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
19430
19431  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19432  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19433  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19434  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
19435
19436  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19437  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19438  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19439  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19440
19441  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19442  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
19443
19444  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19445  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19446  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19447  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
19448
19449 #undef  ARM_VARIANT
19450 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
19451 #undef  THUMB_VARIANT
19452 #define THUMB_VARIANT  & arm_ext_v6t2
19453
19454  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
19455  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
19456      ldrd, t_ldstd),
19457  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
19458                                        ADDRGLDRS), ldrd, t_ldstd),
19459
19460  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19461  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19462
19463 #undef  ARM_VARIANT
19464 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
19465
19466  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
19467
19468 #undef  ARM_VARIANT
19469 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
19470 #undef  THUMB_VARIANT
19471 #define THUMB_VARIANT  & arm_ext_v6
19472
19473  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19474  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
19475  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19476  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19477  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
19478  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19479  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19480  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19481  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
19482  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
19483
19484 #undef  THUMB_VARIANT
19485 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19486
19487  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
19488  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19489                                       strex,  t_strex),
19490 #undef  THUMB_VARIANT
19491 #define THUMB_VARIANT  & arm_ext_v6t2
19492
19493  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19494  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
19495
19496  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
19497  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
19498
19499 /*  ARM V6 not included in V7M.  */
19500 #undef  THUMB_VARIANT
19501 #define THUMB_VARIANT  & arm_ext_v6_notm
19502  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19503  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19504   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
19505   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
19506  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19507  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
19508   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
19509  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
19510   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
19511  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19512  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19513  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
19514   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
19515   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
19516   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
19517   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
19518  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19519  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
19520  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
19521
19522 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
19523 #undef  THUMB_VARIANT
19524 #define THUMB_VARIANT  & arm_ext_v6_dsp
19525  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
19526  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
19527  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19528  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19529  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19530  /* Old name for QASX.  */
19531  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19532  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19533  /* Old name for QSAX.  */
19534  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19535  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19536  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19537  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19538  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19539  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19540  /* Old name for SASX.  */
19541  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19542  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19543  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19544  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19545  /* Old name for SHASX.  */
19546  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19547  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19548  /* Old name for SHSAX.  */
19549  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19550  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19551  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19552  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19553  /* Old name for SSAX.  */
19554  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19555  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19556  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19557  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19558  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19559  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19560  /* Old name for UASX.  */
19561  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19562  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19563  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19564  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19565  /* Old name for UHASX.  */
19566  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19567  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19568  /* Old name for UHSAX.  */
19569  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19570  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19571  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19572  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19573  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19574  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19575  /* Old name for UQASX.  */
19576  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19577  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19578  /* Old name for UQSAX.  */
19579  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
19580  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19581  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19582  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19583  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19584  /* Old name for USAX.  */
19585  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19586  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19587  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19588  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19589  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19590  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19591  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19592  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19593  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
19594  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
19595  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
19596  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19597  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19598  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19599  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19600  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19601  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19602  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19603  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
19604  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19605  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19606  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19607  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19608  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19609  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19610  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19611  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19612  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19613  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19614  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
19615  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
19616  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
19617  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
19618  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
19619
19620 #undef  ARM_VARIANT
19621 #define ARM_VARIANT   & arm_ext_v6k
19622 #undef  THUMB_VARIANT
19623 #define THUMB_VARIANT & arm_ext_v6k
19624
19625  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
19626  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
19627  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
19628  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
19629
19630 #undef  THUMB_VARIANT
19631 #define THUMB_VARIANT  & arm_ext_v6_notm
19632  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
19633                                       ldrexd, t_ldrexd),
19634  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
19635                                        RRnpcb), strexd, t_strexd),
19636
19637 #undef  THUMB_VARIANT
19638 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19639  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
19640      rd_rn,  rd_rn),
19641  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
19642      rd_rn,  rd_rn),
19643  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19644      strex, t_strexbh),
19645  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19646      strex, t_strexbh),
19647  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
19648
19649 #undef  ARM_VARIANT
19650 #define ARM_VARIANT    & arm_ext_sec
19651 #undef  THUMB_VARIANT
19652 #define THUMB_VARIANT  & arm_ext_sec
19653
19654  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
19655
19656 #undef  ARM_VARIANT
19657 #define ARM_VARIANT    & arm_ext_virt
19658 #undef  THUMB_VARIANT
19659 #define THUMB_VARIANT    & arm_ext_virt
19660
19661  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
19662  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
19663
19664 #undef  ARM_VARIANT
19665 #define ARM_VARIANT    & arm_ext_pan
19666 #undef  THUMB_VARIANT
19667 #define THUMB_VARIANT  & arm_ext_pan
19668
19669  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
19670
19671 #undef  ARM_VARIANT
19672 #define ARM_VARIANT    & arm_ext_v6t2
19673 #undef  THUMB_VARIANT
19674 #define THUMB_VARIANT  & arm_ext_v6t2
19675
19676  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
19677  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
19678  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19679  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19680
19681  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19682  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
19683
19684  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19685  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19686  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19687  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19688
19689 #undef  THUMB_VARIANT
19690 #define THUMB_VARIANT  & arm_ext_v6t2_v8m
19691  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
19692  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
19693
19694  /* Thumb-only instructions.  */
19695 #undef  ARM_VARIANT
19696 #define ARM_VARIANT NULL
19697   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
19698   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
19699
19700  /* ARM does not really have an IT instruction, so always allow it.
19701     The opcode is copied from Thumb in order to allow warnings in
19702     -mimplicit-it=[never | arm] modes.  */
19703 #undef  ARM_VARIANT
19704 #define ARM_VARIANT  & arm_ext_v1
19705 #undef  THUMB_VARIANT
19706 #define THUMB_VARIANT  & arm_ext_v6t2
19707
19708  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
19709  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
19710  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
19711  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
19712  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
19713  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
19714  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
19715  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
19716  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
19717  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
19718  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
19719  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
19720  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
19721  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
19722  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
19723  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
19724  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
19725  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
19726
19727  /* Thumb2 only instructions.  */
19728 #undef  ARM_VARIANT
19729 #define ARM_VARIANT  NULL
19730
19731  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19732  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19733  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
19734  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
19735  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
19736  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
19737
19738  /* Hardware division instructions.  */
19739 #undef  ARM_VARIANT
19740 #define ARM_VARIANT    & arm_ext_adiv
19741 #undef  THUMB_VARIANT
19742 #define THUMB_VARIANT  & arm_ext_div
19743
19744  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
19745  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
19746
19747  /* ARM V6M/V7 instructions.  */
19748 #undef  ARM_VARIANT
19749 #define ARM_VARIANT    & arm_ext_barrier
19750 #undef  THUMB_VARIANT
19751 #define THUMB_VARIANT  & arm_ext_barrier
19752
19753  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
19754  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
19755  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
19756
19757  /* ARM V7 instructions.  */
19758 #undef  ARM_VARIANT
19759 #define ARM_VARIANT    & arm_ext_v7
19760 #undef  THUMB_VARIANT
19761 #define THUMB_VARIANT  & arm_ext_v7
19762
19763  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
19764  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
19765
19766 #undef  ARM_VARIANT
19767 #define ARM_VARIANT    & arm_ext_mp
19768 #undef  THUMB_VARIANT
19769 #define THUMB_VARIANT  & arm_ext_mp
19770
19771  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
19772
19773  /* AArchv8 instructions.  */
19774 #undef  ARM_VARIANT
19775 #define ARM_VARIANT   & arm_ext_v8
19776
19777 /* Instructions shared between armv8-a and armv8-m.  */
19778 #undef  THUMB_VARIANT
19779 #define THUMB_VARIANT & arm_ext_atomics
19780
19781  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19782  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19783  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19784  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19785  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19786  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19787  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19788  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
19789  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19790  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
19791                                                         stlex,  t_stlex),
19792  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
19793                                                         stlex, t_stlex),
19794  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
19795                                                         stlex, t_stlex),
19796 #undef  THUMB_VARIANT
19797 #define THUMB_VARIANT & arm_ext_v8
19798
19799  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
19800  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
19801  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
19802                                                         ldrexd, t_ldrexd),
19803  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
19804                                                         strexd, t_strexd),
19805  /* ARMv8 T32 only.  */
19806 #undef  ARM_VARIANT
19807 #define ARM_VARIANT  NULL
19808  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
19809  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
19810  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
19811
19812   /* FP for ARMv8.  */
19813 #undef  ARM_VARIANT
19814 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
19815 #undef  THUMB_VARIANT
19816 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
19817
19818   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
19819   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
19820   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
19821   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
19822   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19823   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19824   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
19825   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
19826   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
19827   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
19828   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
19829   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
19830   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
19831   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
19832   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
19833   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
19834   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
19835
19836   /* Crypto v1 extensions.  */
19837 #undef  ARM_VARIANT
19838 #define ARM_VARIANT & fpu_crypto_ext_armv8
19839 #undef  THUMB_VARIANT
19840 #define THUMB_VARIANT & fpu_crypto_ext_armv8
19841
19842   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
19843   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
19844   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
19845   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
19846   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
19847   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
19848   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
19849   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
19850   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
19851   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
19852   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
19853   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
19854   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
19855   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
19856
19857 #undef  ARM_VARIANT
19858 #define ARM_VARIANT   & crc_ext_armv8
19859 #undef  THUMB_VARIANT
19860 #define THUMB_VARIANT & crc_ext_armv8
19861   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
19862   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
19863   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
19864   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
19865   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
19866   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
19867
19868  /* ARMv8.2 RAS extension.  */
19869 #undef  ARM_VARIANT
19870 #define ARM_VARIANT   & arm_ext_ras
19871 #undef  THUMB_VARIANT
19872 #define THUMB_VARIANT & arm_ext_ras
19873  TUE ("esb", 320f010, f3af8010, 0, (), noargs,  noargs),
19874
19875 #undef  ARM_VARIANT
19876 #define ARM_VARIANT   & arm_ext_v8_3
19877 #undef  THUMB_VARIANT
19878 #define THUMB_VARIANT & arm_ext_v8_3
19879  NCE (vjcvt, eb90bc0, 2, (RVS, RVD), vjcvt),
19880  NUF (vcmla, 0, 4, (RNDQ, RNDQ, RNDQ_RNSC, EXPi), vcmla),
19881  NUF (vcadd, 0, 4, (RNDQ, RNDQ, RNDQ, EXPi), vcadd),
19882
19883 #undef  ARM_VARIANT
19884 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
19885 #undef  THUMB_VARIANT
19886 #define THUMB_VARIANT NULL
19887
19888  cCE("wfs",     e200110, 1, (RR),            rd),
19889  cCE("rfs",     e300110, 1, (RR),            rd),
19890  cCE("wfc",     e400110, 1, (RR),            rd),
19891  cCE("rfc",     e500110, 1, (RR),            rd),
19892
19893  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19894  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19895  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19896  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19897
19898  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19899  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19900  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19901  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19902
19903  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
19904  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
19905  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
19906  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
19907  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
19908  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
19909  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
19910  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
19911  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
19912  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
19913  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
19914  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
19915
19916  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
19917  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
19918  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
19919  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
19920  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
19921  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
19922  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
19923  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
19924  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
19925  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
19926  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
19927  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
19928
19929  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
19930  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
19931  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
19932  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
19933  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
19934  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
19935  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
19936  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
19937  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
19938  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
19939  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
19940  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
19941
19942  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
19943  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
19944  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
19945  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
19946  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
19947  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
19948  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
19949  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
19950  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
19951  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
19952  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
19953  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
19954
19955  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
19956  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
19957  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
19958  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
19959  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
19960  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
19961  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
19962  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
19963  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
19964  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
19965  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
19966  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
19967
19968  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
19969  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
19970  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
19971  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
19972  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
19973  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
19974  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
19975  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
19976  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
19977  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
19978  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
19979  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
19980
19981  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
19982  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
19983  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
19984  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
19985  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
19986  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
19987  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
19988  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
19989  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
19990  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
19991  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
19992  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
19993
19994  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
19995  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
19996  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
19997  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
19998  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
19999  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
20000  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
20001  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
20002  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
20003  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
20004  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
20005  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
20006
20007  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
20008  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
20009  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
20010  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
20011  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
20012  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
20013  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
20014  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
20015  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
20016  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
20017  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
20018  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
20019
20020  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
20021  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
20022  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
20023  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
20024  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
20025  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
20026  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
20027  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
20028  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
20029  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
20030  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
20031  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
20032
20033  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
20034  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
20035  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
20036  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
20037  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
20038  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
20039  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
20040  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
20041  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
20042  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
20043  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
20044  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
20045
20046  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
20047  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
20048  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
20049  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
20050  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
20051  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
20052  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
20053  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
20054  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
20055  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
20056  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
20057  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
20058
20059  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
20060  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
20061  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
20062  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
20063  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
20064  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
20065  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
20066  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
20067  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
20068  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
20069  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
20070  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
20071
20072  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
20073  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
20074  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
20075  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
20076  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
20077  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
20078  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
20079  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
20080  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
20081  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
20082  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
20083  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
20084
20085  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
20086  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
20087  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
20088  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
20089  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
20090  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
20091  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
20092  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
20093  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
20094  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
20095  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
20096  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
20097
20098  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
20099  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
20100  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
20101  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
20102  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
20103  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
20104  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
20105  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
20106  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
20107  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
20108  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
20109  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
20110
20111  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
20112  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
20113  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
20114  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
20115  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
20116  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20117  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20118  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20119  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
20120  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
20121  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
20122  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
20123
20124  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
20125  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
20126  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
20127  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
20128  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
20129  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20130  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20131  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20132  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
20133  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
20134  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
20135  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
20136
20137  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
20138  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
20139  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
20140  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
20141  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
20142  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20143  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20144  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20145  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
20146  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
20147  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
20148  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
20149
20150  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
20151  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
20152  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
20153  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
20154  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
20155  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20156  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20157  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20158  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
20159  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
20160  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
20161  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
20162
20163  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
20164  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
20165  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
20166  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
20167  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
20168  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20169  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20170  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20171  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
20172  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
20173  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
20174  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
20175
20176  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
20177  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
20178  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
20179  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
20180  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
20181  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20182  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20183  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20184  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
20185  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
20186  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
20187  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
20188
20189  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
20190  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
20191  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
20192  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
20193  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
20194  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20195  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20196  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20197  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
20198  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
20199  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
20200  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
20201
20202  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
20203  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
20204  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
20205  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
20206  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
20207  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20208  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20209  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20210  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
20211  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
20212  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
20213  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
20214
20215  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
20216  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
20217  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
20218  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
20219  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
20220  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20221  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20222  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20223  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
20224  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
20225  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
20226  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
20227
20228  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
20229  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
20230  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
20231  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
20232  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
20233  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20234  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20235  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20236  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
20237  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
20238  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
20239  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
20240
20241  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20242  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20243  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20244  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20245  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20246  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20247  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20248  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20249  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20250  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20251  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20252  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20253
20254  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20255  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20256  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20257  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20258  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20259  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20260  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20261  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20262  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20263  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20264  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20265  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20266
20267  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
20268  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
20269  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
20270  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
20271  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
20272  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
20273  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
20274  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
20275  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
20276  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
20277  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
20278  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
20279
20280  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
20281  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
20282  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
20283  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
20284
20285  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
20286  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
20287  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
20288  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
20289  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
20290  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
20291  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
20292  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
20293  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
20294  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
20295  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
20296  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
20297
20298   /* The implementation of the FIX instruction is broken on some
20299      assemblers, in that it accepts a precision specifier as well as a
20300      rounding specifier, despite the fact that this is meaningless.
20301      To be more compatible, we accept it as well, though of course it
20302      does not set any bits.  */
20303  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
20304  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
20305  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
20306  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
20307  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
20308  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
20309  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
20310  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
20311  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
20312  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
20313  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
20314  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
20315  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
20316
20317   /* Instructions that were new with the real FPA, call them V2.  */
20318 #undef  ARM_VARIANT
20319 #define ARM_VARIANT  & fpu_fpa_ext_v2
20320
20321  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20322  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20323  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20324  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20325  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20326  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
20327
20328 #undef  ARM_VARIANT
20329 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
20330
20331   /* Moves and type conversions.  */
20332  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
20333  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
20334  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
20335  cCE("fmstat",  ef1fa10, 0, (),               noargs),
20336  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
20337  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
20338  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20339  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
20340  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20341  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20342  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
20343  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20344  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
20345  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
20346
20347   /* Memory operations.  */
20348  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20349  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
20350  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20351  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20352  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20353  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20354  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20355  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20356  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20357  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20358  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20359  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
20360  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20361  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
20362  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20363  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
20364  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20365  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
20366
20367   /* Monadic operations.  */
20368  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20369  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
20370  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20371
20372   /* Dyadic operations.  */
20373  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20374  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20375  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20376  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20377  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20378  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20379  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20380  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20381  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20382
20383   /* Comparisons.  */
20384  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
20385  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
20386  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
20387  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
20388
20389  /* Double precision load/store are still present on single precision
20390     implementations.  */
20391  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20392  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
20393  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20394  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20395  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20396  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20397  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20398  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
20399  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20400  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
20401
20402 #undef  ARM_VARIANT
20403 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
20404
20405   /* Moves and type conversions.  */
20406  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20407  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20408  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20409  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20410  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
20411  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20412  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
20413  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20414  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
20415  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20416  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20417  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20418  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
20419
20420   /* Monadic operations.  */
20421  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20422  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20423  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20424
20425   /* Dyadic operations.  */
20426  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20427  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20428  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20429  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20430  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20431  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20432  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20433  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20434  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20435
20436   /* Comparisons.  */
20437  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
20438  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
20439  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
20440  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
20441
20442 #undef  ARM_VARIANT
20443 #define ARM_VARIANT  & fpu_vfp_ext_v2
20444
20445  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
20446  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
20447  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
20448  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
20449
20450 /* Instructions which may belong to either the Neon or VFP instruction sets.
20451    Individual encoder functions perform additional architecture checks.  */
20452 #undef  ARM_VARIANT
20453 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
20454 #undef  THUMB_VARIANT
20455 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
20456
20457   /* These mnemonics are unique to VFP.  */
20458  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
20459  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
20460  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20461  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20462  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20463  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20464  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
20465  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
20466  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
20467  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
20468
20469   /* Mnemonics shared by Neon and VFP.  */
20470  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
20471  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20472  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
20473
20474  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20475  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
20476
20477  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20478  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
20479
20480  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20481  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20482  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20483  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20484  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20485  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
20486  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20487  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
20488
20489  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
20490  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
20491  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
20492  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
20493
20494
20495   /* NOTE: All VMOV encoding is special-cased!  */
20496  NCE(vmov,      0,       1, (VMOV), neon_mov),
20497  NCE(vmovq,     0,       1, (VMOV), neon_mov),
20498
20499 #undef  ARM_VARIANT
20500 #define ARM_VARIANT    & arm_ext_fp16
20501 #undef  THUMB_VARIANT
20502 #define THUMB_VARIANT  & arm_ext_fp16
20503  /* New instructions added from v8.2, allowing the extraction and insertion of
20504     the upper 16 bits of a 32-bit vector register.  */
20505  NCE (vmovx,     eb00a40,       2, (RVS, RVS), neon_movhf),
20506  NCE (vins,      eb00ac0,       2, (RVS, RVS), neon_movhf),
20507
20508 #undef  THUMB_VARIANT
20509 #define THUMB_VARIANT  & fpu_neon_ext_v1
20510 #undef  ARM_VARIANT
20511 #define ARM_VARIANT    & fpu_neon_ext_v1
20512
20513   /* Data processing with three registers of the same length.  */
20514   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
20515  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
20516  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
20517  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20518  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20519  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20520  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20521  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
20522  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
20523   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
20524  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20525  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20526  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
20527  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
20528  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20529  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20530  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
20531  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
20532   /* If not immediate, fall back to neon_dyadic_i64_su.
20533      shl_imm should accept I8 I16 I32 I64,
20534      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
20535  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
20536  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
20537  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
20538  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
20539   /* Logic ops, types optional & ignored.  */
20540  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20541  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20542  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20543  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20544  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20545  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20546  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
20547  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
20548  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
20549  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
20550   /* Bitfield ops, untyped.  */
20551  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20552  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20553  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20554  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20555  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
20556  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
20557   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F16 F32.  */
20558  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20559  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20560  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20561  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20562  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
20563  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
20564   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
20565      back to neon_dyadic_if_su.  */
20566  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20567  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20568  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
20569  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
20570  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20571  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20572  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
20573  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
20574   /* Comparison. Type I8 I16 I32 F32.  */
20575  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
20576  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
20577   /* As above, D registers only.  */
20578  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20579  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
20580   /* Int and float variants, signedness unimportant.  */
20581  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20582  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
20583  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
20584   /* Add/sub take types I8 I16 I32 I64 F32.  */
20585  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20586  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
20587   /* vtst takes sizes 8, 16, 32.  */
20588  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
20589  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
20590   /* VMUL takes I8 I16 I32 F32 P8.  */
20591  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
20592   /* VQD{R}MULH takes S16 S32.  */
20593  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20594  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20595  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
20596  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
20597  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20598  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20599  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
20600  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
20601  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20602  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20603  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
20604  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
20605  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20606  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20607  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
20608  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
20609  /* ARM v8.1 extension.  */
20610  nUF (vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20611  nUF (vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20612  nUF (vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qrdmlah),
20613  nUF (vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qrdmlah),
20614
20615   /* Two address, int/float. Types S8 S16 S32 F32.  */
20616  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
20617  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
20618
20619   /* Data processing with two registers and a shift amount.  */
20620   /* Right shifts, and variants with rounding.
20621      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
20622  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20623  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20624  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
20625  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
20626  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20627  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20628  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
20629  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
20630   /* Shift and insert. Sizes accepted 8 16 32 64.  */
20631  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
20632  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
20633  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
20634  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
20635   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
20636  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
20637  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
20638   /* Right shift immediate, saturating & narrowing, with rounding variants.
20639      Types accepted S16 S32 S64 U16 U32 U64.  */
20640  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20641  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20642   /* As above, unsigned. Types accepted S16 S32 S64.  */
20643  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20644  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20645   /* Right shift narrowing. Types accepted I16 I32 I64.  */
20646  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20647  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20648   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
20649  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
20650   /* CVT with optional immediate for fixed-point variant.  */
20651  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
20652
20653  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
20654  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
20655
20656   /* Data processing, three registers of different lengths.  */
20657   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
20658  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
20659  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
20660  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
20661  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
20662   /* If not scalar, fall back to neon_dyadic_long.
20663      Vector types as above, scalar types S16 S32 U16 U32.  */
20664  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20665  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20666   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
20667  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20668  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20669   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
20670  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20671  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20672  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20673  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20674   /* Saturating doubling multiplies. Types S16 S32.  */
20675  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20676  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20677  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20678   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
20679      S16 S32 U16 U32.  */
20680  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
20681
20682   /* Extract. Size 8.  */
20683  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
20684  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
20685
20686   /* Two registers, miscellaneous.  */
20687   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
20688  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
20689  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
20690  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
20691  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
20692  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
20693  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
20694   /* Vector replicate. Sizes 8 16 32.  */
20695  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
20696  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
20697   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
20698  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
20699   /* VMOVN. Types I16 I32 I64.  */
20700  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
20701   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
20702  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
20703   /* VQMOVUN. Types S16 S32 S64.  */
20704  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
20705   /* VZIP / VUZP. Sizes 8 16 32.  */
20706  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20707  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
20708  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20709  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
20710   /* VQABS / VQNEG. Types S8 S16 S32.  */
20711  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20712  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20713  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20714  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20715   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
20716  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
20717  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
20718  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
20719  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
20720   /* Reciprocal estimates.  Types U32 F16 F32.  */
20721  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
20722  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
20723  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
20724  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
20725   /* VCLS. Types S8 S16 S32.  */
20726  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
20727  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
20728   /* VCLZ. Types I8 I16 I32.  */
20729  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
20730  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
20731   /* VCNT. Size 8.  */
20732  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
20733  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
20734   /* Two address, untyped.  */
20735  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
20736  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
20737   /* VTRN. Sizes 8 16 32.  */
20738  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
20739  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
20740
20741   /* Table lookup. Size 8.  */
20742  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20743  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20744
20745 #undef  THUMB_VARIANT
20746 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
20747 #undef  ARM_VARIANT
20748 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
20749
20750   /* Neon element/structure load/store.  */
20751  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20752  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20753  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20754  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20755  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20756  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20757  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20758  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20759
20760 #undef  THUMB_VARIANT
20761 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
20762 #undef  ARM_VARIANT
20763 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
20764  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
20765  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20766  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20767  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20768  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20769  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20770  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20771  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20772  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20773
20774 #undef  THUMB_VARIANT
20775 #define THUMB_VARIANT  & fpu_vfp_ext_v3
20776 #undef  ARM_VARIANT
20777 #define ARM_VARIANT    & fpu_vfp_ext_v3
20778
20779  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
20780  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20781  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20782  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20783  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20784  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20785  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20786  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20787  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20788
20789 #undef  ARM_VARIANT
20790 #define ARM_VARIANT    & fpu_vfp_ext_fma
20791 #undef  THUMB_VARIANT
20792 #define THUMB_VARIANT  & fpu_vfp_ext_fma
20793  /* Mnemonics shared by Neon and VFP.  These are included in the
20794     VFP FMA variant; NEON and VFP FMA always includes the NEON
20795     FMA instructions.  */
20796  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20797  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20798  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
20799     the v form should always be used.  */
20800  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20801  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20802  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20803  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20804  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20805  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20806
20807 #undef THUMB_VARIANT
20808 #undef  ARM_VARIANT
20809 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
20810
20811  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20812  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20813  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20814  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20815  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20816  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20817  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
20818  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
20819
20820 #undef  ARM_VARIANT
20821 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
20822
20823  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
20824  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
20825  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
20826  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
20827  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
20828  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
20829  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
20830  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
20831  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
20832  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20833  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20834  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20835  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20836  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20837  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20838  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20839  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20840  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20841  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
20842  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
20843  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20844  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20845  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20846  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20847  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20848  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20849  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
20850  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
20851  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
20852  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
20853  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
20854  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
20855  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
20856  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
20857  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
20858  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
20859  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
20860  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20861  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20862  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20863  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20864  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20865  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20866  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20867  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20868  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20869  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
20870  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20871  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20872  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20873  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20874  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20875  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20876  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20877  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20878  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20879  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20880  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20881  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20882  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20883  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20884  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20885  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20886  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20887  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20888  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20889  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20890  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20891  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20892  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20893  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20894  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20895  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20896  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20897  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20898  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20899  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20900  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20901  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20902  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20903  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20904  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20905  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20906  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20907  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20908  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20909  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20910  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20911  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
20912  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20913  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20914  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20915  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20916  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20917  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20918  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20919  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20920  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20921  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20922  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20923  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20924  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20925  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20926  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20927  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20928  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20929  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20930  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20931  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20932  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20933  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
20934  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20935  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20936  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20937  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20938  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20939  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20940  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20941  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20942  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20943  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20944  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20945  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20946  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20947  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20948  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20949  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20950  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20951  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20952  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20953  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20954  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20955  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20956  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20957  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20958  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20959  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20960  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20961  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20962  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20963  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20964  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20965  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
20966  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
20967  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
20968  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
20969  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
20970  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
20971  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20972  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20973  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20974  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
20975  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
20976  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
20977  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
20978  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
20979  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
20980  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20981  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20982  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20983  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20984  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
20985
20986 #undef  ARM_VARIANT
20987 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
20988
20989  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
20990  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
20991  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
20992  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
20993  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
20994  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
20995  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20996  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20997  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20998  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20999  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21000  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21001  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21002  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21003  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21004  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21005  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21006  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21007  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21008  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21009  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
21010  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21011  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21012  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21013  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21014  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21015  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21016  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21017  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21018  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21019  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21020  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21021  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21022  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21023  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21024  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21025  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21026  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21027  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21028  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21029  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21030  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21031  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21032  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21033  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21034  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21035  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21036  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21037  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21038  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21039  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21040  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21041  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21042  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21043  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21044  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21045  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
21046
21047 #undef  ARM_VARIANT
21048 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
21049
21050  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21051  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21052  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21053  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21054  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
21055  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
21056  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
21057  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
21058  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
21059  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
21060  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
21061  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
21062  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
21063  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
21064  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
21065  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
21066  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
21067  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
21068  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
21069  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
21070  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
21071  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
21072  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
21073  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
21074  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
21075  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
21076  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
21077  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
21078  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
21079  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
21080  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
21081  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
21082  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
21083  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
21084  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
21085  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
21086  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
21087  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
21088  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
21089  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
21090  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
21091  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
21092  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
21093  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
21094  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
21095  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
21096  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
21097  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
21098  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
21099  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
21100  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
21101  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
21102  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
21103  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
21104  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
21105  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21106  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
21107  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
21108  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
21109  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
21110  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
21111  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
21112  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
21113  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
21114  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21115  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21116  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21117  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21118  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21119  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
21120  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21121  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
21122  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21123  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
21124  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21125  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
21126
21127  /* ARMv8-M instructions.  */
21128 #undef  ARM_VARIANT
21129 #define ARM_VARIANT NULL
21130 #undef  THUMB_VARIANT
21131 #define THUMB_VARIANT & arm_ext_v8m
21132  TUE("sg", 0, e97fe97f, 0, (), 0, noargs),
21133  TUE("blxns", 0, 4784, 1, (RRnpc), 0, t_blx),
21134  TUE("bxns", 0, 4704, 1, (RRnpc), 0, t_bx),
21135  TUE("tt", 0, e840f000, 2, (RRnpc, RRnpc), 0, tt),
21136  TUE("ttt", 0, e840f040, 2, (RRnpc, RRnpc), 0, tt),
21137  TUE("tta", 0, e840f080, 2, (RRnpc, RRnpc), 0, tt),
21138  TUE("ttat", 0, e840f0c0, 2, (RRnpc, RRnpc), 0, tt),
21139
21140  /* FP for ARMv8-M Mainline.  Enabled for ARMv8-M Mainline because the
21141     instructions behave as nop if no VFP is present.  */
21142 #undef  THUMB_VARIANT
21143 #define THUMB_VARIANT & arm_ext_v8m_main
21144  TUEc("vlldm",  0,       ec300a00, 1, (RRnpc),  rn),
21145  TUEc("vlstm",  0,       ec200a00, 1, (RRnpc),  rn),
21146 };
21147 #undef ARM_VARIANT
21148 #undef THUMB_VARIANT
21149 #undef TCE
21150 #undef TUE
21151 #undef TUF
21152 #undef TCC
21153 #undef cCE
21154 #undef cCL
21155 #undef C3E
21156 #undef CE
21157 #undef CM
21158 #undef UE
21159 #undef UF
21160 #undef UT
21161 #undef NUF
21162 #undef nUF
21163 #undef NCE
21164 #undef nCE
21165 #undef OPS0
21166 #undef OPS1
21167 #undef OPS2
21168 #undef OPS3
21169 #undef OPS4
21170 #undef OPS5
21171 #undef OPS6
21172 #undef do_0
21173 \f
21174 /* MD interface: bits in the object file.  */
21175
21176 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
21177    for use in the a.out file, and stores them in the array pointed to by buf.
21178    This knows about the endian-ness of the target machine and does
21179    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
21180    2 (short) and 4 (long)  Floating numbers are put out as a series of
21181    LITTLENUMS (shorts, here at least).  */
21182
21183 void
21184 md_number_to_chars (char * buf, valueT val, int n)
21185 {
21186   if (target_big_endian)
21187     number_to_chars_bigendian (buf, val, n);
21188   else
21189     number_to_chars_littleendian (buf, val, n);
21190 }
21191
21192 static valueT
21193 md_chars_to_number (char * buf, int n)
21194 {
21195   valueT result = 0;
21196   unsigned char * where = (unsigned char *) buf;
21197
21198   if (target_big_endian)
21199     {
21200       while (n--)
21201         {
21202           result <<= 8;
21203           result |= (*where++ & 255);
21204         }
21205     }
21206   else
21207     {
21208       while (n--)
21209         {
21210           result <<= 8;
21211           result |= (where[n] & 255);
21212         }
21213     }
21214
21215   return result;
21216 }
21217
21218 /* MD interface: Sections.  */
21219
21220 /* Calculate the maximum variable size (i.e., excluding fr_fix)
21221    that an rs_machine_dependent frag may reach.  */
21222
21223 unsigned int
21224 arm_frag_max_var (fragS *fragp)
21225 {
21226   /* We only use rs_machine_dependent for variable-size Thumb instructions,
21227      which are either THUMB_SIZE (2) or INSN_SIZE (4).
21228
21229      Note that we generate relaxable instructions even for cases that don't
21230      really need it, like an immediate that's a trivial constant.  So we're
21231      overestimating the instruction size for some of those cases.  Rather
21232      than putting more intelligence here, it would probably be better to
21233      avoid generating a relaxation frag in the first place when it can be
21234      determined up front that a short instruction will suffice.  */
21235
21236   gas_assert (fragp->fr_type == rs_machine_dependent);
21237   return INSN_SIZE;
21238 }
21239
21240 /* Estimate the size of a frag before relaxing.  Assume everything fits in
21241    2 bytes.  */
21242
21243 int
21244 md_estimate_size_before_relax (fragS * fragp,
21245                                segT    segtype ATTRIBUTE_UNUSED)
21246 {
21247   fragp->fr_var = 2;
21248   return 2;
21249 }
21250
21251 /* Convert a machine dependent frag.  */
21252
21253 void
21254 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
21255 {
21256   unsigned long insn;
21257   unsigned long old_op;
21258   char *buf;
21259   expressionS exp;
21260   fixS *fixp;
21261   int reloc_type;
21262   int pc_rel;
21263   int opcode;
21264
21265   buf = fragp->fr_literal + fragp->fr_fix;
21266
21267   old_op = bfd_get_16(abfd, buf);
21268   if (fragp->fr_symbol)
21269     {
21270       exp.X_op = O_symbol;
21271       exp.X_add_symbol = fragp->fr_symbol;
21272     }
21273   else
21274     {
21275       exp.X_op = O_constant;
21276     }
21277   exp.X_add_number = fragp->fr_offset;
21278   opcode = fragp->fr_subtype;
21279   switch (opcode)
21280     {
21281     case T_MNEM_ldr_pc:
21282     case T_MNEM_ldr_pc2:
21283     case T_MNEM_ldr_sp:
21284     case T_MNEM_str_sp:
21285     case T_MNEM_ldr:
21286     case T_MNEM_ldrb:
21287     case T_MNEM_ldrh:
21288     case T_MNEM_str:
21289     case T_MNEM_strb:
21290     case T_MNEM_strh:
21291       if (fragp->fr_var == 4)
21292         {
21293           insn = THUMB_OP32 (opcode);
21294           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
21295             {
21296               insn |= (old_op & 0x700) << 4;
21297             }
21298           else
21299             {
21300               insn |= (old_op & 7) << 12;
21301               insn |= (old_op & 0x38) << 13;
21302             }
21303           insn |= 0x00000c00;
21304           put_thumb32_insn (buf, insn);
21305           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
21306         }
21307       else
21308         {
21309           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
21310         }
21311       pc_rel = (opcode == T_MNEM_ldr_pc2);
21312       break;
21313     case T_MNEM_adr:
21314       if (fragp->fr_var == 4)
21315         {
21316           insn = THUMB_OP32 (opcode);
21317           insn |= (old_op & 0xf0) << 4;
21318           put_thumb32_insn (buf, insn);
21319           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
21320         }
21321       else
21322         {
21323           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21324           exp.X_add_number -= 4;
21325         }
21326       pc_rel = 1;
21327       break;
21328     case T_MNEM_mov:
21329     case T_MNEM_movs:
21330     case T_MNEM_cmp:
21331     case T_MNEM_cmn:
21332       if (fragp->fr_var == 4)
21333         {
21334           int r0off = (opcode == T_MNEM_mov
21335                        || opcode == T_MNEM_movs) ? 0 : 8;
21336           insn = THUMB_OP32 (opcode);
21337           insn = (insn & 0xe1ffffff) | 0x10000000;
21338           insn |= (old_op & 0x700) << r0off;
21339           put_thumb32_insn (buf, insn);
21340           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21341         }
21342       else
21343         {
21344           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
21345         }
21346       pc_rel = 0;
21347       break;
21348     case T_MNEM_b:
21349       if (fragp->fr_var == 4)
21350         {
21351           insn = THUMB_OP32(opcode);
21352           put_thumb32_insn (buf, insn);
21353           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
21354         }
21355       else
21356         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
21357       pc_rel = 1;
21358       break;
21359     case T_MNEM_bcond:
21360       if (fragp->fr_var == 4)
21361         {
21362           insn = THUMB_OP32(opcode);
21363           insn |= (old_op & 0xf00) << 14;
21364           put_thumb32_insn (buf, insn);
21365           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
21366         }
21367       else
21368         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
21369       pc_rel = 1;
21370       break;
21371     case T_MNEM_add_sp:
21372     case T_MNEM_add_pc:
21373     case T_MNEM_inc_sp:
21374     case T_MNEM_dec_sp:
21375       if (fragp->fr_var == 4)
21376         {
21377           /* ??? Choose between add and addw.  */
21378           insn = THUMB_OP32 (opcode);
21379           insn |= (old_op & 0xf0) << 4;
21380           put_thumb32_insn (buf, insn);
21381           if (opcode == T_MNEM_add_pc)
21382             reloc_type = BFD_RELOC_ARM_T32_IMM12;
21383           else
21384             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21385         }
21386       else
21387         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21388       pc_rel = 0;
21389       break;
21390
21391     case T_MNEM_addi:
21392     case T_MNEM_addis:
21393     case T_MNEM_subi:
21394     case T_MNEM_subis:
21395       if (fragp->fr_var == 4)
21396         {
21397           insn = THUMB_OP32 (opcode);
21398           insn |= (old_op & 0xf0) << 4;
21399           insn |= (old_op & 0xf) << 16;
21400           put_thumb32_insn (buf, insn);
21401           if (insn & (1 << 20))
21402             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
21403           else
21404             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
21405         }
21406       else
21407         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
21408       pc_rel = 0;
21409       break;
21410     default:
21411       abort ();
21412     }
21413   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
21414                       (enum bfd_reloc_code_real) reloc_type);
21415   fixp->fx_file = fragp->fr_file;
21416   fixp->fx_line = fragp->fr_line;
21417   fragp->fr_fix += fragp->fr_var;
21418
21419   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
21420   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
21421       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
21422     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
21423 }
21424
21425 /* Return the size of a relaxable immediate operand instruction.
21426    SHIFT and SIZE specify the form of the allowable immediate.  */
21427 static int
21428 relax_immediate (fragS *fragp, int size, int shift)
21429 {
21430   offsetT offset;
21431   offsetT mask;
21432   offsetT low;
21433
21434   /* ??? Should be able to do better than this.  */
21435   if (fragp->fr_symbol)
21436     return 4;
21437
21438   low = (1 << shift) - 1;
21439   mask = (1 << (shift + size)) - (1 << shift);
21440   offset = fragp->fr_offset;
21441   /* Force misaligned offsets to 32-bit variant.  */
21442   if (offset & low)
21443     return 4;
21444   if (offset & ~mask)
21445     return 4;
21446   return 2;
21447 }
21448
21449 /* Get the address of a symbol during relaxation.  */
21450 static addressT
21451 relaxed_symbol_addr (fragS *fragp, long stretch)
21452 {
21453   fragS *sym_frag;
21454   addressT addr;
21455   symbolS *sym;
21456
21457   sym = fragp->fr_symbol;
21458   sym_frag = symbol_get_frag (sym);
21459   know (S_GET_SEGMENT (sym) != absolute_section
21460         || sym_frag == &zero_address_frag);
21461   addr = S_GET_VALUE (sym) + fragp->fr_offset;
21462
21463   /* If frag has yet to be reached on this pass, assume it will
21464      move by STRETCH just as we did.  If this is not so, it will
21465      be because some frag between grows, and that will force
21466      another pass.  */
21467
21468   if (stretch != 0
21469       && sym_frag->relax_marker != fragp->relax_marker)
21470     {
21471       fragS *f;
21472
21473       /* Adjust stretch for any alignment frag.  Note that if have
21474          been expanding the earlier code, the symbol may be
21475          defined in what appears to be an earlier frag.  FIXME:
21476          This doesn't handle the fr_subtype field, which specifies
21477          a maximum number of bytes to skip when doing an
21478          alignment.  */
21479       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
21480         {
21481           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
21482             {
21483               if (stretch < 0)
21484                 stretch = - ((- stretch)
21485                              & ~ ((1 << (int) f->fr_offset) - 1));
21486               else
21487                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
21488               if (stretch == 0)
21489                 break;
21490             }
21491         }
21492       if (f != NULL)
21493         addr += stretch;
21494     }
21495
21496   return addr;
21497 }
21498
21499 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
21500    load.  */
21501 static int
21502 relax_adr (fragS *fragp, asection *sec, long stretch)
21503 {
21504   addressT addr;
21505   offsetT val;
21506
21507   /* Assume worst case for symbols not known to be in the same section.  */
21508   if (fragp->fr_symbol == NULL
21509       || !S_IS_DEFINED (fragp->fr_symbol)
21510       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21511       || S_IS_WEAK (fragp->fr_symbol))
21512     return 4;
21513
21514   val = relaxed_symbol_addr (fragp, stretch);
21515   addr = fragp->fr_address + fragp->fr_fix;
21516   addr = (addr + 4) & ~3;
21517   /* Force misaligned targets to 32-bit variant.  */
21518   if (val & 3)
21519     return 4;
21520   val -= addr;
21521   if (val < 0 || val > 1020)
21522     return 4;
21523   return 2;
21524 }
21525
21526 /* Return the size of a relaxable add/sub immediate instruction.  */
21527 static int
21528 relax_addsub (fragS *fragp, asection *sec)
21529 {
21530   char *buf;
21531   int op;
21532
21533   buf = fragp->fr_literal + fragp->fr_fix;
21534   op = bfd_get_16(sec->owner, buf);
21535   if ((op & 0xf) == ((op >> 4) & 0xf))
21536     return relax_immediate (fragp, 8, 0);
21537   else
21538     return relax_immediate (fragp, 3, 0);
21539 }
21540
21541 /* Return TRUE iff the definition of symbol S could be pre-empted
21542    (overridden) at link or load time.  */
21543 static bfd_boolean
21544 symbol_preemptible (symbolS *s)
21545 {
21546   /* Weak symbols can always be pre-empted.  */
21547   if (S_IS_WEAK (s))
21548     return TRUE;
21549
21550   /* Non-global symbols cannot be pre-empted. */
21551   if (! S_IS_EXTERNAL (s))
21552     return FALSE;
21553
21554 #ifdef OBJ_ELF
21555   /* In ELF, a global symbol can be marked protected, or private.  In that
21556      case it can't be pre-empted (other definitions in the same link unit
21557      would violate the ODR).  */
21558   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
21559     return FALSE;
21560 #endif
21561
21562   /* Other global symbols might be pre-empted.  */
21563   return TRUE;
21564 }
21565
21566 /* Return the size of a relaxable branch instruction.  BITS is the
21567    size of the offset field in the narrow instruction.  */
21568
21569 static int
21570 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
21571 {
21572   addressT addr;
21573   offsetT val;
21574   offsetT limit;
21575
21576   /* Assume worst case for symbols not known to be in the same section.  */
21577   if (!S_IS_DEFINED (fragp->fr_symbol)
21578       || sec != S_GET_SEGMENT (fragp->fr_symbol)
21579       || S_IS_WEAK (fragp->fr_symbol))
21580     return 4;
21581
21582 #ifdef OBJ_ELF
21583   /* A branch to a function in ARM state will require interworking.  */
21584   if (S_IS_DEFINED (fragp->fr_symbol)
21585       && ARM_IS_FUNC (fragp->fr_symbol))
21586       return 4;
21587 #endif
21588
21589   if (symbol_preemptible (fragp->fr_symbol))
21590     return 4;
21591
21592   val = relaxed_symbol_addr (fragp, stretch);
21593   addr = fragp->fr_address + fragp->fr_fix + 4;
21594   val -= addr;
21595
21596   /* Offset is a signed value *2 */
21597   limit = 1 << bits;
21598   if (val >= limit || val < -limit)
21599     return 4;
21600   return 2;
21601 }
21602
21603
21604 /* Relax a machine dependent frag.  This returns the amount by which
21605    the current size of the frag should change.  */
21606
21607 int
21608 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
21609 {
21610   int oldsize;
21611   int newsize;
21612
21613   oldsize = fragp->fr_var;
21614   switch (fragp->fr_subtype)
21615     {
21616     case T_MNEM_ldr_pc2:
21617       newsize = relax_adr (fragp, sec, stretch);
21618       break;
21619     case T_MNEM_ldr_pc:
21620     case T_MNEM_ldr_sp:
21621     case T_MNEM_str_sp:
21622       newsize = relax_immediate (fragp, 8, 2);
21623       break;
21624     case T_MNEM_ldr:
21625     case T_MNEM_str:
21626       newsize = relax_immediate (fragp, 5, 2);
21627       break;
21628     case T_MNEM_ldrh:
21629     case T_MNEM_strh:
21630       newsize = relax_immediate (fragp, 5, 1);
21631       break;
21632     case T_MNEM_ldrb:
21633     case T_MNEM_strb:
21634       newsize = relax_immediate (fragp, 5, 0);
21635       break;
21636     case T_MNEM_adr:
21637       newsize = relax_adr (fragp, sec, stretch);
21638       break;
21639     case T_MNEM_mov:
21640     case T_MNEM_movs:
21641     case T_MNEM_cmp:
21642     case T_MNEM_cmn:
21643       newsize = relax_immediate (fragp, 8, 0);
21644       break;
21645     case T_MNEM_b:
21646       newsize = relax_branch (fragp, sec, 11, stretch);
21647       break;
21648     case T_MNEM_bcond:
21649       newsize = relax_branch (fragp, sec, 8, stretch);
21650       break;
21651     case T_MNEM_add_sp:
21652     case T_MNEM_add_pc:
21653       newsize = relax_immediate (fragp, 8, 2);
21654       break;
21655     case T_MNEM_inc_sp:
21656     case T_MNEM_dec_sp:
21657       newsize = relax_immediate (fragp, 7, 2);
21658       break;
21659     case T_MNEM_addi:
21660     case T_MNEM_addis:
21661     case T_MNEM_subi:
21662     case T_MNEM_subis:
21663       newsize = relax_addsub (fragp, sec);
21664       break;
21665     default:
21666       abort ();
21667     }
21668
21669   fragp->fr_var = newsize;
21670   /* Freeze wide instructions that are at or before the same location as
21671      in the previous pass.  This avoids infinite loops.
21672      Don't freeze them unconditionally because targets may be artificially
21673      misaligned by the expansion of preceding frags.  */
21674   if (stretch <= 0 && newsize > 2)
21675     {
21676       md_convert_frag (sec->owner, sec, fragp);
21677       frag_wane (fragp);
21678     }
21679
21680   return newsize - oldsize;
21681 }
21682
21683 /* Round up a section size to the appropriate boundary.  */
21684
21685 valueT
21686 md_section_align (segT   segment ATTRIBUTE_UNUSED,
21687                   valueT size)
21688 {
21689 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
21690   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
21691     {
21692       /* For a.out, force the section size to be aligned.  If we don't do
21693          this, BFD will align it for us, but it will not write out the
21694          final bytes of the section.  This may be a bug in BFD, but it is
21695          easier to fix it here since that is how the other a.out targets
21696          work.  */
21697       int align;
21698
21699       align = bfd_get_section_alignment (stdoutput, segment);
21700       size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
21701     }
21702 #endif
21703
21704   return size;
21705 }
21706
21707 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
21708    of an rs_align_code fragment.  */
21709
21710 void
21711 arm_handle_align (fragS * fragP)
21712 {
21713   static unsigned char const arm_noop[2][2][4] =
21714     {
21715       {  /* ARMv1 */
21716         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
21717         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
21718       },
21719       {  /* ARMv6k */
21720         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
21721         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
21722       },
21723     };
21724   static unsigned char const thumb_noop[2][2][2] =
21725     {
21726       {  /* Thumb-1 */
21727         {0xc0, 0x46},  /* LE */
21728         {0x46, 0xc0},  /* BE */
21729       },
21730       {  /* Thumb-2 */
21731         {0x00, 0xbf},  /* LE */
21732         {0xbf, 0x00}   /* BE */
21733       }
21734     };
21735   static unsigned char const wide_thumb_noop[2][4] =
21736     {  /* Wide Thumb-2 */
21737       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
21738       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
21739     };
21740
21741   unsigned bytes, fix, noop_size;
21742   char * p;
21743   const unsigned char * noop;
21744   const unsigned char *narrow_noop = NULL;
21745 #ifdef OBJ_ELF
21746   enum mstate state;
21747 #endif
21748
21749   if (fragP->fr_type != rs_align_code)
21750     return;
21751
21752   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
21753   p = fragP->fr_literal + fragP->fr_fix;
21754   fix = 0;
21755
21756   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
21757     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
21758
21759   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
21760
21761   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
21762     {
21763       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21764                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
21765         {
21766           narrow_noop = thumb_noop[1][target_big_endian];
21767           noop = wide_thumb_noop[target_big_endian];
21768         }
21769       else
21770         noop = thumb_noop[0][target_big_endian];
21771       noop_size = 2;
21772 #ifdef OBJ_ELF
21773       state = MAP_THUMB;
21774 #endif
21775     }
21776   else
21777     {
21778       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21779                                            ? selected_cpu : arm_arch_none,
21780                                            arm_ext_v6k) != 0]
21781                      [target_big_endian];
21782       noop_size = 4;
21783 #ifdef OBJ_ELF
21784       state = MAP_ARM;
21785 #endif
21786     }
21787
21788   fragP->fr_var = noop_size;
21789
21790   if (bytes & (noop_size - 1))
21791     {
21792       fix = bytes & (noop_size - 1);
21793 #ifdef OBJ_ELF
21794       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
21795 #endif
21796       memset (p, 0, fix);
21797       p += fix;
21798       bytes -= fix;
21799     }
21800
21801   if (narrow_noop)
21802     {
21803       if (bytes & noop_size)
21804         {
21805           /* Insert a narrow noop.  */
21806           memcpy (p, narrow_noop, noop_size);
21807           p += noop_size;
21808           bytes -= noop_size;
21809           fix += noop_size;
21810         }
21811
21812       /* Use wide noops for the remainder */
21813       noop_size = 4;
21814     }
21815
21816   while (bytes >= noop_size)
21817     {
21818       memcpy (p, noop, noop_size);
21819       p += noop_size;
21820       bytes -= noop_size;
21821       fix += noop_size;
21822     }
21823
21824   fragP->fr_fix += fix;
21825 }
21826
21827 /* Called from md_do_align.  Used to create an alignment
21828    frag in a code section.  */
21829
21830 void
21831 arm_frag_align_code (int n, int max)
21832 {
21833   char * p;
21834
21835   /* We assume that there will never be a requirement
21836      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
21837   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
21838     {
21839       char err_msg[128];
21840
21841       sprintf (err_msg,
21842         _("alignments greater than %d bytes not supported in .text sections."),
21843         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
21844       as_fatal ("%s", err_msg);
21845     }
21846
21847   p = frag_var (rs_align_code,
21848                 MAX_MEM_FOR_RS_ALIGN_CODE,
21849                 1,
21850                 (relax_substateT) max,
21851                 (symbolS *) NULL,
21852                 (offsetT) n,
21853                 (char *) NULL);
21854   *p = 0;
21855 }
21856
21857 /* Perform target specific initialisation of a frag.
21858    Note - despite the name this initialisation is not done when the frag
21859    is created, but only when its type is assigned.  A frag can be created
21860    and used a long time before its type is set, so beware of assuming that
21861    this initialisation is performed first.  */
21862
21863 #ifndef OBJ_ELF
21864 void
21865 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
21866 {
21867   /* Record whether this frag is in an ARM or a THUMB area.  */
21868   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21869 }
21870
21871 #else /* OBJ_ELF is defined.  */
21872 void
21873 arm_init_frag (fragS * fragP, int max_chars)
21874 {
21875   int frag_thumb_mode;
21876
21877   /* If the current ARM vs THUMB mode has not already
21878      been recorded into this frag then do so now.  */
21879   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
21880     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21881
21882   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
21883
21884   /* Record a mapping symbol for alignment frags.  We will delete this
21885      later if the alignment ends up empty.  */
21886   switch (fragP->fr_type)
21887     {
21888     case rs_align:
21889     case rs_align_test:
21890     case rs_fill:
21891       mapping_state_2 (MAP_DATA, max_chars);
21892       break;
21893     case rs_align_code:
21894       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
21895       break;
21896     default:
21897       break;
21898     }
21899 }
21900
21901 /* When we change sections we need to issue a new mapping symbol.  */
21902
21903 void
21904 arm_elf_change_section (void)
21905 {
21906   /* Link an unlinked unwind index table section to the .text section.  */
21907   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
21908       && elf_linked_to_section (now_seg) == NULL)
21909     elf_linked_to_section (now_seg) = text_section;
21910 }
21911
21912 int
21913 arm_elf_section_type (const char * str, size_t len)
21914 {
21915   if (len == 5 && strncmp (str, "exidx", 5) == 0)
21916     return SHT_ARM_EXIDX;
21917
21918   return -1;
21919 }
21920 \f
21921 /* Code to deal with unwinding tables.  */
21922
21923 static void add_unwind_adjustsp (offsetT);
21924
21925 /* Generate any deferred unwind frame offset.  */
21926
21927 static void
21928 flush_pending_unwind (void)
21929 {
21930   offsetT offset;
21931
21932   offset = unwind.pending_offset;
21933   unwind.pending_offset = 0;
21934   if (offset != 0)
21935     add_unwind_adjustsp (offset);
21936 }
21937
21938 /* Add an opcode to this list for this function.  Two-byte opcodes should
21939    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
21940    order.  */
21941
21942 static void
21943 add_unwind_opcode (valueT op, int length)
21944 {
21945   /* Add any deferred stack adjustment.  */
21946   if (unwind.pending_offset)
21947     flush_pending_unwind ();
21948
21949   unwind.sp_restored = 0;
21950
21951   if (unwind.opcode_count + length > unwind.opcode_alloc)
21952     {
21953       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
21954       if (unwind.opcodes)
21955         unwind.opcodes = XRESIZEVEC (unsigned char, unwind.opcodes,
21956                                      unwind.opcode_alloc);
21957       else
21958         unwind.opcodes = XNEWVEC (unsigned char, unwind.opcode_alloc);
21959     }
21960   while (length > 0)
21961     {
21962       length--;
21963       unwind.opcodes[unwind.opcode_count] = op & 0xff;
21964       op >>= 8;
21965       unwind.opcode_count++;
21966     }
21967 }
21968
21969 /* Add unwind opcodes to adjust the stack pointer.  */
21970
21971 static void
21972 add_unwind_adjustsp (offsetT offset)
21973 {
21974   valueT op;
21975
21976   if (offset > 0x200)
21977     {
21978       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
21979       char bytes[5];
21980       int n;
21981       valueT o;
21982
21983       /* Long form: 0xb2, uleb128.  */
21984       /* This might not fit in a word so add the individual bytes,
21985          remembering the list is built in reverse order.  */
21986       o = (valueT) ((offset - 0x204) >> 2);
21987       if (o == 0)
21988         add_unwind_opcode (0, 1);
21989
21990       /* Calculate the uleb128 encoding of the offset.  */
21991       n = 0;
21992       while (o)
21993         {
21994           bytes[n] = o & 0x7f;
21995           o >>= 7;
21996           if (o)
21997             bytes[n] |= 0x80;
21998           n++;
21999         }
22000       /* Add the insn.  */
22001       for (; n; n--)
22002         add_unwind_opcode (bytes[n - 1], 1);
22003       add_unwind_opcode (0xb2, 1);
22004     }
22005   else if (offset > 0x100)
22006     {
22007       /* Two short opcodes.  */
22008       add_unwind_opcode (0x3f, 1);
22009       op = (offset - 0x104) >> 2;
22010       add_unwind_opcode (op, 1);
22011     }
22012   else if (offset > 0)
22013     {
22014       /* Short opcode.  */
22015       op = (offset - 4) >> 2;
22016       add_unwind_opcode (op, 1);
22017     }
22018   else if (offset < 0)
22019     {
22020       offset = -offset;
22021       while (offset > 0x100)
22022         {
22023           add_unwind_opcode (0x7f, 1);
22024           offset -= 0x100;
22025         }
22026       op = ((offset - 4) >> 2) | 0x40;
22027       add_unwind_opcode (op, 1);
22028     }
22029 }
22030
22031 /* Finish the list of unwind opcodes for this function.  */
22032 static void
22033 finish_unwind_opcodes (void)
22034 {
22035   valueT op;
22036
22037   if (unwind.fp_used)
22038     {
22039       /* Adjust sp as necessary.  */
22040       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
22041       flush_pending_unwind ();
22042
22043       /* After restoring sp from the frame pointer.  */
22044       op = 0x90 | unwind.fp_reg;
22045       add_unwind_opcode (op, 1);
22046     }
22047   else
22048     flush_pending_unwind ();
22049 }
22050
22051
22052 /* Start an exception table entry.  If idx is nonzero this is an index table
22053    entry.  */
22054
22055 static void
22056 start_unwind_section (const segT text_seg, int idx)
22057 {
22058   const char * text_name;
22059   const char * prefix;
22060   const char * prefix_once;
22061   const char * group_name;
22062   char * sec_name;
22063   int type;
22064   int flags;
22065   int linkonce;
22066
22067   if (idx)
22068     {
22069       prefix = ELF_STRING_ARM_unwind;
22070       prefix_once = ELF_STRING_ARM_unwind_once;
22071       type = SHT_ARM_EXIDX;
22072     }
22073   else
22074     {
22075       prefix = ELF_STRING_ARM_unwind_info;
22076       prefix_once = ELF_STRING_ARM_unwind_info_once;
22077       type = SHT_PROGBITS;
22078     }
22079
22080   text_name = segment_name (text_seg);
22081   if (streq (text_name, ".text"))
22082     text_name = "";
22083
22084   if (strncmp (text_name, ".gnu.linkonce.t.",
22085                strlen (".gnu.linkonce.t.")) == 0)
22086     {
22087       prefix = prefix_once;
22088       text_name += strlen (".gnu.linkonce.t.");
22089     }
22090
22091   sec_name = concat (prefix, text_name, (char *) NULL);
22092
22093   flags = SHF_ALLOC;
22094   linkonce = 0;
22095   group_name = 0;
22096
22097   /* Handle COMDAT group.  */
22098   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
22099     {
22100       group_name = elf_group_name (text_seg);
22101       if (group_name == NULL)
22102         {
22103           as_bad (_("Group section `%s' has no group signature"),
22104                   segment_name (text_seg));
22105           ignore_rest_of_line ();
22106           return;
22107         }
22108       flags |= SHF_GROUP;
22109       linkonce = 1;
22110     }
22111
22112   obj_elf_change_section (sec_name, type, 0, flags, 0, group_name,
22113                           linkonce, 0);
22114
22115   /* Set the section link for index tables.  */
22116   if (idx)
22117     elf_linked_to_section (now_seg) = text_seg;
22118 }
22119
22120
22121 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
22122    personality routine data.  Returns zero, or the index table value for
22123    an inline entry.  */
22124
22125 static valueT
22126 create_unwind_entry (int have_data)
22127 {
22128   int size;
22129   addressT where;
22130   char *ptr;
22131   /* The current word of data.  */
22132   valueT data;
22133   /* The number of bytes left in this word.  */
22134   int n;
22135
22136   finish_unwind_opcodes ();
22137
22138   /* Remember the current text section.  */
22139   unwind.saved_seg = now_seg;
22140   unwind.saved_subseg = now_subseg;
22141
22142   start_unwind_section (now_seg, 0);
22143
22144   if (unwind.personality_routine == NULL)
22145     {
22146       if (unwind.personality_index == -2)
22147         {
22148           if (have_data)
22149             as_bad (_("handlerdata in cantunwind frame"));
22150           return 1; /* EXIDX_CANTUNWIND.  */
22151         }
22152
22153       /* Use a default personality routine if none is specified.  */
22154       if (unwind.personality_index == -1)
22155         {
22156           if (unwind.opcode_count > 3)
22157             unwind.personality_index = 1;
22158           else
22159             unwind.personality_index = 0;
22160         }
22161
22162       /* Space for the personality routine entry.  */
22163       if (unwind.personality_index == 0)
22164         {
22165           if (unwind.opcode_count > 3)
22166             as_bad (_("too many unwind opcodes for personality routine 0"));
22167
22168           if (!have_data)
22169             {
22170               /* All the data is inline in the index table.  */
22171               data = 0x80;
22172               n = 3;
22173               while (unwind.opcode_count > 0)
22174                 {
22175                   unwind.opcode_count--;
22176                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22177                   n--;
22178                 }
22179
22180               /* Pad with "finish" opcodes.  */
22181               while (n--)
22182                 data = (data << 8) | 0xb0;
22183
22184               return data;
22185             }
22186           size = 0;
22187         }
22188       else
22189         /* We get two opcodes "free" in the first word.  */
22190         size = unwind.opcode_count - 2;
22191     }
22192   else
22193     {
22194       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
22195       if (unwind.personality_index != -1)
22196         {
22197           as_bad (_("attempt to recreate an unwind entry"));
22198           return 1;
22199         }
22200
22201       /* An extra byte is required for the opcode count.        */
22202       size = unwind.opcode_count + 1;
22203     }
22204
22205   size = (size + 3) >> 2;
22206   if (size > 0xff)
22207     as_bad (_("too many unwind opcodes"));
22208
22209   frag_align (2, 0, 0);
22210   record_alignment (now_seg, 2);
22211   unwind.table_entry = expr_build_dot ();
22212
22213   /* Allocate the table entry.  */
22214   ptr = frag_more ((size << 2) + 4);
22215   /* PR 13449: Zero the table entries in case some of them are not used.  */
22216   memset (ptr, 0, (size << 2) + 4);
22217   where = frag_now_fix () - ((size << 2) + 4);
22218
22219   switch (unwind.personality_index)
22220     {
22221     case -1:
22222       /* ??? Should this be a PLT generating relocation?  */
22223       /* Custom personality routine.  */
22224       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
22225                BFD_RELOC_ARM_PREL31);
22226
22227       where += 4;
22228       ptr += 4;
22229
22230       /* Set the first byte to the number of additional words.  */
22231       data = size > 0 ? size - 1 : 0;
22232       n = 3;
22233       break;
22234
22235     /* ABI defined personality routines.  */
22236     case 0:
22237       /* Three opcodes bytes are packed into the first word.  */
22238       data = 0x80;
22239       n = 3;
22240       break;
22241
22242     case 1:
22243     case 2:
22244       /* The size and first two opcode bytes go in the first word.  */
22245       data = ((0x80 + unwind.personality_index) << 8) | size;
22246       n = 2;
22247       break;
22248
22249     default:
22250       /* Should never happen.  */
22251       abort ();
22252     }
22253
22254   /* Pack the opcodes into words (MSB first), reversing the list at the same
22255      time.  */
22256   while (unwind.opcode_count > 0)
22257     {
22258       if (n == 0)
22259         {
22260           md_number_to_chars (ptr, data, 4);
22261           ptr += 4;
22262           n = 4;
22263           data = 0;
22264         }
22265       unwind.opcode_count--;
22266       n--;
22267       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
22268     }
22269
22270   /* Finish off the last word.  */
22271   if (n < 4)
22272     {
22273       /* Pad with "finish" opcodes.  */
22274       while (n--)
22275         data = (data << 8) | 0xb0;
22276
22277       md_number_to_chars (ptr, data, 4);
22278     }
22279
22280   if (!have_data)
22281     {
22282       /* Add an empty descriptor if there is no user-specified data.   */
22283       ptr = frag_more (4);
22284       md_number_to_chars (ptr, 0, 4);
22285     }
22286
22287   return 0;
22288 }
22289
22290
22291 /* Initialize the DWARF-2 unwind information for this procedure.  */
22292
22293 void
22294 tc_arm_frame_initial_instructions (void)
22295 {
22296   cfi_add_CFA_def_cfa (REG_SP, 0);
22297 }
22298 #endif /* OBJ_ELF */
22299
22300 /* Convert REGNAME to a DWARF-2 register number.  */
22301
22302 int
22303 tc_arm_regname_to_dw2regnum (char *regname)
22304 {
22305   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
22306   if (reg != FAIL)
22307     return reg;
22308
22309   /* PR 16694: Allow VFP registers as well.  */
22310   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
22311   if (reg != FAIL)
22312     return 64 + reg;
22313
22314   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
22315   if (reg != FAIL)
22316     return reg + 256;
22317
22318   return -1;
22319 }
22320
22321 #ifdef TE_PE
22322 void
22323 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
22324 {
22325   expressionS exp;
22326
22327   exp.X_op = O_secrel;
22328   exp.X_add_symbol = symbol;
22329   exp.X_add_number = 0;
22330   emit_expr (&exp, size);
22331 }
22332 #endif
22333
22334 /* MD interface: Symbol and relocation handling.  */
22335
22336 /* Return the address within the segment that a PC-relative fixup is
22337    relative to.  For ARM, PC-relative fixups applied to instructions
22338    are generally relative to the location of the fixup plus 8 bytes.
22339    Thumb branches are offset by 4, and Thumb loads relative to PC
22340    require special handling.  */
22341
22342 long
22343 md_pcrel_from_section (fixS * fixP, segT seg)
22344 {
22345   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
22346
22347   /* If this is pc-relative and we are going to emit a relocation
22348      then we just want to put out any pipeline compensation that the linker
22349      will need.  Otherwise we want to use the calculated base.
22350      For WinCE we skip the bias for externals as well, since this
22351      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
22352   if (fixP->fx_pcrel
22353       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
22354           || (arm_force_relocation (fixP)
22355 #ifdef TE_WINCE
22356               && !S_IS_EXTERNAL (fixP->fx_addsy)
22357 #endif
22358               )))
22359     base = 0;
22360
22361
22362   switch (fixP->fx_r_type)
22363     {
22364       /* PC relative addressing on the Thumb is slightly odd as the
22365          bottom two bits of the PC are forced to zero for the
22366          calculation.  This happens *after* application of the
22367          pipeline offset.  However, Thumb adrl already adjusts for
22368          this, so we need not do it again.  */
22369     case BFD_RELOC_ARM_THUMB_ADD:
22370       return base & ~3;
22371
22372     case BFD_RELOC_ARM_THUMB_OFFSET:
22373     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22374     case BFD_RELOC_ARM_T32_ADD_PC12:
22375     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22376       return (base + 4) & ~3;
22377
22378       /* Thumb branches are simply offset by +4.  */
22379     case BFD_RELOC_THUMB_PCREL_BRANCH7:
22380     case BFD_RELOC_THUMB_PCREL_BRANCH9:
22381     case BFD_RELOC_THUMB_PCREL_BRANCH12:
22382     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22383     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22384       return base + 4;
22385
22386     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22387       if (fixP->fx_addsy
22388           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22389           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22390           && ARM_IS_FUNC (fixP->fx_addsy)
22391           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22392         base = fixP->fx_where + fixP->fx_frag->fr_address;
22393        return base + 4;
22394
22395       /* BLX is like branches above, but forces the low two bits of PC to
22396          zero.  */
22397     case BFD_RELOC_THUMB_PCREL_BLX:
22398       if (fixP->fx_addsy
22399           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22400           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22401           && THUMB_IS_FUNC (fixP->fx_addsy)
22402           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22403         base = fixP->fx_where + fixP->fx_frag->fr_address;
22404       return (base + 4) & ~3;
22405
22406       /* ARM mode branches are offset by +8.  However, the Windows CE
22407          loader expects the relocation not to take this into account.  */
22408     case BFD_RELOC_ARM_PCREL_BLX:
22409       if (fixP->fx_addsy
22410           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22411           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22412           && ARM_IS_FUNC (fixP->fx_addsy)
22413           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22414         base = fixP->fx_where + fixP->fx_frag->fr_address;
22415       return base + 8;
22416
22417     case BFD_RELOC_ARM_PCREL_CALL:
22418       if (fixP->fx_addsy
22419           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22420           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22421           && THUMB_IS_FUNC (fixP->fx_addsy)
22422           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22423         base = fixP->fx_where + fixP->fx_frag->fr_address;
22424       return base + 8;
22425
22426     case BFD_RELOC_ARM_PCREL_BRANCH:
22427     case BFD_RELOC_ARM_PCREL_JUMP:
22428     case BFD_RELOC_ARM_PLT32:
22429 #ifdef TE_WINCE
22430       /* When handling fixups immediately, because we have already
22431          discovered the value of a symbol, or the address of the frag involved
22432          we must account for the offset by +8, as the OS loader will never see the reloc.
22433          see fixup_segment() in write.c
22434          The S_IS_EXTERNAL test handles the case of global symbols.
22435          Those need the calculated base, not just the pipe compensation the linker will need.  */
22436       if (fixP->fx_pcrel
22437           && fixP->fx_addsy != NULL
22438           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22439           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
22440         return base + 8;
22441       return base;
22442 #else
22443       return base + 8;
22444 #endif
22445
22446
22447       /* ARM mode loads relative to PC are also offset by +8.  Unlike
22448          branches, the Windows CE loader *does* expect the relocation
22449          to take this into account.  */
22450     case BFD_RELOC_ARM_OFFSET_IMM:
22451     case BFD_RELOC_ARM_OFFSET_IMM8:
22452     case BFD_RELOC_ARM_HWLITERAL:
22453     case BFD_RELOC_ARM_LITERAL:
22454     case BFD_RELOC_ARM_CP_OFF_IMM:
22455       return base + 8;
22456
22457
22458       /* Other PC-relative relocations are un-offset.  */
22459     default:
22460       return base;
22461     }
22462 }
22463
22464 static bfd_boolean flag_warn_syms = TRUE;
22465
22466 bfd_boolean
22467 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
22468 {
22469   /* PR 18347 - Warn if the user attempts to create a symbol with the same
22470      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
22471      does mean that the resulting code might be very confusing to the reader.
22472      Also this warning can be triggered if the user omits an operand before
22473      an immediate address, eg:
22474
22475        LDR =foo
22476
22477      GAS treats this as an assignment of the value of the symbol foo to a
22478      symbol LDR, and so (without this code) it will not issue any kind of
22479      warning or error message.
22480
22481      Note - ARM instructions are case-insensitive but the strings in the hash
22482      table are all stored in lower case, so we must first ensure that name is
22483      lower case too.  */
22484   if (flag_warn_syms && arm_ops_hsh)
22485     {
22486       char * nbuf = strdup (name);
22487       char * p;
22488
22489       for (p = nbuf; *p; p++)
22490         *p = TOLOWER (*p);
22491       if (hash_find (arm_ops_hsh, nbuf) != NULL)
22492         {
22493           static struct hash_control * already_warned = NULL;
22494
22495           if (already_warned == NULL)
22496             already_warned = hash_new ();
22497           /* Only warn about the symbol once.  To keep the code
22498              simple we let hash_insert do the lookup for us.  */
22499           if (hash_insert (already_warned, name, NULL) == NULL)
22500             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
22501         }
22502       else
22503         free (nbuf);
22504     }
22505
22506   return FALSE;
22507 }
22508
22509 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
22510    Otherwise we have no need to default values of symbols.  */
22511
22512 symbolS *
22513 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
22514 {
22515 #ifdef OBJ_ELF
22516   if (name[0] == '_' && name[1] == 'G'
22517       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
22518     {
22519       if (!GOT_symbol)
22520         {
22521           if (symbol_find (name))
22522             as_bad (_("GOT already in the symbol table"));
22523
22524           GOT_symbol = symbol_new (name, undefined_section,
22525                                    (valueT) 0, & zero_address_frag);
22526         }
22527
22528       return GOT_symbol;
22529     }
22530 #endif
22531
22532   return NULL;
22533 }
22534
22535 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
22536    computed as two separate immediate values, added together.  We
22537    already know that this value cannot be computed by just one ARM
22538    instruction.  */
22539
22540 static unsigned int
22541 validate_immediate_twopart (unsigned int   val,
22542                             unsigned int * highpart)
22543 {
22544   unsigned int a;
22545   unsigned int i;
22546
22547   for (i = 0; i < 32; i += 2)
22548     if (((a = rotate_left (val, i)) & 0xff) != 0)
22549       {
22550         if (a & 0xff00)
22551           {
22552             if (a & ~ 0xffff)
22553               continue;
22554             * highpart = (a  >> 8) | ((i + 24) << 7);
22555           }
22556         else if (a & 0xff0000)
22557           {
22558             if (a & 0xff000000)
22559               continue;
22560             * highpart = (a >> 16) | ((i + 16) << 7);
22561           }
22562         else
22563           {
22564             gas_assert (a & 0xff000000);
22565             * highpart = (a >> 24) | ((i + 8) << 7);
22566           }
22567
22568         return (a & 0xff) | (i << 7);
22569       }
22570
22571   return FAIL;
22572 }
22573
22574 static int
22575 validate_offset_imm (unsigned int val, int hwse)
22576 {
22577   if ((hwse && val > 255) || val > 4095)
22578     return FAIL;
22579   return val;
22580 }
22581
22582 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
22583    negative immediate constant by altering the instruction.  A bit of
22584    a hack really.
22585         MOV <-> MVN
22586         AND <-> BIC
22587         ADC <-> SBC
22588         by inverting the second operand, and
22589         ADD <-> SUB
22590         CMP <-> CMN
22591         by negating the second operand.  */
22592
22593 static int
22594 negate_data_op (unsigned long * instruction,
22595                 unsigned long   value)
22596 {
22597   int op, new_inst;
22598   unsigned long negated, inverted;
22599
22600   negated = encode_arm_immediate (-value);
22601   inverted = encode_arm_immediate (~value);
22602
22603   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
22604   switch (op)
22605     {
22606       /* First negates.  */
22607     case OPCODE_SUB:             /* ADD <-> SUB  */
22608       new_inst = OPCODE_ADD;
22609       value = negated;
22610       break;
22611
22612     case OPCODE_ADD:
22613       new_inst = OPCODE_SUB;
22614       value = negated;
22615       break;
22616
22617     case OPCODE_CMP:             /* CMP <-> CMN  */
22618       new_inst = OPCODE_CMN;
22619       value = negated;
22620       break;
22621
22622     case OPCODE_CMN:
22623       new_inst = OPCODE_CMP;
22624       value = negated;
22625       break;
22626
22627       /* Now Inverted ops.  */
22628     case OPCODE_MOV:             /* MOV <-> MVN  */
22629       new_inst = OPCODE_MVN;
22630       value = inverted;
22631       break;
22632
22633     case OPCODE_MVN:
22634       new_inst = OPCODE_MOV;
22635       value = inverted;
22636       break;
22637
22638     case OPCODE_AND:             /* AND <-> BIC  */
22639       new_inst = OPCODE_BIC;
22640       value = inverted;
22641       break;
22642
22643     case OPCODE_BIC:
22644       new_inst = OPCODE_AND;
22645       value = inverted;
22646       break;
22647
22648     case OPCODE_ADC:              /* ADC <-> SBC  */
22649       new_inst = OPCODE_SBC;
22650       value = inverted;
22651       break;
22652
22653     case OPCODE_SBC:
22654       new_inst = OPCODE_ADC;
22655       value = inverted;
22656       break;
22657
22658       /* We cannot do anything.  */
22659     default:
22660       return FAIL;
22661     }
22662
22663   if (value == (unsigned) FAIL)
22664     return FAIL;
22665
22666   *instruction &= OPCODE_MASK;
22667   *instruction |= new_inst << DATA_OP_SHIFT;
22668   return value;
22669 }
22670
22671 /* Like negate_data_op, but for Thumb-2.   */
22672
22673 static unsigned int
22674 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
22675 {
22676   int op, new_inst;
22677   int rd;
22678   unsigned int negated, inverted;
22679
22680   negated = encode_thumb32_immediate (-value);
22681   inverted = encode_thumb32_immediate (~value);
22682
22683   rd = (*instruction >> 8) & 0xf;
22684   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
22685   switch (op)
22686     {
22687       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
22688     case T2_OPCODE_SUB:
22689       new_inst = T2_OPCODE_ADD;
22690       value = negated;
22691       break;
22692
22693     case T2_OPCODE_ADD:
22694       new_inst = T2_OPCODE_SUB;
22695       value = negated;
22696       break;
22697
22698       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
22699     case T2_OPCODE_ORR:
22700       new_inst = T2_OPCODE_ORN;
22701       value = inverted;
22702       break;
22703
22704     case T2_OPCODE_ORN:
22705       new_inst = T2_OPCODE_ORR;
22706       value = inverted;
22707       break;
22708
22709       /* AND <-> BIC.  TST has no inverted equivalent.  */
22710     case T2_OPCODE_AND:
22711       new_inst = T2_OPCODE_BIC;
22712       if (rd == 15)
22713         value = FAIL;
22714       else
22715         value = inverted;
22716       break;
22717
22718     case T2_OPCODE_BIC:
22719       new_inst = T2_OPCODE_AND;
22720       value = inverted;
22721       break;
22722
22723       /* ADC <-> SBC  */
22724     case T2_OPCODE_ADC:
22725       new_inst = T2_OPCODE_SBC;
22726       value = inverted;
22727       break;
22728
22729     case T2_OPCODE_SBC:
22730       new_inst = T2_OPCODE_ADC;
22731       value = inverted;
22732       break;
22733
22734       /* We cannot do anything.  */
22735     default:
22736       return FAIL;
22737     }
22738
22739   if (value == (unsigned int)FAIL)
22740     return FAIL;
22741
22742   *instruction &= T2_OPCODE_MASK;
22743   *instruction |= new_inst << T2_DATA_OP_SHIFT;
22744   return value;
22745 }
22746
22747 /* Read a 32-bit thumb instruction from buf.  */
22748 static unsigned long
22749 get_thumb32_insn (char * buf)
22750 {
22751   unsigned long insn;
22752   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
22753   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22754
22755   return insn;
22756 }
22757
22758
22759 /* We usually want to set the low bit on the address of thumb function
22760    symbols.  In particular .word foo - . should have the low bit set.
22761    Generic code tries to fold the difference of two symbols to
22762    a constant.  Prevent this and force a relocation when the first symbols
22763    is a thumb function.  */
22764
22765 bfd_boolean
22766 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
22767 {
22768   if (op == O_subtract
22769       && l->X_op == O_symbol
22770       && r->X_op == O_symbol
22771       && THUMB_IS_FUNC (l->X_add_symbol))
22772     {
22773       l->X_op = O_subtract;
22774       l->X_op_symbol = r->X_add_symbol;
22775       l->X_add_number -= r->X_add_number;
22776       return TRUE;
22777     }
22778
22779   /* Process as normal.  */
22780   return FALSE;
22781 }
22782
22783 /* Encode Thumb2 unconditional branches and calls. The encoding
22784    for the 2 are identical for the immediate values.  */
22785
22786 static void
22787 encode_thumb2_b_bl_offset (char * buf, offsetT value)
22788 {
22789 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
22790   offsetT newval;
22791   offsetT newval2;
22792   addressT S, I1, I2, lo, hi;
22793
22794   S = (value >> 24) & 0x01;
22795   I1 = (value >> 23) & 0x01;
22796   I2 = (value >> 22) & 0x01;
22797   hi = (value >> 12) & 0x3ff;
22798   lo = (value >> 1) & 0x7ff;
22799   newval   = md_chars_to_number (buf, THUMB_SIZE);
22800   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22801   newval  |= (S << 10) | hi;
22802   newval2 &=  ~T2I1I2MASK;
22803   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
22804   md_number_to_chars (buf, newval, THUMB_SIZE);
22805   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22806 }
22807
22808 void
22809 md_apply_fix (fixS *    fixP,
22810                valueT * valP,
22811                segT     seg)
22812 {
22813   offsetT        value = * valP;
22814   offsetT        newval;
22815   unsigned int   newimm;
22816   unsigned long  temp;
22817   int            sign;
22818   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
22819
22820   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
22821
22822   /* Note whether this will delete the relocation.  */
22823
22824   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
22825     fixP->fx_done = 1;
22826
22827   /* On a 64-bit host, silently truncate 'value' to 32 bits for
22828      consistency with the behaviour on 32-bit hosts.  Remember value
22829      for emit_reloc.  */
22830   value &= 0xffffffff;
22831   value ^= 0x80000000;
22832   value -= 0x80000000;
22833
22834   *valP = value;
22835   fixP->fx_addnumber = value;
22836
22837   /* Same treatment for fixP->fx_offset.  */
22838   fixP->fx_offset &= 0xffffffff;
22839   fixP->fx_offset ^= 0x80000000;
22840   fixP->fx_offset -= 0x80000000;
22841
22842   switch (fixP->fx_r_type)
22843     {
22844     case BFD_RELOC_NONE:
22845       /* This will need to go in the object file.  */
22846       fixP->fx_done = 0;
22847       break;
22848
22849     case BFD_RELOC_ARM_IMMEDIATE:
22850       /* We claim that this fixup has been processed here,
22851          even if in fact we generate an error because we do
22852          not have a reloc for it, so tc_gen_reloc will reject it.  */
22853       fixP->fx_done = 1;
22854
22855       if (fixP->fx_addsy)
22856         {
22857           const char *msg = 0;
22858
22859           if (! S_IS_DEFINED (fixP->fx_addsy))
22860             msg = _("undefined symbol %s used as an immediate value");
22861           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22862             msg = _("symbol %s is in a different section");
22863           else if (S_IS_WEAK (fixP->fx_addsy))
22864             msg = _("symbol %s is weak and may be overridden later");
22865
22866           if (msg)
22867             {
22868               as_bad_where (fixP->fx_file, fixP->fx_line,
22869                             msg, S_GET_NAME (fixP->fx_addsy));
22870               break;
22871             }
22872         }
22873
22874       temp = md_chars_to_number (buf, INSN_SIZE);
22875
22876       /* If the offset is negative, we should use encoding A2 for ADR.  */
22877       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
22878         newimm = negate_data_op (&temp, value);
22879       else
22880         {
22881           newimm = encode_arm_immediate (value);
22882
22883           /* If the instruction will fail, see if we can fix things up by
22884              changing the opcode.  */
22885           if (newimm == (unsigned int) FAIL)
22886             newimm = negate_data_op (&temp, value);
22887           /* MOV accepts both ARM modified immediate (A1 encoding) and
22888              UINT16 (A2 encoding) when possible, MOVW only accepts UINT16.
22889              When disassembling, MOV is preferred when there is no encoding
22890              overlap.  */
22891           if (newimm == (unsigned int) FAIL
22892               && ((temp >> DATA_OP_SHIFT) & 0xf) == OPCODE_MOV
22893               && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)
22894               && !((temp >> SBIT_SHIFT) & 0x1)
22895               && value >= 0 && value <= 0xffff)
22896             {
22897               /* Clear bits[23:20] to change encoding from A1 to A2.  */
22898               temp &= 0xff0fffff;
22899               /* Encoding high 4bits imm.  Code below will encode the remaining
22900                  low 12bits.  */
22901               temp |= (value & 0x0000f000) << 4;
22902               newimm = value & 0x00000fff;
22903             }
22904         }
22905
22906       if (newimm == (unsigned int) FAIL)
22907         {
22908           as_bad_where (fixP->fx_file, fixP->fx_line,
22909                         _("invalid constant (%lx) after fixup"),
22910                         (unsigned long) value);
22911           break;
22912         }
22913
22914       newimm |= (temp & 0xfffff000);
22915       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22916       break;
22917
22918     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
22919       {
22920         unsigned int highpart = 0;
22921         unsigned int newinsn  = 0xe1a00000; /* nop.  */
22922
22923         if (fixP->fx_addsy)
22924           {
22925             const char *msg = 0;
22926
22927             if (! S_IS_DEFINED (fixP->fx_addsy))
22928               msg = _("undefined symbol %s used as an immediate value");
22929             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22930               msg = _("symbol %s is in a different section");
22931             else if (S_IS_WEAK (fixP->fx_addsy))
22932               msg = _("symbol %s is weak and may be overridden later");
22933
22934             if (msg)
22935               {
22936                 as_bad_where (fixP->fx_file, fixP->fx_line,
22937                               msg, S_GET_NAME (fixP->fx_addsy));
22938                 break;
22939               }
22940           }
22941
22942         newimm = encode_arm_immediate (value);
22943         temp = md_chars_to_number (buf, INSN_SIZE);
22944
22945         /* If the instruction will fail, see if we can fix things up by
22946            changing the opcode.  */
22947         if (newimm == (unsigned int) FAIL
22948             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
22949           {
22950             /* No ?  OK - try using two ADD instructions to generate
22951                the value.  */
22952             newimm = validate_immediate_twopart (value, & highpart);
22953
22954             /* Yes - then make sure that the second instruction is
22955                also an add.  */
22956             if (newimm != (unsigned int) FAIL)
22957               newinsn = temp;
22958             /* Still No ?  Try using a negated value.  */
22959             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
22960               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
22961             /* Otherwise - give up.  */
22962             else
22963               {
22964                 as_bad_where (fixP->fx_file, fixP->fx_line,
22965                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
22966                               (long) value);
22967                 break;
22968               }
22969
22970             /* Replace the first operand in the 2nd instruction (which
22971                is the PC) with the destination register.  We have
22972                already added in the PC in the first instruction and we
22973                do not want to do it again.  */
22974             newinsn &= ~ 0xf0000;
22975             newinsn |= ((newinsn & 0x0f000) << 4);
22976           }
22977
22978         newimm |= (temp & 0xfffff000);
22979         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22980
22981         highpart |= (newinsn & 0xfffff000);
22982         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
22983       }
22984       break;
22985
22986     case BFD_RELOC_ARM_OFFSET_IMM:
22987       if (!fixP->fx_done && seg->use_rela_p)
22988         value = 0;
22989       /* Fall through.  */
22990
22991     case BFD_RELOC_ARM_LITERAL:
22992       sign = value > 0;
22993
22994       if (value < 0)
22995         value = - value;
22996
22997       if (validate_offset_imm (value, 0) == FAIL)
22998         {
22999           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
23000             as_bad_where (fixP->fx_file, fixP->fx_line,
23001                           _("invalid literal constant: pool needs to be closer"));
23002           else
23003             as_bad_where (fixP->fx_file, fixP->fx_line,
23004                           _("bad immediate value for offset (%ld)"),
23005                           (long) value);
23006           break;
23007         }
23008
23009       newval = md_chars_to_number (buf, INSN_SIZE);
23010       if (value == 0)
23011         newval &= 0xfffff000;
23012       else
23013         {
23014           newval &= 0xff7ff000;
23015           newval |= value | (sign ? INDEX_UP : 0);
23016         }
23017       md_number_to_chars (buf, newval, INSN_SIZE);
23018       break;
23019
23020     case BFD_RELOC_ARM_OFFSET_IMM8:
23021     case BFD_RELOC_ARM_HWLITERAL:
23022       sign = value > 0;
23023
23024       if (value < 0)
23025         value = - value;
23026
23027       if (validate_offset_imm (value, 1) == FAIL)
23028         {
23029           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
23030             as_bad_where (fixP->fx_file, fixP->fx_line,
23031                           _("invalid literal constant: pool needs to be closer"));
23032           else
23033             as_bad_where (fixP->fx_file, fixP->fx_line,
23034                           _("bad immediate value for 8-bit offset (%ld)"),
23035                           (long) value);
23036           break;
23037         }
23038
23039       newval = md_chars_to_number (buf, INSN_SIZE);
23040       if (value == 0)
23041         newval &= 0xfffff0f0;
23042       else
23043         {
23044           newval &= 0xff7ff0f0;
23045           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
23046         }
23047       md_number_to_chars (buf, newval, INSN_SIZE);
23048       break;
23049
23050     case BFD_RELOC_ARM_T32_OFFSET_U8:
23051       if (value < 0 || value > 1020 || value % 4 != 0)
23052         as_bad_where (fixP->fx_file, fixP->fx_line,
23053                       _("bad immediate value for offset (%ld)"), (long) value);
23054       value /= 4;
23055
23056       newval = md_chars_to_number (buf+2, THUMB_SIZE);
23057       newval |= value;
23058       md_number_to_chars (buf+2, newval, THUMB_SIZE);
23059       break;
23060
23061     case BFD_RELOC_ARM_T32_OFFSET_IMM:
23062       /* This is a complicated relocation used for all varieties of Thumb32
23063          load/store instruction with immediate offset:
23064
23065          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
23066                                                    *4, optional writeback(W)
23067                                                    (doubleword load/store)
23068
23069          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
23070          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
23071          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
23072          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
23073          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
23074
23075          Uppercase letters indicate bits that are already encoded at
23076          this point.  Lowercase letters are our problem.  For the
23077          second block of instructions, the secondary opcode nybble
23078          (bits 8..11) is present, and bit 23 is zero, even if this is
23079          a PC-relative operation.  */
23080       newval = md_chars_to_number (buf, THUMB_SIZE);
23081       newval <<= 16;
23082       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
23083
23084       if ((newval & 0xf0000000) == 0xe0000000)
23085         {
23086           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
23087           if (value >= 0)
23088             newval |= (1 << 23);
23089           else
23090             value = -value;
23091           if (value % 4 != 0)
23092             {
23093               as_bad_where (fixP->fx_file, fixP->fx_line,
23094                             _("offset not a multiple of 4"));
23095               break;
23096             }
23097           value /= 4;
23098           if (value > 0xff)
23099             {
23100               as_bad_where (fixP->fx_file, fixP->fx_line,
23101                             _("offset out of range"));
23102               break;
23103             }
23104           newval &= ~0xff;
23105         }
23106       else if ((newval & 0x000f0000) == 0x000f0000)
23107         {
23108           /* PC-relative, 12-bit offset.  */
23109           if (value >= 0)
23110             newval |= (1 << 23);
23111           else
23112             value = -value;
23113           if (value > 0xfff)
23114             {
23115               as_bad_where (fixP->fx_file, fixP->fx_line,
23116                             _("offset out of range"));
23117               break;
23118             }
23119           newval &= ~0xfff;
23120         }
23121       else if ((newval & 0x00000100) == 0x00000100)
23122         {
23123           /* Writeback: 8-bit, +/- offset.  */
23124           if (value >= 0)
23125             newval |= (1 << 9);
23126           else
23127             value = -value;
23128           if (value > 0xff)
23129             {
23130               as_bad_where (fixP->fx_file, fixP->fx_line,
23131                             _("offset out of range"));
23132               break;
23133             }
23134           newval &= ~0xff;
23135         }
23136       else if ((newval & 0x00000f00) == 0x00000e00)
23137         {
23138           /* T-instruction: positive 8-bit offset.  */
23139           if (value < 0 || value > 0xff)
23140             {
23141               as_bad_where (fixP->fx_file, fixP->fx_line,
23142                             _("offset out of range"));
23143               break;
23144             }
23145           newval &= ~0xff;
23146           newval |= value;
23147         }
23148       else
23149         {
23150           /* Positive 12-bit or negative 8-bit offset.  */
23151           int limit;
23152           if (value >= 0)
23153             {
23154               newval |= (1 << 23);
23155               limit = 0xfff;
23156             }
23157           else
23158             {
23159               value = -value;
23160               limit = 0xff;
23161             }
23162           if (value > limit)
23163             {
23164               as_bad_where (fixP->fx_file, fixP->fx_line,
23165                             _("offset out of range"));
23166               break;
23167             }
23168           newval &= ~limit;
23169         }
23170
23171       newval |= value;
23172       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
23173       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
23174       break;
23175
23176     case BFD_RELOC_ARM_SHIFT_IMM:
23177       newval = md_chars_to_number (buf, INSN_SIZE);
23178       if (((unsigned long) value) > 32
23179           || (value == 32
23180               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
23181         {
23182           as_bad_where (fixP->fx_file, fixP->fx_line,
23183                         _("shift expression is too large"));
23184           break;
23185         }
23186
23187       if (value == 0)
23188         /* Shifts of zero must be done as lsl.  */
23189         newval &= ~0x60;
23190       else if (value == 32)
23191         value = 0;
23192       newval &= 0xfffff07f;
23193       newval |= (value & 0x1f) << 7;
23194       md_number_to_chars (buf, newval, INSN_SIZE);
23195       break;
23196
23197     case BFD_RELOC_ARM_T32_IMMEDIATE:
23198     case BFD_RELOC_ARM_T32_ADD_IMM:
23199     case BFD_RELOC_ARM_T32_IMM12:
23200     case BFD_RELOC_ARM_T32_ADD_PC12:
23201       /* We claim that this fixup has been processed here,
23202          even if in fact we generate an error because we do
23203          not have a reloc for it, so tc_gen_reloc will reject it.  */
23204       fixP->fx_done = 1;
23205
23206       if (fixP->fx_addsy
23207           && ! S_IS_DEFINED (fixP->fx_addsy))
23208         {
23209           as_bad_where (fixP->fx_file, fixP->fx_line,
23210                         _("undefined symbol %s used as an immediate value"),
23211                         S_GET_NAME (fixP->fx_addsy));
23212           break;
23213         }
23214
23215       newval = md_chars_to_number (buf, THUMB_SIZE);
23216       newval <<= 16;
23217       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
23218
23219       newimm = FAIL;
23220       if ((fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23221            /* ARMv8-M Baseline MOV will reach here, but it doesn't support
23222               Thumb2 modified immediate encoding (T2).  */
23223            && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2))
23224           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23225         {
23226           newimm = encode_thumb32_immediate (value);
23227           if (newimm == (unsigned int) FAIL)
23228             newimm = thumb32_negate_data_op (&newval, value);
23229         }
23230       if (newimm == (unsigned int) FAIL)
23231         {
23232           if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE)
23233             {
23234               /* Turn add/sum into addw/subw.  */
23235               if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
23236                 newval = (newval & 0xfeffffff) | 0x02000000;
23237               /* No flat 12-bit imm encoding for addsw/subsw.  */
23238               if ((newval & 0x00100000) == 0)
23239                 {
23240                   /* 12 bit immediate for addw/subw.  */
23241                   if (value < 0)
23242                     {
23243                       value = -value;
23244                       newval ^= 0x00a00000;
23245                     }
23246                   if (value > 0xfff)
23247                     newimm = (unsigned int) FAIL;
23248                   else
23249                     newimm = value;
23250                 }
23251             }
23252           else
23253             {
23254               /* MOV accepts both Thumb2 modified immediate (T2 encoding) and
23255                  UINT16 (T3 encoding), MOVW only accepts UINT16.  When
23256                  disassembling, MOV is preferred when there is no encoding
23257                  overlap.
23258                  NOTE: MOV is using ORR opcode under Thumb 2 mode.  */
23259               if (((newval >> T2_DATA_OP_SHIFT) & 0xf) == T2_OPCODE_ORR
23260                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2_v8m)
23261                   && !((newval >> T2_SBIT_SHIFT) & 0x1)
23262                   && value >= 0 && value <=0xffff)
23263                 {
23264                   /* Toggle bit[25] to change encoding from T2 to T3.  */
23265                   newval ^= 1 << 25;
23266                   /* Clear bits[19:16].  */
23267                   newval &= 0xfff0ffff;
23268                   /* Encoding high 4bits imm.  Code below will encode the
23269                      remaining low 12bits.  */
23270                   newval |= (value & 0x0000f000) << 4;
23271                   newimm = value & 0x00000fff;
23272                 }
23273             }
23274         }
23275
23276       if (newimm == (unsigned int)FAIL)
23277         {
23278           as_bad_where (fixP->fx_file, fixP->fx_line,
23279                         _("invalid constant (%lx) after fixup"),
23280                         (unsigned long) value);
23281           break;
23282         }
23283
23284       newval |= (newimm & 0x800) << 15;
23285       newval |= (newimm & 0x700) << 4;
23286       newval |= (newimm & 0x0ff);
23287
23288       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
23289       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
23290       break;
23291
23292     case BFD_RELOC_ARM_SMC:
23293       if (((unsigned long) value) > 0xffff)
23294         as_bad_where (fixP->fx_file, fixP->fx_line,
23295                       _("invalid smc expression"));
23296       newval = md_chars_to_number (buf, INSN_SIZE);
23297       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23298       md_number_to_chars (buf, newval, INSN_SIZE);
23299       break;
23300
23301     case BFD_RELOC_ARM_HVC:
23302       if (((unsigned long) value) > 0xffff)
23303         as_bad_where (fixP->fx_file, fixP->fx_line,
23304                       _("invalid hvc expression"));
23305       newval = md_chars_to_number (buf, INSN_SIZE);
23306       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
23307       md_number_to_chars (buf, newval, INSN_SIZE);
23308       break;
23309
23310     case BFD_RELOC_ARM_SWI:
23311       if (fixP->tc_fix_data != 0)
23312         {
23313           if (((unsigned long) value) > 0xff)
23314             as_bad_where (fixP->fx_file, fixP->fx_line,
23315                           _("invalid swi expression"));
23316           newval = md_chars_to_number (buf, THUMB_SIZE);
23317           newval |= value;
23318           md_number_to_chars (buf, newval, THUMB_SIZE);
23319         }
23320       else
23321         {
23322           if (((unsigned long) value) > 0x00ffffff)
23323             as_bad_where (fixP->fx_file, fixP->fx_line,
23324                           _("invalid swi expression"));
23325           newval = md_chars_to_number (buf, INSN_SIZE);
23326           newval |= value;
23327           md_number_to_chars (buf, newval, INSN_SIZE);
23328         }
23329       break;
23330
23331     case BFD_RELOC_ARM_MULTI:
23332       if (((unsigned long) value) > 0xffff)
23333         as_bad_where (fixP->fx_file, fixP->fx_line,
23334                       _("invalid expression in load/store multiple"));
23335       newval = value | md_chars_to_number (buf, INSN_SIZE);
23336       md_number_to_chars (buf, newval, INSN_SIZE);
23337       break;
23338
23339 #ifdef OBJ_ELF
23340     case BFD_RELOC_ARM_PCREL_CALL:
23341
23342       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23343           && fixP->fx_addsy
23344           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23345           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23346           && THUMB_IS_FUNC (fixP->fx_addsy))
23347         /* Flip the bl to blx. This is a simple flip
23348            bit here because we generate PCREL_CALL for
23349            unconditional bls.  */
23350         {
23351           newval = md_chars_to_number (buf, INSN_SIZE);
23352           newval = newval | 0x10000000;
23353           md_number_to_chars (buf, newval, INSN_SIZE);
23354           temp = 1;
23355           fixP->fx_done = 1;
23356         }
23357       else
23358         temp = 3;
23359       goto arm_branch_common;
23360
23361     case BFD_RELOC_ARM_PCREL_JUMP:
23362       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23363           && fixP->fx_addsy
23364           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23365           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23366           && THUMB_IS_FUNC (fixP->fx_addsy))
23367         {
23368           /* This would map to a bl<cond>, b<cond>,
23369              b<always> to a Thumb function. We
23370              need to force a relocation for this particular
23371              case.  */
23372           newval = md_chars_to_number (buf, INSN_SIZE);
23373           fixP->fx_done = 0;
23374         }
23375       /* Fall through.  */
23376
23377     case BFD_RELOC_ARM_PLT32:
23378 #endif
23379     case BFD_RELOC_ARM_PCREL_BRANCH:
23380       temp = 3;
23381       goto arm_branch_common;
23382
23383     case BFD_RELOC_ARM_PCREL_BLX:
23384
23385       temp = 1;
23386       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
23387           && fixP->fx_addsy
23388           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23389           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23390           && ARM_IS_FUNC (fixP->fx_addsy))
23391         {
23392           /* Flip the blx to a bl and warn.  */
23393           const char *name = S_GET_NAME (fixP->fx_addsy);
23394           newval = 0xeb000000;
23395           as_warn_where (fixP->fx_file, fixP->fx_line,
23396                          _("blx to '%s' an ARM ISA state function changed to bl"),
23397                           name);
23398           md_number_to_chars (buf, newval, INSN_SIZE);
23399           temp = 3;
23400           fixP->fx_done = 1;
23401         }
23402
23403 #ifdef OBJ_ELF
23404        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23405          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
23406 #endif
23407
23408     arm_branch_common:
23409       /* We are going to store value (shifted right by two) in the
23410          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
23411          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
23412          also be be clear.  */
23413       if (value & temp)
23414         as_bad_where (fixP->fx_file, fixP->fx_line,
23415                       _("misaligned branch destination"));
23416       if ((value & (offsetT)0xfe000000) != (offsetT)0
23417           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
23418         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23419
23420       if (fixP->fx_done || !seg->use_rela_p)
23421         {
23422           newval = md_chars_to_number (buf, INSN_SIZE);
23423           newval |= (value >> 2) & 0x00ffffff;
23424           /* Set the H bit on BLX instructions.  */
23425           if (temp == 1)
23426             {
23427               if (value & 2)
23428                 newval |= 0x01000000;
23429               else
23430                 newval &= ~0x01000000;
23431             }
23432           md_number_to_chars (buf, newval, INSN_SIZE);
23433         }
23434       break;
23435
23436     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
23437       /* CBZ can only branch forward.  */
23438
23439       /* Attempts to use CBZ to branch to the next instruction
23440          (which, strictly speaking, are prohibited) will be turned into
23441          no-ops.
23442
23443          FIXME: It may be better to remove the instruction completely and
23444          perform relaxation.  */
23445       if (value == -2)
23446         {
23447           newval = md_chars_to_number (buf, THUMB_SIZE);
23448           newval = 0xbf00; /* NOP encoding T1 */
23449           md_number_to_chars (buf, newval, THUMB_SIZE);
23450         }
23451       else
23452         {
23453           if (value & ~0x7e)
23454             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23455
23456           if (fixP->fx_done || !seg->use_rela_p)
23457             {
23458               newval = md_chars_to_number (buf, THUMB_SIZE);
23459               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
23460               md_number_to_chars (buf, newval, THUMB_SIZE);
23461             }
23462         }
23463       break;
23464
23465     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
23466       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
23467         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23468
23469       if (fixP->fx_done || !seg->use_rela_p)
23470         {
23471           newval = md_chars_to_number (buf, THUMB_SIZE);
23472           newval |= (value & 0x1ff) >> 1;
23473           md_number_to_chars (buf, newval, THUMB_SIZE);
23474         }
23475       break;
23476
23477     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
23478       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
23479         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23480
23481       if (fixP->fx_done || !seg->use_rela_p)
23482         {
23483           newval = md_chars_to_number (buf, THUMB_SIZE);
23484           newval |= (value & 0xfff) >> 1;
23485           md_number_to_chars (buf, newval, THUMB_SIZE);
23486         }
23487       break;
23488
23489     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23490       if (fixP->fx_addsy
23491           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23492           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23493           && ARM_IS_FUNC (fixP->fx_addsy)
23494           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23495         {
23496           /* Force a relocation for a branch 20 bits wide.  */
23497           fixP->fx_done = 0;
23498         }
23499       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
23500         as_bad_where (fixP->fx_file, fixP->fx_line,
23501                       _("conditional branch out of range"));
23502
23503       if (fixP->fx_done || !seg->use_rela_p)
23504         {
23505           offsetT newval2;
23506           addressT S, J1, J2, lo, hi;
23507
23508           S  = (value & 0x00100000) >> 20;
23509           J2 = (value & 0x00080000) >> 19;
23510           J1 = (value & 0x00040000) >> 18;
23511           hi = (value & 0x0003f000) >> 12;
23512           lo = (value & 0x00000ffe) >> 1;
23513
23514           newval   = md_chars_to_number (buf, THUMB_SIZE);
23515           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23516           newval  |= (S << 10) | hi;
23517           newval2 |= (J1 << 13) | (J2 << 11) | lo;
23518           md_number_to_chars (buf, newval, THUMB_SIZE);
23519           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
23520         }
23521       break;
23522
23523     case BFD_RELOC_THUMB_PCREL_BLX:
23524       /* If there is a blx from a thumb state function to
23525          another thumb function flip this to a bl and warn
23526          about it.  */
23527
23528       if (fixP->fx_addsy
23529           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23530           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23531           && THUMB_IS_FUNC (fixP->fx_addsy))
23532         {
23533           const char *name = S_GET_NAME (fixP->fx_addsy);
23534           as_warn_where (fixP->fx_file, fixP->fx_line,
23535                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
23536                          name);
23537           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23538           newval = newval | 0x1000;
23539           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23540           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23541           fixP->fx_done = 1;
23542         }
23543
23544
23545       goto thumb_bl_common;
23546
23547     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23548       /* A bl from Thumb state ISA to an internal ARM state function
23549          is converted to a blx.  */
23550       if (fixP->fx_addsy
23551           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
23552           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
23553           && ARM_IS_FUNC (fixP->fx_addsy)
23554           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
23555         {
23556           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
23557           newval = newval & ~0x1000;
23558           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
23559           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
23560           fixP->fx_done = 1;
23561         }
23562
23563     thumb_bl_common:
23564
23565       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23566         /* For a BLX instruction, make sure that the relocation is rounded up
23567            to a word boundary.  This follows the semantics of the instruction
23568            which specifies that bit 1 of the target address will come from bit
23569            1 of the base address.  */
23570         value = (value + 3) & ~ 3;
23571
23572 #ifdef OBJ_ELF
23573        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
23574            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
23575          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
23576 #endif
23577
23578       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
23579         {
23580           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6t2)))
23581             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23582           else if ((value & ~0x1ffffff)
23583                    && ((value & ~0x1ffffff) != ~0x1ffffff))
23584             as_bad_where (fixP->fx_file, fixP->fx_line,
23585                           _("Thumb2 branch out of range"));
23586         }
23587
23588       if (fixP->fx_done || !seg->use_rela_p)
23589         encode_thumb2_b_bl_offset (buf, value);
23590
23591       break;
23592
23593     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23594       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
23595         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
23596
23597       if (fixP->fx_done || !seg->use_rela_p)
23598           encode_thumb2_b_bl_offset (buf, value);
23599
23600       break;
23601
23602     case BFD_RELOC_8:
23603       if (fixP->fx_done || !seg->use_rela_p)
23604         *buf = value;
23605       break;
23606
23607     case BFD_RELOC_16:
23608       if (fixP->fx_done || !seg->use_rela_p)
23609         md_number_to_chars (buf, value, 2);
23610       break;
23611
23612 #ifdef OBJ_ELF
23613     case BFD_RELOC_ARM_TLS_CALL:
23614     case BFD_RELOC_ARM_THM_TLS_CALL:
23615     case BFD_RELOC_ARM_TLS_DESCSEQ:
23616     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
23617     case BFD_RELOC_ARM_TLS_GOTDESC:
23618     case BFD_RELOC_ARM_TLS_GD32:
23619     case BFD_RELOC_ARM_TLS_LE32:
23620     case BFD_RELOC_ARM_TLS_IE32:
23621     case BFD_RELOC_ARM_TLS_LDM32:
23622     case BFD_RELOC_ARM_TLS_LDO32:
23623       S_SET_THREAD_LOCAL (fixP->fx_addsy);
23624       break;
23625
23626     case BFD_RELOC_ARM_GOT32:
23627     case BFD_RELOC_ARM_GOTOFF:
23628       break;
23629
23630     case BFD_RELOC_ARM_GOT_PREL:
23631       if (fixP->fx_done || !seg->use_rela_p)
23632         md_number_to_chars (buf, value, 4);
23633       break;
23634
23635     case BFD_RELOC_ARM_TARGET2:
23636       /* TARGET2 is not partial-inplace, so we need to write the
23637          addend here for REL targets, because it won't be written out
23638          during reloc processing later.  */
23639       if (fixP->fx_done || !seg->use_rela_p)
23640         md_number_to_chars (buf, fixP->fx_offset, 4);
23641       break;
23642 #endif
23643
23644     case BFD_RELOC_RVA:
23645     case BFD_RELOC_32:
23646     case BFD_RELOC_ARM_TARGET1:
23647     case BFD_RELOC_ARM_ROSEGREL32:
23648     case BFD_RELOC_ARM_SBREL32:
23649     case BFD_RELOC_32_PCREL:
23650 #ifdef TE_PE
23651     case BFD_RELOC_32_SECREL:
23652 #endif
23653       if (fixP->fx_done || !seg->use_rela_p)
23654 #ifdef TE_WINCE
23655         /* For WinCE we only do this for pcrel fixups.  */
23656         if (fixP->fx_done || fixP->fx_pcrel)
23657 #endif
23658           md_number_to_chars (buf, value, 4);
23659       break;
23660
23661 #ifdef OBJ_ELF
23662     case BFD_RELOC_ARM_PREL31:
23663       if (fixP->fx_done || !seg->use_rela_p)
23664         {
23665           newval = md_chars_to_number (buf, 4) & 0x80000000;
23666           if ((value ^ (value >> 1)) & 0x40000000)
23667             {
23668               as_bad_where (fixP->fx_file, fixP->fx_line,
23669                             _("rel31 relocation overflow"));
23670             }
23671           newval |= value & 0x7fffffff;
23672           md_number_to_chars (buf, newval, 4);
23673         }
23674       break;
23675 #endif
23676
23677     case BFD_RELOC_ARM_CP_OFF_IMM:
23678     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
23679       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM)
23680         newval = md_chars_to_number (buf, INSN_SIZE);
23681       else
23682         newval = get_thumb32_insn (buf);
23683       if ((newval & 0x0f200f00) == 0x0d000900)
23684         {
23685           /* This is a fp16 vstr/vldr.  The immediate offset in the mnemonic
23686              has permitted values that are multiples of 2, in the range 0
23687              to 510.  */
23688           if (value < -510 || value > 510 || (value & 1))
23689             as_bad_where (fixP->fx_file, fixP->fx_line,
23690                           _("co-processor offset out of range"));
23691         }
23692       else if (value < -1023 || value > 1023 || (value & 3))
23693         as_bad_where (fixP->fx_file, fixP->fx_line,
23694                       _("co-processor offset out of range"));
23695     cp_off_common:
23696       sign = value > 0;
23697       if (value < 0)
23698         value = -value;
23699       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23700           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
23701         newval = md_chars_to_number (buf, INSN_SIZE);
23702       else
23703         newval = get_thumb32_insn (buf);
23704       if (value == 0)
23705         newval &= 0xffffff00;
23706       else
23707         {
23708           newval &= 0xff7fff00;
23709           if ((newval & 0x0f200f00) == 0x0d000900)
23710             {
23711               /* This is a fp16 vstr/vldr.
23712
23713                  It requires the immediate offset in the instruction is shifted
23714                  left by 1 to be a half-word offset.
23715
23716                  Here, left shift by 1 first, and later right shift by 2
23717                  should get the right offset.  */
23718               value <<= 1;
23719             }
23720           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
23721         }
23722       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23723           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
23724         md_number_to_chars (buf, newval, INSN_SIZE);
23725       else
23726         put_thumb32_insn (buf, newval);
23727       break;
23728
23729     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
23730     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
23731       if (value < -255 || value > 255)
23732         as_bad_where (fixP->fx_file, fixP->fx_line,
23733                       _("co-processor offset out of range"));
23734       value *= 4;
23735       goto cp_off_common;
23736
23737     case BFD_RELOC_ARM_THUMB_OFFSET:
23738       newval = md_chars_to_number (buf, THUMB_SIZE);
23739       /* Exactly what ranges, and where the offset is inserted depends
23740          on the type of instruction, we can establish this from the
23741          top 4 bits.  */
23742       switch (newval >> 12)
23743         {
23744         case 4: /* PC load.  */
23745           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
23746              forced to zero for these loads; md_pcrel_from has already
23747              compensated for this.  */
23748           if (value & 3)
23749             as_bad_where (fixP->fx_file, fixP->fx_line,
23750                           _("invalid offset, target not word aligned (0x%08lX)"),
23751                           (((unsigned long) fixP->fx_frag->fr_address
23752                             + (unsigned long) fixP->fx_where) & ~3)
23753                           + (unsigned long) value);
23754
23755           if (value & ~0x3fc)
23756             as_bad_where (fixP->fx_file, fixP->fx_line,
23757                           _("invalid offset, value too big (0x%08lX)"),
23758                           (long) value);
23759
23760           newval |= value >> 2;
23761           break;
23762
23763         case 9: /* SP load/store.  */
23764           if (value & ~0x3fc)
23765             as_bad_where (fixP->fx_file, fixP->fx_line,
23766                           _("invalid offset, value too big (0x%08lX)"),
23767                           (long) value);
23768           newval |= value >> 2;
23769           break;
23770
23771         case 6: /* Word load/store.  */
23772           if (value & ~0x7c)
23773             as_bad_where (fixP->fx_file, fixP->fx_line,
23774                           _("invalid offset, value too big (0x%08lX)"),
23775                           (long) value);
23776           newval |= value << 4; /* 6 - 2.  */
23777           break;
23778
23779         case 7: /* Byte load/store.  */
23780           if (value & ~0x1f)
23781             as_bad_where (fixP->fx_file, fixP->fx_line,
23782                           _("invalid offset, value too big (0x%08lX)"),
23783                           (long) value);
23784           newval |= value << 6;
23785           break;
23786
23787         case 8: /* Halfword load/store.  */
23788           if (value & ~0x3e)
23789             as_bad_where (fixP->fx_file, fixP->fx_line,
23790                           _("invalid offset, value too big (0x%08lX)"),
23791                           (long) value);
23792           newval |= value << 5; /* 6 - 1.  */
23793           break;
23794
23795         default:
23796           as_bad_where (fixP->fx_file, fixP->fx_line,
23797                         "Unable to process relocation for thumb opcode: %lx",
23798                         (unsigned long) newval);
23799           break;
23800         }
23801       md_number_to_chars (buf, newval, THUMB_SIZE);
23802       break;
23803
23804     case BFD_RELOC_ARM_THUMB_ADD:
23805       /* This is a complicated relocation, since we use it for all of
23806          the following immediate relocations:
23807
23808             3bit ADD/SUB
23809             8bit ADD/SUB
23810             9bit ADD/SUB SP word-aligned
23811            10bit ADD PC/SP word-aligned
23812
23813          The type of instruction being processed is encoded in the
23814          instruction field:
23815
23816            0x8000  SUB
23817            0x00F0  Rd
23818            0x000F  Rs
23819       */
23820       newval = md_chars_to_number (buf, THUMB_SIZE);
23821       {
23822         int rd = (newval >> 4) & 0xf;
23823         int rs = newval & 0xf;
23824         int subtract = !!(newval & 0x8000);
23825
23826         /* Check for HI regs, only very restricted cases allowed:
23827            Adjusting SP, and using PC or SP to get an address.  */
23828         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
23829             || (rs > 7 && rs != REG_SP && rs != REG_PC))
23830           as_bad_where (fixP->fx_file, fixP->fx_line,
23831                         _("invalid Hi register with immediate"));
23832
23833         /* If value is negative, choose the opposite instruction.  */
23834         if (value < 0)
23835           {
23836             value = -value;
23837             subtract = !subtract;
23838             if (value < 0)
23839               as_bad_where (fixP->fx_file, fixP->fx_line,
23840                             _("immediate value out of range"));
23841           }
23842
23843         if (rd == REG_SP)
23844           {
23845             if (value & ~0x1fc)
23846               as_bad_where (fixP->fx_file, fixP->fx_line,
23847                             _("invalid immediate for stack address calculation"));
23848             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
23849             newval |= value >> 2;
23850           }
23851         else if (rs == REG_PC || rs == REG_SP)
23852           {
23853             /* PR gas/18541.  If the addition is for a defined symbol
23854                within range of an ADR instruction then accept it.  */
23855             if (subtract
23856                 && value == 4
23857                 && fixP->fx_addsy != NULL)
23858               {
23859                 subtract = 0;
23860
23861                 if (! S_IS_DEFINED (fixP->fx_addsy)
23862                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
23863                     || S_IS_WEAK (fixP->fx_addsy))
23864                   {
23865                     as_bad_where (fixP->fx_file, fixP->fx_line,
23866                                   _("address calculation needs a strongly defined nearby symbol"));
23867                   }
23868                 else
23869                   {
23870                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
23871
23872                     /* Round up to the next 4-byte boundary.  */
23873                     if (v & 3)
23874                       v = (v + 3) & ~ 3;
23875                     else
23876                       v += 4;
23877                     v = S_GET_VALUE (fixP->fx_addsy) - v;
23878
23879                     if (v & ~0x3fc)
23880                       {
23881                         as_bad_where (fixP->fx_file, fixP->fx_line,
23882                                       _("symbol too far away"));
23883                       }
23884                     else
23885                       {
23886                         fixP->fx_done = 1;
23887                         value = v;
23888                       }
23889                   }
23890               }
23891
23892             if (subtract || value & ~0x3fc)
23893               as_bad_where (fixP->fx_file, fixP->fx_line,
23894                             _("invalid immediate for address calculation (value = 0x%08lX)"),
23895                             (unsigned long) (subtract ? - value : value));
23896             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
23897             newval |= rd << 8;
23898             newval |= value >> 2;
23899           }
23900         else if (rs == rd)
23901           {
23902             if (value & ~0xff)
23903               as_bad_where (fixP->fx_file, fixP->fx_line,
23904                             _("immediate value out of range"));
23905             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
23906             newval |= (rd << 8) | value;
23907           }
23908         else
23909           {
23910             if (value & ~0x7)
23911               as_bad_where (fixP->fx_file, fixP->fx_line,
23912                             _("immediate value out of range"));
23913             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
23914             newval |= rd | (rs << 3) | (value << 6);
23915           }
23916       }
23917       md_number_to_chars (buf, newval, THUMB_SIZE);
23918       break;
23919
23920     case BFD_RELOC_ARM_THUMB_IMM:
23921       newval = md_chars_to_number (buf, THUMB_SIZE);
23922       if (value < 0 || value > 255)
23923         as_bad_where (fixP->fx_file, fixP->fx_line,
23924                       _("invalid immediate: %ld is out of range"),
23925                       (long) value);
23926       newval |= value;
23927       md_number_to_chars (buf, newval, THUMB_SIZE);
23928       break;
23929
23930     case BFD_RELOC_ARM_THUMB_SHIFT:
23931       /* 5bit shift value (0..32).  LSL cannot take 32.  */
23932       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
23933       temp = newval & 0xf800;
23934       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
23935         as_bad_where (fixP->fx_file, fixP->fx_line,
23936                       _("invalid shift value: %ld"), (long) value);
23937       /* Shifts of zero must be encoded as LSL.  */
23938       if (value == 0)
23939         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
23940       /* Shifts of 32 are encoded as zero.  */
23941       else if (value == 32)
23942         value = 0;
23943       newval |= value << 6;
23944       md_number_to_chars (buf, newval, THUMB_SIZE);
23945       break;
23946
23947     case BFD_RELOC_VTABLE_INHERIT:
23948     case BFD_RELOC_VTABLE_ENTRY:
23949       fixP->fx_done = 0;
23950       return;
23951
23952     case BFD_RELOC_ARM_MOVW:
23953     case BFD_RELOC_ARM_MOVT:
23954     case BFD_RELOC_ARM_THUMB_MOVW:
23955     case BFD_RELOC_ARM_THUMB_MOVT:
23956       if (fixP->fx_done || !seg->use_rela_p)
23957         {
23958           /* REL format relocations are limited to a 16-bit addend.  */
23959           if (!fixP->fx_done)
23960             {
23961               if (value < -0x8000 || value > 0x7fff)
23962                   as_bad_where (fixP->fx_file, fixP->fx_line,
23963                                 _("offset out of range"));
23964             }
23965           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23966                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23967             {
23968               value >>= 16;
23969             }
23970
23971           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23972               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23973             {
23974               newval = get_thumb32_insn (buf);
23975               newval &= 0xfbf08f00;
23976               newval |= (value & 0xf000) << 4;
23977               newval |= (value & 0x0800) << 15;
23978               newval |= (value & 0x0700) << 4;
23979               newval |= (value & 0x00ff);
23980               put_thumb32_insn (buf, newval);
23981             }
23982           else
23983             {
23984               newval = md_chars_to_number (buf, 4);
23985               newval &= 0xfff0f000;
23986               newval |= value & 0x0fff;
23987               newval |= (value & 0xf000) << 4;
23988               md_number_to_chars (buf, newval, 4);
23989             }
23990         }
23991       return;
23992
23993    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
23994    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
23995    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
23996    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
23997       gas_assert (!fixP->fx_done);
23998       {
23999         bfd_vma insn;
24000         bfd_boolean is_mov;
24001         bfd_vma encoded_addend = value;
24002
24003         /* Check that addend can be encoded in instruction.  */
24004         if (!seg->use_rela_p && (value < 0 || value > 255))
24005           as_bad_where (fixP->fx_file, fixP->fx_line,
24006                         _("the offset 0x%08lX is not representable"),
24007                         (unsigned long) encoded_addend);
24008
24009         /* Extract the instruction.  */
24010         insn = md_chars_to_number (buf, THUMB_SIZE);
24011         is_mov = (insn & 0xf800) == 0x2000;
24012
24013         /* Encode insn.  */
24014         if (is_mov)
24015           {
24016             if (!seg->use_rela_p)
24017               insn |= encoded_addend;
24018           }
24019         else
24020           {
24021             int rd, rs;
24022
24023             /* Extract the instruction.  */
24024              /* Encoding is the following
24025                 0x8000  SUB
24026                 0x00F0  Rd
24027                 0x000F  Rs
24028              */
24029              /* The following conditions must be true :
24030                 - ADD
24031                 - Rd == Rs
24032                 - Rd <= 7
24033              */
24034             rd = (insn >> 4) & 0xf;
24035             rs = insn & 0xf;
24036             if ((insn & 0x8000) || (rd != rs) || rd > 7)
24037               as_bad_where (fixP->fx_file, fixP->fx_line,
24038                         _("Unable to process relocation for thumb opcode: %lx"),
24039                         (unsigned long) insn);
24040
24041             /* Encode as ADD immediate8 thumb 1 code.  */
24042             insn = 0x3000 | (rd << 8);
24043
24044             /* Place the encoded addend into the first 8 bits of the
24045                instruction.  */
24046             if (!seg->use_rela_p)
24047               insn |= encoded_addend;
24048           }
24049
24050         /* Update the instruction.  */
24051         md_number_to_chars (buf, insn, THUMB_SIZE);
24052       }
24053       break;
24054
24055    case BFD_RELOC_ARM_ALU_PC_G0_NC:
24056    case BFD_RELOC_ARM_ALU_PC_G0:
24057    case BFD_RELOC_ARM_ALU_PC_G1_NC:
24058    case BFD_RELOC_ARM_ALU_PC_G1:
24059    case BFD_RELOC_ARM_ALU_PC_G2:
24060    case BFD_RELOC_ARM_ALU_SB_G0_NC:
24061    case BFD_RELOC_ARM_ALU_SB_G0:
24062    case BFD_RELOC_ARM_ALU_SB_G1_NC:
24063    case BFD_RELOC_ARM_ALU_SB_G1:
24064    case BFD_RELOC_ARM_ALU_SB_G2:
24065      gas_assert (!fixP->fx_done);
24066      if (!seg->use_rela_p)
24067        {
24068          bfd_vma insn;
24069          bfd_vma encoded_addend;
24070          bfd_vma addend_abs = abs (value);
24071
24072          /* Check that the absolute value of the addend can be
24073             expressed as an 8-bit constant plus a rotation.  */
24074          encoded_addend = encode_arm_immediate (addend_abs);
24075          if (encoded_addend == (unsigned int) FAIL)
24076            as_bad_where (fixP->fx_file, fixP->fx_line,
24077                          _("the offset 0x%08lX is not representable"),
24078                          (unsigned long) addend_abs);
24079
24080          /* Extract the instruction.  */
24081          insn = md_chars_to_number (buf, INSN_SIZE);
24082
24083          /* If the addend is positive, use an ADD instruction.
24084             Otherwise use a SUB.  Take care not to destroy the S bit.  */
24085          insn &= 0xff1fffff;
24086          if (value < 0)
24087            insn |= 1 << 22;
24088          else
24089            insn |= 1 << 23;
24090
24091          /* Place the encoded addend into the first 12 bits of the
24092             instruction.  */
24093          insn &= 0xfffff000;
24094          insn |= encoded_addend;
24095
24096          /* Update the instruction.  */
24097          md_number_to_chars (buf, insn, INSN_SIZE);
24098        }
24099      break;
24100
24101     case BFD_RELOC_ARM_LDR_PC_G0:
24102     case BFD_RELOC_ARM_LDR_PC_G1:
24103     case BFD_RELOC_ARM_LDR_PC_G2:
24104     case BFD_RELOC_ARM_LDR_SB_G0:
24105     case BFD_RELOC_ARM_LDR_SB_G1:
24106     case BFD_RELOC_ARM_LDR_SB_G2:
24107       gas_assert (!fixP->fx_done);
24108       if (!seg->use_rela_p)
24109         {
24110           bfd_vma insn;
24111           bfd_vma addend_abs = abs (value);
24112
24113           /* Check that the absolute value of the addend can be
24114              encoded in 12 bits.  */
24115           if (addend_abs >= 0x1000)
24116             as_bad_where (fixP->fx_file, fixP->fx_line,
24117                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
24118                           (unsigned long) addend_abs);
24119
24120           /* Extract the instruction.  */
24121           insn = md_chars_to_number (buf, INSN_SIZE);
24122
24123           /* If the addend is negative, clear bit 23 of the instruction.
24124              Otherwise set it.  */
24125           if (value < 0)
24126             insn &= ~(1 << 23);
24127           else
24128             insn |= 1 << 23;
24129
24130           /* Place the absolute value of the addend into the first 12 bits
24131              of the instruction.  */
24132           insn &= 0xfffff000;
24133           insn |= addend_abs;
24134
24135           /* Update the instruction.  */
24136           md_number_to_chars (buf, insn, INSN_SIZE);
24137         }
24138       break;
24139
24140     case BFD_RELOC_ARM_LDRS_PC_G0:
24141     case BFD_RELOC_ARM_LDRS_PC_G1:
24142     case BFD_RELOC_ARM_LDRS_PC_G2:
24143     case BFD_RELOC_ARM_LDRS_SB_G0:
24144     case BFD_RELOC_ARM_LDRS_SB_G1:
24145     case BFD_RELOC_ARM_LDRS_SB_G2:
24146       gas_assert (!fixP->fx_done);
24147       if (!seg->use_rela_p)
24148         {
24149           bfd_vma insn;
24150           bfd_vma addend_abs = abs (value);
24151
24152           /* Check that the absolute value of the addend can be
24153              encoded in 8 bits.  */
24154           if (addend_abs >= 0x100)
24155             as_bad_where (fixP->fx_file, fixP->fx_line,
24156                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
24157                           (unsigned long) addend_abs);
24158
24159           /* Extract the instruction.  */
24160           insn = md_chars_to_number (buf, INSN_SIZE);
24161
24162           /* If the addend is negative, clear bit 23 of the instruction.
24163              Otherwise set it.  */
24164           if (value < 0)
24165             insn &= ~(1 << 23);
24166           else
24167             insn |= 1 << 23;
24168
24169           /* Place the first four bits of the absolute value of the addend
24170              into the first 4 bits of the instruction, and the remaining
24171              four into bits 8 .. 11.  */
24172           insn &= 0xfffff0f0;
24173           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
24174
24175           /* Update the instruction.  */
24176           md_number_to_chars (buf, insn, INSN_SIZE);
24177         }
24178       break;
24179
24180     case BFD_RELOC_ARM_LDC_PC_G0:
24181     case BFD_RELOC_ARM_LDC_PC_G1:
24182     case BFD_RELOC_ARM_LDC_PC_G2:
24183     case BFD_RELOC_ARM_LDC_SB_G0:
24184     case BFD_RELOC_ARM_LDC_SB_G1:
24185     case BFD_RELOC_ARM_LDC_SB_G2:
24186       gas_assert (!fixP->fx_done);
24187       if (!seg->use_rela_p)
24188         {
24189           bfd_vma insn;
24190           bfd_vma addend_abs = abs (value);
24191
24192           /* Check that the absolute value of the addend is a multiple of
24193              four and, when divided by four, fits in 8 bits.  */
24194           if (addend_abs & 0x3)
24195             as_bad_where (fixP->fx_file, fixP->fx_line,
24196                           _("bad offset 0x%08lX (must be word-aligned)"),
24197                           (unsigned long) addend_abs);
24198
24199           if ((addend_abs >> 2) > 0xff)
24200             as_bad_where (fixP->fx_file, fixP->fx_line,
24201                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
24202                           (unsigned long) addend_abs);
24203
24204           /* Extract the instruction.  */
24205           insn = md_chars_to_number (buf, INSN_SIZE);
24206
24207           /* If the addend is negative, clear bit 23 of the instruction.
24208              Otherwise set it.  */
24209           if (value < 0)
24210             insn &= ~(1 << 23);
24211           else
24212             insn |= 1 << 23;
24213
24214           /* Place the addend (divided by four) into the first eight
24215              bits of the instruction.  */
24216           insn &= 0xfffffff0;
24217           insn |= addend_abs >> 2;
24218
24219           /* Update the instruction.  */
24220           md_number_to_chars (buf, insn, INSN_SIZE);
24221         }
24222       break;
24223
24224     case BFD_RELOC_ARM_V4BX:
24225       /* This will need to go in the object file.  */
24226       fixP->fx_done = 0;
24227       break;
24228
24229     case BFD_RELOC_UNUSED:
24230     default:
24231       as_bad_where (fixP->fx_file, fixP->fx_line,
24232                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
24233     }
24234 }
24235
24236 /* Translate internal representation of relocation info to BFD target
24237    format.  */
24238
24239 arelent *
24240 tc_gen_reloc (asection *section, fixS *fixp)
24241 {
24242   arelent * reloc;
24243   bfd_reloc_code_real_type code;
24244
24245   reloc = XNEW (arelent);
24246
24247   reloc->sym_ptr_ptr = XNEW (asymbol *);
24248   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
24249   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
24250
24251   if (fixp->fx_pcrel)
24252     {
24253       if (section->use_rela_p)
24254         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
24255       else
24256         fixp->fx_offset = reloc->address;
24257     }
24258   reloc->addend = fixp->fx_offset;
24259
24260   switch (fixp->fx_r_type)
24261     {
24262     case BFD_RELOC_8:
24263       if (fixp->fx_pcrel)
24264         {
24265           code = BFD_RELOC_8_PCREL;
24266           break;
24267         }
24268       /* Fall through.  */
24269
24270     case BFD_RELOC_16:
24271       if (fixp->fx_pcrel)
24272         {
24273           code = BFD_RELOC_16_PCREL;
24274           break;
24275         }
24276       /* Fall through.  */
24277
24278     case BFD_RELOC_32:
24279       if (fixp->fx_pcrel)
24280         {
24281           code = BFD_RELOC_32_PCREL;
24282           break;
24283         }
24284       /* Fall through.  */
24285
24286     case BFD_RELOC_ARM_MOVW:
24287       if (fixp->fx_pcrel)
24288         {
24289           code = BFD_RELOC_ARM_MOVW_PCREL;
24290           break;
24291         }
24292       /* Fall through.  */
24293
24294     case BFD_RELOC_ARM_MOVT:
24295       if (fixp->fx_pcrel)
24296         {
24297           code = BFD_RELOC_ARM_MOVT_PCREL;
24298           break;
24299         }
24300       /* Fall through.  */
24301
24302     case BFD_RELOC_ARM_THUMB_MOVW:
24303       if (fixp->fx_pcrel)
24304         {
24305           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
24306           break;
24307         }
24308       /* Fall through.  */
24309
24310     case BFD_RELOC_ARM_THUMB_MOVT:
24311       if (fixp->fx_pcrel)
24312         {
24313           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
24314           break;
24315         }
24316       /* Fall through.  */
24317
24318     case BFD_RELOC_NONE:
24319     case BFD_RELOC_ARM_PCREL_BRANCH:
24320     case BFD_RELOC_ARM_PCREL_BLX:
24321     case BFD_RELOC_RVA:
24322     case BFD_RELOC_THUMB_PCREL_BRANCH7:
24323     case BFD_RELOC_THUMB_PCREL_BRANCH9:
24324     case BFD_RELOC_THUMB_PCREL_BRANCH12:
24325     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24326     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24327     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24328     case BFD_RELOC_VTABLE_ENTRY:
24329     case BFD_RELOC_VTABLE_INHERIT:
24330 #ifdef TE_PE
24331     case BFD_RELOC_32_SECREL:
24332 #endif
24333       code = fixp->fx_r_type;
24334       break;
24335
24336     case BFD_RELOC_THUMB_PCREL_BLX:
24337 #ifdef OBJ_ELF
24338       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
24339         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
24340       else
24341 #endif
24342         code = BFD_RELOC_THUMB_PCREL_BLX;
24343       break;
24344
24345     case BFD_RELOC_ARM_LITERAL:
24346     case BFD_RELOC_ARM_HWLITERAL:
24347       /* If this is called then the a literal has
24348          been referenced across a section boundary.  */
24349       as_bad_where (fixp->fx_file, fixp->fx_line,
24350                     _("literal referenced across section boundary"));
24351       return NULL;
24352
24353 #ifdef OBJ_ELF
24354     case BFD_RELOC_ARM_TLS_CALL:
24355     case BFD_RELOC_ARM_THM_TLS_CALL:
24356     case BFD_RELOC_ARM_TLS_DESCSEQ:
24357     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
24358     case BFD_RELOC_ARM_GOT32:
24359     case BFD_RELOC_ARM_GOTOFF:
24360     case BFD_RELOC_ARM_GOT_PREL:
24361     case BFD_RELOC_ARM_PLT32:
24362     case BFD_RELOC_ARM_TARGET1:
24363     case BFD_RELOC_ARM_ROSEGREL32:
24364     case BFD_RELOC_ARM_SBREL32:
24365     case BFD_RELOC_ARM_PREL31:
24366     case BFD_RELOC_ARM_TARGET2:
24367     case BFD_RELOC_ARM_TLS_LDO32:
24368     case BFD_RELOC_ARM_PCREL_CALL:
24369     case BFD_RELOC_ARM_PCREL_JUMP:
24370     case BFD_RELOC_ARM_ALU_PC_G0_NC:
24371     case BFD_RELOC_ARM_ALU_PC_G0:
24372     case BFD_RELOC_ARM_ALU_PC_G1_NC:
24373     case BFD_RELOC_ARM_ALU_PC_G1:
24374     case BFD_RELOC_ARM_ALU_PC_G2:
24375     case BFD_RELOC_ARM_LDR_PC_G0:
24376     case BFD_RELOC_ARM_LDR_PC_G1:
24377     case BFD_RELOC_ARM_LDR_PC_G2:
24378     case BFD_RELOC_ARM_LDRS_PC_G0:
24379     case BFD_RELOC_ARM_LDRS_PC_G1:
24380     case BFD_RELOC_ARM_LDRS_PC_G2:
24381     case BFD_RELOC_ARM_LDC_PC_G0:
24382     case BFD_RELOC_ARM_LDC_PC_G1:
24383     case BFD_RELOC_ARM_LDC_PC_G2:
24384     case BFD_RELOC_ARM_ALU_SB_G0_NC:
24385     case BFD_RELOC_ARM_ALU_SB_G0:
24386     case BFD_RELOC_ARM_ALU_SB_G1_NC:
24387     case BFD_RELOC_ARM_ALU_SB_G1:
24388     case BFD_RELOC_ARM_ALU_SB_G2:
24389     case BFD_RELOC_ARM_LDR_SB_G0:
24390     case BFD_RELOC_ARM_LDR_SB_G1:
24391     case BFD_RELOC_ARM_LDR_SB_G2:
24392     case BFD_RELOC_ARM_LDRS_SB_G0:
24393     case BFD_RELOC_ARM_LDRS_SB_G1:
24394     case BFD_RELOC_ARM_LDRS_SB_G2:
24395     case BFD_RELOC_ARM_LDC_SB_G0:
24396     case BFD_RELOC_ARM_LDC_SB_G1:
24397     case BFD_RELOC_ARM_LDC_SB_G2:
24398     case BFD_RELOC_ARM_V4BX:
24399     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
24400     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
24401     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
24402     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
24403       code = fixp->fx_r_type;
24404       break;
24405
24406     case BFD_RELOC_ARM_TLS_GOTDESC:
24407     case BFD_RELOC_ARM_TLS_GD32:
24408     case BFD_RELOC_ARM_TLS_LE32:
24409     case BFD_RELOC_ARM_TLS_IE32:
24410     case BFD_RELOC_ARM_TLS_LDM32:
24411       /* BFD will include the symbol's address in the addend.
24412          But we don't want that, so subtract it out again here.  */
24413       if (!S_IS_COMMON (fixp->fx_addsy))
24414         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
24415       code = fixp->fx_r_type;
24416       break;
24417 #endif
24418
24419     case BFD_RELOC_ARM_IMMEDIATE:
24420       as_bad_where (fixp->fx_file, fixp->fx_line,
24421                     _("internal relocation (type: IMMEDIATE) not fixed up"));
24422       return NULL;
24423
24424     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
24425       as_bad_where (fixp->fx_file, fixp->fx_line,
24426                     _("ADRL used for a symbol not defined in the same file"));
24427       return NULL;
24428
24429     case BFD_RELOC_ARM_OFFSET_IMM:
24430       if (section->use_rela_p)
24431         {
24432           code = fixp->fx_r_type;
24433           break;
24434         }
24435
24436       if (fixp->fx_addsy != NULL
24437           && !S_IS_DEFINED (fixp->fx_addsy)
24438           && S_IS_LOCAL (fixp->fx_addsy))
24439         {
24440           as_bad_where (fixp->fx_file, fixp->fx_line,
24441                         _("undefined local label `%s'"),
24442                         S_GET_NAME (fixp->fx_addsy));
24443           return NULL;
24444         }
24445
24446       as_bad_where (fixp->fx_file, fixp->fx_line,
24447                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
24448       return NULL;
24449
24450     default:
24451       {
24452         const char * type;
24453
24454         switch (fixp->fx_r_type)
24455           {
24456           case BFD_RELOC_NONE:             type = "NONE";         break;
24457           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
24458           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
24459           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
24460           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
24461           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
24462           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
24463           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
24464           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
24465           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
24466           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
24467           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
24468           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
24469           default:                         type = _("<unknown>"); break;
24470           }
24471         as_bad_where (fixp->fx_file, fixp->fx_line,
24472                       _("cannot represent %s relocation in this object file format"),
24473                       type);
24474         return NULL;
24475       }
24476     }
24477
24478 #ifdef OBJ_ELF
24479   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
24480       && GOT_symbol
24481       && fixp->fx_addsy == GOT_symbol)
24482     {
24483       code = BFD_RELOC_ARM_GOTPC;
24484       reloc->addend = fixp->fx_offset = reloc->address;
24485     }
24486 #endif
24487
24488   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
24489
24490   if (reloc->howto == NULL)
24491     {
24492       as_bad_where (fixp->fx_file, fixp->fx_line,
24493                     _("cannot represent %s relocation in this object file format"),
24494                     bfd_get_reloc_code_name (code));
24495       return NULL;
24496     }
24497
24498   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
24499      vtable entry to be used in the relocation's section offset.  */
24500   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24501     reloc->address = fixp->fx_offset;
24502
24503   return reloc;
24504 }
24505
24506 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
24507
24508 void
24509 cons_fix_new_arm (fragS *       frag,
24510                   int           where,
24511                   int           size,
24512                   expressionS * exp,
24513                   bfd_reloc_code_real_type reloc)
24514 {
24515   int pcrel = 0;
24516
24517   /* Pick a reloc.
24518      FIXME: @@ Should look at CPU word size.  */
24519   switch (size)
24520     {
24521     case 1:
24522       reloc = BFD_RELOC_8;
24523       break;
24524     case 2:
24525       reloc = BFD_RELOC_16;
24526       break;
24527     case 4:
24528     default:
24529       reloc = BFD_RELOC_32;
24530       break;
24531     case 8:
24532       reloc = BFD_RELOC_64;
24533       break;
24534     }
24535
24536 #ifdef TE_PE
24537   if (exp->X_op == O_secrel)
24538   {
24539     exp->X_op = O_symbol;
24540     reloc = BFD_RELOC_32_SECREL;
24541   }
24542 #endif
24543
24544   fix_new_exp (frag, where, size, exp, pcrel, reloc);
24545 }
24546
24547 #if defined (OBJ_COFF)
24548 void
24549 arm_validate_fix (fixS * fixP)
24550 {
24551   /* If the destination of the branch is a defined symbol which does not have
24552      the THUMB_FUNC attribute, then we must be calling a function which has
24553      the (interfacearm) attribute.  We look for the Thumb entry point to that
24554      function and change the branch to refer to that function instead.  */
24555   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
24556       && fixP->fx_addsy != NULL
24557       && S_IS_DEFINED (fixP->fx_addsy)
24558       && ! THUMB_IS_FUNC (fixP->fx_addsy))
24559     {
24560       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
24561     }
24562 }
24563 #endif
24564
24565
24566 int
24567 arm_force_relocation (struct fix * fixp)
24568 {
24569 #if defined (OBJ_COFF) && defined (TE_PE)
24570   if (fixp->fx_r_type == BFD_RELOC_RVA)
24571     return 1;
24572 #endif
24573
24574   /* In case we have a call or a branch to a function in ARM ISA mode from
24575      a thumb function or vice-versa force the relocation. These relocations
24576      are cleared off for some cores that might have blx and simple transformations
24577      are possible.  */
24578
24579 #ifdef OBJ_ELF
24580   switch (fixp->fx_r_type)
24581     {
24582     case BFD_RELOC_ARM_PCREL_JUMP:
24583     case BFD_RELOC_ARM_PCREL_CALL:
24584     case BFD_RELOC_THUMB_PCREL_BLX:
24585       if (THUMB_IS_FUNC (fixp->fx_addsy))
24586         return 1;
24587       break;
24588
24589     case BFD_RELOC_ARM_PCREL_BLX:
24590     case BFD_RELOC_THUMB_PCREL_BRANCH25:
24591     case BFD_RELOC_THUMB_PCREL_BRANCH20:
24592     case BFD_RELOC_THUMB_PCREL_BRANCH23:
24593       if (ARM_IS_FUNC (fixp->fx_addsy))
24594         return 1;
24595       break;
24596
24597     default:
24598       break;
24599     }
24600 #endif
24601
24602   /* Resolve these relocations even if the symbol is extern or weak.
24603      Technically this is probably wrong due to symbol preemption.
24604      In practice these relocations do not have enough range to be useful
24605      at dynamic link time, and some code (e.g. in the Linux kernel)
24606      expects these references to be resolved.  */
24607   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
24608       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
24609       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
24610       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
24611       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
24612       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
24613       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
24614       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
24615       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
24616       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
24617       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
24618       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
24619       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
24620       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
24621     return 0;
24622
24623   /* Always leave these relocations for the linker.  */
24624   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
24625        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
24626       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
24627     return 1;
24628
24629   /* Always generate relocations against function symbols.  */
24630   if (fixp->fx_r_type == BFD_RELOC_32
24631       && fixp->fx_addsy
24632       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
24633     return 1;
24634
24635   return generic_force_reloc (fixp);
24636 }
24637
24638 #if defined (OBJ_ELF) || defined (OBJ_COFF)
24639 /* Relocations against function names must be left unadjusted,
24640    so that the linker can use this information to generate interworking
24641    stubs.  The MIPS version of this function
24642    also prevents relocations that are mips-16 specific, but I do not
24643    know why it does this.
24644
24645    FIXME:
24646    There is one other problem that ought to be addressed here, but
24647    which currently is not:  Taking the address of a label (rather
24648    than a function) and then later jumping to that address.  Such
24649    addresses also ought to have their bottom bit set (assuming that
24650    they reside in Thumb code), but at the moment they will not.  */
24651
24652 bfd_boolean
24653 arm_fix_adjustable (fixS * fixP)
24654 {
24655   if (fixP->fx_addsy == NULL)
24656     return 1;
24657
24658   /* Preserve relocations against symbols with function type.  */
24659   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
24660     return FALSE;
24661
24662   if (THUMB_IS_FUNC (fixP->fx_addsy)
24663       && fixP->fx_subsy == NULL)
24664     return FALSE;
24665
24666   /* We need the symbol name for the VTABLE entries.  */
24667   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
24668       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
24669     return FALSE;
24670
24671   /* Don't allow symbols to be discarded on GOT related relocs.  */
24672   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
24673       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
24674       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
24675       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
24676       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
24677       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
24678       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
24679       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
24680       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
24681       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
24682       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
24683       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
24684       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
24685       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
24686     return FALSE;
24687
24688   /* Similarly for group relocations.  */
24689   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
24690        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
24691       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
24692     return FALSE;
24693
24694   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
24695   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
24696       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
24697       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
24698       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
24699       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
24700       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
24701       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
24702       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
24703     return FALSE;
24704
24705   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
24706      offsets, so keep these symbols.  */
24707   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
24708       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
24709     return FALSE;
24710
24711   return TRUE;
24712 }
24713 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
24714
24715 #ifdef OBJ_ELF
24716 const char *
24717 elf32_arm_target_format (void)
24718 {
24719 #ifdef TE_SYMBIAN
24720   return (target_big_endian
24721           ? "elf32-bigarm-symbian"
24722           : "elf32-littlearm-symbian");
24723 #elif defined (TE_VXWORKS)
24724   return (target_big_endian
24725           ? "elf32-bigarm-vxworks"
24726           : "elf32-littlearm-vxworks");
24727 #elif defined (TE_NACL)
24728   return (target_big_endian
24729           ? "elf32-bigarm-nacl"
24730           : "elf32-littlearm-nacl");
24731 #else
24732   if (target_big_endian)
24733     return "elf32-bigarm";
24734   else
24735     return "elf32-littlearm";
24736 #endif
24737 }
24738
24739 void
24740 armelf_frob_symbol (symbolS * symp,
24741                     int *     puntp)
24742 {
24743   elf_frob_symbol (symp, puntp);
24744 }
24745 #endif
24746
24747 /* MD interface: Finalization.  */
24748
24749 void
24750 arm_cleanup (void)
24751 {
24752   literal_pool * pool;
24753
24754   /* Ensure that all the IT blocks are properly closed.  */
24755   check_it_blocks_finished ();
24756
24757   for (pool = list_of_pools; pool; pool = pool->next)
24758     {
24759       /* Put it at the end of the relevant section.  */
24760       subseg_set (pool->section, pool->sub_section);
24761 #ifdef OBJ_ELF
24762       arm_elf_change_section ();
24763 #endif
24764       s_ltorg (0);
24765     }
24766 }
24767
24768 #ifdef OBJ_ELF
24769 /* Remove any excess mapping symbols generated for alignment frags in
24770    SEC.  We may have created a mapping symbol before a zero byte
24771    alignment; remove it if there's a mapping symbol after the
24772    alignment.  */
24773 static void
24774 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
24775                        void *dummy ATTRIBUTE_UNUSED)
24776 {
24777   segment_info_type *seginfo = seg_info (sec);
24778   fragS *fragp;
24779
24780   if (seginfo == NULL || seginfo->frchainP == NULL)
24781     return;
24782
24783   for (fragp = seginfo->frchainP->frch_root;
24784        fragp != NULL;
24785        fragp = fragp->fr_next)
24786     {
24787       symbolS *sym = fragp->tc_frag_data.last_map;
24788       fragS *next = fragp->fr_next;
24789
24790       /* Variable-sized frags have been converted to fixed size by
24791          this point.  But if this was variable-sized to start with,
24792          there will be a fixed-size frag after it.  So don't handle
24793          next == NULL.  */
24794       if (sym == NULL || next == NULL)
24795         continue;
24796
24797       if (S_GET_VALUE (sym) < next->fr_address)
24798         /* Not at the end of this frag.  */
24799         continue;
24800       know (S_GET_VALUE (sym) == next->fr_address);
24801
24802       do
24803         {
24804           if (next->tc_frag_data.first_map != NULL)
24805             {
24806               /* Next frag starts with a mapping symbol.  Discard this
24807                  one.  */
24808               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24809               break;
24810             }
24811
24812           if (next->fr_next == NULL)
24813             {
24814               /* This mapping symbol is at the end of the section.  Discard
24815                  it.  */
24816               know (next->fr_fix == 0 && next->fr_var == 0);
24817               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24818               break;
24819             }
24820
24821           /* As long as we have empty frags without any mapping symbols,
24822              keep looking.  */
24823           /* If the next frag is non-empty and does not start with a
24824              mapping symbol, then this mapping symbol is required.  */
24825           if (next->fr_address != next->fr_next->fr_address)
24826             break;
24827
24828           next = next->fr_next;
24829         }
24830       while (next != NULL);
24831     }
24832 }
24833 #endif
24834
24835 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
24836    ARM ones.  */
24837
24838 void
24839 arm_adjust_symtab (void)
24840 {
24841 #ifdef OBJ_COFF
24842   symbolS * sym;
24843
24844   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24845     {
24846       if (ARM_IS_THUMB (sym))
24847         {
24848           if (THUMB_IS_FUNC (sym))
24849             {
24850               /* Mark the symbol as a Thumb function.  */
24851               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
24852                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
24853                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
24854
24855               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
24856                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
24857               else
24858                 as_bad (_("%s: unexpected function type: %d"),
24859                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
24860             }
24861           else switch (S_GET_STORAGE_CLASS (sym))
24862             {
24863             case C_EXT:
24864               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
24865               break;
24866             case C_STAT:
24867               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
24868               break;
24869             case C_LABEL:
24870               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
24871               break;
24872             default:
24873               /* Do nothing.  */
24874               break;
24875             }
24876         }
24877
24878       if (ARM_IS_INTERWORK (sym))
24879         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
24880     }
24881 #endif
24882 #ifdef OBJ_ELF
24883   symbolS * sym;
24884   char      bind;
24885
24886   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24887     {
24888       if (ARM_IS_THUMB (sym))
24889         {
24890           elf_symbol_type * elf_sym;
24891
24892           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
24893           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
24894
24895           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
24896                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
24897             {
24898               /* If it's a .thumb_func, declare it as so,
24899                  otherwise tag label as .code 16.  */
24900               if (THUMB_IS_FUNC (sym))
24901                 ARM_SET_SYM_BRANCH_TYPE (elf_sym->internal_elf_sym.st_target_internal,
24902                                          ST_BRANCH_TO_THUMB);
24903               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
24904                 elf_sym->internal_elf_sym.st_info =
24905                   ELF_ST_INFO (bind, STT_ARM_16BIT);
24906             }
24907         }
24908     }
24909
24910   /* Remove any overlapping mapping symbols generated by alignment frags.  */
24911   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
24912   /* Now do generic ELF adjustments.  */
24913   elf_adjust_symtab ();
24914 #endif
24915 }
24916
24917 /* MD interface: Initialization.  */
24918
24919 static void
24920 set_constant_flonums (void)
24921 {
24922   int i;
24923
24924   for (i = 0; i < NUM_FLOAT_VALS; i++)
24925     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
24926       abort ();
24927 }
24928
24929 /* Auto-select Thumb mode if it's the only available instruction set for the
24930    given architecture.  */
24931
24932 static void
24933 autoselect_thumb_from_cpu_variant (void)
24934 {
24935   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
24936     opcode_select (16);
24937 }
24938
24939 void
24940 md_begin (void)
24941 {
24942   unsigned mach;
24943   unsigned int i;
24944
24945   if (   (arm_ops_hsh = hash_new ()) == NULL
24946       || (arm_cond_hsh = hash_new ()) == NULL
24947       || (arm_shift_hsh = hash_new ()) == NULL
24948       || (arm_psr_hsh = hash_new ()) == NULL
24949       || (arm_v7m_psr_hsh = hash_new ()) == NULL
24950       || (arm_reg_hsh = hash_new ()) == NULL
24951       || (arm_reloc_hsh = hash_new ()) == NULL
24952       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
24953     as_fatal (_("virtual memory exhausted"));
24954
24955   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
24956     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
24957   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
24958     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
24959   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
24960     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
24961   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
24962     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
24963   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
24964     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
24965                  (void *) (v7m_psrs + i));
24966   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
24967     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
24968   for (i = 0;
24969        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
24970        i++)
24971     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
24972                  (void *) (barrier_opt_names + i));
24973 #ifdef OBJ_ELF
24974   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
24975     {
24976       struct reloc_entry * entry = reloc_names + i;
24977
24978       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
24979         /* This makes encode_branch() use the EABI versions of this relocation.  */
24980         entry->reloc = BFD_RELOC_UNUSED;
24981
24982       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
24983     }
24984 #endif
24985
24986   set_constant_flonums ();
24987
24988   /* Set the cpu variant based on the command-line options.  We prefer
24989      -mcpu= over -march= if both are set (as for GCC); and we prefer
24990      -mfpu= over any other way of setting the floating point unit.
24991      Use of legacy options with new options are faulted.  */
24992   if (legacy_cpu)
24993     {
24994       if (mcpu_cpu_opt || march_cpu_opt)
24995         as_bad (_("use of old and new-style options to set CPU type"));
24996
24997       mcpu_cpu_opt = legacy_cpu;
24998     }
24999   else if (!mcpu_cpu_opt)
25000     mcpu_cpu_opt = march_cpu_opt;
25001
25002   if (legacy_fpu)
25003     {
25004       if (mfpu_opt)
25005         as_bad (_("use of old and new-style options to set FPU type"));
25006
25007       mfpu_opt = legacy_fpu;
25008     }
25009   else if (!mfpu_opt)
25010     {
25011 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
25012         || defined (TE_NetBSD) || defined (TE_VXWORKS))
25013       /* Some environments specify a default FPU.  If they don't, infer it
25014          from the processor.  */
25015       if (mcpu_fpu_opt)
25016         mfpu_opt = mcpu_fpu_opt;
25017       else
25018         mfpu_opt = march_fpu_opt;
25019 #else
25020       mfpu_opt = &fpu_default;
25021 #endif
25022     }
25023
25024   if (!mfpu_opt)
25025     {
25026       if (mcpu_cpu_opt != NULL)
25027         mfpu_opt = &fpu_default;
25028       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
25029         mfpu_opt = &fpu_arch_vfp_v2;
25030       else
25031         mfpu_opt = &fpu_arch_fpa;
25032     }
25033
25034 #ifdef CPU_DEFAULT
25035   if (!mcpu_cpu_opt)
25036     {
25037       mcpu_cpu_opt = &cpu_default;
25038       selected_cpu = cpu_default;
25039     }
25040   else
25041     selected_cpu = *mcpu_cpu_opt;
25042 #else
25043   if (mcpu_cpu_opt)
25044     selected_cpu = *mcpu_cpu_opt;
25045   else
25046     mcpu_cpu_opt = &arm_arch_any;
25047 #endif
25048
25049   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25050
25051   autoselect_thumb_from_cpu_variant ();
25052
25053   arm_arch_used = thumb_arch_used = arm_arch_none;
25054
25055 #if defined OBJ_COFF || defined OBJ_ELF
25056   {
25057     unsigned int flags = 0;
25058
25059 #if defined OBJ_ELF
25060     flags = meabi_flags;
25061
25062     switch (meabi_flags)
25063       {
25064       case EF_ARM_EABI_UNKNOWN:
25065 #endif
25066         /* Set the flags in the private structure.  */
25067         if (uses_apcs_26)      flags |= F_APCS26;
25068         if (support_interwork) flags |= F_INTERWORK;
25069         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
25070         if (pic_code)          flags |= F_PIC;
25071         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
25072           flags |= F_SOFT_FLOAT;
25073
25074         switch (mfloat_abi_opt)
25075           {
25076           case ARM_FLOAT_ABI_SOFT:
25077           case ARM_FLOAT_ABI_SOFTFP:
25078             flags |= F_SOFT_FLOAT;
25079             break;
25080
25081           case ARM_FLOAT_ABI_HARD:
25082             if (flags & F_SOFT_FLOAT)
25083               as_bad (_("hard-float conflicts with specified fpu"));
25084             break;
25085           }
25086
25087         /* Using pure-endian doubles (even if soft-float).      */
25088         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
25089           flags |= F_VFP_FLOAT;
25090
25091 #if defined OBJ_ELF
25092         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
25093             flags |= EF_ARM_MAVERICK_FLOAT;
25094         break;
25095
25096       case EF_ARM_EABI_VER4:
25097       case EF_ARM_EABI_VER5:
25098         /* No additional flags to set.  */
25099         break;
25100
25101       default:
25102         abort ();
25103       }
25104 #endif
25105     bfd_set_private_flags (stdoutput, flags);
25106
25107     /* We have run out flags in the COFF header to encode the
25108        status of ATPCS support, so instead we create a dummy,
25109        empty, debug section called .arm.atpcs.  */
25110     if (atpcs)
25111       {
25112         asection * sec;
25113
25114         sec = bfd_make_section (stdoutput, ".arm.atpcs");
25115
25116         if (sec != NULL)
25117           {
25118             bfd_set_section_flags
25119               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
25120             bfd_set_section_size (stdoutput, sec, 0);
25121             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
25122           }
25123       }
25124   }
25125 #endif
25126
25127   /* Record the CPU type as well.  */
25128   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
25129     mach = bfd_mach_arm_iWMMXt2;
25130   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
25131     mach = bfd_mach_arm_iWMMXt;
25132   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
25133     mach = bfd_mach_arm_XScale;
25134   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
25135     mach = bfd_mach_arm_ep9312;
25136   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
25137     mach = bfd_mach_arm_5TE;
25138   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
25139     {
25140       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25141         mach = bfd_mach_arm_5T;
25142       else
25143         mach = bfd_mach_arm_5;
25144     }
25145   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
25146     {
25147       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
25148         mach = bfd_mach_arm_4T;
25149       else
25150         mach = bfd_mach_arm_4;
25151     }
25152   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
25153     mach = bfd_mach_arm_3M;
25154   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
25155     mach = bfd_mach_arm_3;
25156   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
25157     mach = bfd_mach_arm_2a;
25158   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
25159     mach = bfd_mach_arm_2;
25160   else
25161     mach = bfd_mach_arm_unknown;
25162
25163   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
25164 }
25165
25166 /* Command line processing.  */
25167
25168 /* md_parse_option
25169       Invocation line includes a switch not recognized by the base assembler.
25170       See if it's a processor-specific option.
25171
25172       This routine is somewhat complicated by the need for backwards
25173       compatibility (since older releases of gcc can't be changed).
25174       The new options try to make the interface as compatible as
25175       possible with GCC.
25176
25177       New options (supported) are:
25178
25179               -mcpu=<cpu name>           Assemble for selected processor
25180               -march=<architecture name> Assemble for selected architecture
25181               -mfpu=<fpu architecture>   Assemble for selected FPU.
25182               -EB/-mbig-endian           Big-endian
25183               -EL/-mlittle-endian        Little-endian
25184               -k                         Generate PIC code
25185               -mthumb                    Start in Thumb mode
25186               -mthumb-interwork          Code supports ARM/Thumb interworking
25187
25188               -m[no-]warn-deprecated     Warn about deprecated features
25189               -m[no-]warn-syms           Warn when symbols match instructions
25190
25191       For now we will also provide support for:
25192
25193               -mapcs-32                  32-bit Program counter
25194               -mapcs-26                  26-bit Program counter
25195               -macps-float               Floats passed in FP registers
25196               -mapcs-reentrant           Reentrant code
25197               -matpcs
25198       (sometime these will probably be replaced with -mapcs=<list of options>
25199       and -matpcs=<list of options>)
25200
25201       The remaining options are only supported for back-wards compatibility.
25202       Cpu variants, the arm part is optional:
25203               -m[arm]1                Currently not supported.
25204               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
25205               -m[arm]3                Arm 3 processor
25206               -m[arm]6[xx],           Arm 6 processors
25207               -m[arm]7[xx][t][[d]m]   Arm 7 processors
25208               -m[arm]8[10]            Arm 8 processors
25209               -m[arm]9[20][tdmi]      Arm 9 processors
25210               -mstrongarm[110[0]]     StrongARM processors
25211               -mxscale                XScale processors
25212               -m[arm]v[2345[t[e]]]    Arm architectures
25213               -mall                   All (except the ARM1)
25214       FP variants:
25215               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
25216               -mfpe-old               (No float load/store multiples)
25217               -mvfpxd                 VFP Single precision
25218               -mvfp                   All VFP
25219               -mno-fpu                Disable all floating point instructions
25220
25221       The following CPU names are recognized:
25222               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
25223               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
25224               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
25225               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
25226               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
25227               arm10t arm10e, arm1020t, arm1020e, arm10200e,
25228               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
25229
25230       */
25231
25232 const char * md_shortopts = "m:k";
25233
25234 #ifdef ARM_BI_ENDIAN
25235 #define OPTION_EB (OPTION_MD_BASE + 0)
25236 #define OPTION_EL (OPTION_MD_BASE + 1)
25237 #else
25238 #if TARGET_BYTES_BIG_ENDIAN
25239 #define OPTION_EB (OPTION_MD_BASE + 0)
25240 #else
25241 #define OPTION_EL (OPTION_MD_BASE + 1)
25242 #endif
25243 #endif
25244 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
25245
25246 struct option md_longopts[] =
25247 {
25248 #ifdef OPTION_EB
25249   {"EB", no_argument, NULL, OPTION_EB},
25250 #endif
25251 #ifdef OPTION_EL
25252   {"EL", no_argument, NULL, OPTION_EL},
25253 #endif
25254   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
25255   {NULL, no_argument, NULL, 0}
25256 };
25257
25258
25259 size_t md_longopts_size = sizeof (md_longopts);
25260
25261 struct arm_option_table
25262 {
25263   const char *option;           /* Option name to match.  */
25264   const char *help;             /* Help information.  */
25265   int  *var;            /* Variable to change.  */
25266   int   value;          /* What to change it to.  */
25267   const char *deprecated;       /* If non-null, print this message.  */
25268 };
25269
25270 struct arm_option_table arm_opts[] =
25271 {
25272   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
25273   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
25274   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
25275    &support_interwork, 1, NULL},
25276   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
25277   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
25278   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
25279    1, NULL},
25280   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
25281   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
25282   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
25283   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
25284    NULL},
25285
25286   /* These are recognized by the assembler, but have no affect on code.  */
25287   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
25288   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
25289
25290   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
25291   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
25292    &warn_on_deprecated, 0, NULL},
25293   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
25294   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
25295   {NULL, NULL, NULL, 0, NULL}
25296 };
25297
25298 struct arm_legacy_option_table
25299 {
25300   const char *option;                           /* Option name to match.  */
25301   const arm_feature_set **var;          /* Variable to change.  */
25302   const arm_feature_set value;          /* What to change it to.  */
25303   const char *deprecated;                       /* If non-null, print this message.  */
25304 };
25305
25306 const struct arm_legacy_option_table arm_legacy_opts[] =
25307 {
25308   /* DON'T add any new processors to this list -- we want the whole list
25309      to go away...  Add them to the processors table instead.  */
25310   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25311   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
25312   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25313   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
25314   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25315   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
25316   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25317   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
25318   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25319   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
25320   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25321   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
25322   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25323   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
25324   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25325   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
25326   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25327   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
25328   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25329   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
25330   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25331   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
25332   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25333   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
25334   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25335   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
25336   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25337   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
25338   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25339   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
25340   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25341   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
25342   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25343   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
25344   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25345   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
25346   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25347   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
25348   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25349   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
25350   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25351   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
25352   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25353   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
25354   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25355   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
25356   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25357   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25358   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25359   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
25360   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25361   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
25362   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25363   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
25364   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25365   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
25366   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25367   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
25368   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25369   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
25370   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25371   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
25372   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25373   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
25374   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25375   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
25376   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25377   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
25378   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
25379   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
25380    N_("use -mcpu=strongarm110")},
25381   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
25382    N_("use -mcpu=strongarm1100")},
25383   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
25384    N_("use -mcpu=strongarm1110")},
25385   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
25386   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
25387   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
25388
25389   /* Architecture variants -- don't add any more to this list either.  */
25390   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25391   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
25392   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25393   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
25394   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25395   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
25396   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25397   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
25398   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25399   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
25400   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25401   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
25402   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25403   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
25404   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25405   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
25406   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25407   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
25408
25409   /* Floating point variants -- don't add any more to this list either.  */
25410   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
25411   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
25412   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
25413   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
25414    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
25415
25416   {NULL, NULL, ARM_ARCH_NONE, NULL}
25417 };
25418
25419 struct arm_cpu_option_table
25420 {
25421   const char *name;
25422   size_t name_len;
25423   const arm_feature_set value;
25424   /* For some CPUs we assume an FPU unless the user explicitly sets
25425      -mfpu=...  */
25426   const arm_feature_set default_fpu;
25427   /* The canonical name of the CPU, or NULL to use NAME converted to upper
25428      case.  */
25429   const char *canonical_name;
25430 };
25431
25432 /* This list should, at a minimum, contain all the cpu names
25433    recognized by GCC.  */
25434 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
25435 static const struct arm_cpu_option_table arm_cpus[] =
25436 {
25437   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
25438   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
25439   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
25440   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
25441   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
25442   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25443   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25444   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25445   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25446   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25447   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25448   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25449   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25450   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25451   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25452   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
25453   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25454   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25455   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25456   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25457   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25458   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25459   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25460   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25461   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25462   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25463   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25464   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
25465   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25466   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25467   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25468   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25469   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25470   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25471   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25472   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25473   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25474   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25475   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25476   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
25477   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25478   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25479   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25480   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
25481   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25482   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
25483   /* For V5 or later processors we default to using VFP; but the user
25484      should really set the FPU type explicitly.  */
25485   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25486   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25487   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
25488   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
25489   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
25490   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25491   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
25492   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25493   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
25494   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
25495   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25496   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25497   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25498   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25499   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25500   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
25501   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
25502   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25503   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25504   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
25505                                                                  "ARM1026EJ-S"),
25506   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
25507   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25508   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25509   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25510   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25511   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
25512   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
25513   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
25514   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
25515                                                                  "ARM1136JF-S"),
25516   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
25517   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
25518   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
25519   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
25520   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
25521   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6KZ,   FPU_NONE,        NULL),
25522   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6KZ,   FPU_ARCH_VFP_V2, NULL),
25523   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
25524                                                  FPU_NONE,        "Cortex-A5"),
25525   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25526                                                                   "Cortex-A7"),
25527   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
25528                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
25529                                                         | FPU_NEON_EXT_V1),
25530                                                                   "Cortex-A8"),
25531   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
25532                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
25533                                                         | FPU_NEON_EXT_V1),
25534                                                                   "Cortex-A9"),
25535   ARM_CPU_OPT ("cortex-a12",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25536                                                                   "Cortex-A12"),
25537   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25538                                                                   "Cortex-A15"),
25539   ARM_CPU_OPT ("cortex-a17",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
25540                                                                   "Cortex-A17"),
25541   ARM_CPU_OPT ("cortex-a32",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25542                                                                   "Cortex-A32"),
25543   ARM_CPU_OPT ("cortex-a35",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25544                                                                   "Cortex-A35"),
25545   ARM_CPU_OPT ("cortex-a53",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25546                                                                   "Cortex-A53"),
25547   ARM_CPU_OPT ("cortex-a57",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25548                                                                   "Cortex-A57"),
25549   ARM_CPU_OPT ("cortex-a72",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25550                                                                   "Cortex-A72"),
25551   ARM_CPU_OPT ("cortex-a73",    ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25552                                                                   "Cortex-A73"),
25553   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
25554   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
25555                                                                   "Cortex-R4F"),
25556   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
25557                                                  FPU_NONE,        "Cortex-R5"),
25558   ARM_CPU_OPT ("cortex-r7",     ARM_ARCH_V7R_IDIV,
25559                                                  FPU_ARCH_VFP_V3D16,
25560                                                                   "Cortex-R7"),
25561   ARM_CPU_OPT ("cortex-r8",     ARM_ARCH_V7R_IDIV,
25562                                                  FPU_ARCH_VFP_V3D16,
25563                                                                   "Cortex-R8"),
25564   ARM_CPU_OPT ("cortex-m33",    ARM_ARCH_V8M_MAIN_DSP,
25565                                                  FPU_NONE,        "Cortex-M33"),
25566   ARM_CPU_OPT ("cortex-m23",    ARM_ARCH_V8M_BASE,
25567                                                  FPU_NONE,        "Cortex-M23"),
25568   ARM_CPU_OPT ("cortex-m7",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M7"),
25569   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
25570   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
25571   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
25572   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
25573   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
25574   ARM_CPU_OPT ("exynos-m1",     ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25575                                                                   "Samsung " \
25576                                                                   "Exynos M1"),
25577   ARM_CPU_OPT ("falkor",        ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25578                                                                   "Qualcomm "
25579                                                                   "Falkor"),
25580   ARM_CPU_OPT ("qdf24xx",       ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25581                                                                   "Qualcomm "
25582                                                                   "QDF24XX"),
25583
25584   /* ??? XSCALE is really an architecture.  */
25585   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
25586   /* ??? iwmmxt is not a processor.  */
25587   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
25588   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
25589   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
25590   /* Maverick */
25591   ARM_CPU_OPT ("ep9312",        ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
25592                                                  FPU_ARCH_MAVERICK, "ARM920T"),
25593   /* Marvell processors.  */
25594   ARM_CPU_OPT ("marvell-pj4",   ARM_FEATURE_CORE (ARM_AEXT_V7A | ARM_EXT_MP
25595                                                   | ARM_EXT_SEC,
25596                                                   ARM_EXT2_V6T2_V8M),
25597                                                 FPU_ARCH_VFP_V3D16, NULL),
25598   ARM_CPU_OPT ("marvell-whitney", ARM_FEATURE_CORE (ARM_AEXT_V7A | ARM_EXT_MP
25599                                                     | ARM_EXT_SEC,
25600                                                     ARM_EXT2_V6T2_V8M),
25601                                                FPU_ARCH_NEON_VFP_V4, NULL),
25602   /* APM X-Gene family.  */
25603   ARM_CPU_OPT ("xgene1",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25604                                                                   "APM X-Gene 1"),
25605   ARM_CPU_OPT ("xgene2",        ARM_ARCH_V8A_CRC, FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25606                                                                   "APM X-Gene 2"),
25607
25608   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
25609 };
25610 #undef ARM_CPU_OPT
25611
25612 struct arm_arch_option_table
25613 {
25614   const char *name;
25615   size_t name_len;
25616   const arm_feature_set value;
25617   const arm_feature_set default_fpu;
25618 };
25619
25620 /* This list should, at a minimum, contain all the architecture names
25621    recognized by GCC.  */
25622 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
25623 static const struct arm_arch_option_table arm_archs[] =
25624 {
25625   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
25626   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
25627   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
25628   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
25629   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
25630   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
25631   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
25632   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
25633   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
25634   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
25635   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
25636   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
25637   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
25638   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
25639   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
25640   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
25641   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
25642   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
25643   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
25644   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
25645   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
25646   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
25647      kept to preserve existing behaviour.  */
25648   ARM_ARCH_OPT ("armv6kz",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
25649   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
25650   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
25651   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
25652   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
25653   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
25654      kept to preserve existing behaviour.  */
25655   ARM_ARCH_OPT ("armv6kzt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
25656   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
25657   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
25658   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
25659   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
25660   /* The official spelling of the ARMv7 profile variants is the dashed form.
25661      Accept the non-dashed form for compatibility with old toolchains.  */
25662   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
25663   ARM_ARCH_OPT ("armv7ve",      ARM_ARCH_V7VE,   FPU_ARCH_VFP),
25664   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
25665   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
25666   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
25667   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
25668   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
25669   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
25670   ARM_ARCH_OPT ("armv8-m.base", ARM_ARCH_V8M_BASE, FPU_ARCH_VFP),
25671   ARM_ARCH_OPT ("armv8-m.main", ARM_ARCH_V8M_MAIN, FPU_ARCH_VFP),
25672   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
25673   ARM_ARCH_OPT ("armv8.1-a",    ARM_ARCH_V8_1A,  FPU_ARCH_VFP),
25674   ARM_ARCH_OPT ("armv8.2-a",    ARM_ARCH_V8_2A,  FPU_ARCH_VFP),
25675   ARM_ARCH_OPT ("armv8.3-a",    ARM_ARCH_V8_3A,  FPU_ARCH_VFP),
25676   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
25677   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
25678   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
25679   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
25680 };
25681 #undef ARM_ARCH_OPT
25682
25683 /* ISA extensions in the co-processor and main instruction set space.  */
25684 struct arm_option_extension_value_table
25685 {
25686   const char *name;
25687   size_t name_len;
25688   const arm_feature_set merge_value;
25689   const arm_feature_set clear_value;
25690   /* List of architectures for which an extension is available.  ARM_ARCH_NONE
25691      indicates that an extension is available for all architectures while
25692      ARM_ANY marks an empty entry.  */
25693   const arm_feature_set allowed_archs[2];
25694 };
25695
25696 /* The following table must be in alphabetical order with a NULL last entry.
25697    */
25698 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, { AA, ARM_ANY } }
25699 #define ARM_EXT_OPT2(N, M, C, AA1, AA2) { N, sizeof (N) - 1, M, C, {AA1, AA2} }
25700 static const struct arm_option_extension_value_table arm_extensions[] =
25701 {
25702   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
25703                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25704   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
25705                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
25706                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25707   ARM_EXT_OPT ("dsp",   ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
25708                         ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP | ARM_EXT_V6_DSP),
25709                         ARM_FEATURE_CORE (ARM_EXT_V7M, ARM_EXT2_V8M)),
25710   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
25711                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25712   ARM_EXT_OPT ("fp16",  ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
25713                         ARM_FEATURE_CORE_HIGH (ARM_EXT2_FP16_INST),
25714                         ARM_ARCH_V8_2A),
25715   ARM_EXT_OPT2 ("idiv", ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
25716                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
25717                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
25718                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
25719   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
25720                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ARCH_NONE),
25721   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
25722                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ARCH_NONE),
25723   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
25724                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ARCH_NONE),
25725   ARM_EXT_OPT2 ("mp",   ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
25726                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
25727                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A),
25728                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7R)),
25729   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
25730                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
25731                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
25732   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
25733                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
25734                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25735   ARM_EXT_OPT ("ras",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_RAS),
25736                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_RAS, 0),
25737                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25738   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8_1,
25739                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
25740                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25741   ARM_EXT_OPT2 ("sec",  ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
25742                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
25743                         ARM_FEATURE_CORE_LOW (ARM_EXT_V6K),
25744                         ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
25745   ARM_EXT_OPT ("simd",  FPU_ARCH_NEON_VFP_ARMV8,
25746                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
25747                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
25748   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
25749                                      | ARM_EXT_DIV),
25750                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
25751                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
25752   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
25753                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ARCH_NONE),
25754   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, { ARM_ARCH_NONE, ARM_ARCH_NONE } }
25755 };
25756 #undef ARM_EXT_OPT
25757
25758 /* ISA floating-point and Advanced SIMD extensions.  */
25759 struct arm_option_fpu_value_table
25760 {
25761   const char *name;
25762   const arm_feature_set value;
25763 };
25764
25765 /* This list should, at a minimum, contain all the fpu names
25766    recognized by GCC.  */
25767 static const struct arm_option_fpu_value_table arm_fpus[] =
25768 {
25769   {"softfpa",           FPU_NONE},
25770   {"fpe",               FPU_ARCH_FPE},
25771   {"fpe2",              FPU_ARCH_FPE},
25772   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
25773   {"fpa",               FPU_ARCH_FPA},
25774   {"fpa10",             FPU_ARCH_FPA},
25775   {"fpa11",             FPU_ARCH_FPA},
25776   {"arm7500fe",         FPU_ARCH_FPA},
25777   {"softvfp",           FPU_ARCH_VFP},
25778   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
25779   {"vfp",               FPU_ARCH_VFP_V2},
25780   {"vfp9",              FPU_ARCH_VFP_V2},
25781   {"vfp3",              FPU_ARCH_VFP_V3}, /* Undocumented, use vfpv3.  */
25782   {"vfp10",             FPU_ARCH_VFP_V2},
25783   {"vfp10-r0",          FPU_ARCH_VFP_V1},
25784   {"vfpxd",             FPU_ARCH_VFP_V1xD},
25785   {"vfpv2",             FPU_ARCH_VFP_V2},
25786   {"vfpv3",             FPU_ARCH_VFP_V3},
25787   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
25788   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
25789   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
25790   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
25791   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
25792   {"arm1020t",          FPU_ARCH_VFP_V1},
25793   {"arm1020e",          FPU_ARCH_VFP_V2},
25794   {"arm1136jfs",        FPU_ARCH_VFP_V2}, /* Undocumented, use arm1136jf-s.  */
25795   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
25796   {"maverick",          FPU_ARCH_MAVERICK},
25797   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
25798   {"neon-vfpv3",        FPU_ARCH_VFP_V3_PLUS_NEON_V1},
25799   {"neon-fp16",         FPU_ARCH_NEON_FP16},
25800   {"vfpv4",             FPU_ARCH_VFP_V4},
25801   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
25802   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
25803   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
25804   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
25805   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
25806   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
25807   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
25808   {"crypto-neon-fp-armv8",
25809                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
25810   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
25811   {"crypto-neon-fp-armv8.1",
25812                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
25813   {NULL,                ARM_ARCH_NONE}
25814 };
25815
25816 struct arm_option_value_table
25817 {
25818   const char *name;
25819   long value;
25820 };
25821
25822 static const struct arm_option_value_table arm_float_abis[] =
25823 {
25824   {"hard",      ARM_FLOAT_ABI_HARD},
25825   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
25826   {"soft",      ARM_FLOAT_ABI_SOFT},
25827   {NULL,        0}
25828 };
25829
25830 #ifdef OBJ_ELF
25831 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
25832 static const struct arm_option_value_table arm_eabis[] =
25833 {
25834   {"gnu",       EF_ARM_EABI_UNKNOWN},
25835   {"4",         EF_ARM_EABI_VER4},
25836   {"5",         EF_ARM_EABI_VER5},
25837   {NULL,        0}
25838 };
25839 #endif
25840
25841 struct arm_long_option_table
25842 {
25843   const char * option;          /* Substring to match.  */
25844   const char * help;                    /* Help information.  */
25845   int (* func) (const char * subopt);   /* Function to decode sub-option.  */
25846   const char * deprecated;              /* If non-null, print this message.  */
25847 };
25848
25849 static bfd_boolean
25850 arm_parse_extension (const char *str, const arm_feature_set **opt_p)
25851 {
25852   arm_feature_set *ext_set = XNEW (arm_feature_set);
25853
25854   /* We insist on extensions being specified in alphabetical order, and with
25855      extensions being added before being removed.  We achieve this by having
25856      the global ARM_EXTENSIONS table in alphabetical order, and using the
25857      ADDING_VALUE variable to indicate whether we are adding an extension (1)
25858      or removing it (0) and only allowing it to change in the order
25859      -1 -> 1 -> 0.  */
25860   const struct arm_option_extension_value_table * opt = NULL;
25861   const arm_feature_set arm_any = ARM_ANY;
25862   int adding_value = -1;
25863
25864   /* Copy the feature set, so that we can modify it.  */
25865   *ext_set = **opt_p;
25866   *opt_p = ext_set;
25867
25868   while (str != NULL && *str != 0)
25869     {
25870       const char *ext;
25871       size_t len;
25872
25873       if (*str != '+')
25874         {
25875           as_bad (_("invalid architectural extension"));
25876           return FALSE;
25877         }
25878
25879       str++;
25880       ext = strchr (str, '+');
25881
25882       if (ext != NULL)
25883         len = ext - str;
25884       else
25885         len = strlen (str);
25886
25887       if (len >= 2 && strncmp (str, "no", 2) == 0)
25888         {
25889           if (adding_value != 0)
25890             {
25891               adding_value = 0;
25892               opt = arm_extensions;
25893             }
25894
25895           len -= 2;
25896           str += 2;
25897         }
25898       else if (len > 0)
25899         {
25900           if (adding_value == -1)
25901             {
25902               adding_value = 1;
25903               opt = arm_extensions;
25904             }
25905           else if (adding_value != 1)
25906             {
25907               as_bad (_("must specify extensions to add before specifying "
25908                         "those to remove"));
25909               return FALSE;
25910             }
25911         }
25912
25913       if (len == 0)
25914         {
25915           as_bad (_("missing architectural extension"));
25916           return FALSE;
25917         }
25918
25919       gas_assert (adding_value != -1);
25920       gas_assert (opt != NULL);
25921
25922       /* Scan over the options table trying to find an exact match. */
25923       for (; opt->name != NULL; opt++)
25924         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25925           {
25926             int i, nb_allowed_archs =
25927               sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[0]);
25928             /* Check we can apply the extension to this architecture.  */
25929             for (i = 0; i < nb_allowed_archs; i++)
25930               {
25931                 /* Empty entry.  */
25932                 if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
25933                   continue;
25934                 if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *ext_set))
25935                   break;
25936               }
25937             if (i == nb_allowed_archs)
25938               {
25939                 as_bad (_("extension does not apply to the base architecture"));
25940                 return FALSE;
25941               }
25942
25943             /* Add or remove the extension.  */
25944             if (adding_value)
25945               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
25946             else
25947               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
25948
25949             break;
25950           }
25951
25952       if (opt->name == NULL)
25953         {
25954           /* Did we fail to find an extension because it wasn't specified in
25955              alphabetical order, or because it does not exist?  */
25956
25957           for (opt = arm_extensions; opt->name != NULL; opt++)
25958             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25959               break;
25960
25961           if (opt->name == NULL)
25962             as_bad (_("unknown architectural extension `%s'"), str);
25963           else
25964             as_bad (_("architectural extensions must be specified in "
25965                       "alphabetical order"));
25966
25967           return FALSE;
25968         }
25969       else
25970         {
25971           /* We should skip the extension we've just matched the next time
25972              round.  */
25973           opt++;
25974         }
25975
25976       str = ext;
25977     };
25978
25979   return TRUE;
25980 }
25981
25982 static bfd_boolean
25983 arm_parse_cpu (const char *str)
25984 {
25985   const struct arm_cpu_option_table *opt;
25986   const char *ext = strchr (str, '+');
25987   size_t len;
25988
25989   if (ext != NULL)
25990     len = ext - str;
25991   else
25992     len = strlen (str);
25993
25994   if (len == 0)
25995     {
25996       as_bad (_("missing cpu name `%s'"), str);
25997       return FALSE;
25998     }
25999
26000   for (opt = arm_cpus; opt->name != NULL; opt++)
26001     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26002       {
26003         mcpu_cpu_opt = &opt->value;
26004         mcpu_fpu_opt = &opt->default_fpu;
26005         if (opt->canonical_name)
26006           {
26007             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
26008             strcpy (selected_cpu_name, opt->canonical_name);
26009           }
26010         else
26011           {
26012             size_t i;
26013
26014             if (len >= sizeof selected_cpu_name)
26015               len = (sizeof selected_cpu_name) - 1;
26016
26017             for (i = 0; i < len; i++)
26018               selected_cpu_name[i] = TOUPPER (opt->name[i]);
26019             selected_cpu_name[i] = 0;
26020           }
26021
26022         if (ext != NULL)
26023           return arm_parse_extension (ext, &mcpu_cpu_opt);
26024
26025         return TRUE;
26026       }
26027
26028   as_bad (_("unknown cpu `%s'"), str);
26029   return FALSE;
26030 }
26031
26032 static bfd_boolean
26033 arm_parse_arch (const char *str)
26034 {
26035   const struct arm_arch_option_table *opt;
26036   const char *ext = strchr (str, '+');
26037   size_t len;
26038
26039   if (ext != NULL)
26040     len = ext - str;
26041   else
26042     len = strlen (str);
26043
26044   if (len == 0)
26045     {
26046       as_bad (_("missing architecture name `%s'"), str);
26047       return FALSE;
26048     }
26049
26050   for (opt = arm_archs; opt->name != NULL; opt++)
26051     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
26052       {
26053         march_cpu_opt = &opt->value;
26054         march_fpu_opt = &opt->default_fpu;
26055         strcpy (selected_cpu_name, opt->name);
26056
26057         if (ext != NULL)
26058           return arm_parse_extension (ext, &march_cpu_opt);
26059
26060         return TRUE;
26061       }
26062
26063   as_bad (_("unknown architecture `%s'\n"), str);
26064   return FALSE;
26065 }
26066
26067 static bfd_boolean
26068 arm_parse_fpu (const char * str)
26069 {
26070   const struct arm_option_fpu_value_table * opt;
26071
26072   for (opt = arm_fpus; opt->name != NULL; opt++)
26073     if (streq (opt->name, str))
26074       {
26075         mfpu_opt = &opt->value;
26076         return TRUE;
26077       }
26078
26079   as_bad (_("unknown floating point format `%s'\n"), str);
26080   return FALSE;
26081 }
26082
26083 static bfd_boolean
26084 arm_parse_float_abi (const char * str)
26085 {
26086   const struct arm_option_value_table * opt;
26087
26088   for (opt = arm_float_abis; opt->name != NULL; opt++)
26089     if (streq (opt->name, str))
26090       {
26091         mfloat_abi_opt = opt->value;
26092         return TRUE;
26093       }
26094
26095   as_bad (_("unknown floating point abi `%s'\n"), str);
26096   return FALSE;
26097 }
26098
26099 #ifdef OBJ_ELF
26100 static bfd_boolean
26101 arm_parse_eabi (const char * str)
26102 {
26103   const struct arm_option_value_table *opt;
26104
26105   for (opt = arm_eabis; opt->name != NULL; opt++)
26106     if (streq (opt->name, str))
26107       {
26108         meabi_flags = opt->value;
26109         return TRUE;
26110       }
26111   as_bad (_("unknown EABI `%s'\n"), str);
26112   return FALSE;
26113 }
26114 #endif
26115
26116 static bfd_boolean
26117 arm_parse_it_mode (const char * str)
26118 {
26119   bfd_boolean ret = TRUE;
26120
26121   if (streq ("arm", str))
26122     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
26123   else if (streq ("thumb", str))
26124     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
26125   else if (streq ("always", str))
26126     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
26127   else if (streq ("never", str))
26128     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
26129   else
26130     {
26131       as_bad (_("unknown implicit IT mode `%s', should be "\
26132                 "arm, thumb, always, or never."), str);
26133       ret = FALSE;
26134     }
26135
26136   return ret;
26137 }
26138
26139 static bfd_boolean
26140 arm_ccs_mode (const char * unused ATTRIBUTE_UNUSED)
26141 {
26142   codecomposer_syntax = TRUE;
26143   arm_comment_chars[0] = ';';
26144   arm_line_separator_chars[0] = 0;
26145   return TRUE;
26146 }
26147
26148 struct arm_long_option_table arm_long_opts[] =
26149 {
26150   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
26151    arm_parse_cpu, NULL},
26152   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
26153    arm_parse_arch, NULL},
26154   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
26155    arm_parse_fpu, NULL},
26156   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
26157    arm_parse_float_abi, NULL},
26158 #ifdef OBJ_ELF
26159   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
26160    arm_parse_eabi, NULL},
26161 #endif
26162   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
26163    arm_parse_it_mode, NULL},
26164   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
26165    arm_ccs_mode, NULL},
26166   {NULL, NULL, 0, NULL}
26167 };
26168
26169 int
26170 md_parse_option (int c, const char * arg)
26171 {
26172   struct arm_option_table *opt;
26173   const struct arm_legacy_option_table *fopt;
26174   struct arm_long_option_table *lopt;
26175
26176   switch (c)
26177     {
26178 #ifdef OPTION_EB
26179     case OPTION_EB:
26180       target_big_endian = 1;
26181       break;
26182 #endif
26183
26184 #ifdef OPTION_EL
26185     case OPTION_EL:
26186       target_big_endian = 0;
26187       break;
26188 #endif
26189
26190     case OPTION_FIX_V4BX:
26191       fix_v4bx = TRUE;
26192       break;
26193
26194     case 'a':
26195       /* Listing option.  Just ignore these, we don't support additional
26196          ones.  */
26197       return 0;
26198
26199     default:
26200       for (opt = arm_opts; opt->option != NULL; opt++)
26201         {
26202           if (c == opt->option[0]
26203               && ((arg == NULL && opt->option[1] == 0)
26204                   || streq (arg, opt->option + 1)))
26205             {
26206               /* If the option is deprecated, tell the user.  */
26207               if (warn_on_deprecated && opt->deprecated != NULL)
26208                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26209                            arg ? arg : "", _(opt->deprecated));
26210
26211               if (opt->var != NULL)
26212                 *opt->var = opt->value;
26213
26214               return 1;
26215             }
26216         }
26217
26218       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
26219         {
26220           if (c == fopt->option[0]
26221               && ((arg == NULL && fopt->option[1] == 0)
26222                   || streq (arg, fopt->option + 1)))
26223             {
26224               /* If the option is deprecated, tell the user.  */
26225               if (warn_on_deprecated && fopt->deprecated != NULL)
26226                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
26227                            arg ? arg : "", _(fopt->deprecated));
26228
26229               if (fopt->var != NULL)
26230                 *fopt->var = &fopt->value;
26231
26232               return 1;
26233             }
26234         }
26235
26236       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26237         {
26238           /* These options are expected to have an argument.  */
26239           if (c == lopt->option[0]
26240               && arg != NULL
26241               && strncmp (arg, lopt->option + 1,
26242                           strlen (lopt->option + 1)) == 0)
26243             {
26244               /* If the option is deprecated, tell the user.  */
26245               if (warn_on_deprecated && lopt->deprecated != NULL)
26246                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
26247                            _(lopt->deprecated));
26248
26249               /* Call the sup-option parser.  */
26250               return lopt->func (arg + strlen (lopt->option) - 1);
26251             }
26252         }
26253
26254       return 0;
26255     }
26256
26257   return 1;
26258 }
26259
26260 void
26261 md_show_usage (FILE * fp)
26262 {
26263   struct arm_option_table *opt;
26264   struct arm_long_option_table *lopt;
26265
26266   fprintf (fp, _(" ARM-specific assembler options:\n"));
26267
26268   for (opt = arm_opts; opt->option != NULL; opt++)
26269     if (opt->help != NULL)
26270       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
26271
26272   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
26273     if (lopt->help != NULL)
26274       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
26275
26276 #ifdef OPTION_EB
26277   fprintf (fp, _("\
26278   -EB                     assemble code for a big-endian cpu\n"));
26279 #endif
26280
26281 #ifdef OPTION_EL
26282   fprintf (fp, _("\
26283   -EL                     assemble code for a little-endian cpu\n"));
26284 #endif
26285
26286   fprintf (fp, _("\
26287   --fix-v4bx              Allow BX in ARMv4 code\n"));
26288 }
26289
26290
26291 #ifdef OBJ_ELF
26292 typedef struct
26293 {
26294   int val;
26295   arm_feature_set flags;
26296 } cpu_arch_ver_table;
26297
26298 /* Mapping from CPU features to EABI CPU arch values.  As a general rule, table
26299    must be sorted least features first but some reordering is needed, eg. for
26300    Thumb-2 instructions to be detected as coming from ARMv6T2.  */
26301 static const cpu_arch_ver_table cpu_arch_ver[] =
26302 {
26303     {1, ARM_ARCH_V4},
26304     {2, ARM_ARCH_V4T},
26305     {3, ARM_ARCH_V5},
26306     {3, ARM_ARCH_V5T},
26307     {4, ARM_ARCH_V5TE},
26308     {5, ARM_ARCH_V5TEJ},
26309     {6, ARM_ARCH_V6},
26310     {9, ARM_ARCH_V6K},
26311     {7, ARM_ARCH_V6Z},
26312     {11, ARM_ARCH_V6M},
26313     {12, ARM_ARCH_V6SM},
26314     {8, ARM_ARCH_V6T2},
26315     {10, ARM_ARCH_V7VE},
26316     {10, ARM_ARCH_V7R},
26317     {10, ARM_ARCH_V7M},
26318     {14, ARM_ARCH_V8A},
26319     {16, ARM_ARCH_V8M_BASE},
26320     {17, ARM_ARCH_V8M_MAIN},
26321     {0, ARM_ARCH_NONE}
26322 };
26323
26324 /* Set an attribute if it has not already been set by the user.  */
26325 static void
26326 aeabi_set_attribute_int (int tag, int value)
26327 {
26328   if (tag < 1
26329       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26330       || !attributes_set_explicitly[tag])
26331     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
26332 }
26333
26334 static void
26335 aeabi_set_attribute_string (int tag, const char *value)
26336 {
26337   if (tag < 1
26338       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
26339       || !attributes_set_explicitly[tag])
26340     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
26341 }
26342
26343 /* Set the public EABI object attributes.  */
26344 void
26345 aeabi_set_public_attributes (void)
26346 {
26347   int arch;
26348   char profile;
26349   int virt_sec = 0;
26350   int fp16_optional = 0;
26351   arm_feature_set arm_arch = ARM_ARCH_NONE;
26352   arm_feature_set flags;
26353   arm_feature_set tmp;
26354   arm_feature_set arm_arch_v8m_base = ARM_ARCH_V8M_BASE;
26355   const cpu_arch_ver_table *p;
26356
26357   /* Choose the architecture based on the capabilities of the requested cpu
26358      (if any) and/or the instructions actually used.  */
26359   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
26360   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
26361   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
26362
26363   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
26364     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
26365
26366   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
26367     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
26368
26369   selected_cpu = flags;
26370
26371   /* Allow the user to override the reported architecture.  */
26372   if (object_arch)
26373     {
26374       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
26375       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
26376     }
26377
26378   /* We need to make sure that the attributes do not identify us as v6S-M
26379      when the only v6S-M feature in use is the Operating System Extensions.  */
26380   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
26381       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
26382         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
26383
26384   tmp = flags;
26385   arch = 0;
26386   for (p = cpu_arch_ver; p->val; p++)
26387     {
26388       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
26389         {
26390           arch = p->val;
26391           arm_arch = p->flags;
26392           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
26393         }
26394     }
26395
26396   /* The table lookup above finds the last architecture to contribute
26397      a new feature.  Unfortunately, Tag13 is a subset of the union of
26398      v6T2 and v7-M, so it is never seen as contributing a new feature.
26399      We can not search for the last entry which is entirely used,
26400      because if no CPU is specified we build up only those flags
26401      actually used.  Perhaps we should separate out the specified
26402      and implicit cases.  Avoid taking this path for -march=all by
26403      checking for contradictory v7-A / v7-M features.  */
26404   if (arch == TAG_CPU_ARCH_V7
26405       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
26406       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
26407       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
26408     {
26409       arch = TAG_CPU_ARCH_V7E_M;
26410       arm_arch = (arm_feature_set) ARM_ARCH_V7EM;
26411     }
26412
26413   ARM_CLEAR_FEATURE (tmp, flags, arm_arch_v8m_base);
26414   if (arch == TAG_CPU_ARCH_V8M_BASE && ARM_CPU_HAS_FEATURE (tmp, arm_arch_any))
26415     {
26416       arch = TAG_CPU_ARCH_V8M_MAIN;
26417       arm_arch = (arm_feature_set) ARM_ARCH_V8M_MAIN;
26418     }
26419
26420   /* In cpu_arch_ver ARMv8-A is before ARMv8-M for atomics to be detected as
26421      coming from ARMv8-A.  However, since ARMv8-A has more instructions than
26422      ARMv8-M, -march=all must be detected as ARMv8-A.  */
26423   if (arch == TAG_CPU_ARCH_V8M_MAIN
26424       && ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
26425     {
26426       arch = TAG_CPU_ARCH_V8;
26427       arm_arch = (arm_feature_set) ARM_ARCH_V8A;
26428     }
26429
26430   /* Tag_CPU_name.  */
26431   if (selected_cpu_name[0])
26432     {
26433       char *q;
26434
26435       q = selected_cpu_name;
26436       if (strncmp (q, "armv", 4) == 0)
26437         {
26438           int i;
26439
26440           q += 4;
26441           for (i = 0; q[i]; i++)
26442             q[i] = TOUPPER (q[i]);
26443         }
26444       aeabi_set_attribute_string (Tag_CPU_name, q);
26445     }
26446
26447   /* Tag_CPU_arch.  */
26448   aeabi_set_attribute_int (Tag_CPU_arch, arch);
26449
26450   /* Tag_CPU_arch_profile.  */
26451   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
26452       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26453       || (ARM_CPU_HAS_FEATURE (flags, arm_ext_atomics)
26454           && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only)))
26455     profile = 'A';
26456   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
26457     profile = 'R';
26458   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
26459     profile = 'M';
26460   else
26461     profile = '\0';
26462
26463   if (profile != '\0')
26464     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
26465
26466   /* Tag_DSP_extension.  */
26467   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_dsp))
26468     {
26469       arm_feature_set ext;
26470
26471       /* DSP instructions not in architecture.  */
26472       ARM_CLEAR_FEATURE (ext, flags, arm_arch);
26473       if (ARM_CPU_HAS_FEATURE (ext, arm_ext_dsp))
26474         aeabi_set_attribute_int (Tag_DSP_extension, 1);
26475     }
26476
26477   /* Tag_ARM_ISA_use.  */
26478   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
26479       || arch == 0)
26480     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
26481
26482   /* Tag_THUMB_ISA_use.  */
26483   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
26484       || arch == 0)
26485     {
26486       int thumb_isa_use;
26487
26488       if (!ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26489           && ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m_m_only))
26490         thumb_isa_use = 3;
26491       else if (ARM_CPU_HAS_FEATURE (flags, arm_arch_t2))
26492         thumb_isa_use = 2;
26493       else
26494         thumb_isa_use = 1;
26495       aeabi_set_attribute_int (Tag_THUMB_ISA_use, thumb_isa_use);
26496     }
26497
26498   /* Tag_VFP_arch.  */
26499   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
26500     aeabi_set_attribute_int (Tag_VFP_arch,
26501                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
26502                              ? 7 : 8);
26503   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
26504     aeabi_set_attribute_int (Tag_VFP_arch,
26505                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
26506                              ? 5 : 6);
26507   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
26508     {
26509       fp16_optional = 1;
26510       aeabi_set_attribute_int (Tag_VFP_arch, 3);
26511     }
26512   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
26513     {
26514       aeabi_set_attribute_int (Tag_VFP_arch, 4);
26515       fp16_optional = 1;
26516     }
26517   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
26518     aeabi_set_attribute_int (Tag_VFP_arch, 2);
26519   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
26520            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
26521     aeabi_set_attribute_int (Tag_VFP_arch, 1);
26522
26523   /* Tag_ABI_HardFP_use.  */
26524   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
26525       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
26526     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
26527
26528   /* Tag_WMMX_arch.  */
26529   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
26530     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
26531   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
26532     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
26533
26534   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
26535   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v8_1))
26536     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 4);
26537   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
26538     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
26539   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
26540     {
26541       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
26542         {
26543           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
26544         }
26545       else
26546         {
26547           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
26548           fp16_optional = 1;
26549         }
26550     }
26551
26552   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
26553   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
26554     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
26555
26556   /* Tag_DIV_use.
26557
26558      We set Tag_DIV_use to two when integer divide instructions have been used
26559      in ARM state, or when Thumb integer divide instructions have been used,
26560      but we have no architecture profile set, nor have we any ARM instructions.
26561
26562      For ARMv8-A and ARMv8-M we set the tag to 0 as integer divide is implied
26563      by the base architecture.
26564
26565      For new architectures we will have to check these tests.  */
26566   gas_assert (arch <= TAG_CPU_ARCH_V8
26567               || (arch >= TAG_CPU_ARCH_V8M_BASE
26568                   && arch <= TAG_CPU_ARCH_V8M_MAIN));
26569   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8)
26570       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8m))
26571     aeabi_set_attribute_int (Tag_DIV_use, 0);
26572   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
26573            || (profile == '\0'
26574                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
26575                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
26576     aeabi_set_attribute_int (Tag_DIV_use, 2);
26577
26578   /* Tag_MP_extension_use.  */
26579   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
26580     aeabi_set_attribute_int (Tag_MPextension_use, 1);
26581
26582   /* Tag Virtualization_use.  */
26583   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
26584     virt_sec |= 1;
26585   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
26586     virt_sec |= 2;
26587   if (virt_sec != 0)
26588     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
26589 }
26590
26591 /* Add the default contents for the .ARM.attributes section.  */
26592 void
26593 arm_md_end (void)
26594 {
26595   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
26596     return;
26597
26598   aeabi_set_public_attributes ();
26599 }
26600 #endif /* OBJ_ELF */
26601
26602
26603 /* Parse a .cpu directive.  */
26604
26605 static void
26606 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
26607 {
26608   const struct arm_cpu_option_table *opt;
26609   char *name;
26610   char saved_char;
26611
26612   name = input_line_pointer;
26613   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26614     input_line_pointer++;
26615   saved_char = *input_line_pointer;
26616   *input_line_pointer = 0;
26617
26618   /* Skip the first "all" entry.  */
26619   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
26620     if (streq (opt->name, name))
26621       {
26622         mcpu_cpu_opt = &opt->value;
26623         selected_cpu = opt->value;
26624         if (opt->canonical_name)
26625           strcpy (selected_cpu_name, opt->canonical_name);
26626         else
26627           {
26628             int i;
26629             for (i = 0; opt->name[i]; i++)
26630               selected_cpu_name[i] = TOUPPER (opt->name[i]);
26631
26632             selected_cpu_name[i] = 0;
26633           }
26634         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26635         *input_line_pointer = saved_char;
26636         demand_empty_rest_of_line ();
26637         return;
26638       }
26639   as_bad (_("unknown cpu `%s'"), name);
26640   *input_line_pointer = saved_char;
26641   ignore_rest_of_line ();
26642 }
26643
26644
26645 /* Parse a .arch directive.  */
26646
26647 static void
26648 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
26649 {
26650   const struct arm_arch_option_table *opt;
26651   char saved_char;
26652   char *name;
26653
26654   name = input_line_pointer;
26655   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26656     input_line_pointer++;
26657   saved_char = *input_line_pointer;
26658   *input_line_pointer = 0;
26659
26660   /* Skip the first "all" entry.  */
26661   for (opt = arm_archs + 1; opt->name != NULL; opt++)
26662     if (streq (opt->name, name))
26663       {
26664         mcpu_cpu_opt = &opt->value;
26665         selected_cpu = opt->value;
26666         strcpy (selected_cpu_name, opt->name);
26667         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26668         *input_line_pointer = saved_char;
26669         demand_empty_rest_of_line ();
26670         return;
26671       }
26672
26673   as_bad (_("unknown architecture `%s'\n"), name);
26674   *input_line_pointer = saved_char;
26675   ignore_rest_of_line ();
26676 }
26677
26678
26679 /* Parse a .object_arch directive.  */
26680
26681 static void
26682 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
26683 {
26684   const struct arm_arch_option_table *opt;
26685   char saved_char;
26686   char *name;
26687
26688   name = input_line_pointer;
26689   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26690     input_line_pointer++;
26691   saved_char = *input_line_pointer;
26692   *input_line_pointer = 0;
26693
26694   /* Skip the first "all" entry.  */
26695   for (opt = arm_archs + 1; opt->name != NULL; opt++)
26696     if (streq (opt->name, name))
26697       {
26698         object_arch = &opt->value;
26699         *input_line_pointer = saved_char;
26700         demand_empty_rest_of_line ();
26701         return;
26702       }
26703
26704   as_bad (_("unknown architecture `%s'\n"), name);
26705   *input_line_pointer = saved_char;
26706   ignore_rest_of_line ();
26707 }
26708
26709 /* Parse a .arch_extension directive.  */
26710
26711 static void
26712 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
26713 {
26714   const struct arm_option_extension_value_table *opt;
26715   const arm_feature_set arm_any = ARM_ANY;
26716   char saved_char;
26717   char *name;
26718   int adding_value = 1;
26719
26720   name = input_line_pointer;
26721   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26722     input_line_pointer++;
26723   saved_char = *input_line_pointer;
26724   *input_line_pointer = 0;
26725
26726   if (strlen (name) >= 2
26727       && strncmp (name, "no", 2) == 0)
26728     {
26729       adding_value = 0;
26730       name += 2;
26731     }
26732
26733   for (opt = arm_extensions; opt->name != NULL; opt++)
26734     if (streq (opt->name, name))
26735       {
26736         int i, nb_allowed_archs =
26737           sizeof (opt->allowed_archs) / sizeof (opt->allowed_archs[i]);
26738         for (i = 0; i < nb_allowed_archs; i++)
26739           {
26740             /* Empty entry.  */
26741             if (ARM_FEATURE_EQUAL (opt->allowed_archs[i], arm_any))
26742               continue;
26743             if (ARM_FSET_CPU_SUBSET (opt->allowed_archs[i], *mcpu_cpu_opt))
26744               break;
26745           }
26746
26747         if (i == nb_allowed_archs)
26748           {
26749             as_bad (_("architectural extension `%s' is not allowed for the "
26750                       "current base architecture"), name);
26751             break;
26752           }
26753
26754         if (adding_value)
26755           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu,
26756                                   opt->merge_value);
26757         else
26758           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->clear_value);
26759
26760         mcpu_cpu_opt = &selected_cpu;
26761         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26762         *input_line_pointer = saved_char;
26763         demand_empty_rest_of_line ();
26764         return;
26765       }
26766
26767   if (opt->name == NULL)
26768     as_bad (_("unknown architecture extension `%s'\n"), name);
26769
26770   *input_line_pointer = saved_char;
26771   ignore_rest_of_line ();
26772 }
26773
26774 /* Parse a .fpu directive.  */
26775
26776 static void
26777 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
26778 {
26779   const struct arm_option_fpu_value_table *opt;
26780   char saved_char;
26781   char *name;
26782
26783   name = input_line_pointer;
26784   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
26785     input_line_pointer++;
26786   saved_char = *input_line_pointer;
26787   *input_line_pointer = 0;
26788
26789   for (opt = arm_fpus; opt->name != NULL; opt++)
26790     if (streq (opt->name, name))
26791       {
26792         mfpu_opt = &opt->value;
26793         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
26794         *input_line_pointer = saved_char;
26795         demand_empty_rest_of_line ();
26796         return;
26797       }
26798
26799   as_bad (_("unknown floating point format `%s'\n"), name);
26800   *input_line_pointer = saved_char;
26801   ignore_rest_of_line ();
26802 }
26803
26804 /* Copy symbol information.  */
26805
26806 void
26807 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
26808 {
26809   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
26810 }
26811
26812 #ifdef OBJ_ELF
26813 /* Given a symbolic attribute NAME, return the proper integer value.
26814    Returns -1 if the attribute is not known.  */
26815
26816 int
26817 arm_convert_symbolic_attribute (const char *name)
26818 {
26819   static const struct
26820   {
26821     const char * name;
26822     const int    tag;
26823   }
26824   attribute_table[] =
26825     {
26826       /* When you modify this table you should
26827          also modify the list in doc/c-arm.texi.  */
26828 #define T(tag) {#tag, tag}
26829       T (Tag_CPU_raw_name),
26830       T (Tag_CPU_name),
26831       T (Tag_CPU_arch),
26832       T (Tag_CPU_arch_profile),
26833       T (Tag_ARM_ISA_use),
26834       T (Tag_THUMB_ISA_use),
26835       T (Tag_FP_arch),
26836       T (Tag_VFP_arch),
26837       T (Tag_WMMX_arch),
26838       T (Tag_Advanced_SIMD_arch),
26839       T (Tag_PCS_config),
26840       T (Tag_ABI_PCS_R9_use),
26841       T (Tag_ABI_PCS_RW_data),
26842       T (Tag_ABI_PCS_RO_data),
26843       T (Tag_ABI_PCS_GOT_use),
26844       T (Tag_ABI_PCS_wchar_t),
26845       T (Tag_ABI_FP_rounding),
26846       T (Tag_ABI_FP_denormal),
26847       T (Tag_ABI_FP_exceptions),
26848       T (Tag_ABI_FP_user_exceptions),
26849       T (Tag_ABI_FP_number_model),
26850       T (Tag_ABI_align_needed),
26851       T (Tag_ABI_align8_needed),
26852       T (Tag_ABI_align_preserved),
26853       T (Tag_ABI_align8_preserved),
26854       T (Tag_ABI_enum_size),
26855       T (Tag_ABI_HardFP_use),
26856       T (Tag_ABI_VFP_args),
26857       T (Tag_ABI_WMMX_args),
26858       T (Tag_ABI_optimization_goals),
26859       T (Tag_ABI_FP_optimization_goals),
26860       T (Tag_compatibility),
26861       T (Tag_CPU_unaligned_access),
26862       T (Tag_FP_HP_extension),
26863       T (Tag_VFP_HP_extension),
26864       T (Tag_ABI_FP_16bit_format),
26865       T (Tag_MPextension_use),
26866       T (Tag_DIV_use),
26867       T (Tag_nodefaults),
26868       T (Tag_also_compatible_with),
26869       T (Tag_conformance),
26870       T (Tag_T2EE_use),
26871       T (Tag_Virtualization_use),
26872       T (Tag_DSP_extension),
26873       /* We deliberately do not include Tag_MPextension_use_legacy.  */
26874 #undef T
26875     };
26876   unsigned int i;
26877
26878   if (name == NULL)
26879     return -1;
26880
26881   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
26882     if (streq (name, attribute_table[i].name))
26883       return attribute_table[i].tag;
26884
26885   return -1;
26886 }
26887
26888
26889 /* Apply sym value for relocations only in the case that they are for
26890    local symbols in the same segment as the fixup and you have the
26891    respective architectural feature for blx and simple switches.  */
26892 int
26893 arm_apply_sym_value (struct fix * fixP, segT this_seg)
26894 {
26895   if (fixP->fx_addsy
26896       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26897       /* PR 17444: If the local symbol is in a different section then a reloc
26898          will always be generated for it, so applying the symbol value now
26899          will result in a double offset being stored in the relocation.  */
26900       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
26901       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
26902     {
26903       switch (fixP->fx_r_type)
26904         {
26905         case BFD_RELOC_ARM_PCREL_BLX:
26906         case BFD_RELOC_THUMB_PCREL_BRANCH23:
26907           if (ARM_IS_FUNC (fixP->fx_addsy))
26908             return 1;
26909           break;
26910
26911         case BFD_RELOC_ARM_PCREL_CALL:
26912         case BFD_RELOC_THUMB_PCREL_BLX:
26913           if (THUMB_IS_FUNC (fixP->fx_addsy))
26914             return 1;
26915           break;
26916
26917         default:
26918           break;
26919         }
26920
26921     }
26922   return 0;
26923 }
26924 #endif /* OBJ_ELF */