f9c76ef7c35960cc06d89d842d79024a6a0d7652
[external/binutils.git] / gas / config / tc-arm.c
1 /* tc-arm.c -- Assemble for the ARM
2    Copyright (C) 1994-2015 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 = 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 = FPU_ARCH_VFP_V3;
161 static const arm_feature_set fpu_arch_neon_v1 = 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 static const arm_feature_set fpu_arch_maverick = FPU_ARCH_MAVERICK;
165 static const arm_feature_set fpu_endian_pure = FPU_ARCH_ENDIAN_PURE;
166
167 #ifdef CPU_DEFAULT
168 static const arm_feature_set cpu_default = CPU_DEFAULT;
169 #endif
170
171 static const arm_feature_set arm_ext_v1 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
172 static const arm_feature_set arm_ext_v2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V1);
173 static const arm_feature_set arm_ext_v2s = ARM_FEATURE_CORE_LOW (ARM_EXT_V2S);
174 static const arm_feature_set arm_ext_v3 = ARM_FEATURE_CORE_LOW (ARM_EXT_V3);
175 static const arm_feature_set arm_ext_v3m = ARM_FEATURE_CORE_LOW (ARM_EXT_V3M);
176 static const arm_feature_set arm_ext_v4 = ARM_FEATURE_CORE_LOW (ARM_EXT_V4);
177 static const arm_feature_set arm_ext_v4t = ARM_FEATURE_CORE_LOW (ARM_EXT_V4T);
178 static const arm_feature_set arm_ext_v5 = ARM_FEATURE_CORE_LOW (ARM_EXT_V5);
179 static const arm_feature_set arm_ext_v4t_5 =
180   ARM_FEATURE_CORE_LOW (ARM_EXT_V4T | ARM_EXT_V5);
181 static const arm_feature_set arm_ext_v5t = ARM_FEATURE_CORE_LOW (ARM_EXT_V5T);
182 static const arm_feature_set arm_ext_v5e = ARM_FEATURE_CORE_LOW (ARM_EXT_V5E);
183 static const arm_feature_set arm_ext_v5exp = ARM_FEATURE_CORE_LOW (ARM_EXT_V5ExP);
184 static const arm_feature_set arm_ext_v5j = ARM_FEATURE_CORE_LOW (ARM_EXT_V5J);
185 static const arm_feature_set arm_ext_v6 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6);
186 static const arm_feature_set arm_ext_v6k = ARM_FEATURE_CORE_LOW (ARM_EXT_V6K);
187 static const arm_feature_set arm_ext_v6t2 = ARM_FEATURE_CORE_LOW (ARM_EXT_V6T2);
188 static const arm_feature_set arm_ext_v6m = ARM_FEATURE_CORE_LOW (ARM_EXT_V6M);
189 static const arm_feature_set arm_ext_v6_notm =
190   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_NOTM);
191 static const arm_feature_set arm_ext_v6_dsp =
192   ARM_FEATURE_CORE_LOW (ARM_EXT_V6_DSP);
193 static const arm_feature_set arm_ext_barrier =
194   ARM_FEATURE_CORE_LOW (ARM_EXT_BARRIER);
195 static const arm_feature_set arm_ext_msr =
196   ARM_FEATURE_CORE_LOW (ARM_EXT_THUMB_MSR);
197 static const arm_feature_set arm_ext_div = ARM_FEATURE_CORE_LOW (ARM_EXT_DIV);
198 static const arm_feature_set arm_ext_v7 = ARM_FEATURE_CORE_LOW (ARM_EXT_V7);
199 static const arm_feature_set arm_ext_v7a = ARM_FEATURE_CORE_LOW (ARM_EXT_V7A);
200 static const arm_feature_set arm_ext_v7r = ARM_FEATURE_CORE_LOW (ARM_EXT_V7R);
201 static const arm_feature_set arm_ext_v7m = ARM_FEATURE_CORE_LOW (ARM_EXT_V7M);
202 static const arm_feature_set arm_ext_v8 = ARM_FEATURE_CORE_LOW (ARM_EXT_V8);
203 static const arm_feature_set arm_ext_m =
204   ARM_FEATURE_CORE_LOW (ARM_EXT_V6M | ARM_EXT_OS | ARM_EXT_V7M);
205 static const arm_feature_set arm_ext_mp = ARM_FEATURE_CORE_LOW (ARM_EXT_MP);
206 static const arm_feature_set arm_ext_sec = ARM_FEATURE_CORE_LOW (ARM_EXT_SEC);
207 static const arm_feature_set arm_ext_os = ARM_FEATURE_CORE_LOW (ARM_EXT_OS);
208 static const arm_feature_set arm_ext_adiv = ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV);
209 static const arm_feature_set arm_ext_virt = ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT);
210 static const arm_feature_set arm_ext_pan = ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN);
211
212 static const arm_feature_set arm_arch_any = ARM_ANY;
213 static const arm_feature_set arm_arch_full = ARM_FEATURE (-1, -1, -1);
214 static const arm_feature_set arm_arch_t2 = ARM_ARCH_THUMB2;
215 static const arm_feature_set arm_arch_none = ARM_ARCH_NONE;
216 static const arm_feature_set arm_arch_v6m_only = ARM_ARCH_V6M_ONLY;
217
218 static const arm_feature_set arm_cext_iwmmxt2 =
219   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2);
220 static const arm_feature_set arm_cext_iwmmxt =
221   ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT);
222 static const arm_feature_set arm_cext_xscale =
223   ARM_FEATURE_COPROC (ARM_CEXT_XSCALE);
224 static const arm_feature_set arm_cext_maverick =
225   ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK);
226 static const arm_feature_set fpu_fpa_ext_v1 =
227   ARM_FEATURE_COPROC (FPU_FPA_EXT_V1);
228 static const arm_feature_set fpu_fpa_ext_v2 =
229   ARM_FEATURE_COPROC (FPU_FPA_EXT_V2);
230 static const arm_feature_set fpu_vfp_ext_v1xd =
231   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1xD);
232 static const arm_feature_set fpu_vfp_ext_v1 =
233   ARM_FEATURE_COPROC (FPU_VFP_EXT_V1);
234 static const arm_feature_set fpu_vfp_ext_v2 =
235   ARM_FEATURE_COPROC (FPU_VFP_EXT_V2);
236 static const arm_feature_set fpu_vfp_ext_v3xd =
237   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3xD);
238 static const arm_feature_set fpu_vfp_ext_v3 =
239   ARM_FEATURE_COPROC (FPU_VFP_EXT_V3);
240 static const arm_feature_set fpu_vfp_ext_d32 =
241   ARM_FEATURE_COPROC (FPU_VFP_EXT_D32);
242 static const arm_feature_set fpu_neon_ext_v1 =
243   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1);
244 static const arm_feature_set fpu_vfp_v3_or_neon_ext =
245   ARM_FEATURE_COPROC (FPU_NEON_EXT_V1 | FPU_VFP_EXT_V3);
246 static const arm_feature_set fpu_vfp_fp16 =
247   ARM_FEATURE_COPROC (FPU_VFP_EXT_FP16);
248 static const arm_feature_set fpu_neon_ext_fma =
249   ARM_FEATURE_COPROC (FPU_NEON_EXT_FMA);
250 static const arm_feature_set fpu_vfp_ext_fma =
251   ARM_FEATURE_COPROC (FPU_VFP_EXT_FMA);
252 static const arm_feature_set fpu_vfp_ext_armv8 =
253   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8);
254 static const arm_feature_set fpu_vfp_ext_armv8xd =
255   ARM_FEATURE_COPROC (FPU_VFP_EXT_ARMV8xD);
256 static const arm_feature_set fpu_neon_ext_armv8 =
257   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8);
258 static const arm_feature_set fpu_crypto_ext_armv8 =
259   ARM_FEATURE_COPROC (FPU_CRYPTO_EXT_ARMV8);
260 static const arm_feature_set crc_ext_armv8 =
261   ARM_FEATURE_COPROC (CRC_EXT_ARMV8);
262 static const arm_feature_set fpu_neon_ext_v8_1 =
263   ARM_FEATURE_COPROC (FPU_NEON_EXT_ARMV8 | FPU_NEON_EXT_RDMA);
264
265 static int mfloat_abi_opt = -1;
266 /* Record user cpu selection for object attributes.  */
267 static arm_feature_set selected_cpu = ARM_ARCH_NONE;
268 /* Must be long enough to hold any of the names in arm_cpus.  */
269 static char selected_cpu_name[20];
270
271 extern FLONUM_TYPE generic_floating_point_number;
272
273 /* Return if no cpu was selected on command-line.  */
274 static bfd_boolean
275 no_cpu_selected (void)
276 {
277   return ARM_FEATURE_EQUAL (selected_cpu, arm_arch_none);
278 }
279
280 #ifdef OBJ_ELF
281 # ifdef EABI_DEFAULT
282 static int meabi_flags = EABI_DEFAULT;
283 # else
284 static int meabi_flags = EF_ARM_EABI_UNKNOWN;
285 # endif
286
287 static int attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
288
289 bfd_boolean
290 arm_is_eabi (void)
291 {
292   return (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4);
293 }
294 #endif
295
296 #ifdef OBJ_ELF
297 /* Pre-defined "_GLOBAL_OFFSET_TABLE_"  */
298 symbolS * GOT_symbol;
299 #endif
300
301 /* 0: assemble for ARM,
302    1: assemble for Thumb,
303    2: assemble for Thumb even though target CPU does not support thumb
304       instructions.  */
305 static int thumb_mode = 0;
306 /* A value distinct from the possible values for thumb_mode that we
307    can use to record whether thumb_mode has been copied into the
308    tc_frag_data field of a frag.  */
309 #define MODE_RECORDED (1 << 4)
310
311 /* Specifies the intrinsic IT insn behavior mode.  */
312 enum implicit_it_mode
313 {
314   IMPLICIT_IT_MODE_NEVER  = 0x00,
315   IMPLICIT_IT_MODE_ARM    = 0x01,
316   IMPLICIT_IT_MODE_THUMB  = 0x02,
317   IMPLICIT_IT_MODE_ALWAYS = (IMPLICIT_IT_MODE_ARM | IMPLICIT_IT_MODE_THUMB)
318 };
319 static int implicit_it_mode = IMPLICIT_IT_MODE_ARM;
320
321 /* If unified_syntax is true, we are processing the new unified
322    ARM/Thumb syntax.  Important differences from the old ARM mode:
323
324      - Immediate operands do not require a # prefix.
325      - Conditional affixes always appear at the end of the
326        instruction.  (For backward compatibility, those instructions
327        that formerly had them in the middle, continue to accept them
328        there.)
329      - The IT instruction may appear, and if it does is validated
330        against subsequent conditional affixes.  It does not generate
331        machine code.
332
333    Important differences from the old Thumb mode:
334
335      - Immediate operands do not require a # prefix.
336      - Most of the V6T2 instructions are only available in unified mode.
337      - The .N and .W suffixes are recognized and honored (it is an error
338        if they cannot be honored).
339      - All instructions set the flags if and only if they have an 's' affix.
340      - Conditional affixes may be used.  They are validated against
341        preceding IT instructions.  Unlike ARM mode, you cannot use a
342        conditional affix except in the scope of an IT instruction.  */
343
344 static bfd_boolean unified_syntax = FALSE;
345
346 /* An immediate operand can start with #, and ld*, st*, pld operands
347    can contain [ and ].  We need to tell APP not to elide whitespace
348    before a [, which can appear as the first operand for pld.
349    Likewise, a { can appear as the first operand for push, pop, vld*, etc.  */
350 const char arm_symbol_chars[] = "#[]{}";
351
352 enum neon_el_type
353 {
354   NT_invtype,
355   NT_untyped,
356   NT_integer,
357   NT_float,
358   NT_poly,
359   NT_signed,
360   NT_unsigned
361 };
362
363 struct neon_type_el
364 {
365   enum neon_el_type type;
366   unsigned size;
367 };
368
369 #define NEON_MAX_TYPE_ELS 4
370
371 struct neon_type
372 {
373   struct neon_type_el el[NEON_MAX_TYPE_ELS];
374   unsigned elems;
375 };
376
377 enum it_instruction_type
378 {
379    OUTSIDE_IT_INSN,
380    INSIDE_IT_INSN,
381    INSIDE_IT_LAST_INSN,
382    IF_INSIDE_IT_LAST_INSN, /* Either outside or inside;
383                               if inside, should be the last one.  */
384    NEUTRAL_IT_INSN,        /* This could be either inside or outside,
385                               i.e. BKPT and NOP.  */
386    IT_INSN                 /* The IT insn has been parsed.  */
387 };
388
389 /* The maximum number of operands we need.  */
390 #define ARM_IT_MAX_OPERANDS 6
391
392 struct arm_it
393 {
394   const char *  error;
395   unsigned long instruction;
396   int           size;
397   int           size_req;
398   int           cond;
399   /* "uncond_value" is set to the value in place of the conditional field in
400      unconditional versions of the instruction, or -1 if nothing is
401      appropriate.  */
402   int           uncond_value;
403   struct neon_type vectype;
404   /* This does not indicate an actual NEON instruction, only that
405      the mnemonic accepts neon-style type suffixes.  */
406   int           is_neon;
407   /* Set to the opcode if the instruction needs relaxation.
408      Zero if the instruction is not relaxed.  */
409   unsigned long relax;
410   struct
411   {
412     bfd_reloc_code_real_type type;
413     expressionS              exp;
414     int                      pc_rel;
415   } reloc;
416
417   enum it_instruction_type it_insn_type;
418
419   struct
420   {
421     unsigned reg;
422     signed int imm;
423     struct neon_type_el vectype;
424     unsigned present    : 1;  /* Operand present.  */
425     unsigned isreg      : 1;  /* Operand was a register.  */
426     unsigned immisreg   : 1;  /* .imm field is a second register.  */
427     unsigned isscalar   : 1;  /* Operand is a (Neon) scalar.  */
428     unsigned immisalign : 1;  /* Immediate is an alignment specifier.  */
429     unsigned immisfloat : 1;  /* Immediate was parsed as a float.  */
430     /* Note: we abuse "regisimm" to mean "is Neon register" in VMOV
431        instructions. This allows us to disambiguate ARM <-> vector insns.  */
432     unsigned regisimm   : 1;  /* 64-bit immediate, reg forms high 32 bits.  */
433     unsigned isvec      : 1;  /* Is a single, double or quad VFP/Neon reg.  */
434     unsigned isquad     : 1;  /* Operand is Neon quad-precision register.  */
435     unsigned issingle   : 1;  /* Operand is VFP single-precision register.  */
436     unsigned hasreloc   : 1;  /* Operand has relocation suffix.  */
437     unsigned writeback  : 1;  /* Operand has trailing !  */
438     unsigned preind     : 1;  /* Preindexed address.  */
439     unsigned postind    : 1;  /* Postindexed address.  */
440     unsigned negative   : 1;  /* Index register was negated.  */
441     unsigned shifted    : 1;  /* Shift applied to operation.  */
442     unsigned shift_kind : 3;  /* Shift operation (enum shift_kind).  */
443   } operands[ARM_IT_MAX_OPERANDS];
444 };
445
446 static struct arm_it inst;
447
448 #define NUM_FLOAT_VALS 8
449
450 const char * fp_const[] =
451 {
452   "0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0", 0
453 };
454
455 /* Number of littlenums required to hold an extended precision number.  */
456 #define MAX_LITTLENUMS 6
457
458 LITTLENUM_TYPE fp_values[NUM_FLOAT_VALS][MAX_LITTLENUMS];
459
460 #define FAIL    (-1)
461 #define SUCCESS (0)
462
463 #define SUFF_S 1
464 #define SUFF_D 2
465 #define SUFF_E 3
466 #define SUFF_P 4
467
468 #define CP_T_X   0x00008000
469 #define CP_T_Y   0x00400000
470
471 #define CONDS_BIT        0x00100000
472 #define LOAD_BIT         0x00100000
473
474 #define DOUBLE_LOAD_FLAG 0x00000001
475
476 struct asm_cond
477 {
478   const char *   template_name;
479   unsigned long  value;
480 };
481
482 #define COND_ALWAYS 0xE
483
484 struct asm_psr
485 {
486   const char *   template_name;
487   unsigned long  field;
488 };
489
490 struct asm_barrier_opt
491 {
492   const char *    template_name;
493   unsigned long   value;
494   const arm_feature_set arch;
495 };
496
497 /* The bit that distinguishes CPSR and SPSR.  */
498 #define SPSR_BIT   (1 << 22)
499
500 /* The individual PSR flag bits.  */
501 #define PSR_c   (1 << 16)
502 #define PSR_x   (1 << 17)
503 #define PSR_s   (1 << 18)
504 #define PSR_f   (1 << 19)
505
506 struct reloc_entry
507 {
508   char *                    name;
509   bfd_reloc_code_real_type  reloc;
510 };
511
512 enum vfp_reg_pos
513 {
514   VFP_REG_Sd, VFP_REG_Sm, VFP_REG_Sn,
515   VFP_REG_Dd, VFP_REG_Dm, VFP_REG_Dn
516 };
517
518 enum vfp_ldstm_type
519 {
520   VFP_LDSTMIA, VFP_LDSTMDB, VFP_LDSTMIAX, VFP_LDSTMDBX
521 };
522
523 /* Bits for DEFINED field in neon_typed_alias.  */
524 #define NTA_HASTYPE  1
525 #define NTA_HASINDEX 2
526
527 struct neon_typed_alias
528 {
529   unsigned char        defined;
530   unsigned char        index;
531   struct neon_type_el  eltype;
532 };
533
534 /* ARM register categories.  This includes coprocessor numbers and various
535    architecture extensions' registers.  */
536 enum arm_reg_type
537 {
538   REG_TYPE_RN,
539   REG_TYPE_CP,
540   REG_TYPE_CN,
541   REG_TYPE_FN,
542   REG_TYPE_VFS,
543   REG_TYPE_VFD,
544   REG_TYPE_NQ,
545   REG_TYPE_VFSD,
546   REG_TYPE_NDQ,
547   REG_TYPE_NSDQ,
548   REG_TYPE_VFC,
549   REG_TYPE_MVF,
550   REG_TYPE_MVD,
551   REG_TYPE_MVFX,
552   REG_TYPE_MVDX,
553   REG_TYPE_MVAX,
554   REG_TYPE_DSPSC,
555   REG_TYPE_MMXWR,
556   REG_TYPE_MMXWC,
557   REG_TYPE_MMXWCG,
558   REG_TYPE_XSCALE,
559   REG_TYPE_RNB
560 };
561
562 /* Structure for a hash table entry for a register.
563    If TYPE is REG_TYPE_VFD or REG_TYPE_NQ, the NEON field can point to extra
564    information which states whether a vector type or index is specified (for a
565    register alias created with .dn or .qn). Otherwise NEON should be NULL.  */
566 struct reg_entry
567 {
568   const char *               name;
569   unsigned int               number;
570   unsigned char              type;
571   unsigned char              builtin;
572   struct neon_typed_alias *  neon;
573 };
574
575 /* Diagnostics used when we don't get a register of the expected type.  */
576 const char * const reg_expected_msgs[] =
577 {
578   N_("ARM register expected"),
579   N_("bad or missing co-processor number"),
580   N_("co-processor register expected"),
581   N_("FPA register expected"),
582   N_("VFP single precision register expected"),
583   N_("VFP/Neon double precision register expected"),
584   N_("Neon quad precision register expected"),
585   N_("VFP single or double precision register expected"),
586   N_("Neon double or quad precision register expected"),
587   N_("VFP single, double or Neon quad precision register expected"),
588   N_("VFP system register expected"),
589   N_("Maverick MVF register expected"),
590   N_("Maverick MVD register expected"),
591   N_("Maverick MVFX register expected"),
592   N_("Maverick MVDX register expected"),
593   N_("Maverick MVAX register expected"),
594   N_("Maverick DSPSC register expected"),
595   N_("iWMMXt data register expected"),
596   N_("iWMMXt control register expected"),
597   N_("iWMMXt scalar register expected"),
598   N_("XScale accumulator register expected"),
599 };
600
601 /* Some well known registers that we refer to directly elsewhere.  */
602 #define REG_R12 12
603 #define REG_SP  13
604 #define REG_LR  14
605 #define REG_PC  15
606
607 /* ARM instructions take 4bytes in the object file, Thumb instructions
608    take 2:  */
609 #define INSN_SIZE       4
610
611 struct asm_opcode
612 {
613   /* Basic string to match.  */
614   const char * template_name;
615
616   /* Parameters to instruction.  */
617   unsigned int operands[8];
618
619   /* Conditional tag - see opcode_lookup.  */
620   unsigned int tag : 4;
621
622   /* Basic instruction code.  */
623   unsigned int avalue : 28;
624
625   /* Thumb-format instruction code.  */
626   unsigned int tvalue;
627
628   /* Which architecture variant provides this instruction.  */
629   const arm_feature_set * avariant;
630   const arm_feature_set * tvariant;
631
632   /* Function to call to encode instruction in ARM format.  */
633   void (* aencode) (void);
634
635   /* Function to call to encode instruction in Thumb format.  */
636   void (* tencode) (void);
637 };
638
639 /* Defines for various bits that we will want to toggle.  */
640 #define INST_IMMEDIATE  0x02000000
641 #define OFFSET_REG      0x02000000
642 #define HWOFFSET_IMM    0x00400000
643 #define SHIFT_BY_REG    0x00000010
644 #define PRE_INDEX       0x01000000
645 #define INDEX_UP        0x00800000
646 #define WRITE_BACK      0x00200000
647 #define LDM_TYPE_2_OR_3 0x00400000
648 #define CPSI_MMOD       0x00020000
649
650 #define LITERAL_MASK    0xf000f000
651 #define OPCODE_MASK     0xfe1fffff
652 #define V4_STR_BIT      0x00000020
653 #define VLDR_VMOV_SAME  0x0040f000
654
655 #define T2_SUBS_PC_LR   0xf3de8f00
656
657 #define DATA_OP_SHIFT   21
658
659 #define T2_OPCODE_MASK  0xfe1fffff
660 #define T2_DATA_OP_SHIFT 21
661
662 #define A_COND_MASK         0xf0000000
663 #define A_PUSH_POP_OP_MASK  0x0fff0000
664
665 /* Opcodes for pushing/poping registers to/from the stack.  */
666 #define A1_OPCODE_PUSH    0x092d0000
667 #define A2_OPCODE_PUSH    0x052d0004
668 #define A2_OPCODE_POP     0x049d0004
669
670 /* Codes to distinguish the arithmetic instructions.  */
671 #define OPCODE_AND      0
672 #define OPCODE_EOR      1
673 #define OPCODE_SUB      2
674 #define OPCODE_RSB      3
675 #define OPCODE_ADD      4
676 #define OPCODE_ADC      5
677 #define OPCODE_SBC      6
678 #define OPCODE_RSC      7
679 #define OPCODE_TST      8
680 #define OPCODE_TEQ      9
681 #define OPCODE_CMP      10
682 #define OPCODE_CMN      11
683 #define OPCODE_ORR      12
684 #define OPCODE_MOV      13
685 #define OPCODE_BIC      14
686 #define OPCODE_MVN      15
687
688 #define T2_OPCODE_AND   0
689 #define T2_OPCODE_BIC   1
690 #define T2_OPCODE_ORR   2
691 #define T2_OPCODE_ORN   3
692 #define T2_OPCODE_EOR   4
693 #define T2_OPCODE_ADD   8
694 #define T2_OPCODE_ADC   10
695 #define T2_OPCODE_SBC   11
696 #define T2_OPCODE_SUB   13
697 #define T2_OPCODE_RSB   14
698
699 #define T_OPCODE_MUL 0x4340
700 #define T_OPCODE_TST 0x4200
701 #define T_OPCODE_CMN 0x42c0
702 #define T_OPCODE_NEG 0x4240
703 #define T_OPCODE_MVN 0x43c0
704
705 #define T_OPCODE_ADD_R3 0x1800
706 #define T_OPCODE_SUB_R3 0x1a00
707 #define T_OPCODE_ADD_HI 0x4400
708 #define T_OPCODE_ADD_ST 0xb000
709 #define T_OPCODE_SUB_ST 0xb080
710 #define T_OPCODE_ADD_SP 0xa800
711 #define T_OPCODE_ADD_PC 0xa000
712 #define T_OPCODE_ADD_I8 0x3000
713 #define T_OPCODE_SUB_I8 0x3800
714 #define T_OPCODE_ADD_I3 0x1c00
715 #define T_OPCODE_SUB_I3 0x1e00
716
717 #define T_OPCODE_ASR_R  0x4100
718 #define T_OPCODE_LSL_R  0x4080
719 #define T_OPCODE_LSR_R  0x40c0
720 #define T_OPCODE_ROR_R  0x41c0
721 #define T_OPCODE_ASR_I  0x1000
722 #define T_OPCODE_LSL_I  0x0000
723 #define T_OPCODE_LSR_I  0x0800
724
725 #define T_OPCODE_MOV_I8 0x2000
726 #define T_OPCODE_CMP_I8 0x2800
727 #define T_OPCODE_CMP_LR 0x4280
728 #define T_OPCODE_MOV_HR 0x4600
729 #define T_OPCODE_CMP_HR 0x4500
730
731 #define T_OPCODE_LDR_PC 0x4800
732 #define T_OPCODE_LDR_SP 0x9800
733 #define T_OPCODE_STR_SP 0x9000
734 #define T_OPCODE_LDR_IW 0x6800
735 #define T_OPCODE_STR_IW 0x6000
736 #define T_OPCODE_LDR_IH 0x8800
737 #define T_OPCODE_STR_IH 0x8000
738 #define T_OPCODE_LDR_IB 0x7800
739 #define T_OPCODE_STR_IB 0x7000
740 #define T_OPCODE_LDR_RW 0x5800
741 #define T_OPCODE_STR_RW 0x5000
742 #define T_OPCODE_LDR_RH 0x5a00
743 #define T_OPCODE_STR_RH 0x5200
744 #define T_OPCODE_LDR_RB 0x5c00
745 #define T_OPCODE_STR_RB 0x5400
746
747 #define T_OPCODE_PUSH   0xb400
748 #define T_OPCODE_POP    0xbc00
749
750 #define T_OPCODE_BRANCH 0xe000
751
752 #define THUMB_SIZE      2       /* Size of thumb instruction.  */
753 #define THUMB_PP_PC_LR 0x0100
754 #define THUMB_LOAD_BIT 0x0800
755 #define THUMB2_LOAD_BIT 0x00100000
756
757 #define BAD_ARGS        _("bad arguments to instruction")
758 #define BAD_SP          _("r13 not allowed here")
759 #define BAD_PC          _("r15 not allowed here")
760 #define BAD_COND        _("instruction cannot be conditional")
761 #define BAD_OVERLAP     _("registers may not be the same")
762 #define BAD_HIREG       _("lo register required")
763 #define BAD_THUMB32     _("instruction not supported in Thumb16 mode")
764 #define BAD_ADDR_MODE   _("instruction does not accept this addressing mode");
765 #define BAD_BRANCH      _("branch must be last instruction in IT block")
766 #define BAD_NOT_IT      _("instruction not allowed in IT block")
767 #define BAD_FPU         _("selected FPU does not support instruction")
768 #define BAD_OUT_IT      _("thumb conditional instruction should be in IT block")
769 #define BAD_IT_COND     _("incorrect condition in IT block")
770 #define BAD_IT_IT       _("IT falling in the range of a previous IT block")
771 #define MISSING_FNSTART _("missing .fnstart before unwinding directive")
772 #define BAD_PC_ADDRESSING \
773         _("cannot use register index with PC-relative addressing")
774 #define BAD_PC_WRITEBACK \
775         _("cannot use writeback with PC-relative addressing")
776 #define BAD_RANGE     _("branch out of range")
777 #define UNPRED_REG(R)   _("using " R " results in unpredictable behaviour")
778
779 static struct hash_control * arm_ops_hsh;
780 static struct hash_control * arm_cond_hsh;
781 static struct hash_control * arm_shift_hsh;
782 static struct hash_control * arm_psr_hsh;
783 static struct hash_control * arm_v7m_psr_hsh;
784 static struct hash_control * arm_reg_hsh;
785 static struct hash_control * arm_reloc_hsh;
786 static struct hash_control * arm_barrier_opt_hsh;
787
788 /* Stuff needed to resolve the label ambiguity
789    As:
790      ...
791      label:   <insn>
792    may differ from:
793      ...
794      label:
795               <insn>  */
796
797 symbolS *  last_label_seen;
798 static int label_is_thumb_function_name = FALSE;
799
800 /* Literal pool structure.  Held on a per-section
801    and per-sub-section basis.  */
802
803 #define MAX_LITERAL_POOL_SIZE 1024
804 typedef struct literal_pool
805 {
806   expressionS            literals [MAX_LITERAL_POOL_SIZE];
807   unsigned int           next_free_entry;
808   unsigned int           id;
809   symbolS *              symbol;
810   segT                   section;
811   subsegT                sub_section;
812 #ifdef OBJ_ELF
813   struct dwarf2_line_info locs [MAX_LITERAL_POOL_SIZE];
814 #endif
815   struct literal_pool *  next;
816   unsigned int           alignment;
817 } literal_pool;
818
819 /* Pointer to a linked list of literal pools.  */
820 literal_pool * list_of_pools = NULL;
821
822 typedef enum asmfunc_states
823 {
824   OUTSIDE_ASMFUNC,
825   WAITING_ASMFUNC_NAME,
826   WAITING_ENDASMFUNC
827 } asmfunc_states;
828
829 static asmfunc_states asmfunc_state = OUTSIDE_ASMFUNC;
830
831 #ifdef OBJ_ELF
832 #  define now_it seg_info (now_seg)->tc_segment_info_data.current_it
833 #else
834 static struct current_it now_it;
835 #endif
836
837 static inline int
838 now_it_compatible (int cond)
839 {
840   return (cond & ~1) == (now_it.cc & ~1);
841 }
842
843 static inline int
844 conditional_insn (void)
845 {
846   return inst.cond != COND_ALWAYS;
847 }
848
849 static int in_it_block (void);
850
851 static int handle_it_state (void);
852
853 static void force_automatic_it_block_close (void);
854
855 static void it_fsm_post_encode (void);
856
857 #define set_it_insn_type(type)                  \
858   do                                            \
859     {                                           \
860       inst.it_insn_type = type;                 \
861       if (handle_it_state () == FAIL)           \
862         return;                                 \
863     }                                           \
864   while (0)
865
866 #define set_it_insn_type_nonvoid(type, failret) \
867   do                                            \
868     {                                           \
869       inst.it_insn_type = type;                 \
870       if (handle_it_state () == FAIL)           \
871         return failret;                         \
872     }                                           \
873   while(0)
874
875 #define set_it_insn_type_last()                         \
876   do                                                    \
877     {                                                   \
878       if (inst.cond == COND_ALWAYS)                     \
879         set_it_insn_type (IF_INSIDE_IT_LAST_INSN);      \
880       else                                              \
881         set_it_insn_type (INSIDE_IT_LAST_INSN);         \
882     }                                                   \
883   while (0)
884
885 /* Pure syntax.  */
886
887 /* This array holds the chars that always start a comment.  If the
888    pre-processor is disabled, these aren't very useful.  */
889 char arm_comment_chars[] = "@";
890
891 /* This array holds the chars that only start a comment at the beginning of
892    a line.  If the line seems to have the form '# 123 filename'
893    .line and .file directives will appear in the pre-processed output.  */
894 /* Note that input_file.c hand checks for '#' at the beginning of the
895    first line of the input file.  This is because the compiler outputs
896    #NO_APP at the beginning of its output.  */
897 /* Also note that comments like this one will always work.  */
898 const char line_comment_chars[] = "#";
899
900 char arm_line_separator_chars[] = ";";
901
902 /* Chars that can be used to separate mant
903    from exp in floating point numbers.  */
904 const char EXP_CHARS[] = "eE";
905
906 /* Chars that mean this number is a floating point constant.  */
907 /* As in 0f12.456  */
908 /* or    0d1.2345e12  */
909
910 const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
911
912 /* Prefix characters that indicate the start of an immediate
913    value.  */
914 #define is_immediate_prefix(C) ((C) == '#' || (C) == '$')
915
916 /* Separator character handling.  */
917
918 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
919
920 static inline int
921 skip_past_char (char ** str, char c)
922 {
923   /* PR gas/14987: Allow for whitespace before the expected character.  */
924   skip_whitespace (*str);
925
926   if (**str == c)
927     {
928       (*str)++;
929       return SUCCESS;
930     }
931   else
932     return FAIL;
933 }
934
935 #define skip_past_comma(str) skip_past_char (str, ',')
936
937 /* Arithmetic expressions (possibly involving symbols).  */
938
939 /* Return TRUE if anything in the expression is a bignum.  */
940
941 static int
942 walk_no_bignums (symbolS * sp)
943 {
944   if (symbol_get_value_expression (sp)->X_op == O_big)
945     return 1;
946
947   if (symbol_get_value_expression (sp)->X_add_symbol)
948     {
949       return (walk_no_bignums (symbol_get_value_expression (sp)->X_add_symbol)
950               || (symbol_get_value_expression (sp)->X_op_symbol
951                   && walk_no_bignums (symbol_get_value_expression (sp)->X_op_symbol)));
952     }
953
954   return 0;
955 }
956
957 static int in_my_get_expression = 0;
958
959 /* Third argument to my_get_expression.  */
960 #define GE_NO_PREFIX 0
961 #define GE_IMM_PREFIX 1
962 #define GE_OPT_PREFIX 2
963 /* This is a bit of a hack. Use an optional prefix, and also allow big (64-bit)
964    immediates, as can be used in Neon VMVN and VMOV immediate instructions.  */
965 #define GE_OPT_PREFIX_BIG 3
966
967 static int
968 my_get_expression (expressionS * ep, char ** str, int prefix_mode)
969 {
970   char * save_in;
971   segT   seg;
972
973   /* In unified syntax, all prefixes are optional.  */
974   if (unified_syntax)
975     prefix_mode = (prefix_mode == GE_OPT_PREFIX_BIG) ? prefix_mode
976                   : GE_OPT_PREFIX;
977
978   switch (prefix_mode)
979     {
980     case GE_NO_PREFIX: break;
981     case GE_IMM_PREFIX:
982       if (!is_immediate_prefix (**str))
983         {
984           inst.error = _("immediate expression requires a # prefix");
985           return FAIL;
986         }
987       (*str)++;
988       break;
989     case GE_OPT_PREFIX:
990     case GE_OPT_PREFIX_BIG:
991       if (is_immediate_prefix (**str))
992         (*str)++;
993       break;
994     default: abort ();
995     }
996
997   memset (ep, 0, sizeof (expressionS));
998
999   save_in = input_line_pointer;
1000   input_line_pointer = *str;
1001   in_my_get_expression = 1;
1002   seg = expression (ep);
1003   in_my_get_expression = 0;
1004
1005   if (ep->X_op == O_illegal || ep->X_op == O_absent)
1006     {
1007       /* We found a bad or missing expression in md_operand().  */
1008       *str = input_line_pointer;
1009       input_line_pointer = save_in;
1010       if (inst.error == NULL)
1011         inst.error = (ep->X_op == O_absent
1012                       ? _("missing expression") :_("bad expression"));
1013       return 1;
1014     }
1015
1016 #ifdef OBJ_AOUT
1017   if (seg != absolute_section
1018       && seg != text_section
1019       && seg != data_section
1020       && seg != bss_section
1021       && seg != undefined_section)
1022     {
1023       inst.error = _("bad segment");
1024       *str = input_line_pointer;
1025       input_line_pointer = save_in;
1026       return 1;
1027     }
1028 #else
1029   (void) seg;
1030 #endif
1031
1032   /* Get rid of any bignums now, so that we don't generate an error for which
1033      we can't establish a line number later on.  Big numbers are never valid
1034      in instructions, which is where this routine is always called.  */
1035   if (prefix_mode != GE_OPT_PREFIX_BIG
1036       && (ep->X_op == O_big
1037           || (ep->X_add_symbol
1038               && (walk_no_bignums (ep->X_add_symbol)
1039                   || (ep->X_op_symbol
1040                       && walk_no_bignums (ep->X_op_symbol))))))
1041     {
1042       inst.error = _("invalid constant");
1043       *str = input_line_pointer;
1044       input_line_pointer = save_in;
1045       return 1;
1046     }
1047
1048   *str = input_line_pointer;
1049   input_line_pointer = save_in;
1050   return 0;
1051 }
1052
1053 /* Turn a string in input_line_pointer into a floating point constant
1054    of type TYPE, and store the appropriate bytes in *LITP.  The number
1055    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1056    returned, or NULL on OK.
1057
1058    Note that fp constants aren't represent in the normal way on the ARM.
1059    In big endian mode, things are as expected.  However, in little endian
1060    mode fp constants are big-endian word-wise, and little-endian byte-wise
1061    within the words.  For example, (double) 1.1 in big endian mode is
1062    the byte sequence 3f f1 99 99 99 99 99 9a, and in little endian mode is
1063    the byte sequence 99 99 f1 3f 9a 99 99 99.
1064
1065    ??? The format of 12 byte floats is uncertain according to gcc's arm.h.  */
1066
1067 char *
1068 md_atof (int type, char * litP, int * sizeP)
1069 {
1070   int prec;
1071   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1072   char *t;
1073   int i;
1074
1075   switch (type)
1076     {
1077     case 'f':
1078     case 'F':
1079     case 's':
1080     case 'S':
1081       prec = 2;
1082       break;
1083
1084     case 'd':
1085     case 'D':
1086     case 'r':
1087     case 'R':
1088       prec = 4;
1089       break;
1090
1091     case 'x':
1092     case 'X':
1093       prec = 5;
1094       break;
1095
1096     case 'p':
1097     case 'P':
1098       prec = 5;
1099       break;
1100
1101     default:
1102       *sizeP = 0;
1103       return _("Unrecognized or unsupported floating point constant");
1104     }
1105
1106   t = atof_ieee (input_line_pointer, type, words);
1107   if (t)
1108     input_line_pointer = t;
1109   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1110
1111   if (target_big_endian)
1112     {
1113       for (i = 0; i < prec; i++)
1114         {
1115           md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1116           litP += sizeof (LITTLENUM_TYPE);
1117         }
1118     }
1119   else
1120     {
1121       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
1122         for (i = prec - 1; i >= 0; i--)
1123           {
1124             md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
1125             litP += sizeof (LITTLENUM_TYPE);
1126           }
1127       else
1128         /* For a 4 byte float the order of elements in `words' is 1 0.
1129            For an 8 byte float the order is 1 0 3 2.  */
1130         for (i = 0; i < prec; i += 2)
1131           {
1132             md_number_to_chars (litP, (valueT) words[i + 1],
1133                                 sizeof (LITTLENUM_TYPE));
1134             md_number_to_chars (litP + sizeof (LITTLENUM_TYPE),
1135                                 (valueT) words[i], sizeof (LITTLENUM_TYPE));
1136             litP += 2 * sizeof (LITTLENUM_TYPE);
1137           }
1138     }
1139
1140   return NULL;
1141 }
1142
1143 /* We handle all bad expressions here, so that we can report the faulty
1144    instruction in the error message.  */
1145 void
1146 md_operand (expressionS * exp)
1147 {
1148   if (in_my_get_expression)
1149     exp->X_op = O_illegal;
1150 }
1151
1152 /* Immediate values.  */
1153
1154 /* Generic immediate-value read function for use in directives.
1155    Accepts anything that 'expression' can fold to a constant.
1156    *val receives the number.  */
1157 #ifdef OBJ_ELF
1158 static int
1159 immediate_for_directive (int *val)
1160 {
1161   expressionS exp;
1162   exp.X_op = O_illegal;
1163
1164   if (is_immediate_prefix (*input_line_pointer))
1165     {
1166       input_line_pointer++;
1167       expression (&exp);
1168     }
1169
1170   if (exp.X_op != O_constant)
1171     {
1172       as_bad (_("expected #constant"));
1173       ignore_rest_of_line ();
1174       return FAIL;
1175     }
1176   *val = exp.X_add_number;
1177   return SUCCESS;
1178 }
1179 #endif
1180
1181 /* Register parsing.  */
1182
1183 /* Generic register parser.  CCP points to what should be the
1184    beginning of a register name.  If it is indeed a valid register
1185    name, advance CCP over it and return the reg_entry structure;
1186    otherwise return NULL.  Does not issue diagnostics.  */
1187
1188 static struct reg_entry *
1189 arm_reg_parse_multi (char **ccp)
1190 {
1191   char *start = *ccp;
1192   char *p;
1193   struct reg_entry *reg;
1194
1195   skip_whitespace (start);
1196
1197 #ifdef REGISTER_PREFIX
1198   if (*start != REGISTER_PREFIX)
1199     return NULL;
1200   start++;
1201 #endif
1202 #ifdef OPTIONAL_REGISTER_PREFIX
1203   if (*start == OPTIONAL_REGISTER_PREFIX)
1204     start++;
1205 #endif
1206
1207   p = start;
1208   if (!ISALPHA (*p) || !is_name_beginner (*p))
1209     return NULL;
1210
1211   do
1212     p++;
1213   while (ISALPHA (*p) || ISDIGIT (*p) || *p == '_');
1214
1215   reg = (struct reg_entry *) hash_find_n (arm_reg_hsh, start, p - start);
1216
1217   if (!reg)
1218     return NULL;
1219
1220   *ccp = p;
1221   return reg;
1222 }
1223
1224 static int
1225 arm_reg_alt_syntax (char **ccp, char *start, struct reg_entry *reg,
1226                     enum arm_reg_type type)
1227 {
1228   /* Alternative syntaxes are accepted for a few register classes.  */
1229   switch (type)
1230     {
1231     case REG_TYPE_MVF:
1232     case REG_TYPE_MVD:
1233     case REG_TYPE_MVFX:
1234     case REG_TYPE_MVDX:
1235       /* Generic coprocessor register names are allowed for these.  */
1236       if (reg && reg->type == REG_TYPE_CN)
1237         return reg->number;
1238       break;
1239
1240     case REG_TYPE_CP:
1241       /* For backward compatibility, a bare number is valid here.  */
1242       {
1243         unsigned long processor = strtoul (start, ccp, 10);
1244         if (*ccp != start && processor <= 15)
1245           return processor;
1246       }
1247
1248     case REG_TYPE_MMXWC:
1249       /* WC includes WCG.  ??? I'm not sure this is true for all
1250          instructions that take WC registers.  */
1251       if (reg && reg->type == REG_TYPE_MMXWCG)
1252         return reg->number;
1253       break;
1254
1255     default:
1256       break;
1257     }
1258
1259   return FAIL;
1260 }
1261
1262 /* As arm_reg_parse_multi, but the register must be of type TYPE, and the
1263    return value is the register number or FAIL.  */
1264
1265 static int
1266 arm_reg_parse (char **ccp, enum arm_reg_type type)
1267 {
1268   char *start = *ccp;
1269   struct reg_entry *reg = arm_reg_parse_multi (ccp);
1270   int ret;
1271
1272   /* Do not allow a scalar (reg+index) to parse as a register.  */
1273   if (reg && reg->neon && (reg->neon->defined & NTA_HASINDEX))
1274     return FAIL;
1275
1276   if (reg && reg->type == type)
1277     return reg->number;
1278
1279   if ((ret = arm_reg_alt_syntax (ccp, start, reg, type)) != FAIL)
1280     return ret;
1281
1282   *ccp = start;
1283   return FAIL;
1284 }
1285
1286 /* Parse a Neon type specifier. *STR should point at the leading '.'
1287    character. Does no verification at this stage that the type fits the opcode
1288    properly. E.g.,
1289
1290      .i32.i32.s16
1291      .s32.f32
1292      .u16
1293
1294    Can all be legally parsed by this function.
1295
1296    Fills in neon_type struct pointer with parsed information, and updates STR
1297    to point after the parsed type specifier. Returns SUCCESS if this was a legal
1298    type, FAIL if not.  */
1299
1300 static int
1301 parse_neon_type (struct neon_type *type, char **str)
1302 {
1303   char *ptr = *str;
1304
1305   if (type)
1306     type->elems = 0;
1307
1308   while (type->elems < NEON_MAX_TYPE_ELS)
1309     {
1310       enum neon_el_type thistype = NT_untyped;
1311       unsigned thissize = -1u;
1312
1313       if (*ptr != '.')
1314         break;
1315
1316       ptr++;
1317
1318       /* Just a size without an explicit type.  */
1319       if (ISDIGIT (*ptr))
1320         goto parsesize;
1321
1322       switch (TOLOWER (*ptr))
1323         {
1324         case 'i': thistype = NT_integer; break;
1325         case 'f': thistype = NT_float; break;
1326         case 'p': thistype = NT_poly; break;
1327         case 's': thistype = NT_signed; break;
1328         case 'u': thistype = NT_unsigned; break;
1329         case 'd':
1330           thistype = NT_float;
1331           thissize = 64;
1332           ptr++;
1333           goto done;
1334         default:
1335           as_bad (_("unexpected character `%c' in type specifier"), *ptr);
1336           return FAIL;
1337         }
1338
1339       ptr++;
1340
1341       /* .f is an abbreviation for .f32.  */
1342       if (thistype == NT_float && !ISDIGIT (*ptr))
1343         thissize = 32;
1344       else
1345         {
1346         parsesize:
1347           thissize = strtoul (ptr, &ptr, 10);
1348
1349           if (thissize != 8 && thissize != 16 && thissize != 32
1350               && thissize != 64)
1351             {
1352               as_bad (_("bad size %d in type specifier"), thissize);
1353               return FAIL;
1354             }
1355         }
1356
1357       done:
1358       if (type)
1359         {
1360           type->el[type->elems].type = thistype;
1361           type->el[type->elems].size = thissize;
1362           type->elems++;
1363         }
1364     }
1365
1366   /* Empty/missing type is not a successful parse.  */
1367   if (type->elems == 0)
1368     return FAIL;
1369
1370   *str = ptr;
1371
1372   return SUCCESS;
1373 }
1374
1375 /* Errors may be set multiple times during parsing or bit encoding
1376    (particularly in the Neon bits), but usually the earliest error which is set
1377    will be the most meaningful. Avoid overwriting it with later (cascading)
1378    errors by calling this function.  */
1379
1380 static void
1381 first_error (const char *err)
1382 {
1383   if (!inst.error)
1384     inst.error = err;
1385 }
1386
1387 /* Parse a single type, e.g. ".s32", leading period included.  */
1388 static int
1389 parse_neon_operand_type (struct neon_type_el *vectype, char **ccp)
1390 {
1391   char *str = *ccp;
1392   struct neon_type optype;
1393
1394   if (*str == '.')
1395     {
1396       if (parse_neon_type (&optype, &str) == SUCCESS)
1397         {
1398           if (optype.elems == 1)
1399             *vectype = optype.el[0];
1400           else
1401             {
1402               first_error (_("only one type should be specified for operand"));
1403               return FAIL;
1404             }
1405         }
1406       else
1407         {
1408           first_error (_("vector type expected"));
1409           return FAIL;
1410         }
1411     }
1412   else
1413     return FAIL;
1414
1415   *ccp = str;
1416
1417   return SUCCESS;
1418 }
1419
1420 /* Special meanings for indices (which have a range of 0-7), which will fit into
1421    a 4-bit integer.  */
1422
1423 #define NEON_ALL_LANES          15
1424 #define NEON_INTERLEAVE_LANES   14
1425
1426 /* Parse either a register or a scalar, with an optional type. Return the
1427    register number, and optionally fill in the actual type of the register
1428    when multiple alternatives were given (NEON_TYPE_NDQ) in *RTYPE, and
1429    type/index information in *TYPEINFO.  */
1430
1431 static int
1432 parse_typed_reg_or_scalar (char **ccp, enum arm_reg_type type,
1433                            enum arm_reg_type *rtype,
1434                            struct neon_typed_alias *typeinfo)
1435 {
1436   char *str = *ccp;
1437   struct reg_entry *reg = arm_reg_parse_multi (&str);
1438   struct neon_typed_alias atype;
1439   struct neon_type_el parsetype;
1440
1441   atype.defined = 0;
1442   atype.index = -1;
1443   atype.eltype.type = NT_invtype;
1444   atype.eltype.size = -1;
1445
1446   /* Try alternate syntax for some types of register. Note these are mutually
1447      exclusive with the Neon syntax extensions.  */
1448   if (reg == NULL)
1449     {
1450       int altreg = arm_reg_alt_syntax (&str, *ccp, reg, type);
1451       if (altreg != FAIL)
1452         *ccp = str;
1453       if (typeinfo)
1454         *typeinfo = atype;
1455       return altreg;
1456     }
1457
1458   /* Undo polymorphism when a set of register types may be accepted.  */
1459   if ((type == REG_TYPE_NDQ
1460        && (reg->type == REG_TYPE_NQ || reg->type == REG_TYPE_VFD))
1461       || (type == REG_TYPE_VFSD
1462           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD))
1463       || (type == REG_TYPE_NSDQ
1464           && (reg->type == REG_TYPE_VFS || reg->type == REG_TYPE_VFD
1465               || reg->type == REG_TYPE_NQ))
1466       || (type == REG_TYPE_MMXWC
1467           && (reg->type == REG_TYPE_MMXWCG)))
1468     type = (enum arm_reg_type) reg->type;
1469
1470   if (type != reg->type)
1471     return FAIL;
1472
1473   if (reg->neon)
1474     atype = *reg->neon;
1475
1476   if (parse_neon_operand_type (&parsetype, &str) == SUCCESS)
1477     {
1478       if ((atype.defined & NTA_HASTYPE) != 0)
1479         {
1480           first_error (_("can't redefine type for operand"));
1481           return FAIL;
1482         }
1483       atype.defined |= NTA_HASTYPE;
1484       atype.eltype = parsetype;
1485     }
1486
1487   if (skip_past_char (&str, '[') == SUCCESS)
1488     {
1489       if (type != REG_TYPE_VFD)
1490         {
1491           first_error (_("only D registers may be indexed"));
1492           return FAIL;
1493         }
1494
1495       if ((atype.defined & NTA_HASINDEX) != 0)
1496         {
1497           first_error (_("can't change index for operand"));
1498           return FAIL;
1499         }
1500
1501       atype.defined |= NTA_HASINDEX;
1502
1503       if (skip_past_char (&str, ']') == SUCCESS)
1504         atype.index = NEON_ALL_LANES;
1505       else
1506         {
1507           expressionS exp;
1508
1509           my_get_expression (&exp, &str, GE_NO_PREFIX);
1510
1511           if (exp.X_op != O_constant)
1512             {
1513               first_error (_("constant expression required"));
1514               return FAIL;
1515             }
1516
1517           if (skip_past_char (&str, ']') == FAIL)
1518             return FAIL;
1519
1520           atype.index = exp.X_add_number;
1521         }
1522     }
1523
1524   if (typeinfo)
1525     *typeinfo = atype;
1526
1527   if (rtype)
1528     *rtype = type;
1529
1530   *ccp = str;
1531
1532   return reg->number;
1533 }
1534
1535 /* Like arm_reg_parse, but allow allow the following extra features:
1536     - If RTYPE is non-zero, return the (possibly restricted) type of the
1537       register (e.g. Neon double or quad reg when either has been requested).
1538     - If this is a Neon vector type with additional type information, fill
1539       in the struct pointed to by VECTYPE (if non-NULL).
1540    This function will fault on encountering a scalar.  */
1541
1542 static int
1543 arm_typed_reg_parse (char **ccp, enum arm_reg_type type,
1544                      enum arm_reg_type *rtype, struct neon_type_el *vectype)
1545 {
1546   struct neon_typed_alias atype;
1547   char *str = *ccp;
1548   int reg = parse_typed_reg_or_scalar (&str, type, rtype, &atype);
1549
1550   if (reg == FAIL)
1551     return FAIL;
1552
1553   /* Do not allow regname(... to parse as a register.  */
1554   if (*str == '(')
1555     return FAIL;
1556
1557   /* Do not allow a scalar (reg+index) to parse as a register.  */
1558   if ((atype.defined & NTA_HASINDEX) != 0)
1559     {
1560       first_error (_("register operand expected, but got scalar"));
1561       return FAIL;
1562     }
1563
1564   if (vectype)
1565     *vectype = atype.eltype;
1566
1567   *ccp = str;
1568
1569   return reg;
1570 }
1571
1572 #define NEON_SCALAR_REG(X)      ((X) >> 4)
1573 #define NEON_SCALAR_INDEX(X)    ((X) & 15)
1574
1575 /* Parse a Neon scalar. Most of the time when we're parsing a scalar, we don't
1576    have enough information to be able to do a good job bounds-checking. So, we
1577    just do easy checks here, and do further checks later.  */
1578
1579 static int
1580 parse_scalar (char **ccp, int elsize, struct neon_type_el *type)
1581 {
1582   int reg;
1583   char *str = *ccp;
1584   struct neon_typed_alias atype;
1585
1586   reg = parse_typed_reg_or_scalar (&str, REG_TYPE_VFD, NULL, &atype);
1587
1588   if (reg == FAIL || (atype.defined & NTA_HASINDEX) == 0)
1589     return FAIL;
1590
1591   if (atype.index == NEON_ALL_LANES)
1592     {
1593       first_error (_("scalar must have an index"));
1594       return FAIL;
1595     }
1596   else if (atype.index >= 64 / elsize)
1597     {
1598       first_error (_("scalar index out of range"));
1599       return FAIL;
1600     }
1601
1602   if (type)
1603     *type = atype.eltype;
1604
1605   *ccp = str;
1606
1607   return reg * 16 + atype.index;
1608 }
1609
1610 /* Parse an ARM register list.  Returns the bitmask, or FAIL.  */
1611
1612 static long
1613 parse_reg_list (char ** strp)
1614 {
1615   char * str = * strp;
1616   long   range = 0;
1617   int    another_range;
1618
1619   /* We come back here if we get ranges concatenated by '+' or '|'.  */
1620   do
1621     {
1622       skip_whitespace (str);
1623
1624       another_range = 0;
1625
1626       if (*str == '{')
1627         {
1628           int in_range = 0;
1629           int cur_reg = -1;
1630
1631           str++;
1632           do
1633             {
1634               int reg;
1635
1636               if ((reg = arm_reg_parse (&str, REG_TYPE_RN)) == FAIL)
1637                 {
1638                   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
1639                   return FAIL;
1640                 }
1641
1642               if (in_range)
1643                 {
1644                   int i;
1645
1646                   if (reg <= cur_reg)
1647                     {
1648                       first_error (_("bad range in register list"));
1649                       return FAIL;
1650                     }
1651
1652                   for (i = cur_reg + 1; i < reg; i++)
1653                     {
1654                       if (range & (1 << i))
1655                         as_tsktsk
1656                           (_("Warning: duplicated register (r%d) in register list"),
1657                            i);
1658                       else
1659                         range |= 1 << i;
1660                     }
1661                   in_range = 0;
1662                 }
1663
1664               if (range & (1 << reg))
1665                 as_tsktsk (_("Warning: duplicated register (r%d) in register list"),
1666                            reg);
1667               else if (reg <= cur_reg)
1668                 as_tsktsk (_("Warning: register range not in ascending order"));
1669
1670               range |= 1 << reg;
1671               cur_reg = reg;
1672             }
1673           while (skip_past_comma (&str) != FAIL
1674                  || (in_range = 1, *str++ == '-'));
1675           str--;
1676
1677           if (skip_past_char (&str, '}') == FAIL)
1678             {
1679               first_error (_("missing `}'"));
1680               return FAIL;
1681             }
1682         }
1683       else
1684         {
1685           expressionS exp;
1686
1687           if (my_get_expression (&exp, &str, GE_NO_PREFIX))
1688             return FAIL;
1689
1690           if (exp.X_op == O_constant)
1691             {
1692               if (exp.X_add_number
1693                   != (exp.X_add_number & 0x0000ffff))
1694                 {
1695                   inst.error = _("invalid register mask");
1696                   return FAIL;
1697                 }
1698
1699               if ((range & exp.X_add_number) != 0)
1700                 {
1701                   int regno = range & exp.X_add_number;
1702
1703                   regno &= -regno;
1704                   regno = (1 << regno) - 1;
1705                   as_tsktsk
1706                     (_("Warning: duplicated register (r%d) in register list"),
1707                      regno);
1708                 }
1709
1710               range |= exp.X_add_number;
1711             }
1712           else
1713             {
1714               if (inst.reloc.type != 0)
1715                 {
1716                   inst.error = _("expression too complex");
1717                   return FAIL;
1718                 }
1719
1720               memcpy (&inst.reloc.exp, &exp, sizeof (expressionS));
1721               inst.reloc.type = BFD_RELOC_ARM_MULTI;
1722               inst.reloc.pc_rel = 0;
1723             }
1724         }
1725
1726       if (*str == '|' || *str == '+')
1727         {
1728           str++;
1729           another_range = 1;
1730         }
1731     }
1732   while (another_range);
1733
1734   *strp = str;
1735   return range;
1736 }
1737
1738 /* Types of registers in a list.  */
1739
1740 enum reg_list_els
1741 {
1742   REGLIST_VFP_S,
1743   REGLIST_VFP_D,
1744   REGLIST_NEON_D
1745 };
1746
1747 /* Parse a VFP register list.  If the string is invalid return FAIL.
1748    Otherwise return the number of registers, and set PBASE to the first
1749    register.  Parses registers of type ETYPE.
1750    If REGLIST_NEON_D is used, several syntax enhancements are enabled:
1751      - Q registers can be used to specify pairs of D registers
1752      - { } can be omitted from around a singleton register list
1753          FIXME: This is not implemented, as it would require backtracking in
1754          some cases, e.g.:
1755            vtbl.8 d3,d4,d5
1756          This could be done (the meaning isn't really ambiguous), but doesn't
1757          fit in well with the current parsing framework.
1758      - 32 D registers may be used (also true for VFPv3).
1759    FIXME: Types are ignored in these register lists, which is probably a
1760    bug.  */
1761
1762 static int
1763 parse_vfp_reg_list (char **ccp, unsigned int *pbase, enum reg_list_els etype)
1764 {
1765   char *str = *ccp;
1766   int base_reg;
1767   int new_base;
1768   enum arm_reg_type regtype = (enum arm_reg_type) 0;
1769   int max_regs = 0;
1770   int count = 0;
1771   int warned = 0;
1772   unsigned long mask = 0;
1773   int i;
1774
1775   if (skip_past_char (&str, '{') == FAIL)
1776     {
1777       inst.error = _("expecting {");
1778       return FAIL;
1779     }
1780
1781   switch (etype)
1782     {
1783     case REGLIST_VFP_S:
1784       regtype = REG_TYPE_VFS;
1785       max_regs = 32;
1786       break;
1787
1788     case REGLIST_VFP_D:
1789       regtype = REG_TYPE_VFD;
1790       break;
1791
1792     case REGLIST_NEON_D:
1793       regtype = REG_TYPE_NDQ;
1794       break;
1795     }
1796
1797   if (etype != REGLIST_VFP_S)
1798     {
1799       /* VFPv3 allows 32 D registers, except for the VFPv3-D16 variant.  */
1800       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
1801         {
1802           max_regs = 32;
1803           if (thumb_mode)
1804             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
1805                                     fpu_vfp_ext_d32);
1806           else
1807             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
1808                                     fpu_vfp_ext_d32);
1809         }
1810       else
1811         max_regs = 16;
1812     }
1813
1814   base_reg = max_regs;
1815
1816   do
1817     {
1818       int setmask = 1, addregs = 1;
1819
1820       new_base = arm_typed_reg_parse (&str, regtype, &regtype, NULL);
1821
1822       if (new_base == FAIL)
1823         {
1824           first_error (_(reg_expected_msgs[regtype]));
1825           return FAIL;
1826         }
1827
1828       if (new_base >= max_regs)
1829         {
1830           first_error (_("register out of range in list"));
1831           return FAIL;
1832         }
1833
1834       /* Note: a value of 2 * n is returned for the register Q<n>.  */
1835       if (regtype == REG_TYPE_NQ)
1836         {
1837           setmask = 3;
1838           addregs = 2;
1839         }
1840
1841       if (new_base < base_reg)
1842         base_reg = new_base;
1843
1844       if (mask & (setmask << new_base))
1845         {
1846           first_error (_("invalid register list"));
1847           return FAIL;
1848         }
1849
1850       if ((mask >> new_base) != 0 && ! warned)
1851         {
1852           as_tsktsk (_("register list not in ascending order"));
1853           warned = 1;
1854         }
1855
1856       mask |= setmask << new_base;
1857       count += addregs;
1858
1859       if (*str == '-') /* We have the start of a range expression */
1860         {
1861           int high_range;
1862
1863           str++;
1864
1865           if ((high_range = arm_typed_reg_parse (&str, regtype, NULL, NULL))
1866               == FAIL)
1867             {
1868               inst.error = gettext (reg_expected_msgs[regtype]);
1869               return FAIL;
1870             }
1871
1872           if (high_range >= max_regs)
1873             {
1874               first_error (_("register out of range in list"));
1875               return FAIL;
1876             }
1877
1878           if (regtype == REG_TYPE_NQ)
1879             high_range = high_range + 1;
1880
1881           if (high_range <= new_base)
1882             {
1883               inst.error = _("register range not in ascending order");
1884               return FAIL;
1885             }
1886
1887           for (new_base += addregs; new_base <= high_range; new_base += addregs)
1888             {
1889               if (mask & (setmask << new_base))
1890                 {
1891                   inst.error = _("invalid register list");
1892                   return FAIL;
1893                 }
1894
1895               mask |= setmask << new_base;
1896               count += addregs;
1897             }
1898         }
1899     }
1900   while (skip_past_comma (&str) != FAIL);
1901
1902   str++;
1903
1904   /* Sanity check -- should have raised a parse error above.  */
1905   if (count == 0 || count > max_regs)
1906     abort ();
1907
1908   *pbase = base_reg;
1909
1910   /* Final test -- the registers must be consecutive.  */
1911   mask >>= base_reg;
1912   for (i = 0; i < count; i++)
1913     {
1914       if ((mask & (1u << i)) == 0)
1915         {
1916           inst.error = _("non-contiguous register range");
1917           return FAIL;
1918         }
1919     }
1920
1921   *ccp = str;
1922
1923   return count;
1924 }
1925
1926 /* True if two alias types are the same.  */
1927
1928 static bfd_boolean
1929 neon_alias_types_same (struct neon_typed_alias *a, struct neon_typed_alias *b)
1930 {
1931   if (!a && !b)
1932     return TRUE;
1933
1934   if (!a || !b)
1935     return FALSE;
1936
1937   if (a->defined != b->defined)
1938     return FALSE;
1939
1940   if ((a->defined & NTA_HASTYPE) != 0
1941       && (a->eltype.type != b->eltype.type
1942           || a->eltype.size != b->eltype.size))
1943     return FALSE;
1944
1945   if ((a->defined & NTA_HASINDEX) != 0
1946       && (a->index != b->index))
1947     return FALSE;
1948
1949   return TRUE;
1950 }
1951
1952 /* Parse element/structure lists for Neon VLD<n> and VST<n> instructions.
1953    The base register is put in *PBASE.
1954    The lane (or one of the NEON_*_LANES constants) is placed in bits [3:0] of
1955    the return value.
1956    The register stride (minus one) is put in bit 4 of the return value.
1957    Bits [6:5] encode the list length (minus one).
1958    The type of the list elements is put in *ELTYPE, if non-NULL.  */
1959
1960 #define NEON_LANE(X)            ((X) & 0xf)
1961 #define NEON_REG_STRIDE(X)      ((((X) >> 4) & 1) + 1)
1962 #define NEON_REGLIST_LENGTH(X)  ((((X) >> 5) & 3) + 1)
1963
1964 static int
1965 parse_neon_el_struct_list (char **str, unsigned *pbase,
1966                            struct neon_type_el *eltype)
1967 {
1968   char *ptr = *str;
1969   int base_reg = -1;
1970   int reg_incr = -1;
1971   int count = 0;
1972   int lane = -1;
1973   int leading_brace = 0;
1974   enum arm_reg_type rtype = REG_TYPE_NDQ;
1975   const char *const incr_error = _("register stride must be 1 or 2");
1976   const char *const type_error = _("mismatched element/structure types in list");
1977   struct neon_typed_alias firsttype;
1978
1979   if (skip_past_char (&ptr, '{') == SUCCESS)
1980     leading_brace = 1;
1981
1982   do
1983     {
1984       struct neon_typed_alias atype;
1985       int getreg = parse_typed_reg_or_scalar (&ptr, rtype, &rtype, &atype);
1986
1987       if (getreg == FAIL)
1988         {
1989           first_error (_(reg_expected_msgs[rtype]));
1990           return FAIL;
1991         }
1992
1993       if (base_reg == -1)
1994         {
1995           base_reg = getreg;
1996           if (rtype == REG_TYPE_NQ)
1997             {
1998               reg_incr = 1;
1999             }
2000           firsttype = atype;
2001         }
2002       else if (reg_incr == -1)
2003         {
2004           reg_incr = getreg - base_reg;
2005           if (reg_incr < 1 || reg_incr > 2)
2006             {
2007               first_error (_(incr_error));
2008               return FAIL;
2009             }
2010         }
2011       else if (getreg != base_reg + reg_incr * count)
2012         {
2013           first_error (_(incr_error));
2014           return FAIL;
2015         }
2016
2017       if (! neon_alias_types_same (&atype, &firsttype))
2018         {
2019           first_error (_(type_error));
2020           return FAIL;
2021         }
2022
2023       /* Handle Dn-Dm or Qn-Qm syntax. Can only be used with non-indexed list
2024          modes.  */
2025       if (ptr[0] == '-')
2026         {
2027           struct neon_typed_alias htype;
2028           int hireg, dregs = (rtype == REG_TYPE_NQ) ? 2 : 1;
2029           if (lane == -1)
2030             lane = NEON_INTERLEAVE_LANES;
2031           else if (lane != NEON_INTERLEAVE_LANES)
2032             {
2033               first_error (_(type_error));
2034               return FAIL;
2035             }
2036           if (reg_incr == -1)
2037             reg_incr = 1;
2038           else if (reg_incr != 1)
2039             {
2040               first_error (_("don't use Rn-Rm syntax with non-unit stride"));
2041               return FAIL;
2042             }
2043           ptr++;
2044           hireg = parse_typed_reg_or_scalar (&ptr, rtype, NULL, &htype);
2045           if (hireg == FAIL)
2046             {
2047               first_error (_(reg_expected_msgs[rtype]));
2048               return FAIL;
2049             }
2050           if (! neon_alias_types_same (&htype, &firsttype))
2051             {
2052               first_error (_(type_error));
2053               return FAIL;
2054             }
2055           count += hireg + dregs - getreg;
2056           continue;
2057         }
2058
2059       /* If we're using Q registers, we can't use [] or [n] syntax.  */
2060       if (rtype == REG_TYPE_NQ)
2061         {
2062           count += 2;
2063           continue;
2064         }
2065
2066       if ((atype.defined & NTA_HASINDEX) != 0)
2067         {
2068           if (lane == -1)
2069             lane = atype.index;
2070           else if (lane != atype.index)
2071             {
2072               first_error (_(type_error));
2073               return FAIL;
2074             }
2075         }
2076       else if (lane == -1)
2077         lane = NEON_INTERLEAVE_LANES;
2078       else if (lane != NEON_INTERLEAVE_LANES)
2079         {
2080           first_error (_(type_error));
2081           return FAIL;
2082         }
2083       count++;
2084     }
2085   while ((count != 1 || leading_brace) && skip_past_comma (&ptr) != FAIL);
2086
2087   /* No lane set by [x]. We must be interleaving structures.  */
2088   if (lane == -1)
2089     lane = NEON_INTERLEAVE_LANES;
2090
2091   /* Sanity check.  */
2092   if (lane == -1 || base_reg == -1 || count < 1 || count > 4
2093       || (count > 1 && reg_incr == -1))
2094     {
2095       first_error (_("error parsing element/structure list"));
2096       return FAIL;
2097     }
2098
2099   if ((count > 1 || leading_brace) && skip_past_char (&ptr, '}') == FAIL)
2100     {
2101       first_error (_("expected }"));
2102       return FAIL;
2103     }
2104
2105   if (reg_incr == -1)
2106     reg_incr = 1;
2107
2108   if (eltype)
2109     *eltype = firsttype.eltype;
2110
2111   *pbase = base_reg;
2112   *str = ptr;
2113
2114   return lane | ((reg_incr - 1) << 4) | ((count - 1) << 5);
2115 }
2116
2117 /* Parse an explicit relocation suffix on an expression.  This is
2118    either nothing, or a word in parentheses.  Note that if !OBJ_ELF,
2119    arm_reloc_hsh contains no entries, so this function can only
2120    succeed if there is no () after the word.  Returns -1 on error,
2121    BFD_RELOC_UNUSED if there wasn't any suffix.  */
2122
2123 static int
2124 parse_reloc (char **str)
2125 {
2126   struct reloc_entry *r;
2127   char *p, *q;
2128
2129   if (**str != '(')
2130     return BFD_RELOC_UNUSED;
2131
2132   p = *str + 1;
2133   q = p;
2134
2135   while (*q && *q != ')' && *q != ',')
2136     q++;
2137   if (*q != ')')
2138     return -1;
2139
2140   if ((r = (struct reloc_entry *)
2141        hash_find_n (arm_reloc_hsh, p, q - p)) == NULL)
2142     return -1;
2143
2144   *str = q + 1;
2145   return r->reloc;
2146 }
2147
2148 /* Directives: register aliases.  */
2149
2150 static struct reg_entry *
2151 insert_reg_alias (char *str, unsigned number, int type)
2152 {
2153   struct reg_entry *new_reg;
2154   const char *name;
2155
2156   if ((new_reg = (struct reg_entry *) hash_find (arm_reg_hsh, str)) != 0)
2157     {
2158       if (new_reg->builtin)
2159         as_warn (_("ignoring attempt to redefine built-in register '%s'"), str);
2160
2161       /* Only warn about a redefinition if it's not defined as the
2162          same register.  */
2163       else if (new_reg->number != number || new_reg->type != type)
2164         as_warn (_("ignoring redefinition of register alias '%s'"), str);
2165
2166       return NULL;
2167     }
2168
2169   name = xstrdup (str);
2170   new_reg = (struct reg_entry *) xmalloc (sizeof (struct reg_entry));
2171
2172   new_reg->name = name;
2173   new_reg->number = number;
2174   new_reg->type = type;
2175   new_reg->builtin = FALSE;
2176   new_reg->neon = NULL;
2177
2178   if (hash_insert (arm_reg_hsh, name, (void *) new_reg))
2179     abort ();
2180
2181   return new_reg;
2182 }
2183
2184 static void
2185 insert_neon_reg_alias (char *str, int number, int type,
2186                        struct neon_typed_alias *atype)
2187 {
2188   struct reg_entry *reg = insert_reg_alias (str, number, type);
2189
2190   if (!reg)
2191     {
2192       first_error (_("attempt to redefine typed alias"));
2193       return;
2194     }
2195
2196   if (atype)
2197     {
2198       reg->neon = (struct neon_typed_alias *)
2199           xmalloc (sizeof (struct neon_typed_alias));
2200       *reg->neon = *atype;
2201     }
2202 }
2203
2204 /* Look for the .req directive.  This is of the form:
2205
2206         new_register_name .req existing_register_name
2207
2208    If we find one, or if it looks sufficiently like one that we want to
2209    handle any error here, return TRUE.  Otherwise return FALSE.  */
2210
2211 static bfd_boolean
2212 create_register_alias (char * newname, char *p)
2213 {
2214   struct reg_entry *old;
2215   char *oldname, *nbuf;
2216   size_t nlen;
2217
2218   /* The input scrubber ensures that whitespace after the mnemonic is
2219      collapsed to single spaces.  */
2220   oldname = p;
2221   if (strncmp (oldname, " .req ", 6) != 0)
2222     return FALSE;
2223
2224   oldname += 6;
2225   if (*oldname == '\0')
2226     return FALSE;
2227
2228   old = (struct reg_entry *) hash_find (arm_reg_hsh, oldname);
2229   if (!old)
2230     {
2231       as_warn (_("unknown register '%s' -- .req ignored"), oldname);
2232       return TRUE;
2233     }
2234
2235   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2236      the desired alias name, and p points to its end.  If not, then
2237      the desired alias name is in the global original_case_string.  */
2238 #ifdef TC_CASE_SENSITIVE
2239   nlen = p - newname;
2240 #else
2241   newname = original_case_string;
2242   nlen = strlen (newname);
2243 #endif
2244
2245   nbuf = (char *) alloca (nlen + 1);
2246   memcpy (nbuf, newname, nlen);
2247   nbuf[nlen] = '\0';
2248
2249   /* Create aliases under the new name as stated; an all-lowercase
2250      version of the new name; and an all-uppercase version of the new
2251      name.  */
2252   if (insert_reg_alias (nbuf, old->number, old->type) != NULL)
2253     {
2254       for (p = nbuf; *p; p++)
2255         *p = TOUPPER (*p);
2256
2257       if (strncmp (nbuf, newname, nlen))
2258         {
2259           /* If this attempt to create an additional alias fails, do not bother
2260              trying to create the all-lower case alias.  We will fail and issue
2261              a second, duplicate error message.  This situation arises when the
2262              programmer does something like:
2263                foo .req r0
2264                Foo .req r1
2265              The second .req creates the "Foo" alias but then fails to create
2266              the artificial FOO alias because it has already been created by the
2267              first .req.  */
2268           if (insert_reg_alias (nbuf, old->number, old->type) == NULL)
2269             return TRUE;
2270         }
2271
2272       for (p = nbuf; *p; p++)
2273         *p = TOLOWER (*p);
2274
2275       if (strncmp (nbuf, newname, nlen))
2276         insert_reg_alias (nbuf, old->number, old->type);
2277     }
2278
2279   return TRUE;
2280 }
2281
2282 /* Create a Neon typed/indexed register alias using directives, e.g.:
2283      X .dn d5.s32[1]
2284      Y .qn 6.s16
2285      Z .dn d7
2286      T .dn Z[0]
2287    These typed registers can be used instead of the types specified after the
2288    Neon mnemonic, so long as all operands given have types. Types can also be
2289    specified directly, e.g.:
2290      vadd d0.s32, d1.s32, d2.s32  */
2291
2292 static bfd_boolean
2293 create_neon_reg_alias (char *newname, char *p)
2294 {
2295   enum arm_reg_type basetype;
2296   struct reg_entry *basereg;
2297   struct reg_entry mybasereg;
2298   struct neon_type ntype;
2299   struct neon_typed_alias typeinfo;
2300   char *namebuf, *nameend ATTRIBUTE_UNUSED;
2301   int namelen;
2302
2303   typeinfo.defined = 0;
2304   typeinfo.eltype.type = NT_invtype;
2305   typeinfo.eltype.size = -1;
2306   typeinfo.index = -1;
2307
2308   nameend = p;
2309
2310   if (strncmp (p, " .dn ", 5) == 0)
2311     basetype = REG_TYPE_VFD;
2312   else if (strncmp (p, " .qn ", 5) == 0)
2313     basetype = REG_TYPE_NQ;
2314   else
2315     return FALSE;
2316
2317   p += 5;
2318
2319   if (*p == '\0')
2320     return FALSE;
2321
2322   basereg = arm_reg_parse_multi (&p);
2323
2324   if (basereg && basereg->type != basetype)
2325     {
2326       as_bad (_("bad type for register"));
2327       return FALSE;
2328     }
2329
2330   if (basereg == NULL)
2331     {
2332       expressionS exp;
2333       /* Try parsing as an integer.  */
2334       my_get_expression (&exp, &p, GE_NO_PREFIX);
2335       if (exp.X_op != O_constant)
2336         {
2337           as_bad (_("expression must be constant"));
2338           return FALSE;
2339         }
2340       basereg = &mybasereg;
2341       basereg->number = (basetype == REG_TYPE_NQ) ? exp.X_add_number * 2
2342                                                   : exp.X_add_number;
2343       basereg->neon = 0;
2344     }
2345
2346   if (basereg->neon)
2347     typeinfo = *basereg->neon;
2348
2349   if (parse_neon_type (&ntype, &p) == SUCCESS)
2350     {
2351       /* We got a type.  */
2352       if (typeinfo.defined & NTA_HASTYPE)
2353         {
2354           as_bad (_("can't redefine the type of a register alias"));
2355           return FALSE;
2356         }
2357
2358       typeinfo.defined |= NTA_HASTYPE;
2359       if (ntype.elems != 1)
2360         {
2361           as_bad (_("you must specify a single type only"));
2362           return FALSE;
2363         }
2364       typeinfo.eltype = ntype.el[0];
2365     }
2366
2367   if (skip_past_char (&p, '[') == SUCCESS)
2368     {
2369       expressionS exp;
2370       /* We got a scalar index.  */
2371
2372       if (typeinfo.defined & NTA_HASINDEX)
2373         {
2374           as_bad (_("can't redefine the index of a scalar alias"));
2375           return FALSE;
2376         }
2377
2378       my_get_expression (&exp, &p, GE_NO_PREFIX);
2379
2380       if (exp.X_op != O_constant)
2381         {
2382           as_bad (_("scalar index must be constant"));
2383           return FALSE;
2384         }
2385
2386       typeinfo.defined |= NTA_HASINDEX;
2387       typeinfo.index = exp.X_add_number;
2388
2389       if (skip_past_char (&p, ']') == FAIL)
2390         {
2391           as_bad (_("expecting ]"));
2392           return FALSE;
2393         }
2394     }
2395
2396   /* If TC_CASE_SENSITIVE is defined, then newname already points to
2397      the desired alias name, and p points to its end.  If not, then
2398      the desired alias name is in the global original_case_string.  */
2399 #ifdef TC_CASE_SENSITIVE
2400   namelen = nameend - newname;
2401 #else
2402   newname = original_case_string;
2403   namelen = strlen (newname);
2404 #endif
2405
2406   namebuf = (char *) alloca (namelen + 1);
2407   strncpy (namebuf, newname, namelen);
2408   namebuf[namelen] = '\0';
2409
2410   insert_neon_reg_alias (namebuf, basereg->number, basetype,
2411                          typeinfo.defined != 0 ? &typeinfo : NULL);
2412
2413   /* Insert name in all uppercase.  */
2414   for (p = namebuf; *p; p++)
2415     *p = TOUPPER (*p);
2416
2417   if (strncmp (namebuf, newname, namelen))
2418     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2419                            typeinfo.defined != 0 ? &typeinfo : NULL);
2420
2421   /* Insert name in all lowercase.  */
2422   for (p = namebuf; *p; p++)
2423     *p = TOLOWER (*p);
2424
2425   if (strncmp (namebuf, newname, namelen))
2426     insert_neon_reg_alias (namebuf, basereg->number, basetype,
2427                            typeinfo.defined != 0 ? &typeinfo : NULL);
2428
2429   return TRUE;
2430 }
2431
2432 /* Should never be called, as .req goes between the alias and the
2433    register name, not at the beginning of the line.  */
2434
2435 static void
2436 s_req (int a ATTRIBUTE_UNUSED)
2437 {
2438   as_bad (_("invalid syntax for .req directive"));
2439 }
2440
2441 static void
2442 s_dn (int a ATTRIBUTE_UNUSED)
2443 {
2444   as_bad (_("invalid syntax for .dn directive"));
2445 }
2446
2447 static void
2448 s_qn (int a ATTRIBUTE_UNUSED)
2449 {
2450   as_bad (_("invalid syntax for .qn directive"));
2451 }
2452
2453 /* The .unreq directive deletes an alias which was previously defined
2454    by .req.  For example:
2455
2456        my_alias .req r11
2457        .unreq my_alias    */
2458
2459 static void
2460 s_unreq (int a ATTRIBUTE_UNUSED)
2461 {
2462   char * name;
2463   char saved_char;
2464
2465   name = input_line_pointer;
2466
2467   while (*input_line_pointer != 0
2468          && *input_line_pointer != ' '
2469          && *input_line_pointer != '\n')
2470     ++input_line_pointer;
2471
2472   saved_char = *input_line_pointer;
2473   *input_line_pointer = 0;
2474
2475   if (!*name)
2476     as_bad (_("invalid syntax for .unreq directive"));
2477   else
2478     {
2479       struct reg_entry *reg = (struct reg_entry *) hash_find (arm_reg_hsh,
2480                                                               name);
2481
2482       if (!reg)
2483         as_bad (_("unknown register alias '%s'"), name);
2484       else if (reg->builtin)
2485         as_warn (_("ignoring attempt to use .unreq on fixed register name: '%s'"),
2486                  name);
2487       else
2488         {
2489           char * p;
2490           char * nbuf;
2491
2492           hash_delete (arm_reg_hsh, name, FALSE);
2493           free ((char *) reg->name);
2494           if (reg->neon)
2495             free (reg->neon);
2496           free (reg);
2497
2498           /* Also locate the all upper case and all lower case versions.
2499              Do not complain if we cannot find one or the other as it
2500              was probably deleted above.  */
2501
2502           nbuf = strdup (name);
2503           for (p = nbuf; *p; p++)
2504             *p = TOUPPER (*p);
2505           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2506           if (reg)
2507             {
2508               hash_delete (arm_reg_hsh, nbuf, FALSE);
2509               free ((char *) reg->name);
2510               if (reg->neon)
2511                 free (reg->neon);
2512               free (reg);
2513             }
2514
2515           for (p = nbuf; *p; p++)
2516             *p = TOLOWER (*p);
2517           reg = (struct reg_entry *) hash_find (arm_reg_hsh, nbuf);
2518           if (reg)
2519             {
2520               hash_delete (arm_reg_hsh, nbuf, FALSE);
2521               free ((char *) reg->name);
2522               if (reg->neon)
2523                 free (reg->neon);
2524               free (reg);
2525             }
2526
2527           free (nbuf);
2528         }
2529     }
2530
2531   *input_line_pointer = saved_char;
2532   demand_empty_rest_of_line ();
2533 }
2534
2535 /* Directives: Instruction set selection.  */
2536
2537 #ifdef OBJ_ELF
2538 /* This code is to handle mapping symbols as defined in the ARM ELF spec.
2539    (See "Mapping symbols", section 4.5.5, ARM AAELF version 1.0).
2540    Note that previously, $a and $t has type STT_FUNC (BSF_OBJECT flag),
2541    and $d has type STT_OBJECT (BSF_OBJECT flag). Now all three are untyped.  */
2542
2543 /* Create a new mapping symbol for the transition to STATE.  */
2544
2545 static void
2546 make_mapping_symbol (enum mstate state, valueT value, fragS *frag)
2547 {
2548   symbolS * symbolP;
2549   const char * symname;
2550   int type;
2551
2552   switch (state)
2553     {
2554     case MAP_DATA:
2555       symname = "$d";
2556       type = BSF_NO_FLAGS;
2557       break;
2558     case MAP_ARM:
2559       symname = "$a";
2560       type = BSF_NO_FLAGS;
2561       break;
2562     case MAP_THUMB:
2563       symname = "$t";
2564       type = BSF_NO_FLAGS;
2565       break;
2566     default:
2567       abort ();
2568     }
2569
2570   symbolP = symbol_new (symname, now_seg, value, frag);
2571   symbol_get_bfdsym (symbolP)->flags |= type | BSF_LOCAL;
2572
2573   switch (state)
2574     {
2575     case MAP_ARM:
2576       THUMB_SET_FUNC (symbolP, 0);
2577       ARM_SET_THUMB (symbolP, 0);
2578       ARM_SET_INTERWORK (symbolP, support_interwork);
2579       break;
2580
2581     case MAP_THUMB:
2582       THUMB_SET_FUNC (symbolP, 1);
2583       ARM_SET_THUMB (symbolP, 1);
2584       ARM_SET_INTERWORK (symbolP, support_interwork);
2585       break;
2586
2587     case MAP_DATA:
2588     default:
2589       break;
2590     }
2591
2592   /* Save the mapping symbols for future reference.  Also check that
2593      we do not place two mapping symbols at the same offset within a
2594      frag.  We'll handle overlap between frags in
2595      check_mapping_symbols.
2596
2597      If .fill or other data filling directive generates zero sized data,
2598      the mapping symbol for the following code will have the same value
2599      as the one generated for the data filling directive.  In this case,
2600      we replace the old symbol with the new one at the same address.  */
2601   if (value == 0)
2602     {
2603       if (frag->tc_frag_data.first_map != NULL)
2604         {
2605           know (S_GET_VALUE (frag->tc_frag_data.first_map) == 0);
2606           symbol_remove (frag->tc_frag_data.first_map, &symbol_rootP, &symbol_lastP);
2607         }
2608       frag->tc_frag_data.first_map = symbolP;
2609     }
2610   if (frag->tc_frag_data.last_map != NULL)
2611     {
2612       know (S_GET_VALUE (frag->tc_frag_data.last_map) <= S_GET_VALUE (symbolP));
2613       if (S_GET_VALUE (frag->tc_frag_data.last_map) == S_GET_VALUE (symbolP))
2614         symbol_remove (frag->tc_frag_data.last_map, &symbol_rootP, &symbol_lastP);
2615     }
2616   frag->tc_frag_data.last_map = symbolP;
2617 }
2618
2619 /* We must sometimes convert a region marked as code to data during
2620    code alignment, if an odd number of bytes have to be padded.  The
2621    code mapping symbol is pushed to an aligned address.  */
2622
2623 static void
2624 insert_data_mapping_symbol (enum mstate state,
2625                             valueT value, fragS *frag, offsetT bytes)
2626 {
2627   /* If there was already a mapping symbol, remove it.  */
2628   if (frag->tc_frag_data.last_map != NULL
2629       && S_GET_VALUE (frag->tc_frag_data.last_map) == frag->fr_address + value)
2630     {
2631       symbolS *symp = frag->tc_frag_data.last_map;
2632
2633       if (value == 0)
2634         {
2635           know (frag->tc_frag_data.first_map == symp);
2636           frag->tc_frag_data.first_map = NULL;
2637         }
2638       frag->tc_frag_data.last_map = NULL;
2639       symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2640     }
2641
2642   make_mapping_symbol (MAP_DATA, value, frag);
2643   make_mapping_symbol (state, value + bytes, frag);
2644 }
2645
2646 static void mapping_state_2 (enum mstate state, int max_chars);
2647
2648 /* Set the mapping state to STATE.  Only call this when about to
2649    emit some STATE bytes to the file.  */
2650
2651 #define TRANSITION(from, to) (mapstate == (from) && state == (to))
2652 void
2653 mapping_state (enum mstate state)
2654 {
2655   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2656
2657   if (mapstate == state)
2658     /* The mapping symbol has already been emitted.
2659        There is nothing else to do.  */
2660     return;
2661
2662   if (state == MAP_ARM || state == MAP_THUMB)
2663     /*  PR gas/12931
2664         All ARM instructions require 4-byte alignment.
2665         (Almost) all Thumb instructions require 2-byte alignment.
2666
2667         When emitting instructions into any section, mark the section
2668         appropriately.
2669
2670         Some Thumb instructions are alignment-sensitive modulo 4 bytes,
2671         but themselves require 2-byte alignment; this applies to some
2672         PC- relative forms.  However, these cases will invovle implicit
2673         literal pool generation or an explicit .align >=2, both of
2674         which will cause the section to me marked with sufficient
2675         alignment.  Thus, we don't handle those cases here.  */
2676     record_alignment (now_seg, state == MAP_ARM ? 2 : 1);
2677
2678   if (TRANSITION (MAP_UNDEFINED, MAP_DATA))
2679     /* This case will be evaluated later.  */
2680     return;
2681
2682   mapping_state_2 (state, 0);
2683 }
2684
2685 /* Same as mapping_state, but MAX_CHARS bytes have already been
2686    allocated.  Put the mapping symbol that far back.  */
2687
2688 static void
2689 mapping_state_2 (enum mstate state, int max_chars)
2690 {
2691   enum mstate mapstate = seg_info (now_seg)->tc_segment_info_data.mapstate;
2692
2693   if (!SEG_NORMAL (now_seg))
2694     return;
2695
2696   if (mapstate == state)
2697     /* The mapping symbol has already been emitted.
2698        There is nothing else to do.  */
2699     return;
2700
2701   if (TRANSITION (MAP_UNDEFINED, MAP_ARM)
2702           || TRANSITION (MAP_UNDEFINED, MAP_THUMB))
2703     {
2704       struct frag * const frag_first = seg_info (now_seg)->frchainP->frch_root;
2705       const int add_symbol = (frag_now != frag_first) || (frag_now_fix () > 0);
2706
2707       if (add_symbol)
2708         make_mapping_symbol (MAP_DATA, (valueT) 0, frag_first);
2709     }
2710
2711   seg_info (now_seg)->tc_segment_info_data.mapstate = state;
2712   make_mapping_symbol (state, (valueT) frag_now_fix () - max_chars, frag_now);
2713 }
2714 #undef TRANSITION
2715 #else
2716 #define mapping_state(x) ((void)0)
2717 #define mapping_state_2(x, y) ((void)0)
2718 #endif
2719
2720 /* Find the real, Thumb encoded start of a Thumb function.  */
2721
2722 #ifdef OBJ_COFF
2723 static symbolS *
2724 find_real_start (symbolS * symbolP)
2725 {
2726   char *       real_start;
2727   const char * name = S_GET_NAME (symbolP);
2728   symbolS *    new_target;
2729
2730   /* This definition must agree with the one in gcc/config/arm/thumb.c.  */
2731 #define STUB_NAME ".real_start_of"
2732
2733   if (name == NULL)
2734     abort ();
2735
2736   /* The compiler may generate BL instructions to local labels because
2737      it needs to perform a branch to a far away location. These labels
2738      do not have a corresponding ".real_start_of" label.  We check
2739      both for S_IS_LOCAL and for a leading dot, to give a way to bypass
2740      the ".real_start_of" convention for nonlocal branches.  */
2741   if (S_IS_LOCAL (symbolP) || name[0] == '.')
2742     return symbolP;
2743
2744   real_start = ACONCAT ((STUB_NAME, name, NULL));
2745   new_target = symbol_find (real_start);
2746
2747   if (new_target == NULL)
2748     {
2749       as_warn (_("Failed to find real start of function: %s\n"), name);
2750       new_target = symbolP;
2751     }
2752
2753   return new_target;
2754 }
2755 #endif
2756
2757 static void
2758 opcode_select (int width)
2759 {
2760   switch (width)
2761     {
2762     case 16:
2763       if (! thumb_mode)
2764         {
2765           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
2766             as_bad (_("selected processor does not support THUMB opcodes"));
2767
2768           thumb_mode = 1;
2769           /* No need to force the alignment, since we will have been
2770              coming from ARM mode, which is word-aligned.  */
2771           record_alignment (now_seg, 1);
2772         }
2773       break;
2774
2775     case 32:
2776       if (thumb_mode)
2777         {
2778           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
2779             as_bad (_("selected processor does not support ARM opcodes"));
2780
2781           thumb_mode = 0;
2782
2783           if (!need_pass_2)
2784             frag_align (2, 0, 0);
2785
2786           record_alignment (now_seg, 1);
2787         }
2788       break;
2789
2790     default:
2791       as_bad (_("invalid instruction size selected (%d)"), width);
2792     }
2793 }
2794
2795 static void
2796 s_arm (int ignore ATTRIBUTE_UNUSED)
2797 {
2798   opcode_select (32);
2799   demand_empty_rest_of_line ();
2800 }
2801
2802 static void
2803 s_thumb (int ignore ATTRIBUTE_UNUSED)
2804 {
2805   opcode_select (16);
2806   demand_empty_rest_of_line ();
2807 }
2808
2809 static void
2810 s_code (int unused ATTRIBUTE_UNUSED)
2811 {
2812   int temp;
2813
2814   temp = get_absolute_expression ();
2815   switch (temp)
2816     {
2817     case 16:
2818     case 32:
2819       opcode_select (temp);
2820       break;
2821
2822     default:
2823       as_bad (_("invalid operand to .code directive (%d) (expecting 16 or 32)"), temp);
2824     }
2825 }
2826
2827 static void
2828 s_force_thumb (int ignore ATTRIBUTE_UNUSED)
2829 {
2830   /* If we are not already in thumb mode go into it, EVEN if
2831      the target processor does not support thumb instructions.
2832      This is used by gcc/config/arm/lib1funcs.asm for example
2833      to compile interworking support functions even if the
2834      target processor should not support interworking.  */
2835   if (! thumb_mode)
2836     {
2837       thumb_mode = 2;
2838       record_alignment (now_seg, 1);
2839     }
2840
2841   demand_empty_rest_of_line ();
2842 }
2843
2844 static void
2845 s_thumb_func (int ignore ATTRIBUTE_UNUSED)
2846 {
2847   s_thumb (0);
2848
2849   /* The following label is the name/address of the start of a Thumb function.
2850      We need to know this for the interworking support.  */
2851   label_is_thumb_function_name = TRUE;
2852 }
2853
2854 /* Perform a .set directive, but also mark the alias as
2855    being a thumb function.  */
2856
2857 static void
2858 s_thumb_set (int equiv)
2859 {
2860   /* XXX the following is a duplicate of the code for s_set() in read.c
2861      We cannot just call that code as we need to get at the symbol that
2862      is created.  */
2863   char *    name;
2864   char      delim;
2865   char *    end_name;
2866   symbolS * symbolP;
2867
2868   /* Especial apologies for the random logic:
2869      This just grew, and could be parsed much more simply!
2870      Dean - in haste.  */
2871   delim     = get_symbol_name (& name);
2872   end_name  = input_line_pointer;
2873   (void) restore_line_pointer (delim);
2874
2875   if (*input_line_pointer != ',')
2876     {
2877       *end_name = 0;
2878       as_bad (_("expected comma after name \"%s\""), name);
2879       *end_name = delim;
2880       ignore_rest_of_line ();
2881       return;
2882     }
2883
2884   input_line_pointer++;
2885   *end_name = 0;
2886
2887   if (name[0] == '.' && name[1] == '\0')
2888     {
2889       /* XXX - this should not happen to .thumb_set.  */
2890       abort ();
2891     }
2892
2893   if ((symbolP = symbol_find (name)) == NULL
2894       && (symbolP = md_undefined_symbol (name)) == NULL)
2895     {
2896 #ifndef NO_LISTING
2897       /* When doing symbol listings, play games with dummy fragments living
2898          outside the normal fragment chain to record the file and line info
2899          for this symbol.  */
2900       if (listing & LISTING_SYMBOLS)
2901         {
2902           extern struct list_info_struct * listing_tail;
2903           fragS * dummy_frag = (fragS * ) xmalloc (sizeof (fragS));
2904
2905           memset (dummy_frag, 0, sizeof (fragS));
2906           dummy_frag->fr_type = rs_fill;
2907           dummy_frag->line = listing_tail;
2908           symbolP = symbol_new (name, undefined_section, 0, dummy_frag);
2909           dummy_frag->fr_symbol = symbolP;
2910         }
2911       else
2912 #endif
2913         symbolP = symbol_new (name, undefined_section, 0, &zero_address_frag);
2914
2915 #ifdef OBJ_COFF
2916       /* "set" symbols are local unless otherwise specified.  */
2917       SF_SET_LOCAL (symbolP);
2918 #endif /* OBJ_COFF  */
2919     }                           /* Make a new symbol.  */
2920
2921   symbol_table_insert (symbolP);
2922
2923   * end_name = delim;
2924
2925   if (equiv
2926       && S_IS_DEFINED (symbolP)
2927       && S_GET_SEGMENT (symbolP) != reg_section)
2928     as_bad (_("symbol `%s' already defined"), S_GET_NAME (symbolP));
2929
2930   pseudo_set (symbolP);
2931
2932   demand_empty_rest_of_line ();
2933
2934   /* XXX Now we come to the Thumb specific bit of code.  */
2935
2936   THUMB_SET_FUNC (symbolP, 1);
2937   ARM_SET_THUMB (symbolP, 1);
2938 #if defined OBJ_ELF || defined OBJ_COFF
2939   ARM_SET_INTERWORK (symbolP, support_interwork);
2940 #endif
2941 }
2942
2943 /* Directives: Mode selection.  */
2944
2945 /* .syntax [unified|divided] - choose the new unified syntax
2946    (same for Arm and Thumb encoding, modulo slight differences in what
2947    can be represented) or the old divergent syntax for each mode.  */
2948 static void
2949 s_syntax (int unused ATTRIBUTE_UNUSED)
2950 {
2951   char *name, delim;
2952
2953   delim = get_symbol_name (& name);
2954
2955   if (!strcasecmp (name, "unified"))
2956     unified_syntax = TRUE;
2957   else if (!strcasecmp (name, "divided"))
2958     unified_syntax = FALSE;
2959   else
2960     {
2961       as_bad (_("unrecognized syntax mode \"%s\""), name);
2962       return;
2963     }
2964   (void) restore_line_pointer (delim);
2965   demand_empty_rest_of_line ();
2966 }
2967
2968 /* Directives: sectioning and alignment.  */
2969
2970 static void
2971 s_bss (int ignore ATTRIBUTE_UNUSED)
2972 {
2973   /* We don't support putting frags in the BSS segment, we fake it by
2974      marking in_bss, then looking at s_skip for clues.  */
2975   subseg_set (bss_section, 0);
2976   demand_empty_rest_of_line ();
2977
2978 #ifdef md_elf_section_change_hook
2979   md_elf_section_change_hook ();
2980 #endif
2981 }
2982
2983 static void
2984 s_even (int ignore ATTRIBUTE_UNUSED)
2985 {
2986   /* Never make frag if expect extra pass.  */
2987   if (!need_pass_2)
2988     frag_align (1, 0, 0);
2989
2990   record_alignment (now_seg, 1);
2991
2992   demand_empty_rest_of_line ();
2993 }
2994
2995 /* Directives: CodeComposer Studio.  */
2996
2997 /*  .ref  (for CodeComposer Studio syntax only).  */
2998 static void
2999 s_ccs_ref (int unused ATTRIBUTE_UNUSED)
3000 {
3001   if (codecomposer_syntax)
3002     ignore_rest_of_line ();
3003   else
3004     as_bad (_(".ref pseudo-op only available with -mccs flag."));
3005 }
3006
3007 /*  If name is not NULL, then it is used for marking the beginning of a
3008     function, wherease if it is NULL then it means the function end.  */
3009 static void
3010 asmfunc_debug (const char * name)
3011 {
3012   static const char * last_name = NULL;
3013
3014   if (name != NULL)
3015     {
3016       gas_assert (last_name == NULL);
3017       last_name = name;
3018
3019       if (debug_type == DEBUG_STABS)
3020          stabs_generate_asm_func (name, name);
3021     }
3022   else
3023     {
3024       gas_assert (last_name != NULL);
3025
3026       if (debug_type == DEBUG_STABS)
3027         stabs_generate_asm_endfunc (last_name, last_name);
3028
3029       last_name = NULL;
3030     }
3031 }
3032
3033 static void
3034 s_ccs_asmfunc (int unused ATTRIBUTE_UNUSED)
3035 {
3036   if (codecomposer_syntax)
3037     {
3038       switch (asmfunc_state)
3039         {
3040         case OUTSIDE_ASMFUNC:
3041           asmfunc_state = WAITING_ASMFUNC_NAME;
3042           break;
3043
3044         case WAITING_ASMFUNC_NAME:
3045           as_bad (_(".asmfunc repeated."));
3046           break;
3047
3048         case WAITING_ENDASMFUNC:
3049           as_bad (_(".asmfunc without function."));
3050           break;
3051         }
3052       demand_empty_rest_of_line ();
3053     }
3054   else
3055     as_bad (_(".asmfunc pseudo-op only available with -mccs flag."));
3056 }
3057
3058 static void
3059 s_ccs_endasmfunc (int unused ATTRIBUTE_UNUSED)
3060 {
3061   if (codecomposer_syntax)
3062     {
3063       switch (asmfunc_state)
3064         {
3065         case OUTSIDE_ASMFUNC:
3066           as_bad (_(".endasmfunc without a .asmfunc."));
3067           break;
3068
3069         case WAITING_ASMFUNC_NAME:
3070           as_bad (_(".endasmfunc without function."));
3071           break;
3072
3073         case WAITING_ENDASMFUNC:
3074           asmfunc_state = OUTSIDE_ASMFUNC;
3075           asmfunc_debug (NULL);
3076           break;
3077         }
3078       demand_empty_rest_of_line ();
3079     }
3080   else
3081     as_bad (_(".endasmfunc pseudo-op only available with -mccs flag."));
3082 }
3083
3084 static void
3085 s_ccs_def (int name)
3086 {
3087   if (codecomposer_syntax)
3088     s_globl (name);
3089   else
3090     as_bad (_(".def pseudo-op only available with -mccs flag."));
3091 }
3092
3093 /* Directives: Literal pools.  */
3094
3095 static literal_pool *
3096 find_literal_pool (void)
3097 {
3098   literal_pool * pool;
3099
3100   for (pool = list_of_pools; pool != NULL; pool = pool->next)
3101     {
3102       if (pool->section == now_seg
3103           && pool->sub_section == now_subseg)
3104         break;
3105     }
3106
3107   return pool;
3108 }
3109
3110 static literal_pool *
3111 find_or_make_literal_pool (void)
3112 {
3113   /* Next literal pool ID number.  */
3114   static unsigned int latest_pool_num = 1;
3115   literal_pool *      pool;
3116
3117   pool = find_literal_pool ();
3118
3119   if (pool == NULL)
3120     {
3121       /* Create a new pool.  */
3122       pool = (literal_pool *) xmalloc (sizeof (* pool));
3123       if (! pool)
3124         return NULL;
3125
3126       pool->next_free_entry = 0;
3127       pool->section         = now_seg;
3128       pool->sub_section     = now_subseg;
3129       pool->next            = list_of_pools;
3130       pool->symbol          = NULL;
3131       pool->alignment       = 2;
3132
3133       /* Add it to the list.  */
3134       list_of_pools = pool;
3135     }
3136
3137   /* New pools, and emptied pools, will have a NULL symbol.  */
3138   if (pool->symbol == NULL)
3139     {
3140       pool->symbol = symbol_create (FAKE_LABEL_NAME, undefined_section,
3141                                     (valueT) 0, &zero_address_frag);
3142       pool->id = latest_pool_num ++;
3143     }
3144
3145   /* Done.  */
3146   return pool;
3147 }
3148
3149 /* Add the literal in the global 'inst'
3150    structure to the relevant literal pool.  */
3151
3152 static int
3153 add_to_lit_pool (unsigned int nbytes)
3154 {
3155 #define PADDING_SLOT 0x1
3156 #define LIT_ENTRY_SIZE_MASK 0xFF
3157   literal_pool * pool;
3158   unsigned int entry, pool_size = 0;
3159   bfd_boolean padding_slot_p = FALSE;
3160   unsigned imm1 = 0;
3161   unsigned imm2 = 0;
3162
3163   if (nbytes == 8)
3164     {
3165       imm1 = inst.operands[1].imm;
3166       imm2 = (inst.operands[1].regisimm ? inst.operands[1].reg
3167                : inst.reloc.exp.X_unsigned ? 0
3168                : ((bfd_int64_t) inst.operands[1].imm) >> 32);
3169       if (target_big_endian)
3170         {
3171           imm1 = imm2;
3172           imm2 = inst.operands[1].imm;
3173         }
3174     }
3175
3176   pool = find_or_make_literal_pool ();
3177
3178   /* Check if this literal value is already in the pool.  */
3179   for (entry = 0; entry < pool->next_free_entry; entry ++)
3180     {
3181       if (nbytes == 4)
3182         {
3183           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3184               && (inst.reloc.exp.X_op == O_constant)
3185               && (pool->literals[entry].X_add_number
3186                   == inst.reloc.exp.X_add_number)
3187               && (pool->literals[entry].X_md == nbytes)
3188               && (pool->literals[entry].X_unsigned
3189                   == inst.reloc.exp.X_unsigned))
3190             break;
3191
3192           if ((pool->literals[entry].X_op == inst.reloc.exp.X_op)
3193               && (inst.reloc.exp.X_op == O_symbol)
3194               && (pool->literals[entry].X_add_number
3195                   == inst.reloc.exp.X_add_number)
3196               && (pool->literals[entry].X_add_symbol
3197                   == inst.reloc.exp.X_add_symbol)
3198               && (pool->literals[entry].X_op_symbol
3199                   == inst.reloc.exp.X_op_symbol)
3200               && (pool->literals[entry].X_md == nbytes))
3201             break;
3202         }
3203       else if ((nbytes == 8)
3204                && !(pool_size & 0x7)
3205                && ((entry + 1) != pool->next_free_entry)
3206                && (pool->literals[entry].X_op == O_constant)
3207                && (pool->literals[entry].X_add_number == (offsetT) imm1)
3208                && (pool->literals[entry].X_unsigned
3209                    == inst.reloc.exp.X_unsigned)
3210                && (pool->literals[entry + 1].X_op == O_constant)
3211                && (pool->literals[entry + 1].X_add_number == (offsetT) imm2)
3212                && (pool->literals[entry + 1].X_unsigned
3213                    == inst.reloc.exp.X_unsigned))
3214         break;
3215
3216       padding_slot_p = ((pool->literals[entry].X_md >> 8) == PADDING_SLOT);
3217       if (padding_slot_p && (nbytes == 4))
3218         break;
3219
3220       pool_size += 4;
3221     }
3222
3223   /* Do we need to create a new entry?  */
3224   if (entry == pool->next_free_entry)
3225     {
3226       if (entry >= MAX_LITERAL_POOL_SIZE)
3227         {
3228           inst.error = _("literal pool overflow");
3229           return FAIL;
3230         }
3231
3232       if (nbytes == 8)
3233         {
3234           /* For 8-byte entries, we align to an 8-byte boundary,
3235              and split it into two 4-byte entries, because on 32-bit
3236              host, 8-byte constants are treated as big num, thus
3237              saved in "generic_bignum" which will be overwritten
3238              by later assignments.
3239
3240              We also need to make sure there is enough space for
3241              the split.
3242
3243              We also check to make sure the literal operand is a
3244              constant number.  */
3245           if (!(inst.reloc.exp.X_op == O_constant
3246                 || inst.reloc.exp.X_op == O_big))
3247             {
3248               inst.error = _("invalid type for literal pool");
3249               return FAIL;
3250             }
3251           else if (pool_size & 0x7)
3252             {
3253               if ((entry + 2) >= MAX_LITERAL_POOL_SIZE)
3254                 {
3255                   inst.error = _("literal pool overflow");
3256                   return FAIL;
3257                 }
3258
3259               pool->literals[entry] = inst.reloc.exp;
3260               pool->literals[entry].X_add_number = 0;
3261               pool->literals[entry++].X_md = (PADDING_SLOT << 8) | 4;
3262               pool->next_free_entry += 1;
3263               pool_size += 4;
3264             }
3265           else if ((entry + 1) >= MAX_LITERAL_POOL_SIZE)
3266             {
3267               inst.error = _("literal pool overflow");
3268               return FAIL;
3269             }
3270
3271           pool->literals[entry] = inst.reloc.exp;
3272           pool->literals[entry].X_op = O_constant;
3273           pool->literals[entry].X_add_number = imm1;
3274           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3275           pool->literals[entry++].X_md = 4;
3276           pool->literals[entry] = inst.reloc.exp;
3277           pool->literals[entry].X_op = O_constant;
3278           pool->literals[entry].X_add_number = imm2;
3279           pool->literals[entry].X_unsigned = inst.reloc.exp.X_unsigned;
3280           pool->literals[entry].X_md = 4;
3281           pool->alignment = 3;
3282           pool->next_free_entry += 1;
3283         }
3284       else
3285         {
3286           pool->literals[entry] = inst.reloc.exp;
3287           pool->literals[entry].X_md = 4;
3288         }
3289
3290 #ifdef OBJ_ELF
3291       /* PR ld/12974: Record the location of the first source line to reference
3292          this entry in the literal pool.  If it turns out during linking that the
3293          symbol does not exist we will be able to give an accurate line number for
3294          the (first use of the) missing reference.  */
3295       if (debug_type == DEBUG_DWARF2)
3296         dwarf2_where (pool->locs + entry);
3297 #endif
3298       pool->next_free_entry += 1;
3299     }
3300   else if (padding_slot_p)
3301     {
3302       pool->literals[entry] = inst.reloc.exp;
3303       pool->literals[entry].X_md = nbytes;
3304     }
3305
3306   inst.reloc.exp.X_op         = O_symbol;
3307   inst.reloc.exp.X_add_number = pool_size;
3308   inst.reloc.exp.X_add_symbol = pool->symbol;
3309
3310   return SUCCESS;
3311 }
3312
3313 bfd_boolean
3314 tc_start_label_without_colon (void)
3315 {
3316   bfd_boolean ret = TRUE;
3317
3318   if (codecomposer_syntax && asmfunc_state == WAITING_ASMFUNC_NAME)
3319     {
3320       const char *label = input_line_pointer;
3321
3322       while (!is_end_of_line[(int) label[-1]])
3323         --label;
3324
3325       if (*label == '.')
3326         {
3327           as_bad (_("Invalid label '%s'"), label);
3328           ret = FALSE;
3329         }
3330
3331       asmfunc_debug (label);
3332
3333       asmfunc_state = WAITING_ENDASMFUNC;
3334     }
3335
3336   return ret;
3337 }
3338
3339 /* Can't use symbol_new here, so have to create a symbol and then at
3340    a later date assign it a value. Thats what these functions do.  */
3341
3342 static void
3343 symbol_locate (symbolS *    symbolP,
3344                const char * name,       /* It is copied, the caller can modify.  */
3345                segT         segment,    /* Segment identifier (SEG_<something>).  */
3346                valueT       valu,       /* Symbol value.  */
3347                fragS *      frag)       /* Associated fragment.  */
3348 {
3349   size_t name_length;
3350   char * preserved_copy_of_name;
3351
3352   name_length = strlen (name) + 1;   /* +1 for \0.  */
3353   obstack_grow (&notes, name, name_length);
3354   preserved_copy_of_name = (char *) obstack_finish (&notes);
3355
3356 #ifdef tc_canonicalize_symbol_name
3357   preserved_copy_of_name =
3358     tc_canonicalize_symbol_name (preserved_copy_of_name);
3359 #endif
3360
3361   S_SET_NAME (symbolP, preserved_copy_of_name);
3362
3363   S_SET_SEGMENT (symbolP, segment);
3364   S_SET_VALUE (symbolP, valu);
3365   symbol_clear_list_pointers (symbolP);
3366
3367   symbol_set_frag (symbolP, frag);
3368
3369   /* Link to end of symbol chain.  */
3370   {
3371     extern int symbol_table_frozen;
3372
3373     if (symbol_table_frozen)
3374       abort ();
3375   }
3376
3377   symbol_append (symbolP, symbol_lastP, & symbol_rootP, & symbol_lastP);
3378
3379   obj_symbol_new_hook (symbolP);
3380
3381 #ifdef tc_symbol_new_hook
3382   tc_symbol_new_hook (symbolP);
3383 #endif
3384
3385 #ifdef DEBUG_SYMS
3386   verify_symbol_chain (symbol_rootP, symbol_lastP);
3387 #endif /* DEBUG_SYMS  */
3388 }
3389
3390 static void
3391 s_ltorg (int ignored ATTRIBUTE_UNUSED)
3392 {
3393   unsigned int entry;
3394   literal_pool * pool;
3395   char sym_name[20];
3396
3397   pool = find_literal_pool ();
3398   if (pool == NULL
3399       || pool->symbol == NULL
3400       || pool->next_free_entry == 0)
3401     return;
3402
3403   /* Align pool as you have word accesses.
3404      Only make a frag if we have to.  */
3405   if (!need_pass_2)
3406     frag_align (pool->alignment, 0, 0);
3407
3408   record_alignment (now_seg, 2);
3409
3410 #ifdef OBJ_ELF
3411   seg_info (now_seg)->tc_segment_info_data.mapstate = MAP_DATA;
3412   make_mapping_symbol (MAP_DATA, (valueT) frag_now_fix (), frag_now);
3413 #endif
3414   sprintf (sym_name, "$$lit_\002%x", pool->id);
3415
3416   symbol_locate (pool->symbol, sym_name, now_seg,
3417                  (valueT) frag_now_fix (), frag_now);
3418   symbol_table_insert (pool->symbol);
3419
3420   ARM_SET_THUMB (pool->symbol, thumb_mode);
3421
3422 #if defined OBJ_COFF || defined OBJ_ELF
3423   ARM_SET_INTERWORK (pool->symbol, support_interwork);
3424 #endif
3425
3426   for (entry = 0; entry < pool->next_free_entry; entry ++)
3427     {
3428 #ifdef OBJ_ELF
3429       if (debug_type == DEBUG_DWARF2)
3430         dwarf2_gen_line_info (frag_now_fix (), pool->locs + entry);
3431 #endif
3432       /* First output the expression in the instruction to the pool.  */
3433       emit_expr (&(pool->literals[entry]),
3434                  pool->literals[entry].X_md & LIT_ENTRY_SIZE_MASK);
3435     }
3436
3437   /* Mark the pool as empty.  */
3438   pool->next_free_entry = 0;
3439   pool->symbol = NULL;
3440 }
3441
3442 #ifdef OBJ_ELF
3443 /* Forward declarations for functions below, in the MD interface
3444    section.  */
3445 static void fix_new_arm (fragS *, int, short, expressionS *, int, int);
3446 static valueT create_unwind_entry (int);
3447 static void start_unwind_section (const segT, int);
3448 static void add_unwind_opcode (valueT, int);
3449 static void flush_pending_unwind (void);
3450
3451 /* Directives: Data.  */
3452
3453 static void
3454 s_arm_elf_cons (int nbytes)
3455 {
3456   expressionS exp;
3457
3458 #ifdef md_flush_pending_output
3459   md_flush_pending_output ();
3460 #endif
3461
3462   if (is_it_end_of_statement ())
3463     {
3464       demand_empty_rest_of_line ();
3465       return;
3466     }
3467
3468 #ifdef md_cons_align
3469   md_cons_align (nbytes);
3470 #endif
3471
3472   mapping_state (MAP_DATA);
3473   do
3474     {
3475       int reloc;
3476       char *base = input_line_pointer;
3477
3478       expression (& exp);
3479
3480       if (exp.X_op != O_symbol)
3481         emit_expr (&exp, (unsigned int) nbytes);
3482       else
3483         {
3484           char *before_reloc = input_line_pointer;
3485           reloc = parse_reloc (&input_line_pointer);
3486           if (reloc == -1)
3487             {
3488               as_bad (_("unrecognized relocation suffix"));
3489               ignore_rest_of_line ();
3490               return;
3491             }
3492           else if (reloc == BFD_RELOC_UNUSED)
3493             emit_expr (&exp, (unsigned int) nbytes);
3494           else
3495             {
3496               reloc_howto_type *howto = (reloc_howto_type *)
3497                   bfd_reloc_type_lookup (stdoutput,
3498                                          (bfd_reloc_code_real_type) reloc);
3499               int size = bfd_get_reloc_size (howto);
3500
3501               if (reloc == BFD_RELOC_ARM_PLT32)
3502                 {
3503                   as_bad (_("(plt) is only valid on branch targets"));
3504                   reloc = BFD_RELOC_UNUSED;
3505                   size = 0;
3506                 }
3507
3508               if (size > nbytes)
3509                 as_bad (_("%s relocations do not fit in %d bytes"),
3510                         howto->name, nbytes);
3511               else
3512                 {
3513                   /* We've parsed an expression stopping at O_symbol.
3514                      But there may be more expression left now that we
3515                      have parsed the relocation marker.  Parse it again.
3516                      XXX Surely there is a cleaner way to do this.  */
3517                   char *p = input_line_pointer;
3518                   int offset;
3519                   char *save_buf = (char *) alloca (input_line_pointer - base);
3520                   memcpy (save_buf, base, input_line_pointer - base);
3521                   memmove (base + (input_line_pointer - before_reloc),
3522                            base, before_reloc - base);
3523
3524                   input_line_pointer = base + (input_line_pointer-before_reloc);
3525                   expression (&exp);
3526                   memcpy (base, save_buf, p - base);
3527
3528                   offset = nbytes - size;
3529                   p = frag_more (nbytes);
3530                   memset (p, 0, nbytes);
3531                   fix_new_exp (frag_now, p - frag_now->fr_literal + offset,
3532                                size, &exp, 0, (enum bfd_reloc_code_real) reloc);
3533                 }
3534             }
3535         }
3536     }
3537   while (*input_line_pointer++ == ',');
3538
3539   /* Put terminator back into stream.  */
3540   input_line_pointer --;
3541   demand_empty_rest_of_line ();
3542 }
3543
3544 /* Emit an expression containing a 32-bit thumb instruction.
3545    Implementation based on put_thumb32_insn.  */
3546
3547 static void
3548 emit_thumb32_expr (expressionS * exp)
3549 {
3550   expressionS exp_high = *exp;
3551
3552   exp_high.X_add_number = (unsigned long)exp_high.X_add_number >> 16;
3553   emit_expr (& exp_high, (unsigned int) THUMB_SIZE);
3554   exp->X_add_number &= 0xffff;
3555   emit_expr (exp, (unsigned int) THUMB_SIZE);
3556 }
3557
3558 /*  Guess the instruction size based on the opcode.  */
3559
3560 static int
3561 thumb_insn_size (int opcode)
3562 {
3563   if ((unsigned int) opcode < 0xe800u)
3564     return 2;
3565   else if ((unsigned int) opcode >= 0xe8000000u)
3566     return 4;
3567   else
3568     return 0;
3569 }
3570
3571 static bfd_boolean
3572 emit_insn (expressionS *exp, int nbytes)
3573 {
3574   int size = 0;
3575
3576   if (exp->X_op == O_constant)
3577     {
3578       size = nbytes;
3579
3580       if (size == 0)
3581         size = thumb_insn_size (exp->X_add_number);
3582
3583       if (size != 0)
3584         {
3585           if (size == 2 && (unsigned int)exp->X_add_number > 0xffffu)
3586             {
3587               as_bad (_(".inst.n operand too big. "\
3588                         "Use .inst.w instead"));
3589               size = 0;
3590             }
3591           else
3592             {
3593               if (now_it.state == AUTOMATIC_IT_BLOCK)
3594                 set_it_insn_type_nonvoid (OUTSIDE_IT_INSN, 0);
3595               else
3596                 set_it_insn_type_nonvoid (NEUTRAL_IT_INSN, 0);
3597
3598               if (thumb_mode && (size > THUMB_SIZE) && !target_big_endian)
3599                 emit_thumb32_expr (exp);
3600               else
3601                 emit_expr (exp, (unsigned int) size);
3602
3603               it_fsm_post_encode ();
3604             }
3605         }
3606       else
3607         as_bad (_("cannot determine Thumb instruction size. "   \
3608                   "Use .inst.n/.inst.w instead"));
3609     }
3610   else
3611     as_bad (_("constant expression required"));
3612
3613   return (size != 0);
3614 }
3615
3616 /* Like s_arm_elf_cons but do not use md_cons_align and
3617    set the mapping state to MAP_ARM/MAP_THUMB.  */
3618
3619 static void
3620 s_arm_elf_inst (int nbytes)
3621 {
3622   if (is_it_end_of_statement ())
3623     {
3624       demand_empty_rest_of_line ();
3625       return;
3626     }
3627
3628   /* Calling mapping_state () here will not change ARM/THUMB,
3629      but will ensure not to be in DATA state.  */
3630
3631   if (thumb_mode)
3632     mapping_state (MAP_THUMB);
3633   else
3634     {
3635       if (nbytes != 0)
3636         {
3637           as_bad (_("width suffixes are invalid in ARM mode"));
3638           ignore_rest_of_line ();
3639           return;
3640         }
3641
3642       nbytes = 4;
3643
3644       mapping_state (MAP_ARM);
3645     }
3646
3647   do
3648     {
3649       expressionS exp;
3650
3651       expression (& exp);
3652
3653       if (! emit_insn (& exp, nbytes))
3654         {
3655           ignore_rest_of_line ();
3656           return;
3657         }
3658     }
3659   while (*input_line_pointer++ == ',');
3660
3661   /* Put terminator back into stream.  */
3662   input_line_pointer --;
3663   demand_empty_rest_of_line ();
3664 }
3665
3666 /* Parse a .rel31 directive.  */
3667
3668 static void
3669 s_arm_rel31 (int ignored ATTRIBUTE_UNUSED)
3670 {
3671   expressionS exp;
3672   char *p;
3673   valueT highbit;
3674
3675   highbit = 0;
3676   if (*input_line_pointer == '1')
3677     highbit = 0x80000000;
3678   else if (*input_line_pointer != '0')
3679     as_bad (_("expected 0 or 1"));
3680
3681   input_line_pointer++;
3682   if (*input_line_pointer != ',')
3683     as_bad (_("missing comma"));
3684   input_line_pointer++;
3685
3686 #ifdef md_flush_pending_output
3687   md_flush_pending_output ();
3688 #endif
3689
3690 #ifdef md_cons_align
3691   md_cons_align (4);
3692 #endif
3693
3694   mapping_state (MAP_DATA);
3695
3696   expression (&exp);
3697
3698   p = frag_more (4);
3699   md_number_to_chars (p, highbit, 4);
3700   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 1,
3701                BFD_RELOC_ARM_PREL31);
3702
3703   demand_empty_rest_of_line ();
3704 }
3705
3706 /* Directives: AEABI stack-unwind tables.  */
3707
3708 /* Parse an unwind_fnstart directive.  Simply records the current location.  */
3709
3710 static void
3711 s_arm_unwind_fnstart (int ignored ATTRIBUTE_UNUSED)
3712 {
3713   demand_empty_rest_of_line ();
3714   if (unwind.proc_start)
3715     {
3716       as_bad (_("duplicate .fnstart directive"));
3717       return;
3718     }
3719
3720   /* Mark the start of the function.  */
3721   unwind.proc_start = expr_build_dot ();
3722
3723   /* Reset the rest of the unwind info.  */
3724   unwind.opcode_count = 0;
3725   unwind.table_entry = NULL;
3726   unwind.personality_routine = NULL;
3727   unwind.personality_index = -1;
3728   unwind.frame_size = 0;
3729   unwind.fp_offset = 0;
3730   unwind.fp_reg = REG_SP;
3731   unwind.fp_used = 0;
3732   unwind.sp_restored = 0;
3733 }
3734
3735
3736 /* Parse a handlerdata directive.  Creates the exception handling table entry
3737    for the function.  */
3738
3739 static void
3740 s_arm_unwind_handlerdata (int ignored ATTRIBUTE_UNUSED)
3741 {
3742   demand_empty_rest_of_line ();
3743   if (!unwind.proc_start)
3744     as_bad (MISSING_FNSTART);
3745
3746   if (unwind.table_entry)
3747     as_bad (_("duplicate .handlerdata directive"));
3748
3749   create_unwind_entry (1);
3750 }
3751
3752 /* Parse an unwind_fnend directive.  Generates the index table entry.  */
3753
3754 static void
3755 s_arm_unwind_fnend (int ignored ATTRIBUTE_UNUSED)
3756 {
3757   long where;
3758   char *ptr;
3759   valueT val;
3760   unsigned int marked_pr_dependency;
3761
3762   demand_empty_rest_of_line ();
3763
3764   if (!unwind.proc_start)
3765     {
3766       as_bad (_(".fnend directive without .fnstart"));
3767       return;
3768     }
3769
3770   /* Add eh table entry.  */
3771   if (unwind.table_entry == NULL)
3772     val = create_unwind_entry (0);
3773   else
3774     val = 0;
3775
3776   /* Add index table entry.  This is two words.  */
3777   start_unwind_section (unwind.saved_seg, 1);
3778   frag_align (2, 0, 0);
3779   record_alignment (now_seg, 2);
3780
3781   ptr = frag_more (8);
3782   memset (ptr, 0, 8);
3783   where = frag_now_fix () - 8;
3784
3785   /* Self relative offset of the function start.  */
3786   fix_new (frag_now, where, 4, unwind.proc_start, 0, 1,
3787            BFD_RELOC_ARM_PREL31);
3788
3789   /* Indicate dependency on EHABI-defined personality routines to the
3790      linker, if it hasn't been done already.  */
3791   marked_pr_dependency
3792     = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
3793   if (unwind.personality_index >= 0 && unwind.personality_index < 3
3794       && !(marked_pr_dependency & (1 << unwind.personality_index)))
3795     {
3796       static const char *const name[] =
3797         {
3798           "__aeabi_unwind_cpp_pr0",
3799           "__aeabi_unwind_cpp_pr1",
3800           "__aeabi_unwind_cpp_pr2"
3801         };
3802       symbolS *pr = symbol_find_or_make (name[unwind.personality_index]);
3803       fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
3804       seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
3805         |= 1 << unwind.personality_index;
3806     }
3807
3808   if (val)
3809     /* Inline exception table entry.  */
3810     md_number_to_chars (ptr + 4, val, 4);
3811   else
3812     /* Self relative offset of the table entry.  */
3813     fix_new (frag_now, where + 4, 4, unwind.table_entry, 0, 1,
3814              BFD_RELOC_ARM_PREL31);
3815
3816   /* Restore the original section.  */
3817   subseg_set (unwind.saved_seg, unwind.saved_subseg);
3818
3819   unwind.proc_start = NULL;
3820 }
3821
3822
3823 /* Parse an unwind_cantunwind directive.  */
3824
3825 static void
3826 s_arm_unwind_cantunwind (int ignored ATTRIBUTE_UNUSED)
3827 {
3828   demand_empty_rest_of_line ();
3829   if (!unwind.proc_start)
3830     as_bad (MISSING_FNSTART);
3831
3832   if (unwind.personality_routine || unwind.personality_index != -1)
3833     as_bad (_("personality routine specified for cantunwind frame"));
3834
3835   unwind.personality_index = -2;
3836 }
3837
3838
3839 /* Parse a personalityindex directive.  */
3840
3841 static void
3842 s_arm_unwind_personalityindex (int ignored ATTRIBUTE_UNUSED)
3843 {
3844   expressionS exp;
3845
3846   if (!unwind.proc_start)
3847     as_bad (MISSING_FNSTART);
3848
3849   if (unwind.personality_routine || unwind.personality_index != -1)
3850     as_bad (_("duplicate .personalityindex directive"));
3851
3852   expression (&exp);
3853
3854   if (exp.X_op != O_constant
3855       || exp.X_add_number < 0 || exp.X_add_number > 15)
3856     {
3857       as_bad (_("bad personality routine number"));
3858       ignore_rest_of_line ();
3859       return;
3860     }
3861
3862   unwind.personality_index = exp.X_add_number;
3863
3864   demand_empty_rest_of_line ();
3865 }
3866
3867
3868 /* Parse a personality directive.  */
3869
3870 static void
3871 s_arm_unwind_personality (int ignored ATTRIBUTE_UNUSED)
3872 {
3873   char *name, *p, c;
3874
3875   if (!unwind.proc_start)
3876     as_bad (MISSING_FNSTART);
3877
3878   if (unwind.personality_routine || unwind.personality_index != -1)
3879     as_bad (_("duplicate .personality directive"));
3880
3881   c = get_symbol_name (& name);
3882   p = input_line_pointer;
3883   if (c == '"')
3884     ++ input_line_pointer;
3885   unwind.personality_routine = symbol_find_or_make (name);
3886   *p = c;
3887   demand_empty_rest_of_line ();
3888 }
3889
3890
3891 /* Parse a directive saving core registers.  */
3892
3893 static void
3894 s_arm_unwind_save_core (void)
3895 {
3896   valueT op;
3897   long range;
3898   int n;
3899
3900   range = parse_reg_list (&input_line_pointer);
3901   if (range == FAIL)
3902     {
3903       as_bad (_("expected register list"));
3904       ignore_rest_of_line ();
3905       return;
3906     }
3907
3908   demand_empty_rest_of_line ();
3909
3910   /* Turn .unwind_movsp ip followed by .unwind_save {..., ip, ...}
3911      into .unwind_save {..., sp...}.  We aren't bothered about the value of
3912      ip because it is clobbered by calls.  */
3913   if (unwind.sp_restored && unwind.fp_reg == 12
3914       && (range & 0x3000) == 0x1000)
3915     {
3916       unwind.opcode_count--;
3917       unwind.sp_restored = 0;
3918       range = (range | 0x2000) & ~0x1000;
3919       unwind.pending_offset = 0;
3920     }
3921
3922   /* Pop r4-r15.  */
3923   if (range & 0xfff0)
3924     {
3925       /* See if we can use the short opcodes.  These pop a block of up to 8
3926          registers starting with r4, plus maybe r14.  */
3927       for (n = 0; n < 8; n++)
3928         {
3929           /* Break at the first non-saved register.      */
3930           if ((range & (1 << (n + 4))) == 0)
3931             break;
3932         }
3933       /* See if there are any other bits set.  */
3934       if (n == 0 || (range & (0xfff0 << n) & 0xbff0) != 0)
3935         {
3936           /* Use the long form.  */
3937           op = 0x8000 | ((range >> 4) & 0xfff);
3938           add_unwind_opcode (op, 2);
3939         }
3940       else
3941         {
3942           /* Use the short form.  */
3943           if (range & 0x4000)
3944             op = 0xa8; /* Pop r14.      */
3945           else
3946             op = 0xa0; /* Do not pop r14.  */
3947           op |= (n - 1);
3948           add_unwind_opcode (op, 1);
3949         }
3950     }
3951
3952   /* Pop r0-r3.  */
3953   if (range & 0xf)
3954     {
3955       op = 0xb100 | (range & 0xf);
3956       add_unwind_opcode (op, 2);
3957     }
3958
3959   /* Record the number of bytes pushed.  */
3960   for (n = 0; n < 16; n++)
3961     {
3962       if (range & (1 << n))
3963         unwind.frame_size += 4;
3964     }
3965 }
3966
3967
3968 /* Parse a directive saving FPA registers.  */
3969
3970 static void
3971 s_arm_unwind_save_fpa (int reg)
3972 {
3973   expressionS exp;
3974   int num_regs;
3975   valueT op;
3976
3977   /* Get Number of registers to transfer.  */
3978   if (skip_past_comma (&input_line_pointer) != FAIL)
3979     expression (&exp);
3980   else
3981     exp.X_op = O_illegal;
3982
3983   if (exp.X_op != O_constant)
3984     {
3985       as_bad (_("expected , <constant>"));
3986       ignore_rest_of_line ();
3987       return;
3988     }
3989
3990   num_regs = exp.X_add_number;
3991
3992   if (num_regs < 1 || num_regs > 4)
3993     {
3994       as_bad (_("number of registers must be in the range [1:4]"));
3995       ignore_rest_of_line ();
3996       return;
3997     }
3998
3999   demand_empty_rest_of_line ();
4000
4001   if (reg == 4)
4002     {
4003       /* Short form.  */
4004       op = 0xb4 | (num_regs - 1);
4005       add_unwind_opcode (op, 1);
4006     }
4007   else
4008     {
4009       /* Long form.  */
4010       op = 0xc800 | (reg << 4) | (num_regs - 1);
4011       add_unwind_opcode (op, 2);
4012     }
4013   unwind.frame_size += num_regs * 12;
4014 }
4015
4016
4017 /* Parse a directive saving VFP registers for ARMv6 and above.  */
4018
4019 static void
4020 s_arm_unwind_save_vfp_armv6 (void)
4021 {
4022   int count;
4023   unsigned int start;
4024   valueT op;
4025   int num_vfpv3_regs = 0;
4026   int num_regs_below_16;
4027
4028   count = parse_vfp_reg_list (&input_line_pointer, &start, REGLIST_VFP_D);
4029   if (count == FAIL)
4030     {
4031       as_bad (_("expected register list"));
4032       ignore_rest_of_line ();
4033       return;
4034     }
4035
4036   demand_empty_rest_of_line ();
4037
4038   /* We always generate FSTMD/FLDMD-style unwinding opcodes (rather
4039      than FSTMX/FLDMX-style ones).  */
4040
4041   /* Generate opcode for (VFPv3) registers numbered in the range 16 .. 31.  */
4042   if (start >= 16)
4043     num_vfpv3_regs = count;
4044   else if (start + count > 16)
4045     num_vfpv3_regs = start + count - 16;
4046
4047   if (num_vfpv3_regs > 0)
4048     {
4049       int start_offset = start > 16 ? start - 16 : 0;
4050       op = 0xc800 | (start_offset << 4) | (num_vfpv3_regs - 1);
4051       add_unwind_opcode (op, 2);
4052     }
4053
4054   /* Generate opcode for registers numbered in the range 0 .. 15.  */
4055   num_regs_below_16 = num_vfpv3_regs > 0 ? 16 - (int) start : count;
4056   gas_assert (num_regs_below_16 + num_vfpv3_regs == count);
4057   if (num_regs_below_16 > 0)
4058     {
4059       op = 0xc900 | (start << 4) | (num_regs_below_16 - 1);
4060       add_unwind_opcode (op, 2);
4061     }
4062
4063   unwind.frame_size += count * 8;
4064 }
4065
4066
4067 /* Parse a directive saving VFP registers for pre-ARMv6.  */
4068
4069 static void
4070 s_arm_unwind_save_vfp (void)
4071 {
4072   int count;
4073   unsigned int reg;
4074   valueT op;
4075
4076   count = parse_vfp_reg_list (&input_line_pointer, &reg, REGLIST_VFP_D);
4077   if (count == FAIL)
4078     {
4079       as_bad (_("expected register list"));
4080       ignore_rest_of_line ();
4081       return;
4082     }
4083
4084   demand_empty_rest_of_line ();
4085
4086   if (reg == 8)
4087     {
4088       /* Short form.  */
4089       op = 0xb8 | (count - 1);
4090       add_unwind_opcode (op, 1);
4091     }
4092   else
4093     {
4094       /* Long form.  */
4095       op = 0xb300 | (reg << 4) | (count - 1);
4096       add_unwind_opcode (op, 2);
4097     }
4098   unwind.frame_size += count * 8 + 4;
4099 }
4100
4101
4102 /* Parse a directive saving iWMMXt data registers.  */
4103
4104 static void
4105 s_arm_unwind_save_mmxwr (void)
4106 {
4107   int reg;
4108   int hi_reg;
4109   int i;
4110   unsigned mask = 0;
4111   valueT op;
4112
4113   if (*input_line_pointer == '{')
4114     input_line_pointer++;
4115
4116   do
4117     {
4118       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4119
4120       if (reg == FAIL)
4121         {
4122           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4123           goto error;
4124         }
4125
4126       if (mask >> reg)
4127         as_tsktsk (_("register list not in ascending order"));
4128       mask |= 1 << reg;
4129
4130       if (*input_line_pointer == '-')
4131         {
4132           input_line_pointer++;
4133           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWR);
4134           if (hi_reg == FAIL)
4135             {
4136               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWR]));
4137               goto error;
4138             }
4139           else if (reg >= hi_reg)
4140             {
4141               as_bad (_("bad register range"));
4142               goto error;
4143             }
4144           for (; reg < hi_reg; reg++)
4145             mask |= 1 << reg;
4146         }
4147     }
4148   while (skip_past_comma (&input_line_pointer) != FAIL);
4149
4150   skip_past_char (&input_line_pointer, '}');
4151
4152   demand_empty_rest_of_line ();
4153
4154   /* Generate any deferred opcodes because we're going to be looking at
4155      the list.  */
4156   flush_pending_unwind ();
4157
4158   for (i = 0; i < 16; i++)
4159     {
4160       if (mask & (1 << i))
4161         unwind.frame_size += 8;
4162     }
4163
4164   /* Attempt to combine with a previous opcode.  We do this because gcc
4165      likes to output separate unwind directives for a single block of
4166      registers.  */
4167   if (unwind.opcode_count > 0)
4168     {
4169       i = unwind.opcodes[unwind.opcode_count - 1];
4170       if ((i & 0xf8) == 0xc0)
4171         {
4172           i &= 7;
4173           /* Only merge if the blocks are contiguous.  */
4174           if (i < 6)
4175             {
4176               if ((mask & 0xfe00) == (1 << 9))
4177                 {
4178                   mask |= ((1 << (i + 11)) - 1) & 0xfc00;
4179                   unwind.opcode_count--;
4180                 }
4181             }
4182           else if (i == 6 && unwind.opcode_count >= 2)
4183             {
4184               i = unwind.opcodes[unwind.opcode_count - 2];
4185               reg = i >> 4;
4186               i &= 0xf;
4187
4188               op = 0xffff << (reg - 1);
4189               if (reg > 0
4190                   && ((mask & op) == (1u << (reg - 1))))
4191                 {
4192                   op = (1 << (reg + i + 1)) - 1;
4193                   op &= ~((1 << reg) - 1);
4194                   mask |= op;
4195                   unwind.opcode_count -= 2;
4196                 }
4197             }
4198         }
4199     }
4200
4201   hi_reg = 15;
4202   /* We want to generate opcodes in the order the registers have been
4203      saved, ie. descending order.  */
4204   for (reg = 15; reg >= -1; reg--)
4205     {
4206       /* Save registers in blocks.  */
4207       if (reg < 0
4208           || !(mask & (1 << reg)))
4209         {
4210           /* We found an unsaved reg.  Generate opcodes to save the
4211              preceding block.   */
4212           if (reg != hi_reg)
4213             {
4214               if (reg == 9)
4215                 {
4216                   /* Short form.  */
4217                   op = 0xc0 | (hi_reg - 10);
4218                   add_unwind_opcode (op, 1);
4219                 }
4220               else
4221                 {
4222                   /* Long form.  */
4223                   op = 0xc600 | ((reg + 1) << 4) | ((hi_reg - reg) - 1);
4224                   add_unwind_opcode (op, 2);
4225                 }
4226             }
4227           hi_reg = reg - 1;
4228         }
4229     }
4230
4231   return;
4232 error:
4233   ignore_rest_of_line ();
4234 }
4235
4236 static void
4237 s_arm_unwind_save_mmxwcg (void)
4238 {
4239   int reg;
4240   int hi_reg;
4241   unsigned mask = 0;
4242   valueT op;
4243
4244   if (*input_line_pointer == '{')
4245     input_line_pointer++;
4246
4247   skip_whitespace (input_line_pointer);
4248
4249   do
4250     {
4251       reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4252
4253       if (reg == FAIL)
4254         {
4255           as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4256           goto error;
4257         }
4258
4259       reg -= 8;
4260       if (mask >> reg)
4261         as_tsktsk (_("register list not in ascending order"));
4262       mask |= 1 << reg;
4263
4264       if (*input_line_pointer == '-')
4265         {
4266           input_line_pointer++;
4267           hi_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_MMXWCG);
4268           if (hi_reg == FAIL)
4269             {
4270               as_bad ("%s", _(reg_expected_msgs[REG_TYPE_MMXWCG]));
4271               goto error;
4272             }
4273           else if (reg >= hi_reg)
4274             {
4275               as_bad (_("bad register range"));
4276               goto error;
4277             }
4278           for (; reg < hi_reg; reg++)
4279             mask |= 1 << reg;
4280         }
4281     }
4282   while (skip_past_comma (&input_line_pointer) != FAIL);
4283
4284   skip_past_char (&input_line_pointer, '}');
4285
4286   demand_empty_rest_of_line ();
4287
4288   /* Generate any deferred opcodes because we're going to be looking at
4289      the list.  */
4290   flush_pending_unwind ();
4291
4292   for (reg = 0; reg < 16; reg++)
4293     {
4294       if (mask & (1 << reg))
4295         unwind.frame_size += 4;
4296     }
4297   op = 0xc700 | mask;
4298   add_unwind_opcode (op, 2);
4299   return;
4300 error:
4301   ignore_rest_of_line ();
4302 }
4303
4304
4305 /* Parse an unwind_save directive.
4306    If the argument is non-zero, this is a .vsave directive.  */
4307
4308 static void
4309 s_arm_unwind_save (int arch_v6)
4310 {
4311   char *peek;
4312   struct reg_entry *reg;
4313   bfd_boolean had_brace = FALSE;
4314
4315   if (!unwind.proc_start)
4316     as_bad (MISSING_FNSTART);
4317
4318   /* Figure out what sort of save we have.  */
4319   peek = input_line_pointer;
4320
4321   if (*peek == '{')
4322     {
4323       had_brace = TRUE;
4324       peek++;
4325     }
4326
4327   reg = arm_reg_parse_multi (&peek);
4328
4329   if (!reg)
4330     {
4331       as_bad (_("register expected"));
4332       ignore_rest_of_line ();
4333       return;
4334     }
4335
4336   switch (reg->type)
4337     {
4338     case REG_TYPE_FN:
4339       if (had_brace)
4340         {
4341           as_bad (_("FPA .unwind_save does not take a register list"));
4342           ignore_rest_of_line ();
4343           return;
4344         }
4345       input_line_pointer = peek;
4346       s_arm_unwind_save_fpa (reg->number);
4347       return;
4348
4349     case REG_TYPE_RN:
4350       s_arm_unwind_save_core ();
4351       return;
4352
4353     case REG_TYPE_VFD:
4354       if (arch_v6)
4355         s_arm_unwind_save_vfp_armv6 ();
4356       else
4357         s_arm_unwind_save_vfp ();
4358       return;
4359
4360     case REG_TYPE_MMXWR:
4361       s_arm_unwind_save_mmxwr ();
4362       return;
4363
4364     case REG_TYPE_MMXWCG:
4365       s_arm_unwind_save_mmxwcg ();
4366       return;
4367
4368     default:
4369       as_bad (_(".unwind_save does not support this kind of register"));
4370       ignore_rest_of_line ();
4371     }
4372 }
4373
4374
4375 /* Parse an unwind_movsp directive.  */
4376
4377 static void
4378 s_arm_unwind_movsp (int ignored ATTRIBUTE_UNUSED)
4379 {
4380   int reg;
4381   valueT op;
4382   int offset;
4383
4384   if (!unwind.proc_start)
4385     as_bad (MISSING_FNSTART);
4386
4387   reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4388   if (reg == FAIL)
4389     {
4390       as_bad ("%s", _(reg_expected_msgs[REG_TYPE_RN]));
4391       ignore_rest_of_line ();
4392       return;
4393     }
4394
4395   /* Optional constant.  */
4396   if (skip_past_comma (&input_line_pointer) != FAIL)
4397     {
4398       if (immediate_for_directive (&offset) == FAIL)
4399         return;
4400     }
4401   else
4402     offset = 0;
4403
4404   demand_empty_rest_of_line ();
4405
4406   if (reg == REG_SP || reg == REG_PC)
4407     {
4408       as_bad (_("SP and PC not permitted in .unwind_movsp directive"));
4409       return;
4410     }
4411
4412   if (unwind.fp_reg != REG_SP)
4413     as_bad (_("unexpected .unwind_movsp directive"));
4414
4415   /* Generate opcode to restore the value.  */
4416   op = 0x90 | reg;
4417   add_unwind_opcode (op, 1);
4418
4419   /* Record the information for later.  */
4420   unwind.fp_reg = reg;
4421   unwind.fp_offset = unwind.frame_size - offset;
4422   unwind.sp_restored = 1;
4423 }
4424
4425 /* Parse an unwind_pad directive.  */
4426
4427 static void
4428 s_arm_unwind_pad (int ignored ATTRIBUTE_UNUSED)
4429 {
4430   int offset;
4431
4432   if (!unwind.proc_start)
4433     as_bad (MISSING_FNSTART);
4434
4435   if (immediate_for_directive (&offset) == FAIL)
4436     return;
4437
4438   if (offset & 3)
4439     {
4440       as_bad (_("stack increment must be multiple of 4"));
4441       ignore_rest_of_line ();
4442       return;
4443     }
4444
4445   /* Don't generate any opcodes, just record the details for later.  */
4446   unwind.frame_size += offset;
4447   unwind.pending_offset += offset;
4448
4449   demand_empty_rest_of_line ();
4450 }
4451
4452 /* Parse an unwind_setfp directive.  */
4453
4454 static void
4455 s_arm_unwind_setfp (int ignored ATTRIBUTE_UNUSED)
4456 {
4457   int sp_reg;
4458   int fp_reg;
4459   int offset;
4460
4461   if (!unwind.proc_start)
4462     as_bad (MISSING_FNSTART);
4463
4464   fp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4465   if (skip_past_comma (&input_line_pointer) == FAIL)
4466     sp_reg = FAIL;
4467   else
4468     sp_reg = arm_reg_parse (&input_line_pointer, REG_TYPE_RN);
4469
4470   if (fp_reg == FAIL || sp_reg == FAIL)
4471     {
4472       as_bad (_("expected <reg>, <reg>"));
4473       ignore_rest_of_line ();
4474       return;
4475     }
4476
4477   /* Optional constant.  */
4478   if (skip_past_comma (&input_line_pointer) != FAIL)
4479     {
4480       if (immediate_for_directive (&offset) == FAIL)
4481         return;
4482     }
4483   else
4484     offset = 0;
4485
4486   demand_empty_rest_of_line ();
4487
4488   if (sp_reg != REG_SP && sp_reg != unwind.fp_reg)
4489     {
4490       as_bad (_("register must be either sp or set by a previous"
4491                 "unwind_movsp directive"));
4492       return;
4493     }
4494
4495   /* Don't generate any opcodes, just record the information for later.  */
4496   unwind.fp_reg = fp_reg;
4497   unwind.fp_used = 1;
4498   if (sp_reg == REG_SP)
4499     unwind.fp_offset = unwind.frame_size - offset;
4500   else
4501     unwind.fp_offset -= offset;
4502 }
4503
4504 /* Parse an unwind_raw directive.  */
4505
4506 static void
4507 s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
4508 {
4509   expressionS exp;
4510   /* This is an arbitrary limit.         */
4511   unsigned char op[16];
4512   int count;
4513
4514   if (!unwind.proc_start)
4515     as_bad (MISSING_FNSTART);
4516
4517   expression (&exp);
4518   if (exp.X_op == O_constant
4519       && skip_past_comma (&input_line_pointer) != FAIL)
4520     {
4521       unwind.frame_size += exp.X_add_number;
4522       expression (&exp);
4523     }
4524   else
4525     exp.X_op = O_illegal;
4526
4527   if (exp.X_op != O_constant)
4528     {
4529       as_bad (_("expected <offset>, <opcode>"));
4530       ignore_rest_of_line ();
4531       return;
4532     }
4533
4534   count = 0;
4535
4536   /* Parse the opcode.  */
4537   for (;;)
4538     {
4539       if (count >= 16)
4540         {
4541           as_bad (_("unwind opcode too long"));
4542           ignore_rest_of_line ();
4543         }
4544       if (exp.X_op != O_constant || exp.X_add_number & ~0xff)
4545         {
4546           as_bad (_("invalid unwind opcode"));
4547           ignore_rest_of_line ();
4548           return;
4549         }
4550       op[count++] = exp.X_add_number;
4551
4552       /* Parse the next byte.  */
4553       if (skip_past_comma (&input_line_pointer) == FAIL)
4554         break;
4555
4556       expression (&exp);
4557     }
4558
4559   /* Add the opcode bytes in reverse order.  */
4560   while (count--)
4561     add_unwind_opcode (op[count], 1);
4562
4563   demand_empty_rest_of_line ();
4564 }
4565
4566
4567 /* Parse a .eabi_attribute directive.  */
4568
4569 static void
4570 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
4571 {
4572   int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4573
4574   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
4575     attributes_set_explicitly[tag] = 1;
4576 }
4577
4578 /* Emit a tls fix for the symbol.  */
4579
4580 static void
4581 s_arm_tls_descseq (int ignored ATTRIBUTE_UNUSED)
4582 {
4583   char *p;
4584   expressionS exp;
4585 #ifdef md_flush_pending_output
4586   md_flush_pending_output ();
4587 #endif
4588
4589 #ifdef md_cons_align
4590   md_cons_align (4);
4591 #endif
4592
4593   /* Since we're just labelling the code, there's no need to define a
4594      mapping symbol.  */
4595   expression (&exp);
4596   p = obstack_next_free (&frchain_now->frch_obstack);
4597   fix_new_arm (frag_now, p - frag_now->fr_literal, 4, &exp, 0,
4598                thumb_mode ? BFD_RELOC_ARM_THM_TLS_DESCSEQ
4599                : BFD_RELOC_ARM_TLS_DESCSEQ);
4600 }
4601 #endif /* OBJ_ELF */
4602
4603 static void s_arm_arch (int);
4604 static void s_arm_object_arch (int);
4605 static void s_arm_cpu (int);
4606 static void s_arm_fpu (int);
4607 static void s_arm_arch_extension (int);
4608
4609 #ifdef TE_PE
4610
4611 static void
4612 pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
4613 {
4614   expressionS exp;
4615
4616   do
4617     {
4618       expression (&exp);
4619       if (exp.X_op == O_symbol)
4620         exp.X_op = O_secrel;
4621
4622       emit_expr (&exp, 4);
4623     }
4624   while (*input_line_pointer++ == ',');
4625
4626   input_line_pointer--;
4627   demand_empty_rest_of_line ();
4628 }
4629 #endif /* TE_PE */
4630
4631 /* This table describes all the machine specific pseudo-ops the assembler
4632    has to support.  The fields are:
4633      pseudo-op name without dot
4634      function to call to execute this pseudo-op
4635      Integer arg to pass to the function.  */
4636
4637 const pseudo_typeS md_pseudo_table[] =
4638 {
4639   /* Never called because '.req' does not start a line.  */
4640   { "req",         s_req,         0 },
4641   /* Following two are likewise never called.  */
4642   { "dn",          s_dn,          0 },
4643   { "qn",          s_qn,          0 },
4644   { "unreq",       s_unreq,       0 },
4645   { "bss",         s_bss,         0 },
4646   { "align",       s_align_ptwo,  2 },
4647   { "arm",         s_arm,         0 },
4648   { "thumb",       s_thumb,       0 },
4649   { "code",        s_code,        0 },
4650   { "force_thumb", s_force_thumb, 0 },
4651   { "thumb_func",  s_thumb_func,  0 },
4652   { "thumb_set",   s_thumb_set,   0 },
4653   { "even",        s_even,        0 },
4654   { "ltorg",       s_ltorg,       0 },
4655   { "pool",        s_ltorg,       0 },
4656   { "syntax",      s_syntax,      0 },
4657   { "cpu",         s_arm_cpu,     0 },
4658   { "arch",        s_arm_arch,    0 },
4659   { "object_arch", s_arm_object_arch,   0 },
4660   { "fpu",         s_arm_fpu,     0 },
4661   { "arch_extension", s_arm_arch_extension, 0 },
4662 #ifdef OBJ_ELF
4663   { "word",             s_arm_elf_cons, 4 },
4664   { "long",             s_arm_elf_cons, 4 },
4665   { "inst.n",           s_arm_elf_inst, 2 },
4666   { "inst.w",           s_arm_elf_inst, 4 },
4667   { "inst",             s_arm_elf_inst, 0 },
4668   { "rel31",            s_arm_rel31,      0 },
4669   { "fnstart",          s_arm_unwind_fnstart,   0 },
4670   { "fnend",            s_arm_unwind_fnend,     0 },
4671   { "cantunwind",       s_arm_unwind_cantunwind, 0 },
4672   { "personality",      s_arm_unwind_personality, 0 },
4673   { "personalityindex", s_arm_unwind_personalityindex, 0 },
4674   { "handlerdata",      s_arm_unwind_handlerdata, 0 },
4675   { "save",             s_arm_unwind_save,      0 },
4676   { "vsave",            s_arm_unwind_save,      1 },
4677   { "movsp",            s_arm_unwind_movsp,     0 },
4678   { "pad",              s_arm_unwind_pad,       0 },
4679   { "setfp",            s_arm_unwind_setfp,     0 },
4680   { "unwind_raw",       s_arm_unwind_raw,       0 },
4681   { "eabi_attribute",   s_arm_eabi_attribute,   0 },
4682   { "tlsdescseq",       s_arm_tls_descseq,      0 },
4683 #else
4684   { "word",        cons, 4},
4685
4686   /* These are used for dwarf.  */
4687   {"2byte", cons, 2},
4688   {"4byte", cons, 4},
4689   {"8byte", cons, 8},
4690   /* These are used for dwarf2.  */
4691   { "file", (void (*) (int)) dwarf2_directive_file, 0 },
4692   { "loc",  dwarf2_directive_loc,  0 },
4693   { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
4694 #endif
4695   { "extend",      float_cons, 'x' },
4696   { "ldouble",     float_cons, 'x' },
4697   { "packed",      float_cons, 'p' },
4698 #ifdef TE_PE
4699   {"secrel32", pe_directive_secrel, 0},
4700 #endif
4701
4702   /* These are for compatibility with CodeComposer Studio.  */
4703   {"ref",          s_ccs_ref,        0},
4704   {"def",          s_ccs_def,        0},
4705   {"asmfunc",      s_ccs_asmfunc,    0},
4706   {"endasmfunc",   s_ccs_endasmfunc, 0},
4707
4708   { 0, 0, 0 }
4709 };
4710 \f
4711 /* Parser functions used exclusively in instruction operands.  */
4712
4713 /* Generic immediate-value read function for use in insn parsing.
4714    STR points to the beginning of the immediate (the leading #);
4715    VAL receives the value; if the value is outside [MIN, MAX]
4716    issue an error.  PREFIX_OPT is true if the immediate prefix is
4717    optional.  */
4718
4719 static int
4720 parse_immediate (char **str, int *val, int min, int max,
4721                  bfd_boolean prefix_opt)
4722 {
4723   expressionS exp;
4724   my_get_expression (&exp, str, prefix_opt ? GE_OPT_PREFIX : GE_IMM_PREFIX);
4725   if (exp.X_op != O_constant)
4726     {
4727       inst.error = _("constant expression required");
4728       return FAIL;
4729     }
4730
4731   if (exp.X_add_number < min || exp.X_add_number > max)
4732     {
4733       inst.error = _("immediate value out of range");
4734       return FAIL;
4735     }
4736
4737   *val = exp.X_add_number;
4738   return SUCCESS;
4739 }
4740
4741 /* Less-generic immediate-value read function with the possibility of loading a
4742    big (64-bit) immediate, as required by Neon VMOV, VMVN and logic immediate
4743    instructions. Puts the result directly in inst.operands[i].  */
4744
4745 static int
4746 parse_big_immediate (char **str, int i, expressionS *in_exp,
4747                      bfd_boolean allow_symbol_p)
4748 {
4749   expressionS exp;
4750   expressionS *exp_p = in_exp ? in_exp : &exp;
4751   char *ptr = *str;
4752
4753   my_get_expression (exp_p, &ptr, GE_OPT_PREFIX_BIG);
4754
4755   if (exp_p->X_op == O_constant)
4756     {
4757       inst.operands[i].imm = exp_p->X_add_number & 0xffffffff;
4758       /* If we're on a 64-bit host, then a 64-bit number can be returned using
4759          O_constant.  We have to be careful not to break compilation for
4760          32-bit X_add_number, though.  */
4761       if ((exp_p->X_add_number & ~(offsetT)(0xffffffffU)) != 0)
4762         {
4763           /* X >> 32 is illegal if sizeof (exp_p->X_add_number) == 4.  */
4764           inst.operands[i].reg = (((exp_p->X_add_number >> 16) >> 16)
4765                                   & 0xffffffff);
4766           inst.operands[i].regisimm = 1;
4767         }
4768     }
4769   else if (exp_p->X_op == O_big
4770            && LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 32)
4771     {
4772       unsigned parts = 32 / LITTLENUM_NUMBER_OF_BITS, j, idx = 0;
4773
4774       /* Bignums have their least significant bits in
4775          generic_bignum[0]. Make sure we put 32 bits in imm and
4776          32 bits in reg,  in a (hopefully) portable way.  */
4777       gas_assert (parts != 0);
4778
4779       /* Make sure that the number is not too big.
4780          PR 11972: Bignums can now be sign-extended to the
4781          size of a .octa so check that the out of range bits
4782          are all zero or all one.  */
4783       if (LITTLENUM_NUMBER_OF_BITS * exp_p->X_add_number > 64)
4784         {
4785           LITTLENUM_TYPE m = -1;
4786
4787           if (generic_bignum[parts * 2] != 0
4788               && generic_bignum[parts * 2] != m)
4789             return FAIL;
4790
4791           for (j = parts * 2 + 1; j < (unsigned) exp_p->X_add_number; j++)
4792             if (generic_bignum[j] != generic_bignum[j-1])
4793               return FAIL;
4794         }
4795
4796       inst.operands[i].imm = 0;
4797       for (j = 0; j < parts; j++, idx++)
4798         inst.operands[i].imm |= generic_bignum[idx]
4799                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4800       inst.operands[i].reg = 0;
4801       for (j = 0; j < parts; j++, idx++)
4802         inst.operands[i].reg |= generic_bignum[idx]
4803                                 << (LITTLENUM_NUMBER_OF_BITS * j);
4804       inst.operands[i].regisimm = 1;
4805     }
4806   else if (!(exp_p->X_op == O_symbol && allow_symbol_p))
4807     return FAIL;
4808
4809   *str = ptr;
4810
4811   return SUCCESS;
4812 }
4813
4814 /* Returns the pseudo-register number of an FPA immediate constant,
4815    or FAIL if there isn't a valid constant here.  */
4816
4817 static int
4818 parse_fpa_immediate (char ** str)
4819 {
4820   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4821   char *         save_in;
4822   expressionS    exp;
4823   int            i;
4824   int            j;
4825
4826   /* First try and match exact strings, this is to guarantee
4827      that some formats will work even for cross assembly.  */
4828
4829   for (i = 0; fp_const[i]; i++)
4830     {
4831       if (strncmp (*str, fp_const[i], strlen (fp_const[i])) == 0)
4832         {
4833           char *start = *str;
4834
4835           *str += strlen (fp_const[i]);
4836           if (is_end_of_line[(unsigned char) **str])
4837             return i + 8;
4838           *str = start;
4839         }
4840     }
4841
4842   /* Just because we didn't get a match doesn't mean that the constant
4843      isn't valid, just that it is in a format that we don't
4844      automatically recognize.  Try parsing it with the standard
4845      expression routines.  */
4846
4847   memset (words, 0, MAX_LITTLENUMS * sizeof (LITTLENUM_TYPE));
4848
4849   /* Look for a raw floating point number.  */
4850   if ((save_in = atof_ieee (*str, 'x', words)) != NULL
4851       && is_end_of_line[(unsigned char) *save_in])
4852     {
4853       for (i = 0; i < NUM_FLOAT_VALS; i++)
4854         {
4855           for (j = 0; j < MAX_LITTLENUMS; j++)
4856             {
4857               if (words[j] != fp_values[i][j])
4858                 break;
4859             }
4860
4861           if (j == MAX_LITTLENUMS)
4862             {
4863               *str = save_in;
4864               return i + 8;
4865             }
4866         }
4867     }
4868
4869   /* Try and parse a more complex expression, this will probably fail
4870      unless the code uses a floating point prefix (eg "0f").  */
4871   save_in = input_line_pointer;
4872   input_line_pointer = *str;
4873   if (expression (&exp) == absolute_section
4874       && exp.X_op == O_big
4875       && exp.X_add_number < 0)
4876     {
4877       /* FIXME: 5 = X_PRECISION, should be #define'd where we can use it.
4878          Ditto for 15.  */
4879 #define X_PRECISION 5
4880 #define E_PRECISION 15L
4881       if (gen_to_words (words, X_PRECISION, E_PRECISION) == 0)
4882         {
4883           for (i = 0; i < NUM_FLOAT_VALS; i++)
4884             {
4885               for (j = 0; j < MAX_LITTLENUMS; j++)
4886                 {
4887                   if (words[j] != fp_values[i][j])
4888                     break;
4889                 }
4890
4891               if (j == MAX_LITTLENUMS)
4892                 {
4893                   *str = input_line_pointer;
4894                   input_line_pointer = save_in;
4895                   return i + 8;
4896                 }
4897             }
4898         }
4899     }
4900
4901   *str = input_line_pointer;
4902   input_line_pointer = save_in;
4903   inst.error = _("invalid FPA immediate expression");
4904   return FAIL;
4905 }
4906
4907 /* Returns 1 if a number has "quarter-precision" float format
4908    0baBbbbbbc defgh000 00000000 00000000.  */
4909
4910 static int
4911 is_quarter_float (unsigned imm)
4912 {
4913   int bs = (imm & 0x20000000) ? 0x3e000000 : 0x40000000;
4914   return (imm & 0x7ffff) == 0 && ((imm & 0x7e000000) ^ bs) == 0;
4915 }
4916
4917
4918 /* Detect the presence of a floating point or integer zero constant,
4919    i.e. #0.0 or #0.  */
4920
4921 static bfd_boolean
4922 parse_ifimm_zero (char **in)
4923 {
4924   int error_code;
4925
4926   if (!is_immediate_prefix (**in))
4927     return FALSE;
4928
4929   ++*in;
4930
4931   /* Accept #0x0 as a synonym for #0.  */
4932   if (strncmp (*in, "0x", 2) == 0)
4933     {
4934       int val;
4935       if (parse_immediate (in, &val, 0, 0, TRUE) == FAIL)
4936         return FALSE;
4937       return TRUE;
4938     }
4939
4940   error_code = atof_generic (in, ".", EXP_CHARS,
4941                              &generic_floating_point_number);
4942
4943   if (!error_code
4944       && generic_floating_point_number.sign == '+'
4945       && (generic_floating_point_number.low
4946           > generic_floating_point_number.leader))
4947     return TRUE;
4948
4949   return FALSE;
4950 }
4951
4952 /* Parse an 8-bit "quarter-precision" floating point number of the form:
4953    0baBbbbbbc defgh000 00000000 00000000.
4954    The zero and minus-zero cases need special handling, since they can't be
4955    encoded in the "quarter-precision" float format, but can nonetheless be
4956    loaded as integer constants.  */
4957
4958 static unsigned
4959 parse_qfloat_immediate (char **ccp, int *immed)
4960 {
4961   char *str = *ccp;
4962   char *fpnum;
4963   LITTLENUM_TYPE words[MAX_LITTLENUMS];
4964   int found_fpchar = 0;
4965
4966   skip_past_char (&str, '#');
4967
4968   /* We must not accidentally parse an integer as a floating-point number. Make
4969      sure that the value we parse is not an integer by checking for special
4970      characters '.' or 'e'.
4971      FIXME: This is a horrible hack, but doing better is tricky because type
4972      information isn't in a very usable state at parse time.  */
4973   fpnum = str;
4974   skip_whitespace (fpnum);
4975
4976   if (strncmp (fpnum, "0x", 2) == 0)
4977     return FAIL;
4978   else
4979     {
4980       for (; *fpnum != '\0' && *fpnum != ' ' && *fpnum != '\n'; fpnum++)
4981         if (*fpnum == '.' || *fpnum == 'e' || *fpnum == 'E')
4982           {
4983             found_fpchar = 1;
4984             break;
4985           }
4986
4987       if (!found_fpchar)
4988         return FAIL;
4989     }
4990
4991   if ((str = atof_ieee (str, 's', words)) != NULL)
4992     {
4993       unsigned fpword = 0;
4994       int i;
4995
4996       /* Our FP word must be 32 bits (single-precision FP).  */
4997       for (i = 0; i < 32 / LITTLENUM_NUMBER_OF_BITS; i++)
4998         {
4999           fpword <<= LITTLENUM_NUMBER_OF_BITS;
5000           fpword |= words[i];
5001         }
5002
5003       if (is_quarter_float (fpword) || (fpword & 0x7fffffff) == 0)
5004         *immed = fpword;
5005       else
5006         return FAIL;
5007
5008       *ccp = str;
5009
5010       return SUCCESS;
5011     }
5012
5013   return FAIL;
5014 }
5015
5016 /* Shift operands.  */
5017 enum shift_kind
5018 {
5019   SHIFT_LSL, SHIFT_LSR, SHIFT_ASR, SHIFT_ROR, SHIFT_RRX
5020 };
5021
5022 struct asm_shift_name
5023 {
5024   const char      *name;
5025   enum shift_kind  kind;
5026 };
5027
5028 /* Third argument to parse_shift.  */
5029 enum parse_shift_mode
5030 {
5031   NO_SHIFT_RESTRICT,            /* Any kind of shift is accepted.  */
5032   SHIFT_IMMEDIATE,              /* Shift operand must be an immediate.  */
5033   SHIFT_LSL_OR_ASR_IMMEDIATE,   /* Shift must be LSL or ASR immediate.  */
5034   SHIFT_ASR_IMMEDIATE,          /* Shift must be ASR immediate.  */
5035   SHIFT_LSL_IMMEDIATE,          /* Shift must be LSL immediate.  */
5036 };
5037
5038 /* Parse a <shift> specifier on an ARM data processing instruction.
5039    This has three forms:
5040
5041      (LSL|LSR|ASL|ASR|ROR) Rs
5042      (LSL|LSR|ASL|ASR|ROR) #imm
5043      RRX
5044
5045    Note that ASL is assimilated to LSL in the instruction encoding, and
5046    RRX to ROR #0 (which cannot be written as such).  */
5047
5048 static int
5049 parse_shift (char **str, int i, enum parse_shift_mode mode)
5050 {
5051   const struct asm_shift_name *shift_name;
5052   enum shift_kind shift;
5053   char *s = *str;
5054   char *p = s;
5055   int reg;
5056
5057   for (p = *str; ISALPHA (*p); p++)
5058     ;
5059
5060   if (p == *str)
5061     {
5062       inst.error = _("shift expression expected");
5063       return FAIL;
5064     }
5065
5066   shift_name = (const struct asm_shift_name *) hash_find_n (arm_shift_hsh, *str,
5067                                                             p - *str);
5068
5069   if (shift_name == NULL)
5070     {
5071       inst.error = _("shift expression expected");
5072       return FAIL;
5073     }
5074
5075   shift = shift_name->kind;
5076
5077   switch (mode)
5078     {
5079     case NO_SHIFT_RESTRICT:
5080     case SHIFT_IMMEDIATE:   break;
5081
5082     case SHIFT_LSL_OR_ASR_IMMEDIATE:
5083       if (shift != SHIFT_LSL && shift != SHIFT_ASR)
5084         {
5085           inst.error = _("'LSL' or 'ASR' required");
5086           return FAIL;
5087         }
5088       break;
5089
5090     case SHIFT_LSL_IMMEDIATE:
5091       if (shift != SHIFT_LSL)
5092         {
5093           inst.error = _("'LSL' required");
5094           return FAIL;
5095         }
5096       break;
5097
5098     case SHIFT_ASR_IMMEDIATE:
5099       if (shift != SHIFT_ASR)
5100         {
5101           inst.error = _("'ASR' required");
5102           return FAIL;
5103         }
5104       break;
5105
5106     default: abort ();
5107     }
5108
5109   if (shift != SHIFT_RRX)
5110     {
5111       /* Whitespace can appear here if the next thing is a bare digit.  */
5112       skip_whitespace (p);
5113
5114       if (mode == NO_SHIFT_RESTRICT
5115           && (reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5116         {
5117           inst.operands[i].imm = reg;
5118           inst.operands[i].immisreg = 1;
5119         }
5120       else if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5121         return FAIL;
5122     }
5123   inst.operands[i].shift_kind = shift;
5124   inst.operands[i].shifted = 1;
5125   *str = p;
5126   return SUCCESS;
5127 }
5128
5129 /* Parse a <shifter_operand> for an ARM data processing instruction:
5130
5131       #<immediate>
5132       #<immediate>, <rotate>
5133       <Rm>
5134       <Rm>, <shift>
5135
5136    where <shift> is defined by parse_shift above, and <rotate> is a
5137    multiple of 2 between 0 and 30.  Validation of immediate operands
5138    is deferred to md_apply_fix.  */
5139
5140 static int
5141 parse_shifter_operand (char **str, int i)
5142 {
5143   int value;
5144   expressionS exp;
5145
5146   if ((value = arm_reg_parse (str, REG_TYPE_RN)) != FAIL)
5147     {
5148       inst.operands[i].reg = value;
5149       inst.operands[i].isreg = 1;
5150
5151       /* parse_shift will override this if appropriate */
5152       inst.reloc.exp.X_op = O_constant;
5153       inst.reloc.exp.X_add_number = 0;
5154
5155       if (skip_past_comma (str) == FAIL)
5156         return SUCCESS;
5157
5158       /* Shift operation on register.  */
5159       return parse_shift (str, i, NO_SHIFT_RESTRICT);
5160     }
5161
5162   if (my_get_expression (&inst.reloc.exp, str, GE_IMM_PREFIX))
5163     return FAIL;
5164
5165   if (skip_past_comma (str) == SUCCESS)
5166     {
5167       /* #x, y -- ie explicit rotation by Y.  */
5168       if (my_get_expression (&exp, str, GE_NO_PREFIX))
5169         return FAIL;
5170
5171       if (exp.X_op != O_constant || inst.reloc.exp.X_op != O_constant)
5172         {
5173           inst.error = _("constant expression expected");
5174           return FAIL;
5175         }
5176
5177       value = exp.X_add_number;
5178       if (value < 0 || value > 30 || value % 2 != 0)
5179         {
5180           inst.error = _("invalid rotation");
5181           return FAIL;
5182         }
5183       if (inst.reloc.exp.X_add_number < 0 || inst.reloc.exp.X_add_number > 255)
5184         {
5185           inst.error = _("invalid constant");
5186           return FAIL;
5187         }
5188
5189       /* Encode as specified.  */
5190       inst.operands[i].imm = inst.reloc.exp.X_add_number | value << 7;
5191       return SUCCESS;
5192     }
5193
5194   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
5195   inst.reloc.pc_rel = 0;
5196   return SUCCESS;
5197 }
5198
5199 /* Group relocation information.  Each entry in the table contains the
5200    textual name of the relocation as may appear in assembler source
5201    and must end with a colon.
5202    Along with this textual name are the relocation codes to be used if
5203    the corresponding instruction is an ALU instruction (ADD or SUB only),
5204    an LDR, an LDRS, or an LDC.  */
5205
5206 struct group_reloc_table_entry
5207 {
5208   const char *name;
5209   int alu_code;
5210   int ldr_code;
5211   int ldrs_code;
5212   int ldc_code;
5213 };
5214
5215 typedef enum
5216 {
5217   /* Varieties of non-ALU group relocation.  */
5218
5219   GROUP_LDR,
5220   GROUP_LDRS,
5221   GROUP_LDC
5222 } group_reloc_type;
5223
5224 static struct group_reloc_table_entry group_reloc_table[] =
5225   { /* Program counter relative: */
5226     { "pc_g0_nc",
5227       BFD_RELOC_ARM_ALU_PC_G0_NC,       /* ALU */
5228       0,                                /* LDR */
5229       0,                                /* LDRS */
5230       0 },                              /* LDC */
5231     { "pc_g0",
5232       BFD_RELOC_ARM_ALU_PC_G0,          /* ALU */
5233       BFD_RELOC_ARM_LDR_PC_G0,          /* LDR */
5234       BFD_RELOC_ARM_LDRS_PC_G0,         /* LDRS */
5235       BFD_RELOC_ARM_LDC_PC_G0 },        /* LDC */
5236     { "pc_g1_nc",
5237       BFD_RELOC_ARM_ALU_PC_G1_NC,       /* ALU */
5238       0,                                /* LDR */
5239       0,                                /* LDRS */
5240       0 },                              /* LDC */
5241     { "pc_g1",
5242       BFD_RELOC_ARM_ALU_PC_G1,          /* ALU */
5243       BFD_RELOC_ARM_LDR_PC_G1,          /* LDR */
5244       BFD_RELOC_ARM_LDRS_PC_G1,         /* LDRS */
5245       BFD_RELOC_ARM_LDC_PC_G1 },        /* LDC */
5246     { "pc_g2",
5247       BFD_RELOC_ARM_ALU_PC_G2,          /* ALU */
5248       BFD_RELOC_ARM_LDR_PC_G2,          /* LDR */
5249       BFD_RELOC_ARM_LDRS_PC_G2,         /* LDRS */
5250       BFD_RELOC_ARM_LDC_PC_G2 },        /* LDC */
5251     /* Section base relative */
5252     { "sb_g0_nc",
5253       BFD_RELOC_ARM_ALU_SB_G0_NC,       /* ALU */
5254       0,                                /* LDR */
5255       0,                                /* LDRS */
5256       0 },                              /* LDC */
5257     { "sb_g0",
5258       BFD_RELOC_ARM_ALU_SB_G0,          /* ALU */
5259       BFD_RELOC_ARM_LDR_SB_G0,          /* LDR */
5260       BFD_RELOC_ARM_LDRS_SB_G0,         /* LDRS */
5261       BFD_RELOC_ARM_LDC_SB_G0 },        /* LDC */
5262     { "sb_g1_nc",
5263       BFD_RELOC_ARM_ALU_SB_G1_NC,       /* ALU */
5264       0,                                /* LDR */
5265       0,                                /* LDRS */
5266       0 },                              /* LDC */
5267     { "sb_g1",
5268       BFD_RELOC_ARM_ALU_SB_G1,          /* ALU */
5269       BFD_RELOC_ARM_LDR_SB_G1,          /* LDR */
5270       BFD_RELOC_ARM_LDRS_SB_G1,         /* LDRS */
5271       BFD_RELOC_ARM_LDC_SB_G1 },        /* LDC */
5272     { "sb_g2",
5273       BFD_RELOC_ARM_ALU_SB_G2,          /* ALU */
5274       BFD_RELOC_ARM_LDR_SB_G2,          /* LDR */
5275       BFD_RELOC_ARM_LDRS_SB_G2,         /* LDRS */
5276       BFD_RELOC_ARM_LDC_SB_G2 },        /* LDC */
5277     /* Absolute thumb alu relocations.  */
5278     { "lower0_7",
5279       BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC,/* ALU.  */
5280       0,                                /* LDR.  */
5281       0,                                /* LDRS.  */
5282       0 },                              /* LDC.  */
5283     { "lower8_15",
5284       BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC,/* ALU.  */
5285       0,                                /* LDR.  */
5286       0,                                /* LDRS.  */
5287       0 },                              /* LDC.  */
5288     { "upper0_7",
5289       BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC,/* ALU.  */
5290       0,                                /* LDR.  */
5291       0,                                /* LDRS.  */
5292       0 },                              /* LDC.  */
5293     { "upper8_15",
5294       BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC,/* ALU.  */
5295       0,                                /* LDR.  */
5296       0,                                /* LDRS.  */
5297       0 } };                            /* LDC.  */
5298
5299 /* Given the address of a pointer pointing to the textual name of a group
5300    relocation as may appear in assembler source, attempt to find its details
5301    in group_reloc_table.  The pointer will be updated to the character after
5302    the trailing colon.  On failure, FAIL will be returned; SUCCESS
5303    otherwise.  On success, *entry will be updated to point at the relevant
5304    group_reloc_table entry. */
5305
5306 static int
5307 find_group_reloc_table_entry (char **str, struct group_reloc_table_entry **out)
5308 {
5309   unsigned int i;
5310   for (i = 0; i < ARRAY_SIZE (group_reloc_table); i++)
5311     {
5312       int length = strlen (group_reloc_table[i].name);
5313
5314       if (strncasecmp (group_reloc_table[i].name, *str, length) == 0
5315           && (*str)[length] == ':')
5316         {
5317           *out = &group_reloc_table[i];
5318           *str += (length + 1);
5319           return SUCCESS;
5320         }
5321     }
5322
5323   return FAIL;
5324 }
5325
5326 /* Parse a <shifter_operand> for an ARM data processing instruction
5327    (as for parse_shifter_operand) where group relocations are allowed:
5328
5329       #<immediate>
5330       #<immediate>, <rotate>
5331       #:<group_reloc>:<expression>
5332       <Rm>
5333       <Rm>, <shift>
5334
5335    where <group_reloc> is one of the strings defined in group_reloc_table.
5336    The hashes are optional.
5337
5338    Everything else is as for parse_shifter_operand.  */
5339
5340 static parse_operand_result
5341 parse_shifter_operand_group_reloc (char **str, int i)
5342 {
5343   /* Determine if we have the sequence of characters #: or just :
5344      coming next.  If we do, then we check for a group relocation.
5345      If we don't, punt the whole lot to parse_shifter_operand.  */
5346
5347   if (((*str)[0] == '#' && (*str)[1] == ':')
5348       || (*str)[0] == ':')
5349     {
5350       struct group_reloc_table_entry *entry;
5351
5352       if ((*str)[0] == '#')
5353         (*str) += 2;
5354       else
5355         (*str)++;
5356
5357       /* Try to parse a group relocation.  Anything else is an error.  */
5358       if (find_group_reloc_table_entry (str, &entry) == FAIL)
5359         {
5360           inst.error = _("unknown group relocation");
5361           return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5362         }
5363
5364       /* We now have the group relocation table entry corresponding to
5365          the name in the assembler source.  Next, we parse the expression.  */
5366       if (my_get_expression (&inst.reloc.exp, str, GE_NO_PREFIX))
5367         return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5368
5369       /* Record the relocation type (always the ALU variant here).  */
5370       inst.reloc.type = (bfd_reloc_code_real_type) entry->alu_code;
5371       gas_assert (inst.reloc.type != 0);
5372
5373       return PARSE_OPERAND_SUCCESS;
5374     }
5375   else
5376     return parse_shifter_operand (str, i) == SUCCESS
5377            ? PARSE_OPERAND_SUCCESS : PARSE_OPERAND_FAIL;
5378
5379   /* Never reached.  */
5380 }
5381
5382 /* Parse a Neon alignment expression.  Information is written to
5383    inst.operands[i].  We assume the initial ':' has been skipped.
5384
5385    align        .imm = align << 8, .immisalign=1, .preind=0  */
5386 static parse_operand_result
5387 parse_neon_alignment (char **str, int i)
5388 {
5389   char *p = *str;
5390   expressionS exp;
5391
5392   my_get_expression (&exp, &p, GE_NO_PREFIX);
5393
5394   if (exp.X_op != O_constant)
5395     {
5396       inst.error = _("alignment must be constant");
5397       return PARSE_OPERAND_FAIL;
5398     }
5399
5400   inst.operands[i].imm = exp.X_add_number << 8;
5401   inst.operands[i].immisalign = 1;
5402   /* Alignments are not pre-indexes.  */
5403   inst.operands[i].preind = 0;
5404
5405   *str = p;
5406   return PARSE_OPERAND_SUCCESS;
5407 }
5408
5409 /* Parse all forms of an ARM address expression.  Information is written
5410    to inst.operands[i] and/or inst.reloc.
5411
5412    Preindexed addressing (.preind=1):
5413
5414    [Rn, #offset]       .reg=Rn .reloc.exp=offset
5415    [Rn, +/-Rm]         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5416    [Rn, +/-Rm, shift]  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5417                        .shift_kind=shift .reloc.exp=shift_imm
5418
5419    These three may have a trailing ! which causes .writeback to be set also.
5420
5421    Postindexed addressing (.postind=1, .writeback=1):
5422
5423    [Rn], #offset       .reg=Rn .reloc.exp=offset
5424    [Rn], +/-Rm         .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5425    [Rn], +/-Rm, shift  .reg=Rn .imm=Rm .immisreg=1 .negative=0/1
5426                        .shift_kind=shift .reloc.exp=shift_imm
5427
5428    Unindexed addressing (.preind=0, .postind=0):
5429
5430    [Rn], {option}      .reg=Rn .imm=option .immisreg=0
5431
5432    Other:
5433
5434    [Rn]{!}             shorthand for [Rn,#0]{!}
5435    =immediate          .isreg=0 .reloc.exp=immediate
5436    label               .reg=PC .reloc.pc_rel=1 .reloc.exp=label
5437
5438   It is the caller's responsibility to check for addressing modes not
5439   supported by the instruction, and to set inst.reloc.type.  */
5440
5441 static parse_operand_result
5442 parse_address_main (char **str, int i, int group_relocations,
5443                     group_reloc_type group_type)
5444 {
5445   char *p = *str;
5446   int reg;
5447
5448   if (skip_past_char (&p, '[') == FAIL)
5449     {
5450       if (skip_past_char (&p, '=') == FAIL)
5451         {
5452           /* Bare address - translate to PC-relative offset.  */
5453           inst.reloc.pc_rel = 1;
5454           inst.operands[i].reg = REG_PC;
5455           inst.operands[i].isreg = 1;
5456           inst.operands[i].preind = 1;
5457
5458           if (my_get_expression (&inst.reloc.exp, &p, GE_OPT_PREFIX_BIG))
5459             return PARSE_OPERAND_FAIL;
5460         }
5461       else if (parse_big_immediate (&p, i, &inst.reloc.exp,
5462                                     /*allow_symbol_p=*/TRUE))
5463         return PARSE_OPERAND_FAIL;
5464
5465       *str = p;
5466       return PARSE_OPERAND_SUCCESS;
5467     }
5468
5469   /* PR gas/14887: Allow for whitespace after the opening bracket.  */
5470   skip_whitespace (p);
5471
5472   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
5473     {
5474       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
5475       return PARSE_OPERAND_FAIL;
5476     }
5477   inst.operands[i].reg = reg;
5478   inst.operands[i].isreg = 1;
5479
5480   if (skip_past_comma (&p) == SUCCESS)
5481     {
5482       inst.operands[i].preind = 1;
5483
5484       if (*p == '+') p++;
5485       else if (*p == '-') p++, inst.operands[i].negative = 1;
5486
5487       if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5488         {
5489           inst.operands[i].imm = reg;
5490           inst.operands[i].immisreg = 1;
5491
5492           if (skip_past_comma (&p) == SUCCESS)
5493             if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5494               return PARSE_OPERAND_FAIL;
5495         }
5496       else if (skip_past_char (&p, ':') == SUCCESS)
5497         {
5498           /* FIXME: '@' should be used here, but it's filtered out by generic
5499              code before we get to see it here. This may be subject to
5500              change.  */
5501           parse_operand_result result = parse_neon_alignment (&p, i);
5502
5503           if (result != PARSE_OPERAND_SUCCESS)
5504             return result;
5505         }
5506       else
5507         {
5508           if (inst.operands[i].negative)
5509             {
5510               inst.operands[i].negative = 0;
5511               p--;
5512             }
5513
5514           if (group_relocations
5515               && ((*p == '#' && *(p + 1) == ':') || *p == ':'))
5516             {
5517               struct group_reloc_table_entry *entry;
5518
5519               /* Skip over the #: or : sequence.  */
5520               if (*p == '#')
5521                 p += 2;
5522               else
5523                 p++;
5524
5525               /* Try to parse a group relocation.  Anything else is an
5526                  error.  */
5527               if (find_group_reloc_table_entry (&p, &entry) == FAIL)
5528                 {
5529                   inst.error = _("unknown group relocation");
5530                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5531                 }
5532
5533               /* We now have the group relocation table entry corresponding to
5534                  the name in the assembler source.  Next, we parse the
5535                  expression.  */
5536               if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5537                 return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5538
5539               /* Record the relocation type.  */
5540               switch (group_type)
5541                 {
5542                   case GROUP_LDR:
5543                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldr_code;
5544                     break;
5545
5546                   case GROUP_LDRS:
5547                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldrs_code;
5548                     break;
5549
5550                   case GROUP_LDC:
5551                     inst.reloc.type = (bfd_reloc_code_real_type) entry->ldc_code;
5552                     break;
5553
5554                   default:
5555                     gas_assert (0);
5556                 }
5557
5558               if (inst.reloc.type == 0)
5559                 {
5560                   inst.error = _("this group relocation is not allowed on this instruction");
5561                   return PARSE_OPERAND_FAIL_NO_BACKTRACK;
5562                 }
5563             }
5564           else
5565             {
5566               char *q = p;
5567               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5568                 return PARSE_OPERAND_FAIL;
5569               /* If the offset is 0, find out if it's a +0 or -0.  */
5570               if (inst.reloc.exp.X_op == O_constant
5571                   && inst.reloc.exp.X_add_number == 0)
5572                 {
5573                   skip_whitespace (q);
5574                   if (*q == '#')
5575                     {
5576                       q++;
5577                       skip_whitespace (q);
5578                     }
5579                   if (*q == '-')
5580                     inst.operands[i].negative = 1;
5581                 }
5582             }
5583         }
5584     }
5585   else if (skip_past_char (&p, ':') == SUCCESS)
5586     {
5587       /* FIXME: '@' should be used here, but it's filtered out by generic code
5588          before we get to see it here. This may be subject to change.  */
5589       parse_operand_result result = parse_neon_alignment (&p, i);
5590
5591       if (result != PARSE_OPERAND_SUCCESS)
5592         return result;
5593     }
5594
5595   if (skip_past_char (&p, ']') == FAIL)
5596     {
5597       inst.error = _("']' expected");
5598       return PARSE_OPERAND_FAIL;
5599     }
5600
5601   if (skip_past_char (&p, '!') == SUCCESS)
5602     inst.operands[i].writeback = 1;
5603
5604   else if (skip_past_comma (&p) == SUCCESS)
5605     {
5606       if (skip_past_char (&p, '{') == SUCCESS)
5607         {
5608           /* [Rn], {expr} - unindexed, with option */
5609           if (parse_immediate (&p, &inst.operands[i].imm,
5610                                0, 255, TRUE) == FAIL)
5611             return PARSE_OPERAND_FAIL;
5612
5613           if (skip_past_char (&p, '}') == FAIL)
5614             {
5615               inst.error = _("'}' expected at end of 'option' field");
5616               return PARSE_OPERAND_FAIL;
5617             }
5618           if (inst.operands[i].preind)
5619             {
5620               inst.error = _("cannot combine index with option");
5621               return PARSE_OPERAND_FAIL;
5622             }
5623           *str = p;
5624           return PARSE_OPERAND_SUCCESS;
5625         }
5626       else
5627         {
5628           inst.operands[i].postind = 1;
5629           inst.operands[i].writeback = 1;
5630
5631           if (inst.operands[i].preind)
5632             {
5633               inst.error = _("cannot combine pre- and post-indexing");
5634               return PARSE_OPERAND_FAIL;
5635             }
5636
5637           if (*p == '+') p++;
5638           else if (*p == '-') p++, inst.operands[i].negative = 1;
5639
5640           if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) != FAIL)
5641             {
5642               /* We might be using the immediate for alignment already. If we
5643                  are, OR the register number into the low-order bits.  */
5644               if (inst.operands[i].immisalign)
5645                 inst.operands[i].imm |= reg;
5646               else
5647                 inst.operands[i].imm = reg;
5648               inst.operands[i].immisreg = 1;
5649
5650               if (skip_past_comma (&p) == SUCCESS)
5651                 if (parse_shift (&p, i, SHIFT_IMMEDIATE) == FAIL)
5652                   return PARSE_OPERAND_FAIL;
5653             }
5654           else
5655             {
5656               char *q = p;
5657               if (inst.operands[i].negative)
5658                 {
5659                   inst.operands[i].negative = 0;
5660                   p--;
5661                 }
5662               if (my_get_expression (&inst.reloc.exp, &p, GE_IMM_PREFIX))
5663                 return PARSE_OPERAND_FAIL;
5664               /* If the offset is 0, find out if it's a +0 or -0.  */
5665               if (inst.reloc.exp.X_op == O_constant
5666                   && inst.reloc.exp.X_add_number == 0)
5667                 {
5668                   skip_whitespace (q);
5669                   if (*q == '#')
5670                     {
5671                       q++;
5672                       skip_whitespace (q);
5673                     }
5674                   if (*q == '-')
5675                     inst.operands[i].negative = 1;
5676                 }
5677             }
5678         }
5679     }
5680
5681   /* If at this point neither .preind nor .postind is set, we have a
5682      bare [Rn]{!}, which is shorthand for [Rn,#0]{!}.  */
5683   if (inst.operands[i].preind == 0 && inst.operands[i].postind == 0)
5684     {
5685       inst.operands[i].preind = 1;
5686       inst.reloc.exp.X_op = O_constant;
5687       inst.reloc.exp.X_add_number = 0;
5688     }
5689   *str = p;
5690   return PARSE_OPERAND_SUCCESS;
5691 }
5692
5693 static int
5694 parse_address (char **str, int i)
5695 {
5696   return parse_address_main (str, i, 0, GROUP_LDR) == PARSE_OPERAND_SUCCESS
5697          ? SUCCESS : FAIL;
5698 }
5699
5700 static parse_operand_result
5701 parse_address_group_reloc (char **str, int i, group_reloc_type type)
5702 {
5703   return parse_address_main (str, i, 1, type);
5704 }
5705
5706 /* Parse an operand for a MOVW or MOVT instruction.  */
5707 static int
5708 parse_half (char **str)
5709 {
5710   char * p;
5711
5712   p = *str;
5713   skip_past_char (&p, '#');
5714   if (strncasecmp (p, ":lower16:", 9) == 0)
5715     inst.reloc.type = BFD_RELOC_ARM_MOVW;
5716   else if (strncasecmp (p, ":upper16:", 9) == 0)
5717     inst.reloc.type = BFD_RELOC_ARM_MOVT;
5718
5719   if (inst.reloc.type != BFD_RELOC_UNUSED)
5720     {
5721       p += 9;
5722       skip_whitespace (p);
5723     }
5724
5725   if (my_get_expression (&inst.reloc.exp, &p, GE_NO_PREFIX))
5726     return FAIL;
5727
5728   if (inst.reloc.type == BFD_RELOC_UNUSED)
5729     {
5730       if (inst.reloc.exp.X_op != O_constant)
5731         {
5732           inst.error = _("constant expression expected");
5733           return FAIL;
5734         }
5735       if (inst.reloc.exp.X_add_number < 0
5736           || inst.reloc.exp.X_add_number > 0xffff)
5737         {
5738           inst.error = _("immediate value out of range");
5739           return FAIL;
5740         }
5741     }
5742   *str = p;
5743   return SUCCESS;
5744 }
5745
5746 /* Miscellaneous. */
5747
5748 /* Parse a PSR flag operand.  The value returned is FAIL on syntax error,
5749    or a bitmask suitable to be or-ed into the ARM msr instruction.  */
5750 static int
5751 parse_psr (char **str, bfd_boolean lhs)
5752 {
5753   char *p;
5754   unsigned long psr_field;
5755   const struct asm_psr *psr;
5756   char *start;
5757   bfd_boolean is_apsr = FALSE;
5758   bfd_boolean m_profile = ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m);
5759
5760   /* PR gas/12698:  If the user has specified -march=all then m_profile will
5761      be TRUE, but we want to ignore it in this case as we are building for any
5762      CPU type, including non-m variants.  */
5763   if (ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any))
5764     m_profile = FALSE;
5765
5766   /* CPSR's and SPSR's can now be lowercase.  This is just a convenience
5767      feature for ease of use and backwards compatibility.  */
5768   p = *str;
5769   if (strncasecmp (p, "SPSR", 4) == 0)
5770     {
5771       if (m_profile)
5772         goto unsupported_psr;
5773
5774       psr_field = SPSR_BIT;
5775     }
5776   else if (strncasecmp (p, "CPSR", 4) == 0)
5777     {
5778       if (m_profile)
5779         goto unsupported_psr;
5780
5781       psr_field = 0;
5782     }
5783   else if (strncasecmp (p, "APSR", 4) == 0)
5784     {
5785       /* APSR[_<bits>] can be used as a synonym for CPSR[_<flags>] on ARMv7-A
5786          and ARMv7-R architecture CPUs.  */
5787       is_apsr = TRUE;
5788       psr_field = 0;
5789     }
5790   else if (m_profile)
5791     {
5792       start = p;
5793       do
5794         p++;
5795       while (ISALNUM (*p) || *p == '_');
5796
5797       if (strncasecmp (start, "iapsr", 5) == 0
5798           || strncasecmp (start, "eapsr", 5) == 0
5799           || strncasecmp (start, "xpsr", 4) == 0
5800           || strncasecmp (start, "psr", 3) == 0)
5801         p = start + strcspn (start, "rR") + 1;
5802
5803       psr = (const struct asm_psr *) hash_find_n (arm_v7m_psr_hsh, start,
5804                                                   p - start);
5805
5806       if (!psr)
5807         return FAIL;
5808
5809       /* If APSR is being written, a bitfield may be specified.  Note that
5810          APSR itself is handled above.  */
5811       if (psr->field <= 3)
5812         {
5813           psr_field = psr->field;
5814           is_apsr = TRUE;
5815           goto check_suffix;
5816         }
5817
5818       *str = p;
5819       /* M-profile MSR instructions have the mask field set to "10", except
5820          *PSR variants which modify APSR, which may use a different mask (and
5821          have been handled already).  Do that by setting the PSR_f field
5822          here.  */
5823       return psr->field | (lhs ? PSR_f : 0);
5824     }
5825   else
5826     goto unsupported_psr;
5827
5828   p += 4;
5829 check_suffix:
5830   if (*p == '_')
5831     {
5832       /* A suffix follows.  */
5833       p++;
5834       start = p;
5835
5836       do
5837         p++;
5838       while (ISALNUM (*p) || *p == '_');
5839
5840       if (is_apsr)
5841         {
5842           /* APSR uses a notation for bits, rather than fields.  */
5843           unsigned int nzcvq_bits = 0;
5844           unsigned int g_bit = 0;
5845           char *bit;
5846
5847           for (bit = start; bit != p; bit++)
5848             {
5849               switch (TOLOWER (*bit))
5850                 {
5851                 case 'n':
5852                   nzcvq_bits |= (nzcvq_bits & 0x01) ? 0x20 : 0x01;
5853                   break;
5854
5855                 case 'z':
5856                   nzcvq_bits |= (nzcvq_bits & 0x02) ? 0x20 : 0x02;
5857                   break;
5858
5859                 case 'c':
5860                   nzcvq_bits |= (nzcvq_bits & 0x04) ? 0x20 : 0x04;
5861                   break;
5862
5863                 case 'v':
5864                   nzcvq_bits |= (nzcvq_bits & 0x08) ? 0x20 : 0x08;
5865                   break;
5866
5867                 case 'q':
5868                   nzcvq_bits |= (nzcvq_bits & 0x10) ? 0x20 : 0x10;
5869                   break;
5870
5871                 case 'g':
5872                   g_bit |= (g_bit & 0x1) ? 0x2 : 0x1;
5873                   break;
5874
5875                 default:
5876                   inst.error = _("unexpected bit specified after APSR");
5877                   return FAIL;
5878                 }
5879             }
5880
5881           if (nzcvq_bits == 0x1f)
5882             psr_field |= PSR_f;
5883
5884           if (g_bit == 0x1)
5885             {
5886               if (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp))
5887                 {
5888                   inst.error = _("selected processor does not "
5889                                  "support DSP extension");
5890                   return FAIL;
5891                 }
5892
5893               psr_field |= PSR_s;
5894             }
5895
5896           if ((nzcvq_bits & 0x20) != 0
5897               || (nzcvq_bits != 0x1f && nzcvq_bits != 0)
5898               || (g_bit & 0x2) != 0)
5899             {
5900               inst.error = _("bad bitmask specified after APSR");
5901               return FAIL;
5902             }
5903         }
5904       else
5905         {
5906           psr = (const struct asm_psr *) hash_find_n (arm_psr_hsh, start,
5907                                                       p - start);
5908           if (!psr)
5909             goto error;
5910
5911           psr_field |= psr->field;
5912         }
5913     }
5914   else
5915     {
5916       if (ISALNUM (*p))
5917         goto error;    /* Garbage after "[CS]PSR".  */
5918
5919       /* Unadorned APSR is equivalent to APSR_nzcvq/CPSR_f (for writes).  This
5920          is deprecated, but allow it anyway.  */
5921       if (is_apsr && lhs)
5922         {
5923           psr_field |= PSR_f;
5924           as_tsktsk (_("writing to APSR without specifying a bitmask is "
5925                        "deprecated"));
5926         }
5927       else if (!m_profile)
5928         /* These bits are never right for M-profile devices: don't set them
5929            (only code paths which read/write APSR reach here).  */
5930         psr_field |= (PSR_c | PSR_f);
5931     }
5932   *str = p;
5933   return psr_field;
5934
5935  unsupported_psr:
5936   inst.error = _("selected processor does not support requested special "
5937                  "purpose register");
5938   return FAIL;
5939
5940  error:
5941   inst.error = _("flag for {c}psr instruction expected");
5942   return FAIL;
5943 }
5944
5945 /* Parse the flags argument to CPSI[ED].  Returns FAIL on error, or a
5946    value suitable for splatting into the AIF field of the instruction.  */
5947
5948 static int
5949 parse_cps_flags (char **str)
5950 {
5951   int val = 0;
5952   int saw_a_flag = 0;
5953   char *s = *str;
5954
5955   for (;;)
5956     switch (*s++)
5957       {
5958       case '\0': case ',':
5959         goto done;
5960
5961       case 'a': case 'A': saw_a_flag = 1; val |= 0x4; break;
5962       case 'i': case 'I': saw_a_flag = 1; val |= 0x2; break;
5963       case 'f': case 'F': saw_a_flag = 1; val |= 0x1; break;
5964
5965       default:
5966         inst.error = _("unrecognized CPS flag");
5967         return FAIL;
5968       }
5969
5970  done:
5971   if (saw_a_flag == 0)
5972     {
5973       inst.error = _("missing CPS flags");
5974       return FAIL;
5975     }
5976
5977   *str = s - 1;
5978   return val;
5979 }
5980
5981 /* Parse an endian specifier ("BE" or "LE", case insensitive);
5982    returns 0 for big-endian, 1 for little-endian, FAIL for an error.  */
5983
5984 static int
5985 parse_endian_specifier (char **str)
5986 {
5987   int little_endian;
5988   char *s = *str;
5989
5990   if (strncasecmp (s, "BE", 2))
5991     little_endian = 0;
5992   else if (strncasecmp (s, "LE", 2))
5993     little_endian = 1;
5994   else
5995     {
5996       inst.error = _("valid endian specifiers are be or le");
5997       return FAIL;
5998     }
5999
6000   if (ISALNUM (s[2]) || s[2] == '_')
6001     {
6002       inst.error = _("valid endian specifiers are be or le");
6003       return FAIL;
6004     }
6005
6006   *str = s + 2;
6007   return little_endian;
6008 }
6009
6010 /* Parse a rotation specifier: ROR #0, #8, #16, #24.  *val receives a
6011    value suitable for poking into the rotate field of an sxt or sxta
6012    instruction, or FAIL on error.  */
6013
6014 static int
6015 parse_ror (char **str)
6016 {
6017   int rot;
6018   char *s = *str;
6019
6020   if (strncasecmp (s, "ROR", 3) == 0)
6021     s += 3;
6022   else
6023     {
6024       inst.error = _("missing rotation field after comma");
6025       return FAIL;
6026     }
6027
6028   if (parse_immediate (&s, &rot, 0, 24, FALSE) == FAIL)
6029     return FAIL;
6030
6031   switch (rot)
6032     {
6033     case  0: *str = s; return 0x0;
6034     case  8: *str = s; return 0x1;
6035     case 16: *str = s; return 0x2;
6036     case 24: *str = s; return 0x3;
6037
6038     default:
6039       inst.error = _("rotation can only be 0, 8, 16, or 24");
6040       return FAIL;
6041     }
6042 }
6043
6044 /* Parse a conditional code (from conds[] below).  The value returned is in the
6045    range 0 .. 14, or FAIL.  */
6046 static int
6047 parse_cond (char **str)
6048 {
6049   char *q;
6050   const struct asm_cond *c;
6051   int n;
6052   /* Condition codes are always 2 characters, so matching up to
6053      3 characters is sufficient.  */
6054   char cond[3];
6055
6056   q = *str;
6057   n = 0;
6058   while (ISALPHA (*q) && n < 3)
6059     {
6060       cond[n] = TOLOWER (*q);
6061       q++;
6062       n++;
6063     }
6064
6065   c = (const struct asm_cond *) hash_find_n (arm_cond_hsh, cond, n);
6066   if (!c)
6067     {
6068       inst.error = _("condition required");
6069       return FAIL;
6070     }
6071
6072   *str = q;
6073   return c->value;
6074 }
6075
6076 /* If the given feature available in the selected CPU, mark it as used.
6077    Returns TRUE iff feature is available.  */
6078 static bfd_boolean
6079 mark_feature_used (const arm_feature_set *feature)
6080 {
6081   /* Ensure the option is valid on the current architecture.  */
6082   if (!ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
6083     return FALSE;
6084
6085   /* Add the appropriate architecture feature for the barrier option used.
6086      */
6087   if (thumb_mode)
6088     ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, *feature);
6089   else
6090     ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, *feature);
6091
6092   return TRUE;
6093 }
6094
6095 /* Parse an option for a barrier instruction.  Returns the encoding for the
6096    option, or FAIL.  */
6097 static int
6098 parse_barrier (char **str)
6099 {
6100   char *p, *q;
6101   const struct asm_barrier_opt *o;
6102
6103   p = q = *str;
6104   while (ISALPHA (*q))
6105     q++;
6106
6107   o = (const struct asm_barrier_opt *) hash_find_n (arm_barrier_opt_hsh, p,
6108                                                     q - p);
6109   if (!o)
6110     return FAIL;
6111
6112   if (!mark_feature_used (&o->arch))
6113     return FAIL;
6114
6115   *str = q;
6116   return o->value;
6117 }
6118
6119 /* Parse the operands of a table branch instruction.  Similar to a memory
6120    operand.  */
6121 static int
6122 parse_tb (char **str)
6123 {
6124   char * p = *str;
6125   int reg;
6126
6127   if (skip_past_char (&p, '[') == FAIL)
6128     {
6129       inst.error = _("'[' expected");
6130       return FAIL;
6131     }
6132
6133   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6134     {
6135       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6136       return FAIL;
6137     }
6138   inst.operands[0].reg = reg;
6139
6140   if (skip_past_comma (&p) == FAIL)
6141     {
6142       inst.error = _("',' expected");
6143       return FAIL;
6144     }
6145
6146   if ((reg = arm_reg_parse (&p, REG_TYPE_RN)) == FAIL)
6147     {
6148       inst.error = _(reg_expected_msgs[REG_TYPE_RN]);
6149       return FAIL;
6150     }
6151   inst.operands[0].imm = reg;
6152
6153   if (skip_past_comma (&p) == SUCCESS)
6154     {
6155       if (parse_shift (&p, 0, SHIFT_LSL_IMMEDIATE) == FAIL)
6156         return FAIL;
6157       if (inst.reloc.exp.X_add_number != 1)
6158         {
6159           inst.error = _("invalid shift");
6160           return FAIL;
6161         }
6162       inst.operands[0].shifted = 1;
6163     }
6164
6165   if (skip_past_char (&p, ']') == FAIL)
6166     {
6167       inst.error = _("']' expected");
6168       return FAIL;
6169     }
6170   *str = p;
6171   return SUCCESS;
6172 }
6173
6174 /* Parse the operands of a Neon VMOV instruction. See do_neon_mov for more
6175    information on the types the operands can take and how they are encoded.
6176    Up to four operands may be read; this function handles setting the
6177    ".present" field for each read operand itself.
6178    Updates STR and WHICH_OPERAND if parsing is successful and returns SUCCESS,
6179    else returns FAIL.  */
6180
6181 static int
6182 parse_neon_mov (char **str, int *which_operand)
6183 {
6184   int i = *which_operand, val;
6185   enum arm_reg_type rtype;
6186   char *ptr = *str;
6187   struct neon_type_el optype;
6188
6189   if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6190     {
6191       /* Case 4: VMOV<c><q>.<size> <Dn[x]>, <Rd>.  */
6192       inst.operands[i].reg = val;
6193       inst.operands[i].isscalar = 1;
6194       inst.operands[i].vectype = optype;
6195       inst.operands[i++].present = 1;
6196
6197       if (skip_past_comma (&ptr) == FAIL)
6198         goto wanted_comma;
6199
6200       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6201         goto wanted_arm;
6202
6203       inst.operands[i].reg = val;
6204       inst.operands[i].isreg = 1;
6205       inst.operands[i].present = 1;
6206     }
6207   else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype, &optype))
6208            != FAIL)
6209     {
6210       /* Cases 0, 1, 2, 3, 5 (D only).  */
6211       if (skip_past_comma (&ptr) == FAIL)
6212         goto wanted_comma;
6213
6214       inst.operands[i].reg = val;
6215       inst.operands[i].isreg = 1;
6216       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6217       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6218       inst.operands[i].isvec = 1;
6219       inst.operands[i].vectype = optype;
6220       inst.operands[i++].present = 1;
6221
6222       if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6223         {
6224           /* Case 5: VMOV<c><q> <Dm>, <Rd>, <Rn>.
6225              Case 13: VMOV <Sd>, <Rm>  */
6226           inst.operands[i].reg = val;
6227           inst.operands[i].isreg = 1;
6228           inst.operands[i].present = 1;
6229
6230           if (rtype == REG_TYPE_NQ)
6231             {
6232               first_error (_("can't use Neon quad register here"));
6233               return FAIL;
6234             }
6235           else if (rtype != REG_TYPE_VFS)
6236             {
6237               i++;
6238               if (skip_past_comma (&ptr) == FAIL)
6239                 goto wanted_comma;
6240               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6241                 goto wanted_arm;
6242               inst.operands[i].reg = val;
6243               inst.operands[i].isreg = 1;
6244               inst.operands[i].present = 1;
6245             }
6246         }
6247       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_NSDQ, &rtype,
6248                                            &optype)) != FAIL)
6249         {
6250           /* Case 0: VMOV<c><q> <Qd>, <Qm>
6251              Case 1: VMOV<c><q> <Dd>, <Dm>
6252              Case 8: VMOV.F32 <Sd>, <Sm>
6253              Case 15: VMOV <Sd>, <Se>, <Rn>, <Rm>  */
6254
6255           inst.operands[i].reg = val;
6256           inst.operands[i].isreg = 1;
6257           inst.operands[i].isquad = (rtype == REG_TYPE_NQ);
6258           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6259           inst.operands[i].isvec = 1;
6260           inst.operands[i].vectype = optype;
6261           inst.operands[i].present = 1;
6262
6263           if (skip_past_comma (&ptr) == SUCCESS)
6264             {
6265               /* Case 15.  */
6266               i++;
6267
6268               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6269                 goto wanted_arm;
6270
6271               inst.operands[i].reg = val;
6272               inst.operands[i].isreg = 1;
6273               inst.operands[i++].present = 1;
6274
6275               if (skip_past_comma (&ptr) == FAIL)
6276                 goto wanted_comma;
6277
6278               if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) == FAIL)
6279                 goto wanted_arm;
6280
6281               inst.operands[i].reg = val;
6282               inst.operands[i].isreg = 1;
6283               inst.operands[i].present = 1;
6284             }
6285         }
6286       else if (parse_qfloat_immediate (&ptr, &inst.operands[i].imm) == SUCCESS)
6287           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<float-imm>
6288              Case 3: VMOV<c><q>.<dt> <Dd>, #<float-imm>
6289              Case 10: VMOV.F32 <Sd>, #<imm>
6290              Case 11: VMOV.F64 <Dd>, #<imm>  */
6291         inst.operands[i].immisfloat = 1;
6292       else if (parse_big_immediate (&ptr, i, NULL, /*allow_symbol_p=*/FALSE)
6293                == SUCCESS)
6294           /* Case 2: VMOV<c><q>.<dt> <Qd>, #<imm>
6295              Case 3: VMOV<c><q>.<dt> <Dd>, #<imm>  */
6296         ;
6297       else
6298         {
6299           first_error (_("expected <Rm> or <Dm> or <Qm> operand"));
6300           return FAIL;
6301         }
6302     }
6303   else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6304     {
6305       /* Cases 6, 7.  */
6306       inst.operands[i].reg = val;
6307       inst.operands[i].isreg = 1;
6308       inst.operands[i++].present = 1;
6309
6310       if (skip_past_comma (&ptr) == FAIL)
6311         goto wanted_comma;
6312
6313       if ((val = parse_scalar (&ptr, 8, &optype)) != FAIL)
6314         {
6315           /* Case 6: VMOV<c><q>.<dt> <Rd>, <Dn[x]>  */
6316           inst.operands[i].reg = val;
6317           inst.operands[i].isscalar = 1;
6318           inst.operands[i].present = 1;
6319           inst.operands[i].vectype = optype;
6320         }
6321       else if ((val = arm_reg_parse (&ptr, REG_TYPE_RN)) != FAIL)
6322         {
6323           /* Case 7: VMOV<c><q> <Rd>, <Rn>, <Dm>  */
6324           inst.operands[i].reg = val;
6325           inst.operands[i].isreg = 1;
6326           inst.operands[i++].present = 1;
6327
6328           if (skip_past_comma (&ptr) == FAIL)
6329             goto wanted_comma;
6330
6331           if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFSD, &rtype, &optype))
6332               == FAIL)
6333             {
6334               first_error (_(reg_expected_msgs[REG_TYPE_VFSD]));
6335               return FAIL;
6336             }
6337
6338           inst.operands[i].reg = val;
6339           inst.operands[i].isreg = 1;
6340           inst.operands[i].isvec = 1;
6341           inst.operands[i].issingle = (rtype == REG_TYPE_VFS);
6342           inst.operands[i].vectype = optype;
6343           inst.operands[i].present = 1;
6344
6345           if (rtype == REG_TYPE_VFS)
6346             {
6347               /* Case 14.  */
6348               i++;
6349               if (skip_past_comma (&ptr) == FAIL)
6350                 goto wanted_comma;
6351               if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL,
6352                                               &optype)) == FAIL)
6353                 {
6354                   first_error (_(reg_expected_msgs[REG_TYPE_VFS]));
6355                   return FAIL;
6356                 }
6357               inst.operands[i].reg = val;
6358               inst.operands[i].isreg = 1;
6359               inst.operands[i].isvec = 1;
6360               inst.operands[i].issingle = 1;
6361               inst.operands[i].vectype = optype;
6362               inst.operands[i].present = 1;
6363             }
6364         }
6365       else if ((val = arm_typed_reg_parse (&ptr, REG_TYPE_VFS, NULL, &optype))
6366                != FAIL)
6367         {
6368           /* Case 13.  */
6369           inst.operands[i].reg = val;
6370           inst.operands[i].isreg = 1;
6371           inst.operands[i].isvec = 1;
6372           inst.operands[i].issingle = 1;
6373           inst.operands[i].vectype = optype;
6374           inst.operands[i].present = 1;
6375         }
6376     }
6377   else
6378     {
6379       first_error (_("parse error"));
6380       return FAIL;
6381     }
6382
6383   /* Successfully parsed the operands. Update args.  */
6384   *which_operand = i;
6385   *str = ptr;
6386   return SUCCESS;
6387
6388  wanted_comma:
6389   first_error (_("expected comma"));
6390   return FAIL;
6391
6392  wanted_arm:
6393   first_error (_(reg_expected_msgs[REG_TYPE_RN]));
6394   return FAIL;
6395 }
6396
6397 /* Use this macro when the operand constraints are different
6398    for ARM and THUMB (e.g. ldrd).  */
6399 #define MIX_ARM_THUMB_OPERANDS(arm_operand, thumb_operand) \
6400         ((arm_operand) | ((thumb_operand) << 16))
6401
6402 /* Matcher codes for parse_operands.  */
6403 enum operand_parse_code
6404 {
6405   OP_stop,      /* end of line */
6406
6407   OP_RR,        /* ARM register */
6408   OP_RRnpc,     /* ARM register, not r15 */
6409   OP_RRnpcsp,   /* ARM register, neither r15 nor r13 (a.k.a. 'BadReg') */
6410   OP_RRnpcb,    /* ARM register, not r15, in square brackets */
6411   OP_RRnpctw,   /* ARM register, not r15 in Thumb-state or with writeback,
6412                    optional trailing ! */
6413   OP_RRw,       /* ARM register, not r15, optional trailing ! */
6414   OP_RCP,       /* Coprocessor number */
6415   OP_RCN,       /* Coprocessor register */
6416   OP_RF,        /* FPA register */
6417   OP_RVS,       /* VFP single precision register */
6418   OP_RVD,       /* VFP double precision register (0..15) */
6419   OP_RND,       /* Neon double precision register (0..31) */
6420   OP_RNQ,       /* Neon quad precision register */
6421   OP_RVSD,      /* VFP single or double precision register */
6422   OP_RNDQ,      /* Neon double or quad precision register */
6423   OP_RNSDQ,     /* Neon single, double or quad precision register */
6424   OP_RNSC,      /* Neon scalar D[X] */
6425   OP_RVC,       /* VFP control register */
6426   OP_RMF,       /* Maverick F register */
6427   OP_RMD,       /* Maverick D register */
6428   OP_RMFX,      /* Maverick FX register */
6429   OP_RMDX,      /* Maverick DX register */
6430   OP_RMAX,      /* Maverick AX register */
6431   OP_RMDS,      /* Maverick DSPSC register */
6432   OP_RIWR,      /* iWMMXt wR register */
6433   OP_RIWC,      /* iWMMXt wC register */
6434   OP_RIWG,      /* iWMMXt wCG register */
6435   OP_RXA,       /* XScale accumulator register */
6436
6437   OP_REGLST,    /* ARM register list */
6438   OP_VRSLST,    /* VFP single-precision register list */
6439   OP_VRDLST,    /* VFP double-precision register list */
6440   OP_VRSDLST,   /* VFP single or double-precision register list (& quad) */
6441   OP_NRDLST,    /* Neon double-precision register list (d0-d31, qN aliases) */
6442   OP_NSTRLST,   /* Neon element/structure list */
6443
6444   OP_RNDQ_I0,   /* Neon D or Q reg, or immediate zero.  */
6445   OP_RVSD_I0,   /* VFP S or D reg, or immediate zero.  */
6446   OP_RSVD_FI0, /* VFP S or D reg, or floating point immediate zero.  */
6447   OP_RR_RNSC,   /* ARM reg or Neon scalar.  */
6448   OP_RNSDQ_RNSC, /* Vector S, D or Q reg, or Neon scalar.  */
6449   OP_RNDQ_RNSC, /* Neon D or Q reg, or Neon scalar.  */
6450   OP_RND_RNSC,  /* Neon D reg, or Neon scalar.  */
6451   OP_VMOV,      /* Neon VMOV operands.  */
6452   OP_RNDQ_Ibig, /* Neon D or Q reg, or big immediate for logic and VMVN.  */
6453   OP_RNDQ_I63b, /* Neon D or Q reg, or immediate for shift.  */
6454   OP_RIWR_I32z, /* iWMMXt wR register, or immediate 0 .. 32 for iWMMXt2.  */
6455
6456   OP_I0,        /* immediate zero */
6457   OP_I7,        /* immediate value 0 .. 7 */
6458   OP_I15,       /*                 0 .. 15 */
6459   OP_I16,       /*                 1 .. 16 */
6460   OP_I16z,      /*                 0 .. 16 */
6461   OP_I31,       /*                 0 .. 31 */
6462   OP_I31w,      /*                 0 .. 31, optional trailing ! */
6463   OP_I32,       /*                 1 .. 32 */
6464   OP_I32z,      /*                 0 .. 32 */
6465   OP_I63,       /*                 0 .. 63 */
6466   OP_I63s,      /*               -64 .. 63 */
6467   OP_I64,       /*                 1 .. 64 */
6468   OP_I64z,      /*                 0 .. 64 */
6469   OP_I255,      /*                 0 .. 255 */
6470
6471   OP_I4b,       /* immediate, prefix optional, 1 .. 4 */
6472   OP_I7b,       /*                             0 .. 7 */
6473   OP_I15b,      /*                             0 .. 15 */
6474   OP_I31b,      /*                             0 .. 31 */
6475
6476   OP_SH,        /* shifter operand */
6477   OP_SHG,       /* shifter operand with possible group relocation */
6478   OP_ADDR,      /* Memory address expression (any mode) */
6479   OP_ADDRGLDR,  /* Mem addr expr (any mode) with possible LDR group reloc */
6480   OP_ADDRGLDRS, /* Mem addr expr (any mode) with possible LDRS group reloc */
6481   OP_ADDRGLDC,  /* Mem addr expr (any mode) with possible LDC group reloc */
6482   OP_EXP,       /* arbitrary expression */
6483   OP_EXPi,      /* same, with optional immediate prefix */
6484   OP_EXPr,      /* same, with optional relocation suffix */
6485   OP_HALF,      /* 0 .. 65535 or low/high reloc.  */
6486
6487   OP_CPSF,      /* CPS flags */
6488   OP_ENDI,      /* Endianness specifier */
6489   OP_wPSR,      /* CPSR/SPSR/APSR mask for msr (writing).  */
6490   OP_rPSR,      /* CPSR/SPSR/APSR mask for msr (reading).  */
6491   OP_COND,      /* conditional code */
6492   OP_TB,        /* Table branch.  */
6493
6494   OP_APSR_RR,   /* ARM register or "APSR_nzcv".  */
6495
6496   OP_RRnpc_I0,  /* ARM register or literal 0 */
6497   OP_RR_EXr,    /* ARM register or expression with opt. reloc suff. */
6498   OP_RR_EXi,    /* ARM register or expression with imm prefix */
6499   OP_RF_IF,     /* FPA register or immediate */
6500   OP_RIWR_RIWC, /* iWMMXt R or C reg */
6501   OP_RIWC_RIWG, /* iWMMXt wC or wCG reg */
6502
6503   /* Optional operands.  */
6504   OP_oI7b,       /* immediate, prefix optional, 0 .. 7 */
6505   OP_oI31b,      /*                             0 .. 31 */
6506   OP_oI32b,      /*                             1 .. 32 */
6507   OP_oI32z,      /*                             0 .. 32 */
6508   OP_oIffffb,    /*                             0 .. 65535 */
6509   OP_oI255c,     /*       curly-brace enclosed, 0 .. 255 */
6510
6511   OP_oRR,        /* ARM register */
6512   OP_oRRnpc,     /* ARM register, not the PC */
6513   OP_oRRnpcsp,   /* ARM register, neither the PC nor the SP (a.k.a. BadReg) */
6514   OP_oRRw,       /* ARM register, not r15, optional trailing ! */
6515   OP_oRND,       /* Optional Neon double precision register */
6516   OP_oRNQ,       /* Optional Neon quad precision register */
6517   OP_oRNDQ,      /* Optional Neon double or quad precision register */
6518   OP_oRNSDQ,     /* Optional single, double or quad precision vector register */
6519   OP_oSHll,      /* LSL immediate */
6520   OP_oSHar,      /* ASR immediate */
6521   OP_oSHllar,    /* LSL or ASR immediate */
6522   OP_oROR,       /* ROR 0/8/16/24 */
6523   OP_oBARRIER_I15, /* Option argument for a barrier instruction.  */
6524
6525   /* Some pre-defined mixed (ARM/THUMB) operands.  */
6526   OP_RR_npcsp           = MIX_ARM_THUMB_OPERANDS (OP_RR, OP_RRnpcsp),
6527   OP_RRnpc_npcsp        = MIX_ARM_THUMB_OPERANDS (OP_RRnpc, OP_RRnpcsp),
6528   OP_oRRnpc_npcsp       = MIX_ARM_THUMB_OPERANDS (OP_oRRnpc, OP_oRRnpcsp),
6529
6530   OP_FIRST_OPTIONAL = OP_oI7b
6531 };
6532
6533 /* Generic instruction operand parser.  This does no encoding and no
6534    semantic validation; it merely squirrels values away in the inst
6535    structure.  Returns SUCCESS or FAIL depending on whether the
6536    specified grammar matched.  */
6537 static int
6538 parse_operands (char *str, const unsigned int *pattern, bfd_boolean thumb)
6539 {
6540   unsigned const int *upat = pattern;
6541   char *backtrack_pos = 0;
6542   const char *backtrack_error = 0;
6543   int i, val = 0, backtrack_index = 0;
6544   enum arm_reg_type rtype;
6545   parse_operand_result result;
6546   unsigned int op_parse_code;
6547
6548 #define po_char_or_fail(chr)                    \
6549   do                                            \
6550     {                                           \
6551       if (skip_past_char (&str, chr) == FAIL)   \
6552         goto bad_args;                          \
6553     }                                           \
6554   while (0)
6555
6556 #define po_reg_or_fail(regtype)                                 \
6557   do                                                            \
6558     {                                                           \
6559       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6560                                  & inst.operands[i].vectype);   \
6561       if (val == FAIL)                                          \
6562         {                                                       \
6563           first_error (_(reg_expected_msgs[regtype]));          \
6564           goto failure;                                         \
6565         }                                                       \
6566       inst.operands[i].reg = val;                               \
6567       inst.operands[i].isreg = 1;                               \
6568       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6569       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6570       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6571                              || rtype == REG_TYPE_VFD           \
6572                              || rtype == REG_TYPE_NQ);          \
6573     }                                                           \
6574   while (0)
6575
6576 #define po_reg_or_goto(regtype, label)                          \
6577   do                                                            \
6578     {                                                           \
6579       val = arm_typed_reg_parse (& str, regtype, & rtype,       \
6580                                  & inst.operands[i].vectype);   \
6581       if (val == FAIL)                                          \
6582         goto label;                                             \
6583                                                                 \
6584       inst.operands[i].reg = val;                               \
6585       inst.operands[i].isreg = 1;                               \
6586       inst.operands[i].isquad = (rtype == REG_TYPE_NQ);         \
6587       inst.operands[i].issingle = (rtype == REG_TYPE_VFS);      \
6588       inst.operands[i].isvec = (rtype == REG_TYPE_VFS           \
6589                              || rtype == REG_TYPE_VFD           \
6590                              || rtype == REG_TYPE_NQ);          \
6591     }                                                           \
6592   while (0)
6593
6594 #define po_imm_or_fail(min, max, popt)                          \
6595   do                                                            \
6596     {                                                           \
6597       if (parse_immediate (&str, &val, min, max, popt) == FAIL) \
6598         goto failure;                                           \
6599       inst.operands[i].imm = val;                               \
6600     }                                                           \
6601   while (0)
6602
6603 #define po_scalar_or_goto(elsz, label)                                  \
6604   do                                                                    \
6605     {                                                                   \
6606       val = parse_scalar (& str, elsz, & inst.operands[i].vectype);     \
6607       if (val == FAIL)                                                  \
6608         goto label;                                                     \
6609       inst.operands[i].reg = val;                                       \
6610       inst.operands[i].isscalar = 1;                                    \
6611     }                                                                   \
6612   while (0)
6613
6614 #define po_misc_or_fail(expr)                   \
6615   do                                            \
6616     {                                           \
6617       if (expr)                                 \
6618         goto failure;                           \
6619     }                                           \
6620   while (0)
6621
6622 #define po_misc_or_fail_no_backtrack(expr)              \
6623   do                                                    \
6624     {                                                   \
6625       result = expr;                                    \
6626       if (result == PARSE_OPERAND_FAIL_NO_BACKTRACK)    \
6627         backtrack_pos = 0;                              \
6628       if (result != PARSE_OPERAND_SUCCESS)              \
6629         goto failure;                                   \
6630     }                                                   \
6631   while (0)
6632
6633 #define po_barrier_or_imm(str)                             \
6634   do                                                       \
6635     {                                                      \
6636       val = parse_barrier (&str);                          \
6637       if (val == FAIL && ! ISALPHA (*str))                 \
6638         goto immediate;                                    \
6639       if (val == FAIL                                      \
6640           /* ISB can only take SY as an option.  */        \
6641           || ((inst.instruction & 0xf0) == 0x60            \
6642                && val != 0xf))                             \
6643         {                                                  \
6644            inst.error = _("invalid barrier type");         \
6645            backtrack_pos = 0;                              \
6646            goto failure;                                   \
6647         }                                                  \
6648     }                                                      \
6649   while (0)
6650
6651   skip_whitespace (str);
6652
6653   for (i = 0; upat[i] != OP_stop; i++)
6654     {
6655       op_parse_code = upat[i];
6656       if (op_parse_code >= 1<<16)
6657         op_parse_code = thumb ? (op_parse_code >> 16)
6658                                 : (op_parse_code & ((1<<16)-1));
6659
6660       if (op_parse_code >= OP_FIRST_OPTIONAL)
6661         {
6662           /* Remember where we are in case we need to backtrack.  */
6663           gas_assert (!backtrack_pos);
6664           backtrack_pos = str;
6665           backtrack_error = inst.error;
6666           backtrack_index = i;
6667         }
6668
6669       if (i > 0 && (i > 1 || inst.operands[0].present))
6670         po_char_or_fail (',');
6671
6672       switch (op_parse_code)
6673         {
6674           /* Registers */
6675         case OP_oRRnpc:
6676         case OP_oRRnpcsp:
6677         case OP_RRnpc:
6678         case OP_RRnpcsp:
6679         case OP_oRR:
6680         case OP_RR:    po_reg_or_fail (REG_TYPE_RN);      break;
6681         case OP_RCP:   po_reg_or_fail (REG_TYPE_CP);      break;
6682         case OP_RCN:   po_reg_or_fail (REG_TYPE_CN);      break;
6683         case OP_RF:    po_reg_or_fail (REG_TYPE_FN);      break;
6684         case OP_RVS:   po_reg_or_fail (REG_TYPE_VFS);     break;
6685         case OP_RVD:   po_reg_or_fail (REG_TYPE_VFD);     break;
6686         case OP_oRND:
6687         case OP_RND:   po_reg_or_fail (REG_TYPE_VFD);     break;
6688         case OP_RVC:
6689           po_reg_or_goto (REG_TYPE_VFC, coproc_reg);
6690           break;
6691           /* Also accept generic coprocessor regs for unknown registers.  */
6692           coproc_reg:
6693           po_reg_or_fail (REG_TYPE_CN);
6694           break;
6695         case OP_RMF:   po_reg_or_fail (REG_TYPE_MVF);     break;
6696         case OP_RMD:   po_reg_or_fail (REG_TYPE_MVD);     break;
6697         case OP_RMFX:  po_reg_or_fail (REG_TYPE_MVFX);    break;
6698         case OP_RMDX:  po_reg_or_fail (REG_TYPE_MVDX);    break;
6699         case OP_RMAX:  po_reg_or_fail (REG_TYPE_MVAX);    break;
6700         case OP_RMDS:  po_reg_or_fail (REG_TYPE_DSPSC);   break;
6701         case OP_RIWR:  po_reg_or_fail (REG_TYPE_MMXWR);   break;
6702         case OP_RIWC:  po_reg_or_fail (REG_TYPE_MMXWC);   break;
6703         case OP_RIWG:  po_reg_or_fail (REG_TYPE_MMXWCG);  break;
6704         case OP_RXA:   po_reg_or_fail (REG_TYPE_XSCALE);  break;
6705         case OP_oRNQ:
6706         case OP_RNQ:   po_reg_or_fail (REG_TYPE_NQ);      break;
6707         case OP_oRNDQ:
6708         case OP_RNDQ:  po_reg_or_fail (REG_TYPE_NDQ);     break;
6709         case OP_RVSD:  po_reg_or_fail (REG_TYPE_VFSD);    break;
6710         case OP_oRNSDQ:
6711         case OP_RNSDQ: po_reg_or_fail (REG_TYPE_NSDQ);    break;
6712
6713         /* Neon scalar. Using an element size of 8 means that some invalid
6714            scalars are accepted here, so deal with those in later code.  */
6715         case OP_RNSC:  po_scalar_or_goto (8, failure);    break;
6716
6717         case OP_RNDQ_I0:
6718           {
6719             po_reg_or_goto (REG_TYPE_NDQ, try_imm0);
6720             break;
6721             try_imm0:
6722             po_imm_or_fail (0, 0, TRUE);
6723           }
6724           break;
6725
6726         case OP_RVSD_I0:
6727           po_reg_or_goto (REG_TYPE_VFSD, try_imm0);
6728           break;
6729
6730         case OP_RSVD_FI0:
6731           {
6732             po_reg_or_goto (REG_TYPE_VFSD, try_ifimm0);
6733             break;
6734             try_ifimm0:
6735             if (parse_ifimm_zero (&str))
6736               inst.operands[i].imm = 0;
6737             else
6738             {
6739               inst.error
6740                 = _("only floating point zero is allowed as immediate value");
6741               goto failure;
6742             }
6743           }
6744           break;
6745
6746         case OP_RR_RNSC:
6747           {
6748             po_scalar_or_goto (8, try_rr);
6749             break;
6750             try_rr:
6751             po_reg_or_fail (REG_TYPE_RN);
6752           }
6753           break;
6754
6755         case OP_RNSDQ_RNSC:
6756           {
6757             po_scalar_or_goto (8, try_nsdq);
6758             break;
6759             try_nsdq:
6760             po_reg_or_fail (REG_TYPE_NSDQ);
6761           }
6762           break;
6763
6764         case OP_RNDQ_RNSC:
6765           {
6766             po_scalar_or_goto (8, try_ndq);
6767             break;
6768             try_ndq:
6769             po_reg_or_fail (REG_TYPE_NDQ);
6770           }
6771           break;
6772
6773         case OP_RND_RNSC:
6774           {
6775             po_scalar_or_goto (8, try_vfd);
6776             break;
6777             try_vfd:
6778             po_reg_or_fail (REG_TYPE_VFD);
6779           }
6780           break;
6781
6782         case OP_VMOV:
6783           /* WARNING: parse_neon_mov can move the operand counter, i. If we're
6784              not careful then bad things might happen.  */
6785           po_misc_or_fail (parse_neon_mov (&str, &i) == FAIL);
6786           break;
6787
6788         case OP_RNDQ_Ibig:
6789           {
6790             po_reg_or_goto (REG_TYPE_NDQ, try_immbig);
6791             break;
6792             try_immbig:
6793             /* There's a possibility of getting a 64-bit immediate here, so
6794                we need special handling.  */
6795             if (parse_big_immediate (&str, i, NULL, /*allow_symbol_p=*/FALSE)
6796                 == FAIL)
6797               {
6798                 inst.error = _("immediate value is out of range");
6799                 goto failure;
6800               }
6801           }
6802           break;
6803
6804         case OP_RNDQ_I63b:
6805           {
6806             po_reg_or_goto (REG_TYPE_NDQ, try_shimm);
6807             break;
6808             try_shimm:
6809             po_imm_or_fail (0, 63, TRUE);
6810           }
6811           break;
6812
6813         case OP_RRnpcb:
6814           po_char_or_fail ('[');
6815           po_reg_or_fail  (REG_TYPE_RN);
6816           po_char_or_fail (']');
6817           break;
6818
6819         case OP_RRnpctw:
6820         case OP_RRw:
6821         case OP_oRRw:
6822           po_reg_or_fail (REG_TYPE_RN);
6823           if (skip_past_char (&str, '!') == SUCCESS)
6824             inst.operands[i].writeback = 1;
6825           break;
6826
6827           /* Immediates */
6828         case OP_I7:      po_imm_or_fail (  0,      7, FALSE);   break;
6829         case OP_I15:     po_imm_or_fail (  0,     15, FALSE);   break;
6830         case OP_I16:     po_imm_or_fail (  1,     16, FALSE);   break;
6831         case OP_I16z:    po_imm_or_fail (  0,     16, FALSE);   break;
6832         case OP_I31:     po_imm_or_fail (  0,     31, FALSE);   break;
6833         case OP_I32:     po_imm_or_fail (  1,     32, FALSE);   break;
6834         case OP_I32z:    po_imm_or_fail (  0,     32, FALSE);   break;
6835         case OP_I63s:    po_imm_or_fail (-64,     63, FALSE);   break;
6836         case OP_I63:     po_imm_or_fail (  0,     63, FALSE);   break;
6837         case OP_I64:     po_imm_or_fail (  1,     64, FALSE);   break;
6838         case OP_I64z:    po_imm_or_fail (  0,     64, FALSE);   break;
6839         case OP_I255:    po_imm_or_fail (  0,    255, FALSE);   break;
6840
6841         case OP_I4b:     po_imm_or_fail (  1,      4, TRUE);    break;
6842         case OP_oI7b:
6843         case OP_I7b:     po_imm_or_fail (  0,      7, TRUE);    break;
6844         case OP_I15b:    po_imm_or_fail (  0,     15, TRUE);    break;
6845         case OP_oI31b:
6846         case OP_I31b:    po_imm_or_fail (  0,     31, TRUE);    break;
6847         case OP_oI32b:   po_imm_or_fail (  1,     32, TRUE);    break;
6848         case OP_oI32z:   po_imm_or_fail (  0,     32, TRUE);    break;
6849         case OP_oIffffb: po_imm_or_fail (  0, 0xffff, TRUE);    break;
6850
6851           /* Immediate variants */
6852         case OP_oI255c:
6853           po_char_or_fail ('{');
6854           po_imm_or_fail (0, 255, TRUE);
6855           po_char_or_fail ('}');
6856           break;
6857
6858         case OP_I31w:
6859           /* The expression parser chokes on a trailing !, so we have
6860              to find it first and zap it.  */
6861           {
6862             char *s = str;
6863             while (*s && *s != ',')
6864               s++;
6865             if (s[-1] == '!')
6866               {
6867                 s[-1] = '\0';
6868                 inst.operands[i].writeback = 1;
6869               }
6870             po_imm_or_fail (0, 31, TRUE);
6871             if (str == s - 1)
6872               str = s;
6873           }
6874           break;
6875
6876           /* Expressions */
6877         case OP_EXPi:   EXPi:
6878           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6879                                               GE_OPT_PREFIX));
6880           break;
6881
6882         case OP_EXP:
6883           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6884                                               GE_NO_PREFIX));
6885           break;
6886
6887         case OP_EXPr:   EXPr:
6888           po_misc_or_fail (my_get_expression (&inst.reloc.exp, &str,
6889                                               GE_NO_PREFIX));
6890           if (inst.reloc.exp.X_op == O_symbol)
6891             {
6892               val = parse_reloc (&str);
6893               if (val == -1)
6894                 {
6895                   inst.error = _("unrecognized relocation suffix");
6896                   goto failure;
6897                 }
6898               else if (val != BFD_RELOC_UNUSED)
6899                 {
6900                   inst.operands[i].imm = val;
6901                   inst.operands[i].hasreloc = 1;
6902                 }
6903             }
6904           break;
6905
6906           /* Operand for MOVW or MOVT.  */
6907         case OP_HALF:
6908           po_misc_or_fail (parse_half (&str));
6909           break;
6910
6911           /* Register or expression.  */
6912         case OP_RR_EXr:   po_reg_or_goto (REG_TYPE_RN, EXPr); break;
6913         case OP_RR_EXi:   po_reg_or_goto (REG_TYPE_RN, EXPi); break;
6914
6915           /* Register or immediate.  */
6916         case OP_RRnpc_I0: po_reg_or_goto (REG_TYPE_RN, I0);   break;
6917         I0:               po_imm_or_fail (0, 0, FALSE);       break;
6918
6919         case OP_RF_IF:    po_reg_or_goto (REG_TYPE_FN, IF);   break;
6920         IF:
6921           if (!is_immediate_prefix (*str))
6922             goto bad_args;
6923           str++;
6924           val = parse_fpa_immediate (&str);
6925           if (val == FAIL)
6926             goto failure;
6927           /* FPA immediates are encoded as registers 8-15.
6928              parse_fpa_immediate has already applied the offset.  */
6929           inst.operands[i].reg = val;
6930           inst.operands[i].isreg = 1;
6931           break;
6932
6933         case OP_RIWR_I32z: po_reg_or_goto (REG_TYPE_MMXWR, I32z); break;
6934         I32z:             po_imm_or_fail (0, 32, FALSE);          break;
6935
6936           /* Two kinds of register.  */
6937         case OP_RIWR_RIWC:
6938           {
6939             struct reg_entry *rege = arm_reg_parse_multi (&str);
6940             if (!rege
6941                 || (rege->type != REG_TYPE_MMXWR
6942                     && rege->type != REG_TYPE_MMXWC
6943                     && rege->type != REG_TYPE_MMXWCG))
6944               {
6945                 inst.error = _("iWMMXt data or control register expected");
6946                 goto failure;
6947               }
6948             inst.operands[i].reg = rege->number;
6949             inst.operands[i].isreg = (rege->type == REG_TYPE_MMXWR);
6950           }
6951           break;
6952
6953         case OP_RIWC_RIWG:
6954           {
6955             struct reg_entry *rege = arm_reg_parse_multi (&str);
6956             if (!rege
6957                 || (rege->type != REG_TYPE_MMXWC
6958                     && rege->type != REG_TYPE_MMXWCG))
6959               {
6960                 inst.error = _("iWMMXt control register expected");
6961                 goto failure;
6962               }
6963             inst.operands[i].reg = rege->number;
6964             inst.operands[i].isreg = 1;
6965           }
6966           break;
6967
6968           /* Misc */
6969         case OP_CPSF:    val = parse_cps_flags (&str);          break;
6970         case OP_ENDI:    val = parse_endian_specifier (&str);   break;
6971         case OP_oROR:    val = parse_ror (&str);                break;
6972         case OP_COND:    val = parse_cond (&str);               break;
6973         case OP_oBARRIER_I15:
6974           po_barrier_or_imm (str); break;
6975           immediate:
6976           if (parse_immediate (&str, &val, 0, 15, TRUE) == FAIL)
6977             goto failure;
6978           break;
6979
6980         case OP_wPSR:
6981         case OP_rPSR:
6982           po_reg_or_goto (REG_TYPE_RNB, try_psr);
6983           if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_virt))
6984             {
6985               inst.error = _("Banked registers are not available with this "
6986                              "architecture.");
6987               goto failure;
6988             }
6989           break;
6990           try_psr:
6991           val = parse_psr (&str, op_parse_code == OP_wPSR);
6992           break;
6993
6994         case OP_APSR_RR:
6995           po_reg_or_goto (REG_TYPE_RN, try_apsr);
6996           break;
6997           try_apsr:
6998           /* Parse "APSR_nvzc" operand (for FMSTAT-equivalent MRS
6999              instruction).  */
7000           if (strncasecmp (str, "APSR_", 5) == 0)
7001             {
7002               unsigned found = 0;
7003               str += 5;
7004               while (found < 15)
7005                 switch (*str++)
7006                   {
7007                   case 'c': found = (found & 1) ? 16 : found | 1; break;
7008                   case 'n': found = (found & 2) ? 16 : found | 2; break;
7009                   case 'z': found = (found & 4) ? 16 : found | 4; break;
7010                   case 'v': found = (found & 8) ? 16 : found | 8; break;
7011                   default: found = 16;
7012                   }
7013               if (found != 15)
7014                 goto failure;
7015               inst.operands[i].isvec = 1;
7016               /* APSR_nzcv is encoded in instructions as if it were the REG_PC.  */
7017               inst.operands[i].reg = REG_PC;
7018             }
7019           else
7020             goto failure;
7021           break;
7022
7023         case OP_TB:
7024           po_misc_or_fail (parse_tb (&str));
7025           break;
7026
7027           /* Register lists.  */
7028         case OP_REGLST:
7029           val = parse_reg_list (&str);
7030           if (*str == '^')
7031             {
7032               inst.operands[i].writeback = 1;
7033               str++;
7034             }
7035           break;
7036
7037         case OP_VRSLST:
7038           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_S);
7039           break;
7040
7041         case OP_VRDLST:
7042           val = parse_vfp_reg_list (&str, &inst.operands[i].reg, REGLIST_VFP_D);
7043           break;
7044
7045         case OP_VRSDLST:
7046           /* Allow Q registers too.  */
7047           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7048                                     REGLIST_NEON_D);
7049           if (val == FAIL)
7050             {
7051               inst.error = NULL;
7052               val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7053                                         REGLIST_VFP_S);
7054               inst.operands[i].issingle = 1;
7055             }
7056           break;
7057
7058         case OP_NRDLST:
7059           val = parse_vfp_reg_list (&str, &inst.operands[i].reg,
7060                                     REGLIST_NEON_D);
7061           break;
7062
7063         case OP_NSTRLST:
7064           val = parse_neon_el_struct_list (&str, &inst.operands[i].reg,
7065                                            &inst.operands[i].vectype);
7066           break;
7067
7068           /* Addressing modes */
7069         case OP_ADDR:
7070           po_misc_or_fail (parse_address (&str, i));
7071           break;
7072
7073         case OP_ADDRGLDR:
7074           po_misc_or_fail_no_backtrack (
7075             parse_address_group_reloc (&str, i, GROUP_LDR));
7076           break;
7077
7078         case OP_ADDRGLDRS:
7079           po_misc_or_fail_no_backtrack (
7080             parse_address_group_reloc (&str, i, GROUP_LDRS));
7081           break;
7082
7083         case OP_ADDRGLDC:
7084           po_misc_or_fail_no_backtrack (
7085             parse_address_group_reloc (&str, i, GROUP_LDC));
7086           break;
7087
7088         case OP_SH:
7089           po_misc_or_fail (parse_shifter_operand (&str, i));
7090           break;
7091
7092         case OP_SHG:
7093           po_misc_or_fail_no_backtrack (
7094             parse_shifter_operand_group_reloc (&str, i));
7095           break;
7096
7097         case OP_oSHll:
7098           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_IMMEDIATE));
7099           break;
7100
7101         case OP_oSHar:
7102           po_misc_or_fail (parse_shift (&str, i, SHIFT_ASR_IMMEDIATE));
7103           break;
7104
7105         case OP_oSHllar:
7106           po_misc_or_fail (parse_shift (&str, i, SHIFT_LSL_OR_ASR_IMMEDIATE));
7107           break;
7108
7109         default:
7110           as_fatal (_("unhandled operand code %d"), op_parse_code);
7111         }
7112
7113       /* Various value-based sanity checks and shared operations.  We
7114          do not signal immediate failures for the register constraints;
7115          this allows a syntax error to take precedence.  */
7116       switch (op_parse_code)
7117         {
7118         case OP_oRRnpc:
7119         case OP_RRnpc:
7120         case OP_RRnpcb:
7121         case OP_RRw:
7122         case OP_oRRw:
7123         case OP_RRnpc_I0:
7124           if (inst.operands[i].isreg && inst.operands[i].reg == REG_PC)
7125             inst.error = BAD_PC;
7126           break;
7127
7128         case OP_oRRnpcsp:
7129         case OP_RRnpcsp:
7130           if (inst.operands[i].isreg)
7131             {
7132               if (inst.operands[i].reg == REG_PC)
7133                 inst.error = BAD_PC;
7134               else if (inst.operands[i].reg == REG_SP)
7135                 inst.error = BAD_SP;
7136             }
7137           break;
7138
7139         case OP_RRnpctw:
7140           if (inst.operands[i].isreg
7141               && inst.operands[i].reg == REG_PC
7142               && (inst.operands[i].writeback || thumb))
7143             inst.error = BAD_PC;
7144           break;
7145
7146         case OP_CPSF:
7147         case OP_ENDI:
7148         case OP_oROR:
7149         case OP_wPSR:
7150         case OP_rPSR:
7151         case OP_COND:
7152         case OP_oBARRIER_I15:
7153         case OP_REGLST:
7154         case OP_VRSLST:
7155         case OP_VRDLST:
7156         case OP_VRSDLST:
7157         case OP_NRDLST:
7158         case OP_NSTRLST:
7159           if (val == FAIL)
7160             goto failure;
7161           inst.operands[i].imm = val;
7162           break;
7163
7164         default:
7165           break;
7166         }
7167
7168       /* If we get here, this operand was successfully parsed.  */
7169       inst.operands[i].present = 1;
7170       continue;
7171
7172     bad_args:
7173       inst.error = BAD_ARGS;
7174
7175     failure:
7176       if (!backtrack_pos)
7177         {
7178           /* The parse routine should already have set inst.error, but set a
7179              default here just in case.  */
7180           if (!inst.error)
7181             inst.error = _("syntax error");
7182           return FAIL;
7183         }
7184
7185       /* Do not backtrack over a trailing optional argument that
7186          absorbed some text.  We will only fail again, with the
7187          'garbage following instruction' error message, which is
7188          probably less helpful than the current one.  */
7189       if (backtrack_index == i && backtrack_pos != str
7190           && upat[i+1] == OP_stop)
7191         {
7192           if (!inst.error)
7193             inst.error = _("syntax error");
7194           return FAIL;
7195         }
7196
7197       /* Try again, skipping the optional argument at backtrack_pos.  */
7198       str = backtrack_pos;
7199       inst.error = backtrack_error;
7200       inst.operands[backtrack_index].present = 0;
7201       i = backtrack_index;
7202       backtrack_pos = 0;
7203     }
7204
7205   /* Check that we have parsed all the arguments.  */
7206   if (*str != '\0' && !inst.error)
7207     inst.error = _("garbage following instruction");
7208
7209   return inst.error ? FAIL : SUCCESS;
7210 }
7211
7212 #undef po_char_or_fail
7213 #undef po_reg_or_fail
7214 #undef po_reg_or_goto
7215 #undef po_imm_or_fail
7216 #undef po_scalar_or_fail
7217 #undef po_barrier_or_imm
7218
7219 /* Shorthand macro for instruction encoding functions issuing errors.  */
7220 #define constraint(expr, err)                   \
7221   do                                            \
7222     {                                           \
7223       if (expr)                                 \
7224         {                                       \
7225           inst.error = err;                     \
7226           return;                               \
7227         }                                       \
7228     }                                           \
7229   while (0)
7230
7231 /* Reject "bad registers" for Thumb-2 instructions.  Many Thumb-2
7232    instructions are unpredictable if these registers are used.  This
7233    is the BadReg predicate in ARM's Thumb-2 documentation.  */
7234 #define reject_bad_reg(reg)                             \
7235   do                                                    \
7236    if (reg == REG_SP || reg == REG_PC)                  \
7237      {                                                  \
7238        inst.error = (reg == REG_SP) ? BAD_SP : BAD_PC;  \
7239        return;                                          \
7240      }                                                  \
7241   while (0)
7242
7243 /* If REG is R13 (the stack pointer), warn that its use is
7244    deprecated.  */
7245 #define warn_deprecated_sp(reg)                 \
7246   do                                            \
7247     if (warn_on_deprecated && reg == REG_SP)    \
7248        as_tsktsk (_("use of r13 is deprecated"));       \
7249   while (0)
7250
7251 /* Functions for operand encoding.  ARM, then Thumb.  */
7252
7253 #define rotate_left(v, n) (v << (n & 31) | v >> ((32 - n) & 31))
7254
7255 /* If VAL can be encoded in the immediate field of an ARM instruction,
7256    return the encoded form.  Otherwise, return FAIL.  */
7257
7258 static unsigned int
7259 encode_arm_immediate (unsigned int val)
7260 {
7261   unsigned int a, i;
7262
7263   for (i = 0; i < 32; i += 2)
7264     if ((a = rotate_left (val, i)) <= 0xff)
7265       return a | (i << 7); /* 12-bit pack: [shift-cnt,const].  */
7266
7267   return FAIL;
7268 }
7269
7270 /* If VAL can be encoded in the immediate field of a Thumb32 instruction,
7271    return the encoded form.  Otherwise, return FAIL.  */
7272 static unsigned int
7273 encode_thumb32_immediate (unsigned int val)
7274 {
7275   unsigned int a, i;
7276
7277   if (val <= 0xff)
7278     return val;
7279
7280   for (i = 1; i <= 24; i++)
7281     {
7282       a = val >> i;
7283       if ((val & ~(0xff << i)) == 0)
7284         return ((val >> i) & 0x7f) | ((32 - i) << 7);
7285     }
7286
7287   a = val & 0xff;
7288   if (val == ((a << 16) | a))
7289     return 0x100 | a;
7290   if (val == ((a << 24) | (a << 16) | (a << 8) | a))
7291     return 0x300 | a;
7292
7293   a = val & 0xff00;
7294   if (val == ((a << 16) | a))
7295     return 0x200 | (a >> 8);
7296
7297   return FAIL;
7298 }
7299 /* Encode a VFP SP or DP register number into inst.instruction.  */
7300
7301 static void
7302 encode_arm_vfp_reg (int reg, enum vfp_reg_pos pos)
7303 {
7304   if ((pos == VFP_REG_Dd || pos == VFP_REG_Dn || pos == VFP_REG_Dm)
7305       && reg > 15)
7306     {
7307       if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_d32))
7308         {
7309           if (thumb_mode)
7310             ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
7311                                     fpu_vfp_ext_d32);
7312           else
7313             ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
7314                                     fpu_vfp_ext_d32);
7315         }
7316       else
7317         {
7318           first_error (_("D register out of range for selected VFP version"));
7319           return;
7320         }
7321     }
7322
7323   switch (pos)
7324     {
7325     case VFP_REG_Sd:
7326       inst.instruction |= ((reg >> 1) << 12) | ((reg & 1) << 22);
7327       break;
7328
7329     case VFP_REG_Sn:
7330       inst.instruction |= ((reg >> 1) << 16) | ((reg & 1) << 7);
7331       break;
7332
7333     case VFP_REG_Sm:
7334       inst.instruction |= ((reg >> 1) << 0) | ((reg & 1) << 5);
7335       break;
7336
7337     case VFP_REG_Dd:
7338       inst.instruction |= ((reg & 15) << 12) | ((reg >> 4) << 22);
7339       break;
7340
7341     case VFP_REG_Dn:
7342       inst.instruction |= ((reg & 15) << 16) | ((reg >> 4) << 7);
7343       break;
7344
7345     case VFP_REG_Dm:
7346       inst.instruction |= (reg & 15) | ((reg >> 4) << 5);
7347       break;
7348
7349     default:
7350       abort ();
7351     }
7352 }
7353
7354 /* Encode a <shift> in an ARM-format instruction.  The immediate,
7355    if any, is handled by md_apply_fix.   */
7356 static void
7357 encode_arm_shift (int i)
7358 {
7359   if (inst.operands[i].shift_kind == SHIFT_RRX)
7360     inst.instruction |= SHIFT_ROR << 5;
7361   else
7362     {
7363       inst.instruction |= inst.operands[i].shift_kind << 5;
7364       if (inst.operands[i].immisreg)
7365         {
7366           inst.instruction |= SHIFT_BY_REG;
7367           inst.instruction |= inst.operands[i].imm << 8;
7368         }
7369       else
7370         inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7371     }
7372 }
7373
7374 static void
7375 encode_arm_shifter_operand (int i)
7376 {
7377   if (inst.operands[i].isreg)
7378     {
7379       inst.instruction |= inst.operands[i].reg;
7380       encode_arm_shift (i);
7381     }
7382   else
7383     {
7384       inst.instruction |= INST_IMMEDIATE;
7385       if (inst.reloc.type != BFD_RELOC_ARM_IMMEDIATE)
7386         inst.instruction |= inst.operands[i].imm;
7387     }
7388 }
7389
7390 /* Subroutine of encode_arm_addr_mode_2 and encode_arm_addr_mode_3.  */
7391 static void
7392 encode_arm_addr_mode_common (int i, bfd_boolean is_t)
7393 {
7394   /* PR 14260:
7395      Generate an error if the operand is not a register.  */
7396   constraint (!inst.operands[i].isreg,
7397               _("Instruction does not support =N addresses"));
7398
7399   inst.instruction |= inst.operands[i].reg << 16;
7400
7401   if (inst.operands[i].preind)
7402     {
7403       if (is_t)
7404         {
7405           inst.error = _("instruction does not accept preindexed addressing");
7406           return;
7407         }
7408       inst.instruction |= PRE_INDEX;
7409       if (inst.operands[i].writeback)
7410         inst.instruction |= WRITE_BACK;
7411
7412     }
7413   else if (inst.operands[i].postind)
7414     {
7415       gas_assert (inst.operands[i].writeback);
7416       if (is_t)
7417         inst.instruction |= WRITE_BACK;
7418     }
7419   else /* unindexed - only for coprocessor */
7420     {
7421       inst.error = _("instruction does not accept unindexed addressing");
7422       return;
7423     }
7424
7425   if (((inst.instruction & WRITE_BACK) || !(inst.instruction & PRE_INDEX))
7426       && (((inst.instruction & 0x000f0000) >> 16)
7427           == ((inst.instruction & 0x0000f000) >> 12)))
7428     as_warn ((inst.instruction & LOAD_BIT)
7429              ? _("destination register same as write-back base")
7430              : _("source register same as write-back base"));
7431 }
7432
7433 /* inst.operands[i] was set up by parse_address.  Encode it into an
7434    ARM-format mode 2 load or store instruction.  If is_t is true,
7435    reject forms that cannot be used with a T instruction (i.e. not
7436    post-indexed).  */
7437 static void
7438 encode_arm_addr_mode_2 (int i, bfd_boolean is_t)
7439 {
7440   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
7441
7442   encode_arm_addr_mode_common (i, is_t);
7443
7444   if (inst.operands[i].immisreg)
7445     {
7446       constraint ((inst.operands[i].imm == REG_PC
7447                    || (is_pc && inst.operands[i].writeback)),
7448                   BAD_PC_ADDRESSING);
7449       inst.instruction |= INST_IMMEDIATE;  /* yes, this is backwards */
7450       inst.instruction |= inst.operands[i].imm;
7451       if (!inst.operands[i].negative)
7452         inst.instruction |= INDEX_UP;
7453       if (inst.operands[i].shifted)
7454         {
7455           if (inst.operands[i].shift_kind == SHIFT_RRX)
7456             inst.instruction |= SHIFT_ROR << 5;
7457           else
7458             {
7459               inst.instruction |= inst.operands[i].shift_kind << 5;
7460               inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
7461             }
7462         }
7463     }
7464   else /* immediate offset in inst.reloc */
7465     {
7466       if (is_pc && !inst.reloc.pc_rel)
7467         {
7468           const bfd_boolean is_load = ((inst.instruction & LOAD_BIT) != 0);
7469
7470           /* If is_t is TRUE, it's called from do_ldstt.  ldrt/strt
7471              cannot use PC in addressing.
7472              PC cannot be used in writeback addressing, either.  */
7473           constraint ((is_t || inst.operands[i].writeback),
7474                       BAD_PC_ADDRESSING);
7475
7476           /* Use of PC in str is deprecated for ARMv7.  */
7477           if (warn_on_deprecated
7478               && !is_load
7479               && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7))
7480             as_tsktsk (_("use of PC in this instruction is deprecated"));
7481         }
7482
7483       if (inst.reloc.type == BFD_RELOC_UNUSED)
7484         {
7485           /* Prefer + for zero encoded value.  */
7486           if (!inst.operands[i].negative)
7487             inst.instruction |= INDEX_UP;
7488           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM;
7489         }
7490     }
7491 }
7492
7493 /* inst.operands[i] was set up by parse_address.  Encode it into an
7494    ARM-format mode 3 load or store instruction.  Reject forms that
7495    cannot be used with such instructions.  If is_t is true, reject
7496    forms that cannot be used with a T instruction (i.e. not
7497    post-indexed).  */
7498 static void
7499 encode_arm_addr_mode_3 (int i, bfd_boolean is_t)
7500 {
7501   if (inst.operands[i].immisreg && inst.operands[i].shifted)
7502     {
7503       inst.error = _("instruction does not accept scaled register index");
7504       return;
7505     }
7506
7507   encode_arm_addr_mode_common (i, is_t);
7508
7509   if (inst.operands[i].immisreg)
7510     {
7511       constraint ((inst.operands[i].imm == REG_PC
7512                    || (is_t && inst.operands[i].reg == REG_PC)),
7513                   BAD_PC_ADDRESSING);
7514       constraint (inst.operands[i].reg == REG_PC && inst.operands[i].writeback,
7515                   BAD_PC_WRITEBACK);
7516       inst.instruction |= inst.operands[i].imm;
7517       if (!inst.operands[i].negative)
7518         inst.instruction |= INDEX_UP;
7519     }
7520   else /* immediate offset in inst.reloc */
7521     {
7522       constraint ((inst.operands[i].reg == REG_PC && !inst.reloc.pc_rel
7523                    && inst.operands[i].writeback),
7524                   BAD_PC_WRITEBACK);
7525       inst.instruction |= HWOFFSET_IMM;
7526       if (inst.reloc.type == BFD_RELOC_UNUSED)
7527         {
7528           /* Prefer + for zero encoded value.  */
7529           if (!inst.operands[i].negative)
7530             inst.instruction |= INDEX_UP;
7531
7532           inst.reloc.type = BFD_RELOC_ARM_OFFSET_IMM8;
7533         }
7534     }
7535 }
7536
7537 /* Write immediate bits [7:0] to the following locations:
7538
7539   |28/24|23     19|18 16|15                    4|3     0|
7540   |  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|
7541
7542   This function is used by VMOV/VMVN/VORR/VBIC.  */
7543
7544 static void
7545 neon_write_immbits (unsigned immbits)
7546 {
7547   inst.instruction |= immbits & 0xf;
7548   inst.instruction |= ((immbits >> 4) & 0x7) << 16;
7549   inst.instruction |= ((immbits >> 7) & 0x1) << (thumb_mode ? 28 : 24);
7550 }
7551
7552 /* Invert low-order SIZE bits of XHI:XLO.  */
7553
7554 static void
7555 neon_invert_size (unsigned *xlo, unsigned *xhi, int size)
7556 {
7557   unsigned immlo = xlo ? *xlo : 0;
7558   unsigned immhi = xhi ? *xhi : 0;
7559
7560   switch (size)
7561     {
7562     case 8:
7563       immlo = (~immlo) & 0xff;
7564       break;
7565
7566     case 16:
7567       immlo = (~immlo) & 0xffff;
7568       break;
7569
7570     case 64:
7571       immhi = (~immhi) & 0xffffffff;
7572       /* fall through.  */
7573
7574     case 32:
7575       immlo = (~immlo) & 0xffffffff;
7576       break;
7577
7578     default:
7579       abort ();
7580     }
7581
7582   if (xlo)
7583     *xlo = immlo;
7584
7585   if (xhi)
7586     *xhi = immhi;
7587 }
7588
7589 /* True if IMM has form 0bAAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD for bits
7590    A, B, C, D.  */
7591
7592 static int
7593 neon_bits_same_in_bytes (unsigned imm)
7594 {
7595   return ((imm & 0x000000ff) == 0 || (imm & 0x000000ff) == 0x000000ff)
7596          && ((imm & 0x0000ff00) == 0 || (imm & 0x0000ff00) == 0x0000ff00)
7597          && ((imm & 0x00ff0000) == 0 || (imm & 0x00ff0000) == 0x00ff0000)
7598          && ((imm & 0xff000000) == 0 || (imm & 0xff000000) == 0xff000000);
7599 }
7600
7601 /* For immediate of above form, return 0bABCD.  */
7602
7603 static unsigned
7604 neon_squash_bits (unsigned imm)
7605 {
7606   return (imm & 0x01) | ((imm & 0x0100) >> 7) | ((imm & 0x010000) >> 14)
7607          | ((imm & 0x01000000) >> 21);
7608 }
7609
7610 /* Compress quarter-float representation to 0b...000 abcdefgh.  */
7611
7612 static unsigned
7613 neon_qfloat_bits (unsigned imm)
7614 {
7615   return ((imm >> 19) & 0x7f) | ((imm >> 24) & 0x80);
7616 }
7617
7618 /* Returns CMODE. IMMBITS [7:0] is set to bits suitable for inserting into
7619    the instruction. *OP is passed as the initial value of the op field, and
7620    may be set to a different value depending on the constant (i.e.
7621    "MOV I64, 0bAAAAAAAABBBB..." which uses OP = 1 despite being MOV not
7622    MVN).  If the immediate looks like a repeated pattern then also
7623    try smaller element sizes.  */
7624
7625 static int
7626 neon_cmode_for_move_imm (unsigned immlo, unsigned immhi, int float_p,
7627                          unsigned *immbits, int *op, int size,
7628                          enum neon_el_type type)
7629 {
7630   /* Only permit float immediates (including 0.0/-0.0) if the operand type is
7631      float.  */
7632   if (type == NT_float && !float_p)
7633     return FAIL;
7634
7635   if (type == NT_float && is_quarter_float (immlo) && immhi == 0)
7636     {
7637       if (size != 32 || *op == 1)
7638         return FAIL;
7639       *immbits = neon_qfloat_bits (immlo);
7640       return 0xf;
7641     }
7642
7643   if (size == 64)
7644     {
7645       if (neon_bits_same_in_bytes (immhi)
7646           && neon_bits_same_in_bytes (immlo))
7647         {
7648           if (*op == 1)
7649             return FAIL;
7650           *immbits = (neon_squash_bits (immhi) << 4)
7651                      | neon_squash_bits (immlo);
7652           *op = 1;
7653           return 0xe;
7654         }
7655
7656       if (immhi != immlo)
7657         return FAIL;
7658     }
7659
7660   if (size >= 32)
7661     {
7662       if (immlo == (immlo & 0x000000ff))
7663         {
7664           *immbits = immlo;
7665           return 0x0;
7666         }
7667       else if (immlo == (immlo & 0x0000ff00))
7668         {
7669           *immbits = immlo >> 8;
7670           return 0x2;
7671         }
7672       else if (immlo == (immlo & 0x00ff0000))
7673         {
7674           *immbits = immlo >> 16;
7675           return 0x4;
7676         }
7677       else if (immlo == (immlo & 0xff000000))
7678         {
7679           *immbits = immlo >> 24;
7680           return 0x6;
7681         }
7682       else if (immlo == ((immlo & 0x0000ff00) | 0x000000ff))
7683         {
7684           *immbits = (immlo >> 8) & 0xff;
7685           return 0xc;
7686         }
7687       else if (immlo == ((immlo & 0x00ff0000) | 0x0000ffff))
7688         {
7689           *immbits = (immlo >> 16) & 0xff;
7690           return 0xd;
7691         }
7692
7693       if ((immlo & 0xffff) != (immlo >> 16))
7694         return FAIL;
7695       immlo &= 0xffff;
7696     }
7697
7698   if (size >= 16)
7699     {
7700       if (immlo == (immlo & 0x000000ff))
7701         {
7702           *immbits = immlo;
7703           return 0x8;
7704         }
7705       else if (immlo == (immlo & 0x0000ff00))
7706         {
7707           *immbits = immlo >> 8;
7708           return 0xa;
7709         }
7710
7711       if ((immlo & 0xff) != (immlo >> 8))
7712         return FAIL;
7713       immlo &= 0xff;
7714     }
7715
7716   if (immlo == (immlo & 0x000000ff))
7717     {
7718       /* Don't allow MVN with 8-bit immediate.  */
7719       if (*op == 1)
7720         return FAIL;
7721       *immbits = immlo;
7722       return 0xe;
7723     }
7724
7725   return FAIL;
7726 }
7727
7728 #if defined BFD_HOST_64_BIT
7729 /* Returns TRUE if double precision value V may be cast
7730    to single precision without loss of accuracy.  */
7731
7732 static bfd_boolean
7733 is_double_a_single (bfd_int64_t v)
7734 {
7735   int exp = (int)((v >> 52) & 0x7FF);
7736   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7737
7738   return (exp == 0 || exp == 0x7FF
7739           || (exp >= 1023 - 126 && exp <= 1023 + 127))
7740     && (mantissa & 0x1FFFFFFFl) == 0;
7741 }
7742
7743 /* Returns a double precision value casted to single precision
7744    (ignoring the least significant bits in exponent and mantissa).  */
7745
7746 static int
7747 double_to_single (bfd_int64_t v)
7748 {
7749   int sign = (int) ((v >> 63) & 1l);
7750   int exp = (int) ((v >> 52) & 0x7FF);
7751   bfd_int64_t mantissa = (v & (bfd_int64_t)0xFFFFFFFFFFFFFULL);
7752
7753   if (exp == 0x7FF)
7754     exp = 0xFF;
7755   else
7756     {
7757       exp = exp - 1023 + 127;
7758       if (exp >= 0xFF)
7759         {
7760           /* Infinity.  */
7761           exp = 0x7F;
7762           mantissa = 0;
7763         }
7764       else if (exp < 0)
7765         {
7766           /* No denormalized numbers.  */
7767           exp = 0;
7768           mantissa = 0;
7769         }
7770     }
7771   mantissa >>= 29;
7772   return (sign << 31) | (exp << 23) | mantissa;
7773 }
7774 #endif /* BFD_HOST_64_BIT */
7775
7776 enum lit_type
7777 {
7778   CONST_THUMB,
7779   CONST_ARM,
7780   CONST_VEC
7781 };
7782
7783 static void do_vfp_nsyn_opcode (const char *);
7784
7785 /* inst.reloc.exp describes an "=expr" load pseudo-operation.
7786    Determine whether it can be performed with a move instruction; if
7787    it can, convert inst.instruction to that move instruction and
7788    return TRUE; if it can't, convert inst.instruction to a literal-pool
7789    load and return FALSE.  If this is not a valid thing to do in the
7790    current context, set inst.error and return TRUE.
7791
7792    inst.operands[i] describes the destination register.  */
7793
7794 static bfd_boolean
7795 move_or_literal_pool (int i, enum lit_type t, bfd_boolean mode_3)
7796 {
7797   unsigned long tbit;
7798   bfd_boolean thumb_p = (t == CONST_THUMB);
7799   bfd_boolean arm_p   = (t == CONST_ARM);
7800
7801   if (thumb_p)
7802     tbit = (inst.instruction > 0xffff) ? THUMB2_LOAD_BIT : THUMB_LOAD_BIT;
7803   else
7804     tbit = LOAD_BIT;
7805
7806   if ((inst.instruction & tbit) == 0)
7807     {
7808       inst.error = _("invalid pseudo operation");
7809       return TRUE;
7810     }
7811
7812   if (inst.reloc.exp.X_op != O_constant
7813       && inst.reloc.exp.X_op != O_symbol
7814       && inst.reloc.exp.X_op != O_big)
7815     {
7816       inst.error = _("constant expression expected");
7817       return TRUE;
7818     }
7819
7820   if (inst.reloc.exp.X_op == O_constant
7821       || inst.reloc.exp.X_op == O_big)
7822     {
7823 #if defined BFD_HOST_64_BIT
7824       bfd_int64_t v;
7825 #else
7826       offsetT v;
7827 #endif
7828       if (inst.reloc.exp.X_op == O_big)
7829         {
7830           LITTLENUM_TYPE w[X_PRECISION];
7831           LITTLENUM_TYPE * l;
7832
7833           if (inst.reloc.exp.X_add_number == -1)
7834             {
7835               gen_to_words (w, X_PRECISION, E_PRECISION);
7836               l = w;
7837               /* FIXME: Should we check words w[2..5] ?  */
7838             }
7839           else
7840             l = generic_bignum;
7841
7842 #if defined BFD_HOST_64_BIT
7843           v =
7844             ((((((((bfd_int64_t) l[3] & LITTLENUM_MASK)
7845                   << LITTLENUM_NUMBER_OF_BITS)
7846                  | ((bfd_int64_t) l[2] & LITTLENUM_MASK))
7847                 << LITTLENUM_NUMBER_OF_BITS)
7848                | ((bfd_int64_t) l[1] & LITTLENUM_MASK))
7849               << LITTLENUM_NUMBER_OF_BITS)
7850              | ((bfd_int64_t) l[0] & LITTLENUM_MASK));
7851 #else
7852           v = ((l[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
7853             |  (l[0] & LITTLENUM_MASK);
7854 #endif
7855         }
7856       else
7857         v = inst.reloc.exp.X_add_number;
7858
7859       if (!inst.operands[i].issingle)
7860         {
7861           if (thumb_p)
7862             {
7863               /* This can be encoded only for a low register.  */
7864               if ((v & ~0xFF) == 0 && (inst.operands[i].reg < 8))
7865                 {
7866                   /* This can be done with a mov(1) instruction.  */
7867                   inst.instruction = T_OPCODE_MOV_I8 | (inst.operands[i].reg << 8);
7868                   inst.instruction |= v;
7869                   return TRUE;
7870                 }
7871
7872               if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
7873                 {
7874                   /* Check if on thumb2 it can be done with a mov.w or mvn.w
7875                      instruction.  */
7876                   unsigned int newimm;
7877                   bfd_boolean isNegated;
7878
7879                   newimm = encode_thumb32_immediate (v);
7880                   if (newimm != (unsigned int) FAIL)
7881                     isNegated = FALSE;
7882                   else
7883                     {
7884                       newimm = encode_thumb32_immediate (~v);
7885                       if (newimm != (unsigned int) FAIL)
7886                         isNegated = TRUE;
7887                     }
7888
7889                   if (newimm != (unsigned int) FAIL)
7890                     {
7891                       inst.instruction = (0xf04f0000
7892                                           | (inst.operands[i].reg << 8));
7893                       inst.instruction |= (isNegated ? 0x200000 : 0);
7894                       inst.instruction |= (newimm & 0x800) << 15;
7895                       inst.instruction |= (newimm & 0x700) << 4;
7896                       inst.instruction |= (newimm & 0x0ff);
7897                       return TRUE;
7898                     }
7899                   else if ((v & ~0xFFFF) == 0)
7900                     {
7901                       /* The number can be loaded with a mov.w instruction.  */
7902                       int imm = v & 0xFFFF;
7903
7904                       inst.instruction = 0xf2400000;  /* MOVW.  */
7905                       inst.instruction |= (inst.operands[i].reg << 8);
7906                       inst.instruction |= (imm & 0xf000) << 4;
7907                       inst.instruction |= (imm & 0x0800) << 15;
7908                       inst.instruction |= (imm & 0x0700) << 4;
7909                       inst.instruction |= (imm & 0x00ff);
7910                       return TRUE;
7911                     }
7912                 }
7913             }
7914           else if (arm_p)
7915             {
7916               int value = encode_arm_immediate (v);
7917
7918               if (value != FAIL)
7919                 {
7920                   /* This can be done with a mov instruction.  */
7921                   inst.instruction &= LITERAL_MASK;
7922                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MOV << DATA_OP_SHIFT);
7923                   inst.instruction |= value & 0xfff;
7924                   return TRUE;
7925                 }
7926
7927               value = encode_arm_immediate (~ v);
7928               if (value != FAIL)
7929                 {
7930                   /* This can be done with a mvn instruction.  */
7931                   inst.instruction &= LITERAL_MASK;
7932                   inst.instruction |= INST_IMMEDIATE | (OPCODE_MVN << DATA_OP_SHIFT);
7933                   inst.instruction |= value & 0xfff;
7934                   return TRUE;
7935                 }
7936             }
7937           else if (t == CONST_VEC)
7938             {
7939               int op = 0;
7940               unsigned immbits = 0;
7941               unsigned immlo = inst.operands[1].imm;
7942               unsigned immhi = inst.operands[1].regisimm
7943                 ? inst.operands[1].reg
7944                 : inst.reloc.exp.X_unsigned
7945                 ? 0
7946                 : ((bfd_int64_t)((int) immlo)) >> 32;
7947               int cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
7948                                                    &op, 64, NT_invtype);
7949
7950               if (cmode == FAIL)
7951                 {
7952                   neon_invert_size (&immlo, &immhi, 64);
7953                   op = !op;
7954                   cmode = neon_cmode_for_move_imm (immlo, immhi, FALSE, &immbits,
7955                                                    &op, 64, NT_invtype);
7956                 }
7957
7958               if (cmode != FAIL)
7959                 {
7960                   inst.instruction = (inst.instruction & VLDR_VMOV_SAME)
7961                     | (1 << 23)
7962                     | (cmode << 8)
7963                     | (op << 5)
7964                     | (1 << 4);
7965
7966                   /* Fill other bits in vmov encoding for both thumb and arm.  */
7967                   if (thumb_mode)
7968                     inst.instruction |= (0x7U << 29) | (0xF << 24);
7969                   else
7970                     inst.instruction |= (0xFU << 28) | (0x1 << 25);
7971                   neon_write_immbits (immbits);
7972                   return TRUE;
7973                 }
7974             }
7975         }
7976
7977       if (t == CONST_VEC)
7978         {
7979           /* Check if vldr Rx, =constant could be optimized to vmov Rx, #constant.  */
7980           if (inst.operands[i].issingle
7981               && is_quarter_float (inst.operands[1].imm)
7982               && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3xd))
7983             {
7984               inst.operands[1].imm =
7985                 neon_qfloat_bits (v);
7986               do_vfp_nsyn_opcode ("fconsts");
7987               return TRUE;
7988             }
7989
7990           /* If our host does not support a 64-bit type then we cannot perform
7991              the following optimization.  This mean that there will be a
7992              discrepancy between the output produced by an assembler built for
7993              a 32-bit-only host and the output produced from a 64-bit host, but
7994              this cannot be helped.  */
7995 #if defined BFD_HOST_64_BIT
7996           else if (!inst.operands[1].issingle
7997                    && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v3))
7998             {
7999               if (is_double_a_single (v)
8000                   && is_quarter_float (double_to_single (v)))
8001                 {
8002                   inst.operands[1].imm =
8003                     neon_qfloat_bits (double_to_single (v));
8004                   do_vfp_nsyn_opcode ("fconstd");
8005                   return TRUE;
8006                 }
8007             }
8008 #endif
8009         }
8010     }
8011
8012   if (add_to_lit_pool ((!inst.operands[i].isvec
8013                         || inst.operands[i].issingle) ? 4 : 8) == FAIL)
8014     return TRUE;
8015
8016   inst.operands[1].reg = REG_PC;
8017   inst.operands[1].isreg = 1;
8018   inst.operands[1].preind = 1;
8019   inst.reloc.pc_rel = 1;
8020   inst.reloc.type = (thumb_p
8021                      ? BFD_RELOC_ARM_THUMB_OFFSET
8022                      : (mode_3
8023                         ? BFD_RELOC_ARM_HWLITERAL
8024                         : BFD_RELOC_ARM_LITERAL));
8025   return FALSE;
8026 }
8027
8028 /* inst.operands[i] was set up by parse_address.  Encode it into an
8029    ARM-format instruction.  Reject all forms which cannot be encoded
8030    into a coprocessor load/store instruction.  If wb_ok is false,
8031    reject use of writeback; if unind_ok is false, reject use of
8032    unindexed addressing.  If reloc_override is not 0, use it instead
8033    of BFD_ARM_CP_OFF_IMM, unless the initial relocation is a group one
8034    (in which case it is preserved).  */
8035
8036 static int
8037 encode_arm_cp_address (int i, int wb_ok, int unind_ok, int reloc_override)
8038 {
8039   if (!inst.operands[i].isreg)
8040     {
8041       /* PR 18256 */
8042       if (! inst.operands[0].isvec)
8043         {
8044           inst.error = _("invalid co-processor operand");
8045           return FAIL;
8046         }
8047       if (move_or_literal_pool (0, CONST_VEC, /*mode_3=*/FALSE))
8048         return SUCCESS;
8049     }
8050
8051   inst.instruction |= inst.operands[i].reg << 16;
8052
8053   gas_assert (!(inst.operands[i].preind && inst.operands[i].postind));
8054
8055   if (!inst.operands[i].preind && !inst.operands[i].postind) /* unindexed */
8056     {
8057       gas_assert (!inst.operands[i].writeback);
8058       if (!unind_ok)
8059         {
8060           inst.error = _("instruction does not support unindexed addressing");
8061           return FAIL;
8062         }
8063       inst.instruction |= inst.operands[i].imm;
8064       inst.instruction |= INDEX_UP;
8065       return SUCCESS;
8066     }
8067
8068   if (inst.operands[i].preind)
8069     inst.instruction |= PRE_INDEX;
8070
8071   if (inst.operands[i].writeback)
8072     {
8073       if (inst.operands[i].reg == REG_PC)
8074         {
8075           inst.error = _("pc may not be used with write-back");
8076           return FAIL;
8077         }
8078       if (!wb_ok)
8079         {
8080           inst.error = _("instruction does not support writeback");
8081           return FAIL;
8082         }
8083       inst.instruction |= WRITE_BACK;
8084     }
8085
8086   if (reloc_override)
8087     inst.reloc.type = (bfd_reloc_code_real_type) reloc_override;
8088   else if ((inst.reloc.type < BFD_RELOC_ARM_ALU_PC_G0_NC
8089             || inst.reloc.type > BFD_RELOC_ARM_LDC_SB_G2)
8090            && inst.reloc.type != BFD_RELOC_ARM_LDR_PC_G0)
8091     {
8092       if (thumb_mode)
8093         inst.reloc.type = BFD_RELOC_ARM_T32_CP_OFF_IMM;
8094       else
8095         inst.reloc.type = BFD_RELOC_ARM_CP_OFF_IMM;
8096     }
8097
8098   /* Prefer + for zero encoded value.  */
8099   if (!inst.operands[i].negative)
8100     inst.instruction |= INDEX_UP;
8101
8102   return SUCCESS;
8103 }
8104
8105 /* Functions for instruction encoding, sorted by sub-architecture.
8106    First some generics; their names are taken from the conventional
8107    bit positions for register arguments in ARM format instructions.  */
8108
8109 static void
8110 do_noargs (void)
8111 {
8112 }
8113
8114 static void
8115 do_rd (void)
8116 {
8117   inst.instruction |= inst.operands[0].reg << 12;
8118 }
8119
8120 static void
8121 do_rd_rm (void)
8122 {
8123   inst.instruction |= inst.operands[0].reg << 12;
8124   inst.instruction |= inst.operands[1].reg;
8125 }
8126
8127 static void
8128 do_rm_rn (void)
8129 {
8130   inst.instruction |= inst.operands[0].reg;
8131   inst.instruction |= inst.operands[1].reg << 16;
8132 }
8133
8134 static void
8135 do_rd_rn (void)
8136 {
8137   inst.instruction |= inst.operands[0].reg << 12;
8138   inst.instruction |= inst.operands[1].reg << 16;
8139 }
8140
8141 static void
8142 do_rn_rd (void)
8143 {
8144   inst.instruction |= inst.operands[0].reg << 16;
8145   inst.instruction |= inst.operands[1].reg << 12;
8146 }
8147
8148 static bfd_boolean
8149 check_obsolete (const arm_feature_set *feature, const char *msg)
8150 {
8151   if (ARM_CPU_IS_ANY (cpu_variant))
8152     {
8153       as_tsktsk ("%s", msg);
8154       return TRUE;
8155     }
8156   else if (ARM_CPU_HAS_FEATURE (cpu_variant, *feature))
8157     {
8158       as_bad ("%s", msg);
8159       return TRUE;
8160     }
8161
8162   return FALSE;
8163 }
8164
8165 static void
8166 do_rd_rm_rn (void)
8167 {
8168   unsigned Rn = inst.operands[2].reg;
8169   /* Enforce restrictions on SWP instruction.  */
8170   if ((inst.instruction & 0x0fbfffff) == 0x01000090)
8171     {
8172       constraint (Rn == inst.operands[0].reg || Rn == inst.operands[1].reg,
8173                   _("Rn must not overlap other operands"));
8174
8175       /* SWP{b} is obsolete for ARMv8-A, and deprecated for ARMv6* and ARMv7.
8176        */
8177       if (!check_obsolete (&arm_ext_v8,
8178                            _("swp{b} use is obsoleted for ARMv8 and later"))
8179           && warn_on_deprecated
8180           && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6))
8181         as_tsktsk (_("swp{b} use is deprecated for ARMv6 and ARMv7"));
8182     }
8183
8184   inst.instruction |= inst.operands[0].reg << 12;
8185   inst.instruction |= inst.operands[1].reg;
8186   inst.instruction |= Rn << 16;
8187 }
8188
8189 static void
8190 do_rd_rn_rm (void)
8191 {
8192   inst.instruction |= inst.operands[0].reg << 12;
8193   inst.instruction |= inst.operands[1].reg << 16;
8194   inst.instruction |= inst.operands[2].reg;
8195 }
8196
8197 static void
8198 do_rm_rd_rn (void)
8199 {
8200   constraint ((inst.operands[2].reg == REG_PC), BAD_PC);
8201   constraint (((inst.reloc.exp.X_op != O_constant
8202                 && inst.reloc.exp.X_op != O_illegal)
8203                || inst.reloc.exp.X_add_number != 0),
8204               BAD_ADDR_MODE);
8205   inst.instruction |= inst.operands[0].reg;
8206   inst.instruction |= inst.operands[1].reg << 12;
8207   inst.instruction |= inst.operands[2].reg << 16;
8208 }
8209
8210 static void
8211 do_imm0 (void)
8212 {
8213   inst.instruction |= inst.operands[0].imm;
8214 }
8215
8216 static void
8217 do_rd_cpaddr (void)
8218 {
8219   inst.instruction |= inst.operands[0].reg << 12;
8220   encode_arm_cp_address (1, TRUE, TRUE, 0);
8221 }
8222
8223 /* ARM instructions, in alphabetical order by function name (except
8224    that wrapper functions appear immediately after the function they
8225    wrap).  */
8226
8227 /* This is a pseudo-op of the form "adr rd, label" to be converted
8228    into a relative address of the form "add rd, pc, #label-.-8".  */
8229
8230 static void
8231 do_adr (void)
8232 {
8233   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8234
8235   /* Frag hacking will turn this into a sub instruction if the offset turns
8236      out to be negative.  */
8237   inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
8238   inst.reloc.pc_rel = 1;
8239   inst.reloc.exp.X_add_number -= 8;
8240 }
8241
8242 /* This is a pseudo-op of the form "adrl rd, label" to be converted
8243    into a relative address of the form:
8244    add rd, pc, #low(label-.-8)"
8245    add rd, rd, #high(label-.-8)"  */
8246
8247 static void
8248 do_adrl (void)
8249 {
8250   inst.instruction |= (inst.operands[0].reg << 12);  /* Rd */
8251
8252   /* Frag hacking will turn this into a sub instruction if the offset turns
8253      out to be negative.  */
8254   inst.reloc.type              = BFD_RELOC_ARM_ADRL_IMMEDIATE;
8255   inst.reloc.pc_rel            = 1;
8256   inst.size                    = INSN_SIZE * 2;
8257   inst.reloc.exp.X_add_number -= 8;
8258 }
8259
8260 static void
8261 do_arit (void)
8262 {
8263   if (!inst.operands[1].present)
8264     inst.operands[1].reg = inst.operands[0].reg;
8265   inst.instruction |= inst.operands[0].reg << 12;
8266   inst.instruction |= inst.operands[1].reg << 16;
8267   encode_arm_shifter_operand (2);
8268 }
8269
8270 static void
8271 do_barrier (void)
8272 {
8273   if (inst.operands[0].present)
8274     inst.instruction |= inst.operands[0].imm;
8275   else
8276     inst.instruction |= 0xf;
8277 }
8278
8279 static void
8280 do_bfc (void)
8281 {
8282   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
8283   constraint (msb > 32, _("bit-field extends past end of register"));
8284   /* The instruction encoding stores the LSB and MSB,
8285      not the LSB and width.  */
8286   inst.instruction |= inst.operands[0].reg << 12;
8287   inst.instruction |= inst.operands[1].imm << 7;
8288   inst.instruction |= (msb - 1) << 16;
8289 }
8290
8291 static void
8292 do_bfi (void)
8293 {
8294   unsigned int msb;
8295
8296   /* #0 in second position is alternative syntax for bfc, which is
8297      the same instruction but with REG_PC in the Rm field.  */
8298   if (!inst.operands[1].isreg)
8299     inst.operands[1].reg = REG_PC;
8300
8301   msb = inst.operands[2].imm + inst.operands[3].imm;
8302   constraint (msb > 32, _("bit-field extends past end of register"));
8303   /* The instruction encoding stores the LSB and MSB,
8304      not the LSB and width.  */
8305   inst.instruction |= inst.operands[0].reg << 12;
8306   inst.instruction |= inst.operands[1].reg;
8307   inst.instruction |= inst.operands[2].imm << 7;
8308   inst.instruction |= (msb - 1) << 16;
8309 }
8310
8311 static void
8312 do_bfx (void)
8313 {
8314   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
8315               _("bit-field extends past end of register"));
8316   inst.instruction |= inst.operands[0].reg << 12;
8317   inst.instruction |= inst.operands[1].reg;
8318   inst.instruction |= inst.operands[2].imm << 7;
8319   inst.instruction |= (inst.operands[3].imm - 1) << 16;
8320 }
8321
8322 /* ARM V5 breakpoint instruction (argument parse)
8323      BKPT <16 bit unsigned immediate>
8324      Instruction is not conditional.
8325         The bit pattern given in insns[] has the COND_ALWAYS condition,
8326         and it is an error if the caller tried to override that.  */
8327
8328 static void
8329 do_bkpt (void)
8330 {
8331   /* Top 12 of 16 bits to bits 19:8.  */
8332   inst.instruction |= (inst.operands[0].imm & 0xfff0) << 4;
8333
8334   /* Bottom 4 of 16 bits to bits 3:0.  */
8335   inst.instruction |= inst.operands[0].imm & 0xf;
8336 }
8337
8338 static void
8339 encode_branch (int default_reloc)
8340 {
8341   if (inst.operands[0].hasreloc)
8342     {
8343       constraint (inst.operands[0].imm != BFD_RELOC_ARM_PLT32
8344                   && inst.operands[0].imm != BFD_RELOC_ARM_TLS_CALL,
8345                   _("the only valid suffixes here are '(plt)' and '(tlscall)'"));
8346       inst.reloc.type = inst.operands[0].imm == BFD_RELOC_ARM_PLT32
8347         ? BFD_RELOC_ARM_PLT32
8348         : thumb_mode ? BFD_RELOC_ARM_THM_TLS_CALL : BFD_RELOC_ARM_TLS_CALL;
8349     }
8350   else
8351     inst.reloc.type = (bfd_reloc_code_real_type) default_reloc;
8352   inst.reloc.pc_rel = 1;
8353 }
8354
8355 static void
8356 do_branch (void)
8357 {
8358 #ifdef OBJ_ELF
8359   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8360     encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8361   else
8362 #endif
8363     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8364 }
8365
8366 static void
8367 do_bl (void)
8368 {
8369 #ifdef OBJ_ELF
8370   if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
8371     {
8372       if (inst.cond == COND_ALWAYS)
8373         encode_branch (BFD_RELOC_ARM_PCREL_CALL);
8374       else
8375         encode_branch (BFD_RELOC_ARM_PCREL_JUMP);
8376     }
8377   else
8378 #endif
8379     encode_branch (BFD_RELOC_ARM_PCREL_BRANCH);
8380 }
8381
8382 /* ARM V5 branch-link-exchange instruction (argument parse)
8383      BLX <target_addr>          ie BLX(1)
8384      BLX{<condition>} <Rm>      ie BLX(2)
8385    Unfortunately, there are two different opcodes for this mnemonic.
8386    So, the insns[].value is not used, and the code here zaps values
8387         into inst.instruction.
8388    Also, the <target_addr> can be 25 bits, hence has its own reloc.  */
8389
8390 static void
8391 do_blx (void)
8392 {
8393   if (inst.operands[0].isreg)
8394     {
8395       /* Arg is a register; the opcode provided by insns[] is correct.
8396          It is not illegal to do "blx pc", just useless.  */
8397       if (inst.operands[0].reg == REG_PC)
8398         as_tsktsk (_("use of r15 in blx in ARM mode is not really useful"));
8399
8400       inst.instruction |= inst.operands[0].reg;
8401     }
8402   else
8403     {
8404       /* Arg is an address; this instruction cannot be executed
8405          conditionally, and the opcode must be adjusted.
8406          We retain the BFD_RELOC_ARM_PCREL_BLX till the very end
8407          where we generate out a BFD_RELOC_ARM_PCREL_CALL instead.  */
8408       constraint (inst.cond != COND_ALWAYS, BAD_COND);
8409       inst.instruction = 0xfa000000;
8410       encode_branch (BFD_RELOC_ARM_PCREL_BLX);
8411     }
8412 }
8413
8414 static void
8415 do_bx (void)
8416 {
8417   bfd_boolean want_reloc;
8418
8419   if (inst.operands[0].reg == REG_PC)
8420     as_tsktsk (_("use of r15 in bx in ARM mode is not really useful"));
8421
8422   inst.instruction |= inst.operands[0].reg;
8423   /* Output R_ARM_V4BX relocations if is an EABI object that looks like
8424      it is for ARMv4t or earlier.  */
8425   want_reloc = !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5);
8426   if (object_arch && !ARM_CPU_HAS_FEATURE (*object_arch, arm_ext_v5))
8427       want_reloc = TRUE;
8428
8429 #ifdef OBJ_ELF
8430   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
8431 #endif
8432     want_reloc = FALSE;
8433
8434   if (want_reloc)
8435     inst.reloc.type = BFD_RELOC_ARM_V4BX;
8436 }
8437
8438
8439 /* ARM v5TEJ.  Jump to Jazelle code.  */
8440
8441 static void
8442 do_bxj (void)
8443 {
8444   if (inst.operands[0].reg == REG_PC)
8445     as_tsktsk (_("use of r15 in bxj is not really useful"));
8446
8447   inst.instruction |= inst.operands[0].reg;
8448 }
8449
8450 /* Co-processor data operation:
8451       CDP{cond} <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}
8452       CDP2      <coproc>, <opcode_1>, <CRd>, <CRn>, <CRm>{, <opcode_2>}  */
8453 static void
8454 do_cdp (void)
8455 {
8456   inst.instruction |= inst.operands[0].reg << 8;
8457   inst.instruction |= inst.operands[1].imm << 20;
8458   inst.instruction |= inst.operands[2].reg << 12;
8459   inst.instruction |= inst.operands[3].reg << 16;
8460   inst.instruction |= inst.operands[4].reg;
8461   inst.instruction |= inst.operands[5].imm << 5;
8462 }
8463
8464 static void
8465 do_cmp (void)
8466 {
8467   inst.instruction |= inst.operands[0].reg << 16;
8468   encode_arm_shifter_operand (1);
8469 }
8470
8471 /* Transfer between coprocessor and ARM registers.
8472    MRC{cond} <coproc>, <opcode_1>, <Rd>, <CRn>, <CRm>{, <opcode_2>}
8473    MRC2
8474    MCR{cond}
8475    MCR2
8476
8477    No special properties.  */
8478
8479 struct deprecated_coproc_regs_s
8480 {
8481   unsigned cp;
8482   int opc1;
8483   unsigned crn;
8484   unsigned crm;
8485   int opc2;
8486   arm_feature_set deprecated;
8487   arm_feature_set obsoleted;
8488   const char *dep_msg;
8489   const char *obs_msg;
8490 };
8491
8492 #define DEPR_ACCESS_V8 \
8493   N_("This coprocessor register access is deprecated in ARMv8")
8494
8495 /* Table of all deprecated coprocessor registers.  */
8496 static struct deprecated_coproc_regs_s deprecated_coproc_regs[] =
8497 {
8498     {15, 0, 7, 10, 5,                                   /* CP15DMB.  */
8499      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8500      DEPR_ACCESS_V8, NULL},
8501     {15, 0, 7, 10, 4,                                   /* CP15DSB.  */
8502      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8503      DEPR_ACCESS_V8, NULL},
8504     {15, 0, 7,  5, 4,                                   /* CP15ISB.  */
8505      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8506      DEPR_ACCESS_V8, NULL},
8507     {14, 6, 1,  0, 0,                                   /* TEEHBR.  */
8508      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8509      DEPR_ACCESS_V8, NULL},
8510     {14, 6, 0,  0, 0,                                   /* TEECR.  */
8511      ARM_FEATURE_CORE_LOW (ARM_EXT_V8), ARM_ARCH_NONE,
8512      DEPR_ACCESS_V8, NULL},
8513 };
8514
8515 #undef DEPR_ACCESS_V8
8516
8517 static const size_t deprecated_coproc_reg_count =
8518   sizeof (deprecated_coproc_regs) / sizeof (deprecated_coproc_regs[0]);
8519
8520 static void
8521 do_co_reg (void)
8522 {
8523   unsigned Rd;
8524   size_t i;
8525
8526   Rd = inst.operands[2].reg;
8527   if (thumb_mode)
8528     {
8529       if (inst.instruction == 0xee000010
8530           || inst.instruction == 0xfe000010)
8531         /* MCR, MCR2  */
8532         reject_bad_reg (Rd);
8533       else
8534         /* MRC, MRC2  */
8535         constraint (Rd == REG_SP, BAD_SP);
8536     }
8537   else
8538     {
8539       /* MCR */
8540       if (inst.instruction == 0xe000010)
8541         constraint (Rd == REG_PC, BAD_PC);
8542     }
8543
8544     for (i = 0; i < deprecated_coproc_reg_count; ++i)
8545       {
8546         const struct deprecated_coproc_regs_s *r =
8547           deprecated_coproc_regs + i;
8548
8549         if (inst.operands[0].reg == r->cp
8550             && inst.operands[1].imm == r->opc1
8551             && inst.operands[3].reg == r->crn
8552             && inst.operands[4].reg == r->crm
8553             && inst.operands[5].imm == r->opc2)
8554           {
8555             if (! ARM_CPU_IS_ANY (cpu_variant)
8556                 && warn_on_deprecated
8557                 && ARM_CPU_HAS_FEATURE (cpu_variant, r->deprecated))
8558               as_tsktsk ("%s", r->dep_msg);
8559           }
8560       }
8561
8562   inst.instruction |= inst.operands[0].reg << 8;
8563   inst.instruction |= inst.operands[1].imm << 21;
8564   inst.instruction |= Rd << 12;
8565   inst.instruction |= inst.operands[3].reg << 16;
8566   inst.instruction |= inst.operands[4].reg;
8567   inst.instruction |= inst.operands[5].imm << 5;
8568 }
8569
8570 /* Transfer between coprocessor register and pair of ARM registers.
8571    MCRR{cond} <coproc>, <opcode>, <Rd>, <Rn>, <CRm>.
8572    MCRR2
8573    MRRC{cond}
8574    MRRC2
8575
8576    Two XScale instructions are special cases of these:
8577
8578      MAR{cond} acc0, <RdLo>, <RdHi> == MCRR{cond} p0, #0, <RdLo>, <RdHi>, c0
8579      MRA{cond} acc0, <RdLo>, <RdHi> == MRRC{cond} p0, #0, <RdLo>, <RdHi>, c0
8580
8581    Result unpredictable if Rd or Rn is R15.  */
8582
8583 static void
8584 do_co_reg2c (void)
8585 {
8586   unsigned Rd, Rn;
8587
8588   Rd = inst.operands[2].reg;
8589   Rn = inst.operands[3].reg;
8590
8591   if (thumb_mode)
8592     {
8593       reject_bad_reg (Rd);
8594       reject_bad_reg (Rn);
8595     }
8596   else
8597     {
8598       constraint (Rd == REG_PC, BAD_PC);
8599       constraint (Rn == REG_PC, BAD_PC);
8600     }
8601
8602   inst.instruction |= inst.operands[0].reg << 8;
8603   inst.instruction |= inst.operands[1].imm << 4;
8604   inst.instruction |= Rd << 12;
8605   inst.instruction |= Rn << 16;
8606   inst.instruction |= inst.operands[4].reg;
8607 }
8608
8609 static void
8610 do_cpsi (void)
8611 {
8612   inst.instruction |= inst.operands[0].imm << 6;
8613   if (inst.operands[1].present)
8614     {
8615       inst.instruction |= CPSI_MMOD;
8616       inst.instruction |= inst.operands[1].imm;
8617     }
8618 }
8619
8620 static void
8621 do_dbg (void)
8622 {
8623   inst.instruction |= inst.operands[0].imm;
8624 }
8625
8626 static void
8627 do_div (void)
8628 {
8629   unsigned Rd, Rn, Rm;
8630
8631   Rd = inst.operands[0].reg;
8632   Rn = (inst.operands[1].present
8633         ? inst.operands[1].reg : Rd);
8634   Rm = inst.operands[2].reg;
8635
8636   constraint ((Rd == REG_PC), BAD_PC);
8637   constraint ((Rn == REG_PC), BAD_PC);
8638   constraint ((Rm == REG_PC), BAD_PC);
8639
8640   inst.instruction |= Rd << 16;
8641   inst.instruction |= Rn << 0;
8642   inst.instruction |= Rm << 8;
8643 }
8644
8645 static void
8646 do_it (void)
8647 {
8648   /* There is no IT instruction in ARM mode.  We
8649      process it to do the validation as if in
8650      thumb mode, just in case the code gets
8651      assembled for thumb using the unified syntax.  */
8652
8653   inst.size = 0;
8654   if (unified_syntax)
8655     {
8656       set_it_insn_type (IT_INSN);
8657       now_it.mask = (inst.instruction & 0xf) | 0x10;
8658       now_it.cc = inst.operands[0].imm;
8659     }
8660 }
8661
8662 /* If there is only one register in the register list,
8663    then return its register number.  Otherwise return -1.  */
8664 static int
8665 only_one_reg_in_list (int range)
8666 {
8667   int i = ffs (range) - 1;
8668   return (i > 15 || range != (1 << i)) ? -1 : i;
8669 }
8670
8671 static void
8672 encode_ldmstm(int from_push_pop_mnem)
8673 {
8674   int base_reg = inst.operands[0].reg;
8675   int range = inst.operands[1].imm;
8676   int one_reg;
8677
8678   inst.instruction |= base_reg << 16;
8679   inst.instruction |= range;
8680
8681   if (inst.operands[1].writeback)
8682     inst.instruction |= LDM_TYPE_2_OR_3;
8683
8684   if (inst.operands[0].writeback)
8685     {
8686       inst.instruction |= WRITE_BACK;
8687       /* Check for unpredictable uses of writeback.  */
8688       if (inst.instruction & LOAD_BIT)
8689         {
8690           /* Not allowed in LDM type 2.  */
8691           if ((inst.instruction & LDM_TYPE_2_OR_3)
8692               && ((range & (1 << REG_PC)) == 0))
8693             as_warn (_("writeback of base register is UNPREDICTABLE"));
8694           /* Only allowed if base reg not in list for other types.  */
8695           else if (range & (1 << base_reg))
8696             as_warn (_("writeback of base register when in register list is UNPREDICTABLE"));
8697         }
8698       else /* STM.  */
8699         {
8700           /* Not allowed for type 2.  */
8701           if (inst.instruction & LDM_TYPE_2_OR_3)
8702             as_warn (_("writeback of base register is UNPREDICTABLE"));
8703           /* Only allowed if base reg not in list, or first in list.  */
8704           else if ((range & (1 << base_reg))
8705                    && (range & ((1 << base_reg) - 1)))
8706             as_warn (_("if writeback register is in list, it must be the lowest reg in the list"));
8707         }
8708     }
8709
8710   /* If PUSH/POP has only one register, then use the A2 encoding.  */
8711   one_reg = only_one_reg_in_list (range);
8712   if (from_push_pop_mnem && one_reg >= 0)
8713     {
8714       int is_push = (inst.instruction & A_PUSH_POP_OP_MASK) == A1_OPCODE_PUSH;
8715
8716       inst.instruction &= A_COND_MASK;
8717       inst.instruction |= is_push ? A2_OPCODE_PUSH : A2_OPCODE_POP;
8718       inst.instruction |= one_reg << 12;
8719     }
8720 }
8721
8722 static void
8723 do_ldmstm (void)
8724 {
8725   encode_ldmstm (/*from_push_pop_mnem=*/FALSE);
8726 }
8727
8728 /* ARMv5TE load-consecutive (argument parse)
8729    Mode is like LDRH.
8730
8731      LDRccD R, mode
8732      STRccD R, mode.  */
8733
8734 static void
8735 do_ldrd (void)
8736 {
8737   constraint (inst.operands[0].reg % 2 != 0,
8738               _("first transfer register must be even"));
8739   constraint (inst.operands[1].present
8740               && inst.operands[1].reg != inst.operands[0].reg + 1,
8741               _("can only transfer two consecutive registers"));
8742   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8743   constraint (!inst.operands[2].isreg, _("'[' expected"));
8744
8745   if (!inst.operands[1].present)
8746     inst.operands[1].reg = inst.operands[0].reg + 1;
8747
8748   /* encode_arm_addr_mode_3 will diagnose overlap between the base
8749      register and the first register written; we have to diagnose
8750      overlap between the base and the second register written here.  */
8751
8752   if (inst.operands[2].reg == inst.operands[1].reg
8753       && (inst.operands[2].writeback || inst.operands[2].postind))
8754     as_warn (_("base register written back, and overlaps "
8755                "second transfer register"));
8756
8757   if (!(inst.instruction & V4_STR_BIT))
8758     {
8759       /* For an index-register load, the index register must not overlap the
8760         destination (even if not write-back).  */
8761       if (inst.operands[2].immisreg
8762               && ((unsigned) inst.operands[2].imm == inst.operands[0].reg
8763               || (unsigned) inst.operands[2].imm == inst.operands[1].reg))
8764         as_warn (_("index register overlaps transfer register"));
8765     }
8766   inst.instruction |= inst.operands[0].reg << 12;
8767   encode_arm_addr_mode_3 (2, /*is_t=*/FALSE);
8768 }
8769
8770 static void
8771 do_ldrex (void)
8772 {
8773   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
8774               || inst.operands[1].postind || inst.operands[1].writeback
8775               || inst.operands[1].immisreg || inst.operands[1].shifted
8776               || inst.operands[1].negative
8777               /* This can arise if the programmer has written
8778                    strex rN, rM, foo
8779                  or if they have mistakenly used a register name as the last
8780                  operand,  eg:
8781                    strex rN, rM, rX
8782                  It is very difficult to distinguish between these two cases
8783                  because "rX" might actually be a label. ie the register
8784                  name has been occluded by a symbol of the same name. So we
8785                  just generate a general 'bad addressing mode' type error
8786                  message and leave it up to the programmer to discover the
8787                  true cause and fix their mistake.  */
8788               || (inst.operands[1].reg == REG_PC),
8789               BAD_ADDR_MODE);
8790
8791   constraint (inst.reloc.exp.X_op != O_constant
8792               || inst.reloc.exp.X_add_number != 0,
8793               _("offset must be zero in ARM encoding"));
8794
8795   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
8796
8797   inst.instruction |= inst.operands[0].reg << 12;
8798   inst.instruction |= inst.operands[1].reg << 16;
8799   inst.reloc.type = BFD_RELOC_UNUSED;
8800 }
8801
8802 static void
8803 do_ldrexd (void)
8804 {
8805   constraint (inst.operands[0].reg % 2 != 0,
8806               _("even register required"));
8807   constraint (inst.operands[1].present
8808               && inst.operands[1].reg != inst.operands[0].reg + 1,
8809               _("can only load two consecutive registers"));
8810   /* If op 1 were present and equal to PC, this function wouldn't
8811      have been called in the first place.  */
8812   constraint (inst.operands[0].reg == REG_LR, _("r14 not allowed here"));
8813
8814   inst.instruction |= inst.operands[0].reg << 12;
8815   inst.instruction |= inst.operands[2].reg << 16;
8816 }
8817
8818 /* In both ARM and thumb state 'ldr pc, #imm'  with an immediate
8819    which is not a multiple of four is UNPREDICTABLE.  */
8820 static void
8821 check_ldr_r15_aligned (void)
8822 {
8823   constraint (!(inst.operands[1].immisreg)
8824               && (inst.operands[0].reg == REG_PC
8825               && inst.operands[1].reg == REG_PC
8826               && (inst.reloc.exp.X_add_number & 0x3)),
8827               _("ldr to register 15 must be 4-byte alligned"));
8828 }
8829
8830 static void
8831 do_ldst (void)
8832 {
8833   inst.instruction |= inst.operands[0].reg << 12;
8834   if (!inst.operands[1].isreg)
8835     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/FALSE))
8836       return;
8837   encode_arm_addr_mode_2 (1, /*is_t=*/FALSE);
8838   check_ldr_r15_aligned ();
8839 }
8840
8841 static void
8842 do_ldstt (void)
8843 {
8844   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8845      reject [Rn,...].  */
8846   if (inst.operands[1].preind)
8847     {
8848       constraint (inst.reloc.exp.X_op != O_constant
8849                   || inst.reloc.exp.X_add_number != 0,
8850                   _("this instruction requires a post-indexed address"));
8851
8852       inst.operands[1].preind = 0;
8853       inst.operands[1].postind = 1;
8854       inst.operands[1].writeback = 1;
8855     }
8856   inst.instruction |= inst.operands[0].reg << 12;
8857   encode_arm_addr_mode_2 (1, /*is_t=*/TRUE);
8858 }
8859
8860 /* Halfword and signed-byte load/store operations.  */
8861
8862 static void
8863 do_ldstv4 (void)
8864 {
8865   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
8866   inst.instruction |= inst.operands[0].reg << 12;
8867   if (!inst.operands[1].isreg)
8868     if (move_or_literal_pool (0, CONST_ARM, /*mode_3=*/TRUE))
8869       return;
8870   encode_arm_addr_mode_3 (1, /*is_t=*/FALSE);
8871 }
8872
8873 static void
8874 do_ldsttv4 (void)
8875 {
8876   /* ldrt/strt always use post-indexed addressing.  Turn [Rn] into [Rn]! and
8877      reject [Rn,...].  */
8878   if (inst.operands[1].preind)
8879     {
8880       constraint (inst.reloc.exp.X_op != O_constant
8881                   || inst.reloc.exp.X_add_number != 0,
8882                   _("this instruction requires a post-indexed address"));
8883
8884       inst.operands[1].preind = 0;
8885       inst.operands[1].postind = 1;
8886       inst.operands[1].writeback = 1;
8887     }
8888   inst.instruction |= inst.operands[0].reg << 12;
8889   encode_arm_addr_mode_3 (1, /*is_t=*/TRUE);
8890 }
8891
8892 /* Co-processor register load/store.
8893    Format: <LDC|STC>{cond}[L] CP#,CRd,<address>  */
8894 static void
8895 do_lstc (void)
8896 {
8897   inst.instruction |= inst.operands[0].reg << 8;
8898   inst.instruction |= inst.operands[1].reg << 12;
8899   encode_arm_cp_address (2, TRUE, TRUE, 0);
8900 }
8901
8902 static void
8903 do_mlas (void)
8904 {
8905   /* This restriction does not apply to mls (nor to mla in v6 or later).  */
8906   if (inst.operands[0].reg == inst.operands[1].reg
8907       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6)
8908       && !(inst.instruction & 0x00400000))
8909     as_tsktsk (_("Rd and Rm should be different in mla"));
8910
8911   inst.instruction |= inst.operands[0].reg << 16;
8912   inst.instruction |= inst.operands[1].reg;
8913   inst.instruction |= inst.operands[2].reg << 8;
8914   inst.instruction |= inst.operands[3].reg << 12;
8915 }
8916
8917 static void
8918 do_mov (void)
8919 {
8920   inst.instruction |= inst.operands[0].reg << 12;
8921   encode_arm_shifter_operand (1);
8922 }
8923
8924 /* ARM V6T2 16-bit immediate register load: MOV[WT]{cond} Rd, #<imm16>.  */
8925 static void
8926 do_mov16 (void)
8927 {
8928   bfd_vma imm;
8929   bfd_boolean top;
8930
8931   top = (inst.instruction & 0x00400000) != 0;
8932   constraint (top && inst.reloc.type == BFD_RELOC_ARM_MOVW,
8933               _(":lower16: not allowed this instruction"));
8934   constraint (!top && inst.reloc.type == BFD_RELOC_ARM_MOVT,
8935               _(":upper16: not allowed instruction"));
8936   inst.instruction |= inst.operands[0].reg << 12;
8937   if (inst.reloc.type == BFD_RELOC_UNUSED)
8938     {
8939       imm = inst.reloc.exp.X_add_number;
8940       /* The value is in two pieces: 0:11, 16:19.  */
8941       inst.instruction |= (imm & 0x00000fff);
8942       inst.instruction |= (imm & 0x0000f000) << 4;
8943     }
8944 }
8945
8946 static int
8947 do_vfp_nsyn_mrs (void)
8948 {
8949   if (inst.operands[0].isvec)
8950     {
8951       if (inst.operands[1].reg != 1)
8952         first_error (_("operand 1 must be FPSCR"));
8953       memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
8954       memset (&inst.operands[1], '\0', sizeof (inst.operands[1]));
8955       do_vfp_nsyn_opcode ("fmstat");
8956     }
8957   else if (inst.operands[1].isvec)
8958     do_vfp_nsyn_opcode ("fmrx");
8959   else
8960     return FAIL;
8961
8962   return SUCCESS;
8963 }
8964
8965 static int
8966 do_vfp_nsyn_msr (void)
8967 {
8968   if (inst.operands[0].isvec)
8969     do_vfp_nsyn_opcode ("fmxr");
8970   else
8971     return FAIL;
8972
8973   return SUCCESS;
8974 }
8975
8976 static void
8977 do_vmrs (void)
8978 {
8979   unsigned Rt = inst.operands[0].reg;
8980
8981   if (thumb_mode && Rt == REG_SP)
8982     {
8983       inst.error = BAD_SP;
8984       return;
8985     }
8986
8987   /* APSR_ sets isvec. All other refs to PC are illegal.  */
8988   if (!inst.operands[0].isvec && Rt == REG_PC)
8989     {
8990       inst.error = BAD_PC;
8991       return;
8992     }
8993
8994   /* If we get through parsing the register name, we just insert the number
8995      generated into the instruction without further validation.  */
8996   inst.instruction |= (inst.operands[1].reg << 16);
8997   inst.instruction |= (Rt << 12);
8998 }
8999
9000 static void
9001 do_vmsr (void)
9002 {
9003   unsigned Rt = inst.operands[1].reg;
9004
9005   if (thumb_mode)
9006     reject_bad_reg (Rt);
9007   else if (Rt == REG_PC)
9008     {
9009       inst.error = BAD_PC;
9010       return;
9011     }
9012
9013   /* If we get through parsing the register name, we just insert the number
9014      generated into the instruction without further validation.  */
9015   inst.instruction |= (inst.operands[0].reg << 16);
9016   inst.instruction |= (Rt << 12);
9017 }
9018
9019 static void
9020 do_mrs (void)
9021 {
9022   unsigned br;
9023
9024   if (do_vfp_nsyn_mrs () == SUCCESS)
9025     return;
9026
9027   constraint (inst.operands[0].reg == REG_PC, BAD_PC);
9028   inst.instruction |= inst.operands[0].reg << 12;
9029
9030   if (inst.operands[1].isreg)
9031     {
9032       br = inst.operands[1].reg;
9033       if (((br & 0x200) == 0) && ((br & 0xf0000) != 0xf000))
9034         as_bad (_("bad register for mrs"));
9035     }
9036   else
9037     {
9038       /* mrs only accepts CPSR/SPSR/CPSR_all/SPSR_all.  */
9039       constraint ((inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f))
9040                   != (PSR_c|PSR_f),
9041                   _("'APSR', 'CPSR' or 'SPSR' expected"));
9042       br = (15<<16) | (inst.operands[1].imm & SPSR_BIT);
9043     }
9044
9045   inst.instruction |= br;
9046 }
9047
9048 /* Two possible forms:
9049       "{C|S}PSR_<field>, Rm",
9050       "{C|S}PSR_f, #expression".  */
9051
9052 static void
9053 do_msr (void)
9054 {
9055   if (do_vfp_nsyn_msr () == SUCCESS)
9056     return;
9057
9058   inst.instruction |= inst.operands[0].imm;
9059   if (inst.operands[1].isreg)
9060     inst.instruction |= inst.operands[1].reg;
9061   else
9062     {
9063       inst.instruction |= INST_IMMEDIATE;
9064       inst.reloc.type = BFD_RELOC_ARM_IMMEDIATE;
9065       inst.reloc.pc_rel = 0;
9066     }
9067 }
9068
9069 static void
9070 do_mul (void)
9071 {
9072   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
9073
9074   if (!inst.operands[2].present)
9075     inst.operands[2].reg = inst.operands[0].reg;
9076   inst.instruction |= inst.operands[0].reg << 16;
9077   inst.instruction |= inst.operands[1].reg;
9078   inst.instruction |= inst.operands[2].reg << 8;
9079
9080   if (inst.operands[0].reg == inst.operands[1].reg
9081       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9082     as_tsktsk (_("Rd and Rm should be different in mul"));
9083 }
9084
9085 /* Long Multiply Parser
9086    UMULL RdLo, RdHi, Rm, Rs
9087    SMULL RdLo, RdHi, Rm, Rs
9088    UMLAL RdLo, RdHi, Rm, Rs
9089    SMLAL RdLo, RdHi, Rm, Rs.  */
9090
9091 static void
9092 do_mull (void)
9093 {
9094   inst.instruction |= inst.operands[0].reg << 12;
9095   inst.instruction |= inst.operands[1].reg << 16;
9096   inst.instruction |= inst.operands[2].reg;
9097   inst.instruction |= inst.operands[3].reg << 8;
9098
9099   /* rdhi and rdlo must be different.  */
9100   if (inst.operands[0].reg == inst.operands[1].reg)
9101     as_tsktsk (_("rdhi and rdlo must be different"));
9102
9103   /* rdhi, rdlo and rm must all be different before armv6.  */
9104   if ((inst.operands[0].reg == inst.operands[2].reg
9105       || inst.operands[1].reg == inst.operands[2].reg)
9106       && !ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6))
9107     as_tsktsk (_("rdhi, rdlo and rm must all be different"));
9108 }
9109
9110 static void
9111 do_nop (void)
9112 {
9113   if (inst.operands[0].present
9114       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6k))
9115     {
9116       /* Architectural NOP hints are CPSR sets with no bits selected.  */
9117       inst.instruction &= 0xf0000000;
9118       inst.instruction |= 0x0320f000;
9119       if (inst.operands[0].present)
9120         inst.instruction |= inst.operands[0].imm;
9121     }
9122 }
9123
9124 /* ARM V6 Pack Halfword Bottom Top instruction (argument parse).
9125    PKHBT {<cond>} <Rd>, <Rn>, <Rm> {, LSL #<shift_imm>}
9126    Condition defaults to COND_ALWAYS.
9127    Error if Rd, Rn or Rm are R15.  */
9128
9129 static void
9130 do_pkhbt (void)
9131 {
9132   inst.instruction |= inst.operands[0].reg << 12;
9133   inst.instruction |= inst.operands[1].reg << 16;
9134   inst.instruction |= inst.operands[2].reg;
9135   if (inst.operands[3].present)
9136     encode_arm_shift (3);
9137 }
9138
9139 /* ARM V6 PKHTB (Argument Parse).  */
9140
9141 static void
9142 do_pkhtb (void)
9143 {
9144   if (!inst.operands[3].present)
9145     {
9146       /* If the shift specifier is omitted, turn the instruction
9147          into pkhbt rd, rm, rn. */
9148       inst.instruction &= 0xfff00010;
9149       inst.instruction |= inst.operands[0].reg << 12;
9150       inst.instruction |= inst.operands[1].reg;
9151       inst.instruction |= inst.operands[2].reg << 16;
9152     }
9153   else
9154     {
9155       inst.instruction |= inst.operands[0].reg << 12;
9156       inst.instruction |= inst.operands[1].reg << 16;
9157       inst.instruction |= inst.operands[2].reg;
9158       encode_arm_shift (3);
9159     }
9160 }
9161
9162 /* ARMv5TE: Preload-Cache
9163    MP Extensions: Preload for write
9164
9165     PLD(W) <addr_mode>
9166
9167   Syntactically, like LDR with B=1, W=0, L=1.  */
9168
9169 static void
9170 do_pld (void)
9171 {
9172   constraint (!inst.operands[0].isreg,
9173               _("'[' expected after PLD mnemonic"));
9174   constraint (inst.operands[0].postind,
9175               _("post-indexed expression used in preload instruction"));
9176   constraint (inst.operands[0].writeback,
9177               _("writeback used in preload instruction"));
9178   constraint (!inst.operands[0].preind,
9179               _("unindexed addressing used in preload instruction"));
9180   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9181 }
9182
9183 /* ARMv7: PLI <addr_mode>  */
9184 static void
9185 do_pli (void)
9186 {
9187   constraint (!inst.operands[0].isreg,
9188               _("'[' expected after PLI mnemonic"));
9189   constraint (inst.operands[0].postind,
9190               _("post-indexed expression used in preload instruction"));
9191   constraint (inst.operands[0].writeback,
9192               _("writeback used in preload instruction"));
9193   constraint (!inst.operands[0].preind,
9194               _("unindexed addressing used in preload instruction"));
9195   encode_arm_addr_mode_2 (0, /*is_t=*/FALSE);
9196   inst.instruction &= ~PRE_INDEX;
9197 }
9198
9199 static void
9200 do_push_pop (void)
9201 {
9202   constraint (inst.operands[0].writeback,
9203               _("push/pop do not support {reglist}^"));
9204   inst.operands[1] = inst.operands[0];
9205   memset (&inst.operands[0], 0, sizeof inst.operands[0]);
9206   inst.operands[0].isreg = 1;
9207   inst.operands[0].writeback = 1;
9208   inst.operands[0].reg = REG_SP;
9209   encode_ldmstm (/*from_push_pop_mnem=*/TRUE);
9210 }
9211
9212 /* ARM V6 RFE (Return from Exception) loads the PC and CPSR from the
9213    word at the specified address and the following word
9214    respectively.
9215    Unconditionally executed.
9216    Error if Rn is R15.  */
9217
9218 static void
9219 do_rfe (void)
9220 {
9221   inst.instruction |= inst.operands[0].reg << 16;
9222   if (inst.operands[0].writeback)
9223     inst.instruction |= WRITE_BACK;
9224 }
9225
9226 /* ARM V6 ssat (argument parse).  */
9227
9228 static void
9229 do_ssat (void)
9230 {
9231   inst.instruction |= inst.operands[0].reg << 12;
9232   inst.instruction |= (inst.operands[1].imm - 1) << 16;
9233   inst.instruction |= inst.operands[2].reg;
9234
9235   if (inst.operands[3].present)
9236     encode_arm_shift (3);
9237 }
9238
9239 /* ARM V6 usat (argument parse).  */
9240
9241 static void
9242 do_usat (void)
9243 {
9244   inst.instruction |= inst.operands[0].reg << 12;
9245   inst.instruction |= inst.operands[1].imm << 16;
9246   inst.instruction |= inst.operands[2].reg;
9247
9248   if (inst.operands[3].present)
9249     encode_arm_shift (3);
9250 }
9251
9252 /* ARM V6 ssat16 (argument parse).  */
9253
9254 static void
9255 do_ssat16 (void)
9256 {
9257   inst.instruction |= inst.operands[0].reg << 12;
9258   inst.instruction |= ((inst.operands[1].imm - 1) << 16);
9259   inst.instruction |= inst.operands[2].reg;
9260 }
9261
9262 static void
9263 do_usat16 (void)
9264 {
9265   inst.instruction |= inst.operands[0].reg << 12;
9266   inst.instruction |= inst.operands[1].imm << 16;
9267   inst.instruction |= inst.operands[2].reg;
9268 }
9269
9270 /* ARM V6 SETEND (argument parse).  Sets the E bit in the CPSR while
9271    preserving the other bits.
9272
9273    setend <endian_specifier>, where <endian_specifier> is either
9274    BE or LE.  */
9275
9276 static void
9277 do_setend (void)
9278 {
9279   if (warn_on_deprecated
9280       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
9281       as_tsktsk (_("setend use is deprecated for ARMv8"));
9282
9283   if (inst.operands[0].imm)
9284     inst.instruction |= 0x200;
9285 }
9286
9287 static void
9288 do_shift (void)
9289 {
9290   unsigned int Rm = (inst.operands[1].present
9291                      ? inst.operands[1].reg
9292                      : inst.operands[0].reg);
9293
9294   inst.instruction |= inst.operands[0].reg << 12;
9295   inst.instruction |= Rm;
9296   if (inst.operands[2].isreg)  /* Rd, {Rm,} Rs */
9297     {
9298       inst.instruction |= inst.operands[2].reg << 8;
9299       inst.instruction |= SHIFT_BY_REG;
9300       /* PR 12854: Error on extraneous shifts.  */
9301       constraint (inst.operands[2].shifted,
9302                   _("extraneous shift as part of operand to shift insn"));
9303     }
9304   else
9305     inst.reloc.type = BFD_RELOC_ARM_SHIFT_IMM;
9306 }
9307
9308 static void
9309 do_smc (void)
9310 {
9311   inst.reloc.type = BFD_RELOC_ARM_SMC;
9312   inst.reloc.pc_rel = 0;
9313 }
9314
9315 static void
9316 do_hvc (void)
9317 {
9318   inst.reloc.type = BFD_RELOC_ARM_HVC;
9319   inst.reloc.pc_rel = 0;
9320 }
9321
9322 static void
9323 do_swi (void)
9324 {
9325   inst.reloc.type = BFD_RELOC_ARM_SWI;
9326   inst.reloc.pc_rel = 0;
9327 }
9328
9329 static void
9330 do_setpan (void)
9331 {
9332   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9333               _("selected processor does not support SETPAN instruction"));
9334
9335   inst.instruction |= ((inst.operands[0].imm & 1) << 9);
9336 }
9337
9338 static void
9339 do_t_setpan (void)
9340 {
9341   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_pan),
9342               _("selected processor does not support SETPAN instruction"));
9343
9344   inst.instruction |= (inst.operands[0].imm << 3);
9345 }
9346
9347 /* ARM V5E (El Segundo) signed-multiply-accumulate (argument parse)
9348    SMLAxy{cond} Rd,Rm,Rs,Rn
9349    SMLAWy{cond} Rd,Rm,Rs,Rn
9350    Error if any register is R15.  */
9351
9352 static void
9353 do_smla (void)
9354 {
9355   inst.instruction |= inst.operands[0].reg << 16;
9356   inst.instruction |= inst.operands[1].reg;
9357   inst.instruction |= inst.operands[2].reg << 8;
9358   inst.instruction |= inst.operands[3].reg << 12;
9359 }
9360
9361 /* ARM V5E (El Segundo) signed-multiply-accumulate-long (argument parse)
9362    SMLALxy{cond} Rdlo,Rdhi,Rm,Rs
9363    Error if any register is R15.
9364    Warning if Rdlo == Rdhi.  */
9365
9366 static void
9367 do_smlal (void)
9368 {
9369   inst.instruction |= inst.operands[0].reg << 12;
9370   inst.instruction |= inst.operands[1].reg << 16;
9371   inst.instruction |= inst.operands[2].reg;
9372   inst.instruction |= inst.operands[3].reg << 8;
9373
9374   if (inst.operands[0].reg == inst.operands[1].reg)
9375     as_tsktsk (_("rdhi and rdlo must be different"));
9376 }
9377
9378 /* ARM V5E (El Segundo) signed-multiply (argument parse)
9379    SMULxy{cond} Rd,Rm,Rs
9380    Error if any register is R15.  */
9381
9382 static void
9383 do_smul (void)
9384 {
9385   inst.instruction |= inst.operands[0].reg << 16;
9386   inst.instruction |= inst.operands[1].reg;
9387   inst.instruction |= inst.operands[2].reg << 8;
9388 }
9389
9390 /* ARM V6 srs (argument parse).  The variable fields in the encoding are
9391    the same for both ARM and Thumb-2.  */
9392
9393 static void
9394 do_srs (void)
9395 {
9396   int reg;
9397
9398   if (inst.operands[0].present)
9399     {
9400       reg = inst.operands[0].reg;
9401       constraint (reg != REG_SP, _("SRS base register must be r13"));
9402     }
9403   else
9404     reg = REG_SP;
9405
9406   inst.instruction |= reg << 16;
9407   inst.instruction |= inst.operands[1].imm;
9408   if (inst.operands[0].writeback || inst.operands[1].writeback)
9409     inst.instruction |= WRITE_BACK;
9410 }
9411
9412 /* ARM V6 strex (argument parse).  */
9413
9414 static void
9415 do_strex (void)
9416 {
9417   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9418               || inst.operands[2].postind || inst.operands[2].writeback
9419               || inst.operands[2].immisreg || inst.operands[2].shifted
9420               || inst.operands[2].negative
9421               /* See comment in do_ldrex().  */
9422               || (inst.operands[2].reg == REG_PC),
9423               BAD_ADDR_MODE);
9424
9425   constraint (inst.operands[0].reg == inst.operands[1].reg
9426               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9427
9428   constraint (inst.reloc.exp.X_op != O_constant
9429               || inst.reloc.exp.X_add_number != 0,
9430               _("offset must be zero in ARM encoding"));
9431
9432   inst.instruction |= inst.operands[0].reg << 12;
9433   inst.instruction |= inst.operands[1].reg;
9434   inst.instruction |= inst.operands[2].reg << 16;
9435   inst.reloc.type = BFD_RELOC_UNUSED;
9436 }
9437
9438 static void
9439 do_t_strexbh (void)
9440 {
9441   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
9442               || inst.operands[2].postind || inst.operands[2].writeback
9443               || inst.operands[2].immisreg || inst.operands[2].shifted
9444               || inst.operands[2].negative,
9445               BAD_ADDR_MODE);
9446
9447   constraint (inst.operands[0].reg == inst.operands[1].reg
9448               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9449
9450   do_rm_rd_rn ();
9451 }
9452
9453 static void
9454 do_strexd (void)
9455 {
9456   constraint (inst.operands[1].reg % 2 != 0,
9457               _("even register required"));
9458   constraint (inst.operands[2].present
9459               && inst.operands[2].reg != inst.operands[1].reg + 1,
9460               _("can only store two consecutive registers"));
9461   /* If op 2 were present and equal to PC, this function wouldn't
9462      have been called in the first place.  */
9463   constraint (inst.operands[1].reg == REG_LR, _("r14 not allowed here"));
9464
9465   constraint (inst.operands[0].reg == inst.operands[1].reg
9466               || inst.operands[0].reg == inst.operands[1].reg + 1
9467               || inst.operands[0].reg == inst.operands[3].reg,
9468               BAD_OVERLAP);
9469
9470   inst.instruction |= inst.operands[0].reg << 12;
9471   inst.instruction |= inst.operands[1].reg;
9472   inst.instruction |= inst.operands[3].reg << 16;
9473 }
9474
9475 /* ARM V8 STRL.  */
9476 static void
9477 do_stlex (void)
9478 {
9479   constraint (inst.operands[0].reg == inst.operands[1].reg
9480               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9481
9482   do_rd_rm_rn ();
9483 }
9484
9485 static void
9486 do_t_stlex (void)
9487 {
9488   constraint (inst.operands[0].reg == inst.operands[1].reg
9489               || inst.operands[0].reg == inst.operands[2].reg, BAD_OVERLAP);
9490
9491   do_rm_rd_rn ();
9492 }
9493
9494 /* ARM V6 SXTAH extracts a 16-bit value from a register, sign
9495    extends it to 32-bits, and adds the result to a value in another
9496    register.  You can specify a rotation by 0, 8, 16, or 24 bits
9497    before extracting the 16-bit value.
9498    SXTAH{<cond>} <Rd>, <Rn>, <Rm>{, <rotation>}
9499    Condition defaults to COND_ALWAYS.
9500    Error if any register uses R15.  */
9501
9502 static void
9503 do_sxtah (void)
9504 {
9505   inst.instruction |= inst.operands[0].reg << 12;
9506   inst.instruction |= inst.operands[1].reg << 16;
9507   inst.instruction |= inst.operands[2].reg;
9508   inst.instruction |= inst.operands[3].imm << 10;
9509 }
9510
9511 /* ARM V6 SXTH.
9512
9513    SXTH {<cond>} <Rd>, <Rm>{, <rotation>}
9514    Condition defaults to COND_ALWAYS.
9515    Error if any register uses R15.  */
9516
9517 static void
9518 do_sxth (void)
9519 {
9520   inst.instruction |= inst.operands[0].reg << 12;
9521   inst.instruction |= inst.operands[1].reg;
9522   inst.instruction |= inst.operands[2].imm << 10;
9523 }
9524 \f
9525 /* VFP instructions.  In a logical order: SP variant first, monad
9526    before dyad, arithmetic then move then load/store.  */
9527
9528 static void
9529 do_vfp_sp_monadic (void)
9530 {
9531   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9532   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9533 }
9534
9535 static void
9536 do_vfp_sp_dyadic (void)
9537 {
9538   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9539   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9540   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9541 }
9542
9543 static void
9544 do_vfp_sp_compare_z (void)
9545 {
9546   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9547 }
9548
9549 static void
9550 do_vfp_dp_sp_cvt (void)
9551 {
9552   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9553   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sm);
9554 }
9555
9556 static void
9557 do_vfp_sp_dp_cvt (void)
9558 {
9559   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9560   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9561 }
9562
9563 static void
9564 do_vfp_reg_from_sp (void)
9565 {
9566   inst.instruction |= inst.operands[0].reg << 12;
9567   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sn);
9568 }
9569
9570 static void
9571 do_vfp_reg2_from_sp2 (void)
9572 {
9573   constraint (inst.operands[2].imm != 2,
9574               _("only two consecutive VFP SP registers allowed here"));
9575   inst.instruction |= inst.operands[0].reg << 12;
9576   inst.instruction |= inst.operands[1].reg << 16;
9577   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Sm);
9578 }
9579
9580 static void
9581 do_vfp_sp_from_reg (void)
9582 {
9583   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sn);
9584   inst.instruction |= inst.operands[1].reg << 12;
9585 }
9586
9587 static void
9588 do_vfp_sp2_from_reg2 (void)
9589 {
9590   constraint (inst.operands[0].imm != 2,
9591               _("only two consecutive VFP SP registers allowed here"));
9592   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sm);
9593   inst.instruction |= inst.operands[1].reg << 12;
9594   inst.instruction |= inst.operands[2].reg << 16;
9595 }
9596
9597 static void
9598 do_vfp_sp_ldst (void)
9599 {
9600   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9601   encode_arm_cp_address (1, FALSE, TRUE, 0);
9602 }
9603
9604 static void
9605 do_vfp_dp_ldst (void)
9606 {
9607   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9608   encode_arm_cp_address (1, FALSE, TRUE, 0);
9609 }
9610
9611
9612 static void
9613 vfp_sp_ldstm (enum vfp_ldstm_type ldstm_type)
9614 {
9615   if (inst.operands[0].writeback)
9616     inst.instruction |= WRITE_BACK;
9617   else
9618     constraint (ldstm_type != VFP_LDSTMIA,
9619                 _("this addressing mode requires base-register writeback"));
9620   inst.instruction |= inst.operands[0].reg << 16;
9621   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Sd);
9622   inst.instruction |= inst.operands[1].imm;
9623 }
9624
9625 static void
9626 vfp_dp_ldstm (enum vfp_ldstm_type ldstm_type)
9627 {
9628   int count;
9629
9630   if (inst.operands[0].writeback)
9631     inst.instruction |= WRITE_BACK;
9632   else
9633     constraint (ldstm_type != VFP_LDSTMIA && ldstm_type != VFP_LDSTMIAX,
9634                 _("this addressing mode requires base-register writeback"));
9635
9636   inst.instruction |= inst.operands[0].reg << 16;
9637   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9638
9639   count = inst.operands[1].imm << 1;
9640   if (ldstm_type == VFP_LDSTMIAX || ldstm_type == VFP_LDSTMDBX)
9641     count += 1;
9642
9643   inst.instruction |= count;
9644 }
9645
9646 static void
9647 do_vfp_sp_ldstmia (void)
9648 {
9649   vfp_sp_ldstm (VFP_LDSTMIA);
9650 }
9651
9652 static void
9653 do_vfp_sp_ldstmdb (void)
9654 {
9655   vfp_sp_ldstm (VFP_LDSTMDB);
9656 }
9657
9658 static void
9659 do_vfp_dp_ldstmia (void)
9660 {
9661   vfp_dp_ldstm (VFP_LDSTMIA);
9662 }
9663
9664 static void
9665 do_vfp_dp_ldstmdb (void)
9666 {
9667   vfp_dp_ldstm (VFP_LDSTMDB);
9668 }
9669
9670 static void
9671 do_vfp_xp_ldstmia (void)
9672 {
9673   vfp_dp_ldstm (VFP_LDSTMIAX);
9674 }
9675
9676 static void
9677 do_vfp_xp_ldstmdb (void)
9678 {
9679   vfp_dp_ldstm (VFP_LDSTMDBX);
9680 }
9681
9682 static void
9683 do_vfp_dp_rd_rm (void)
9684 {
9685   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9686   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dm);
9687 }
9688
9689 static void
9690 do_vfp_dp_rn_rd (void)
9691 {
9692   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dn);
9693   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9694 }
9695
9696 static void
9697 do_vfp_dp_rd_rn (void)
9698 {
9699   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9700   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9701 }
9702
9703 static void
9704 do_vfp_dp_rd_rn_rm (void)
9705 {
9706   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9707   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dn);
9708   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dm);
9709 }
9710
9711 static void
9712 do_vfp_dp_rd (void)
9713 {
9714   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9715 }
9716
9717 static void
9718 do_vfp_dp_rm_rd_rn (void)
9719 {
9720   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dm);
9721   encode_arm_vfp_reg (inst.operands[1].reg, VFP_REG_Dd);
9722   encode_arm_vfp_reg (inst.operands[2].reg, VFP_REG_Dn);
9723 }
9724
9725 /* VFPv3 instructions.  */
9726 static void
9727 do_vfp_sp_const (void)
9728 {
9729   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9730   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9731   inst.instruction |= (inst.operands[1].imm & 0x0f);
9732 }
9733
9734 static void
9735 do_vfp_dp_const (void)
9736 {
9737   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9738   inst.instruction |= (inst.operands[1].imm & 0xf0) << 12;
9739   inst.instruction |= (inst.operands[1].imm & 0x0f);
9740 }
9741
9742 static void
9743 vfp_conv (int srcsize)
9744 {
9745   int immbits = srcsize - inst.operands[1].imm;
9746
9747   if (srcsize == 16 && !(immbits >= 0 && immbits <= srcsize))
9748     {
9749       /* If srcsize is 16, inst.operands[1].imm must be in the range 0-16.
9750          i.e. immbits must be in range 0 - 16.  */
9751       inst.error = _("immediate value out of range, expected range [0, 16]");
9752       return;
9753     }
9754   else if (srcsize == 32 && !(immbits >= 0 && immbits < srcsize))
9755     {
9756       /* If srcsize is 32, inst.operands[1].imm must be in the range 1-32.
9757          i.e. immbits must be in range 0 - 31.  */
9758       inst.error = _("immediate value out of range, expected range [1, 32]");
9759       return;
9760     }
9761
9762   inst.instruction |= (immbits & 1) << 5;
9763   inst.instruction |= (immbits >> 1);
9764 }
9765
9766 static void
9767 do_vfp_sp_conv_16 (void)
9768 {
9769   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9770   vfp_conv (16);
9771 }
9772
9773 static void
9774 do_vfp_dp_conv_16 (void)
9775 {
9776   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9777   vfp_conv (16);
9778 }
9779
9780 static void
9781 do_vfp_sp_conv_32 (void)
9782 {
9783   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
9784   vfp_conv (32);
9785 }
9786
9787 static void
9788 do_vfp_dp_conv_32 (void)
9789 {
9790   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Dd);
9791   vfp_conv (32);
9792 }
9793 \f
9794 /* FPA instructions.  Also in a logical order.  */
9795
9796 static void
9797 do_fpa_cmp (void)
9798 {
9799   inst.instruction |= inst.operands[0].reg << 16;
9800   inst.instruction |= inst.operands[1].reg;
9801 }
9802
9803 static void
9804 do_fpa_ldmstm (void)
9805 {
9806   inst.instruction |= inst.operands[0].reg << 12;
9807   switch (inst.operands[1].imm)
9808     {
9809     case 1: inst.instruction |= CP_T_X;          break;
9810     case 2: inst.instruction |= CP_T_Y;          break;
9811     case 3: inst.instruction |= CP_T_Y | CP_T_X; break;
9812     case 4:                                      break;
9813     default: abort ();
9814     }
9815
9816   if (inst.instruction & (PRE_INDEX | INDEX_UP))
9817     {
9818       /* The instruction specified "ea" or "fd", so we can only accept
9819          [Rn]{!}.  The instruction does not really support stacking or
9820          unstacking, so we have to emulate these by setting appropriate
9821          bits and offsets.  */
9822       constraint (inst.reloc.exp.X_op != O_constant
9823                   || inst.reloc.exp.X_add_number != 0,
9824                   _("this instruction does not support indexing"));
9825
9826       if ((inst.instruction & PRE_INDEX) || inst.operands[2].writeback)
9827         inst.reloc.exp.X_add_number = 12 * inst.operands[1].imm;
9828
9829       if (!(inst.instruction & INDEX_UP))
9830         inst.reloc.exp.X_add_number = -inst.reloc.exp.X_add_number;
9831
9832       if (!(inst.instruction & PRE_INDEX) && inst.operands[2].writeback)
9833         {
9834           inst.operands[2].preind = 0;
9835           inst.operands[2].postind = 1;
9836         }
9837     }
9838
9839   encode_arm_cp_address (2, TRUE, TRUE, 0);
9840 }
9841 \f
9842 /* iWMMXt instructions: strictly in alphabetical order.  */
9843
9844 static void
9845 do_iwmmxt_tandorc (void)
9846 {
9847   constraint (inst.operands[0].reg != REG_PC, _("only r15 allowed here"));
9848 }
9849
9850 static void
9851 do_iwmmxt_textrc (void)
9852 {
9853   inst.instruction |= inst.operands[0].reg << 12;
9854   inst.instruction |= inst.operands[1].imm;
9855 }
9856
9857 static void
9858 do_iwmmxt_textrm (void)
9859 {
9860   inst.instruction |= inst.operands[0].reg << 12;
9861   inst.instruction |= inst.operands[1].reg << 16;
9862   inst.instruction |= inst.operands[2].imm;
9863 }
9864
9865 static void
9866 do_iwmmxt_tinsr (void)
9867 {
9868   inst.instruction |= inst.operands[0].reg << 16;
9869   inst.instruction |= inst.operands[1].reg << 12;
9870   inst.instruction |= inst.operands[2].imm;
9871 }
9872
9873 static void
9874 do_iwmmxt_tmia (void)
9875 {
9876   inst.instruction |= inst.operands[0].reg << 5;
9877   inst.instruction |= inst.operands[1].reg;
9878   inst.instruction |= inst.operands[2].reg << 12;
9879 }
9880
9881 static void
9882 do_iwmmxt_waligni (void)
9883 {
9884   inst.instruction |= inst.operands[0].reg << 12;
9885   inst.instruction |= inst.operands[1].reg << 16;
9886   inst.instruction |= inst.operands[2].reg;
9887   inst.instruction |= inst.operands[3].imm << 20;
9888 }
9889
9890 static void
9891 do_iwmmxt_wmerge (void)
9892 {
9893   inst.instruction |= inst.operands[0].reg << 12;
9894   inst.instruction |= inst.operands[1].reg << 16;
9895   inst.instruction |= inst.operands[2].reg;
9896   inst.instruction |= inst.operands[3].imm << 21;
9897 }
9898
9899 static void
9900 do_iwmmxt_wmov (void)
9901 {
9902   /* WMOV rD, rN is an alias for WOR rD, rN, rN.  */
9903   inst.instruction |= inst.operands[0].reg << 12;
9904   inst.instruction |= inst.operands[1].reg << 16;
9905   inst.instruction |= inst.operands[1].reg;
9906 }
9907
9908 static void
9909 do_iwmmxt_wldstbh (void)
9910 {
9911   int reloc;
9912   inst.instruction |= inst.operands[0].reg << 12;
9913   if (thumb_mode)
9914     reloc = BFD_RELOC_ARM_T32_CP_OFF_IMM_S2;
9915   else
9916     reloc = BFD_RELOC_ARM_CP_OFF_IMM_S2;
9917   encode_arm_cp_address (1, TRUE, FALSE, reloc);
9918 }
9919
9920 static void
9921 do_iwmmxt_wldstw (void)
9922 {
9923   /* RIWR_RIWC clears .isreg for a control register.  */
9924   if (!inst.operands[0].isreg)
9925     {
9926       constraint (inst.cond != COND_ALWAYS, BAD_COND);
9927       inst.instruction |= 0xf0000000;
9928     }
9929
9930   inst.instruction |= inst.operands[0].reg << 12;
9931   encode_arm_cp_address (1, TRUE, TRUE, 0);
9932 }
9933
9934 static void
9935 do_iwmmxt_wldstd (void)
9936 {
9937   inst.instruction |= inst.operands[0].reg << 12;
9938   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2)
9939       && inst.operands[1].immisreg)
9940     {
9941       inst.instruction &= ~0x1a000ff;
9942       inst.instruction |= (0xfU << 28);
9943       if (inst.operands[1].preind)
9944         inst.instruction |= PRE_INDEX;
9945       if (!inst.operands[1].negative)
9946         inst.instruction |= INDEX_UP;
9947       if (inst.operands[1].writeback)
9948         inst.instruction |= WRITE_BACK;
9949       inst.instruction |= inst.operands[1].reg << 16;
9950       inst.instruction |= inst.reloc.exp.X_add_number << 4;
9951       inst.instruction |= inst.operands[1].imm;
9952     }
9953   else
9954     encode_arm_cp_address (1, TRUE, FALSE, 0);
9955 }
9956
9957 static void
9958 do_iwmmxt_wshufh (void)
9959 {
9960   inst.instruction |= inst.operands[0].reg << 12;
9961   inst.instruction |= inst.operands[1].reg << 16;
9962   inst.instruction |= ((inst.operands[2].imm & 0xf0) << 16);
9963   inst.instruction |= (inst.operands[2].imm & 0x0f);
9964 }
9965
9966 static void
9967 do_iwmmxt_wzero (void)
9968 {
9969   /* WZERO reg is an alias for WANDN reg, reg, reg.  */
9970   inst.instruction |= inst.operands[0].reg;
9971   inst.instruction |= inst.operands[0].reg << 12;
9972   inst.instruction |= inst.operands[0].reg << 16;
9973 }
9974
9975 static void
9976 do_iwmmxt_wrwrwr_or_imm5 (void)
9977 {
9978   if (inst.operands[2].isreg)
9979     do_rd_rn_rm ();
9980   else {
9981     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2),
9982                 _("immediate operand requires iWMMXt2"));
9983     do_rd_rn ();
9984     if (inst.operands[2].imm == 0)
9985       {
9986         switch ((inst.instruction >> 20) & 0xf)
9987           {
9988           case 4:
9989           case 5:
9990           case 6:
9991           case 7:
9992             /* w...h wrd, wrn, #0 -> wrorh wrd, wrn, #16.  */
9993             inst.operands[2].imm = 16;
9994             inst.instruction = (inst.instruction & 0xff0fffff) | (0x7 << 20);
9995             break;
9996           case 8:
9997           case 9:
9998           case 10:
9999           case 11:
10000             /* w...w wrd, wrn, #0 -> wrorw wrd, wrn, #32.  */
10001             inst.operands[2].imm = 32;
10002             inst.instruction = (inst.instruction & 0xff0fffff) | (0xb << 20);
10003             break;
10004           case 12:
10005           case 13:
10006           case 14:
10007           case 15:
10008             {
10009               /* w...d wrd, wrn, #0 -> wor wrd, wrn, wrn.  */
10010               unsigned long wrn;
10011               wrn = (inst.instruction >> 16) & 0xf;
10012               inst.instruction &= 0xff0fff0f;
10013               inst.instruction |= wrn;
10014               /* Bail out here; the instruction is now assembled.  */
10015               return;
10016             }
10017           }
10018       }
10019     /* Map 32 -> 0, etc.  */
10020     inst.operands[2].imm &= 0x1f;
10021     inst.instruction |= (0xfU << 28) | ((inst.operands[2].imm & 0x10) << 4) | (inst.operands[2].imm & 0xf);
10022   }
10023 }
10024 \f
10025 /* Cirrus Maverick instructions.  Simple 2-, 3-, and 4-register
10026    operations first, then control, shift, and load/store.  */
10027
10028 /* Insns like "foo X,Y,Z".  */
10029
10030 static void
10031 do_mav_triple (void)
10032 {
10033   inst.instruction |= inst.operands[0].reg << 16;
10034   inst.instruction |= inst.operands[1].reg;
10035   inst.instruction |= inst.operands[2].reg << 12;
10036 }
10037
10038 /* Insns like "foo W,X,Y,Z".
10039     where W=MVAX[0:3] and X,Y,Z=MVFX[0:15].  */
10040
10041 static void
10042 do_mav_quad (void)
10043 {
10044   inst.instruction |= inst.operands[0].reg << 5;
10045   inst.instruction |= inst.operands[1].reg << 12;
10046   inst.instruction |= inst.operands[2].reg << 16;
10047   inst.instruction |= inst.operands[3].reg;
10048 }
10049
10050 /* cfmvsc32<cond> DSPSC,MVDX[15:0].  */
10051 static void
10052 do_mav_dspsc (void)
10053 {
10054   inst.instruction |= inst.operands[1].reg << 12;
10055 }
10056
10057 /* Maverick shift immediate instructions.
10058    cfsh32<cond> MVFX[15:0],MVFX[15:0],Shift[6:0].
10059    cfsh64<cond> MVDX[15:0],MVDX[15:0],Shift[6:0].  */
10060
10061 static void
10062 do_mav_shift (void)
10063 {
10064   int imm = inst.operands[2].imm;
10065
10066   inst.instruction |= inst.operands[0].reg << 12;
10067   inst.instruction |= inst.operands[1].reg << 16;
10068
10069   /* Bits 0-3 of the insn should have bits 0-3 of the immediate.
10070      Bits 5-7 of the insn should have bits 4-6 of the immediate.
10071      Bit 4 should be 0.  */
10072   imm = (imm & 0xf) | ((imm & 0x70) << 1);
10073
10074   inst.instruction |= imm;
10075 }
10076 \f
10077 /* XScale instructions.  Also sorted arithmetic before move.  */
10078
10079 /* Xscale multiply-accumulate (argument parse)
10080      MIAcc   acc0,Rm,Rs
10081      MIAPHcc acc0,Rm,Rs
10082      MIAxycc acc0,Rm,Rs.  */
10083
10084 static void
10085 do_xsc_mia (void)
10086 {
10087   inst.instruction |= inst.operands[1].reg;
10088   inst.instruction |= inst.operands[2].reg << 12;
10089 }
10090
10091 /* Xscale move-accumulator-register (argument parse)
10092
10093      MARcc   acc0,RdLo,RdHi.  */
10094
10095 static void
10096 do_xsc_mar (void)
10097 {
10098   inst.instruction |= inst.operands[1].reg << 12;
10099   inst.instruction |= inst.operands[2].reg << 16;
10100 }
10101
10102 /* Xscale move-register-accumulator (argument parse)
10103
10104      MRAcc   RdLo,RdHi,acc0.  */
10105
10106 static void
10107 do_xsc_mra (void)
10108 {
10109   constraint (inst.operands[0].reg == inst.operands[1].reg, BAD_OVERLAP);
10110   inst.instruction |= inst.operands[0].reg << 12;
10111   inst.instruction |= inst.operands[1].reg << 16;
10112 }
10113 \f
10114 /* Encoding functions relevant only to Thumb.  */
10115
10116 /* inst.operands[i] is a shifted-register operand; encode
10117    it into inst.instruction in the format used by Thumb32.  */
10118
10119 static void
10120 encode_thumb32_shifted_operand (int i)
10121 {
10122   unsigned int value = inst.reloc.exp.X_add_number;
10123   unsigned int shift = inst.operands[i].shift_kind;
10124
10125   constraint (inst.operands[i].immisreg,
10126               _("shift by register not allowed in thumb mode"));
10127   inst.instruction |= inst.operands[i].reg;
10128   if (shift == SHIFT_RRX)
10129     inst.instruction |= SHIFT_ROR << 4;
10130   else
10131     {
10132       constraint (inst.reloc.exp.X_op != O_constant,
10133                   _("expression too complex"));
10134
10135       constraint (value > 32
10136                   || (value == 32 && (shift == SHIFT_LSL
10137                                       || shift == SHIFT_ROR)),
10138                   _("shift expression is too large"));
10139
10140       if (value == 0)
10141         shift = SHIFT_LSL;
10142       else if (value == 32)
10143         value = 0;
10144
10145       inst.instruction |= shift << 4;
10146       inst.instruction |= (value & 0x1c) << 10;
10147       inst.instruction |= (value & 0x03) << 6;
10148     }
10149 }
10150
10151
10152 /* inst.operands[i] was set up by parse_address.  Encode it into a
10153    Thumb32 format load or store instruction.  Reject forms that cannot
10154    be used with such instructions.  If is_t is true, reject forms that
10155    cannot be used with a T instruction; if is_d is true, reject forms
10156    that cannot be used with a D instruction.  If it is a store insn,
10157    reject PC in Rn.  */
10158
10159 static void
10160 encode_thumb32_addr_mode (int i, bfd_boolean is_t, bfd_boolean is_d)
10161 {
10162   const bfd_boolean is_pc = (inst.operands[i].reg == REG_PC);
10163
10164   constraint (!inst.operands[i].isreg,
10165               _("Instruction does not support =N addresses"));
10166
10167   inst.instruction |= inst.operands[i].reg << 16;
10168   if (inst.operands[i].immisreg)
10169     {
10170       constraint (is_pc, BAD_PC_ADDRESSING);
10171       constraint (is_t || is_d, _("cannot use register index with this instruction"));
10172       constraint (inst.operands[i].negative,
10173                   _("Thumb does not support negative register indexing"));
10174       constraint (inst.operands[i].postind,
10175                   _("Thumb does not support register post-indexing"));
10176       constraint (inst.operands[i].writeback,
10177                   _("Thumb does not support register indexing with writeback"));
10178       constraint (inst.operands[i].shifted && inst.operands[i].shift_kind != SHIFT_LSL,
10179                   _("Thumb supports only LSL in shifted register indexing"));
10180
10181       inst.instruction |= inst.operands[i].imm;
10182       if (inst.operands[i].shifted)
10183         {
10184           constraint (inst.reloc.exp.X_op != O_constant,
10185                       _("expression too complex"));
10186           constraint (inst.reloc.exp.X_add_number < 0
10187                       || inst.reloc.exp.X_add_number > 3,
10188                       _("shift out of range"));
10189           inst.instruction |= inst.reloc.exp.X_add_number << 4;
10190         }
10191       inst.reloc.type = BFD_RELOC_UNUSED;
10192     }
10193   else if (inst.operands[i].preind)
10194     {
10195       constraint (is_pc && inst.operands[i].writeback, BAD_PC_WRITEBACK);
10196       constraint (is_t && inst.operands[i].writeback,
10197                   _("cannot use writeback with this instruction"));
10198       constraint (is_pc && ((inst.instruction & THUMB2_LOAD_BIT) == 0),
10199                   BAD_PC_ADDRESSING);
10200
10201       if (is_d)
10202         {
10203           inst.instruction |= 0x01000000;
10204           if (inst.operands[i].writeback)
10205             inst.instruction |= 0x00200000;
10206         }
10207       else
10208         {
10209           inst.instruction |= 0x00000c00;
10210           if (inst.operands[i].writeback)
10211             inst.instruction |= 0x00000100;
10212         }
10213       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10214     }
10215   else if (inst.operands[i].postind)
10216     {
10217       gas_assert (inst.operands[i].writeback);
10218       constraint (is_pc, _("cannot use post-indexing with PC-relative addressing"));
10219       constraint (is_t, _("cannot use post-indexing with this instruction"));
10220
10221       if (is_d)
10222         inst.instruction |= 0x00200000;
10223       else
10224         inst.instruction |= 0x00000900;
10225       inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_IMM;
10226     }
10227   else /* unindexed - only for coprocessor */
10228     inst.error = _("instruction does not accept unindexed addressing");
10229 }
10230
10231 /* Table of Thumb instructions which exist in both 16- and 32-bit
10232    encodings (the latter only in post-V6T2 cores).  The index is the
10233    value used in the insns table below.  When there is more than one
10234    possible 16-bit encoding for the instruction, this table always
10235    holds variant (1).
10236    Also contains several pseudo-instructions used during relaxation.  */
10237 #define T16_32_TAB                              \
10238   X(_adc,   4140, eb400000),                    \
10239   X(_adcs,  4140, eb500000),                    \
10240   X(_add,   1c00, eb000000),                    \
10241   X(_adds,  1c00, eb100000),                    \
10242   X(_addi,  0000, f1000000),                    \
10243   X(_addis, 0000, f1100000),                    \
10244   X(_add_pc,000f, f20f0000),                    \
10245   X(_add_sp,000d, f10d0000),                    \
10246   X(_adr,   000f, f20f0000),                    \
10247   X(_and,   4000, ea000000),                    \
10248   X(_ands,  4000, ea100000),                    \
10249   X(_asr,   1000, fa40f000),                    \
10250   X(_asrs,  1000, fa50f000),                    \
10251   X(_b,     e000, f000b000),                    \
10252   X(_bcond, d000, f0008000),                    \
10253   X(_bic,   4380, ea200000),                    \
10254   X(_bics,  4380, ea300000),                    \
10255   X(_cmn,   42c0, eb100f00),                    \
10256   X(_cmp,   2800, ebb00f00),                    \
10257   X(_cpsie, b660, f3af8400),                    \
10258   X(_cpsid, b670, f3af8600),                    \
10259   X(_cpy,   4600, ea4f0000),                    \
10260   X(_dec_sp,80dd, f1ad0d00),                    \
10261   X(_eor,   4040, ea800000),                    \
10262   X(_eors,  4040, ea900000),                    \
10263   X(_inc_sp,00dd, f10d0d00),                    \
10264   X(_ldmia, c800, e8900000),                    \
10265   X(_ldr,   6800, f8500000),                    \
10266   X(_ldrb,  7800, f8100000),                    \
10267   X(_ldrh,  8800, f8300000),                    \
10268   X(_ldrsb, 5600, f9100000),                    \
10269   X(_ldrsh, 5e00, f9300000),                    \
10270   X(_ldr_pc,4800, f85f0000),                    \
10271   X(_ldr_pc2,4800, f85f0000),                   \
10272   X(_ldr_sp,9800, f85d0000),                    \
10273   X(_lsl,   0000, fa00f000),                    \
10274   X(_lsls,  0000, fa10f000),                    \
10275   X(_lsr,   0800, fa20f000),                    \
10276   X(_lsrs,  0800, fa30f000),                    \
10277   X(_mov,   2000, ea4f0000),                    \
10278   X(_movs,  2000, ea5f0000),                    \
10279   X(_mul,   4340, fb00f000),                     \
10280   X(_muls,  4340, ffffffff), /* no 32b muls */  \
10281   X(_mvn,   43c0, ea6f0000),                    \
10282   X(_mvns,  43c0, ea7f0000),                    \
10283   X(_neg,   4240, f1c00000), /* rsb #0 */       \
10284   X(_negs,  4240, f1d00000), /* rsbs #0 */      \
10285   X(_orr,   4300, ea400000),                    \
10286   X(_orrs,  4300, ea500000),                    \
10287   X(_pop,   bc00, e8bd0000), /* ldmia sp!,... */        \
10288   X(_push,  b400, e92d0000), /* stmdb sp!,... */        \
10289   X(_rev,   ba00, fa90f080),                    \
10290   X(_rev16, ba40, fa90f090),                    \
10291   X(_revsh, bac0, fa90f0b0),                    \
10292   X(_ror,   41c0, fa60f000),                    \
10293   X(_rors,  41c0, fa70f000),                    \
10294   X(_sbc,   4180, eb600000),                    \
10295   X(_sbcs,  4180, eb700000),                    \
10296   X(_stmia, c000, e8800000),                    \
10297   X(_str,   6000, f8400000),                    \
10298   X(_strb,  7000, f8000000),                    \
10299   X(_strh,  8000, f8200000),                    \
10300   X(_str_sp,9000, f84d0000),                    \
10301   X(_sub,   1e00, eba00000),                    \
10302   X(_subs,  1e00, ebb00000),                    \
10303   X(_subi,  8000, f1a00000),                    \
10304   X(_subis, 8000, f1b00000),                    \
10305   X(_sxtb,  b240, fa4ff080),                    \
10306   X(_sxth,  b200, fa0ff080),                    \
10307   X(_tst,   4200, ea100f00),                    \
10308   X(_uxtb,  b2c0, fa5ff080),                    \
10309   X(_uxth,  b280, fa1ff080),                    \
10310   X(_nop,   bf00, f3af8000),                    \
10311   X(_yield, bf10, f3af8001),                    \
10312   X(_wfe,   bf20, f3af8002),                    \
10313   X(_wfi,   bf30, f3af8003),                    \
10314   X(_sev,   bf40, f3af8004),                    \
10315   X(_sevl,  bf50, f3af8005),                    \
10316   X(_udf,   de00, f7f0a000)
10317
10318 /* To catch errors in encoding functions, the codes are all offset by
10319    0xF800, putting them in one of the 32-bit prefix ranges, ergo undefined
10320    as 16-bit instructions.  */
10321 #define X(a,b,c) T_MNEM##a
10322 enum t16_32_codes { T16_32_OFFSET = 0xF7FF, T16_32_TAB };
10323 #undef X
10324
10325 #define X(a,b,c) 0x##b
10326 static const unsigned short thumb_op16[] = { T16_32_TAB };
10327 #define THUMB_OP16(n) (thumb_op16[(n) - (T16_32_OFFSET + 1)])
10328 #undef X
10329
10330 #define X(a,b,c) 0x##c
10331 static const unsigned int thumb_op32[] = { T16_32_TAB };
10332 #define THUMB_OP32(n)        (thumb_op32[(n) - (T16_32_OFFSET + 1)])
10333 #define THUMB_SETS_FLAGS(n)  (THUMB_OP32 (n) & 0x00100000)
10334 #undef X
10335 #undef T16_32_TAB
10336
10337 /* Thumb instruction encoders, in alphabetical order.  */
10338
10339 /* ADDW or SUBW.  */
10340
10341 static void
10342 do_t_add_sub_w (void)
10343 {
10344   int Rd, Rn;
10345
10346   Rd = inst.operands[0].reg;
10347   Rn = inst.operands[1].reg;
10348
10349   /* If Rn is REG_PC, this is ADR; if Rn is REG_SP, then this
10350      is the SP-{plus,minus}-immediate form of the instruction.  */
10351   if (Rn == REG_SP)
10352     constraint (Rd == REG_PC, BAD_PC);
10353   else
10354     reject_bad_reg (Rd);
10355
10356   inst.instruction |= (Rn << 16) | (Rd << 8);
10357   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10358 }
10359
10360 /* Parse an add or subtract instruction.  We get here with inst.instruction
10361    equalling any of THUMB_OPCODE_add, adds, sub, or subs.  */
10362
10363 static void
10364 do_t_add_sub (void)
10365 {
10366   int Rd, Rs, Rn;
10367
10368   Rd = inst.operands[0].reg;
10369   Rs = (inst.operands[1].present
10370         ? inst.operands[1].reg    /* Rd, Rs, foo */
10371         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10372
10373   if (Rd == REG_PC)
10374     set_it_insn_type_last ();
10375
10376   if (unified_syntax)
10377     {
10378       bfd_boolean flags;
10379       bfd_boolean narrow;
10380       int opcode;
10381
10382       flags = (inst.instruction == T_MNEM_adds
10383                || inst.instruction == T_MNEM_subs);
10384       if (flags)
10385         narrow = !in_it_block ();
10386       else
10387         narrow = in_it_block ();
10388       if (!inst.operands[2].isreg)
10389         {
10390           int add;
10391
10392           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10393
10394           add = (inst.instruction == T_MNEM_add
10395                  || inst.instruction == T_MNEM_adds);
10396           opcode = 0;
10397           if (inst.size_req != 4)
10398             {
10399               /* Attempt to use a narrow opcode, with relaxation if
10400                  appropriate.  */
10401               if (Rd == REG_SP && Rs == REG_SP && !flags)
10402                 opcode = add ? T_MNEM_inc_sp : T_MNEM_dec_sp;
10403               else if (Rd <= 7 && Rs == REG_SP && add && !flags)
10404                 opcode = T_MNEM_add_sp;
10405               else if (Rd <= 7 && Rs == REG_PC && add && !flags)
10406                 opcode = T_MNEM_add_pc;
10407               else if (Rd <= 7 && Rs <= 7 && narrow)
10408                 {
10409                   if (flags)
10410                     opcode = add ? T_MNEM_addis : T_MNEM_subis;
10411                   else
10412                     opcode = add ? T_MNEM_addi : T_MNEM_subi;
10413                 }
10414               if (opcode)
10415                 {
10416                   inst.instruction = THUMB_OP16(opcode);
10417                   inst.instruction |= (Rd << 4) | Rs;
10418                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
10419                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
10420                     inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10421                   if (inst.size_req != 2)
10422                     inst.relax = opcode;
10423                 }
10424               else
10425                 constraint (inst.size_req == 2, BAD_HIREG);
10426             }
10427           if (inst.size_req == 4
10428               || (inst.size_req != 2 && !opcode))
10429             {
10430               if (Rd == REG_PC)
10431                 {
10432                   constraint (add, BAD_PC);
10433                   constraint (Rs != REG_LR || inst.instruction != T_MNEM_subs,
10434                              _("only SUBS PC, LR, #const allowed"));
10435                   constraint (inst.reloc.exp.X_op != O_constant,
10436                               _("expression too complex"));
10437                   constraint (inst.reloc.exp.X_add_number < 0
10438                               || inst.reloc.exp.X_add_number > 0xff,
10439                              _("immediate value out of range"));
10440                   inst.instruction = T2_SUBS_PC_LR
10441                                      | inst.reloc.exp.X_add_number;
10442                   inst.reloc.type = BFD_RELOC_UNUSED;
10443                   return;
10444                 }
10445               else if (Rs == REG_PC)
10446                 {
10447                   /* Always use addw/subw.  */
10448                   inst.instruction = add ? 0xf20f0000 : 0xf2af0000;
10449                   inst.reloc.type = BFD_RELOC_ARM_T32_IMM12;
10450                 }
10451               else
10452                 {
10453                   inst.instruction = THUMB_OP32 (inst.instruction);
10454                   inst.instruction = (inst.instruction & 0xe1ffffff)
10455                                      | 0x10000000;
10456                   if (flags)
10457                     inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10458                   else
10459                     inst.reloc.type = BFD_RELOC_ARM_T32_ADD_IMM;
10460                 }
10461               inst.instruction |= Rd << 8;
10462               inst.instruction |= Rs << 16;
10463             }
10464         }
10465       else
10466         {
10467           unsigned int value = inst.reloc.exp.X_add_number;
10468           unsigned int shift = inst.operands[2].shift_kind;
10469
10470           Rn = inst.operands[2].reg;
10471           /* See if we can do this with a 16-bit instruction.  */
10472           if (!inst.operands[2].shifted && inst.size_req != 4)
10473             {
10474               if (Rd > 7 || Rs > 7 || Rn > 7)
10475                 narrow = FALSE;
10476
10477               if (narrow)
10478                 {
10479                   inst.instruction = ((inst.instruction == T_MNEM_adds
10480                                        || inst.instruction == T_MNEM_add)
10481                                       ? T_OPCODE_ADD_R3
10482                                       : T_OPCODE_SUB_R3);
10483                   inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10484                   return;
10485                 }
10486
10487               if (inst.instruction == T_MNEM_add && (Rd == Rs || Rd == Rn))
10488                 {
10489                   /* Thumb-1 cores (except v6-M) require at least one high
10490                      register in a narrow non flag setting add.  */
10491                   if (Rd > 7 || Rn > 7
10492                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2)
10493                       || ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_msr))
10494                     {
10495                       if (Rd == Rn)
10496                         {
10497                           Rn = Rs;
10498                           Rs = Rd;
10499                         }
10500                       inst.instruction = T_OPCODE_ADD_HI;
10501                       inst.instruction |= (Rd & 8) << 4;
10502                       inst.instruction |= (Rd & 7);
10503                       inst.instruction |= Rn << 3;
10504                       return;
10505                     }
10506                 }
10507             }
10508
10509           constraint (Rd == REG_PC, BAD_PC);
10510           constraint (Rd == REG_SP && Rs != REG_SP, BAD_SP);
10511           constraint (Rs == REG_PC, BAD_PC);
10512           reject_bad_reg (Rn);
10513
10514           /* If we get here, it can't be done in 16 bits.  */
10515           constraint (inst.operands[2].shifted && inst.operands[2].immisreg,
10516                       _("shift must be constant"));
10517           inst.instruction = THUMB_OP32 (inst.instruction);
10518           inst.instruction |= Rd << 8;
10519           inst.instruction |= Rs << 16;
10520           constraint (Rd == REG_SP && Rs == REG_SP && value > 3,
10521                       _("shift value over 3 not allowed in thumb mode"));
10522           constraint (Rd == REG_SP && Rs == REG_SP && shift != SHIFT_LSL,
10523                       _("only LSL shift allowed in thumb mode"));
10524           encode_thumb32_shifted_operand (2);
10525         }
10526     }
10527   else
10528     {
10529       constraint (inst.instruction == T_MNEM_adds
10530                   || inst.instruction == T_MNEM_subs,
10531                   BAD_THUMB32);
10532
10533       if (!inst.operands[2].isreg) /* Rd, Rs, #imm */
10534         {
10535           constraint ((Rd > 7 && (Rd != REG_SP || Rs != REG_SP))
10536                       || (Rs > 7 && Rs != REG_SP && Rs != REG_PC),
10537                       BAD_HIREG);
10538
10539           inst.instruction = (inst.instruction == T_MNEM_add
10540                               ? 0x0000 : 0x8000);
10541           inst.instruction |= (Rd << 4) | Rs;
10542           inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10543           return;
10544         }
10545
10546       Rn = inst.operands[2].reg;
10547       constraint (inst.operands[2].shifted, _("unshifted register required"));
10548
10549       /* We now have Rd, Rs, and Rn set to registers.  */
10550       if (Rd > 7 || Rs > 7 || Rn > 7)
10551         {
10552           /* Can't do this for SUB.      */
10553           constraint (inst.instruction == T_MNEM_sub, BAD_HIREG);
10554           inst.instruction = T_OPCODE_ADD_HI;
10555           inst.instruction |= (Rd & 8) << 4;
10556           inst.instruction |= (Rd & 7);
10557           if (Rs == Rd)
10558             inst.instruction |= Rn << 3;
10559           else if (Rn == Rd)
10560             inst.instruction |= Rs << 3;
10561           else
10562             constraint (1, _("dest must overlap one source register"));
10563         }
10564       else
10565         {
10566           inst.instruction = (inst.instruction == T_MNEM_add
10567                               ? T_OPCODE_ADD_R3 : T_OPCODE_SUB_R3);
10568           inst.instruction |= Rd | (Rs << 3) | (Rn << 6);
10569         }
10570     }
10571 }
10572
10573 static void
10574 do_t_adr (void)
10575 {
10576   unsigned Rd;
10577
10578   Rd = inst.operands[0].reg;
10579   reject_bad_reg (Rd);
10580
10581   if (unified_syntax && inst.size_req == 0 && Rd <= 7)
10582     {
10583       /* Defer to section relaxation.  */
10584       inst.relax = inst.instruction;
10585       inst.instruction = THUMB_OP16 (inst.instruction);
10586       inst.instruction |= Rd << 4;
10587     }
10588   else if (unified_syntax && inst.size_req != 2)
10589     {
10590       /* Generate a 32-bit opcode.  */
10591       inst.instruction = THUMB_OP32 (inst.instruction);
10592       inst.instruction |= Rd << 8;
10593       inst.reloc.type = BFD_RELOC_ARM_T32_ADD_PC12;
10594       inst.reloc.pc_rel = 1;
10595     }
10596   else
10597     {
10598       /* Generate a 16-bit opcode.  */
10599       inst.instruction = THUMB_OP16 (inst.instruction);
10600       inst.reloc.type = BFD_RELOC_ARM_THUMB_ADD;
10601       inst.reloc.exp.X_add_number -= 4; /* PC relative adjust.  */
10602       inst.reloc.pc_rel = 1;
10603
10604       inst.instruction |= Rd << 4;
10605     }
10606 }
10607
10608 /* Arithmetic instructions for which there is just one 16-bit
10609    instruction encoding, and it allows only two low registers.
10610    For maximal compatibility with ARM syntax, we allow three register
10611    operands even when Thumb-32 instructions are not available, as long
10612    as the first two are identical.  For instance, both "sbc r0,r1" and
10613    "sbc r0,r0,r1" are allowed.  */
10614 static void
10615 do_t_arit3 (void)
10616 {
10617   int Rd, Rs, Rn;
10618
10619   Rd = inst.operands[0].reg;
10620   Rs = (inst.operands[1].present
10621         ? inst.operands[1].reg    /* Rd, Rs, foo */
10622         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10623   Rn = inst.operands[2].reg;
10624
10625   reject_bad_reg (Rd);
10626   reject_bad_reg (Rs);
10627   if (inst.operands[2].isreg)
10628     reject_bad_reg (Rn);
10629
10630   if (unified_syntax)
10631     {
10632       if (!inst.operands[2].isreg)
10633         {
10634           /* For an immediate, we always generate a 32-bit opcode;
10635              section relaxation will shrink it later if possible.  */
10636           inst.instruction = THUMB_OP32 (inst.instruction);
10637           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10638           inst.instruction |= Rd << 8;
10639           inst.instruction |= Rs << 16;
10640           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10641         }
10642       else
10643         {
10644           bfd_boolean narrow;
10645
10646           /* See if we can do this with a 16-bit instruction.  */
10647           if (THUMB_SETS_FLAGS (inst.instruction))
10648             narrow = !in_it_block ();
10649           else
10650             narrow = in_it_block ();
10651
10652           if (Rd > 7 || Rn > 7 || Rs > 7)
10653             narrow = FALSE;
10654           if (inst.operands[2].shifted)
10655             narrow = FALSE;
10656           if (inst.size_req == 4)
10657             narrow = FALSE;
10658
10659           if (narrow
10660               && Rd == Rs)
10661             {
10662               inst.instruction = THUMB_OP16 (inst.instruction);
10663               inst.instruction |= Rd;
10664               inst.instruction |= Rn << 3;
10665               return;
10666             }
10667
10668           /* If we get here, it can't be done in 16 bits.  */
10669           constraint (inst.operands[2].shifted
10670                       && inst.operands[2].immisreg,
10671                       _("shift must be constant"));
10672           inst.instruction = THUMB_OP32 (inst.instruction);
10673           inst.instruction |= Rd << 8;
10674           inst.instruction |= Rs << 16;
10675           encode_thumb32_shifted_operand (2);
10676         }
10677     }
10678   else
10679     {
10680       /* On its face this is a lie - the instruction does set the
10681          flags.  However, the only supported mnemonic in this mode
10682          says it doesn't.  */
10683       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10684
10685       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10686                   _("unshifted register required"));
10687       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10688       constraint (Rd != Rs,
10689                   _("dest and source1 must be the same register"));
10690
10691       inst.instruction = THUMB_OP16 (inst.instruction);
10692       inst.instruction |= Rd;
10693       inst.instruction |= Rn << 3;
10694     }
10695 }
10696
10697 /* Similarly, but for instructions where the arithmetic operation is
10698    commutative, so we can allow either of them to be different from
10699    the destination operand in a 16-bit instruction.  For instance, all
10700    three of "adc r0,r1", "adc r0,r0,r1", and "adc r0,r1,r0" are
10701    accepted.  */
10702 static void
10703 do_t_arit3c (void)
10704 {
10705   int Rd, Rs, Rn;
10706
10707   Rd = inst.operands[0].reg;
10708   Rs = (inst.operands[1].present
10709         ? inst.operands[1].reg    /* Rd, Rs, foo */
10710         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
10711   Rn = inst.operands[2].reg;
10712
10713   reject_bad_reg (Rd);
10714   reject_bad_reg (Rs);
10715   if (inst.operands[2].isreg)
10716     reject_bad_reg (Rn);
10717
10718   if (unified_syntax)
10719     {
10720       if (!inst.operands[2].isreg)
10721         {
10722           /* For an immediate, we always generate a 32-bit opcode;
10723              section relaxation will shrink it later if possible.  */
10724           inst.instruction = THUMB_OP32 (inst.instruction);
10725           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
10726           inst.instruction |= Rd << 8;
10727           inst.instruction |= Rs << 16;
10728           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
10729         }
10730       else
10731         {
10732           bfd_boolean narrow;
10733
10734           /* See if we can do this with a 16-bit instruction.  */
10735           if (THUMB_SETS_FLAGS (inst.instruction))
10736             narrow = !in_it_block ();
10737           else
10738             narrow = in_it_block ();
10739
10740           if (Rd > 7 || Rn > 7 || Rs > 7)
10741             narrow = FALSE;
10742           if (inst.operands[2].shifted)
10743             narrow = FALSE;
10744           if (inst.size_req == 4)
10745             narrow = FALSE;
10746
10747           if (narrow)
10748             {
10749               if (Rd == Rs)
10750                 {
10751                   inst.instruction = THUMB_OP16 (inst.instruction);
10752                   inst.instruction |= Rd;
10753                   inst.instruction |= Rn << 3;
10754                   return;
10755                 }
10756               if (Rd == Rn)
10757                 {
10758                   inst.instruction = THUMB_OP16 (inst.instruction);
10759                   inst.instruction |= Rd;
10760                   inst.instruction |= Rs << 3;
10761                   return;
10762                 }
10763             }
10764
10765           /* If we get here, it can't be done in 16 bits.  */
10766           constraint (inst.operands[2].shifted
10767                       && inst.operands[2].immisreg,
10768                       _("shift must be constant"));
10769           inst.instruction = THUMB_OP32 (inst.instruction);
10770           inst.instruction |= Rd << 8;
10771           inst.instruction |= Rs << 16;
10772           encode_thumb32_shifted_operand (2);
10773         }
10774     }
10775   else
10776     {
10777       /* On its face this is a lie - the instruction does set the
10778          flags.  However, the only supported mnemonic in this mode
10779          says it doesn't.  */
10780       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
10781
10782       constraint (!inst.operands[2].isreg || inst.operands[2].shifted,
10783                   _("unshifted register required"));
10784       constraint (Rd > 7 || Rs > 7 || Rn > 7, BAD_HIREG);
10785
10786       inst.instruction = THUMB_OP16 (inst.instruction);
10787       inst.instruction |= Rd;
10788
10789       if (Rd == Rs)
10790         inst.instruction |= Rn << 3;
10791       else if (Rd == Rn)
10792         inst.instruction |= Rs << 3;
10793       else
10794         constraint (1, _("dest must overlap one source register"));
10795     }
10796 }
10797
10798 static void
10799 do_t_bfc (void)
10800 {
10801   unsigned Rd;
10802   unsigned int msb = inst.operands[1].imm + inst.operands[2].imm;
10803   constraint (msb > 32, _("bit-field extends past end of register"));
10804   /* The instruction encoding stores the LSB and MSB,
10805      not the LSB and width.  */
10806   Rd = inst.operands[0].reg;
10807   reject_bad_reg (Rd);
10808   inst.instruction |= Rd << 8;
10809   inst.instruction |= (inst.operands[1].imm & 0x1c) << 10;
10810   inst.instruction |= (inst.operands[1].imm & 0x03) << 6;
10811   inst.instruction |= msb - 1;
10812 }
10813
10814 static void
10815 do_t_bfi (void)
10816 {
10817   int Rd, Rn;
10818   unsigned int msb;
10819
10820   Rd = inst.operands[0].reg;
10821   reject_bad_reg (Rd);
10822
10823   /* #0 in second position is alternative syntax for bfc, which is
10824      the same instruction but with REG_PC in the Rm field.  */
10825   if (!inst.operands[1].isreg)
10826     Rn = REG_PC;
10827   else
10828     {
10829       Rn = inst.operands[1].reg;
10830       reject_bad_reg (Rn);
10831     }
10832
10833   msb = inst.operands[2].imm + inst.operands[3].imm;
10834   constraint (msb > 32, _("bit-field extends past end of register"));
10835   /* The instruction encoding stores the LSB and MSB,
10836      not the LSB and width.  */
10837   inst.instruction |= Rd << 8;
10838   inst.instruction |= Rn << 16;
10839   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10840   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10841   inst.instruction |= msb - 1;
10842 }
10843
10844 static void
10845 do_t_bfx (void)
10846 {
10847   unsigned Rd, Rn;
10848
10849   Rd = inst.operands[0].reg;
10850   Rn = inst.operands[1].reg;
10851
10852   reject_bad_reg (Rd);
10853   reject_bad_reg (Rn);
10854
10855   constraint (inst.operands[2].imm + inst.operands[3].imm > 32,
10856               _("bit-field extends past end of register"));
10857   inst.instruction |= Rd << 8;
10858   inst.instruction |= Rn << 16;
10859   inst.instruction |= (inst.operands[2].imm & 0x1c) << 10;
10860   inst.instruction |= (inst.operands[2].imm & 0x03) << 6;
10861   inst.instruction |= inst.operands[3].imm - 1;
10862 }
10863
10864 /* ARM V5 Thumb BLX (argument parse)
10865         BLX <target_addr>       which is BLX(1)
10866         BLX <Rm>                which is BLX(2)
10867    Unfortunately, there are two different opcodes for this mnemonic.
10868    So, the insns[].value is not used, and the code here zaps values
10869         into inst.instruction.
10870
10871    ??? How to take advantage of the additional two bits of displacement
10872    available in Thumb32 mode?  Need new relocation?  */
10873
10874 static void
10875 do_t_blx (void)
10876 {
10877   set_it_insn_type_last ();
10878
10879   if (inst.operands[0].isreg)
10880     {
10881       constraint (inst.operands[0].reg == REG_PC, BAD_PC);
10882       /* We have a register, so this is BLX(2).  */
10883       inst.instruction |= inst.operands[0].reg << 3;
10884     }
10885   else
10886     {
10887       /* No register.  This must be BLX(1).  */
10888       inst.instruction = 0xf000e800;
10889       encode_branch (BFD_RELOC_THUMB_PCREL_BLX);
10890     }
10891 }
10892
10893 static void
10894 do_t_branch (void)
10895 {
10896   int opcode;
10897   int cond;
10898   int reloc;
10899
10900   cond = inst.cond;
10901   set_it_insn_type (IF_INSIDE_IT_LAST_INSN);
10902
10903   if (in_it_block ())
10904     {
10905       /* Conditional branches inside IT blocks are encoded as unconditional
10906          branches.  */
10907       cond = COND_ALWAYS;
10908     }
10909   else
10910     cond = inst.cond;
10911
10912   if (cond != COND_ALWAYS)
10913     opcode = T_MNEM_bcond;
10914   else
10915     opcode = inst.instruction;
10916
10917   if (unified_syntax
10918       && (inst.size_req == 4
10919           || (inst.size_req != 2
10920               && (inst.operands[0].hasreloc
10921                   || inst.reloc.exp.X_op == O_constant))))
10922     {
10923       inst.instruction = THUMB_OP32(opcode);
10924       if (cond == COND_ALWAYS)
10925         reloc = BFD_RELOC_THUMB_PCREL_BRANCH25;
10926       else
10927         {
10928           gas_assert (cond != 0xF);
10929           inst.instruction |= cond << 22;
10930           reloc = BFD_RELOC_THUMB_PCREL_BRANCH20;
10931         }
10932     }
10933   else
10934     {
10935       inst.instruction = THUMB_OP16(opcode);
10936       if (cond == COND_ALWAYS)
10937         reloc = BFD_RELOC_THUMB_PCREL_BRANCH12;
10938       else
10939         {
10940           inst.instruction |= cond << 8;
10941           reloc = BFD_RELOC_THUMB_PCREL_BRANCH9;
10942         }
10943       /* Allow section relaxation.  */
10944       if (unified_syntax && inst.size_req != 2)
10945         inst.relax = opcode;
10946     }
10947   inst.reloc.type = reloc;
10948   inst.reloc.pc_rel = 1;
10949 }
10950
10951 /* Actually do the work for Thumb state bkpt and hlt.  The only difference
10952    between the two is the maximum immediate allowed - which is passed in
10953    RANGE.  */
10954 static void
10955 do_t_bkpt_hlt1 (int range)
10956 {
10957   constraint (inst.cond != COND_ALWAYS,
10958               _("instruction is always unconditional"));
10959   if (inst.operands[0].present)
10960     {
10961       constraint (inst.operands[0].imm > range,
10962                   _("immediate value out of range"));
10963       inst.instruction |= inst.operands[0].imm;
10964     }
10965
10966   set_it_insn_type (NEUTRAL_IT_INSN);
10967 }
10968
10969 static void
10970 do_t_hlt (void)
10971 {
10972   do_t_bkpt_hlt1 (63);
10973 }
10974
10975 static void
10976 do_t_bkpt (void)
10977 {
10978   do_t_bkpt_hlt1 (255);
10979 }
10980
10981 static void
10982 do_t_branch23 (void)
10983 {
10984   set_it_insn_type_last ();
10985   encode_branch (BFD_RELOC_THUMB_PCREL_BRANCH23);
10986
10987   /* md_apply_fix blows up with 'bl foo(PLT)' where foo is defined in
10988      this file.  We used to simply ignore the PLT reloc type here --
10989      the branch encoding is now needed to deal with TLSCALL relocs.
10990      So if we see a PLT reloc now, put it back to how it used to be to
10991      keep the preexisting behaviour.  */
10992   if (inst.reloc.type == BFD_RELOC_ARM_PLT32)
10993     inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH23;
10994
10995 #if defined(OBJ_COFF)
10996   /* If the destination of the branch is a defined symbol which does not have
10997      the THUMB_FUNC attribute, then we must be calling a function which has
10998      the (interfacearm) attribute.  We look for the Thumb entry point to that
10999      function and change the branch to refer to that function instead.  */
11000   if (   inst.reloc.exp.X_op == O_symbol
11001       && inst.reloc.exp.X_add_symbol != NULL
11002       && S_IS_DEFINED (inst.reloc.exp.X_add_symbol)
11003       && ! THUMB_IS_FUNC (inst.reloc.exp.X_add_symbol))
11004     inst.reloc.exp.X_add_symbol =
11005       find_real_start (inst.reloc.exp.X_add_symbol);
11006 #endif
11007 }
11008
11009 static void
11010 do_t_bx (void)
11011 {
11012   set_it_insn_type_last ();
11013   inst.instruction |= inst.operands[0].reg << 3;
11014   /* ??? FIXME: Should add a hacky reloc here if reg is REG_PC.  The reloc
11015      should cause the alignment to be checked once it is known.  This is
11016      because BX PC only works if the instruction is word aligned.  */
11017 }
11018
11019 static void
11020 do_t_bxj (void)
11021 {
11022   int Rm;
11023
11024   set_it_insn_type_last ();
11025   Rm = inst.operands[0].reg;
11026   reject_bad_reg (Rm);
11027   inst.instruction |= Rm << 16;
11028 }
11029
11030 static void
11031 do_t_clz (void)
11032 {
11033   unsigned Rd;
11034   unsigned Rm;
11035
11036   Rd = inst.operands[0].reg;
11037   Rm = inst.operands[1].reg;
11038
11039   reject_bad_reg (Rd);
11040   reject_bad_reg (Rm);
11041
11042   inst.instruction |= Rd << 8;
11043   inst.instruction |= Rm << 16;
11044   inst.instruction |= Rm;
11045 }
11046
11047 static void
11048 do_t_cps (void)
11049 {
11050   set_it_insn_type (OUTSIDE_IT_INSN);
11051   inst.instruction |= inst.operands[0].imm;
11052 }
11053
11054 static void
11055 do_t_cpsi (void)
11056 {
11057   set_it_insn_type (OUTSIDE_IT_INSN);
11058   if (unified_syntax
11059       && (inst.operands[1].present || inst.size_req == 4)
11060       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6_notm))
11061     {
11062       unsigned int imod = (inst.instruction & 0x0030) >> 4;
11063       inst.instruction = 0xf3af8000;
11064       inst.instruction |= imod << 9;
11065       inst.instruction |= inst.operands[0].imm << 5;
11066       if (inst.operands[1].present)
11067         inst.instruction |= 0x100 | inst.operands[1].imm;
11068     }
11069   else
11070     {
11071       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1)
11072                   && (inst.operands[0].imm & 4),
11073                   _("selected processor does not support 'A' form "
11074                     "of this instruction"));
11075       constraint (inst.operands[1].present || inst.size_req == 4,
11076                   _("Thumb does not support the 2-argument "
11077                     "form of this instruction"));
11078       inst.instruction |= inst.operands[0].imm;
11079     }
11080 }
11081
11082 /* THUMB CPY instruction (argument parse).  */
11083
11084 static void
11085 do_t_cpy (void)
11086 {
11087   if (inst.size_req == 4)
11088     {
11089       inst.instruction = THUMB_OP32 (T_MNEM_mov);
11090       inst.instruction |= inst.operands[0].reg << 8;
11091       inst.instruction |= inst.operands[1].reg;
11092     }
11093   else
11094     {
11095       inst.instruction |= (inst.operands[0].reg & 0x8) << 4;
11096       inst.instruction |= (inst.operands[0].reg & 0x7);
11097       inst.instruction |= inst.operands[1].reg << 3;
11098     }
11099 }
11100
11101 static void
11102 do_t_cbz (void)
11103 {
11104   set_it_insn_type (OUTSIDE_IT_INSN);
11105   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11106   inst.instruction |= inst.operands[0].reg;
11107   inst.reloc.pc_rel = 1;
11108   inst.reloc.type = BFD_RELOC_THUMB_PCREL_BRANCH7;
11109 }
11110
11111 static void
11112 do_t_dbg (void)
11113 {
11114   inst.instruction |= inst.operands[0].imm;
11115 }
11116
11117 static void
11118 do_t_div (void)
11119 {
11120   unsigned Rd, Rn, Rm;
11121
11122   Rd = inst.operands[0].reg;
11123   Rn = (inst.operands[1].present
11124         ? inst.operands[1].reg : Rd);
11125   Rm = inst.operands[2].reg;
11126
11127   reject_bad_reg (Rd);
11128   reject_bad_reg (Rn);
11129   reject_bad_reg (Rm);
11130
11131   inst.instruction |= Rd << 8;
11132   inst.instruction |= Rn << 16;
11133   inst.instruction |= Rm;
11134 }
11135
11136 static void
11137 do_t_hint (void)
11138 {
11139   if (unified_syntax && inst.size_req == 4)
11140     inst.instruction = THUMB_OP32 (inst.instruction);
11141   else
11142     inst.instruction = THUMB_OP16 (inst.instruction);
11143 }
11144
11145 static void
11146 do_t_it (void)
11147 {
11148   unsigned int cond = inst.operands[0].imm;
11149
11150   set_it_insn_type (IT_INSN);
11151   now_it.mask = (inst.instruction & 0xf) | 0x10;
11152   now_it.cc = cond;
11153   now_it.warn_deprecated = FALSE;
11154
11155   /* If the condition is a negative condition, invert the mask.  */
11156   if ((cond & 0x1) == 0x0)
11157     {
11158       unsigned int mask = inst.instruction & 0x000f;
11159
11160       if ((mask & 0x7) == 0)
11161         {
11162           /* No conversion needed.  */
11163           now_it.block_length = 1;
11164         }
11165       else if ((mask & 0x3) == 0)
11166         {
11167           mask ^= 0x8;
11168           now_it.block_length = 2;
11169         }
11170       else if ((mask & 0x1) == 0)
11171         {
11172           mask ^= 0xC;
11173           now_it.block_length = 3;
11174         }
11175       else
11176         {
11177           mask ^= 0xE;
11178           now_it.block_length = 4;
11179         }
11180
11181       inst.instruction &= 0xfff0;
11182       inst.instruction |= mask;
11183     }
11184
11185   inst.instruction |= cond << 4;
11186 }
11187
11188 /* Helper function used for both push/pop and ldm/stm.  */
11189 static void
11190 encode_thumb2_ldmstm (int base, unsigned mask, bfd_boolean writeback)
11191 {
11192   bfd_boolean load;
11193
11194   load = (inst.instruction & (1 << 20)) != 0;
11195
11196   if (mask & (1 << 13))
11197     inst.error =  _("SP not allowed in register list");
11198
11199   if ((mask & (1 << base)) != 0
11200       && writeback)
11201     inst.error = _("having the base register in the register list when "
11202                    "using write back is UNPREDICTABLE");
11203
11204   if (load)
11205     {
11206       if (mask & (1 << 15))
11207         {
11208           if (mask & (1 << 14))
11209             inst.error = _("LR and PC should not both be in register list");
11210           else
11211             set_it_insn_type_last ();
11212         }
11213     }
11214   else
11215     {
11216       if (mask & (1 << 15))
11217         inst.error = _("PC not allowed in register list");
11218     }
11219
11220   if ((mask & (mask - 1)) == 0)
11221     {
11222       /* Single register transfers implemented as str/ldr.  */
11223       if (writeback)
11224         {
11225           if (inst.instruction & (1 << 23))
11226             inst.instruction = 0x00000b04; /* ia! -> [base], #4 */
11227           else
11228             inst.instruction = 0x00000d04; /* db! -> [base, #-4]! */
11229         }
11230       else
11231         {
11232           if (inst.instruction & (1 << 23))
11233             inst.instruction = 0x00800000; /* ia -> [base] */
11234           else
11235             inst.instruction = 0x00000c04; /* db -> [base, #-4] */
11236         }
11237
11238       inst.instruction |= 0xf8400000;
11239       if (load)
11240         inst.instruction |= 0x00100000;
11241
11242       mask = ffs (mask) - 1;
11243       mask <<= 12;
11244     }
11245   else if (writeback)
11246     inst.instruction |= WRITE_BACK;
11247
11248   inst.instruction |= mask;
11249   inst.instruction |= base << 16;
11250 }
11251
11252 static void
11253 do_t_ldmstm (void)
11254 {
11255   /* This really doesn't seem worth it.  */
11256   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
11257               _("expression too complex"));
11258   constraint (inst.operands[1].writeback,
11259               _("Thumb load/store multiple does not support {reglist}^"));
11260
11261   if (unified_syntax)
11262     {
11263       bfd_boolean narrow;
11264       unsigned mask;
11265
11266       narrow = FALSE;
11267       /* See if we can use a 16-bit instruction.  */
11268       if (inst.instruction < 0xffff /* not ldmdb/stmdb */
11269           && inst.size_req != 4
11270           && !(inst.operands[1].imm & ~0xff))
11271         {
11272           mask = 1 << inst.operands[0].reg;
11273
11274           if (inst.operands[0].reg <= 7)
11275             {
11276               if (inst.instruction == T_MNEM_stmia
11277                   ? inst.operands[0].writeback
11278                   : (inst.operands[0].writeback
11279                      == !(inst.operands[1].imm & mask)))
11280                 {
11281                   if (inst.instruction == T_MNEM_stmia
11282                       && (inst.operands[1].imm & mask)
11283                       && (inst.operands[1].imm & (mask - 1)))
11284                     as_warn (_("value stored for r%d is UNKNOWN"),
11285                              inst.operands[0].reg);
11286
11287                   inst.instruction = THUMB_OP16 (inst.instruction);
11288                   inst.instruction |= inst.operands[0].reg << 8;
11289                   inst.instruction |= inst.operands[1].imm;
11290                   narrow = TRUE;
11291                 }
11292               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11293                 {
11294                   /* This means 1 register in reg list one of 3 situations:
11295                      1. Instruction is stmia, but without writeback.
11296                      2. lmdia without writeback, but with Rn not in
11297                         reglist.
11298                      3. ldmia with writeback, but with Rn in reglist.
11299                      Case 3 is UNPREDICTABLE behaviour, so we handle
11300                      case 1 and 2 which can be converted into a 16-bit
11301                      str or ldr. The SP cases are handled below.  */
11302                   unsigned long opcode;
11303                   /* First, record an error for Case 3.  */
11304                   if (inst.operands[1].imm & mask
11305                       && inst.operands[0].writeback)
11306                     inst.error =
11307                         _("having the base register in the register list when "
11308                           "using write back is UNPREDICTABLE");
11309
11310                   opcode = (inst.instruction == T_MNEM_stmia ? T_MNEM_str
11311                                                              : T_MNEM_ldr);
11312                   inst.instruction = THUMB_OP16 (opcode);
11313                   inst.instruction |= inst.operands[0].reg << 3;
11314                   inst.instruction |= (ffs (inst.operands[1].imm)-1);
11315                   narrow = TRUE;
11316                 }
11317             }
11318           else if (inst.operands[0] .reg == REG_SP)
11319             {
11320               if (inst.operands[0].writeback)
11321                 {
11322                   inst.instruction =
11323                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11324                                     ? T_MNEM_push : T_MNEM_pop);
11325                   inst.instruction |= inst.operands[1].imm;
11326                   narrow = TRUE;
11327                 }
11328               else if ((inst.operands[1].imm & (inst.operands[1].imm-1)) == 0)
11329                 {
11330                   inst.instruction =
11331                         THUMB_OP16 (inst.instruction == T_MNEM_stmia
11332                                     ? T_MNEM_str_sp : T_MNEM_ldr_sp);
11333                   inst.instruction |= ((ffs (inst.operands[1].imm)-1) << 8);
11334                   narrow = TRUE;
11335                 }
11336             }
11337         }
11338
11339       if (!narrow)
11340         {
11341           if (inst.instruction < 0xffff)
11342             inst.instruction = THUMB_OP32 (inst.instruction);
11343
11344           encode_thumb2_ldmstm (inst.operands[0].reg, inst.operands[1].imm,
11345                                 inst.operands[0].writeback);
11346         }
11347     }
11348   else
11349     {
11350       constraint (inst.operands[0].reg > 7
11351                   || (inst.operands[1].imm & ~0xff), BAD_HIREG);
11352       constraint (inst.instruction != T_MNEM_ldmia
11353                   && inst.instruction != T_MNEM_stmia,
11354                   _("Thumb-2 instruction only valid in unified syntax"));
11355       if (inst.instruction == T_MNEM_stmia)
11356         {
11357           if (!inst.operands[0].writeback)
11358             as_warn (_("this instruction will write back the base register"));
11359           if ((inst.operands[1].imm & (1 << inst.operands[0].reg))
11360               && (inst.operands[1].imm & ((1 << inst.operands[0].reg) - 1)))
11361             as_warn (_("value stored for r%d is UNKNOWN"),
11362                      inst.operands[0].reg);
11363         }
11364       else
11365         {
11366           if (!inst.operands[0].writeback
11367               && !(inst.operands[1].imm & (1 << inst.operands[0].reg)))
11368             as_warn (_("this instruction will write back the base register"));
11369           else if (inst.operands[0].writeback
11370                    && (inst.operands[1].imm & (1 << inst.operands[0].reg)))
11371             as_warn (_("this instruction will not write back the base register"));
11372         }
11373
11374       inst.instruction = THUMB_OP16 (inst.instruction);
11375       inst.instruction |= inst.operands[0].reg << 8;
11376       inst.instruction |= inst.operands[1].imm;
11377     }
11378 }
11379
11380 static void
11381 do_t_ldrex (void)
11382 {
11383   constraint (!inst.operands[1].isreg || !inst.operands[1].preind
11384               || inst.operands[1].postind || inst.operands[1].writeback
11385               || inst.operands[1].immisreg || inst.operands[1].shifted
11386               || inst.operands[1].negative,
11387               BAD_ADDR_MODE);
11388
11389   constraint ((inst.operands[1].reg == REG_PC), BAD_PC);
11390
11391   inst.instruction |= inst.operands[0].reg << 12;
11392   inst.instruction |= inst.operands[1].reg << 16;
11393   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
11394 }
11395
11396 static void
11397 do_t_ldrexd (void)
11398 {
11399   if (!inst.operands[1].present)
11400     {
11401       constraint (inst.operands[0].reg == REG_LR,
11402                   _("r14 not allowed as first register "
11403                     "when second register is omitted"));
11404       inst.operands[1].reg = inst.operands[0].reg + 1;
11405     }
11406   constraint (inst.operands[0].reg == inst.operands[1].reg,
11407               BAD_OVERLAP);
11408
11409   inst.instruction |= inst.operands[0].reg << 12;
11410   inst.instruction |= inst.operands[1].reg << 8;
11411   inst.instruction |= inst.operands[2].reg << 16;
11412 }
11413
11414 static void
11415 do_t_ldst (void)
11416 {
11417   unsigned long opcode;
11418   int Rn;
11419
11420   if (inst.operands[0].isreg
11421       && !inst.operands[0].preind
11422       && inst.operands[0].reg == REG_PC)
11423     set_it_insn_type_last ();
11424
11425   opcode = inst.instruction;
11426   if (unified_syntax)
11427     {
11428       if (!inst.operands[1].isreg)
11429         {
11430           if (opcode <= 0xffff)
11431             inst.instruction = THUMB_OP32 (opcode);
11432           if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11433             return;
11434         }
11435       if (inst.operands[1].isreg
11436           && !inst.operands[1].writeback
11437           && !inst.operands[1].shifted && !inst.operands[1].postind
11438           && !inst.operands[1].negative && inst.operands[0].reg <= 7
11439           && opcode <= 0xffff
11440           && inst.size_req != 4)
11441         {
11442           /* Insn may have a 16-bit form.  */
11443           Rn = inst.operands[1].reg;
11444           if (inst.operands[1].immisreg)
11445             {
11446               inst.instruction = THUMB_OP16 (opcode);
11447               /* [Rn, Rik] */
11448               if (Rn <= 7 && inst.operands[1].imm <= 7)
11449                 goto op16;
11450               else if (opcode != T_MNEM_ldr && opcode != T_MNEM_str)
11451                 reject_bad_reg (inst.operands[1].imm);
11452             }
11453           else if ((Rn <= 7 && opcode != T_MNEM_ldrsh
11454                     && opcode != T_MNEM_ldrsb)
11455                    || ((Rn == REG_PC || Rn == REG_SP) && opcode == T_MNEM_ldr)
11456                    || (Rn == REG_SP && opcode == T_MNEM_str))
11457             {
11458               /* [Rn, #const] */
11459               if (Rn > 7)
11460                 {
11461                   if (Rn == REG_PC)
11462                     {
11463                       if (inst.reloc.pc_rel)
11464                         opcode = T_MNEM_ldr_pc2;
11465                       else
11466                         opcode = T_MNEM_ldr_pc;
11467                     }
11468                   else
11469                     {
11470                       if (opcode == T_MNEM_ldr)
11471                         opcode = T_MNEM_ldr_sp;
11472                       else
11473                         opcode = T_MNEM_str_sp;
11474                     }
11475                   inst.instruction = inst.operands[0].reg << 8;
11476                 }
11477               else
11478                 {
11479                   inst.instruction = inst.operands[0].reg;
11480                   inst.instruction |= inst.operands[1].reg << 3;
11481                 }
11482               inst.instruction |= THUMB_OP16 (opcode);
11483               if (inst.size_req == 2)
11484                 inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11485               else
11486                 inst.relax = opcode;
11487               return;
11488             }
11489         }
11490       /* Definitely a 32-bit variant.  */
11491
11492       /* Warning for Erratum 752419.  */
11493       if (opcode == T_MNEM_ldr
11494           && inst.operands[0].reg == REG_SP
11495           && inst.operands[1].writeback == 1
11496           && !inst.operands[1].immisreg)
11497         {
11498           if (no_cpu_selected ()
11499               || (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7)
11500                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a)
11501                   && !ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7r)))
11502             as_warn (_("This instruction may be unpredictable "
11503                        "if executed on M-profile cores "
11504                        "with interrupts enabled."));
11505         }
11506
11507       /* Do some validations regarding addressing modes.  */
11508       if (inst.operands[1].immisreg)
11509         reject_bad_reg (inst.operands[1].imm);
11510
11511       constraint (inst.operands[1].writeback == 1
11512                   && inst.operands[0].reg == inst.operands[1].reg,
11513                   BAD_OVERLAP);
11514
11515       inst.instruction = THUMB_OP32 (opcode);
11516       inst.instruction |= inst.operands[0].reg << 12;
11517       encode_thumb32_addr_mode (1, /*is_t=*/FALSE, /*is_d=*/FALSE);
11518       check_ldr_r15_aligned ();
11519       return;
11520     }
11521
11522   constraint (inst.operands[0].reg > 7, BAD_HIREG);
11523
11524   if (inst.instruction == T_MNEM_ldrsh || inst.instruction == T_MNEM_ldrsb)
11525     {
11526       /* Only [Rn,Rm] is acceptable.  */
11527       constraint (inst.operands[1].reg > 7 || inst.operands[1].imm > 7, BAD_HIREG);
11528       constraint (!inst.operands[1].isreg || !inst.operands[1].immisreg
11529                   || inst.operands[1].postind || inst.operands[1].shifted
11530                   || inst.operands[1].negative,
11531                   _("Thumb does not support this addressing mode"));
11532       inst.instruction = THUMB_OP16 (inst.instruction);
11533       goto op16;
11534     }
11535
11536   inst.instruction = THUMB_OP16 (inst.instruction);
11537   if (!inst.operands[1].isreg)
11538     if (move_or_literal_pool (0, CONST_THUMB, /*mode_3=*/FALSE))
11539       return;
11540
11541   constraint (!inst.operands[1].preind
11542               || inst.operands[1].shifted
11543               || inst.operands[1].writeback,
11544               _("Thumb does not support this addressing mode"));
11545   if (inst.operands[1].reg == REG_PC || inst.operands[1].reg == REG_SP)
11546     {
11547       constraint (inst.instruction & 0x0600,
11548                   _("byte or halfword not valid for base register"));
11549       constraint (inst.operands[1].reg == REG_PC
11550                   && !(inst.instruction & THUMB_LOAD_BIT),
11551                   _("r15 based store not allowed"));
11552       constraint (inst.operands[1].immisreg,
11553                   _("invalid base register for register offset"));
11554
11555       if (inst.operands[1].reg == REG_PC)
11556         inst.instruction = T_OPCODE_LDR_PC;
11557       else if (inst.instruction & THUMB_LOAD_BIT)
11558         inst.instruction = T_OPCODE_LDR_SP;
11559       else
11560         inst.instruction = T_OPCODE_STR_SP;
11561
11562       inst.instruction |= inst.operands[0].reg << 8;
11563       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11564       return;
11565     }
11566
11567   constraint (inst.operands[1].reg > 7, BAD_HIREG);
11568   if (!inst.operands[1].immisreg)
11569     {
11570       /* Immediate offset.  */
11571       inst.instruction |= inst.operands[0].reg;
11572       inst.instruction |= inst.operands[1].reg << 3;
11573       inst.reloc.type = BFD_RELOC_ARM_THUMB_OFFSET;
11574       return;
11575     }
11576
11577   /* Register offset.  */
11578   constraint (inst.operands[1].imm > 7, BAD_HIREG);
11579   constraint (inst.operands[1].negative,
11580               _("Thumb does not support this addressing mode"));
11581
11582  op16:
11583   switch (inst.instruction)
11584     {
11585     case T_OPCODE_STR_IW: inst.instruction = T_OPCODE_STR_RW; break;
11586     case T_OPCODE_STR_IH: inst.instruction = T_OPCODE_STR_RH; break;
11587     case T_OPCODE_STR_IB: inst.instruction = T_OPCODE_STR_RB; break;
11588     case T_OPCODE_LDR_IW: inst.instruction = T_OPCODE_LDR_RW; break;
11589     case T_OPCODE_LDR_IH: inst.instruction = T_OPCODE_LDR_RH; break;
11590     case T_OPCODE_LDR_IB: inst.instruction = T_OPCODE_LDR_RB; break;
11591     case 0x5600 /* ldrsb */:
11592     case 0x5e00 /* ldrsh */: break;
11593     default: abort ();
11594     }
11595
11596   inst.instruction |= inst.operands[0].reg;
11597   inst.instruction |= inst.operands[1].reg << 3;
11598   inst.instruction |= inst.operands[1].imm << 6;
11599 }
11600
11601 static void
11602 do_t_ldstd (void)
11603 {
11604   if (!inst.operands[1].present)
11605     {
11606       inst.operands[1].reg = inst.operands[0].reg + 1;
11607       constraint (inst.operands[0].reg == REG_LR,
11608                   _("r14 not allowed here"));
11609       constraint (inst.operands[0].reg == REG_R12,
11610                   _("r12 not allowed here"));
11611     }
11612
11613   if (inst.operands[2].writeback
11614       && (inst.operands[0].reg == inst.operands[2].reg
11615       || inst.operands[1].reg == inst.operands[2].reg))
11616     as_warn (_("base register written back, and overlaps "
11617                "one of transfer registers"));
11618
11619   inst.instruction |= inst.operands[0].reg << 12;
11620   inst.instruction |= inst.operands[1].reg << 8;
11621   encode_thumb32_addr_mode (2, /*is_t=*/FALSE, /*is_d=*/TRUE);
11622 }
11623
11624 static void
11625 do_t_ldstt (void)
11626 {
11627   inst.instruction |= inst.operands[0].reg << 12;
11628   encode_thumb32_addr_mode (1, /*is_t=*/TRUE, /*is_d=*/FALSE);
11629 }
11630
11631 static void
11632 do_t_mla (void)
11633 {
11634   unsigned Rd, Rn, Rm, Ra;
11635
11636   Rd = inst.operands[0].reg;
11637   Rn = inst.operands[1].reg;
11638   Rm = inst.operands[2].reg;
11639   Ra = inst.operands[3].reg;
11640
11641   reject_bad_reg (Rd);
11642   reject_bad_reg (Rn);
11643   reject_bad_reg (Rm);
11644   reject_bad_reg (Ra);
11645
11646   inst.instruction |= Rd << 8;
11647   inst.instruction |= Rn << 16;
11648   inst.instruction |= Rm;
11649   inst.instruction |= Ra << 12;
11650 }
11651
11652 static void
11653 do_t_mlal (void)
11654 {
11655   unsigned RdLo, RdHi, Rn, Rm;
11656
11657   RdLo = inst.operands[0].reg;
11658   RdHi = inst.operands[1].reg;
11659   Rn = inst.operands[2].reg;
11660   Rm = inst.operands[3].reg;
11661
11662   reject_bad_reg (RdLo);
11663   reject_bad_reg (RdHi);
11664   reject_bad_reg (Rn);
11665   reject_bad_reg (Rm);
11666
11667   inst.instruction |= RdLo << 12;
11668   inst.instruction |= RdHi << 8;
11669   inst.instruction |= Rn << 16;
11670   inst.instruction |= Rm;
11671 }
11672
11673 static void
11674 do_t_mov_cmp (void)
11675 {
11676   unsigned Rn, Rm;
11677
11678   Rn = inst.operands[0].reg;
11679   Rm = inst.operands[1].reg;
11680
11681   if (Rn == REG_PC)
11682     set_it_insn_type_last ();
11683
11684   if (unified_syntax)
11685     {
11686       int r0off = (inst.instruction == T_MNEM_mov
11687                    || inst.instruction == T_MNEM_movs) ? 8 : 16;
11688       unsigned long opcode;
11689       bfd_boolean narrow;
11690       bfd_boolean low_regs;
11691
11692       low_regs = (Rn <= 7 && Rm <= 7);
11693       opcode = inst.instruction;
11694       if (in_it_block ())
11695         narrow = opcode != T_MNEM_movs;
11696       else
11697         narrow = opcode != T_MNEM_movs || low_regs;
11698       if (inst.size_req == 4
11699           || inst.operands[1].shifted)
11700         narrow = FALSE;
11701
11702       /* MOVS PC, LR is encoded as SUBS PC, LR, #0.  */
11703       if (opcode == T_MNEM_movs && inst.operands[1].isreg
11704           && !inst.operands[1].shifted
11705           && Rn == REG_PC
11706           && Rm == REG_LR)
11707         {
11708           inst.instruction = T2_SUBS_PC_LR;
11709           return;
11710         }
11711
11712       if (opcode == T_MNEM_cmp)
11713         {
11714           constraint (Rn == REG_PC, BAD_PC);
11715           if (narrow)
11716             {
11717               /* In the Thumb-2 ISA, use of R13 as Rm is deprecated,
11718                  but valid.  */
11719               warn_deprecated_sp (Rm);
11720               /* R15 was documented as a valid choice for Rm in ARMv6,
11721                  but as UNPREDICTABLE in ARMv7.  ARM's proprietary
11722                  tools reject R15, so we do too.  */
11723               constraint (Rm == REG_PC, BAD_PC);
11724             }
11725           else
11726             reject_bad_reg (Rm);
11727         }
11728       else if (opcode == T_MNEM_mov
11729                || opcode == T_MNEM_movs)
11730         {
11731           if (inst.operands[1].isreg)
11732             {
11733               if (opcode == T_MNEM_movs)
11734                 {
11735                   reject_bad_reg (Rn);
11736                   reject_bad_reg (Rm);
11737                 }
11738               else if (narrow)
11739                 {
11740                   /* This is mov.n.  */
11741                   if ((Rn == REG_SP || Rn == REG_PC)
11742                       && (Rm == REG_SP || Rm == REG_PC))
11743                     {
11744                       as_tsktsk (_("Use of r%u as a source register is "
11745                                  "deprecated when r%u is the destination "
11746                                  "register."), Rm, Rn);
11747                     }
11748                 }
11749               else
11750                 {
11751                   /* This is mov.w.  */
11752                   constraint (Rn == REG_PC, BAD_PC);
11753                   constraint (Rm == REG_PC, BAD_PC);
11754                   constraint (Rn == REG_SP && Rm == REG_SP, BAD_SP);
11755                 }
11756             }
11757           else
11758             reject_bad_reg (Rn);
11759         }
11760
11761       if (!inst.operands[1].isreg)
11762         {
11763           /* Immediate operand.  */
11764           if (!in_it_block () && opcode == T_MNEM_mov)
11765             narrow = 0;
11766           if (low_regs && narrow)
11767             {
11768               inst.instruction = THUMB_OP16 (opcode);
11769               inst.instruction |= Rn << 8;
11770               if (inst.size_req == 2)
11771                 {
11772                   if (inst.reloc.type < BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
11773                       || inst.reloc.type > BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
11774                     inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11775                 }
11776               else
11777                   inst.relax = opcode;
11778             }
11779           else
11780             {
11781               inst.instruction = THUMB_OP32 (inst.instruction);
11782               inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
11783               inst.instruction |= Rn << r0off;
11784               inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
11785             }
11786         }
11787       else if (inst.operands[1].shifted && inst.operands[1].immisreg
11788                && (inst.instruction == T_MNEM_mov
11789                    || inst.instruction == T_MNEM_movs))
11790         {
11791           /* Register shifts are encoded as separate shift instructions.  */
11792           bfd_boolean flags = (inst.instruction == T_MNEM_movs);
11793
11794           if (in_it_block ())
11795             narrow = !flags;
11796           else
11797             narrow = flags;
11798
11799           if (inst.size_req == 4)
11800             narrow = FALSE;
11801
11802           if (!low_regs || inst.operands[1].imm > 7)
11803             narrow = FALSE;
11804
11805           if (Rn != Rm)
11806             narrow = FALSE;
11807
11808           switch (inst.operands[1].shift_kind)
11809             {
11810             case SHIFT_LSL:
11811               opcode = narrow ? T_OPCODE_LSL_R : THUMB_OP32 (T_MNEM_lsl);
11812               break;
11813             case SHIFT_ASR:
11814               opcode = narrow ? T_OPCODE_ASR_R : THUMB_OP32 (T_MNEM_asr);
11815               break;
11816             case SHIFT_LSR:
11817               opcode = narrow ? T_OPCODE_LSR_R : THUMB_OP32 (T_MNEM_lsr);
11818               break;
11819             case SHIFT_ROR:
11820               opcode = narrow ? T_OPCODE_ROR_R : THUMB_OP32 (T_MNEM_ror);
11821               break;
11822             default:
11823               abort ();
11824             }
11825
11826           inst.instruction = opcode;
11827           if (narrow)
11828             {
11829               inst.instruction |= Rn;
11830               inst.instruction |= inst.operands[1].imm << 3;
11831             }
11832           else
11833             {
11834               if (flags)
11835                 inst.instruction |= CONDS_BIT;
11836
11837               inst.instruction |= Rn << 8;
11838               inst.instruction |= Rm << 16;
11839               inst.instruction |= inst.operands[1].imm;
11840             }
11841         }
11842       else if (!narrow)
11843         {
11844           /* Some mov with immediate shift have narrow variants.
11845              Register shifts are handled above.  */
11846           if (low_regs && inst.operands[1].shifted
11847               && (inst.instruction == T_MNEM_mov
11848                   || inst.instruction == T_MNEM_movs))
11849             {
11850               if (in_it_block ())
11851                 narrow = (inst.instruction == T_MNEM_mov);
11852               else
11853                 narrow = (inst.instruction == T_MNEM_movs);
11854             }
11855
11856           if (narrow)
11857             {
11858               switch (inst.operands[1].shift_kind)
11859                 {
11860                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
11861                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
11862                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
11863                 default: narrow = FALSE; break;
11864                 }
11865             }
11866
11867           if (narrow)
11868             {
11869               inst.instruction |= Rn;
11870               inst.instruction |= Rm << 3;
11871               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
11872             }
11873           else
11874             {
11875               inst.instruction = THUMB_OP32 (inst.instruction);
11876               inst.instruction |= Rn << r0off;
11877               encode_thumb32_shifted_operand (1);
11878             }
11879         }
11880       else
11881         switch (inst.instruction)
11882           {
11883           case T_MNEM_mov:
11884             /* In v4t or v5t a move of two lowregs produces unpredictable
11885                results. Don't allow this.  */
11886             if (low_regs)
11887               {
11888                 constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6),
11889                             "MOV Rd, Rs with two low registers is not "
11890                             "permitted on this architecture");
11891                 ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
11892                                         arm_ext_v6);
11893               }
11894
11895             inst.instruction = T_OPCODE_MOV_HR;
11896             inst.instruction |= (Rn & 0x8) << 4;
11897             inst.instruction |= (Rn & 0x7);
11898             inst.instruction |= Rm << 3;
11899             break;
11900
11901           case T_MNEM_movs:
11902             /* We know we have low registers at this point.
11903                Generate LSLS Rd, Rs, #0.  */
11904             inst.instruction = T_OPCODE_LSL_I;
11905             inst.instruction |= Rn;
11906             inst.instruction |= Rm << 3;
11907             break;
11908
11909           case T_MNEM_cmp:
11910             if (low_regs)
11911               {
11912                 inst.instruction = T_OPCODE_CMP_LR;
11913                 inst.instruction |= Rn;
11914                 inst.instruction |= Rm << 3;
11915               }
11916             else
11917               {
11918                 inst.instruction = T_OPCODE_CMP_HR;
11919                 inst.instruction |= (Rn & 0x8) << 4;
11920                 inst.instruction |= (Rn & 0x7);
11921                 inst.instruction |= Rm << 3;
11922               }
11923             break;
11924           }
11925       return;
11926     }
11927
11928   inst.instruction = THUMB_OP16 (inst.instruction);
11929
11930   /* PR 10443: Do not silently ignore shifted operands.  */
11931   constraint (inst.operands[1].shifted,
11932               _("shifts in CMP/MOV instructions are only supported in unified syntax"));
11933
11934   if (inst.operands[1].isreg)
11935     {
11936       if (Rn < 8 && Rm < 8)
11937         {
11938           /* A move of two lowregs is encoded as ADD Rd, Rs, #0
11939              since a MOV instruction produces unpredictable results.  */
11940           if (inst.instruction == T_OPCODE_MOV_I8)
11941             inst.instruction = T_OPCODE_ADD_I3;
11942           else
11943             inst.instruction = T_OPCODE_CMP_LR;
11944
11945           inst.instruction |= Rn;
11946           inst.instruction |= Rm << 3;
11947         }
11948       else
11949         {
11950           if (inst.instruction == T_OPCODE_MOV_I8)
11951             inst.instruction = T_OPCODE_MOV_HR;
11952           else
11953             inst.instruction = T_OPCODE_CMP_HR;
11954           do_t_cpy ();
11955         }
11956     }
11957   else
11958     {
11959       constraint (Rn > 7,
11960                   _("only lo regs allowed with immediate"));
11961       inst.instruction |= Rn << 8;
11962       inst.reloc.type = BFD_RELOC_ARM_THUMB_IMM;
11963     }
11964 }
11965
11966 static void
11967 do_t_mov16 (void)
11968 {
11969   unsigned Rd;
11970   bfd_vma imm;
11971   bfd_boolean top;
11972
11973   top = (inst.instruction & 0x00800000) != 0;
11974   if (inst.reloc.type == BFD_RELOC_ARM_MOVW)
11975     {
11976       constraint (top, _(":lower16: not allowed this instruction"));
11977       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVW;
11978     }
11979   else if (inst.reloc.type == BFD_RELOC_ARM_MOVT)
11980     {
11981       constraint (!top, _(":upper16: not allowed this instruction"));
11982       inst.reloc.type = BFD_RELOC_ARM_THUMB_MOVT;
11983     }
11984
11985   Rd = inst.operands[0].reg;
11986   reject_bad_reg (Rd);
11987
11988   inst.instruction |= Rd << 8;
11989   if (inst.reloc.type == BFD_RELOC_UNUSED)
11990     {
11991       imm = inst.reloc.exp.X_add_number;
11992       inst.instruction |= (imm & 0xf000) << 4;
11993       inst.instruction |= (imm & 0x0800) << 15;
11994       inst.instruction |= (imm & 0x0700) << 4;
11995       inst.instruction |= (imm & 0x00ff);
11996     }
11997 }
11998
11999 static void
12000 do_t_mvn_tst (void)
12001 {
12002   unsigned Rn, Rm;
12003
12004   Rn = inst.operands[0].reg;
12005   Rm = inst.operands[1].reg;
12006
12007   if (inst.instruction == T_MNEM_cmp
12008       || inst.instruction == T_MNEM_cmn)
12009     constraint (Rn == REG_PC, BAD_PC);
12010   else
12011     reject_bad_reg (Rn);
12012   reject_bad_reg (Rm);
12013
12014   if (unified_syntax)
12015     {
12016       int r0off = (inst.instruction == T_MNEM_mvn
12017                    || inst.instruction == T_MNEM_mvns) ? 8 : 16;
12018       bfd_boolean narrow;
12019
12020       if (inst.size_req == 4
12021           || inst.instruction > 0xffff
12022           || inst.operands[1].shifted
12023           || Rn > 7 || Rm > 7)
12024         narrow = FALSE;
12025       else if (inst.instruction == T_MNEM_cmn
12026                || inst.instruction == T_MNEM_tst)
12027         narrow = TRUE;
12028       else if (THUMB_SETS_FLAGS (inst.instruction))
12029         narrow = !in_it_block ();
12030       else
12031         narrow = in_it_block ();
12032
12033       if (!inst.operands[1].isreg)
12034         {
12035           /* For an immediate, we always generate a 32-bit opcode;
12036              section relaxation will shrink it later if possible.  */
12037           if (inst.instruction < 0xffff)
12038             inst.instruction = THUMB_OP32 (inst.instruction);
12039           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12040           inst.instruction |= Rn << r0off;
12041           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12042         }
12043       else
12044         {
12045           /* See if we can do this with a 16-bit instruction.  */
12046           if (narrow)
12047             {
12048               inst.instruction = THUMB_OP16 (inst.instruction);
12049               inst.instruction |= Rn;
12050               inst.instruction |= Rm << 3;
12051             }
12052           else
12053             {
12054               constraint (inst.operands[1].shifted
12055                           && inst.operands[1].immisreg,
12056                           _("shift must be constant"));
12057               if (inst.instruction < 0xffff)
12058                 inst.instruction = THUMB_OP32 (inst.instruction);
12059               inst.instruction |= Rn << r0off;
12060               encode_thumb32_shifted_operand (1);
12061             }
12062         }
12063     }
12064   else
12065     {
12066       constraint (inst.instruction > 0xffff
12067                   || inst.instruction == T_MNEM_mvns, BAD_THUMB32);
12068       constraint (!inst.operands[1].isreg || inst.operands[1].shifted,
12069                   _("unshifted register required"));
12070       constraint (Rn > 7 || Rm > 7,
12071                   BAD_HIREG);
12072
12073       inst.instruction = THUMB_OP16 (inst.instruction);
12074       inst.instruction |= Rn;
12075       inst.instruction |= Rm << 3;
12076     }
12077 }
12078
12079 static void
12080 do_t_mrs (void)
12081 {
12082   unsigned Rd;
12083
12084   if (do_vfp_nsyn_mrs () == SUCCESS)
12085     return;
12086
12087   Rd = inst.operands[0].reg;
12088   reject_bad_reg (Rd);
12089   inst.instruction |= Rd << 8;
12090
12091   if (inst.operands[1].isreg)
12092     {
12093       unsigned br = inst.operands[1].reg;
12094       if (((br & 0x200) == 0) && ((br & 0xf000) != 0xf000))
12095         as_bad (_("bad register for mrs"));
12096
12097       inst.instruction |= br & (0xf << 16);
12098       inst.instruction |= (br & 0x300) >> 4;
12099       inst.instruction |= (br & SPSR_BIT) >> 2;
12100     }
12101   else
12102     {
12103       int flags = inst.operands[1].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12104
12105       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12106         {
12107           /* PR gas/12698:  The constraint is only applied for m_profile.
12108              If the user has specified -march=all, we want to ignore it as
12109              we are building for any CPU type, including non-m variants.  */
12110           bfd_boolean m_profile =
12111             !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12112           constraint ((flags != 0) && m_profile, _("selected processor does "
12113                                                    "not support requested special purpose register"));
12114         }
12115       else
12116         /* mrs only accepts APSR/CPSR/SPSR/CPSR_all/SPSR_all (for non-M profile
12117            devices).  */
12118         constraint ((flags & ~SPSR_BIT) != (PSR_c|PSR_f),
12119                     _("'APSR', 'CPSR' or 'SPSR' expected"));
12120
12121       inst.instruction |= (flags & SPSR_BIT) >> 2;
12122       inst.instruction |= inst.operands[1].imm & 0xff;
12123       inst.instruction |= 0xf0000;
12124     }
12125 }
12126
12127 static void
12128 do_t_msr (void)
12129 {
12130   int flags;
12131   unsigned Rn;
12132
12133   if (do_vfp_nsyn_msr () == SUCCESS)
12134     return;
12135
12136   constraint (!inst.operands[1].isreg,
12137               _("Thumb encoding does not support an immediate here"));
12138
12139   if (inst.operands[0].isreg)
12140     flags = (int)(inst.operands[0].reg);
12141   else
12142     flags = inst.operands[0].imm;
12143
12144   if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_m))
12145     {
12146       int bits = inst.operands[0].imm & (PSR_c|PSR_x|PSR_s|PSR_f|SPSR_BIT);
12147
12148       /* PR gas/12698:  The constraint is only applied for m_profile.
12149          If the user has specified -march=all, we want to ignore it as
12150          we are building for any CPU type, including non-m variants.  */
12151       bfd_boolean m_profile =
12152         !ARM_FEATURE_CORE_EQUAL (selected_cpu, arm_arch_any);
12153       constraint (((ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12154            && (bits & ~(PSR_s | PSR_f)) != 0)
12155           || (!ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6_dsp)
12156               && bits != PSR_f)) && m_profile,
12157           _("selected processor does not support requested special "
12158             "purpose register"));
12159     }
12160   else
12161      constraint ((flags & 0xff) != 0, _("selected processor does not support "
12162                  "requested special purpose register"));
12163
12164   Rn = inst.operands[1].reg;
12165   reject_bad_reg (Rn);
12166
12167   inst.instruction |= (flags & SPSR_BIT) >> 2;
12168   inst.instruction |= (flags & 0xf0000) >> 8;
12169   inst.instruction |= (flags & 0x300) >> 4;
12170   inst.instruction |= (flags & 0xff);
12171   inst.instruction |= Rn << 16;
12172 }
12173
12174 static void
12175 do_t_mul (void)
12176 {
12177   bfd_boolean narrow;
12178   unsigned Rd, Rn, Rm;
12179
12180   if (!inst.operands[2].present)
12181     inst.operands[2].reg = inst.operands[0].reg;
12182
12183   Rd = inst.operands[0].reg;
12184   Rn = inst.operands[1].reg;
12185   Rm = inst.operands[2].reg;
12186
12187   if (unified_syntax)
12188     {
12189       if (inst.size_req == 4
12190           || (Rd != Rn
12191               && Rd != Rm)
12192           || Rn > 7
12193           || Rm > 7)
12194         narrow = FALSE;
12195       else if (inst.instruction == T_MNEM_muls)
12196         narrow = !in_it_block ();
12197       else
12198         narrow = in_it_block ();
12199     }
12200   else
12201     {
12202       constraint (inst.instruction == T_MNEM_muls, BAD_THUMB32);
12203       constraint (Rn > 7 || Rm > 7,
12204                   BAD_HIREG);
12205       narrow = TRUE;
12206     }
12207
12208   if (narrow)
12209     {
12210       /* 16-bit MULS/Conditional MUL.  */
12211       inst.instruction = THUMB_OP16 (inst.instruction);
12212       inst.instruction |= Rd;
12213
12214       if (Rd == Rn)
12215         inst.instruction |= Rm << 3;
12216       else if (Rd == Rm)
12217         inst.instruction |= Rn << 3;
12218       else
12219         constraint (1, _("dest must overlap one source register"));
12220     }
12221   else
12222     {
12223       constraint (inst.instruction != T_MNEM_mul,
12224                   _("Thumb-2 MUL must not set flags"));
12225       /* 32-bit MUL.  */
12226       inst.instruction = THUMB_OP32 (inst.instruction);
12227       inst.instruction |= Rd << 8;
12228       inst.instruction |= Rn << 16;
12229       inst.instruction |= Rm << 0;
12230
12231       reject_bad_reg (Rd);
12232       reject_bad_reg (Rn);
12233       reject_bad_reg (Rm);
12234     }
12235 }
12236
12237 static void
12238 do_t_mull (void)
12239 {
12240   unsigned RdLo, RdHi, Rn, Rm;
12241
12242   RdLo = inst.operands[0].reg;
12243   RdHi = inst.operands[1].reg;
12244   Rn = inst.operands[2].reg;
12245   Rm = inst.operands[3].reg;
12246
12247   reject_bad_reg (RdLo);
12248   reject_bad_reg (RdHi);
12249   reject_bad_reg (Rn);
12250   reject_bad_reg (Rm);
12251
12252   inst.instruction |= RdLo << 12;
12253   inst.instruction |= RdHi << 8;
12254   inst.instruction |= Rn << 16;
12255   inst.instruction |= Rm;
12256
12257  if (RdLo == RdHi)
12258     as_tsktsk (_("rdhi and rdlo must be different"));
12259 }
12260
12261 static void
12262 do_t_nop (void)
12263 {
12264   set_it_insn_type (NEUTRAL_IT_INSN);
12265
12266   if (unified_syntax)
12267     {
12268       if (inst.size_req == 4 || inst.operands[0].imm > 15)
12269         {
12270           inst.instruction = THUMB_OP32 (inst.instruction);
12271           inst.instruction |= inst.operands[0].imm;
12272         }
12273       else
12274         {
12275           /* PR9722: Check for Thumb2 availability before
12276              generating a thumb2 nop instruction.  */
12277           if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v6t2))
12278             {
12279               inst.instruction = THUMB_OP16 (inst.instruction);
12280               inst.instruction |= inst.operands[0].imm << 4;
12281             }
12282           else
12283             inst.instruction = 0x46c0;
12284         }
12285     }
12286   else
12287     {
12288       constraint (inst.operands[0].present,
12289                   _("Thumb does not support NOP with hints"));
12290       inst.instruction = 0x46c0;
12291     }
12292 }
12293
12294 static void
12295 do_t_neg (void)
12296 {
12297   if (unified_syntax)
12298     {
12299       bfd_boolean narrow;
12300
12301       if (THUMB_SETS_FLAGS (inst.instruction))
12302         narrow = !in_it_block ();
12303       else
12304         narrow = in_it_block ();
12305       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12306         narrow = FALSE;
12307       if (inst.size_req == 4)
12308         narrow = FALSE;
12309
12310       if (!narrow)
12311         {
12312           inst.instruction = THUMB_OP32 (inst.instruction);
12313           inst.instruction |= inst.operands[0].reg << 8;
12314           inst.instruction |= inst.operands[1].reg << 16;
12315         }
12316       else
12317         {
12318           inst.instruction = THUMB_OP16 (inst.instruction);
12319           inst.instruction |= inst.operands[0].reg;
12320           inst.instruction |= inst.operands[1].reg << 3;
12321         }
12322     }
12323   else
12324     {
12325       constraint (inst.operands[0].reg > 7 || inst.operands[1].reg > 7,
12326                   BAD_HIREG);
12327       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12328
12329       inst.instruction = THUMB_OP16 (inst.instruction);
12330       inst.instruction |= inst.operands[0].reg;
12331       inst.instruction |= inst.operands[1].reg << 3;
12332     }
12333 }
12334
12335 static void
12336 do_t_orn (void)
12337 {
12338   unsigned Rd, Rn;
12339
12340   Rd = inst.operands[0].reg;
12341   Rn = inst.operands[1].present ? inst.operands[1].reg : Rd;
12342
12343   reject_bad_reg (Rd);
12344   /* Rn == REG_SP is unpredictable; Rn == REG_PC is MVN.  */
12345   reject_bad_reg (Rn);
12346
12347   inst.instruction |= Rd << 8;
12348   inst.instruction |= Rn << 16;
12349
12350   if (!inst.operands[2].isreg)
12351     {
12352       inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12353       inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12354     }
12355   else
12356     {
12357       unsigned Rm;
12358
12359       Rm = inst.operands[2].reg;
12360       reject_bad_reg (Rm);
12361
12362       constraint (inst.operands[2].shifted
12363                   && inst.operands[2].immisreg,
12364                   _("shift must be constant"));
12365       encode_thumb32_shifted_operand (2);
12366     }
12367 }
12368
12369 static void
12370 do_t_pkhbt (void)
12371 {
12372   unsigned Rd, Rn, Rm;
12373
12374   Rd = inst.operands[0].reg;
12375   Rn = inst.operands[1].reg;
12376   Rm = inst.operands[2].reg;
12377
12378   reject_bad_reg (Rd);
12379   reject_bad_reg (Rn);
12380   reject_bad_reg (Rm);
12381
12382   inst.instruction |= Rd << 8;
12383   inst.instruction |= Rn << 16;
12384   inst.instruction |= Rm;
12385   if (inst.operands[3].present)
12386     {
12387       unsigned int val = inst.reloc.exp.X_add_number;
12388       constraint (inst.reloc.exp.X_op != O_constant,
12389                   _("expression too complex"));
12390       inst.instruction |= (val & 0x1c) << 10;
12391       inst.instruction |= (val & 0x03) << 6;
12392     }
12393 }
12394
12395 static void
12396 do_t_pkhtb (void)
12397 {
12398   if (!inst.operands[3].present)
12399     {
12400       unsigned Rtmp;
12401
12402       inst.instruction &= ~0x00000020;
12403
12404       /* PR 10168.  Swap the Rm and Rn registers.  */
12405       Rtmp = inst.operands[1].reg;
12406       inst.operands[1].reg = inst.operands[2].reg;
12407       inst.operands[2].reg = Rtmp;
12408     }
12409   do_t_pkhbt ();
12410 }
12411
12412 static void
12413 do_t_pld (void)
12414 {
12415   if (inst.operands[0].immisreg)
12416     reject_bad_reg (inst.operands[0].imm);
12417
12418   encode_thumb32_addr_mode (0, /*is_t=*/FALSE, /*is_d=*/FALSE);
12419 }
12420
12421 static void
12422 do_t_push_pop (void)
12423 {
12424   unsigned mask;
12425
12426   constraint (inst.operands[0].writeback,
12427               _("push/pop do not support {reglist}^"));
12428   constraint (inst.reloc.type != BFD_RELOC_UNUSED,
12429               _("expression too complex"));
12430
12431   mask = inst.operands[0].imm;
12432   if (inst.size_req != 4 && (mask & ~0xff) == 0)
12433     inst.instruction = THUMB_OP16 (inst.instruction) | mask;
12434   else if (inst.size_req != 4
12435            && (mask & ~0xff) == (1 << (inst.instruction == T_MNEM_push
12436                                        ? REG_LR : REG_PC)))
12437     {
12438       inst.instruction = THUMB_OP16 (inst.instruction);
12439       inst.instruction |= THUMB_PP_PC_LR;
12440       inst.instruction |= mask & 0xff;
12441     }
12442   else if (unified_syntax)
12443     {
12444       inst.instruction = THUMB_OP32 (inst.instruction);
12445       encode_thumb2_ldmstm (13, mask, TRUE);
12446     }
12447   else
12448     {
12449       inst.error = _("invalid register list to push/pop instruction");
12450       return;
12451     }
12452 }
12453
12454 static void
12455 do_t_rbit (void)
12456 {
12457   unsigned Rd, Rm;
12458
12459   Rd = inst.operands[0].reg;
12460   Rm = inst.operands[1].reg;
12461
12462   reject_bad_reg (Rd);
12463   reject_bad_reg (Rm);
12464
12465   inst.instruction |= Rd << 8;
12466   inst.instruction |= Rm << 16;
12467   inst.instruction |= Rm;
12468 }
12469
12470 static void
12471 do_t_rev (void)
12472 {
12473   unsigned Rd, Rm;
12474
12475   Rd = inst.operands[0].reg;
12476   Rm = inst.operands[1].reg;
12477
12478   reject_bad_reg (Rd);
12479   reject_bad_reg (Rm);
12480
12481   if (Rd <= 7 && Rm <= 7
12482       && inst.size_req != 4)
12483     {
12484       inst.instruction = THUMB_OP16 (inst.instruction);
12485       inst.instruction |= Rd;
12486       inst.instruction |= Rm << 3;
12487     }
12488   else if (unified_syntax)
12489     {
12490       inst.instruction = THUMB_OP32 (inst.instruction);
12491       inst.instruction |= Rd << 8;
12492       inst.instruction |= Rm << 16;
12493       inst.instruction |= Rm;
12494     }
12495   else
12496     inst.error = BAD_HIREG;
12497 }
12498
12499 static void
12500 do_t_rrx (void)
12501 {
12502   unsigned Rd, Rm;
12503
12504   Rd = inst.operands[0].reg;
12505   Rm = inst.operands[1].reg;
12506
12507   reject_bad_reg (Rd);
12508   reject_bad_reg (Rm);
12509
12510   inst.instruction |= Rd << 8;
12511   inst.instruction |= Rm;
12512 }
12513
12514 static void
12515 do_t_rsb (void)
12516 {
12517   unsigned Rd, Rs;
12518
12519   Rd = inst.operands[0].reg;
12520   Rs = (inst.operands[1].present
12521         ? inst.operands[1].reg    /* Rd, Rs, foo */
12522         : inst.operands[0].reg);  /* Rd, foo -> Rd, Rd, foo */
12523
12524   reject_bad_reg (Rd);
12525   reject_bad_reg (Rs);
12526   if (inst.operands[2].isreg)
12527     reject_bad_reg (inst.operands[2].reg);
12528
12529   inst.instruction |= Rd << 8;
12530   inst.instruction |= Rs << 16;
12531   if (!inst.operands[2].isreg)
12532     {
12533       bfd_boolean narrow;
12534
12535       if ((inst.instruction & 0x00100000) != 0)
12536         narrow = !in_it_block ();
12537       else
12538         narrow = in_it_block ();
12539
12540       if (Rd > 7 || Rs > 7)
12541         narrow = FALSE;
12542
12543       if (inst.size_req == 4 || !unified_syntax)
12544         narrow = FALSE;
12545
12546       if (inst.reloc.exp.X_op != O_constant
12547           || inst.reloc.exp.X_add_number != 0)
12548         narrow = FALSE;
12549
12550       /* Turn rsb #0 into 16-bit neg.  We should probably do this via
12551          relaxation, but it doesn't seem worth the hassle.  */
12552       if (narrow)
12553         {
12554           inst.reloc.type = BFD_RELOC_UNUSED;
12555           inst.instruction = THUMB_OP16 (T_MNEM_negs);
12556           inst.instruction |= Rs << 3;
12557           inst.instruction |= Rd;
12558         }
12559       else
12560         {
12561           inst.instruction = (inst.instruction & 0xe1ffffff) | 0x10000000;
12562           inst.reloc.type = BFD_RELOC_ARM_T32_IMMEDIATE;
12563         }
12564     }
12565   else
12566     encode_thumb32_shifted_operand (2);
12567 }
12568
12569 static void
12570 do_t_setend (void)
12571 {
12572   if (warn_on_deprecated
12573       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
12574       as_tsktsk (_("setend use is deprecated for ARMv8"));
12575
12576   set_it_insn_type (OUTSIDE_IT_INSN);
12577   if (inst.operands[0].imm)
12578     inst.instruction |= 0x8;
12579 }
12580
12581 static void
12582 do_t_shift (void)
12583 {
12584   if (!inst.operands[1].present)
12585     inst.operands[1].reg = inst.operands[0].reg;
12586
12587   if (unified_syntax)
12588     {
12589       bfd_boolean narrow;
12590       int shift_kind;
12591
12592       switch (inst.instruction)
12593         {
12594         case T_MNEM_asr:
12595         case T_MNEM_asrs: shift_kind = SHIFT_ASR; break;
12596         case T_MNEM_lsl:
12597         case T_MNEM_lsls: shift_kind = SHIFT_LSL; break;
12598         case T_MNEM_lsr:
12599         case T_MNEM_lsrs: shift_kind = SHIFT_LSR; break;
12600         case T_MNEM_ror:
12601         case T_MNEM_rors: shift_kind = SHIFT_ROR; break;
12602         default: abort ();
12603         }
12604
12605       if (THUMB_SETS_FLAGS (inst.instruction))
12606         narrow = !in_it_block ();
12607       else
12608         narrow = in_it_block ();
12609       if (inst.operands[0].reg > 7 || inst.operands[1].reg > 7)
12610         narrow = FALSE;
12611       if (!inst.operands[2].isreg && shift_kind == SHIFT_ROR)
12612         narrow = FALSE;
12613       if (inst.operands[2].isreg
12614           && (inst.operands[1].reg != inst.operands[0].reg
12615               || inst.operands[2].reg > 7))
12616         narrow = FALSE;
12617       if (inst.size_req == 4)
12618         narrow = FALSE;
12619
12620       reject_bad_reg (inst.operands[0].reg);
12621       reject_bad_reg (inst.operands[1].reg);
12622
12623       if (!narrow)
12624         {
12625           if (inst.operands[2].isreg)
12626             {
12627               reject_bad_reg (inst.operands[2].reg);
12628               inst.instruction = THUMB_OP32 (inst.instruction);
12629               inst.instruction |= inst.operands[0].reg << 8;
12630               inst.instruction |= inst.operands[1].reg << 16;
12631               inst.instruction |= inst.operands[2].reg;
12632
12633               /* PR 12854: Error on extraneous shifts.  */
12634               constraint (inst.operands[2].shifted,
12635                           _("extraneous shift as part of operand to shift insn"));
12636             }
12637           else
12638             {
12639               inst.operands[1].shifted = 1;
12640               inst.operands[1].shift_kind = shift_kind;
12641               inst.instruction = THUMB_OP32 (THUMB_SETS_FLAGS (inst.instruction)
12642                                              ? T_MNEM_movs : T_MNEM_mov);
12643               inst.instruction |= inst.operands[0].reg << 8;
12644               encode_thumb32_shifted_operand (1);
12645               /* Prevent the incorrect generation of an ARM_IMMEDIATE fixup.  */
12646               inst.reloc.type = BFD_RELOC_UNUSED;
12647             }
12648         }
12649       else
12650         {
12651           if (inst.operands[2].isreg)
12652             {
12653               switch (shift_kind)
12654                 {
12655                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_R; break;
12656                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_R; break;
12657                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_R; break;
12658                 case SHIFT_ROR: inst.instruction = T_OPCODE_ROR_R; break;
12659                 default: abort ();
12660                 }
12661
12662               inst.instruction |= inst.operands[0].reg;
12663               inst.instruction |= inst.operands[2].reg << 3;
12664
12665               /* PR 12854: Error on extraneous shifts.  */
12666               constraint (inst.operands[2].shifted,
12667                           _("extraneous shift as part of operand to shift insn"));
12668             }
12669           else
12670             {
12671               switch (shift_kind)
12672                 {
12673                 case SHIFT_ASR: inst.instruction = T_OPCODE_ASR_I; break;
12674                 case SHIFT_LSL: inst.instruction = T_OPCODE_LSL_I; break;
12675                 case SHIFT_LSR: inst.instruction = T_OPCODE_LSR_I; break;
12676                 default: abort ();
12677                 }
12678               inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12679               inst.instruction |= inst.operands[0].reg;
12680               inst.instruction |= inst.operands[1].reg << 3;
12681             }
12682         }
12683     }
12684   else
12685     {
12686       constraint (inst.operands[0].reg > 7
12687                   || inst.operands[1].reg > 7, BAD_HIREG);
12688       constraint (THUMB_SETS_FLAGS (inst.instruction), BAD_THUMB32);
12689
12690       if (inst.operands[2].isreg)  /* Rd, {Rs,} Rn */
12691         {
12692           constraint (inst.operands[2].reg > 7, BAD_HIREG);
12693           constraint (inst.operands[0].reg != inst.operands[1].reg,
12694                       _("source1 and dest must be same register"));
12695
12696           switch (inst.instruction)
12697             {
12698             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_R; break;
12699             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_R; break;
12700             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_R; break;
12701             case T_MNEM_ror: inst.instruction = T_OPCODE_ROR_R; break;
12702             default: abort ();
12703             }
12704
12705           inst.instruction |= inst.operands[0].reg;
12706           inst.instruction |= inst.operands[2].reg << 3;
12707
12708           /* PR 12854: Error on extraneous shifts.  */
12709           constraint (inst.operands[2].shifted,
12710                       _("extraneous shift as part of operand to shift insn"));
12711         }
12712       else
12713         {
12714           switch (inst.instruction)
12715             {
12716             case T_MNEM_asr: inst.instruction = T_OPCODE_ASR_I; break;
12717             case T_MNEM_lsl: inst.instruction = T_OPCODE_LSL_I; break;
12718             case T_MNEM_lsr: inst.instruction = T_OPCODE_LSR_I; break;
12719             case T_MNEM_ror: inst.error = _("ror #imm not supported"); return;
12720             default: abort ();
12721             }
12722           inst.reloc.type = BFD_RELOC_ARM_THUMB_SHIFT;
12723           inst.instruction |= inst.operands[0].reg;
12724           inst.instruction |= inst.operands[1].reg << 3;
12725         }
12726     }
12727 }
12728
12729 static void
12730 do_t_simd (void)
12731 {
12732   unsigned Rd, Rn, Rm;
12733
12734   Rd = inst.operands[0].reg;
12735   Rn = inst.operands[1].reg;
12736   Rm = inst.operands[2].reg;
12737
12738   reject_bad_reg (Rd);
12739   reject_bad_reg (Rn);
12740   reject_bad_reg (Rm);
12741
12742   inst.instruction |= Rd << 8;
12743   inst.instruction |= Rn << 16;
12744   inst.instruction |= Rm;
12745 }
12746
12747 static void
12748 do_t_simd2 (void)
12749 {
12750   unsigned Rd, Rn, Rm;
12751
12752   Rd = inst.operands[0].reg;
12753   Rm = inst.operands[1].reg;
12754   Rn = inst.operands[2].reg;
12755
12756   reject_bad_reg (Rd);
12757   reject_bad_reg (Rn);
12758   reject_bad_reg (Rm);
12759
12760   inst.instruction |= Rd << 8;
12761   inst.instruction |= Rn << 16;
12762   inst.instruction |= Rm;
12763 }
12764
12765 static void
12766 do_t_smc (void)
12767 {
12768   unsigned int value = inst.reloc.exp.X_add_number;
12769   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7a),
12770               _("SMC is not permitted on this architecture"));
12771   constraint (inst.reloc.exp.X_op != O_constant,
12772               _("expression too complex"));
12773   inst.reloc.type = BFD_RELOC_UNUSED;
12774   inst.instruction |= (value & 0xf000) >> 12;
12775   inst.instruction |= (value & 0x0ff0);
12776   inst.instruction |= (value & 0x000f) << 16;
12777   /* PR gas/15623: SMC instructions must be last in an IT block.  */
12778   set_it_insn_type_last ();
12779 }
12780
12781 static void
12782 do_t_hvc (void)
12783 {
12784   unsigned int value = inst.reloc.exp.X_add_number;
12785
12786   inst.reloc.type = BFD_RELOC_UNUSED;
12787   inst.instruction |= (value & 0x0fff);
12788   inst.instruction |= (value & 0xf000) << 4;
12789 }
12790
12791 static void
12792 do_t_ssat_usat (int bias)
12793 {
12794   unsigned Rd, Rn;
12795
12796   Rd = inst.operands[0].reg;
12797   Rn = inst.operands[2].reg;
12798
12799   reject_bad_reg (Rd);
12800   reject_bad_reg (Rn);
12801
12802   inst.instruction |= Rd << 8;
12803   inst.instruction |= inst.operands[1].imm - bias;
12804   inst.instruction |= Rn << 16;
12805
12806   if (inst.operands[3].present)
12807     {
12808       offsetT shift_amount = inst.reloc.exp.X_add_number;
12809
12810       inst.reloc.type = BFD_RELOC_UNUSED;
12811
12812       constraint (inst.reloc.exp.X_op != O_constant,
12813                   _("expression too complex"));
12814
12815       if (shift_amount != 0)
12816         {
12817           constraint (shift_amount > 31,
12818                       _("shift expression is too large"));
12819
12820           if (inst.operands[3].shift_kind == SHIFT_ASR)
12821             inst.instruction |= 0x00200000;  /* sh bit.  */
12822
12823           inst.instruction |= (shift_amount & 0x1c) << 10;
12824           inst.instruction |= (shift_amount & 0x03) << 6;
12825         }
12826     }
12827 }
12828
12829 static void
12830 do_t_ssat (void)
12831 {
12832   do_t_ssat_usat (1);
12833 }
12834
12835 static void
12836 do_t_ssat16 (void)
12837 {
12838   unsigned Rd, Rn;
12839
12840   Rd = inst.operands[0].reg;
12841   Rn = inst.operands[2].reg;
12842
12843   reject_bad_reg (Rd);
12844   reject_bad_reg (Rn);
12845
12846   inst.instruction |= Rd << 8;
12847   inst.instruction |= inst.operands[1].imm - 1;
12848   inst.instruction |= Rn << 16;
12849 }
12850
12851 static void
12852 do_t_strex (void)
12853 {
12854   constraint (!inst.operands[2].isreg || !inst.operands[2].preind
12855               || inst.operands[2].postind || inst.operands[2].writeback
12856               || inst.operands[2].immisreg || inst.operands[2].shifted
12857               || inst.operands[2].negative,
12858               BAD_ADDR_MODE);
12859
12860   constraint (inst.operands[2].reg == REG_PC, BAD_PC);
12861
12862   inst.instruction |= inst.operands[0].reg << 8;
12863   inst.instruction |= inst.operands[1].reg << 12;
12864   inst.instruction |= inst.operands[2].reg << 16;
12865   inst.reloc.type = BFD_RELOC_ARM_T32_OFFSET_U8;
12866 }
12867
12868 static void
12869 do_t_strexd (void)
12870 {
12871   if (!inst.operands[2].present)
12872     inst.operands[2].reg = inst.operands[1].reg + 1;
12873
12874   constraint (inst.operands[0].reg == inst.operands[1].reg
12875               || inst.operands[0].reg == inst.operands[2].reg
12876               || inst.operands[0].reg == inst.operands[3].reg,
12877               BAD_OVERLAP);
12878
12879   inst.instruction |= inst.operands[0].reg;
12880   inst.instruction |= inst.operands[1].reg << 12;
12881   inst.instruction |= inst.operands[2].reg << 8;
12882   inst.instruction |= inst.operands[3].reg << 16;
12883 }
12884
12885 static void
12886 do_t_sxtah (void)
12887 {
12888   unsigned Rd, Rn, Rm;
12889
12890   Rd = inst.operands[0].reg;
12891   Rn = inst.operands[1].reg;
12892   Rm = inst.operands[2].reg;
12893
12894   reject_bad_reg (Rd);
12895   reject_bad_reg (Rn);
12896   reject_bad_reg (Rm);
12897
12898   inst.instruction |= Rd << 8;
12899   inst.instruction |= Rn << 16;
12900   inst.instruction |= Rm;
12901   inst.instruction |= inst.operands[3].imm << 4;
12902 }
12903
12904 static void
12905 do_t_sxth (void)
12906 {
12907   unsigned Rd, Rm;
12908
12909   Rd = inst.operands[0].reg;
12910   Rm = inst.operands[1].reg;
12911
12912   reject_bad_reg (Rd);
12913   reject_bad_reg (Rm);
12914
12915   if (inst.instruction <= 0xffff
12916       && inst.size_req != 4
12917       && Rd <= 7 && Rm <= 7
12918       && (!inst.operands[2].present || inst.operands[2].imm == 0))
12919     {
12920       inst.instruction = THUMB_OP16 (inst.instruction);
12921       inst.instruction |= Rd;
12922       inst.instruction |= Rm << 3;
12923     }
12924   else if (unified_syntax)
12925     {
12926       if (inst.instruction <= 0xffff)
12927         inst.instruction = THUMB_OP32 (inst.instruction);
12928       inst.instruction |= Rd << 8;
12929       inst.instruction |= Rm;
12930       inst.instruction |= inst.operands[2].imm << 4;
12931     }
12932   else
12933     {
12934       constraint (inst.operands[2].present && inst.operands[2].imm != 0,
12935                   _("Thumb encoding does not support rotation"));
12936       constraint (1, BAD_HIREG);
12937     }
12938 }
12939
12940 static void
12941 do_t_swi (void)
12942 {
12943   /* We have to do the following check manually as ARM_EXT_OS only applies
12944      to ARM_EXT_V6M.  */
12945   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v6m))
12946     {
12947       if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_os)
12948           /* This only applies to the v6m howver, not later architectures.  */
12949           && ! ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v7))
12950         as_bad (_("SVC is not permitted on this architecture"));
12951       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used, arm_ext_os);
12952     }
12953
12954   inst.reloc.type = BFD_RELOC_ARM_SWI;
12955 }
12956
12957 static void
12958 do_t_tb (void)
12959 {
12960   unsigned Rn, Rm;
12961   int half;
12962
12963   half = (inst.instruction & 0x10) != 0;
12964   set_it_insn_type_last ();
12965   constraint (inst.operands[0].immisreg,
12966               _("instruction requires register index"));
12967
12968   Rn = inst.operands[0].reg;
12969   Rm = inst.operands[0].imm;
12970
12971   constraint (Rn == REG_SP, BAD_SP);
12972   reject_bad_reg (Rm);
12973
12974   constraint (!half && inst.operands[0].shifted,
12975               _("instruction does not allow shifted index"));
12976   inst.instruction |= (Rn << 16) | Rm;
12977 }
12978
12979 static void
12980 do_t_udf (void)
12981 {
12982   if (!inst.operands[0].present)
12983     inst.operands[0].imm = 0;
12984
12985   if ((unsigned int) inst.operands[0].imm > 255 || inst.size_req == 4)
12986     {
12987       constraint (inst.size_req == 2,
12988                   _("immediate value out of range"));
12989       inst.instruction = THUMB_OP32 (inst.instruction);
12990       inst.instruction |= (inst.operands[0].imm & 0xf000u) << 4;
12991       inst.instruction |= (inst.operands[0].imm & 0x0fffu) << 0;
12992     }
12993   else
12994     {
12995       inst.instruction = THUMB_OP16 (inst.instruction);
12996       inst.instruction |= inst.operands[0].imm;
12997     }
12998
12999   set_it_insn_type (NEUTRAL_IT_INSN);
13000 }
13001
13002
13003 static void
13004 do_t_usat (void)
13005 {
13006   do_t_ssat_usat (0);
13007 }
13008
13009 static void
13010 do_t_usat16 (void)
13011 {
13012   unsigned Rd, Rn;
13013
13014   Rd = inst.operands[0].reg;
13015   Rn = inst.operands[2].reg;
13016
13017   reject_bad_reg (Rd);
13018   reject_bad_reg (Rn);
13019
13020   inst.instruction |= Rd << 8;
13021   inst.instruction |= inst.operands[1].imm;
13022   inst.instruction |= Rn << 16;
13023 }
13024
13025 /* Neon instruction encoder helpers.  */
13026
13027 /* Encodings for the different types for various Neon opcodes.  */
13028
13029 /* An "invalid" code for the following tables.  */
13030 #define N_INV -1u
13031
13032 struct neon_tab_entry
13033 {
13034   unsigned integer;
13035   unsigned float_or_poly;
13036   unsigned scalar_or_imm;
13037 };
13038
13039 /* Map overloaded Neon opcodes to their respective encodings.  */
13040 #define NEON_ENC_TAB                                    \
13041   X(vabd,       0x0000700, 0x1200d00, N_INV),           \
13042   X(vmax,       0x0000600, 0x0000f00, N_INV),           \
13043   X(vmin,       0x0000610, 0x0200f00, N_INV),           \
13044   X(vpadd,      0x0000b10, 0x1000d00, N_INV),           \
13045   X(vpmax,      0x0000a00, 0x1000f00, N_INV),           \
13046   X(vpmin,      0x0000a10, 0x1200f00, N_INV),           \
13047   X(vadd,       0x0000800, 0x0000d00, N_INV),           \
13048   X(vsub,       0x1000800, 0x0200d00, N_INV),           \
13049   X(vceq,       0x1000810, 0x0000e00, 0x1b10100),       \
13050   X(vcge,       0x0000310, 0x1000e00, 0x1b10080),       \
13051   X(vcgt,       0x0000300, 0x1200e00, 0x1b10000),       \
13052   /* Register variants of the following two instructions are encoded as
13053      vcge / vcgt with the operands reversed.  */        \
13054   X(vclt,       0x0000300, 0x1200e00, 0x1b10200),       \
13055   X(vcle,       0x0000310, 0x1000e00, 0x1b10180),       \
13056   X(vfma,       N_INV, 0x0000c10, N_INV),               \
13057   X(vfms,       N_INV, 0x0200c10, N_INV),               \
13058   X(vmla,       0x0000900, 0x0000d10, 0x0800040),       \
13059   X(vmls,       0x1000900, 0x0200d10, 0x0800440),       \
13060   X(vmul,       0x0000910, 0x1000d10, 0x0800840),       \
13061   X(vmull,      0x0800c00, 0x0800e00, 0x0800a40), /* polynomial not float.  */ \
13062   X(vmlal,      0x0800800, N_INV,     0x0800240),       \
13063   X(vmlsl,      0x0800a00, N_INV,     0x0800640),       \
13064   X(vqdmlal,    0x0800900, N_INV,     0x0800340),       \
13065   X(vqdmlsl,    0x0800b00, N_INV,     0x0800740),       \
13066   X(vqdmull,    0x0800d00, N_INV,     0x0800b40),       \
13067   X(vqdmulh,    0x0000b00, N_INV,     0x0800c40),       \
13068   X(vqrdmulh,   0x1000b00, N_INV,     0x0800d40),       \
13069   X(vqrdmlah,   0x3000b10, N_INV,     0x0800e40),       \
13070   X(vqrdmlsh,   0x3000c10, N_INV,     0x0800f40),       \
13071   X(vshl,       0x0000400, N_INV,     0x0800510),       \
13072   X(vqshl,      0x0000410, N_INV,     0x0800710),       \
13073   X(vand,       0x0000110, N_INV,     0x0800030),       \
13074   X(vbic,       0x0100110, N_INV,     0x0800030),       \
13075   X(veor,       0x1000110, N_INV,     N_INV),           \
13076   X(vorn,       0x0300110, N_INV,     0x0800010),       \
13077   X(vorr,       0x0200110, N_INV,     0x0800010),       \
13078   X(vmvn,       0x1b00580, N_INV,     0x0800030),       \
13079   X(vshll,      0x1b20300, N_INV,     0x0800a10), /* max shift, immediate.  */ \
13080   X(vcvt,       0x1b30600, N_INV,     0x0800e10), /* integer, fixed-point.  */ \
13081   X(vdup,       0xe800b10, N_INV,     0x1b00c00), /* arm, scalar.  */ \
13082   X(vld1,       0x0200000, 0x0a00000, 0x0a00c00), /* interlv, lane, dup.  */ \
13083   X(vst1,       0x0000000, 0x0800000, N_INV),           \
13084   X(vld2,       0x0200100, 0x0a00100, 0x0a00d00),       \
13085   X(vst2,       0x0000100, 0x0800100, N_INV),           \
13086   X(vld3,       0x0200200, 0x0a00200, 0x0a00e00),       \
13087   X(vst3,       0x0000200, 0x0800200, N_INV),           \
13088   X(vld4,       0x0200300, 0x0a00300, 0x0a00f00),       \
13089   X(vst4,       0x0000300, 0x0800300, N_INV),           \
13090   X(vmovn,      0x1b20200, N_INV,     N_INV),           \
13091   X(vtrn,       0x1b20080, N_INV,     N_INV),           \
13092   X(vqmovn,     0x1b20200, N_INV,     N_INV),           \
13093   X(vqmovun,    0x1b20240, N_INV,     N_INV),           \
13094   X(vnmul,      0xe200a40, 0xe200b40, N_INV),           \
13095   X(vnmla,      0xe100a40, 0xe100b40, N_INV),           \
13096   X(vnmls,      0xe100a00, 0xe100b00, N_INV),           \
13097   X(vfnma,      0xe900a40, 0xe900b40, N_INV),           \
13098   X(vfnms,      0xe900a00, 0xe900b00, N_INV),           \
13099   X(vcmp,       0xeb40a40, 0xeb40b40, N_INV),           \
13100   X(vcmpz,      0xeb50a40, 0xeb50b40, N_INV),           \
13101   X(vcmpe,      0xeb40ac0, 0xeb40bc0, N_INV),           \
13102   X(vcmpez,     0xeb50ac0, 0xeb50bc0, N_INV),           \
13103   X(vseleq,     0xe000a00, N_INV,     N_INV),           \
13104   X(vselvs,     0xe100a00, N_INV,     N_INV),           \
13105   X(vselge,     0xe200a00, N_INV,     N_INV),           \
13106   X(vselgt,     0xe300a00, N_INV,     N_INV),           \
13107   X(vmaxnm,     0xe800a00, 0x3000f10, N_INV),           \
13108   X(vminnm,     0xe800a40, 0x3200f10, N_INV),           \
13109   X(vcvta,      0xebc0a40, 0x3bb0000, N_INV),           \
13110   X(vrintr,     0xeb60a40, 0x3ba0400, N_INV),           \
13111   X(vrinta,     0xeb80a40, 0x3ba0400, N_INV),           \
13112   X(aes,        0x3b00300, N_INV,     N_INV),           \
13113   X(sha3op,     0x2000c00, N_INV,     N_INV),           \
13114   X(sha1h,      0x3b902c0, N_INV,     N_INV),           \
13115   X(sha2op,     0x3ba0380, N_INV,     N_INV)
13116
13117 enum neon_opc
13118 {
13119 #define X(OPC,I,F,S) N_MNEM_##OPC
13120 NEON_ENC_TAB
13121 #undef X
13122 };
13123
13124 static const struct neon_tab_entry neon_enc_tab[] =
13125 {
13126 #define X(OPC,I,F,S) { (I), (F), (S) }
13127 NEON_ENC_TAB
13128 #undef X
13129 };
13130
13131 /* Do not use these macros; instead, use NEON_ENCODE defined below.  */
13132 #define NEON_ENC_INTEGER_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13133 #define NEON_ENC_ARMREG_(X)  (neon_enc_tab[(X) & 0x0fffffff].integer)
13134 #define NEON_ENC_POLY_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13135 #define NEON_ENC_FLOAT_(X)   (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13136 #define NEON_ENC_SCALAR_(X)  (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13137 #define NEON_ENC_IMMED_(X)   (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13138 #define NEON_ENC_INTERLV_(X) (neon_enc_tab[(X) & 0x0fffffff].integer)
13139 #define NEON_ENC_LANE_(X)    (neon_enc_tab[(X) & 0x0fffffff].float_or_poly)
13140 #define NEON_ENC_DUP_(X)     (neon_enc_tab[(X) & 0x0fffffff].scalar_or_imm)
13141 #define NEON_ENC_SINGLE_(X) \
13142   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf0000000))
13143 #define NEON_ENC_DOUBLE_(X) \
13144   ((neon_enc_tab[(X) & 0x0fffffff].float_or_poly) | ((X) & 0xf0000000))
13145 #define NEON_ENC_FPV8_(X) \
13146   ((neon_enc_tab[(X) & 0x0fffffff].integer) | ((X) & 0xf000000))
13147
13148 #define NEON_ENCODE(type, inst)                                 \
13149   do                                                            \
13150     {                                                           \
13151       inst.instruction = NEON_ENC_##type##_ (inst.instruction); \
13152       inst.is_neon = 1;                                         \
13153     }                                                           \
13154   while (0)
13155
13156 #define check_neon_suffixes                                             \
13157   do                                                                    \
13158     {                                                                   \
13159       if (!inst.error && inst.vectype.elems > 0 && !inst.is_neon)       \
13160         {                                                               \
13161           as_bad (_("invalid neon suffix for non neon instruction"));   \
13162           return;                                                       \
13163         }                                                               \
13164     }                                                                   \
13165   while (0)
13166
13167 /* Define shapes for instruction operands. The following mnemonic characters
13168    are used in this table:
13169
13170      F - VFP S<n> register
13171      D - Neon D<n> register
13172      Q - Neon Q<n> register
13173      I - Immediate
13174      S - Scalar
13175      R - ARM register
13176      L - D<n> register list
13177
13178    This table is used to generate various data:
13179      - enumerations of the form NS_DDR to be used as arguments to
13180        neon_select_shape.
13181      - a table classifying shapes into single, double, quad, mixed.
13182      - a table used to drive neon_select_shape.  */
13183
13184 #define NEON_SHAPE_DEF                  \
13185   X(3, (D, D, D), DOUBLE),              \
13186   X(3, (Q, Q, Q), QUAD),                \
13187   X(3, (D, D, I), DOUBLE),              \
13188   X(3, (Q, Q, I), QUAD),                \
13189   X(3, (D, D, S), DOUBLE),              \
13190   X(3, (Q, Q, S), QUAD),                \
13191   X(2, (D, D), DOUBLE),                 \
13192   X(2, (Q, Q), QUAD),                   \
13193   X(2, (D, S), DOUBLE),                 \
13194   X(2, (Q, S), QUAD),                   \
13195   X(2, (D, R), DOUBLE),                 \
13196   X(2, (Q, R), QUAD),                   \
13197   X(2, (D, I), DOUBLE),                 \
13198   X(2, (Q, I), QUAD),                   \
13199   X(3, (D, L, D), DOUBLE),              \
13200   X(2, (D, Q), MIXED),                  \
13201   X(2, (Q, D), MIXED),                  \
13202   X(3, (D, Q, I), MIXED),               \
13203   X(3, (Q, D, I), MIXED),               \
13204   X(3, (Q, D, D), MIXED),               \
13205   X(3, (D, Q, Q), MIXED),               \
13206   X(3, (Q, Q, D), MIXED),               \
13207   X(3, (Q, D, S), MIXED),               \
13208   X(3, (D, Q, S), MIXED),               \
13209   X(4, (D, D, D, I), DOUBLE),           \
13210   X(4, (Q, Q, Q, I), QUAD),             \
13211   X(2, (F, F), SINGLE),                 \
13212   X(3, (F, F, F), SINGLE),              \
13213   X(2, (F, I), SINGLE),                 \
13214   X(2, (F, D), MIXED),                  \
13215   X(2, (D, F), MIXED),                  \
13216   X(3, (F, F, I), MIXED),               \
13217   X(4, (R, R, F, F), SINGLE),           \
13218   X(4, (F, F, R, R), SINGLE),           \
13219   X(3, (D, R, R), DOUBLE),              \
13220   X(3, (R, R, D), DOUBLE),              \
13221   X(2, (S, R), SINGLE),                 \
13222   X(2, (R, S), SINGLE),                 \
13223   X(2, (F, R), SINGLE),                 \
13224   X(2, (R, F), SINGLE)
13225
13226 #define S2(A,B)         NS_##A##B
13227 #define S3(A,B,C)       NS_##A##B##C
13228 #define S4(A,B,C,D)     NS_##A##B##C##D
13229
13230 #define X(N, L, C) S##N L
13231
13232 enum neon_shape
13233 {
13234   NEON_SHAPE_DEF,
13235   NS_NULL
13236 };
13237
13238 #undef X
13239 #undef S2
13240 #undef S3
13241 #undef S4
13242
13243 enum neon_shape_class
13244 {
13245   SC_SINGLE,
13246   SC_DOUBLE,
13247   SC_QUAD,
13248   SC_MIXED
13249 };
13250
13251 #define X(N, L, C) SC_##C
13252
13253 static enum neon_shape_class neon_shape_class[] =
13254 {
13255   NEON_SHAPE_DEF
13256 };
13257
13258 #undef X
13259
13260 enum neon_shape_el
13261 {
13262   SE_F,
13263   SE_D,
13264   SE_Q,
13265   SE_I,
13266   SE_S,
13267   SE_R,
13268   SE_L
13269 };
13270
13271 /* Register widths of above.  */
13272 static unsigned neon_shape_el_size[] =
13273 {
13274   32,
13275   64,
13276   128,
13277   0,
13278   32,
13279   32,
13280   0
13281 };
13282
13283 struct neon_shape_info
13284 {
13285   unsigned els;
13286   enum neon_shape_el el[NEON_MAX_TYPE_ELS];
13287 };
13288
13289 #define S2(A,B)         { SE_##A, SE_##B }
13290 #define S3(A,B,C)       { SE_##A, SE_##B, SE_##C }
13291 #define S4(A,B,C,D)     { SE_##A, SE_##B, SE_##C, SE_##D }
13292
13293 #define X(N, L, C) { N, S##N L }
13294
13295 static struct neon_shape_info neon_shape_tab[] =
13296 {
13297   NEON_SHAPE_DEF
13298 };
13299
13300 #undef X
13301 #undef S2
13302 #undef S3
13303 #undef S4
13304
13305 /* Bit masks used in type checking given instructions.
13306   'N_EQK' means the type must be the same as (or based on in some way) the key
13307    type, which itself is marked with the 'N_KEY' bit. If the 'N_EQK' bit is
13308    set, various other bits can be set as well in order to modify the meaning of
13309    the type constraint.  */
13310
13311 enum neon_type_mask
13312 {
13313   N_S8   = 0x0000001,
13314   N_S16  = 0x0000002,
13315   N_S32  = 0x0000004,
13316   N_S64  = 0x0000008,
13317   N_U8   = 0x0000010,
13318   N_U16  = 0x0000020,
13319   N_U32  = 0x0000040,
13320   N_U64  = 0x0000080,
13321   N_I8   = 0x0000100,
13322   N_I16  = 0x0000200,
13323   N_I32  = 0x0000400,
13324   N_I64  = 0x0000800,
13325   N_8    = 0x0001000,
13326   N_16   = 0x0002000,
13327   N_32   = 0x0004000,
13328   N_64   = 0x0008000,
13329   N_P8   = 0x0010000,
13330   N_P16  = 0x0020000,
13331   N_F16  = 0x0040000,
13332   N_F32  = 0x0080000,
13333   N_F64  = 0x0100000,
13334   N_P64  = 0x0200000,
13335   N_KEY  = 0x1000000, /* Key element (main type specifier).  */
13336   N_EQK  = 0x2000000, /* Given operand has the same type & size as the key.  */
13337   N_VFP  = 0x4000000, /* VFP mode: operand size must match register width.  */
13338   N_UNT  = 0x8000000, /* Must be explicitly untyped.  */
13339   N_DBL  = 0x0000001, /* If N_EQK, this operand is twice the size.  */
13340   N_HLF  = 0x0000002, /* If N_EQK, this operand is half the size.  */
13341   N_SGN  = 0x0000004, /* If N_EQK, this operand is forced to be signed.  */
13342   N_UNS  = 0x0000008, /* If N_EQK, this operand is forced to be unsigned.  */
13343   N_INT  = 0x0000010, /* If N_EQK, this operand is forced to be integer.  */
13344   N_FLT  = 0x0000020, /* If N_EQK, this operand is forced to be float.  */
13345   N_SIZ  = 0x0000040, /* If N_EQK, this operand is forced to be size-only.  */
13346   N_UTYP = 0,
13347   N_MAX_NONSPECIAL = N_P64
13348 };
13349
13350 #define N_ALLMODS  (N_DBL | N_HLF | N_SGN | N_UNS | N_INT | N_FLT | N_SIZ)
13351
13352 #define N_SU_ALL   (N_S8 | N_S16 | N_S32 | N_S64 | N_U8 | N_U16 | N_U32 | N_U64)
13353 #define N_SU_32    (N_S8 | N_S16 | N_S32 | N_U8 | N_U16 | N_U32)
13354 #define N_SU_16_64 (N_S16 | N_S32 | N_S64 | N_U16 | N_U32 | N_U64)
13355 #define N_SUF_32   (N_SU_32 | N_F32)
13356 #define N_I_ALL    (N_I8 | N_I16 | N_I32 | N_I64)
13357 #define N_IF_32    (N_I8 | N_I16 | N_I32 | N_F32)
13358
13359 /* Pass this as the first type argument to neon_check_type to ignore types
13360    altogether.  */
13361 #define N_IGNORE_TYPE (N_KEY | N_EQK)
13362
13363 /* Select a "shape" for the current instruction (describing register types or
13364    sizes) from a list of alternatives. Return NS_NULL if the current instruction
13365    doesn't fit. For non-polymorphic shapes, checking is usually done as a
13366    function of operand parsing, so this function doesn't need to be called.
13367    Shapes should be listed in order of decreasing length.  */
13368
13369 static enum neon_shape
13370 neon_select_shape (enum neon_shape shape, ...)
13371 {
13372   va_list ap;
13373   enum neon_shape first_shape = shape;
13374
13375   /* Fix missing optional operands. FIXME: we don't know at this point how
13376      many arguments we should have, so this makes the assumption that we have
13377      > 1. This is true of all current Neon opcodes, I think, but may not be
13378      true in the future.  */
13379   if (!inst.operands[1].present)
13380     inst.operands[1] = inst.operands[0];
13381
13382   va_start (ap, shape);
13383
13384   for (; shape != NS_NULL; shape = (enum neon_shape) va_arg (ap, int))
13385     {
13386       unsigned j;
13387       int matches = 1;
13388
13389       for (j = 0; j < neon_shape_tab[shape].els; j++)
13390         {
13391           if (!inst.operands[j].present)
13392             {
13393               matches = 0;
13394               break;
13395             }
13396
13397           switch (neon_shape_tab[shape].el[j])
13398             {
13399             case SE_F:
13400               if (!(inst.operands[j].isreg
13401                     && inst.operands[j].isvec
13402                     && inst.operands[j].issingle
13403                     && !inst.operands[j].isquad))
13404                 matches = 0;
13405               break;
13406
13407             case SE_D:
13408               if (!(inst.operands[j].isreg
13409                     && inst.operands[j].isvec
13410                     && !inst.operands[j].isquad
13411                     && !inst.operands[j].issingle))
13412                 matches = 0;
13413               break;
13414
13415             case SE_R:
13416               if (!(inst.operands[j].isreg
13417                     && !inst.operands[j].isvec))
13418                 matches = 0;
13419               break;
13420
13421             case SE_Q:
13422               if (!(inst.operands[j].isreg
13423                     && inst.operands[j].isvec
13424                     && inst.operands[j].isquad
13425                     && !inst.operands[j].issingle))
13426                 matches = 0;
13427               break;
13428
13429             case SE_I:
13430               if (!(!inst.operands[j].isreg
13431                     && !inst.operands[j].isscalar))
13432                 matches = 0;
13433               break;
13434
13435             case SE_S:
13436               if (!(!inst.operands[j].isreg
13437                     && inst.operands[j].isscalar))
13438                 matches = 0;
13439               break;
13440
13441             case SE_L:
13442               break;
13443             }
13444           if (!matches)
13445             break;
13446         }
13447       if (matches && (j >= ARM_IT_MAX_OPERANDS || !inst.operands[j].present))
13448         /* We've matched all the entries in the shape table, and we don't
13449            have any left over operands which have not been matched.  */
13450         break;
13451     }
13452
13453   va_end (ap);
13454
13455   if (shape == NS_NULL && first_shape != NS_NULL)
13456     first_error (_("invalid instruction shape"));
13457
13458   return shape;
13459 }
13460
13461 /* True if SHAPE is predominantly a quadword operation (most of the time, this
13462    means the Q bit should be set).  */
13463
13464 static int
13465 neon_quad (enum neon_shape shape)
13466 {
13467   return neon_shape_class[shape] == SC_QUAD;
13468 }
13469
13470 static void
13471 neon_modify_type_size (unsigned typebits, enum neon_el_type *g_type,
13472                        unsigned *g_size)
13473 {
13474   /* Allow modification to be made to types which are constrained to be
13475      based on the key element, based on bits set alongside N_EQK.  */
13476   if ((typebits & N_EQK) != 0)
13477     {
13478       if ((typebits & N_HLF) != 0)
13479         *g_size /= 2;
13480       else if ((typebits & N_DBL) != 0)
13481         *g_size *= 2;
13482       if ((typebits & N_SGN) != 0)
13483         *g_type = NT_signed;
13484       else if ((typebits & N_UNS) != 0)
13485         *g_type = NT_unsigned;
13486       else if ((typebits & N_INT) != 0)
13487         *g_type = NT_integer;
13488       else if ((typebits & N_FLT) != 0)
13489         *g_type = NT_float;
13490       else if ((typebits & N_SIZ) != 0)
13491         *g_type = NT_untyped;
13492     }
13493 }
13494
13495 /* Return operand OPNO promoted by bits set in THISARG. KEY should be the "key"
13496    operand type, i.e. the single type specified in a Neon instruction when it
13497    is the only one given.  */
13498
13499 static struct neon_type_el
13500 neon_type_promote (struct neon_type_el *key, unsigned thisarg)
13501 {
13502   struct neon_type_el dest = *key;
13503
13504   gas_assert ((thisarg & N_EQK) != 0);
13505
13506   neon_modify_type_size (thisarg, &dest.type, &dest.size);
13507
13508   return dest;
13509 }
13510
13511 /* Convert Neon type and size into compact bitmask representation.  */
13512
13513 static enum neon_type_mask
13514 type_chk_of_el_type (enum neon_el_type type, unsigned size)
13515 {
13516   switch (type)
13517     {
13518     case NT_untyped:
13519       switch (size)
13520         {
13521         case 8:  return N_8;
13522         case 16: return N_16;
13523         case 32: return N_32;
13524         case 64: return N_64;
13525         default: ;
13526         }
13527       break;
13528
13529     case NT_integer:
13530       switch (size)
13531         {
13532         case 8:  return N_I8;
13533         case 16: return N_I16;
13534         case 32: return N_I32;
13535         case 64: return N_I64;
13536         default: ;
13537         }
13538       break;
13539
13540     case NT_float:
13541       switch (size)
13542         {
13543         case 16: return N_F16;
13544         case 32: return N_F32;
13545         case 64: return N_F64;
13546         default: ;
13547         }
13548       break;
13549
13550     case NT_poly:
13551       switch (size)
13552         {
13553         case 8:  return N_P8;
13554         case 16: return N_P16;
13555         case 64: return N_P64;
13556         default: ;
13557         }
13558       break;
13559
13560     case NT_signed:
13561       switch (size)
13562         {
13563         case 8:  return N_S8;
13564         case 16: return N_S16;
13565         case 32: return N_S32;
13566         case 64: return N_S64;
13567         default: ;
13568         }
13569       break;
13570
13571     case NT_unsigned:
13572       switch (size)
13573         {
13574         case 8:  return N_U8;
13575         case 16: return N_U16;
13576         case 32: return N_U32;
13577         case 64: return N_U64;
13578         default: ;
13579         }
13580       break;
13581
13582     default: ;
13583     }
13584
13585   return N_UTYP;
13586 }
13587
13588 /* Convert compact Neon bitmask type representation to a type and size. Only
13589    handles the case where a single bit is set in the mask.  */
13590
13591 static int
13592 el_type_of_type_chk (enum neon_el_type *type, unsigned *size,
13593                      enum neon_type_mask mask)
13594 {
13595   if ((mask & N_EQK) != 0)
13596     return FAIL;
13597
13598   if ((mask & (N_S8 | N_U8 | N_I8 | N_8 | N_P8)) != 0)
13599     *size = 8;
13600   else if ((mask & (N_S16 | N_U16 | N_I16 | N_16 | N_F16 | N_P16)) != 0)
13601     *size = 16;
13602   else if ((mask & (N_S32 | N_U32 | N_I32 | N_32 | N_F32)) != 0)
13603     *size = 32;
13604   else if ((mask & (N_S64 | N_U64 | N_I64 | N_64 | N_F64 | N_P64)) != 0)
13605     *size = 64;
13606   else
13607     return FAIL;
13608
13609   if ((mask & (N_S8 | N_S16 | N_S32 | N_S64)) != 0)
13610     *type = NT_signed;
13611   else if ((mask & (N_U8 | N_U16 | N_U32 | N_U64)) != 0)
13612     *type = NT_unsigned;
13613   else if ((mask & (N_I8 | N_I16 | N_I32 | N_I64)) != 0)
13614     *type = NT_integer;
13615   else if ((mask & (N_8 | N_16 | N_32 | N_64)) != 0)
13616     *type = NT_untyped;
13617   else if ((mask & (N_P8 | N_P16 | N_P64)) != 0)
13618     *type = NT_poly;
13619   else if ((mask & (N_F16 | N_F32 | N_F64)) != 0)
13620     *type = NT_float;
13621   else
13622     return FAIL;
13623
13624   return SUCCESS;
13625 }
13626
13627 /* Modify a bitmask of allowed types. This is only needed for type
13628    relaxation.  */
13629
13630 static unsigned
13631 modify_types_allowed (unsigned allowed, unsigned mods)
13632 {
13633   unsigned size;
13634   enum neon_el_type type;
13635   unsigned destmask;
13636   int i;
13637
13638   destmask = 0;
13639
13640   for (i = 1; i <= N_MAX_NONSPECIAL; i <<= 1)
13641     {
13642       if (el_type_of_type_chk (&type, &size,
13643                                (enum neon_type_mask) (allowed & i)) == SUCCESS)
13644         {
13645           neon_modify_type_size (mods, &type, &size);
13646           destmask |= type_chk_of_el_type (type, size);
13647         }
13648     }
13649
13650   return destmask;
13651 }
13652
13653 /* Check type and return type classification.
13654    The manual states (paraphrase): If one datatype is given, it indicates the
13655    type given in:
13656     - the second operand, if there is one
13657     - the operand, if there is no second operand
13658     - the result, if there are no operands.
13659    This isn't quite good enough though, so we use a concept of a "key" datatype
13660    which is set on a per-instruction basis, which is the one which matters when
13661    only one data type is written.
13662    Note: this function has side-effects (e.g. filling in missing operands). All
13663    Neon instructions should call it before performing bit encoding.  */
13664
13665 static struct neon_type_el
13666 neon_check_type (unsigned els, enum neon_shape ns, ...)
13667 {
13668   va_list ap;
13669   unsigned i, pass, key_el = 0;
13670   unsigned types[NEON_MAX_TYPE_ELS];
13671   enum neon_el_type k_type = NT_invtype;
13672   unsigned k_size = -1u;
13673   struct neon_type_el badtype = {NT_invtype, -1};
13674   unsigned key_allowed = 0;
13675
13676   /* Optional registers in Neon instructions are always (not) in operand 1.
13677      Fill in the missing operand here, if it was omitted.  */
13678   if (els > 1 && !inst.operands[1].present)
13679     inst.operands[1] = inst.operands[0];
13680
13681   /* Suck up all the varargs.  */
13682   va_start (ap, ns);
13683   for (i = 0; i < els; i++)
13684     {
13685       unsigned thisarg = va_arg (ap, unsigned);
13686       if (thisarg == N_IGNORE_TYPE)
13687         {
13688           va_end (ap);
13689           return badtype;
13690         }
13691       types[i] = thisarg;
13692       if ((thisarg & N_KEY) != 0)
13693         key_el = i;
13694     }
13695   va_end (ap);
13696
13697   if (inst.vectype.elems > 0)
13698     for (i = 0; i < els; i++)
13699       if (inst.operands[i].vectype.type != NT_invtype)
13700         {
13701           first_error (_("types specified in both the mnemonic and operands"));
13702           return badtype;
13703         }
13704
13705   /* Duplicate inst.vectype elements here as necessary.
13706      FIXME: No idea if this is exactly the same as the ARM assembler,
13707      particularly when an insn takes one register and one non-register
13708      operand. */
13709   if (inst.vectype.elems == 1 && els > 1)
13710     {
13711       unsigned j;
13712       inst.vectype.elems = els;
13713       inst.vectype.el[key_el] = inst.vectype.el[0];
13714       for (j = 0; j < els; j++)
13715         if (j != key_el)
13716           inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13717                                                   types[j]);
13718     }
13719   else if (inst.vectype.elems == 0 && els > 0)
13720     {
13721       unsigned j;
13722       /* No types were given after the mnemonic, so look for types specified
13723          after each operand. We allow some flexibility here; as long as the
13724          "key" operand has a type, we can infer the others.  */
13725       for (j = 0; j < els; j++)
13726         if (inst.operands[j].vectype.type != NT_invtype)
13727           inst.vectype.el[j] = inst.operands[j].vectype;
13728
13729       if (inst.operands[key_el].vectype.type != NT_invtype)
13730         {
13731           for (j = 0; j < els; j++)
13732             if (inst.operands[j].vectype.type == NT_invtype)
13733               inst.vectype.el[j] = neon_type_promote (&inst.vectype.el[key_el],
13734                                                       types[j]);
13735         }
13736       else
13737         {
13738           first_error (_("operand types can't be inferred"));
13739           return badtype;
13740         }
13741     }
13742   else if (inst.vectype.elems != els)
13743     {
13744       first_error (_("type specifier has the wrong number of parts"));
13745       return badtype;
13746     }
13747
13748   for (pass = 0; pass < 2; pass++)
13749     {
13750       for (i = 0; i < els; i++)
13751         {
13752           unsigned thisarg = types[i];
13753           unsigned types_allowed = ((thisarg & N_EQK) != 0 && pass != 0)
13754             ? modify_types_allowed (key_allowed, thisarg) : thisarg;
13755           enum neon_el_type g_type = inst.vectype.el[i].type;
13756           unsigned g_size = inst.vectype.el[i].size;
13757
13758           /* Decay more-specific signed & unsigned types to sign-insensitive
13759              integer types if sign-specific variants are unavailable.  */
13760           if ((g_type == NT_signed || g_type == NT_unsigned)
13761               && (types_allowed & N_SU_ALL) == 0)
13762             g_type = NT_integer;
13763
13764           /* If only untyped args are allowed, decay any more specific types to
13765              them. Some instructions only care about signs for some element
13766              sizes, so handle that properly.  */
13767           if (((types_allowed & N_UNT) == 0)
13768               && ((g_size == 8 && (types_allowed & N_8) != 0)
13769                   || (g_size == 16 && (types_allowed & N_16) != 0)
13770                   || (g_size == 32 && (types_allowed & N_32) != 0)
13771                   || (g_size == 64 && (types_allowed & N_64) != 0)))
13772             g_type = NT_untyped;
13773
13774           if (pass == 0)
13775             {
13776               if ((thisarg & N_KEY) != 0)
13777                 {
13778                   k_type = g_type;
13779                   k_size = g_size;
13780                   key_allowed = thisarg & ~N_KEY;
13781                 }
13782             }
13783           else
13784             {
13785               if ((thisarg & N_VFP) != 0)
13786                 {
13787                   enum neon_shape_el regshape;
13788                   unsigned regwidth, match;
13789
13790                   /* PR 11136: Catch the case where we are passed a shape of NS_NULL.  */
13791                   if (ns == NS_NULL)
13792                     {
13793                       first_error (_("invalid instruction shape"));
13794                       return badtype;
13795                     }
13796                   regshape = neon_shape_tab[ns].el[i];
13797                   regwidth = neon_shape_el_size[regshape];
13798
13799                   /* In VFP mode, operands must match register widths. If we
13800                      have a key operand, use its width, else use the width of
13801                      the current operand.  */
13802                   if (k_size != -1u)
13803                     match = k_size;
13804                   else
13805                     match = g_size;
13806
13807                   if (regwidth != match)
13808                     {
13809                       first_error (_("operand size must match register width"));
13810                       return badtype;
13811                     }
13812                 }
13813
13814               if ((thisarg & N_EQK) == 0)
13815                 {
13816                   unsigned given_type = type_chk_of_el_type (g_type, g_size);
13817
13818                   if ((given_type & types_allowed) == 0)
13819                     {
13820                       first_error (_("bad type in Neon instruction"));
13821                       return badtype;
13822                     }
13823                 }
13824               else
13825                 {
13826                   enum neon_el_type mod_k_type = k_type;
13827                   unsigned mod_k_size = k_size;
13828                   neon_modify_type_size (thisarg, &mod_k_type, &mod_k_size);
13829                   if (g_type != mod_k_type || g_size != mod_k_size)
13830                     {
13831                       first_error (_("inconsistent types in Neon instruction"));
13832                       return badtype;
13833                     }
13834                 }
13835             }
13836         }
13837     }
13838
13839   return inst.vectype.el[key_el];
13840 }
13841
13842 /* Neon-style VFP instruction forwarding.  */
13843
13844 /* Thumb VFP instructions have 0xE in the condition field.  */
13845
13846 static void
13847 do_vfp_cond_or_thumb (void)
13848 {
13849   inst.is_neon = 1;
13850
13851   if (thumb_mode)
13852     inst.instruction |= 0xe0000000;
13853   else
13854     inst.instruction |= inst.cond << 28;
13855 }
13856
13857 /* Look up and encode a simple mnemonic, for use as a helper function for the
13858    Neon-style VFP syntax.  This avoids duplication of bits of the insns table,
13859    etc.  It is assumed that operand parsing has already been done, and that the
13860    operands are in the form expected by the given opcode (this isn't necessarily
13861    the same as the form in which they were parsed, hence some massaging must
13862    take place before this function is called).
13863    Checks current arch version against that in the looked-up opcode.  */
13864
13865 static void
13866 do_vfp_nsyn_opcode (const char *opname)
13867 {
13868   const struct asm_opcode *opcode;
13869
13870   opcode = (const struct asm_opcode *) hash_find (arm_ops_hsh, opname);
13871
13872   if (!opcode)
13873     abort ();
13874
13875   constraint (!ARM_CPU_HAS_FEATURE (cpu_variant,
13876                 thumb_mode ? *opcode->tvariant : *opcode->avariant),
13877               _(BAD_FPU));
13878
13879   inst.is_neon = 1;
13880
13881   if (thumb_mode)
13882     {
13883       inst.instruction = opcode->tvalue;
13884       opcode->tencode ();
13885     }
13886   else
13887     {
13888       inst.instruction = (inst.cond << 28) | opcode->avalue;
13889       opcode->aencode ();
13890     }
13891 }
13892
13893 static void
13894 do_vfp_nsyn_add_sub (enum neon_shape rs)
13895 {
13896   int is_add = (inst.instruction & 0x0fffffff) == N_MNEM_vadd;
13897
13898   if (rs == NS_FFF)
13899     {
13900       if (is_add)
13901         do_vfp_nsyn_opcode ("fadds");
13902       else
13903         do_vfp_nsyn_opcode ("fsubs");
13904     }
13905   else
13906     {
13907       if (is_add)
13908         do_vfp_nsyn_opcode ("faddd");
13909       else
13910         do_vfp_nsyn_opcode ("fsubd");
13911     }
13912 }
13913
13914 /* Check operand types to see if this is a VFP instruction, and if so call
13915    PFN ().  */
13916
13917 static int
13918 try_vfp_nsyn (int args, void (*pfn) (enum neon_shape))
13919 {
13920   enum neon_shape rs;
13921   struct neon_type_el et;
13922
13923   switch (args)
13924     {
13925     case 2:
13926       rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
13927       et = neon_check_type (2, rs,
13928         N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13929       break;
13930
13931     case 3:
13932       rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
13933       et = neon_check_type (3, rs,
13934         N_EQK | N_VFP, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
13935       break;
13936
13937     default:
13938       abort ();
13939     }
13940
13941   if (et.type != NT_invtype)
13942     {
13943       pfn (rs);
13944       return SUCCESS;
13945     }
13946
13947   inst.error = NULL;
13948   return FAIL;
13949 }
13950
13951 static void
13952 do_vfp_nsyn_mla_mls (enum neon_shape rs)
13953 {
13954   int is_mla = (inst.instruction & 0x0fffffff) == N_MNEM_vmla;
13955
13956   if (rs == NS_FFF)
13957     {
13958       if (is_mla)
13959         do_vfp_nsyn_opcode ("fmacs");
13960       else
13961         do_vfp_nsyn_opcode ("fnmacs");
13962     }
13963   else
13964     {
13965       if (is_mla)
13966         do_vfp_nsyn_opcode ("fmacd");
13967       else
13968         do_vfp_nsyn_opcode ("fnmacd");
13969     }
13970 }
13971
13972 static void
13973 do_vfp_nsyn_fma_fms (enum neon_shape rs)
13974 {
13975   int is_fma = (inst.instruction & 0x0fffffff) == N_MNEM_vfma;
13976
13977   if (rs == NS_FFF)
13978     {
13979       if (is_fma)
13980         do_vfp_nsyn_opcode ("ffmas");
13981       else
13982         do_vfp_nsyn_opcode ("ffnmas");
13983     }
13984   else
13985     {
13986       if (is_fma)
13987         do_vfp_nsyn_opcode ("ffmad");
13988       else
13989         do_vfp_nsyn_opcode ("ffnmad");
13990     }
13991 }
13992
13993 static void
13994 do_vfp_nsyn_mul (enum neon_shape rs)
13995 {
13996   if (rs == NS_FFF)
13997     do_vfp_nsyn_opcode ("fmuls");
13998   else
13999     do_vfp_nsyn_opcode ("fmuld");
14000 }
14001
14002 static void
14003 do_vfp_nsyn_abs_neg (enum neon_shape rs)
14004 {
14005   int is_neg = (inst.instruction & 0x80) != 0;
14006   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_VFP | N_KEY);
14007
14008   if (rs == NS_FF)
14009     {
14010       if (is_neg)
14011         do_vfp_nsyn_opcode ("fnegs");
14012       else
14013         do_vfp_nsyn_opcode ("fabss");
14014     }
14015   else
14016     {
14017       if (is_neg)
14018         do_vfp_nsyn_opcode ("fnegd");
14019       else
14020         do_vfp_nsyn_opcode ("fabsd");
14021     }
14022 }
14023
14024 /* Encode single-precision (only!) VFP fldm/fstm instructions. Double precision
14025    insns belong to Neon, and are handled elsewhere.  */
14026
14027 static void
14028 do_vfp_nsyn_ldm_stm (int is_dbmode)
14029 {
14030   int is_ldm = (inst.instruction & (1 << 20)) != 0;
14031   if (is_ldm)
14032     {
14033       if (is_dbmode)
14034         do_vfp_nsyn_opcode ("fldmdbs");
14035       else
14036         do_vfp_nsyn_opcode ("fldmias");
14037     }
14038   else
14039     {
14040       if (is_dbmode)
14041         do_vfp_nsyn_opcode ("fstmdbs");
14042       else
14043         do_vfp_nsyn_opcode ("fstmias");
14044     }
14045 }
14046
14047 static void
14048 do_vfp_nsyn_sqrt (void)
14049 {
14050   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
14051   neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
14052
14053   if (rs == NS_FF)
14054     do_vfp_nsyn_opcode ("fsqrts");
14055   else
14056     do_vfp_nsyn_opcode ("fsqrtd");
14057 }
14058
14059 static void
14060 do_vfp_nsyn_div (void)
14061 {
14062   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
14063   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14064     N_F32 | N_F64 | N_KEY | N_VFP);
14065
14066   if (rs == NS_FFF)
14067     do_vfp_nsyn_opcode ("fdivs");
14068   else
14069     do_vfp_nsyn_opcode ("fdivd");
14070 }
14071
14072 static void
14073 do_vfp_nsyn_nmul (void)
14074 {
14075   enum neon_shape rs = neon_select_shape (NS_FFF, NS_DDD, NS_NULL);
14076   neon_check_type (3, rs, N_EQK | N_VFP, N_EQK | N_VFP,
14077     N_F32 | N_F64 | N_KEY | N_VFP);
14078
14079   if (rs == NS_FFF)
14080     {
14081       NEON_ENCODE (SINGLE, inst);
14082       do_vfp_sp_dyadic ();
14083     }
14084   else
14085     {
14086       NEON_ENCODE (DOUBLE, inst);
14087       do_vfp_dp_rd_rn_rm ();
14088     }
14089   do_vfp_cond_or_thumb ();
14090 }
14091
14092 static void
14093 do_vfp_nsyn_cmp (void)
14094 {
14095   if (inst.operands[1].isreg)
14096     {
14097       enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_NULL);
14098       neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
14099
14100       if (rs == NS_FF)
14101         {
14102           NEON_ENCODE (SINGLE, inst);
14103           do_vfp_sp_monadic ();
14104         }
14105       else
14106         {
14107           NEON_ENCODE (DOUBLE, inst);
14108           do_vfp_dp_rd_rm ();
14109         }
14110     }
14111   else
14112     {
14113       enum neon_shape rs = neon_select_shape (NS_FI, NS_DI, NS_NULL);
14114       neon_check_type (2, rs, N_F32 | N_F64 | N_KEY | N_VFP, N_EQK);
14115
14116       switch (inst.instruction & 0x0fffffff)
14117         {
14118         case N_MNEM_vcmp:
14119           inst.instruction += N_MNEM_vcmpz - N_MNEM_vcmp;
14120           break;
14121         case N_MNEM_vcmpe:
14122           inst.instruction += N_MNEM_vcmpez - N_MNEM_vcmpe;
14123           break;
14124         default:
14125           abort ();
14126         }
14127
14128       if (rs == NS_FI)
14129         {
14130           NEON_ENCODE (SINGLE, inst);
14131           do_vfp_sp_compare_z ();
14132         }
14133       else
14134         {
14135           NEON_ENCODE (DOUBLE, inst);
14136           do_vfp_dp_rd ();
14137         }
14138     }
14139   do_vfp_cond_or_thumb ();
14140 }
14141
14142 static void
14143 nsyn_insert_sp (void)
14144 {
14145   inst.operands[1] = inst.operands[0];
14146   memset (&inst.operands[0], '\0', sizeof (inst.operands[0]));
14147   inst.operands[0].reg = REG_SP;
14148   inst.operands[0].isreg = 1;
14149   inst.operands[0].writeback = 1;
14150   inst.operands[0].present = 1;
14151 }
14152
14153 static void
14154 do_vfp_nsyn_push (void)
14155 {
14156   nsyn_insert_sp ();
14157   if (inst.operands[1].issingle)
14158     do_vfp_nsyn_opcode ("fstmdbs");
14159   else
14160     do_vfp_nsyn_opcode ("fstmdbd");
14161 }
14162
14163 static void
14164 do_vfp_nsyn_pop (void)
14165 {
14166   nsyn_insert_sp ();
14167   if (inst.operands[1].issingle)
14168     do_vfp_nsyn_opcode ("fldmias");
14169   else
14170     do_vfp_nsyn_opcode ("fldmiad");
14171 }
14172
14173 /* Fix up Neon data-processing instructions, ORing in the correct bits for
14174    ARM mode or Thumb mode and moving the encoded bit 24 to bit 28.  */
14175
14176 static void
14177 neon_dp_fixup (struct arm_it* insn)
14178 {
14179   unsigned int i = insn->instruction;
14180   insn->is_neon = 1;
14181
14182   if (thumb_mode)
14183     {
14184       /* The U bit is at bit 24 by default. Move to bit 28 in Thumb mode.  */
14185       if (i & (1 << 24))
14186         i |= 1 << 28;
14187
14188       i &= ~(1 << 24);
14189
14190       i |= 0xef000000;
14191     }
14192   else
14193     i |= 0xf2000000;
14194
14195   insn->instruction = i;
14196 }
14197
14198 /* Turn a size (8, 16, 32, 64) into the respective bit number minus 3
14199    (0, 1, 2, 3).  */
14200
14201 static unsigned
14202 neon_logbits (unsigned x)
14203 {
14204   return ffs (x) - 4;
14205 }
14206
14207 #define LOW4(R) ((R) & 0xf)
14208 #define HI1(R) (((R) >> 4) & 1)
14209
14210 /* Encode insns with bit pattern:
14211
14212   |28/24|23|22 |21 20|19 16|15 12|11    8|7|6|5|4|3  0|
14213   |  U  |x |D  |size | Rn  | Rd  |x x x x|N|Q|M|x| Rm |
14214
14215   SIZE is passed in bits. -1 means size field isn't changed, in case it has a
14216   different meaning for some instruction.  */
14217
14218 static void
14219 neon_three_same (int isquad, int ubit, int size)
14220 {
14221   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14222   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14223   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14224   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14225   inst.instruction |= LOW4 (inst.operands[2].reg);
14226   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
14227   inst.instruction |= (isquad != 0) << 6;
14228   inst.instruction |= (ubit != 0) << 24;
14229   if (size != -1)
14230     inst.instruction |= neon_logbits (size) << 20;
14231
14232   neon_dp_fixup (&inst);
14233 }
14234
14235 /* Encode instructions of the form:
14236
14237   |28/24|23|22|21 20|19 18|17 16|15 12|11      7|6|5|4|3  0|
14238   |  U  |x |D |x  x |size |x  x | Rd  |x x x x x|Q|M|x| Rm |
14239
14240   Don't write size if SIZE == -1.  */
14241
14242 static void
14243 neon_two_same (int qbit, int ubit, int size)
14244 {
14245   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14246   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14247   inst.instruction |= LOW4 (inst.operands[1].reg);
14248   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14249   inst.instruction |= (qbit != 0) << 6;
14250   inst.instruction |= (ubit != 0) << 24;
14251
14252   if (size != -1)
14253     inst.instruction |= neon_logbits (size) << 18;
14254
14255   neon_dp_fixup (&inst);
14256 }
14257
14258 /* Neon instruction encoders, in approximate order of appearance.  */
14259
14260 static void
14261 do_neon_dyadic_i_su (void)
14262 {
14263   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14264   struct neon_type_el et = neon_check_type (3, rs,
14265     N_EQK, N_EQK, N_SU_32 | N_KEY);
14266   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14267 }
14268
14269 static void
14270 do_neon_dyadic_i64_su (void)
14271 {
14272   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14273   struct neon_type_el et = neon_check_type (3, rs,
14274     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14275   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14276 }
14277
14278 static void
14279 neon_imm_shift (int write_ubit, int uval, int isquad, struct neon_type_el et,
14280                 unsigned immbits)
14281 {
14282   unsigned size = et.size >> 3;
14283   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14284   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14285   inst.instruction |= LOW4 (inst.operands[1].reg);
14286   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14287   inst.instruction |= (isquad != 0) << 6;
14288   inst.instruction |= immbits << 16;
14289   inst.instruction |= (size >> 3) << 7;
14290   inst.instruction |= (size & 0x7) << 19;
14291   if (write_ubit)
14292     inst.instruction |= (uval != 0) << 24;
14293
14294   neon_dp_fixup (&inst);
14295 }
14296
14297 static void
14298 do_neon_shl_imm (void)
14299 {
14300   if (!inst.operands[2].isreg)
14301     {
14302       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14303       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_KEY | N_I_ALL);
14304       int imm = inst.operands[2].imm;
14305
14306       constraint (imm < 0 || (unsigned)imm >= et.size,
14307                   _("immediate out of range for shift"));
14308       NEON_ENCODE (IMMED, inst);
14309       neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14310     }
14311   else
14312     {
14313       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14314       struct neon_type_el et = neon_check_type (3, rs,
14315         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14316       unsigned int tmp;
14317
14318       /* VSHL/VQSHL 3-register variants have syntax such as:
14319            vshl.xx Dd, Dm, Dn
14320          whereas other 3-register operations encoded by neon_three_same have
14321          syntax like:
14322            vadd.xx Dd, Dn, Dm
14323          (i.e. with Dn & Dm reversed). Swap operands[1].reg and operands[2].reg
14324          here.  */
14325       tmp = inst.operands[2].reg;
14326       inst.operands[2].reg = inst.operands[1].reg;
14327       inst.operands[1].reg = tmp;
14328       NEON_ENCODE (INTEGER, inst);
14329       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14330     }
14331 }
14332
14333 static void
14334 do_neon_qshl_imm (void)
14335 {
14336   if (!inst.operands[2].isreg)
14337     {
14338       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14339       struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
14340       int imm = inst.operands[2].imm;
14341
14342       constraint (imm < 0 || (unsigned)imm >= et.size,
14343                   _("immediate out of range for shift"));
14344       NEON_ENCODE (IMMED, inst);
14345       neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et, imm);
14346     }
14347   else
14348     {
14349       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14350       struct neon_type_el et = neon_check_type (3, rs,
14351         N_EQK, N_SU_ALL | N_KEY, N_EQK | N_SGN);
14352       unsigned int tmp;
14353
14354       /* See note in do_neon_shl_imm.  */
14355       tmp = inst.operands[2].reg;
14356       inst.operands[2].reg = inst.operands[1].reg;
14357       inst.operands[1].reg = tmp;
14358       NEON_ENCODE (INTEGER, inst);
14359       neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14360     }
14361 }
14362
14363 static void
14364 do_neon_rshl (void)
14365 {
14366   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14367   struct neon_type_el et = neon_check_type (3, rs,
14368     N_EQK, N_EQK, N_SU_ALL | N_KEY);
14369   unsigned int tmp;
14370
14371   tmp = inst.operands[2].reg;
14372   inst.operands[2].reg = inst.operands[1].reg;
14373   inst.operands[1].reg = tmp;
14374   neon_three_same (neon_quad (rs), et.type == NT_unsigned, et.size);
14375 }
14376
14377 static int
14378 neon_cmode_for_logic_imm (unsigned immediate, unsigned *immbits, int size)
14379 {
14380   /* Handle .I8 pseudo-instructions.  */
14381   if (size == 8)
14382     {
14383       /* Unfortunately, this will make everything apart from zero out-of-range.
14384          FIXME is this the intended semantics? There doesn't seem much point in
14385          accepting .I8 if so.  */
14386       immediate |= immediate << 8;
14387       size = 16;
14388     }
14389
14390   if (size >= 32)
14391     {
14392       if (immediate == (immediate & 0x000000ff))
14393         {
14394           *immbits = immediate;
14395           return 0x1;
14396         }
14397       else if (immediate == (immediate & 0x0000ff00))
14398         {
14399           *immbits = immediate >> 8;
14400           return 0x3;
14401         }
14402       else if (immediate == (immediate & 0x00ff0000))
14403         {
14404           *immbits = immediate >> 16;
14405           return 0x5;
14406         }
14407       else if (immediate == (immediate & 0xff000000))
14408         {
14409           *immbits = immediate >> 24;
14410           return 0x7;
14411         }
14412       if ((immediate & 0xffff) != (immediate >> 16))
14413         goto bad_immediate;
14414       immediate &= 0xffff;
14415     }
14416
14417   if (immediate == (immediate & 0x000000ff))
14418     {
14419       *immbits = immediate;
14420       return 0x9;
14421     }
14422   else if (immediate == (immediate & 0x0000ff00))
14423     {
14424       *immbits = immediate >> 8;
14425       return 0xb;
14426     }
14427
14428   bad_immediate:
14429   first_error (_("immediate value out of range"));
14430   return FAIL;
14431 }
14432
14433 static void
14434 do_neon_logic (void)
14435 {
14436   if (inst.operands[2].present && inst.operands[2].isreg)
14437     {
14438       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14439       neon_check_type (3, rs, N_IGNORE_TYPE);
14440       /* U bit and size field were set as part of the bitmask.  */
14441       NEON_ENCODE (INTEGER, inst);
14442       neon_three_same (neon_quad (rs), 0, -1);
14443     }
14444   else
14445     {
14446       const int three_ops_form = (inst.operands[2].present
14447                                   && !inst.operands[2].isreg);
14448       const int immoperand = (three_ops_form ? 2 : 1);
14449       enum neon_shape rs = (three_ops_form
14450                             ? neon_select_shape (NS_DDI, NS_QQI, NS_NULL)
14451                             : neon_select_shape (NS_DI, NS_QI, NS_NULL));
14452       struct neon_type_el et = neon_check_type (2, rs,
14453         N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
14454       enum neon_opc opcode = (enum neon_opc) inst.instruction & 0x0fffffff;
14455       unsigned immbits;
14456       int cmode;
14457
14458       if (et.type == NT_invtype)
14459         return;
14460
14461       if (three_ops_form)
14462         constraint (inst.operands[0].reg != inst.operands[1].reg,
14463                     _("first and second operands shall be the same register"));
14464
14465       NEON_ENCODE (IMMED, inst);
14466
14467       immbits = inst.operands[immoperand].imm;
14468       if (et.size == 64)
14469         {
14470           /* .i64 is a pseudo-op, so the immediate must be a repeating
14471              pattern.  */
14472           if (immbits != (inst.operands[immoperand].regisimm ?
14473                           inst.operands[immoperand].reg : 0))
14474             {
14475               /* Set immbits to an invalid constant.  */
14476               immbits = 0xdeadbeef;
14477             }
14478         }
14479
14480       switch (opcode)
14481         {
14482         case N_MNEM_vbic:
14483           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14484           break;
14485
14486         case N_MNEM_vorr:
14487           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14488           break;
14489
14490         case N_MNEM_vand:
14491           /* Pseudo-instruction for VBIC.  */
14492           neon_invert_size (&immbits, 0, et.size);
14493           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14494           break;
14495
14496         case N_MNEM_vorn:
14497           /* Pseudo-instruction for VORR.  */
14498           neon_invert_size (&immbits, 0, et.size);
14499           cmode = neon_cmode_for_logic_imm (immbits, &immbits, et.size);
14500           break;
14501
14502         default:
14503           abort ();
14504         }
14505
14506       if (cmode == FAIL)
14507         return;
14508
14509       inst.instruction |= neon_quad (rs) << 6;
14510       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14511       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14512       inst.instruction |= cmode << 8;
14513       neon_write_immbits (immbits);
14514
14515       neon_dp_fixup (&inst);
14516     }
14517 }
14518
14519 static void
14520 do_neon_bitfield (void)
14521 {
14522   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14523   neon_check_type (3, rs, N_IGNORE_TYPE);
14524   neon_three_same (neon_quad (rs), 0, -1);
14525 }
14526
14527 static void
14528 neon_dyadic_misc (enum neon_el_type ubit_meaning, unsigned types,
14529                   unsigned destbits)
14530 {
14531   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14532   struct neon_type_el et = neon_check_type (3, rs, N_EQK | destbits, N_EQK,
14533                                             types | N_KEY);
14534   if (et.type == NT_float)
14535     {
14536       NEON_ENCODE (FLOAT, inst);
14537       neon_three_same (neon_quad (rs), 0, -1);
14538     }
14539   else
14540     {
14541       NEON_ENCODE (INTEGER, inst);
14542       neon_three_same (neon_quad (rs), et.type == ubit_meaning, et.size);
14543     }
14544 }
14545
14546 static void
14547 do_neon_dyadic_if_su (void)
14548 {
14549   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14550 }
14551
14552 static void
14553 do_neon_dyadic_if_su_d (void)
14554 {
14555   /* This version only allow D registers, but that constraint is enforced during
14556      operand parsing so we don't need to do anything extra here.  */
14557   neon_dyadic_misc (NT_unsigned, N_SUF_32, 0);
14558 }
14559
14560 static void
14561 do_neon_dyadic_if_i_d (void)
14562 {
14563   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14564      affected if we specify unsigned args.  */
14565   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14566 }
14567
14568 enum vfp_or_neon_is_neon_bits
14569 {
14570   NEON_CHECK_CC = 1,
14571   NEON_CHECK_ARCH = 2,
14572   NEON_CHECK_ARCH8 = 4
14573 };
14574
14575 /* Call this function if an instruction which may have belonged to the VFP or
14576    Neon instruction sets, but turned out to be a Neon instruction (due to the
14577    operand types involved, etc.). We have to check and/or fix-up a couple of
14578    things:
14579
14580      - Make sure the user hasn't attempted to make a Neon instruction
14581        conditional.
14582      - Alter the value in the condition code field if necessary.
14583      - Make sure that the arch supports Neon instructions.
14584
14585    Which of these operations take place depends on bits from enum
14586    vfp_or_neon_is_neon_bits.
14587
14588    WARNING: This function has side effects! If NEON_CHECK_CC is used and the
14589    current instruction's condition is COND_ALWAYS, the condition field is
14590    changed to inst.uncond_value. This is necessary because instructions shared
14591    between VFP and Neon may be conditional for the VFP variants only, and the
14592    unconditional Neon version must have, e.g., 0xF in the condition field.  */
14593
14594 static int
14595 vfp_or_neon_is_neon (unsigned check)
14596 {
14597   /* Conditions are always legal in Thumb mode (IT blocks).  */
14598   if (!thumb_mode && (check & NEON_CHECK_CC))
14599     {
14600       if (inst.cond != COND_ALWAYS)
14601         {
14602           first_error (_(BAD_COND));
14603           return FAIL;
14604         }
14605       if (inst.uncond_value != -1)
14606         inst.instruction |= inst.uncond_value << 28;
14607     }
14608
14609   if ((check & NEON_CHECK_ARCH)
14610       && !mark_feature_used (&fpu_neon_ext_v1))
14611     {
14612       first_error (_(BAD_FPU));
14613       return FAIL;
14614     }
14615
14616   if ((check & NEON_CHECK_ARCH8)
14617       && !mark_feature_used (&fpu_neon_ext_armv8))
14618     {
14619       first_error (_(BAD_FPU));
14620       return FAIL;
14621     }
14622
14623   return SUCCESS;
14624 }
14625
14626 static void
14627 do_neon_addsub_if_i (void)
14628 {
14629   if (try_vfp_nsyn (3, do_vfp_nsyn_add_sub) == SUCCESS)
14630     return;
14631
14632   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14633     return;
14634
14635   /* The "untyped" case can't happen. Do this to stop the "U" bit being
14636      affected if we specify unsigned args.  */
14637   neon_dyadic_misc (NT_untyped, N_IF_32 | N_I64, 0);
14638 }
14639
14640 /* Swaps operands 1 and 2. If operand 1 (optional arg) was omitted, we want the
14641    result to be:
14642      V<op> A,B     (A is operand 0, B is operand 2)
14643    to mean:
14644      V<op> A,B,A
14645    not:
14646      V<op> A,B,B
14647    so handle that case specially.  */
14648
14649 static void
14650 neon_exchange_operands (void)
14651 {
14652   void *scratch = alloca (sizeof (inst.operands[0]));
14653   if (inst.operands[1].present)
14654     {
14655       /* Swap operands[1] and operands[2].  */
14656       memcpy (scratch, &inst.operands[1], sizeof (inst.operands[0]));
14657       inst.operands[1] = inst.operands[2];
14658       memcpy (&inst.operands[2], scratch, sizeof (inst.operands[0]));
14659     }
14660   else
14661     {
14662       inst.operands[1] = inst.operands[2];
14663       inst.operands[2] = inst.operands[0];
14664     }
14665 }
14666
14667 static void
14668 neon_compare (unsigned regtypes, unsigned immtypes, int invert)
14669 {
14670   if (inst.operands[2].isreg)
14671     {
14672       if (invert)
14673         neon_exchange_operands ();
14674       neon_dyadic_misc (NT_unsigned, regtypes, N_SIZ);
14675     }
14676   else
14677     {
14678       enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14679       struct neon_type_el et = neon_check_type (2, rs,
14680         N_EQK | N_SIZ, immtypes | N_KEY);
14681
14682       NEON_ENCODE (IMMED, inst);
14683       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14684       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14685       inst.instruction |= LOW4 (inst.operands[1].reg);
14686       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14687       inst.instruction |= neon_quad (rs) << 6;
14688       inst.instruction |= (et.type == NT_float) << 10;
14689       inst.instruction |= neon_logbits (et.size) << 18;
14690
14691       neon_dp_fixup (&inst);
14692     }
14693 }
14694
14695 static void
14696 do_neon_cmp (void)
14697 {
14698   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, FALSE);
14699 }
14700
14701 static void
14702 do_neon_cmp_inv (void)
14703 {
14704   neon_compare (N_SUF_32, N_S8 | N_S16 | N_S32 | N_F32, TRUE);
14705 }
14706
14707 static void
14708 do_neon_ceq (void)
14709 {
14710   neon_compare (N_IF_32, N_IF_32, FALSE);
14711 }
14712
14713 /* For multiply instructions, we have the possibility of 16-bit or 32-bit
14714    scalars, which are encoded in 5 bits, M : Rm.
14715    For 16-bit scalars, the register is encoded in Rm[2:0] and the index in
14716    M:Rm[3], and for 32-bit scalars, the register is encoded in Rm[3:0] and the
14717    index in M.  */
14718
14719 static unsigned
14720 neon_scalar_for_mul (unsigned scalar, unsigned elsize)
14721 {
14722   unsigned regno = NEON_SCALAR_REG (scalar);
14723   unsigned elno = NEON_SCALAR_INDEX (scalar);
14724
14725   switch (elsize)
14726     {
14727     case 16:
14728       if (regno > 7 || elno > 3)
14729         goto bad_scalar;
14730       return regno | (elno << 3);
14731
14732     case 32:
14733       if (regno > 15 || elno > 1)
14734         goto bad_scalar;
14735       return regno | (elno << 4);
14736
14737     default:
14738     bad_scalar:
14739       first_error (_("scalar out of range for multiply instruction"));
14740     }
14741
14742   return 0;
14743 }
14744
14745 /* Encode multiply / multiply-accumulate scalar instructions.  */
14746
14747 static void
14748 neon_mul_mac (struct neon_type_el et, int ubit)
14749 {
14750   unsigned scalar;
14751
14752   /* Give a more helpful error message if we have an invalid type.  */
14753   if (et.type == NT_invtype)
14754     return;
14755
14756   scalar = neon_scalar_for_mul (inst.operands[2].reg, et.size);
14757   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14758   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14759   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
14760   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
14761   inst.instruction |= LOW4 (scalar);
14762   inst.instruction |= HI1 (scalar) << 5;
14763   inst.instruction |= (et.type == NT_float) << 8;
14764   inst.instruction |= neon_logbits (et.size) << 20;
14765   inst.instruction |= (ubit != 0) << 24;
14766
14767   neon_dp_fixup (&inst);
14768 }
14769
14770 static void
14771 do_neon_mac_maybe_scalar (void)
14772 {
14773   if (try_vfp_nsyn (3, do_vfp_nsyn_mla_mls) == SUCCESS)
14774     return;
14775
14776   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14777     return;
14778
14779   if (inst.operands[2].isscalar)
14780     {
14781       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14782       struct neon_type_el et = neon_check_type (3, rs,
14783         N_EQK, N_EQK, N_I16 | N_I32 | N_F32 | N_KEY);
14784       NEON_ENCODE (SCALAR, inst);
14785       neon_mul_mac (et, neon_quad (rs));
14786     }
14787   else
14788     {
14789       /* The "untyped" case can't happen.  Do this to stop the "U" bit being
14790          affected if we specify unsigned args.  */
14791       neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14792     }
14793 }
14794
14795 static void
14796 do_neon_fmac (void)
14797 {
14798   if (try_vfp_nsyn (3, do_vfp_nsyn_fma_fms) == SUCCESS)
14799     return;
14800
14801   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14802     return;
14803
14804   neon_dyadic_misc (NT_untyped, N_IF_32, 0);
14805 }
14806
14807 static void
14808 do_neon_tst (void)
14809 {
14810   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14811   struct neon_type_el et = neon_check_type (3, rs,
14812     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_KEY);
14813   neon_three_same (neon_quad (rs), 0, et.size);
14814 }
14815
14816 /* VMUL with 3 registers allows the P8 type. The scalar version supports the
14817    same types as the MAC equivalents. The polynomial type for this instruction
14818    is encoded the same as the integer type.  */
14819
14820 static void
14821 do_neon_mul (void)
14822 {
14823   if (try_vfp_nsyn (3, do_vfp_nsyn_mul) == SUCCESS)
14824     return;
14825
14826   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14827     return;
14828
14829   if (inst.operands[2].isscalar)
14830     do_neon_mac_maybe_scalar ();
14831   else
14832     neon_dyadic_misc (NT_poly, N_I8 | N_I16 | N_I32 | N_F32 | N_P8, 0);
14833 }
14834
14835 static void
14836 do_neon_qdmulh (void)
14837 {
14838   if (inst.operands[2].isscalar)
14839     {
14840       enum neon_shape rs = neon_select_shape (NS_DDS, NS_QQS, NS_NULL);
14841       struct neon_type_el et = neon_check_type (3, rs,
14842         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14843       NEON_ENCODE (SCALAR, inst);
14844       neon_mul_mac (et, neon_quad (rs));
14845     }
14846   else
14847     {
14848       enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14849       struct neon_type_el et = neon_check_type (3, rs,
14850         N_EQK, N_EQK, N_S16 | N_S32 | N_KEY);
14851       NEON_ENCODE (INTEGER, inst);
14852       /* The U bit (rounding) comes from bit mask.  */
14853       neon_three_same (neon_quad (rs), 0, et.size);
14854     }
14855 }
14856
14857 static void
14858 do_neon_fcmp_absolute (void)
14859 {
14860   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14861   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14862   /* Size field comes from bit mask.  */
14863   neon_three_same (neon_quad (rs), 1, -1);
14864 }
14865
14866 static void
14867 do_neon_fcmp_absolute_inv (void)
14868 {
14869   neon_exchange_operands ();
14870   do_neon_fcmp_absolute ();
14871 }
14872
14873 static void
14874 do_neon_step (void)
14875 {
14876   enum neon_shape rs = neon_select_shape (NS_DDD, NS_QQQ, NS_NULL);
14877   neon_check_type (3, rs, N_EQK, N_EQK, N_F32 | N_KEY);
14878   neon_three_same (neon_quad (rs), 0, -1);
14879 }
14880
14881 static void
14882 do_neon_abs_neg (void)
14883 {
14884   enum neon_shape rs;
14885   struct neon_type_el et;
14886
14887   if (try_vfp_nsyn (2, do_vfp_nsyn_abs_neg) == SUCCESS)
14888     return;
14889
14890   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
14891     return;
14892
14893   rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
14894   et = neon_check_type (2, rs, N_EQK, N_S8 | N_S16 | N_S32 | N_F32 | N_KEY);
14895
14896   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
14897   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
14898   inst.instruction |= LOW4 (inst.operands[1].reg);
14899   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
14900   inst.instruction |= neon_quad (rs) << 6;
14901   inst.instruction |= (et.type == NT_float) << 10;
14902   inst.instruction |= neon_logbits (et.size) << 18;
14903
14904   neon_dp_fixup (&inst);
14905 }
14906
14907 static void
14908 do_neon_sli (void)
14909 {
14910   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14911   struct neon_type_el et = neon_check_type (2, rs,
14912     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14913   int imm = inst.operands[2].imm;
14914   constraint (imm < 0 || (unsigned)imm >= et.size,
14915               _("immediate out of range for insert"));
14916   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14917 }
14918
14919 static void
14920 do_neon_sri (void)
14921 {
14922   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14923   struct neon_type_el et = neon_check_type (2, rs,
14924     N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
14925   int imm = inst.operands[2].imm;
14926   constraint (imm < 1 || (unsigned)imm > et.size,
14927               _("immediate out of range for insert"));
14928   neon_imm_shift (FALSE, 0, neon_quad (rs), et, et.size - imm);
14929 }
14930
14931 static void
14932 do_neon_qshlu_imm (void)
14933 {
14934   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
14935   struct neon_type_el et = neon_check_type (2, rs,
14936     N_EQK | N_UNS, N_S8 | N_S16 | N_S32 | N_S64 | N_KEY);
14937   int imm = inst.operands[2].imm;
14938   constraint (imm < 0 || (unsigned)imm >= et.size,
14939               _("immediate out of range for shift"));
14940   /* Only encodes the 'U present' variant of the instruction.
14941      In this case, signed types have OP (bit 8) set to 0.
14942      Unsigned types have OP set to 1.  */
14943   inst.instruction |= (et.type == NT_unsigned) << 8;
14944   /* The rest of the bits are the same as other immediate shifts.  */
14945   neon_imm_shift (FALSE, 0, neon_quad (rs), et, imm);
14946 }
14947
14948 static void
14949 do_neon_qmovn (void)
14950 {
14951   struct neon_type_el et = neon_check_type (2, NS_DQ,
14952     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14953   /* Saturating move where operands can be signed or unsigned, and the
14954      destination has the same signedness.  */
14955   NEON_ENCODE (INTEGER, inst);
14956   if (et.type == NT_unsigned)
14957     inst.instruction |= 0xc0;
14958   else
14959     inst.instruction |= 0x80;
14960   neon_two_same (0, 1, et.size / 2);
14961 }
14962
14963 static void
14964 do_neon_qmovun (void)
14965 {
14966   struct neon_type_el et = neon_check_type (2, NS_DQ,
14967     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
14968   /* Saturating move with unsigned results. Operands must be signed.  */
14969   NEON_ENCODE (INTEGER, inst);
14970   neon_two_same (0, 1, et.size / 2);
14971 }
14972
14973 static void
14974 do_neon_rshift_sat_narrow (void)
14975 {
14976   /* FIXME: Types for narrowing. If operands are signed, results can be signed
14977      or unsigned. If operands are unsigned, results must also be unsigned.  */
14978   struct neon_type_el et = neon_check_type (2, NS_DQI,
14979     N_EQK | N_HLF, N_SU_16_64 | N_KEY);
14980   int imm = inst.operands[2].imm;
14981   /* This gets the bounds check, size encoding and immediate bits calculation
14982      right.  */
14983   et.size /= 2;
14984
14985   /* VQ{R}SHRN.I<size> <Dd>, <Qm>, #0 is a synonym for
14986      VQMOVN.I<size> <Dd>, <Qm>.  */
14987   if (imm == 0)
14988     {
14989       inst.operands[2].present = 0;
14990       inst.instruction = N_MNEM_vqmovn;
14991       do_neon_qmovn ();
14992       return;
14993     }
14994
14995   constraint (imm < 1 || (unsigned)imm > et.size,
14996               _("immediate out of range"));
14997   neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, et.size - imm);
14998 }
14999
15000 static void
15001 do_neon_rshift_sat_narrow_u (void)
15002 {
15003   /* FIXME: Types for narrowing. If operands are signed, results can be signed
15004      or unsigned. If operands are unsigned, results must also be unsigned.  */
15005   struct neon_type_el et = neon_check_type (2, NS_DQI,
15006     N_EQK | N_HLF | N_UNS, N_S16 | N_S32 | N_S64 | N_KEY);
15007   int imm = inst.operands[2].imm;
15008   /* This gets the bounds check, size encoding and immediate bits calculation
15009      right.  */
15010   et.size /= 2;
15011
15012   /* VQSHRUN.I<size> <Dd>, <Qm>, #0 is a synonym for
15013      VQMOVUN.I<size> <Dd>, <Qm>.  */
15014   if (imm == 0)
15015     {
15016       inst.operands[2].present = 0;
15017       inst.instruction = N_MNEM_vqmovun;
15018       do_neon_qmovun ();
15019       return;
15020     }
15021
15022   constraint (imm < 1 || (unsigned)imm > et.size,
15023               _("immediate out of range"));
15024   /* FIXME: The manual is kind of unclear about what value U should have in
15025      VQ{R}SHRUN instructions, but U=0, op=0 definitely encodes VRSHR, so it
15026      must be 1.  */
15027   neon_imm_shift (TRUE, 1, 0, et, et.size - imm);
15028 }
15029
15030 static void
15031 do_neon_movn (void)
15032 {
15033   struct neon_type_el et = neon_check_type (2, NS_DQ,
15034     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15035   NEON_ENCODE (INTEGER, inst);
15036   neon_two_same (0, 1, et.size / 2);
15037 }
15038
15039 static void
15040 do_neon_rshift_narrow (void)
15041 {
15042   struct neon_type_el et = neon_check_type (2, NS_DQI,
15043     N_EQK | N_HLF, N_I16 | N_I32 | N_I64 | N_KEY);
15044   int imm = inst.operands[2].imm;
15045   /* This gets the bounds check, size encoding and immediate bits calculation
15046      right.  */
15047   et.size /= 2;
15048
15049   /* If immediate is zero then we are a pseudo-instruction for
15050      VMOVN.I<size> <Dd>, <Qm>  */
15051   if (imm == 0)
15052     {
15053       inst.operands[2].present = 0;
15054       inst.instruction = N_MNEM_vmovn;
15055       do_neon_movn ();
15056       return;
15057     }
15058
15059   constraint (imm < 1 || (unsigned)imm > et.size,
15060               _("immediate out of range for narrowing operation"));
15061   neon_imm_shift (FALSE, 0, 0, et, et.size - imm);
15062 }
15063
15064 static void
15065 do_neon_shll (void)
15066 {
15067   /* FIXME: Type checking when lengthening.  */
15068   struct neon_type_el et = neon_check_type (2, NS_QDI,
15069     N_EQK | N_DBL, N_I8 | N_I16 | N_I32 | N_KEY);
15070   unsigned imm = inst.operands[2].imm;
15071
15072   if (imm == et.size)
15073     {
15074       /* Maximum shift variant.  */
15075       NEON_ENCODE (INTEGER, inst);
15076       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15077       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15078       inst.instruction |= LOW4 (inst.operands[1].reg);
15079       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15080       inst.instruction |= neon_logbits (et.size) << 18;
15081
15082       neon_dp_fixup (&inst);
15083     }
15084   else
15085     {
15086       /* A more-specific type check for non-max versions.  */
15087       et = neon_check_type (2, NS_QDI,
15088         N_EQK | N_DBL, N_SU_32 | N_KEY);
15089       NEON_ENCODE (IMMED, inst);
15090       neon_imm_shift (TRUE, et.type == NT_unsigned, 0, et, imm);
15091     }
15092 }
15093
15094 /* Check the various types for the VCVT instruction, and return which version
15095    the current instruction is.  */
15096
15097 #define CVT_FLAVOUR_VAR                                                       \
15098   CVT_VAR (s32_f32, N_S32, N_F32, whole_reg,   "ftosls", "ftosis", "ftosizs") \
15099   CVT_VAR (u32_f32, N_U32, N_F32, whole_reg,   "ftouls", "ftouis", "ftouizs") \
15100   CVT_VAR (f32_s32, N_F32, N_S32, whole_reg,   "fsltos", "fsitos", NULL)      \
15101   CVT_VAR (f32_u32, N_F32, N_U32, whole_reg,   "fultos", "fuitos", NULL)      \
15102   /* Half-precision conversions.  */                                          \
15103   CVT_VAR (f32_f16, N_F32, N_F16, whole_reg,   NULL,     NULL,     NULL)      \
15104   CVT_VAR (f16_f32, N_F16, N_F32, whole_reg,   NULL,     NULL,     NULL)      \
15105   /* VFP instructions.  */                                                    \
15106   CVT_VAR (f32_f64, N_F32, N_F64, N_VFP,       NULL,     "fcvtsd", NULL)      \
15107   CVT_VAR (f64_f32, N_F64, N_F32, N_VFP,       NULL,     "fcvtds", NULL)      \
15108   CVT_VAR (s32_f64, N_S32, N_F64 | key, N_VFP, "ftosld", "ftosid", "ftosizd") \
15109   CVT_VAR (u32_f64, N_U32, N_F64 | key, N_VFP, "ftould", "ftouid", "ftouizd") \
15110   CVT_VAR (f64_s32, N_F64 | key, N_S32, N_VFP, "fsltod", "fsitod", NULL)      \
15111   CVT_VAR (f64_u32, N_F64 | key, N_U32, N_VFP, "fultod", "fuitod", NULL)      \
15112   /* VFP instructions with bitshift.  */                                      \
15113   CVT_VAR (f32_s16, N_F32 | key, N_S16, N_VFP, "fshtos", NULL,     NULL)      \
15114   CVT_VAR (f32_u16, N_F32 | key, N_U16, N_VFP, "fuhtos", NULL,     NULL)      \
15115   CVT_VAR (f64_s16, N_F64 | key, N_S16, N_VFP, "fshtod", NULL,     NULL)      \
15116   CVT_VAR (f64_u16, N_F64 | key, N_U16, N_VFP, "fuhtod", NULL,     NULL)      \
15117   CVT_VAR (s16_f32, N_S16, N_F32 | key, N_VFP, "ftoshs", NULL,     NULL)      \
15118   CVT_VAR (u16_f32, N_U16, N_F32 | key, N_VFP, "ftouhs", NULL,     NULL)      \
15119   CVT_VAR (s16_f64, N_S16, N_F64 | key, N_VFP, "ftoshd", NULL,     NULL)      \
15120   CVT_VAR (u16_f64, N_U16, N_F64 | key, N_VFP, "ftouhd", NULL,     NULL)
15121
15122 #define CVT_VAR(C, X, Y, R, BSN, CN, ZN) \
15123   neon_cvt_flavour_##C,
15124
15125 /* The different types of conversions we can do.  */
15126 enum neon_cvt_flavour
15127 {
15128   CVT_FLAVOUR_VAR
15129   neon_cvt_flavour_invalid,
15130   neon_cvt_flavour_first_fp = neon_cvt_flavour_f32_f64
15131 };
15132
15133 #undef CVT_VAR
15134
15135 static enum neon_cvt_flavour
15136 get_neon_cvt_flavour (enum neon_shape rs)
15137 {
15138 #define CVT_VAR(C,X,Y,R,BSN,CN,ZN)                      \
15139   et = neon_check_type (2, rs, (R) | (X), (R) | (Y));   \
15140   if (et.type != NT_invtype)                            \
15141     {                                                   \
15142       inst.error = NULL;                                \
15143       return (neon_cvt_flavour_##C);                    \
15144     }
15145
15146   struct neon_type_el et;
15147   unsigned whole_reg = (rs == NS_FFI || rs == NS_FD || rs == NS_DF
15148                         || rs == NS_FF) ? N_VFP : 0;
15149   /* The instruction versions which take an immediate take one register
15150      argument, which is extended to the width of the full register. Thus the
15151      "source" and "destination" registers must have the same width.  Hack that
15152      here by making the size equal to the key (wider, in this case) operand.  */
15153   unsigned key = (rs == NS_QQI || rs == NS_DDI || rs == NS_FFI) ? N_KEY : 0;
15154
15155   CVT_FLAVOUR_VAR;
15156
15157   return neon_cvt_flavour_invalid;
15158 #undef CVT_VAR
15159 }
15160
15161 enum neon_cvt_mode
15162 {
15163   neon_cvt_mode_a,
15164   neon_cvt_mode_n,
15165   neon_cvt_mode_p,
15166   neon_cvt_mode_m,
15167   neon_cvt_mode_z,
15168   neon_cvt_mode_x,
15169   neon_cvt_mode_r
15170 };
15171
15172 /* Neon-syntax VFP conversions.  */
15173
15174 static void
15175 do_vfp_nsyn_cvt (enum neon_shape rs, enum neon_cvt_flavour flavour)
15176 {
15177   const char *opname = 0;
15178
15179   if (rs == NS_DDI || rs == NS_QQI || rs == NS_FFI)
15180     {
15181       /* Conversions with immediate bitshift.  */
15182       const char *enc[] =
15183         {
15184 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) BSN,
15185           CVT_FLAVOUR_VAR
15186           NULL
15187 #undef CVT_VAR
15188         };
15189
15190       if (flavour < (int) ARRAY_SIZE (enc))
15191         {
15192           opname = enc[flavour];
15193           constraint (inst.operands[0].reg != inst.operands[1].reg,
15194                       _("operands 0 and 1 must be the same register"));
15195           inst.operands[1] = inst.operands[2];
15196           memset (&inst.operands[2], '\0', sizeof (inst.operands[2]));
15197         }
15198     }
15199   else
15200     {
15201       /* Conversions without bitshift.  */
15202       const char *enc[] =
15203         {
15204 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) CN,
15205           CVT_FLAVOUR_VAR
15206           NULL
15207 #undef CVT_VAR
15208         };
15209
15210       if (flavour < (int) ARRAY_SIZE (enc))
15211         opname = enc[flavour];
15212     }
15213
15214   if (opname)
15215     do_vfp_nsyn_opcode (opname);
15216 }
15217
15218 static void
15219 do_vfp_nsyn_cvtz (void)
15220 {
15221   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_NULL);
15222   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15223   const char *enc[] =
15224     {
15225 #define CVT_VAR(C,A,B,R,BSN,CN,ZN) ZN,
15226       CVT_FLAVOUR_VAR
15227       NULL
15228 #undef CVT_VAR
15229     };
15230
15231   if (flavour < (int) ARRAY_SIZE (enc) && enc[flavour])
15232     do_vfp_nsyn_opcode (enc[flavour]);
15233 }
15234
15235 static void
15236 do_vfp_nsyn_cvt_fpv8 (enum neon_cvt_flavour flavour,
15237                       enum neon_cvt_mode mode)
15238 {
15239   int sz, op;
15240   int rm;
15241
15242   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
15243      D register operands.  */
15244   if (flavour == neon_cvt_flavour_s32_f64
15245       || flavour == neon_cvt_flavour_u32_f64)
15246     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15247                 _(BAD_FPU));
15248
15249   set_it_insn_type (OUTSIDE_IT_INSN);
15250
15251   switch (flavour)
15252     {
15253     case neon_cvt_flavour_s32_f64:
15254       sz = 1;
15255       op = 1;
15256       break;
15257     case neon_cvt_flavour_s32_f32:
15258       sz = 0;
15259       op = 1;
15260       break;
15261     case neon_cvt_flavour_u32_f64:
15262       sz = 1;
15263       op = 0;
15264       break;
15265     case neon_cvt_flavour_u32_f32:
15266       sz = 0;
15267       op = 0;
15268       break;
15269     default:
15270       first_error (_("invalid instruction shape"));
15271       return;
15272     }
15273
15274   switch (mode)
15275     {
15276     case neon_cvt_mode_a: rm = 0; break;
15277     case neon_cvt_mode_n: rm = 1; break;
15278     case neon_cvt_mode_p: rm = 2; break;
15279     case neon_cvt_mode_m: rm = 3; break;
15280     default: first_error (_("invalid rounding mode")); return;
15281     }
15282
15283   NEON_ENCODE (FPV8, inst);
15284   encode_arm_vfp_reg (inst.operands[0].reg, VFP_REG_Sd);
15285   encode_arm_vfp_reg (inst.operands[1].reg, sz == 1 ? VFP_REG_Dm : VFP_REG_Sm);
15286   inst.instruction |= sz << 8;
15287   inst.instruction |= op << 7;
15288   inst.instruction |= rm << 16;
15289   inst.instruction |= 0xf0000000;
15290   inst.is_neon = TRUE;
15291 }
15292
15293 static void
15294 do_neon_cvt_1 (enum neon_cvt_mode mode)
15295 {
15296   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_FFI, NS_DD, NS_QQ,
15297     NS_FD, NS_DF, NS_FF, NS_QD, NS_DQ, NS_NULL);
15298   enum neon_cvt_flavour flavour = get_neon_cvt_flavour (rs);
15299
15300   /* PR11109: Handle round-to-zero for VCVT conversions.  */
15301   if (mode == neon_cvt_mode_z
15302       && ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_vfp_v2)
15303       && (flavour == neon_cvt_flavour_s32_f32
15304           || flavour == neon_cvt_flavour_u32_f32
15305           || flavour == neon_cvt_flavour_s32_f64
15306           || flavour == neon_cvt_flavour_u32_f64)
15307       && (rs == NS_FD || rs == NS_FF))
15308     {
15309       do_vfp_nsyn_cvtz ();
15310       return;
15311     }
15312
15313   /* VFP rather than Neon conversions.  */
15314   if (flavour >= neon_cvt_flavour_first_fp)
15315     {
15316       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15317         do_vfp_nsyn_cvt (rs, flavour);
15318       else
15319         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15320
15321       return;
15322     }
15323
15324   switch (rs)
15325     {
15326     case NS_DDI:
15327     case NS_QQI:
15328       {
15329         unsigned immbits;
15330         unsigned enctab[] = { 0x0000100, 0x1000100, 0x0, 0x1000000 };
15331
15332         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15333           return;
15334
15335         /* Fixed-point conversion with #0 immediate is encoded as an
15336            integer conversion.  */
15337         if (inst.operands[2].present && inst.operands[2].imm == 0)
15338           goto int_encode;
15339        immbits = 32 - inst.operands[2].imm;
15340         NEON_ENCODE (IMMED, inst);
15341         if (flavour != neon_cvt_flavour_invalid)
15342           inst.instruction |= enctab[flavour];
15343         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15344         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15345         inst.instruction |= LOW4 (inst.operands[1].reg);
15346         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15347         inst.instruction |= neon_quad (rs) << 6;
15348         inst.instruction |= 1 << 21;
15349         inst.instruction |= immbits << 16;
15350
15351         neon_dp_fixup (&inst);
15352       }
15353       break;
15354
15355     case NS_DD:
15356     case NS_QQ:
15357       if (mode != neon_cvt_mode_x && mode != neon_cvt_mode_z)
15358         {
15359           NEON_ENCODE (FLOAT, inst);
15360           set_it_insn_type (OUTSIDE_IT_INSN);
15361
15362           if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
15363             return;
15364
15365           inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15366           inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15367           inst.instruction |= LOW4 (inst.operands[1].reg);
15368           inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15369           inst.instruction |= neon_quad (rs) << 6;
15370           inst.instruction |= (flavour == neon_cvt_flavour_u32_f32) << 7;
15371           inst.instruction |= mode << 8;
15372           if (thumb_mode)
15373             inst.instruction |= 0xfc000000;
15374           else
15375             inst.instruction |= 0xf0000000;
15376         }
15377       else
15378         {
15379     int_encode:
15380           {
15381             unsigned enctab[] = { 0x100, 0x180, 0x0, 0x080 };
15382
15383             NEON_ENCODE (INTEGER, inst);
15384
15385             if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15386               return;
15387
15388             if (flavour != neon_cvt_flavour_invalid)
15389               inst.instruction |= enctab[flavour];
15390
15391             inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15392             inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15393             inst.instruction |= LOW4 (inst.operands[1].reg);
15394             inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15395             inst.instruction |= neon_quad (rs) << 6;
15396             inst.instruction |= 2 << 18;
15397
15398             neon_dp_fixup (&inst);
15399           }
15400         }
15401       break;
15402
15403     /* Half-precision conversions for Advanced SIMD -- neon.  */
15404     case NS_QD:
15405     case NS_DQ:
15406
15407       if ((rs == NS_DQ)
15408           && (inst.vectype.el[0].size != 16 || inst.vectype.el[1].size != 32))
15409           {
15410             as_bad (_("operand size must match register width"));
15411             break;
15412           }
15413
15414       if ((rs == NS_QD)
15415           && ((inst.vectype.el[0].size != 32 || inst.vectype.el[1].size != 16)))
15416           {
15417             as_bad (_("operand size must match register width"));
15418             break;
15419           }
15420
15421       if (rs == NS_DQ)
15422         inst.instruction = 0x3b60600;
15423       else
15424         inst.instruction = 0x3b60700;
15425
15426       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15427       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15428       inst.instruction |= LOW4 (inst.operands[1].reg);
15429       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15430       neon_dp_fixup (&inst);
15431       break;
15432
15433     default:
15434       /* Some VFP conversions go here (s32 <-> f32, u32 <-> f32).  */
15435       if (mode == neon_cvt_mode_x || mode == neon_cvt_mode_z)
15436         do_vfp_nsyn_cvt (rs, flavour);
15437       else
15438         do_vfp_nsyn_cvt_fpv8 (flavour, mode);
15439     }
15440 }
15441
15442 static void
15443 do_neon_cvtr (void)
15444 {
15445   do_neon_cvt_1 (neon_cvt_mode_x);
15446 }
15447
15448 static void
15449 do_neon_cvt (void)
15450 {
15451   do_neon_cvt_1 (neon_cvt_mode_z);
15452 }
15453
15454 static void
15455 do_neon_cvta (void)
15456 {
15457   do_neon_cvt_1 (neon_cvt_mode_a);
15458 }
15459
15460 static void
15461 do_neon_cvtn (void)
15462 {
15463   do_neon_cvt_1 (neon_cvt_mode_n);
15464 }
15465
15466 static void
15467 do_neon_cvtp (void)
15468 {
15469   do_neon_cvt_1 (neon_cvt_mode_p);
15470 }
15471
15472 static void
15473 do_neon_cvtm (void)
15474 {
15475   do_neon_cvt_1 (neon_cvt_mode_m);
15476 }
15477
15478 static void
15479 do_neon_cvttb_2 (bfd_boolean t, bfd_boolean to, bfd_boolean is_double)
15480 {
15481   if (is_double)
15482     mark_feature_used (&fpu_vfp_ext_armv8);
15483
15484   encode_arm_vfp_reg (inst.operands[0].reg,
15485                       (is_double && !to) ? VFP_REG_Dd : VFP_REG_Sd);
15486   encode_arm_vfp_reg (inst.operands[1].reg,
15487                       (is_double && to) ? VFP_REG_Dm : VFP_REG_Sm);
15488   inst.instruction |= to ? 0x10000 : 0;
15489   inst.instruction |= t ? 0x80 : 0;
15490   inst.instruction |= is_double ? 0x100 : 0;
15491   do_vfp_cond_or_thumb ();
15492 }
15493
15494 static void
15495 do_neon_cvttb_1 (bfd_boolean t)
15496 {
15497   enum neon_shape rs = neon_select_shape (NS_FF, NS_FD, NS_DF, NS_NULL);
15498
15499   if (rs == NS_NULL)
15500     return;
15501   else if (neon_check_type (2, rs, N_F16, N_F32 | N_VFP).type != NT_invtype)
15502     {
15503       inst.error = NULL;
15504       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/FALSE);
15505     }
15506   else if (neon_check_type (2, rs, N_F32 | N_VFP, N_F16).type != NT_invtype)
15507     {
15508       inst.error = NULL;
15509       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/FALSE);
15510     }
15511   else if (neon_check_type (2, rs, N_F16, N_F64 | N_VFP).type != NT_invtype)
15512     {
15513       /* The VCVTB and VCVTT instructions with D-register operands
15514          don't work for SP only targets.  */
15515       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15516                   _(BAD_FPU));
15517
15518       inst.error = NULL;
15519       do_neon_cvttb_2 (t, /*to=*/TRUE, /*is_double=*/TRUE);
15520     }
15521   else if (neon_check_type (2, rs, N_F64 | N_VFP, N_F16).type != NT_invtype)
15522     {
15523       /* The VCVTB and VCVTT instructions with D-register operands
15524          don't work for SP only targets.  */
15525       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
15526                   _(BAD_FPU));
15527
15528       inst.error = NULL;
15529       do_neon_cvttb_2 (t, /*to=*/FALSE, /*is_double=*/TRUE);
15530     }
15531   else
15532     return;
15533 }
15534
15535 static void
15536 do_neon_cvtb (void)
15537 {
15538   do_neon_cvttb_1 (FALSE);
15539 }
15540
15541
15542 static void
15543 do_neon_cvtt (void)
15544 {
15545   do_neon_cvttb_1 (TRUE);
15546 }
15547
15548 static void
15549 neon_move_immediate (void)
15550 {
15551   enum neon_shape rs = neon_select_shape (NS_DI, NS_QI, NS_NULL);
15552   struct neon_type_el et = neon_check_type (2, rs,
15553     N_I8 | N_I16 | N_I32 | N_I64 | N_F32 | N_KEY, N_EQK);
15554   unsigned immlo, immhi = 0, immbits;
15555   int op, cmode, float_p;
15556
15557   constraint (et.type == NT_invtype,
15558               _("operand size must be specified for immediate VMOV"));
15559
15560   /* We start out as an MVN instruction if OP = 1, MOV otherwise.  */
15561   op = (inst.instruction & (1 << 5)) != 0;
15562
15563   immlo = inst.operands[1].imm;
15564   if (inst.operands[1].regisimm)
15565     immhi = inst.operands[1].reg;
15566
15567   constraint (et.size < 32 && (immlo & ~((1 << et.size) - 1)) != 0,
15568               _("immediate has bits set outside the operand size"));
15569
15570   float_p = inst.operands[1].immisfloat;
15571
15572   if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits, &op,
15573                                         et.size, et.type)) == FAIL)
15574     {
15575       /* Invert relevant bits only.  */
15576       neon_invert_size (&immlo, &immhi, et.size);
15577       /* Flip from VMOV/VMVN to VMVN/VMOV. Some immediate types are unavailable
15578          with one or the other; those cases are caught by
15579          neon_cmode_for_move_imm.  */
15580       op = !op;
15581       if ((cmode = neon_cmode_for_move_imm (immlo, immhi, float_p, &immbits,
15582                                             &op, et.size, et.type)) == FAIL)
15583         {
15584           first_error (_("immediate out of range"));
15585           return;
15586         }
15587     }
15588
15589   inst.instruction &= ~(1 << 5);
15590   inst.instruction |= op << 5;
15591
15592   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15593   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15594   inst.instruction |= neon_quad (rs) << 6;
15595   inst.instruction |= cmode << 8;
15596
15597   neon_write_immbits (immbits);
15598 }
15599
15600 static void
15601 do_neon_mvn (void)
15602 {
15603   if (inst.operands[1].isreg)
15604     {
15605       enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15606
15607       NEON_ENCODE (INTEGER, inst);
15608       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15609       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15610       inst.instruction |= LOW4 (inst.operands[1].reg);
15611       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15612       inst.instruction |= neon_quad (rs) << 6;
15613     }
15614   else
15615     {
15616       NEON_ENCODE (IMMED, inst);
15617       neon_move_immediate ();
15618     }
15619
15620   neon_dp_fixup (&inst);
15621 }
15622
15623 /* Encode instructions of form:
15624
15625   |28/24|23|22|21 20|19 16|15 12|11    8|7|6|5|4|3  0|
15626   |  U  |x |D |size | Rn  | Rd  |x x x x|N|x|M|x| Rm |  */
15627
15628 static void
15629 neon_mixed_length (struct neon_type_el et, unsigned size)
15630 {
15631   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15632   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15633   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15634   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15635   inst.instruction |= LOW4 (inst.operands[2].reg);
15636   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15637   inst.instruction |= (et.type == NT_unsigned) << 24;
15638   inst.instruction |= neon_logbits (size) << 20;
15639
15640   neon_dp_fixup (&inst);
15641 }
15642
15643 static void
15644 do_neon_dyadic_long (void)
15645 {
15646   /* FIXME: Type checking for lengthening op.  */
15647   struct neon_type_el et = neon_check_type (3, NS_QDD,
15648     N_EQK | N_DBL, N_EQK, N_SU_32 | N_KEY);
15649   neon_mixed_length (et, et.size);
15650 }
15651
15652 static void
15653 do_neon_abal (void)
15654 {
15655   struct neon_type_el et = neon_check_type (3, NS_QDD,
15656     N_EQK | N_INT | N_DBL, N_EQK, N_SU_32 | N_KEY);
15657   neon_mixed_length (et, et.size);
15658 }
15659
15660 static void
15661 neon_mac_reg_scalar_long (unsigned regtypes, unsigned scalartypes)
15662 {
15663   if (inst.operands[2].isscalar)
15664     {
15665       struct neon_type_el et = neon_check_type (3, NS_QDS,
15666         N_EQK | N_DBL, N_EQK, regtypes | N_KEY);
15667       NEON_ENCODE (SCALAR, inst);
15668       neon_mul_mac (et, et.type == NT_unsigned);
15669     }
15670   else
15671     {
15672       struct neon_type_el et = neon_check_type (3, NS_QDD,
15673         N_EQK | N_DBL, N_EQK, scalartypes | N_KEY);
15674       NEON_ENCODE (INTEGER, inst);
15675       neon_mixed_length (et, et.size);
15676     }
15677 }
15678
15679 static void
15680 do_neon_mac_maybe_scalar_long (void)
15681 {
15682   neon_mac_reg_scalar_long (N_S16 | N_S32 | N_U16 | N_U32, N_SU_32);
15683 }
15684
15685 static void
15686 do_neon_dyadic_wide (void)
15687 {
15688   struct neon_type_el et = neon_check_type (3, NS_QQD,
15689     N_EQK | N_DBL, N_EQK | N_DBL, N_SU_32 | N_KEY);
15690   neon_mixed_length (et, et.size);
15691 }
15692
15693 static void
15694 do_neon_dyadic_narrow (void)
15695 {
15696   struct neon_type_el et = neon_check_type (3, NS_QDD,
15697     N_EQK | N_DBL, N_EQK, N_I16 | N_I32 | N_I64 | N_KEY);
15698   /* Operand sign is unimportant, and the U bit is part of the opcode,
15699      so force the operand type to integer.  */
15700   et.type = NT_integer;
15701   neon_mixed_length (et, et.size / 2);
15702 }
15703
15704 static void
15705 do_neon_mul_sat_scalar_long (void)
15706 {
15707   neon_mac_reg_scalar_long (N_S16 | N_S32, N_S16 | N_S32);
15708 }
15709
15710 static void
15711 do_neon_vmull (void)
15712 {
15713   if (inst.operands[2].isscalar)
15714     do_neon_mac_maybe_scalar_long ();
15715   else
15716     {
15717       struct neon_type_el et = neon_check_type (3, NS_QDD,
15718         N_EQK | N_DBL, N_EQK, N_SU_32 | N_P8 | N_P64 | N_KEY);
15719
15720       if (et.type == NT_poly)
15721         NEON_ENCODE (POLY, inst);
15722       else
15723         NEON_ENCODE (INTEGER, inst);
15724
15725       /* For polynomial encoding the U bit must be zero, and the size must
15726          be 8 (encoded as 0b00) or, on ARMv8 or later 64 (encoded, non
15727          obviously, as 0b10).  */
15728       if (et.size == 64)
15729         {
15730           /* Check we're on the correct architecture.  */
15731           if (!mark_feature_used (&fpu_crypto_ext_armv8))
15732             inst.error =
15733               _("Instruction form not available on this architecture.");
15734
15735           et.size = 32;
15736         }
15737
15738       neon_mixed_length (et, et.size);
15739     }
15740 }
15741
15742 static void
15743 do_neon_ext (void)
15744 {
15745   enum neon_shape rs = neon_select_shape (NS_DDDI, NS_QQQI, NS_NULL);
15746   struct neon_type_el et = neon_check_type (3, rs,
15747     N_EQK, N_EQK, N_8 | N_16 | N_32 | N_64 | N_KEY);
15748   unsigned imm = (inst.operands[3].imm * et.size) / 8;
15749
15750   constraint (imm >= (unsigned) (neon_quad (rs) ? 16 : 8),
15751               _("shift out of range"));
15752   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15753   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15754   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15755   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15756   inst.instruction |= LOW4 (inst.operands[2].reg);
15757   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
15758   inst.instruction |= neon_quad (rs) << 6;
15759   inst.instruction |= imm << 8;
15760
15761   neon_dp_fixup (&inst);
15762 }
15763
15764 static void
15765 do_neon_rev (void)
15766 {
15767   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
15768   struct neon_type_el et = neon_check_type (2, rs,
15769     N_EQK, N_8 | N_16 | N_32 | N_KEY);
15770   unsigned op = (inst.instruction >> 7) & 3;
15771   /* N (width of reversed regions) is encoded as part of the bitmask. We
15772      extract it here to check the elements to be reversed are smaller.
15773      Otherwise we'd get a reserved instruction.  */
15774   unsigned elsize = (op == 2) ? 16 : (op == 1) ? 32 : (op == 0) ? 64 : 0;
15775   gas_assert (elsize != 0);
15776   constraint (et.size >= elsize,
15777               _("elements must be smaller than reversal region"));
15778   neon_two_same (neon_quad (rs), 1, et.size);
15779 }
15780
15781 static void
15782 do_neon_dup (void)
15783 {
15784   if (inst.operands[1].isscalar)
15785     {
15786       enum neon_shape rs = neon_select_shape (NS_DS, NS_QS, NS_NULL);
15787       struct neon_type_el et = neon_check_type (2, rs,
15788         N_EQK, N_8 | N_16 | N_32 | N_KEY);
15789       unsigned sizebits = et.size >> 3;
15790       unsigned dm = NEON_SCALAR_REG (inst.operands[1].reg);
15791       int logsize = neon_logbits (et.size);
15792       unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg) << logsize;
15793
15794       if (vfp_or_neon_is_neon (NEON_CHECK_CC) == FAIL)
15795         return;
15796
15797       NEON_ENCODE (SCALAR, inst);
15798       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15799       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15800       inst.instruction |= LOW4 (dm);
15801       inst.instruction |= HI1 (dm) << 5;
15802       inst.instruction |= neon_quad (rs) << 6;
15803       inst.instruction |= x << 17;
15804       inst.instruction |= sizebits << 16;
15805
15806       neon_dp_fixup (&inst);
15807     }
15808   else
15809     {
15810       enum neon_shape rs = neon_select_shape (NS_DR, NS_QR, NS_NULL);
15811       struct neon_type_el et = neon_check_type (2, rs,
15812         N_8 | N_16 | N_32 | N_KEY, N_EQK);
15813       /* Duplicate ARM register to lanes of vector.  */
15814       NEON_ENCODE (ARMREG, inst);
15815       switch (et.size)
15816         {
15817         case 8:  inst.instruction |= 0x400000; break;
15818         case 16: inst.instruction |= 0x000020; break;
15819         case 32: inst.instruction |= 0x000000; break;
15820         default: break;
15821         }
15822       inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
15823       inst.instruction |= LOW4 (inst.operands[0].reg) << 16;
15824       inst.instruction |= HI1 (inst.operands[0].reg) << 7;
15825       inst.instruction |= neon_quad (rs) << 21;
15826       /* The encoding for this instruction is identical for the ARM and Thumb
15827          variants, except for the condition field.  */
15828       do_vfp_cond_or_thumb ();
15829     }
15830 }
15831
15832 /* VMOV has particularly many variations. It can be one of:
15833      0. VMOV<c><q> <Qd>, <Qm>
15834      1. VMOV<c><q> <Dd>, <Dm>
15835    (Register operations, which are VORR with Rm = Rn.)
15836      2. VMOV<c><q>.<dt> <Qd>, #<imm>
15837      3. VMOV<c><q>.<dt> <Dd>, #<imm>
15838    (Immediate loads.)
15839      4. VMOV<c><q>.<size> <Dn[x]>, <Rd>
15840    (ARM register to scalar.)
15841      5. VMOV<c><q> <Dm>, <Rd>, <Rn>
15842    (Two ARM registers to vector.)
15843      6. VMOV<c><q>.<dt> <Rd>, <Dn[x]>
15844    (Scalar to ARM register.)
15845      7. VMOV<c><q> <Rd>, <Rn>, <Dm>
15846    (Vector to two ARM registers.)
15847      8. VMOV.F32 <Sd>, <Sm>
15848      9. VMOV.F64 <Dd>, <Dm>
15849    (VFP register moves.)
15850     10. VMOV.F32 <Sd>, #imm
15851     11. VMOV.F64 <Dd>, #imm
15852    (VFP float immediate load.)
15853     12. VMOV <Rd>, <Sm>
15854    (VFP single to ARM reg.)
15855     13. VMOV <Sd>, <Rm>
15856    (ARM reg to VFP single.)
15857     14. VMOV <Rd>, <Re>, <Sn>, <Sm>
15858    (Two ARM regs to two VFP singles.)
15859     15. VMOV <Sd>, <Se>, <Rn>, <Rm>
15860    (Two VFP singles to two ARM regs.)
15861
15862    These cases can be disambiguated using neon_select_shape, except cases 1/9
15863    and 3/11 which depend on the operand type too.
15864
15865    All the encoded bits are hardcoded by this function.
15866
15867    Cases 4, 6 may be used with VFPv1 and above (only 32-bit transfers!).
15868    Cases 5, 7 may be used with VFPv2 and above.
15869
15870    FIXME: Some of the checking may be a bit sloppy (in a couple of cases you
15871    can specify a type where it doesn't make sense to, and is ignored).  */
15872
15873 static void
15874 do_neon_mov (void)
15875 {
15876   enum neon_shape rs = neon_select_shape (NS_RRFF, NS_FFRR, NS_DRR, NS_RRD,
15877     NS_QQ, NS_DD, NS_QI, NS_DI, NS_SR, NS_RS, NS_FF, NS_FI, NS_RF, NS_FR,
15878     NS_NULL);
15879   struct neon_type_el et;
15880   const char *ldconst = 0;
15881
15882   switch (rs)
15883     {
15884     case NS_DD:  /* case 1/9.  */
15885       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15886       /* It is not an error here if no type is given.  */
15887       inst.error = NULL;
15888       if (et.type == NT_float && et.size == 64)
15889         {
15890           do_vfp_nsyn_opcode ("fcpyd");
15891           break;
15892         }
15893       /* fall through.  */
15894
15895     case NS_QQ:  /* case 0/1.  */
15896       {
15897         if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15898           return;
15899         /* The architecture manual I have doesn't explicitly state which
15900            value the U bit should have for register->register moves, but
15901            the equivalent VORR instruction has U = 0, so do that.  */
15902         inst.instruction = 0x0200110;
15903         inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
15904         inst.instruction |= HI1 (inst.operands[0].reg) << 22;
15905         inst.instruction |= LOW4 (inst.operands[1].reg);
15906         inst.instruction |= HI1 (inst.operands[1].reg) << 5;
15907         inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
15908         inst.instruction |= HI1 (inst.operands[1].reg) << 7;
15909         inst.instruction |= neon_quad (rs) << 6;
15910
15911         neon_dp_fixup (&inst);
15912       }
15913       break;
15914
15915     case NS_DI:  /* case 3/11.  */
15916       et = neon_check_type (2, rs, N_EQK, N_F64 | N_KEY);
15917       inst.error = NULL;
15918       if (et.type == NT_float && et.size == 64)
15919         {
15920           /* case 11 (fconstd).  */
15921           ldconst = "fconstd";
15922           goto encode_fconstd;
15923         }
15924       /* fall through.  */
15925
15926     case NS_QI:  /* case 2/3.  */
15927       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH) == FAIL)
15928         return;
15929       inst.instruction = 0x0800010;
15930       neon_move_immediate ();
15931       neon_dp_fixup (&inst);
15932       break;
15933
15934     case NS_SR:  /* case 4.  */
15935       {
15936         unsigned bcdebits = 0;
15937         int logsize;
15938         unsigned dn = NEON_SCALAR_REG (inst.operands[0].reg);
15939         unsigned x = NEON_SCALAR_INDEX (inst.operands[0].reg);
15940
15941         /* .<size> is optional here, defaulting to .32. */
15942         if (inst.vectype.elems == 0
15943             && inst.operands[0].vectype.type == NT_invtype
15944             && inst.operands[1].vectype.type == NT_invtype)
15945           {
15946             inst.vectype.el[0].type = NT_untyped;
15947             inst.vectype.el[0].size = 32;
15948             inst.vectype.elems = 1;
15949           }
15950
15951         et = neon_check_type (2, NS_NULL, N_8 | N_16 | N_32 | N_KEY, N_EQK);
15952         logsize = neon_logbits (et.size);
15953
15954         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
15955                     _(BAD_FPU));
15956         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
15957                     && et.size != 32, _(BAD_FPU));
15958         constraint (et.type == NT_invtype, _("bad type for scalar"));
15959         constraint (x >= 64 / et.size, _("scalar index out of range"));
15960
15961         switch (et.size)
15962           {
15963           case 8:  bcdebits = 0x8; break;
15964           case 16: bcdebits = 0x1; break;
15965           case 32: bcdebits = 0x0; break;
15966           default: ;
15967           }
15968
15969         bcdebits |= x << logsize;
15970
15971         inst.instruction = 0xe000b10;
15972         do_vfp_cond_or_thumb ();
15973         inst.instruction |= LOW4 (dn) << 16;
15974         inst.instruction |= HI1 (dn) << 7;
15975         inst.instruction |= inst.operands[1].reg << 12;
15976         inst.instruction |= (bcdebits & 3) << 5;
15977         inst.instruction |= (bcdebits >> 2) << 21;
15978       }
15979       break;
15980
15981     case NS_DRR:  /* case 5 (fmdrr).  */
15982       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
15983                   _(BAD_FPU));
15984
15985       inst.instruction = 0xc400b10;
15986       do_vfp_cond_or_thumb ();
15987       inst.instruction |= LOW4 (inst.operands[0].reg);
15988       inst.instruction |= HI1 (inst.operands[0].reg) << 5;
15989       inst.instruction |= inst.operands[1].reg << 12;
15990       inst.instruction |= inst.operands[2].reg << 16;
15991       break;
15992
15993     case NS_RS:  /* case 6.  */
15994       {
15995         unsigned logsize;
15996         unsigned dn = NEON_SCALAR_REG (inst.operands[1].reg);
15997         unsigned x = NEON_SCALAR_INDEX (inst.operands[1].reg);
15998         unsigned abcdebits = 0;
15999
16000         /* .<dt> is optional here, defaulting to .32. */
16001         if (inst.vectype.elems == 0
16002             && inst.operands[0].vectype.type == NT_invtype
16003             && inst.operands[1].vectype.type == NT_invtype)
16004           {
16005             inst.vectype.el[0].type = NT_untyped;
16006             inst.vectype.el[0].size = 32;
16007             inst.vectype.elems = 1;
16008           }
16009
16010         et = neon_check_type (2, NS_NULL,
16011                               N_EQK, N_S8 | N_S16 | N_U8 | N_U16 | N_32 | N_KEY);
16012         logsize = neon_logbits (et.size);
16013
16014         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v1),
16015                     _(BAD_FPU));
16016         constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_neon_ext_v1)
16017                     && et.size != 32, _(BAD_FPU));
16018         constraint (et.type == NT_invtype, _("bad type for scalar"));
16019         constraint (x >= 64 / et.size, _("scalar index out of range"));
16020
16021         switch (et.size)
16022           {
16023           case 8:  abcdebits = (et.type == NT_signed) ? 0x08 : 0x18; break;
16024           case 16: abcdebits = (et.type == NT_signed) ? 0x01 : 0x11; break;
16025           case 32: abcdebits = 0x00; break;
16026           default: ;
16027           }
16028
16029         abcdebits |= x << logsize;
16030         inst.instruction = 0xe100b10;
16031         do_vfp_cond_or_thumb ();
16032         inst.instruction |= LOW4 (dn) << 16;
16033         inst.instruction |= HI1 (dn) << 7;
16034         inst.instruction |= inst.operands[0].reg << 12;
16035         inst.instruction |= (abcdebits & 3) << 5;
16036         inst.instruction |= (abcdebits >> 2) << 21;
16037       }
16038       break;
16039
16040     case NS_RRD:  /* case 7 (fmrrd).  */
16041       constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_v2),
16042                   _(BAD_FPU));
16043
16044       inst.instruction = 0xc500b10;
16045       do_vfp_cond_or_thumb ();
16046       inst.instruction |= inst.operands[0].reg << 12;
16047       inst.instruction |= inst.operands[1].reg << 16;
16048       inst.instruction |= LOW4 (inst.operands[2].reg);
16049       inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16050       break;
16051
16052     case NS_FF:  /* case 8 (fcpys).  */
16053       do_vfp_nsyn_opcode ("fcpys");
16054       break;
16055
16056     case NS_FI:  /* case 10 (fconsts).  */
16057       ldconst = "fconsts";
16058       encode_fconstd:
16059       if (is_quarter_float (inst.operands[1].imm))
16060         {
16061           inst.operands[1].imm = neon_qfloat_bits (inst.operands[1].imm);
16062           do_vfp_nsyn_opcode (ldconst);
16063         }
16064       else
16065         first_error (_("immediate out of range"));
16066       break;
16067
16068     case NS_RF:  /* case 12 (fmrs).  */
16069       do_vfp_nsyn_opcode ("fmrs");
16070       break;
16071
16072     case NS_FR:  /* case 13 (fmsr).  */
16073       do_vfp_nsyn_opcode ("fmsr");
16074       break;
16075
16076     /* The encoders for the fmrrs and fmsrr instructions expect three operands
16077        (one of which is a list), but we have parsed four.  Do some fiddling to
16078        make the operands what do_vfp_reg2_from_sp2 and do_vfp_sp2_from_reg2
16079        expect.  */
16080     case NS_RRFF:  /* case 14 (fmrrs).  */
16081       constraint (inst.operands[3].reg != inst.operands[2].reg + 1,
16082                   _("VFP registers must be adjacent"));
16083       inst.operands[2].imm = 2;
16084       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16085       do_vfp_nsyn_opcode ("fmrrs");
16086       break;
16087
16088     case NS_FFRR:  /* case 15 (fmsrr).  */
16089       constraint (inst.operands[1].reg != inst.operands[0].reg + 1,
16090                   _("VFP registers must be adjacent"));
16091       inst.operands[1] = inst.operands[2];
16092       inst.operands[2] = inst.operands[3];
16093       inst.operands[0].imm = 2;
16094       memset (&inst.operands[3], '\0', sizeof (inst.operands[3]));
16095       do_vfp_nsyn_opcode ("fmsrr");
16096       break;
16097
16098     case NS_NULL:
16099       /* neon_select_shape has determined that the instruction
16100          shape is wrong and has already set the error message.  */
16101       break;
16102
16103     default:
16104       abort ();
16105     }
16106 }
16107
16108 static void
16109 do_neon_rshift_round_imm (void)
16110 {
16111   enum neon_shape rs = neon_select_shape (NS_DDI, NS_QQI, NS_NULL);
16112   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_ALL | N_KEY);
16113   int imm = inst.operands[2].imm;
16114
16115   /* imm == 0 case is encoded as VMOV for V{R}SHR.  */
16116   if (imm == 0)
16117     {
16118       inst.operands[2].present = 0;
16119       do_neon_mov ();
16120       return;
16121     }
16122
16123   constraint (imm < 1 || (unsigned)imm > et.size,
16124               _("immediate out of range for shift"));
16125   neon_imm_shift (TRUE, et.type == NT_unsigned, neon_quad (rs), et,
16126                   et.size - imm);
16127 }
16128
16129 static void
16130 do_neon_movl (void)
16131 {
16132   struct neon_type_el et = neon_check_type (2, NS_QD,
16133     N_EQK | N_DBL, N_SU_32 | N_KEY);
16134   unsigned sizebits = et.size >> 3;
16135   inst.instruction |= sizebits << 19;
16136   neon_two_same (0, et.type == NT_unsigned, -1);
16137 }
16138
16139 static void
16140 do_neon_trn (void)
16141 {
16142   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16143   struct neon_type_el et = neon_check_type (2, rs,
16144     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16145   NEON_ENCODE (INTEGER, inst);
16146   neon_two_same (neon_quad (rs), 1, et.size);
16147 }
16148
16149 static void
16150 do_neon_zip_uzp (void)
16151 {
16152   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16153   struct neon_type_el et = neon_check_type (2, rs,
16154     N_EQK, N_8 | N_16 | N_32 | N_KEY);
16155   if (rs == NS_DD && et.size == 32)
16156     {
16157       /* Special case: encode as VTRN.32 <Dd>, <Dm>.  */
16158       inst.instruction = N_MNEM_vtrn;
16159       do_neon_trn ();
16160       return;
16161     }
16162   neon_two_same (neon_quad (rs), 1, et.size);
16163 }
16164
16165 static void
16166 do_neon_sat_abs_neg (void)
16167 {
16168   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16169   struct neon_type_el et = neon_check_type (2, rs,
16170     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16171   neon_two_same (neon_quad (rs), 1, et.size);
16172 }
16173
16174 static void
16175 do_neon_pair_long (void)
16176 {
16177   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16178   struct neon_type_el et = neon_check_type (2, rs, N_EQK, N_SU_32 | N_KEY);
16179   /* Unsigned is encoded in OP field (bit 7) for these instruction.  */
16180   inst.instruction |= (et.type == NT_unsigned) << 7;
16181   neon_two_same (neon_quad (rs), 1, et.size);
16182 }
16183
16184 static void
16185 do_neon_recip_est (void)
16186 {
16187   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16188   struct neon_type_el et = neon_check_type (2, rs,
16189     N_EQK | N_FLT, N_F32 | N_U32 | N_KEY);
16190   inst.instruction |= (et.type == NT_float) << 8;
16191   neon_two_same (neon_quad (rs), 1, et.size);
16192 }
16193
16194 static void
16195 do_neon_cls (void)
16196 {
16197   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16198   struct neon_type_el et = neon_check_type (2, rs,
16199     N_EQK, N_S8 | N_S16 | N_S32 | N_KEY);
16200   neon_two_same (neon_quad (rs), 1, et.size);
16201 }
16202
16203 static void
16204 do_neon_clz (void)
16205 {
16206   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16207   struct neon_type_el et = neon_check_type (2, rs,
16208     N_EQK, N_I8 | N_I16 | N_I32 | N_KEY);
16209   neon_two_same (neon_quad (rs), 1, et.size);
16210 }
16211
16212 static void
16213 do_neon_cnt (void)
16214 {
16215   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16216   struct neon_type_el et = neon_check_type (2, rs,
16217     N_EQK | N_INT, N_8 | N_KEY);
16218   neon_two_same (neon_quad (rs), 1, et.size);
16219 }
16220
16221 static void
16222 do_neon_swp (void)
16223 {
16224   enum neon_shape rs = neon_select_shape (NS_DD, NS_QQ, NS_NULL);
16225   neon_two_same (neon_quad (rs), 1, -1);
16226 }
16227
16228 static void
16229 do_neon_tbl_tbx (void)
16230 {
16231   unsigned listlenbits;
16232   neon_check_type (3, NS_DLD, N_EQK, N_EQK, N_8 | N_KEY);
16233
16234   if (inst.operands[1].imm < 1 || inst.operands[1].imm > 4)
16235     {
16236       first_error (_("bad list length for table lookup"));
16237       return;
16238     }
16239
16240   listlenbits = inst.operands[1].imm - 1;
16241   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16242   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16243   inst.instruction |= LOW4 (inst.operands[1].reg) << 16;
16244   inst.instruction |= HI1 (inst.operands[1].reg) << 7;
16245   inst.instruction |= LOW4 (inst.operands[2].reg);
16246   inst.instruction |= HI1 (inst.operands[2].reg) << 5;
16247   inst.instruction |= listlenbits << 8;
16248
16249   neon_dp_fixup (&inst);
16250 }
16251
16252 static void
16253 do_neon_ldm_stm (void)
16254 {
16255   /* P, U and L bits are part of bitmask.  */
16256   int is_dbmode = (inst.instruction & (1 << 24)) != 0;
16257   unsigned offsetbits = inst.operands[1].imm * 2;
16258
16259   if (inst.operands[1].issingle)
16260     {
16261       do_vfp_nsyn_ldm_stm (is_dbmode);
16262       return;
16263     }
16264
16265   constraint (is_dbmode && !inst.operands[0].writeback,
16266               _("writeback (!) must be used for VLDMDB and VSTMDB"));
16267
16268   constraint (inst.operands[1].imm < 1 || inst.operands[1].imm > 16,
16269               _("register list must contain at least 1 and at most 16 "
16270                 "registers"));
16271
16272   inst.instruction |= inst.operands[0].reg << 16;
16273   inst.instruction |= inst.operands[0].writeback << 21;
16274   inst.instruction |= LOW4 (inst.operands[1].reg) << 12;
16275   inst.instruction |= HI1 (inst.operands[1].reg) << 22;
16276
16277   inst.instruction |= offsetbits;
16278
16279   do_vfp_cond_or_thumb ();
16280 }
16281
16282 static void
16283 do_neon_ldr_str (void)
16284 {
16285   int is_ldr = (inst.instruction & (1 << 20)) != 0;
16286
16287   /* Use of PC in vstr in ARM mode is deprecated in ARMv7.
16288      And is UNPREDICTABLE in thumb mode.  */
16289   if (!is_ldr
16290       && inst.operands[1].reg == REG_PC
16291       && (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v7) || thumb_mode))
16292     {
16293       if (thumb_mode)
16294         inst.error = _("Use of PC here is UNPREDICTABLE");
16295       else if (warn_on_deprecated)
16296         as_tsktsk (_("Use of PC here is deprecated"));
16297     }
16298
16299   if (inst.operands[0].issingle)
16300     {
16301       if (is_ldr)
16302         do_vfp_nsyn_opcode ("flds");
16303       else
16304         do_vfp_nsyn_opcode ("fsts");
16305     }
16306   else
16307     {
16308       if (is_ldr)
16309         do_vfp_nsyn_opcode ("fldd");
16310       else
16311         do_vfp_nsyn_opcode ("fstd");
16312     }
16313 }
16314
16315 /* "interleave" version also handles non-interleaving register VLD1/VST1
16316    instructions.  */
16317
16318 static void
16319 do_neon_ld_st_interleave (void)
16320 {
16321   struct neon_type_el et = neon_check_type (1, NS_NULL,
16322                                             N_8 | N_16 | N_32 | N_64);
16323   unsigned alignbits = 0;
16324   unsigned idx;
16325   /* The bits in this table go:
16326      0: register stride of one (0) or two (1)
16327      1,2: register list length, minus one (1, 2, 3, 4).
16328      3,4: <n> in instruction type, minus one (VLD<n> / VST<n>).
16329      We use -1 for invalid entries.  */
16330   const int typetable[] =
16331     {
16332       0x7,  -1, 0xa,  -1, 0x6,  -1, 0x2,  -1, /* VLD1 / VST1.  */
16333        -1,  -1, 0x8, 0x9,  -1,  -1, 0x3,  -1, /* VLD2 / VST2.  */
16334        -1,  -1,  -1,  -1, 0x4, 0x5,  -1,  -1, /* VLD3 / VST3.  */
16335        -1,  -1,  -1,  -1,  -1,  -1, 0x0, 0x1  /* VLD4 / VST4.  */
16336     };
16337   int typebits;
16338
16339   if (et.type == NT_invtype)
16340     return;
16341
16342   if (inst.operands[1].immisalign)
16343     switch (inst.operands[1].imm >> 8)
16344       {
16345       case 64: alignbits = 1; break;
16346       case 128:
16347         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2
16348             && NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16349           goto bad_alignment;
16350         alignbits = 2;
16351         break;
16352       case 256:
16353         if (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4)
16354           goto bad_alignment;
16355         alignbits = 3;
16356         break;
16357       default:
16358       bad_alignment:
16359         first_error (_("bad alignment"));
16360         return;
16361       }
16362
16363   inst.instruction |= alignbits << 4;
16364   inst.instruction |= neon_logbits (et.size) << 6;
16365
16366   /* Bits [4:6] of the immediate in a list specifier encode register stride
16367      (minus 1) in bit 4, and list length in bits [5:6]. We put the <n> of
16368      VLD<n>/VST<n> in bits [9:8] of the initial bitmask. Suck it out here, look
16369      up the right value for "type" in a table based on this value and the given
16370      list style, then stick it back.  */
16371   idx = ((inst.operands[0].imm >> 4) & 7)
16372         | (((inst.instruction >> 8) & 3) << 3);
16373
16374   typebits = typetable[idx];
16375
16376   constraint (typebits == -1, _("bad list type for instruction"));
16377   constraint (((inst.instruction >> 8) & 3) && et.size == 64,
16378               _("bad element type for instruction"));
16379
16380   inst.instruction &= ~0xf00;
16381   inst.instruction |= typebits << 8;
16382 }
16383
16384 /* Check alignment is valid for do_neon_ld_st_lane and do_neon_ld_dup.
16385    *DO_ALIGN is set to 1 if the relevant alignment bit should be set, 0
16386    otherwise. The variable arguments are a list of pairs of legal (size, align)
16387    values, terminated with -1.  */
16388
16389 static int
16390 neon_alignment_bit (int size, int align, int *do_align, ...)
16391 {
16392   va_list ap;
16393   int result = FAIL, thissize, thisalign;
16394
16395   if (!inst.operands[1].immisalign)
16396     {
16397       *do_align = 0;
16398       return SUCCESS;
16399     }
16400
16401   va_start (ap, do_align);
16402
16403   do
16404     {
16405       thissize = va_arg (ap, int);
16406       if (thissize == -1)
16407         break;
16408       thisalign = va_arg (ap, int);
16409
16410       if (size == thissize && align == thisalign)
16411         result = SUCCESS;
16412     }
16413   while (result != SUCCESS);
16414
16415   va_end (ap);
16416
16417   if (result == SUCCESS)
16418     *do_align = 1;
16419   else
16420     first_error (_("unsupported alignment for instruction"));
16421
16422   return result;
16423 }
16424
16425 static void
16426 do_neon_ld_st_lane (void)
16427 {
16428   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16429   int align_good, do_align = 0;
16430   int logsize = neon_logbits (et.size);
16431   int align = inst.operands[1].imm >> 8;
16432   int n = (inst.instruction >> 8) & 3;
16433   int max_el = 64 / et.size;
16434
16435   if (et.type == NT_invtype)
16436     return;
16437
16438   constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != n + 1,
16439               _("bad list length"));
16440   constraint (NEON_LANE (inst.operands[0].imm) >= max_el,
16441               _("scalar index out of range"));
16442   constraint (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2
16443               && et.size == 8,
16444               _("stride of 2 unavailable when element size is 8"));
16445
16446   switch (n)
16447     {
16448     case 0:  /* VLD1 / VST1.  */
16449       align_good = neon_alignment_bit (et.size, align, &do_align, 16, 16,
16450                                        32, 32, -1);
16451       if (align_good == FAIL)
16452         return;
16453       if (do_align)
16454         {
16455           unsigned alignbits = 0;
16456           switch (et.size)
16457             {
16458             case 16: alignbits = 0x1; break;
16459             case 32: alignbits = 0x3; break;
16460             default: ;
16461             }
16462           inst.instruction |= alignbits << 4;
16463         }
16464       break;
16465
16466     case 1:  /* VLD2 / VST2.  */
16467       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 16, 16, 32,
16468                                        32, 64, -1);
16469       if (align_good == FAIL)
16470         return;
16471       if (do_align)
16472         inst.instruction |= 1 << 4;
16473       break;
16474
16475     case 2:  /* VLD3 / VST3.  */
16476       constraint (inst.operands[1].immisalign,
16477                   _("can't use alignment with this instruction"));
16478       break;
16479
16480     case 3:  /* VLD4 / VST4.  */
16481       align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
16482                                        16, 64, 32, 64, 32, 128, -1);
16483       if (align_good == FAIL)
16484         return;
16485       if (do_align)
16486         {
16487           unsigned alignbits = 0;
16488           switch (et.size)
16489             {
16490             case 8:  alignbits = 0x1; break;
16491             case 16: alignbits = 0x1; break;
16492             case 32: alignbits = (align == 64) ? 0x1 : 0x2; break;
16493             default: ;
16494             }
16495           inst.instruction |= alignbits << 4;
16496         }
16497       break;
16498
16499     default: ;
16500     }
16501
16502   /* Reg stride of 2 is encoded in bit 5 when size==16, bit 6 when size==32.  */
16503   if (n != 0 && NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16504     inst.instruction |= 1 << (4 + logsize);
16505
16506   inst.instruction |= NEON_LANE (inst.operands[0].imm) << (logsize + 5);
16507   inst.instruction |= logsize << 10;
16508 }
16509
16510 /* Encode single n-element structure to all lanes VLD<n> instructions.  */
16511
16512 static void
16513 do_neon_ld_dup (void)
16514 {
16515   struct neon_type_el et = neon_check_type (1, NS_NULL, N_8 | N_16 | N_32);
16516   int align_good, do_align = 0;
16517
16518   if (et.type == NT_invtype)
16519     return;
16520
16521   switch ((inst.instruction >> 8) & 3)
16522     {
16523     case 0:  /* VLD1.  */
16524       gas_assert (NEON_REG_STRIDE (inst.operands[0].imm) != 2);
16525       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16526                                        &do_align, 16, 16, 32, 32, -1);
16527       if (align_good == FAIL)
16528         return;
16529       switch (NEON_REGLIST_LENGTH (inst.operands[0].imm))
16530         {
16531         case 1: break;
16532         case 2: inst.instruction |= 1 << 5; break;
16533         default: first_error (_("bad list length")); return;
16534         }
16535       inst.instruction |= neon_logbits (et.size) << 6;
16536       break;
16537
16538     case 1:  /* VLD2.  */
16539       align_good = neon_alignment_bit (et.size, inst.operands[1].imm >> 8,
16540                                        &do_align, 8, 16, 16, 32, 32, 64, -1);
16541       if (align_good == FAIL)
16542         return;
16543       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 2,
16544                   _("bad list length"));
16545       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16546         inst.instruction |= 1 << 5;
16547       inst.instruction |= neon_logbits (et.size) << 6;
16548       break;
16549
16550     case 2:  /* VLD3.  */
16551       constraint (inst.operands[1].immisalign,
16552                   _("can't use alignment with this instruction"));
16553       constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 3,
16554                   _("bad list length"));
16555       if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16556         inst.instruction |= 1 << 5;
16557       inst.instruction |= neon_logbits (et.size) << 6;
16558       break;
16559
16560     case 3:  /* VLD4.  */
16561       {
16562         int align = inst.operands[1].imm >> 8;
16563         align_good = neon_alignment_bit (et.size, align, &do_align, 8, 32,
16564                                          16, 64, 32, 64, 32, 128, -1);
16565         if (align_good == FAIL)
16566           return;
16567         constraint (NEON_REGLIST_LENGTH (inst.operands[0].imm) != 4,
16568                     _("bad list length"));
16569         if (NEON_REG_STRIDE (inst.operands[0].imm) == 2)
16570           inst.instruction |= 1 << 5;
16571         if (et.size == 32 && align == 128)
16572           inst.instruction |= 0x3 << 6;
16573         else
16574           inst.instruction |= neon_logbits (et.size) << 6;
16575       }
16576       break;
16577
16578     default: ;
16579     }
16580
16581   inst.instruction |= do_align << 4;
16582 }
16583
16584 /* Disambiguate VLD<n> and VST<n> instructions, and fill in common bits (those
16585    apart from bits [11:4].  */
16586
16587 static void
16588 do_neon_ldx_stx (void)
16589 {
16590   if (inst.operands[1].isreg)
16591     constraint (inst.operands[1].reg == REG_PC, BAD_PC);
16592
16593   switch (NEON_LANE (inst.operands[0].imm))
16594     {
16595     case NEON_INTERLEAVE_LANES:
16596       NEON_ENCODE (INTERLV, inst);
16597       do_neon_ld_st_interleave ();
16598       break;
16599
16600     case NEON_ALL_LANES:
16601       NEON_ENCODE (DUP, inst);
16602       if (inst.instruction == N_INV)
16603         {
16604           first_error ("only loads support such operands");
16605           break;
16606         }
16607       do_neon_ld_dup ();
16608       break;
16609
16610     default:
16611       NEON_ENCODE (LANE, inst);
16612       do_neon_ld_st_lane ();
16613     }
16614
16615   /* L bit comes from bit mask.  */
16616   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16617   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16618   inst.instruction |= inst.operands[1].reg << 16;
16619
16620   if (inst.operands[1].postind)
16621     {
16622       int postreg = inst.operands[1].imm & 0xf;
16623       constraint (!inst.operands[1].immisreg,
16624                   _("post-index must be a register"));
16625       constraint (postreg == 0xd || postreg == 0xf,
16626                   _("bad register for post-index"));
16627       inst.instruction |= postreg;
16628     }
16629   else
16630     {
16631       constraint (inst.operands[1].immisreg, BAD_ADDR_MODE);
16632       constraint (inst.reloc.exp.X_op != O_constant
16633                   || inst.reloc.exp.X_add_number != 0,
16634                   BAD_ADDR_MODE);
16635
16636       if (inst.operands[1].writeback)
16637         {
16638           inst.instruction |= 0xd;
16639         }
16640       else
16641         inst.instruction |= 0xf;
16642     }
16643
16644   if (thumb_mode)
16645     inst.instruction |= 0xf9000000;
16646   else
16647     inst.instruction |= 0xf4000000;
16648 }
16649
16650 /* FP v8.  */
16651 static void
16652 do_vfp_nsyn_fpv8 (enum neon_shape rs)
16653 {
16654   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
16655      D register operands.  */
16656   if (neon_shape_class[rs] == SC_DOUBLE)
16657     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16658                 _(BAD_FPU));
16659
16660   NEON_ENCODE (FPV8, inst);
16661
16662   if (rs == NS_FFF)
16663     do_vfp_sp_dyadic ();
16664   else
16665     do_vfp_dp_rd_rn_rm ();
16666
16667   if (rs == NS_DDD)
16668     inst.instruction |= 0x100;
16669
16670   inst.instruction |= 0xf0000000;
16671 }
16672
16673 static void
16674 do_vsel (void)
16675 {
16676   set_it_insn_type (OUTSIDE_IT_INSN);
16677
16678   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) != SUCCESS)
16679     first_error (_("invalid instruction shape"));
16680 }
16681
16682 static void
16683 do_vmaxnm (void)
16684 {
16685   set_it_insn_type (OUTSIDE_IT_INSN);
16686
16687   if (try_vfp_nsyn (3, do_vfp_nsyn_fpv8) == SUCCESS)
16688     return;
16689
16690   if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16691     return;
16692
16693   neon_dyadic_misc (NT_untyped, N_F32, 0);
16694 }
16695
16696 static void
16697 do_vrint_1 (enum neon_cvt_mode mode)
16698 {
16699   enum neon_shape rs = neon_select_shape (NS_FF, NS_DD, NS_QQ, NS_NULL);
16700   struct neon_type_el et;
16701
16702   if (rs == NS_NULL)
16703     return;
16704
16705   /* Targets like FPv5-SP-D16 don't support FP v8 instructions with
16706      D register operands.  */
16707   if (neon_shape_class[rs] == SC_DOUBLE)
16708     constraint (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_vfp_ext_armv8),
16709                 _(BAD_FPU));
16710
16711   et = neon_check_type (2, rs, N_EQK | N_VFP, N_F32 | N_F64 | N_KEY | N_VFP);
16712   if (et.type != NT_invtype)
16713     {
16714       /* VFP encodings.  */
16715       if (mode == neon_cvt_mode_a || mode == neon_cvt_mode_n
16716           || mode == neon_cvt_mode_p || mode == neon_cvt_mode_m)
16717         set_it_insn_type (OUTSIDE_IT_INSN);
16718
16719       NEON_ENCODE (FPV8, inst);
16720       if (rs == NS_FF)
16721         do_vfp_sp_monadic ();
16722       else
16723         do_vfp_dp_rd_rm ();
16724
16725       switch (mode)
16726         {
16727         case neon_cvt_mode_r: inst.instruction |= 0x00000000; break;
16728         case neon_cvt_mode_z: inst.instruction |= 0x00000080; break;
16729         case neon_cvt_mode_x: inst.instruction |= 0x00010000; break;
16730         case neon_cvt_mode_a: inst.instruction |= 0xf0000000; break;
16731         case neon_cvt_mode_n: inst.instruction |= 0xf0010000; break;
16732         case neon_cvt_mode_p: inst.instruction |= 0xf0020000; break;
16733         case neon_cvt_mode_m: inst.instruction |= 0xf0030000; break;
16734         default: abort ();
16735         }
16736
16737       inst.instruction |= (rs == NS_DD) << 8;
16738       do_vfp_cond_or_thumb ();
16739     }
16740   else
16741     {
16742       /* Neon encodings (or something broken...).  */
16743       inst.error = NULL;
16744       et = neon_check_type (2, rs, N_EQK, N_F32 | N_KEY);
16745
16746       if (et.type == NT_invtype)
16747         return;
16748
16749       set_it_insn_type (OUTSIDE_IT_INSN);
16750       NEON_ENCODE (FLOAT, inst);
16751
16752       if (vfp_or_neon_is_neon (NEON_CHECK_CC | NEON_CHECK_ARCH8) == FAIL)
16753         return;
16754
16755       inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16756       inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16757       inst.instruction |= LOW4 (inst.operands[1].reg);
16758       inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16759       inst.instruction |= neon_quad (rs) << 6;
16760       switch (mode)
16761         {
16762         case neon_cvt_mode_z: inst.instruction |= 3 << 7; break;
16763         case neon_cvt_mode_x: inst.instruction |= 1 << 7; break;
16764         case neon_cvt_mode_a: inst.instruction |= 2 << 7; break;
16765         case neon_cvt_mode_n: inst.instruction |= 0 << 7; break;
16766         case neon_cvt_mode_p: inst.instruction |= 7 << 7; break;
16767         case neon_cvt_mode_m: inst.instruction |= 5 << 7; break;
16768         case neon_cvt_mode_r: inst.error = _("invalid rounding mode"); break;
16769         default: abort ();
16770         }
16771
16772       if (thumb_mode)
16773         inst.instruction |= 0xfc000000;
16774       else
16775         inst.instruction |= 0xf0000000;
16776     }
16777 }
16778
16779 static void
16780 do_vrintx (void)
16781 {
16782   do_vrint_1 (neon_cvt_mode_x);
16783 }
16784
16785 static void
16786 do_vrintz (void)
16787 {
16788   do_vrint_1 (neon_cvt_mode_z);
16789 }
16790
16791 static void
16792 do_vrintr (void)
16793 {
16794   do_vrint_1 (neon_cvt_mode_r);
16795 }
16796
16797 static void
16798 do_vrinta (void)
16799 {
16800   do_vrint_1 (neon_cvt_mode_a);
16801 }
16802
16803 static void
16804 do_vrintn (void)
16805 {
16806   do_vrint_1 (neon_cvt_mode_n);
16807 }
16808
16809 static void
16810 do_vrintp (void)
16811 {
16812   do_vrint_1 (neon_cvt_mode_p);
16813 }
16814
16815 static void
16816 do_vrintm (void)
16817 {
16818   do_vrint_1 (neon_cvt_mode_m);
16819 }
16820
16821 /* Crypto v1 instructions.  */
16822 static void
16823 do_crypto_2op_1 (unsigned elttype, int op)
16824 {
16825   set_it_insn_type (OUTSIDE_IT_INSN);
16826
16827   if (neon_check_type (2, NS_QQ, N_EQK | N_UNT, elttype | N_UNT | N_KEY).type
16828       == NT_invtype)
16829     return;
16830
16831   inst.error = NULL;
16832
16833   NEON_ENCODE (INTEGER, inst);
16834   inst.instruction |= LOW4 (inst.operands[0].reg) << 12;
16835   inst.instruction |= HI1 (inst.operands[0].reg) << 22;
16836   inst.instruction |= LOW4 (inst.operands[1].reg);
16837   inst.instruction |= HI1 (inst.operands[1].reg) << 5;
16838   if (op != -1)
16839     inst.instruction |= op << 6;
16840
16841   if (thumb_mode)
16842     inst.instruction |= 0xfc000000;
16843   else
16844     inst.instruction |= 0xf0000000;
16845 }
16846
16847 static void
16848 do_crypto_3op_1 (int u, int op)
16849 {
16850   set_it_insn_type (OUTSIDE_IT_INSN);
16851
16852   if (neon_check_type (3, NS_QQQ, N_EQK | N_UNT, N_EQK | N_UNT,
16853                        N_32 | N_UNT | N_KEY).type == NT_invtype)
16854     return;
16855
16856   inst.error = NULL;
16857
16858   NEON_ENCODE (INTEGER, inst);
16859   neon_three_same (1, u, 8 << op);
16860 }
16861
16862 static void
16863 do_aese (void)
16864 {
16865   do_crypto_2op_1 (N_8, 0);
16866 }
16867
16868 static void
16869 do_aesd (void)
16870 {
16871   do_crypto_2op_1 (N_8, 1);
16872 }
16873
16874 static void
16875 do_aesmc (void)
16876 {
16877   do_crypto_2op_1 (N_8, 2);
16878 }
16879
16880 static void
16881 do_aesimc (void)
16882 {
16883   do_crypto_2op_1 (N_8, 3);
16884 }
16885
16886 static void
16887 do_sha1c (void)
16888 {
16889   do_crypto_3op_1 (0, 0);
16890 }
16891
16892 static void
16893 do_sha1p (void)
16894 {
16895   do_crypto_3op_1 (0, 1);
16896 }
16897
16898 static void
16899 do_sha1m (void)
16900 {
16901   do_crypto_3op_1 (0, 2);
16902 }
16903
16904 static void
16905 do_sha1su0 (void)
16906 {
16907   do_crypto_3op_1 (0, 3);
16908 }
16909
16910 static void
16911 do_sha256h (void)
16912 {
16913   do_crypto_3op_1 (1, 0);
16914 }
16915
16916 static void
16917 do_sha256h2 (void)
16918 {
16919   do_crypto_3op_1 (1, 1);
16920 }
16921
16922 static void
16923 do_sha256su1 (void)
16924 {
16925   do_crypto_3op_1 (1, 2);
16926 }
16927
16928 static void
16929 do_sha1h (void)
16930 {
16931   do_crypto_2op_1 (N_32, -1);
16932 }
16933
16934 static void
16935 do_sha1su1 (void)
16936 {
16937   do_crypto_2op_1 (N_32, 0);
16938 }
16939
16940 static void
16941 do_sha256su0 (void)
16942 {
16943   do_crypto_2op_1 (N_32, 1);
16944 }
16945
16946 static void
16947 do_crc32_1 (unsigned int poly, unsigned int sz)
16948 {
16949   unsigned int Rd = inst.operands[0].reg;
16950   unsigned int Rn = inst.operands[1].reg;
16951   unsigned int Rm = inst.operands[2].reg;
16952
16953   set_it_insn_type (OUTSIDE_IT_INSN);
16954   inst.instruction |= LOW4 (Rd) << (thumb_mode ? 8 : 12);
16955   inst.instruction |= LOW4 (Rn) << 16;
16956   inst.instruction |= LOW4 (Rm);
16957   inst.instruction |= sz << (thumb_mode ? 4 : 21);
16958   inst.instruction |= poly << (thumb_mode ? 20 : 9);
16959
16960   if (Rd == REG_PC || Rn == REG_PC || Rm == REG_PC)
16961     as_warn (UNPRED_REG ("r15"));
16962   if (thumb_mode && (Rd == REG_SP || Rn == REG_SP || Rm == REG_SP))
16963     as_warn (UNPRED_REG ("r13"));
16964 }
16965
16966 static void
16967 do_crc32b (void)
16968 {
16969   do_crc32_1 (0, 0);
16970 }
16971
16972 static void
16973 do_crc32h (void)
16974 {
16975   do_crc32_1 (0, 1);
16976 }
16977
16978 static void
16979 do_crc32w (void)
16980 {
16981   do_crc32_1 (0, 2);
16982 }
16983
16984 static void
16985 do_crc32cb (void)
16986 {
16987   do_crc32_1 (1, 0);
16988 }
16989
16990 static void
16991 do_crc32ch (void)
16992 {
16993   do_crc32_1 (1, 1);
16994 }
16995
16996 static void
16997 do_crc32cw (void)
16998 {
16999   do_crc32_1 (1, 2);
17000 }
17001
17002 \f
17003 /* Overall per-instruction processing.  */
17004
17005 /* We need to be able to fix up arbitrary expressions in some statements.
17006    This is so that we can handle symbols that are an arbitrary distance from
17007    the pc.  The most common cases are of the form ((+/-sym -/+ . - 8) & mask),
17008    which returns part of an address in a form which will be valid for
17009    a data instruction.  We do this by pushing the expression into a symbol
17010    in the expr_section, and creating a fix for that.  */
17011
17012 static void
17013 fix_new_arm (fragS *       frag,
17014              int           where,
17015              short int     size,
17016              expressionS * exp,
17017              int           pc_rel,
17018              int           reloc)
17019 {
17020   fixS *           new_fix;
17021
17022   switch (exp->X_op)
17023     {
17024     case O_constant:
17025       if (pc_rel)
17026         {
17027           /* Create an absolute valued symbol, so we have something to
17028              refer to in the object file.  Unfortunately for us, gas's
17029              generic expression parsing will already have folded out
17030              any use of .set foo/.type foo %function that may have
17031              been used to set type information of the target location,
17032              that's being specified symbolically.  We have to presume
17033              the user knows what they are doing.  */
17034           char name[16 + 8];
17035           symbolS *symbol;
17036
17037           sprintf (name, "*ABS*0x%lx", (unsigned long)exp->X_add_number);
17038
17039           symbol = symbol_find_or_make (name);
17040           S_SET_SEGMENT (symbol, absolute_section);
17041           symbol_set_frag (symbol, &zero_address_frag);
17042           S_SET_VALUE (symbol, exp->X_add_number);
17043           exp->X_op = O_symbol;
17044           exp->X_add_symbol = symbol;
17045           exp->X_add_number = 0;
17046         }
17047       /* FALLTHROUGH */
17048     case O_symbol:
17049     case O_add:
17050     case O_subtract:
17051       new_fix = fix_new_exp (frag, where, size, exp, pc_rel,
17052                              (enum bfd_reloc_code_real) reloc);
17053       break;
17054
17055     default:
17056       new_fix = (fixS *) fix_new (frag, where, size, make_expr_symbol (exp), 0,
17057                                   pc_rel, (enum bfd_reloc_code_real) reloc);
17058       break;
17059     }
17060
17061   /* Mark whether the fix is to a THUMB instruction, or an ARM
17062      instruction.  */
17063   new_fix->tc_fix_data = thumb_mode;
17064 }
17065
17066 /* Create a frg for an instruction requiring relaxation.  */
17067 static void
17068 output_relax_insn (void)
17069 {
17070   char * to;
17071   symbolS *sym;
17072   int offset;
17073
17074   /* The size of the instruction is unknown, so tie the debug info to the
17075      start of the instruction.  */
17076   dwarf2_emit_insn (0);
17077
17078   switch (inst.reloc.exp.X_op)
17079     {
17080     case O_symbol:
17081       sym = inst.reloc.exp.X_add_symbol;
17082       offset = inst.reloc.exp.X_add_number;
17083       break;
17084     case O_constant:
17085       sym = NULL;
17086       offset = inst.reloc.exp.X_add_number;
17087       break;
17088     default:
17089       sym = make_expr_symbol (&inst.reloc.exp);
17090       offset = 0;
17091       break;
17092   }
17093   to = frag_var (rs_machine_dependent, INSN_SIZE, THUMB_SIZE,
17094                  inst.relax, sym, offset, NULL/*offset, opcode*/);
17095   md_number_to_chars (to, inst.instruction, THUMB_SIZE);
17096 }
17097
17098 /* Write a 32-bit thumb instruction to buf.  */
17099 static void
17100 put_thumb32_insn (char * buf, unsigned long insn)
17101 {
17102   md_number_to_chars (buf, insn >> 16, THUMB_SIZE);
17103   md_number_to_chars (buf + THUMB_SIZE, insn, THUMB_SIZE);
17104 }
17105
17106 static void
17107 output_inst (const char * str)
17108 {
17109   char * to = NULL;
17110
17111   if (inst.error)
17112     {
17113       as_bad ("%s -- `%s'", inst.error, str);
17114       return;
17115     }
17116   if (inst.relax)
17117     {
17118       output_relax_insn ();
17119       return;
17120     }
17121   if (inst.size == 0)
17122     return;
17123
17124   to = frag_more (inst.size);
17125   /* PR 9814: Record the thumb mode into the current frag so that we know
17126      what type of NOP padding to use, if necessary.  We override any previous
17127      setting so that if the mode has changed then the NOPS that we use will
17128      match the encoding of the last instruction in the frag.  */
17129   frag_now->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
17130
17131   if (thumb_mode && (inst.size > THUMB_SIZE))
17132     {
17133       gas_assert (inst.size == (2 * THUMB_SIZE));
17134       put_thumb32_insn (to, inst.instruction);
17135     }
17136   else if (inst.size > INSN_SIZE)
17137     {
17138       gas_assert (inst.size == (2 * INSN_SIZE));
17139       md_number_to_chars (to, inst.instruction, INSN_SIZE);
17140       md_number_to_chars (to + INSN_SIZE, inst.instruction, INSN_SIZE);
17141     }
17142   else
17143     md_number_to_chars (to, inst.instruction, inst.size);
17144
17145   if (inst.reloc.type != BFD_RELOC_UNUSED)
17146     fix_new_arm (frag_now, to - frag_now->fr_literal,
17147                  inst.size, & inst.reloc.exp, inst.reloc.pc_rel,
17148                  inst.reloc.type);
17149
17150   dwarf2_emit_insn (inst.size);
17151 }
17152
17153 static char *
17154 output_it_inst (int cond, int mask, char * to)
17155 {
17156   unsigned long instruction = 0xbf00;
17157
17158   mask &= 0xf;
17159   instruction |= mask;
17160   instruction |= cond << 4;
17161
17162   if (to == NULL)
17163     {
17164       to = frag_more (2);
17165 #ifdef OBJ_ELF
17166       dwarf2_emit_insn (2);
17167 #endif
17168     }
17169
17170   md_number_to_chars (to, instruction, 2);
17171
17172   return to;
17173 }
17174
17175 /* Tag values used in struct asm_opcode's tag field.  */
17176 enum opcode_tag
17177 {
17178   OT_unconditional,     /* Instruction cannot be conditionalized.
17179                            The ARM condition field is still 0xE.  */
17180   OT_unconditionalF,    /* Instruction cannot be conditionalized
17181                            and carries 0xF in its ARM condition field.  */
17182   OT_csuffix,           /* Instruction takes a conditional suffix.  */
17183   OT_csuffixF,          /* Some forms of the instruction take a conditional
17184                            suffix, others place 0xF where the condition field
17185                            would be.  */
17186   OT_cinfix3,           /* Instruction takes a conditional infix,
17187                            beginning at character index 3.  (In
17188                            unified mode, it becomes a suffix.)  */
17189   OT_cinfix3_deprecated, /* The same as OT_cinfix3.  This is used for
17190                             tsts, cmps, cmns, and teqs. */
17191   OT_cinfix3_legacy,    /* Legacy instruction takes a conditional infix at
17192                            character index 3, even in unified mode.  Used for
17193                            legacy instructions where suffix and infix forms
17194                            may be ambiguous.  */
17195   OT_csuf_or_in3,       /* Instruction takes either a conditional
17196                            suffix or an infix at character index 3.  */
17197   OT_odd_infix_unc,     /* This is the unconditional variant of an
17198                            instruction that takes a conditional infix
17199                            at an unusual position.  In unified mode,
17200                            this variant will accept a suffix.  */
17201   OT_odd_infix_0        /* Values greater than or equal to OT_odd_infix_0
17202                            are the conditional variants of instructions that
17203                            take conditional infixes in unusual positions.
17204                            The infix appears at character index
17205                            (tag - OT_odd_infix_0).  These are not accepted
17206                            in unified mode.  */
17207 };
17208
17209 /* Subroutine of md_assemble, responsible for looking up the primary
17210    opcode from the mnemonic the user wrote.  STR points to the
17211    beginning of the mnemonic.
17212
17213    This is not simply a hash table lookup, because of conditional
17214    variants.  Most instructions have conditional variants, which are
17215    expressed with a _conditional affix_ to the mnemonic.  If we were
17216    to encode each conditional variant as a literal string in the opcode
17217    table, it would have approximately 20,000 entries.
17218
17219    Most mnemonics take this affix as a suffix, and in unified syntax,
17220    'most' is upgraded to 'all'.  However, in the divided syntax, some
17221    instructions take the affix as an infix, notably the s-variants of
17222    the arithmetic instructions.  Of those instructions, all but six
17223    have the infix appear after the third character of the mnemonic.
17224
17225    Accordingly, the algorithm for looking up primary opcodes given
17226    an identifier is:
17227
17228    1. Look up the identifier in the opcode table.
17229       If we find a match, go to step U.
17230
17231    2. Look up the last two characters of the identifier in the
17232       conditions table.  If we find a match, look up the first N-2
17233       characters of the identifier in the opcode table.  If we
17234       find a match, go to step CE.
17235
17236    3. Look up the fourth and fifth characters of the identifier in
17237       the conditions table.  If we find a match, extract those
17238       characters from the identifier, and look up the remaining
17239       characters in the opcode table.  If we find a match, go
17240       to step CM.
17241
17242    4. Fail.
17243
17244    U. Examine the tag field of the opcode structure, in case this is
17245       one of the six instructions with its conditional infix in an
17246       unusual place.  If it is, the tag tells us where to find the
17247       infix; look it up in the conditions table and set inst.cond
17248       accordingly.  Otherwise, this is an unconditional instruction.
17249       Again set inst.cond accordingly.  Return the opcode structure.
17250
17251   CE. Examine the tag field to make sure this is an instruction that
17252       should receive a conditional suffix.  If it is not, fail.
17253       Otherwise, set inst.cond from the suffix we already looked up,
17254       and return the opcode structure.
17255
17256   CM. Examine the tag field to make sure this is an instruction that
17257       should receive a conditional infix after the third character.
17258       If it is not, fail.  Otherwise, undo the edits to the current
17259       line of input and proceed as for case CE.  */
17260
17261 static const struct asm_opcode *
17262 opcode_lookup (char **str)
17263 {
17264   char *end, *base;
17265   char *affix;
17266   const struct asm_opcode *opcode;
17267   const struct asm_cond *cond;
17268   char save[2];
17269
17270   /* Scan up to the end of the mnemonic, which must end in white space,
17271      '.' (in unified mode, or for Neon/VFP instructions), or end of string.  */
17272   for (base = end = *str; *end != '\0'; end++)
17273     if (*end == ' ' || *end == '.')
17274       break;
17275
17276   if (end == base)
17277     return NULL;
17278
17279   /* Handle a possible width suffix and/or Neon type suffix.  */
17280   if (end[0] == '.')
17281     {
17282       int offset = 2;
17283
17284       /* The .w and .n suffixes are only valid if the unified syntax is in
17285          use.  */
17286       if (unified_syntax && end[1] == 'w')
17287         inst.size_req = 4;
17288       else if (unified_syntax && end[1] == 'n')
17289         inst.size_req = 2;
17290       else
17291         offset = 0;
17292
17293       inst.vectype.elems = 0;
17294
17295       *str = end + offset;
17296
17297       if (end[offset] == '.')
17298         {
17299           /* See if we have a Neon type suffix (possible in either unified or
17300              non-unified ARM syntax mode).  */
17301           if (parse_neon_type (&inst.vectype, str) == FAIL)
17302             return NULL;
17303         }
17304       else if (end[offset] != '\0' && end[offset] != ' ')
17305         return NULL;
17306     }
17307   else
17308     *str = end;
17309
17310   /* Look for unaffixed or special-case affixed mnemonic.  */
17311   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17312                                                     end - base);
17313   if (opcode)
17314     {
17315       /* step U */
17316       if (opcode->tag < OT_odd_infix_0)
17317         {
17318           inst.cond = COND_ALWAYS;
17319           return opcode;
17320         }
17321
17322       if (warn_on_deprecated && unified_syntax)
17323         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17324       affix = base + (opcode->tag - OT_odd_infix_0);
17325       cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17326       gas_assert (cond);
17327
17328       inst.cond = cond->value;
17329       return opcode;
17330     }
17331
17332   /* Cannot have a conditional suffix on a mnemonic of less than two
17333      characters.  */
17334   if (end - base < 3)
17335     return NULL;
17336
17337   /* Look for suffixed mnemonic.  */
17338   affix = end - 2;
17339   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17340   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17341                                                     affix - base);
17342   if (opcode && cond)
17343     {
17344       /* step CE */
17345       switch (opcode->tag)
17346         {
17347         case OT_cinfix3_legacy:
17348           /* Ignore conditional suffixes matched on infix only mnemonics.  */
17349           break;
17350
17351         case OT_cinfix3:
17352         case OT_cinfix3_deprecated:
17353         case OT_odd_infix_unc:
17354           if (!unified_syntax)
17355             return 0;
17356           /* else fall through */
17357
17358         case OT_csuffix:
17359         case OT_csuffixF:
17360         case OT_csuf_or_in3:
17361           inst.cond = cond->value;
17362           return opcode;
17363
17364         case OT_unconditional:
17365         case OT_unconditionalF:
17366           if (thumb_mode)
17367             inst.cond = cond->value;
17368           else
17369             {
17370               /* Delayed diagnostic.  */
17371               inst.error = BAD_COND;
17372               inst.cond = COND_ALWAYS;
17373             }
17374           return opcode;
17375
17376         default:
17377           return NULL;
17378         }
17379     }
17380
17381   /* Cannot have a usual-position infix on a mnemonic of less than
17382      six characters (five would be a suffix).  */
17383   if (end - base < 6)
17384     return NULL;
17385
17386   /* Look for infixed mnemonic in the usual position.  */
17387   affix = base + 3;
17388   cond = (const struct asm_cond *) hash_find_n (arm_cond_hsh, affix, 2);
17389   if (!cond)
17390     return NULL;
17391
17392   memcpy (save, affix, 2);
17393   memmove (affix, affix + 2, (end - affix) - 2);
17394   opcode = (const struct asm_opcode *) hash_find_n (arm_ops_hsh, base,
17395                                                     (end - base) - 2);
17396   memmove (affix + 2, affix, (end - affix) - 2);
17397   memcpy (affix, save, 2);
17398
17399   if (opcode
17400       && (opcode->tag == OT_cinfix3
17401           || opcode->tag == OT_cinfix3_deprecated
17402           || opcode->tag == OT_csuf_or_in3
17403           || opcode->tag == OT_cinfix3_legacy))
17404     {
17405       /* Step CM.  */
17406       if (warn_on_deprecated && unified_syntax
17407           && (opcode->tag == OT_cinfix3
17408               || opcode->tag == OT_cinfix3_deprecated))
17409         as_tsktsk (_("conditional infixes are deprecated in unified syntax"));
17410
17411       inst.cond = cond->value;
17412       return opcode;
17413     }
17414
17415   return NULL;
17416 }
17417
17418 /* This function generates an initial IT instruction, leaving its block
17419    virtually open for the new instructions. Eventually,
17420    the mask will be updated by now_it_add_mask () each time
17421    a new instruction needs to be included in the IT block.
17422    Finally, the block is closed with close_automatic_it_block ().
17423    The block closure can be requested either from md_assemble (),
17424    a tencode (), or due to a label hook.  */
17425
17426 static void
17427 new_automatic_it_block (int cond)
17428 {
17429   now_it.state = AUTOMATIC_IT_BLOCK;
17430   now_it.mask = 0x18;
17431   now_it.cc = cond;
17432   now_it.block_length = 1;
17433   mapping_state (MAP_THUMB);
17434   now_it.insn = output_it_inst (cond, now_it.mask, NULL);
17435   now_it.warn_deprecated = FALSE;
17436   now_it.insn_cond = TRUE;
17437 }
17438
17439 /* Close an automatic IT block.
17440    See comments in new_automatic_it_block ().  */
17441
17442 static void
17443 close_automatic_it_block (void)
17444 {
17445   now_it.mask = 0x10;
17446   now_it.block_length = 0;
17447 }
17448
17449 /* Update the mask of the current automatically-generated IT
17450    instruction. See comments in new_automatic_it_block ().  */
17451
17452 static void
17453 now_it_add_mask (int cond)
17454 {
17455 #define CLEAR_BIT(value, nbit)  ((value) & ~(1 << (nbit)))
17456 #define SET_BIT_VALUE(value, bitvalue, nbit)  (CLEAR_BIT (value, nbit) \
17457                                               | ((bitvalue) << (nbit)))
17458   const int resulting_bit = (cond & 1);
17459
17460   now_it.mask &= 0xf;
17461   now_it.mask = SET_BIT_VALUE (now_it.mask,
17462                                    resulting_bit,
17463                                   (5 - now_it.block_length));
17464   now_it.mask = SET_BIT_VALUE (now_it.mask,
17465                                    1,
17466                                    ((5 - now_it.block_length) - 1) );
17467   output_it_inst (now_it.cc, now_it.mask, now_it.insn);
17468
17469 #undef CLEAR_BIT
17470 #undef SET_BIT_VALUE
17471 }
17472
17473 /* The IT blocks handling machinery is accessed through the these functions:
17474      it_fsm_pre_encode ()               from md_assemble ()
17475      set_it_insn_type ()                optional, from the tencode functions
17476      set_it_insn_type_last ()           ditto
17477      in_it_block ()                     ditto
17478      it_fsm_post_encode ()              from md_assemble ()
17479      force_automatic_it_block_close ()  from label habdling functions
17480
17481    Rationale:
17482      1) md_assemble () calls it_fsm_pre_encode () before calling tencode (),
17483         initializing the IT insn type with a generic initial value depending
17484         on the inst.condition.
17485      2) During the tencode function, two things may happen:
17486         a) The tencode function overrides the IT insn type by
17487            calling either set_it_insn_type (type) or set_it_insn_type_last ().
17488         b) The tencode function queries the IT block state by
17489            calling in_it_block () (i.e. to determine narrow/not narrow mode).
17490
17491         Both set_it_insn_type and in_it_block run the internal FSM state
17492         handling function (handle_it_state), because: a) setting the IT insn
17493         type may incur in an invalid state (exiting the function),
17494         and b) querying the state requires the FSM to be updated.
17495         Specifically we want to avoid creating an IT block for conditional
17496         branches, so it_fsm_pre_encode is actually a guess and we can't
17497         determine whether an IT block is required until the tencode () routine
17498         has decided what type of instruction this actually it.
17499         Because of this, if set_it_insn_type and in_it_block have to be used,
17500         set_it_insn_type has to be called first.
17501
17502         set_it_insn_type_last () is a wrapper of set_it_insn_type (type), that
17503         determines the insn IT type depending on the inst.cond code.
17504         When a tencode () routine encodes an instruction that can be
17505         either outside an IT block, or, in the case of being inside, has to be
17506         the last one, set_it_insn_type_last () will determine the proper
17507         IT instruction type based on the inst.cond code. Otherwise,
17508         set_it_insn_type can be called for overriding that logic or
17509         for covering other cases.
17510
17511         Calling handle_it_state () may not transition the IT block state to
17512         OUTSIDE_IT_BLOCK immediatelly, since the (current) state could be
17513         still queried. Instead, if the FSM determines that the state should
17514         be transitioned to OUTSIDE_IT_BLOCK, a flag is marked to be closed
17515         after the tencode () function: that's what it_fsm_post_encode () does.
17516
17517         Since in_it_block () calls the state handling function to get an
17518         updated state, an error may occur (due to invalid insns combination).
17519         In that case, inst.error is set.
17520         Therefore, inst.error has to be checked after the execution of
17521         the tencode () routine.
17522
17523      3) Back in md_assemble(), it_fsm_post_encode () is called to commit
17524         any pending state change (if any) that didn't take place in
17525         handle_it_state () as explained above.  */
17526
17527 static void
17528 it_fsm_pre_encode (void)
17529 {
17530   if (inst.cond != COND_ALWAYS)
17531     inst.it_insn_type = INSIDE_IT_INSN;
17532   else
17533     inst.it_insn_type = OUTSIDE_IT_INSN;
17534
17535   now_it.state_handled = 0;
17536 }
17537
17538 /* IT state FSM handling function.  */
17539
17540 static int
17541 handle_it_state (void)
17542 {
17543   now_it.state_handled = 1;
17544   now_it.insn_cond = FALSE;
17545
17546   switch (now_it.state)
17547     {
17548     case OUTSIDE_IT_BLOCK:
17549       switch (inst.it_insn_type)
17550         {
17551         case OUTSIDE_IT_INSN:
17552           break;
17553
17554         case INSIDE_IT_INSN:
17555         case INSIDE_IT_LAST_INSN:
17556           if (thumb_mode == 0)
17557             {
17558               if (unified_syntax
17559                   && !(implicit_it_mode & IMPLICIT_IT_MODE_ARM))
17560                 as_tsktsk (_("Warning: conditional outside an IT block"\
17561                              " for Thumb."));
17562             }
17563           else
17564             {
17565               if ((implicit_it_mode & IMPLICIT_IT_MODE_THUMB)
17566                   && ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2))
17567                 {
17568                   /* Automatically generate the IT instruction.  */
17569                   new_automatic_it_block (inst.cond);
17570                   if (inst.it_insn_type == INSIDE_IT_LAST_INSN)
17571                     close_automatic_it_block ();
17572                 }
17573               else
17574                 {
17575                   inst.error = BAD_OUT_IT;
17576                   return FAIL;
17577                 }
17578             }
17579           break;
17580
17581         case IF_INSIDE_IT_LAST_INSN:
17582         case NEUTRAL_IT_INSN:
17583           break;
17584
17585         case IT_INSN:
17586           now_it.state = MANUAL_IT_BLOCK;
17587           now_it.block_length = 0;
17588           break;
17589         }
17590       break;
17591
17592     case AUTOMATIC_IT_BLOCK:
17593       /* Three things may happen now:
17594          a) We should increment current it block size;
17595          b) We should close current it block (closing insn or 4 insns);
17596          c) We should close current it block and start a new one (due
17597          to incompatible conditions or
17598          4 insns-length block reached).  */
17599
17600       switch (inst.it_insn_type)
17601         {
17602         case OUTSIDE_IT_INSN:
17603           /* The closure of the block shall happen immediatelly,
17604              so any in_it_block () call reports the block as closed.  */
17605           force_automatic_it_block_close ();
17606           break;
17607
17608         case INSIDE_IT_INSN:
17609         case INSIDE_IT_LAST_INSN:
17610         case IF_INSIDE_IT_LAST_INSN:
17611           now_it.block_length++;
17612
17613           if (now_it.block_length > 4
17614               || !now_it_compatible (inst.cond))
17615             {
17616               force_automatic_it_block_close ();
17617               if (inst.it_insn_type != IF_INSIDE_IT_LAST_INSN)
17618                 new_automatic_it_block (inst.cond);
17619             }
17620           else
17621             {
17622               now_it.insn_cond = TRUE;
17623               now_it_add_mask (inst.cond);
17624             }
17625
17626           if (now_it.state == AUTOMATIC_IT_BLOCK
17627               && (inst.it_insn_type == INSIDE_IT_LAST_INSN
17628                   || inst.it_insn_type == IF_INSIDE_IT_LAST_INSN))
17629             close_automatic_it_block ();
17630           break;
17631
17632         case NEUTRAL_IT_INSN:
17633           now_it.block_length++;
17634           now_it.insn_cond = TRUE;
17635
17636           if (now_it.block_length > 4)
17637             force_automatic_it_block_close ();
17638           else
17639             now_it_add_mask (now_it.cc & 1);
17640           break;
17641
17642         case IT_INSN:
17643           close_automatic_it_block ();
17644           now_it.state = MANUAL_IT_BLOCK;
17645           break;
17646         }
17647       break;
17648
17649     case MANUAL_IT_BLOCK:
17650       {
17651         /* Check conditional suffixes.  */
17652         const int cond = now_it.cc ^ ((now_it.mask >> 4) & 1) ^ 1;
17653         int is_last;
17654         now_it.mask <<= 1;
17655         now_it.mask &= 0x1f;
17656         is_last = (now_it.mask == 0x10);
17657         now_it.insn_cond = TRUE;
17658
17659         switch (inst.it_insn_type)
17660           {
17661           case OUTSIDE_IT_INSN:
17662             inst.error = BAD_NOT_IT;
17663             return FAIL;
17664
17665           case INSIDE_IT_INSN:
17666             if (cond != inst.cond)
17667               {
17668                 inst.error = BAD_IT_COND;
17669                 return FAIL;
17670               }
17671             break;
17672
17673           case INSIDE_IT_LAST_INSN:
17674           case IF_INSIDE_IT_LAST_INSN:
17675             if (cond != inst.cond)
17676               {
17677                 inst.error = BAD_IT_COND;
17678                 return FAIL;
17679               }
17680             if (!is_last)
17681               {
17682                 inst.error = BAD_BRANCH;
17683                 return FAIL;
17684               }
17685             break;
17686
17687           case NEUTRAL_IT_INSN:
17688             /* The BKPT instruction is unconditional even in an IT block.  */
17689             break;
17690
17691           case IT_INSN:
17692             inst.error = BAD_IT_IT;
17693             return FAIL;
17694           }
17695       }
17696       break;
17697     }
17698
17699   return SUCCESS;
17700 }
17701
17702 struct depr_insn_mask
17703 {
17704   unsigned long pattern;
17705   unsigned long mask;
17706   const char* description;
17707 };
17708
17709 /* List of 16-bit instruction patterns deprecated in an IT block in
17710    ARMv8.  */
17711 static const struct depr_insn_mask depr_it_insns[] = {
17712   { 0xc000, 0xc000, N_("Short branches, Undefined, SVC, LDM/STM") },
17713   { 0xb000, 0xb000, N_("Miscellaneous 16-bit instructions") },
17714   { 0xa000, 0xb800, N_("ADR") },
17715   { 0x4800, 0xf800, N_("Literal loads") },
17716   { 0x4478, 0xf478, N_("Hi-register ADD, MOV, CMP, BX, BLX using pc") },
17717   { 0x4487, 0xfc87, N_("Hi-register ADD, MOV, CMP using pc") },
17718   /* NOTE: 0x00dd is not the real encoding, instead, it is the 'tvalue'
17719      field in asm_opcode. 'tvalue' is used at the stage this check happen.  */
17720   { 0x00dd, 0x7fff, N_("ADD/SUB sp, sp #imm") },
17721   { 0, 0, NULL }
17722 };
17723
17724 static void
17725 it_fsm_post_encode (void)
17726 {
17727   int is_last;
17728
17729   if (!now_it.state_handled)
17730     handle_it_state ();
17731
17732   if (now_it.insn_cond
17733       && !now_it.warn_deprecated
17734       && warn_on_deprecated
17735       && ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v8))
17736     {
17737       if (inst.instruction >= 0x10000)
17738         {
17739           as_tsktsk (_("IT blocks containing 32-bit Thumb instructions are "
17740                      "deprecated in ARMv8"));
17741           now_it.warn_deprecated = TRUE;
17742         }
17743       else
17744         {
17745           const struct depr_insn_mask *p = depr_it_insns;
17746
17747           while (p->mask != 0)
17748             {
17749               if ((inst.instruction & p->mask) == p->pattern)
17750                 {
17751                   as_tsktsk (_("IT blocks containing 16-bit Thumb instructions "
17752                              "of the following class are deprecated in ARMv8: "
17753                              "%s"), p->description);
17754                   now_it.warn_deprecated = TRUE;
17755                   break;
17756                 }
17757
17758               ++p;
17759             }
17760         }
17761
17762       if (now_it.block_length > 1)
17763         {
17764           as_tsktsk (_("IT blocks containing more than one conditional "
17765                      "instruction are deprecated in ARMv8"));
17766           now_it.warn_deprecated = TRUE;
17767         }
17768     }
17769
17770   is_last = (now_it.mask == 0x10);
17771   if (is_last)
17772     {
17773       now_it.state = OUTSIDE_IT_BLOCK;
17774       now_it.mask = 0;
17775     }
17776 }
17777
17778 static void
17779 force_automatic_it_block_close (void)
17780 {
17781   if (now_it.state == AUTOMATIC_IT_BLOCK)
17782     {
17783       close_automatic_it_block ();
17784       now_it.state = OUTSIDE_IT_BLOCK;
17785       now_it.mask = 0;
17786     }
17787 }
17788
17789 static int
17790 in_it_block (void)
17791 {
17792   if (!now_it.state_handled)
17793     handle_it_state ();
17794
17795   return now_it.state != OUTSIDE_IT_BLOCK;
17796 }
17797
17798 void
17799 md_assemble (char *str)
17800 {
17801   char *p = str;
17802   const struct asm_opcode * opcode;
17803
17804   /* Align the previous label if needed.  */
17805   if (last_label_seen != NULL)
17806     {
17807       symbol_set_frag (last_label_seen, frag_now);
17808       S_SET_VALUE (last_label_seen, (valueT) frag_now_fix ());
17809       S_SET_SEGMENT (last_label_seen, now_seg);
17810     }
17811
17812   memset (&inst, '\0', sizeof (inst));
17813   inst.reloc.type = BFD_RELOC_UNUSED;
17814
17815   opcode = opcode_lookup (&p);
17816   if (!opcode)
17817     {
17818       /* It wasn't an instruction, but it might be a register alias of
17819          the form alias .req reg, or a Neon .dn/.qn directive.  */
17820       if (! create_register_alias (str, p)
17821           && ! create_neon_reg_alias (str, p))
17822         as_bad (_("bad instruction `%s'"), str);
17823
17824       return;
17825     }
17826
17827   if (warn_on_deprecated && opcode->tag == OT_cinfix3_deprecated)
17828     as_tsktsk (_("s suffix on comparison instruction is deprecated"));
17829
17830   /* The value which unconditional instructions should have in place of the
17831      condition field.  */
17832   inst.uncond_value = (opcode->tag == OT_csuffixF) ? 0xf : -1;
17833
17834   if (thumb_mode)
17835     {
17836       arm_feature_set variant;
17837
17838       variant = cpu_variant;
17839       /* Only allow coprocessor instructions on Thumb-2 capable devices.  */
17840       if (!ARM_CPU_HAS_FEATURE (variant, arm_arch_t2))
17841         ARM_CLEAR_FEATURE (variant, variant, fpu_any_hard);
17842       /* Check that this instruction is supported for this CPU.  */
17843       if (!opcode->tvariant
17844           || (thumb_mode == 1
17845               && !ARM_CPU_HAS_FEATURE (variant, *opcode->tvariant)))
17846         {
17847           as_bad (_("selected processor does not support `%s' in Thumb mode"), str);
17848           return;
17849         }
17850       if (inst.cond != COND_ALWAYS && !unified_syntax
17851           && opcode->tencode != do_t_branch)
17852         {
17853           as_bad (_("Thumb does not support conditional execution"));
17854           return;
17855         }
17856
17857       if (!ARM_CPU_HAS_FEATURE (variant, arm_ext_v6t2))
17858         {
17859           if (opcode->tencode != do_t_blx && opcode->tencode != do_t_branch23
17860               && !(ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_msr)
17861                    || ARM_CPU_HAS_FEATURE(*opcode->tvariant, arm_ext_barrier)))
17862             {
17863               /* Two things are addressed here.
17864                  1) Implicit require narrow instructions on Thumb-1.
17865                     This avoids relaxation accidentally introducing Thumb-2
17866                      instructions.
17867                  2) Reject wide instructions in non Thumb-2 cores.  */
17868               if (inst.size_req == 0)
17869                 inst.size_req = 2;
17870               else if (inst.size_req == 4)
17871                 {
17872                   as_bad (_("selected processor does not support `%s' in Thumb-2 mode"), str);
17873                   return;
17874                 }
17875             }
17876         }
17877
17878       inst.instruction = opcode->tvalue;
17879
17880       if (!parse_operands (p, opcode->operands, /*thumb=*/TRUE))
17881         {
17882           /* Prepare the it_insn_type for those encodings that don't set
17883              it.  */
17884           it_fsm_pre_encode ();
17885
17886           opcode->tencode ();
17887
17888           it_fsm_post_encode ();
17889         }
17890
17891       if (!(inst.error || inst.relax))
17892         {
17893           gas_assert (inst.instruction < 0xe800 || inst.instruction > 0xffff);
17894           inst.size = (inst.instruction > 0xffff ? 4 : 2);
17895           if (inst.size_req && inst.size_req != inst.size)
17896             {
17897               as_bad (_("cannot honor width suffix -- `%s'"), str);
17898               return;
17899             }
17900         }
17901
17902       /* Something has gone badly wrong if we try to relax a fixed size
17903          instruction.  */
17904       gas_assert (inst.size_req == 0 || !inst.relax);
17905
17906       ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17907                               *opcode->tvariant);
17908       /* Many Thumb-2 instructions also have Thumb-1 variants, so explicitly
17909          set those bits when Thumb-2 32-bit instructions are seen.  ie.
17910          anything other than bl/blx and v6-M instructions.
17911          The impact of relaxable instructions will be considered later after we
17912          finish all relaxation.  */
17913       if ((inst.size == 4 && (inst.instruction & 0xf800e800) != 0xf000e800)
17914           && !(ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_msr)
17915                || ARM_CPU_HAS_FEATURE (*opcode->tvariant, arm_ext_barrier)))
17916         ARM_MERGE_FEATURE_SETS (thumb_arch_used, thumb_arch_used,
17917                                 arm_ext_v6t2);
17918
17919       check_neon_suffixes;
17920
17921       if (!inst.error)
17922         {
17923           mapping_state (MAP_THUMB);
17924         }
17925     }
17926   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
17927     {
17928       bfd_boolean is_bx;
17929
17930       /* bx is allowed on v5 cores, and sometimes on v4 cores.  */
17931       is_bx = (opcode->aencode == do_bx);
17932
17933       /* Check that this instruction is supported for this CPU.  */
17934       if (!(is_bx && fix_v4bx)
17935           && !(opcode->avariant &&
17936                ARM_CPU_HAS_FEATURE (cpu_variant, *opcode->avariant)))
17937         {
17938           as_bad (_("selected processor does not support `%s' in ARM mode"), str);
17939           return;
17940         }
17941       if (inst.size_req)
17942         {
17943           as_bad (_("width suffixes are invalid in ARM mode -- `%s'"), str);
17944           return;
17945         }
17946
17947       inst.instruction = opcode->avalue;
17948       if (opcode->tag == OT_unconditionalF)
17949         inst.instruction |= 0xFU << 28;
17950       else
17951         inst.instruction |= inst.cond << 28;
17952       inst.size = INSN_SIZE;
17953       if (!parse_operands (p, opcode->operands, /*thumb=*/FALSE))
17954         {
17955           it_fsm_pre_encode ();
17956           opcode->aencode ();
17957           it_fsm_post_encode ();
17958         }
17959       /* Arm mode bx is marked as both v4T and v5 because it's still required
17960          on a hypothetical non-thumb v5 core.  */
17961       if (is_bx)
17962         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used, arm_ext_v4t);
17963       else
17964         ARM_MERGE_FEATURE_SETS (arm_arch_used, arm_arch_used,
17965                                 *opcode->avariant);
17966
17967       check_neon_suffixes;
17968
17969       if (!inst.error)
17970         {
17971           mapping_state (MAP_ARM);
17972         }
17973     }
17974   else
17975     {
17976       as_bad (_("attempt to use an ARM instruction on a Thumb-only processor "
17977                 "-- `%s'"), str);
17978       return;
17979     }
17980   output_inst (str);
17981 }
17982
17983 static void
17984 check_it_blocks_finished (void)
17985 {
17986 #ifdef OBJ_ELF
17987   asection *sect;
17988
17989   for (sect = stdoutput->sections; sect != NULL; sect = sect->next)
17990     if (seg_info (sect)->tc_segment_info_data.current_it.state
17991         == MANUAL_IT_BLOCK)
17992       {
17993         as_warn (_("section '%s' finished with an open IT block."),
17994                  sect->name);
17995       }
17996 #else
17997   if (now_it.state == MANUAL_IT_BLOCK)
17998     as_warn (_("file finished with an open IT block."));
17999 #endif
18000 }
18001
18002 /* Various frobbings of labels and their addresses.  */
18003
18004 void
18005 arm_start_line_hook (void)
18006 {
18007   last_label_seen = NULL;
18008 }
18009
18010 void
18011 arm_frob_label (symbolS * sym)
18012 {
18013   last_label_seen = sym;
18014
18015   ARM_SET_THUMB (sym, thumb_mode);
18016
18017 #if defined OBJ_COFF || defined OBJ_ELF
18018   ARM_SET_INTERWORK (sym, support_interwork);
18019 #endif
18020
18021   force_automatic_it_block_close ();
18022
18023   /* Note - do not allow local symbols (.Lxxx) to be labelled
18024      as Thumb functions.  This is because these labels, whilst
18025      they exist inside Thumb code, are not the entry points for
18026      possible ARM->Thumb calls.  Also, these labels can be used
18027      as part of a computed goto or switch statement.  eg gcc
18028      can generate code that looks like this:
18029
18030                 ldr  r2, [pc, .Laaa]
18031                 lsl  r3, r3, #2
18032                 ldr  r2, [r3, r2]
18033                 mov  pc, r2
18034
18035        .Lbbb:  .word .Lxxx
18036        .Lccc:  .word .Lyyy
18037        ..etc...
18038        .Laaa:   .word Lbbb
18039
18040      The first instruction loads the address of the jump table.
18041      The second instruction converts a table index into a byte offset.
18042      The third instruction gets the jump address out of the table.
18043      The fourth instruction performs the jump.
18044
18045      If the address stored at .Laaa is that of a symbol which has the
18046      Thumb_Func bit set, then the linker will arrange for this address
18047      to have the bottom bit set, which in turn would mean that the
18048      address computation performed by the third instruction would end
18049      up with the bottom bit set.  Since the ARM is capable of unaligned
18050      word loads, the instruction would then load the incorrect address
18051      out of the jump table, and chaos would ensue.  */
18052   if (label_is_thumb_function_name
18053       && (S_GET_NAME (sym)[0] != '.' || S_GET_NAME (sym)[1] != 'L')
18054       && (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
18055     {
18056       /* When the address of a Thumb function is taken the bottom
18057          bit of that address should be set.  This will allow
18058          interworking between Arm and Thumb functions to work
18059          correctly.  */
18060
18061       THUMB_SET_FUNC (sym, 1);
18062
18063       label_is_thumb_function_name = FALSE;
18064     }
18065
18066   dwarf2_emit_label (sym);
18067 }
18068
18069 bfd_boolean
18070 arm_data_in_code (void)
18071 {
18072   if (thumb_mode && ! strncmp (input_line_pointer + 1, "data:", 5))
18073     {
18074       *input_line_pointer = '/';
18075       input_line_pointer += 5;
18076       *input_line_pointer = 0;
18077       return TRUE;
18078     }
18079
18080   return FALSE;
18081 }
18082
18083 char *
18084 arm_canonicalize_symbol_name (char * name)
18085 {
18086   int len;
18087
18088   if (thumb_mode && (len = strlen (name)) > 5
18089       && streq (name + len - 5, "/data"))
18090     *(name + len - 5) = 0;
18091
18092   return name;
18093 }
18094 \f
18095 /* Table of all register names defined by default.  The user can
18096    define additional names with .req.  Note that all register names
18097    should appear in both upper and lowercase variants.  Some registers
18098    also have mixed-case names.  */
18099
18100 #define REGDEF(s,n,t) { #s, n, REG_TYPE_##t, TRUE, 0 }
18101 #define REGNUM(p,n,t) REGDEF(p##n, n, t)
18102 #define REGNUM2(p,n,t) REGDEF(p##n, 2 * n, t)
18103 #define REGSET(p,t) \
18104   REGNUM(p, 0,t), REGNUM(p, 1,t), REGNUM(p, 2,t), REGNUM(p, 3,t), \
18105   REGNUM(p, 4,t), REGNUM(p, 5,t), REGNUM(p, 6,t), REGNUM(p, 7,t), \
18106   REGNUM(p, 8,t), REGNUM(p, 9,t), REGNUM(p,10,t), REGNUM(p,11,t), \
18107   REGNUM(p,12,t), REGNUM(p,13,t), REGNUM(p,14,t), REGNUM(p,15,t)
18108 #define REGSETH(p,t) \
18109   REGNUM(p,16,t), REGNUM(p,17,t), REGNUM(p,18,t), REGNUM(p,19,t), \
18110   REGNUM(p,20,t), REGNUM(p,21,t), REGNUM(p,22,t), REGNUM(p,23,t), \
18111   REGNUM(p,24,t), REGNUM(p,25,t), REGNUM(p,26,t), REGNUM(p,27,t), \
18112   REGNUM(p,28,t), REGNUM(p,29,t), REGNUM(p,30,t), REGNUM(p,31,t)
18113 #define REGSET2(p,t) \
18114   REGNUM2(p, 0,t), REGNUM2(p, 1,t), REGNUM2(p, 2,t), REGNUM2(p, 3,t), \
18115   REGNUM2(p, 4,t), REGNUM2(p, 5,t), REGNUM2(p, 6,t), REGNUM2(p, 7,t), \
18116   REGNUM2(p, 8,t), REGNUM2(p, 9,t), REGNUM2(p,10,t), REGNUM2(p,11,t), \
18117   REGNUM2(p,12,t), REGNUM2(p,13,t), REGNUM2(p,14,t), REGNUM2(p,15,t)
18118 #define SPLRBANK(base,bank,t) \
18119   REGDEF(lr_##bank, 768|((base+0)<<16), t), \
18120   REGDEF(sp_##bank, 768|((base+1)<<16), t), \
18121   REGDEF(spsr_##bank, 768|(base<<16)|SPSR_BIT, t), \
18122   REGDEF(LR_##bank, 768|((base+0)<<16), t), \
18123   REGDEF(SP_##bank, 768|((base+1)<<16), t), \
18124   REGDEF(SPSR_##bank, 768|(base<<16)|SPSR_BIT, t)
18125
18126 static const struct reg_entry reg_names[] =
18127 {
18128   /* ARM integer registers.  */
18129   REGSET(r, RN), REGSET(R, RN),
18130
18131   /* ATPCS synonyms.  */
18132   REGDEF(a1,0,RN), REGDEF(a2,1,RN), REGDEF(a3, 2,RN), REGDEF(a4, 3,RN),
18133   REGDEF(v1,4,RN), REGDEF(v2,5,RN), REGDEF(v3, 6,RN), REGDEF(v4, 7,RN),
18134   REGDEF(v5,8,RN), REGDEF(v6,9,RN), REGDEF(v7,10,RN), REGDEF(v8,11,RN),
18135
18136   REGDEF(A1,0,RN), REGDEF(A2,1,RN), REGDEF(A3, 2,RN), REGDEF(A4, 3,RN),
18137   REGDEF(V1,4,RN), REGDEF(V2,5,RN), REGDEF(V3, 6,RN), REGDEF(V4, 7,RN),
18138   REGDEF(V5,8,RN), REGDEF(V6,9,RN), REGDEF(V7,10,RN), REGDEF(V8,11,RN),
18139
18140   /* Well-known aliases.  */
18141   REGDEF(wr, 7,RN), REGDEF(sb, 9,RN), REGDEF(sl,10,RN), REGDEF(fp,11,RN),
18142   REGDEF(ip,12,RN), REGDEF(sp,13,RN), REGDEF(lr,14,RN), REGDEF(pc,15,RN),
18143
18144   REGDEF(WR, 7,RN), REGDEF(SB, 9,RN), REGDEF(SL,10,RN), REGDEF(FP,11,RN),
18145   REGDEF(IP,12,RN), REGDEF(SP,13,RN), REGDEF(LR,14,RN), REGDEF(PC,15,RN),
18146
18147   /* Coprocessor numbers.  */
18148   REGSET(p, CP), REGSET(P, CP),
18149
18150   /* Coprocessor register numbers.  The "cr" variants are for backward
18151      compatibility.  */
18152   REGSET(c,  CN), REGSET(C, CN),
18153   REGSET(cr, CN), REGSET(CR, CN),
18154
18155   /* ARM banked registers.  */
18156   REGDEF(R8_usr,512|(0<<16),RNB), REGDEF(r8_usr,512|(0<<16),RNB),
18157   REGDEF(R9_usr,512|(1<<16),RNB), REGDEF(r9_usr,512|(1<<16),RNB),
18158   REGDEF(R10_usr,512|(2<<16),RNB), REGDEF(r10_usr,512|(2<<16),RNB),
18159   REGDEF(R11_usr,512|(3<<16),RNB), REGDEF(r11_usr,512|(3<<16),RNB),
18160   REGDEF(R12_usr,512|(4<<16),RNB), REGDEF(r12_usr,512|(4<<16),RNB),
18161   REGDEF(SP_usr,512|(5<<16),RNB), REGDEF(sp_usr,512|(5<<16),RNB),
18162   REGDEF(LR_usr,512|(6<<16),RNB), REGDEF(lr_usr,512|(6<<16),RNB),
18163
18164   REGDEF(R8_fiq,512|(8<<16),RNB), REGDEF(r8_fiq,512|(8<<16),RNB),
18165   REGDEF(R9_fiq,512|(9<<16),RNB), REGDEF(r9_fiq,512|(9<<16),RNB),
18166   REGDEF(R10_fiq,512|(10<<16),RNB), REGDEF(r10_fiq,512|(10<<16),RNB),
18167   REGDEF(R11_fiq,512|(11<<16),RNB), REGDEF(r11_fiq,512|(11<<16),RNB),
18168   REGDEF(R12_fiq,512|(12<<16),RNB), REGDEF(r12_fiq,512|(12<<16),RNB),
18169   REGDEF(SP_fiq,512|(13<<16),RNB), REGDEF(sp_fiq,512|(13<<16),RNB),
18170   REGDEF(LR_fiq,512|(14<<16),RNB), REGDEF(lr_fiq,512|(14<<16),RNB),
18171   REGDEF(SPSR_fiq,512|(14<<16)|SPSR_BIT,RNB), REGDEF(spsr_fiq,512|(14<<16)|SPSR_BIT,RNB),
18172
18173   SPLRBANK(0,IRQ,RNB), SPLRBANK(0,irq,RNB),
18174   SPLRBANK(2,SVC,RNB), SPLRBANK(2,svc,RNB),
18175   SPLRBANK(4,ABT,RNB), SPLRBANK(4,abt,RNB),
18176   SPLRBANK(6,UND,RNB), SPLRBANK(6,und,RNB),
18177   SPLRBANK(12,MON,RNB), SPLRBANK(12,mon,RNB),
18178   REGDEF(elr_hyp,768|(14<<16),RNB), REGDEF(ELR_hyp,768|(14<<16),RNB),
18179   REGDEF(sp_hyp,768|(15<<16),RNB), REGDEF(SP_hyp,768|(15<<16),RNB),
18180   REGDEF(spsr_hyp,768|(14<<16)|SPSR_BIT,RNB),
18181   REGDEF(SPSR_hyp,768|(14<<16)|SPSR_BIT,RNB),
18182
18183   /* FPA registers.  */
18184   REGNUM(f,0,FN), REGNUM(f,1,FN), REGNUM(f,2,FN), REGNUM(f,3,FN),
18185   REGNUM(f,4,FN), REGNUM(f,5,FN), REGNUM(f,6,FN), REGNUM(f,7, FN),
18186
18187   REGNUM(F,0,FN), REGNUM(F,1,FN), REGNUM(F,2,FN), REGNUM(F,3,FN),
18188   REGNUM(F,4,FN), REGNUM(F,5,FN), REGNUM(F,6,FN), REGNUM(F,7, FN),
18189
18190   /* VFP SP registers.  */
18191   REGSET(s,VFS),  REGSET(S,VFS),
18192   REGSETH(s,VFS), REGSETH(S,VFS),
18193
18194   /* VFP DP Registers.  */
18195   REGSET(d,VFD),  REGSET(D,VFD),
18196   /* Extra Neon DP registers.  */
18197   REGSETH(d,VFD), REGSETH(D,VFD),
18198
18199   /* Neon QP registers.  */
18200   REGSET2(q,NQ),  REGSET2(Q,NQ),
18201
18202   /* VFP control registers.  */
18203   REGDEF(fpsid,0,VFC), REGDEF(fpscr,1,VFC), REGDEF(fpexc,8,VFC),
18204   REGDEF(FPSID,0,VFC), REGDEF(FPSCR,1,VFC), REGDEF(FPEXC,8,VFC),
18205   REGDEF(fpinst,9,VFC), REGDEF(fpinst2,10,VFC),
18206   REGDEF(FPINST,9,VFC), REGDEF(FPINST2,10,VFC),
18207   REGDEF(mvfr0,7,VFC), REGDEF(mvfr1,6,VFC),
18208   REGDEF(MVFR0,7,VFC), REGDEF(MVFR1,6,VFC),
18209
18210   /* Maverick DSP coprocessor registers.  */
18211   REGSET(mvf,MVF),  REGSET(mvd,MVD),  REGSET(mvfx,MVFX),  REGSET(mvdx,MVDX),
18212   REGSET(MVF,MVF),  REGSET(MVD,MVD),  REGSET(MVFX,MVFX),  REGSET(MVDX,MVDX),
18213
18214   REGNUM(mvax,0,MVAX), REGNUM(mvax,1,MVAX),
18215   REGNUM(mvax,2,MVAX), REGNUM(mvax,3,MVAX),
18216   REGDEF(dspsc,0,DSPSC),
18217
18218   REGNUM(MVAX,0,MVAX), REGNUM(MVAX,1,MVAX),
18219   REGNUM(MVAX,2,MVAX), REGNUM(MVAX,3,MVAX),
18220   REGDEF(DSPSC,0,DSPSC),
18221
18222   /* iWMMXt data registers - p0, c0-15.  */
18223   REGSET(wr,MMXWR), REGSET(wR,MMXWR), REGSET(WR, MMXWR),
18224
18225   /* iWMMXt control registers - p1, c0-3.  */
18226   REGDEF(wcid,  0,MMXWC),  REGDEF(wCID,  0,MMXWC),  REGDEF(WCID,  0,MMXWC),
18227   REGDEF(wcon,  1,MMXWC),  REGDEF(wCon,  1,MMXWC),  REGDEF(WCON,  1,MMXWC),
18228   REGDEF(wcssf, 2,MMXWC),  REGDEF(wCSSF, 2,MMXWC),  REGDEF(WCSSF, 2,MMXWC),
18229   REGDEF(wcasf, 3,MMXWC),  REGDEF(wCASF, 3,MMXWC),  REGDEF(WCASF, 3,MMXWC),
18230
18231   /* iWMMXt scalar (constant/offset) registers - p1, c8-11.  */
18232   REGDEF(wcgr0, 8,MMXWCG),  REGDEF(wCGR0, 8,MMXWCG),  REGDEF(WCGR0, 8,MMXWCG),
18233   REGDEF(wcgr1, 9,MMXWCG),  REGDEF(wCGR1, 9,MMXWCG),  REGDEF(WCGR1, 9,MMXWCG),
18234   REGDEF(wcgr2,10,MMXWCG),  REGDEF(wCGR2,10,MMXWCG),  REGDEF(WCGR2,10,MMXWCG),
18235   REGDEF(wcgr3,11,MMXWCG),  REGDEF(wCGR3,11,MMXWCG),  REGDEF(WCGR3,11,MMXWCG),
18236
18237   /* XScale accumulator registers.  */
18238   REGNUM(acc,0,XSCALE), REGNUM(ACC,0,XSCALE),
18239 };
18240 #undef REGDEF
18241 #undef REGNUM
18242 #undef REGSET
18243
18244 /* Table of all PSR suffixes.  Bare "CPSR" and "SPSR" are handled
18245    within psr_required_here.  */
18246 static const struct asm_psr psrs[] =
18247 {
18248   /* Backward compatibility notation.  Note that "all" is no longer
18249      truly all possible PSR bits.  */
18250   {"all",  PSR_c | PSR_f},
18251   {"flg",  PSR_f},
18252   {"ctl",  PSR_c},
18253
18254   /* Individual flags.  */
18255   {"f",    PSR_f},
18256   {"c",    PSR_c},
18257   {"x",    PSR_x},
18258   {"s",    PSR_s},
18259
18260   /* Combinations of flags.  */
18261   {"fs",   PSR_f | PSR_s},
18262   {"fx",   PSR_f | PSR_x},
18263   {"fc",   PSR_f | PSR_c},
18264   {"sf",   PSR_s | PSR_f},
18265   {"sx",   PSR_s | PSR_x},
18266   {"sc",   PSR_s | PSR_c},
18267   {"xf",   PSR_x | PSR_f},
18268   {"xs",   PSR_x | PSR_s},
18269   {"xc",   PSR_x | PSR_c},
18270   {"cf",   PSR_c | PSR_f},
18271   {"cs",   PSR_c | PSR_s},
18272   {"cx",   PSR_c | PSR_x},
18273   {"fsx",  PSR_f | PSR_s | PSR_x},
18274   {"fsc",  PSR_f | PSR_s | PSR_c},
18275   {"fxs",  PSR_f | PSR_x | PSR_s},
18276   {"fxc",  PSR_f | PSR_x | PSR_c},
18277   {"fcs",  PSR_f | PSR_c | PSR_s},
18278   {"fcx",  PSR_f | PSR_c | PSR_x},
18279   {"sfx",  PSR_s | PSR_f | PSR_x},
18280   {"sfc",  PSR_s | PSR_f | PSR_c},
18281   {"sxf",  PSR_s | PSR_x | PSR_f},
18282   {"sxc",  PSR_s | PSR_x | PSR_c},
18283   {"scf",  PSR_s | PSR_c | PSR_f},
18284   {"scx",  PSR_s | PSR_c | PSR_x},
18285   {"xfs",  PSR_x | PSR_f | PSR_s},
18286   {"xfc",  PSR_x | PSR_f | PSR_c},
18287   {"xsf",  PSR_x | PSR_s | PSR_f},
18288   {"xsc",  PSR_x | PSR_s | PSR_c},
18289   {"xcf",  PSR_x | PSR_c | PSR_f},
18290   {"xcs",  PSR_x | PSR_c | PSR_s},
18291   {"cfs",  PSR_c | PSR_f | PSR_s},
18292   {"cfx",  PSR_c | PSR_f | PSR_x},
18293   {"csf",  PSR_c | PSR_s | PSR_f},
18294   {"csx",  PSR_c | PSR_s | PSR_x},
18295   {"cxf",  PSR_c | PSR_x | PSR_f},
18296   {"cxs",  PSR_c | PSR_x | PSR_s},
18297   {"fsxc", PSR_f | PSR_s | PSR_x | PSR_c},
18298   {"fscx", PSR_f | PSR_s | PSR_c | PSR_x},
18299   {"fxsc", PSR_f | PSR_x | PSR_s | PSR_c},
18300   {"fxcs", PSR_f | PSR_x | PSR_c | PSR_s},
18301   {"fcsx", PSR_f | PSR_c | PSR_s | PSR_x},
18302   {"fcxs", PSR_f | PSR_c | PSR_x | PSR_s},
18303   {"sfxc", PSR_s | PSR_f | PSR_x | PSR_c},
18304   {"sfcx", PSR_s | PSR_f | PSR_c | PSR_x},
18305   {"sxfc", PSR_s | PSR_x | PSR_f | PSR_c},
18306   {"sxcf", PSR_s | PSR_x | PSR_c | PSR_f},
18307   {"scfx", PSR_s | PSR_c | PSR_f | PSR_x},
18308   {"scxf", PSR_s | PSR_c | PSR_x | PSR_f},
18309   {"xfsc", PSR_x | PSR_f | PSR_s | PSR_c},
18310   {"xfcs", PSR_x | PSR_f | PSR_c | PSR_s},
18311   {"xsfc", PSR_x | PSR_s | PSR_f | PSR_c},
18312   {"xscf", PSR_x | PSR_s | PSR_c | PSR_f},
18313   {"xcfs", PSR_x | PSR_c | PSR_f | PSR_s},
18314   {"xcsf", PSR_x | PSR_c | PSR_s | PSR_f},
18315   {"cfsx", PSR_c | PSR_f | PSR_s | PSR_x},
18316   {"cfxs", PSR_c | PSR_f | PSR_x | PSR_s},
18317   {"csfx", PSR_c | PSR_s | PSR_f | PSR_x},
18318   {"csxf", PSR_c | PSR_s | PSR_x | PSR_f},
18319   {"cxfs", PSR_c | PSR_x | PSR_f | PSR_s},
18320   {"cxsf", PSR_c | PSR_x | PSR_s | PSR_f},
18321 };
18322
18323 /* Table of V7M psr names.  */
18324 static const struct asm_psr v7m_psrs[] =
18325 {
18326   {"apsr",        0 }, {"APSR",         0 },
18327   {"iapsr",       1 }, {"IAPSR",        1 },
18328   {"eapsr",       2 }, {"EAPSR",        2 },
18329   {"psr",         3 }, {"PSR",          3 },
18330   {"xpsr",        3 }, {"XPSR",         3 }, {"xPSR",     3 },
18331   {"ipsr",        5 }, {"IPSR",         5 },
18332   {"epsr",        6 }, {"EPSR",         6 },
18333   {"iepsr",       7 }, {"IEPSR",        7 },
18334   {"msp",         8 }, {"MSP",          8 },
18335   {"psp",         9 }, {"PSP",          9 },
18336   {"primask",     16}, {"PRIMASK",      16},
18337   {"basepri",     17}, {"BASEPRI",      17},
18338   {"basepri_max", 18}, {"BASEPRI_MAX",  18},
18339   {"basepri_max", 18}, {"BASEPRI_MASK", 18}, /* Typo, preserved for backwards compatibility.  */
18340   {"faultmask",   19}, {"FAULTMASK",    19},
18341   {"control",     20}, {"CONTROL",      20}
18342 };
18343
18344 /* Table of all shift-in-operand names.  */
18345 static const struct asm_shift_name shift_names [] =
18346 {
18347   { "asl", SHIFT_LSL },  { "ASL", SHIFT_LSL },
18348   { "lsl", SHIFT_LSL },  { "LSL", SHIFT_LSL },
18349   { "lsr", SHIFT_LSR },  { "LSR", SHIFT_LSR },
18350   { "asr", SHIFT_ASR },  { "ASR", SHIFT_ASR },
18351   { "ror", SHIFT_ROR },  { "ROR", SHIFT_ROR },
18352   { "rrx", SHIFT_RRX },  { "RRX", SHIFT_RRX }
18353 };
18354
18355 /* Table of all explicit relocation names.  */
18356 #ifdef OBJ_ELF
18357 static struct reloc_entry reloc_names[] =
18358 {
18359   { "got",     BFD_RELOC_ARM_GOT32   },  { "GOT",     BFD_RELOC_ARM_GOT32   },
18360   { "gotoff",  BFD_RELOC_ARM_GOTOFF  },  { "GOTOFF",  BFD_RELOC_ARM_GOTOFF  },
18361   { "plt",     BFD_RELOC_ARM_PLT32   },  { "PLT",     BFD_RELOC_ARM_PLT32   },
18362   { "target1", BFD_RELOC_ARM_TARGET1 },  { "TARGET1", BFD_RELOC_ARM_TARGET1 },
18363   { "target2", BFD_RELOC_ARM_TARGET2 },  { "TARGET2", BFD_RELOC_ARM_TARGET2 },
18364   { "sbrel",   BFD_RELOC_ARM_SBREL32 },  { "SBREL",   BFD_RELOC_ARM_SBREL32 },
18365   { "tlsgd",   BFD_RELOC_ARM_TLS_GD32},  { "TLSGD",   BFD_RELOC_ARM_TLS_GD32},
18366   { "tlsldm",  BFD_RELOC_ARM_TLS_LDM32}, { "TLSLDM",  BFD_RELOC_ARM_TLS_LDM32},
18367   { "tlsldo",  BFD_RELOC_ARM_TLS_LDO32}, { "TLSLDO",  BFD_RELOC_ARM_TLS_LDO32},
18368   { "gottpoff",BFD_RELOC_ARM_TLS_IE32},  { "GOTTPOFF",BFD_RELOC_ARM_TLS_IE32},
18369   { "tpoff",   BFD_RELOC_ARM_TLS_LE32},  { "TPOFF",   BFD_RELOC_ARM_TLS_LE32},
18370   { "got_prel", BFD_RELOC_ARM_GOT_PREL}, { "GOT_PREL", BFD_RELOC_ARM_GOT_PREL},
18371   { "tlsdesc", BFD_RELOC_ARM_TLS_GOTDESC},
18372         { "TLSDESC", BFD_RELOC_ARM_TLS_GOTDESC},
18373   { "tlscall", BFD_RELOC_ARM_TLS_CALL},
18374         { "TLSCALL", BFD_RELOC_ARM_TLS_CALL},
18375   { "tlsdescseq", BFD_RELOC_ARM_TLS_DESCSEQ},
18376         { "TLSDESCSEQ", BFD_RELOC_ARM_TLS_DESCSEQ}
18377 };
18378 #endif
18379
18380 /* Table of all conditional affixes.  0xF is not defined as a condition code.  */
18381 static const struct asm_cond conds[] =
18382 {
18383   {"eq", 0x0},
18384   {"ne", 0x1},
18385   {"cs", 0x2}, {"hs", 0x2},
18386   {"cc", 0x3}, {"ul", 0x3}, {"lo", 0x3},
18387   {"mi", 0x4},
18388   {"pl", 0x5},
18389   {"vs", 0x6},
18390   {"vc", 0x7},
18391   {"hi", 0x8},
18392   {"ls", 0x9},
18393   {"ge", 0xa},
18394   {"lt", 0xb},
18395   {"gt", 0xc},
18396   {"le", 0xd},
18397   {"al", 0xe}
18398 };
18399
18400 #define UL_BARRIER(L,U,CODE,FEAT) \
18401   { L, CODE, ARM_FEATURE_CORE_LOW (FEAT) }, \
18402   { U, CODE, ARM_FEATURE_CORE_LOW (FEAT) }
18403
18404 static struct asm_barrier_opt barrier_opt_names[] =
18405 {
18406   UL_BARRIER ("sy",     "SY",    0xf, ARM_EXT_BARRIER),
18407   UL_BARRIER ("st",     "ST",    0xe, ARM_EXT_BARRIER),
18408   UL_BARRIER ("ld",     "LD",    0xd, ARM_EXT_V8),
18409   UL_BARRIER ("ish",    "ISH",   0xb, ARM_EXT_BARRIER),
18410   UL_BARRIER ("sh",     "SH",    0xb, ARM_EXT_BARRIER),
18411   UL_BARRIER ("ishst",  "ISHST", 0xa, ARM_EXT_BARRIER),
18412   UL_BARRIER ("shst",   "SHST",  0xa, ARM_EXT_BARRIER),
18413   UL_BARRIER ("ishld",  "ISHLD", 0x9, ARM_EXT_V8),
18414   UL_BARRIER ("un",     "UN",    0x7, ARM_EXT_BARRIER),
18415   UL_BARRIER ("nsh",    "NSH",   0x7, ARM_EXT_BARRIER),
18416   UL_BARRIER ("unst",   "UNST",  0x6, ARM_EXT_BARRIER),
18417   UL_BARRIER ("nshst",  "NSHST", 0x6, ARM_EXT_BARRIER),
18418   UL_BARRIER ("nshld",  "NSHLD", 0x5, ARM_EXT_V8),
18419   UL_BARRIER ("osh",    "OSH",   0x3, ARM_EXT_BARRIER),
18420   UL_BARRIER ("oshst",  "OSHST", 0x2, ARM_EXT_BARRIER),
18421   UL_BARRIER ("oshld",  "OSHLD", 0x1, ARM_EXT_V8)
18422 };
18423
18424 #undef UL_BARRIER
18425
18426 /* Table of ARM-format instructions.    */
18427
18428 /* Macros for gluing together operand strings.  N.B. In all cases
18429    other than OPS0, the trailing OP_stop comes from default
18430    zero-initialization of the unspecified elements of the array.  */
18431 #define OPS0()            { OP_stop, }
18432 #define OPS1(a)           { OP_##a, }
18433 #define OPS2(a,b)         { OP_##a,OP_##b, }
18434 #define OPS3(a,b,c)       { OP_##a,OP_##b,OP_##c, }
18435 #define OPS4(a,b,c,d)     { OP_##a,OP_##b,OP_##c,OP_##d, }
18436 #define OPS5(a,b,c,d,e)   { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e, }
18437 #define OPS6(a,b,c,d,e,f) { OP_##a,OP_##b,OP_##c,OP_##d,OP_##e,OP_##f, }
18438
18439 /* These macros are similar to the OPSn, but do not prepend the OP_ prefix.
18440    This is useful when mixing operands for ARM and THUMB, i.e. using the
18441    MIX_ARM_THUMB_OPERANDS macro.
18442    In order to use these macros, prefix the number of operands with _
18443    e.g. _3.  */
18444 #define OPS_1(a)           { a, }
18445 #define OPS_2(a,b)         { a,b, }
18446 #define OPS_3(a,b,c)       { a,b,c, }
18447 #define OPS_4(a,b,c,d)     { a,b,c,d, }
18448 #define OPS_5(a,b,c,d,e)   { a,b,c,d,e, }
18449 #define OPS_6(a,b,c,d,e,f) { a,b,c,d,e,f, }
18450
18451 /* These macros abstract out the exact format of the mnemonic table and
18452    save some repeated characters.  */
18453
18454 /* The normal sort of mnemonic; has a Thumb variant; takes a conditional suffix.  */
18455 #define TxCE(mnem, op, top, nops, ops, ae, te) \
18456   { mnem, OPS##nops ops, OT_csuffix, 0x##op, top, ARM_VARIANT, \
18457     THUMB_VARIANT, do_##ae, do_##te }
18458
18459 /* Two variants of the above - TCE for a numeric Thumb opcode, tCE for
18460    a T_MNEM_xyz enumerator.  */
18461 #define TCE(mnem, aop, top, nops, ops, ae, te) \
18462       TxCE (mnem, aop, 0x##top, nops, ops, ae, te)
18463 #define tCE(mnem, aop, top, nops, ops, ae, te) \
18464       TxCE (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18465
18466 /* Second most common sort of mnemonic: has a Thumb variant, takes a conditional
18467    infix after the third character.  */
18468 #define TxC3(mnem, op, top, nops, ops, ae, te) \
18469   { mnem, OPS##nops ops, OT_cinfix3, 0x##op, top, ARM_VARIANT, \
18470     THUMB_VARIANT, do_##ae, do_##te }
18471 #define TxC3w(mnem, op, top, nops, ops, ae, te) \
18472   { mnem, OPS##nops ops, OT_cinfix3_deprecated, 0x##op, top, ARM_VARIANT, \
18473     THUMB_VARIANT, do_##ae, do_##te }
18474 #define TC3(mnem, aop, top, nops, ops, ae, te) \
18475       TxC3 (mnem, aop, 0x##top, nops, ops, ae, te)
18476 #define TC3w(mnem, aop, top, nops, ops, ae, te) \
18477       TxC3w (mnem, aop, 0x##top, nops, ops, ae, te)
18478 #define tC3(mnem, aop, top, nops, ops, ae, te) \
18479       TxC3 (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18480 #define tC3w(mnem, aop, top, nops, ops, ae, te) \
18481       TxC3w (mnem, aop, T_MNEM##top, nops, ops, ae, te)
18482
18483 /* Mnemonic that cannot be conditionalized.  The ARM condition-code
18484    field is still 0xE.  Many of the Thumb variants can be executed
18485    conditionally, so this is checked separately.  */
18486 #define TUE(mnem, op, top, nops, ops, ae, te)                           \
18487   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18488     THUMB_VARIANT, do_##ae, do_##te }
18489
18490 /* Same as TUE but the encoding function for ARM and Thumb modes is the same.
18491    Used by mnemonics that have very minimal differences in the encoding for
18492    ARM and Thumb variants and can be handled in a common function.  */
18493 #define TUEc(mnem, op, top, nops, ops, en) \
18494   { mnem, OPS##nops ops, OT_unconditional, 0x##op, 0x##top, ARM_VARIANT, \
18495     THUMB_VARIANT, do_##en, do_##en }
18496
18497 /* Mnemonic that cannot be conditionalized, and bears 0xF in its ARM
18498    condition code field.  */
18499 #define TUF(mnem, op, top, nops, ops, ae, te)                           \
18500   { mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##top, ARM_VARIANT, \
18501     THUMB_VARIANT, do_##ae, do_##te }
18502
18503 /* ARM-only variants of all the above.  */
18504 #define CE(mnem,  op, nops, ops, ae)    \
18505   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18506
18507 #define C3(mnem, op, nops, ops, ae)     \
18508   { #mnem, OPS##nops ops, OT_cinfix3, 0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18509
18510 /* Legacy mnemonics that always have conditional infix after the third
18511    character.  */
18512 #define CL(mnem, op, nops, ops, ae)     \
18513   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
18514     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18515
18516 /* Coprocessor instructions.  Isomorphic between Arm and Thumb-2.  */
18517 #define cCE(mnem,  op, nops, ops, ae)   \
18518   { mnem, OPS##nops ops, OT_csuffix, 0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18519
18520 /* Legacy coprocessor instructions where conditional infix and conditional
18521    suffix are ambiguous.  For consistency this includes all FPA instructions,
18522    not just the potentially ambiguous ones.  */
18523 #define cCL(mnem, op, nops, ops, ae)    \
18524   { mnem, OPS##nops ops, OT_cinfix3_legacy, \
18525     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18526
18527 /* Coprocessor, takes either a suffix or a position-3 infix
18528    (for an FPA corner case). */
18529 #define C3E(mnem, op, nops, ops, ae) \
18530   { mnem, OPS##nops ops, OT_csuf_or_in3, \
18531     0x##op, 0xe##op, ARM_VARIANT, ARM_VARIANT, do_##ae, do_##ae }
18532
18533 #define xCM_(m1, m2, m3, op, nops, ops, ae)     \
18534   { m1 #m2 m3, OPS##nops ops, \
18535     sizeof (#m2) == 1 ? OT_odd_infix_unc : OT_odd_infix_0 + sizeof (m1) - 1, \
18536     0x##op, 0x0, ARM_VARIANT, 0, do_##ae, NULL }
18537
18538 #define CM(m1, m2, op, nops, ops, ae)   \
18539   xCM_ (m1,   , m2, op, nops, ops, ae), \
18540   xCM_ (m1, eq, m2, op, nops, ops, ae), \
18541   xCM_ (m1, ne, m2, op, nops, ops, ae), \
18542   xCM_ (m1, cs, m2, op, nops, ops, ae), \
18543   xCM_ (m1, hs, m2, op, nops, ops, ae), \
18544   xCM_ (m1, cc, m2, op, nops, ops, ae), \
18545   xCM_ (m1, ul, m2, op, nops, ops, ae), \
18546   xCM_ (m1, lo, m2, op, nops, ops, ae), \
18547   xCM_ (m1, mi, m2, op, nops, ops, ae), \
18548   xCM_ (m1, pl, m2, op, nops, ops, ae), \
18549   xCM_ (m1, vs, m2, op, nops, ops, ae), \
18550   xCM_ (m1, vc, m2, op, nops, ops, ae), \
18551   xCM_ (m1, hi, m2, op, nops, ops, ae), \
18552   xCM_ (m1, ls, m2, op, nops, ops, ae), \
18553   xCM_ (m1, ge, m2, op, nops, ops, ae), \
18554   xCM_ (m1, lt, m2, op, nops, ops, ae), \
18555   xCM_ (m1, gt, m2, op, nops, ops, ae), \
18556   xCM_ (m1, le, m2, op, nops, ops, ae), \
18557   xCM_ (m1, al, m2, op, nops, ops, ae)
18558
18559 #define UE(mnem, op, nops, ops, ae)     \
18560   { #mnem, OPS##nops ops, OT_unconditional, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
18561
18562 #define UF(mnem, op, nops, ops, ae)     \
18563   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0, ARM_VARIANT, 0, do_##ae, NULL }
18564
18565 /* Neon data-processing. ARM versions are unconditional with cond=0xf.
18566    The Thumb and ARM variants are mostly the same (bits 0-23 and 24/28), so we
18567    use the same encoding function for each.  */
18568 #define NUF(mnem, op, nops, ops, enc)                                   \
18569   { #mnem, OPS##nops ops, OT_unconditionalF, 0x##op, 0x##op,            \
18570     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18571
18572 /* Neon data processing, version which indirects through neon_enc_tab for
18573    the various overloaded versions of opcodes.  */
18574 #define nUF(mnem, op, nops, ops, enc)                                   \
18575   { #mnem, OPS##nops ops, OT_unconditionalF, N_MNEM##op, N_MNEM##op,    \
18576     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18577
18578 /* Neon insn with conditional suffix for the ARM version, non-overloaded
18579    version.  */
18580 #define NCE_tag(mnem, op, nops, ops, enc, tag)                          \
18581   { #mnem, OPS##nops ops, tag, 0x##op, 0x##op, ARM_VARIANT,             \
18582     THUMB_VARIANT, do_##enc, do_##enc }
18583
18584 #define NCE(mnem, op, nops, ops, enc)                                   \
18585    NCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
18586
18587 #define NCEF(mnem, op, nops, ops, enc)                                  \
18588     NCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
18589
18590 /* Neon insn with conditional suffix for the ARM version, overloaded types.  */
18591 #define nCE_tag(mnem, op, nops, ops, enc, tag)                          \
18592   { #mnem, OPS##nops ops, tag, N_MNEM##op, N_MNEM##op,          \
18593     ARM_VARIANT, THUMB_VARIANT, do_##enc, do_##enc }
18594
18595 #define nCE(mnem, op, nops, ops, enc)                                   \
18596    nCE_tag (mnem, op, nops, ops, enc, OT_csuffix)
18597
18598 #define nCEF(mnem, op, nops, ops, enc)                                  \
18599     nCE_tag (mnem, op, nops, ops, enc, OT_csuffixF)
18600
18601 #define do_0 0
18602
18603 static const struct asm_opcode insns[] =
18604 {
18605 #define ARM_VARIANT    & arm_ext_v1 /* Core ARM Instructions.  */
18606 #define THUMB_VARIANT  & arm_ext_v4t
18607  tCE("and",     0000000, _and,     3, (RR, oRR, SH), arit, t_arit3c),
18608  tC3("ands",    0100000, _ands,    3, (RR, oRR, SH), arit, t_arit3c),
18609  tCE("eor",     0200000, _eor,     3, (RR, oRR, SH), arit, t_arit3c),
18610  tC3("eors",    0300000, _eors,    3, (RR, oRR, SH), arit, t_arit3c),
18611  tCE("sub",     0400000, _sub,     3, (RR, oRR, SH), arit, t_add_sub),
18612  tC3("subs",    0500000, _subs,    3, (RR, oRR, SH), arit, t_add_sub),
18613  tCE("add",     0800000, _add,     3, (RR, oRR, SHG), arit, t_add_sub),
18614  tC3("adds",    0900000, _adds,    3, (RR, oRR, SHG), arit, t_add_sub),
18615  tCE("adc",     0a00000, _adc,     3, (RR, oRR, SH), arit, t_arit3c),
18616  tC3("adcs",    0b00000, _adcs,    3, (RR, oRR, SH), arit, t_arit3c),
18617  tCE("sbc",     0c00000, _sbc,     3, (RR, oRR, SH), arit, t_arit3),
18618  tC3("sbcs",    0d00000, _sbcs,    3, (RR, oRR, SH), arit, t_arit3),
18619  tCE("orr",     1800000, _orr,     3, (RR, oRR, SH), arit, t_arit3c),
18620  tC3("orrs",    1900000, _orrs,    3, (RR, oRR, SH), arit, t_arit3c),
18621  tCE("bic",     1c00000, _bic,     3, (RR, oRR, SH), arit, t_arit3),
18622  tC3("bics",    1d00000, _bics,    3, (RR, oRR, SH), arit, t_arit3),
18623
18624  /* The p-variants of tst/cmp/cmn/teq (below) are the pre-V6 mechanism
18625     for setting PSR flag bits.  They are obsolete in V6 and do not
18626     have Thumb equivalents. */
18627  tCE("tst",     1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
18628  tC3w("tsts",   1100000, _tst,     2, (RR, SH),      cmp,  t_mvn_tst),
18629   CL("tstp",    110f000,           2, (RR, SH),      cmp),
18630  tCE("cmp",     1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
18631  tC3w("cmps",   1500000, _cmp,     2, (RR, SH),      cmp,  t_mov_cmp),
18632   CL("cmpp",    150f000,           2, (RR, SH),      cmp),
18633  tCE("cmn",     1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
18634  tC3w("cmns",   1700000, _cmn,     2, (RR, SH),      cmp,  t_mvn_tst),
18635   CL("cmnp",    170f000,           2, (RR, SH),      cmp),
18636
18637  tCE("mov",     1a00000, _mov,     2, (RR, SH),      mov,  t_mov_cmp),
18638  tC3("movs",    1b00000, _movs,    2, (RR, SHG),     mov,  t_mov_cmp),
18639  tCE("mvn",     1e00000, _mvn,     2, (RR, SH),      mov,  t_mvn_tst),
18640  tC3("mvns",    1f00000, _mvns,    2, (RR, SH),      mov,  t_mvn_tst),
18641
18642  tCE("ldr",     4100000, _ldr,     2, (RR, ADDRGLDR),ldst, t_ldst),
18643  tC3("ldrb",    4500000, _ldrb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
18644  tCE("str",     4000000, _str,     _2, (MIX_ARM_THUMB_OPERANDS (OP_RR,
18645                                                                 OP_RRnpc),
18646                                         OP_ADDRGLDR),ldst, t_ldst),
18647  tC3("strb",    4400000, _strb,    2, (RRnpc_npcsp, ADDRGLDR),ldst, t_ldst),
18648
18649  tCE("stm",     8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18650  tC3("stmia",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18651  tC3("stmea",   8800000, _stmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18652  tCE("ldm",     8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18653  tC3("ldmia",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18654  tC3("ldmfd",   8900000, _ldmia,    2, (RRw, REGLST), ldmstm, t_ldmstm),
18655
18656  TCE("swi",     f000000, df00,     1, (EXPi),        swi, t_swi),
18657  TCE("svc",     f000000, df00,     1, (EXPi),        swi, t_swi),
18658  tCE("b",       a000000, _b,       1, (EXPr),        branch, t_branch),
18659  TCE("bl",      b000000, f000f800, 1, (EXPr),        bl, t_branch23),
18660
18661   /* Pseudo ops.  */
18662  tCE("adr",     28f0000, _adr,     2, (RR, EXP),     adr,  t_adr),
18663   C3(adrl,      28f0000,           2, (RR, EXP),     adrl),
18664  tCE("nop",     1a00000, _nop,     1, (oI255c),      nop,  t_nop),
18665  tCE("udf",     7f000f0, _udf,     1, (oIffffb),     bkpt, t_udf),
18666
18667   /* Thumb-compatibility pseudo ops.  */
18668  tCE("lsl",     1a00000, _lsl,     3, (RR, oRR, SH), shift, t_shift),
18669  tC3("lsls",    1b00000, _lsls,    3, (RR, oRR, SH), shift, t_shift),
18670  tCE("lsr",     1a00020, _lsr,     3, (RR, oRR, SH), shift, t_shift),
18671  tC3("lsrs",    1b00020, _lsrs,    3, (RR, oRR, SH), shift, t_shift),
18672  tCE("asr",     1a00040, _asr,     3, (RR, oRR, SH), shift, t_shift),
18673  tC3("asrs",      1b00040, _asrs,     3, (RR, oRR, SH), shift, t_shift),
18674  tCE("ror",     1a00060, _ror,     3, (RR, oRR, SH), shift, t_shift),
18675  tC3("rors",    1b00060, _rors,    3, (RR, oRR, SH), shift, t_shift),
18676  tCE("neg",     2600000, _neg,     2, (RR, RR),      rd_rn, t_neg),
18677  tC3("negs",    2700000, _negs,    2, (RR, RR),      rd_rn, t_neg),
18678  tCE("push",    92d0000, _push,     1, (REGLST),             push_pop, t_push_pop),
18679  tCE("pop",     8bd0000, _pop,     1, (REGLST),      push_pop, t_push_pop),
18680
18681  /* These may simplify to neg.  */
18682  TCE("rsb",     0600000, ebc00000, 3, (RR, oRR, SH), arit, t_rsb),
18683  TC3("rsbs",    0700000, ebd00000, 3, (RR, oRR, SH), arit, t_rsb),
18684
18685 #undef  THUMB_VARIANT
18686 #define THUMB_VARIANT  & arm_ext_v6
18687
18688  TCE("cpy",       1a00000, 4600,     2, (RR, RR),      rd_rm, t_cpy),
18689
18690  /* V1 instructions with no Thumb analogue prior to V6T2.  */
18691 #undef  THUMB_VARIANT
18692 #define THUMB_VARIANT  & arm_ext_v6t2
18693
18694  TCE("teq",     1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
18695  TC3w("teqs",   1300000, ea900f00, 2, (RR, SH),      cmp,  t_mvn_tst),
18696   CL("teqp",    130f000,           2, (RR, SH),      cmp),
18697
18698  TC3("ldrt",    4300000, f8500e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18699  TC3("ldrbt",   4700000, f8100e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18700  TC3("strt",    4200000, f8400e00, 2, (RR_npcsp, ADDR),   ldstt, t_ldstt),
18701  TC3("strbt",   4600000, f8000e00, 2, (RRnpc_npcsp, ADDR),ldstt, t_ldstt),
18702
18703  TC3("stmdb",   9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18704  TC3("stmfd",     9000000, e9000000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18705
18706  TC3("ldmdb",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18707  TC3("ldmea",   9100000, e9100000, 2, (RRw, REGLST), ldmstm, t_ldmstm),
18708
18709  /* V1 instructions with no Thumb analogue at all.  */
18710   CE("rsc",     0e00000,           3, (RR, oRR, SH), arit),
18711   C3(rscs,      0f00000,           3, (RR, oRR, SH), arit),
18712
18713   C3(stmib,     9800000,           2, (RRw, REGLST), ldmstm),
18714   C3(stmfa,     9800000,           2, (RRw, REGLST), ldmstm),
18715   C3(stmda,     8000000,           2, (RRw, REGLST), ldmstm),
18716   C3(stmed,     8000000,           2, (RRw, REGLST), ldmstm),
18717   C3(ldmib,     9900000,           2, (RRw, REGLST), ldmstm),
18718   C3(ldmed,     9900000,           2, (RRw, REGLST), ldmstm),
18719   C3(ldmda,     8100000,           2, (RRw, REGLST), ldmstm),
18720   C3(ldmfa,     8100000,           2, (RRw, REGLST), ldmstm),
18721
18722 #undef  ARM_VARIANT
18723 #define ARM_VARIANT    & arm_ext_v2     /* ARM 2 - multiplies.  */
18724 #undef  THUMB_VARIANT
18725 #define THUMB_VARIANT  & arm_ext_v4t
18726
18727  tCE("mul",     0000090, _mul,     3, (RRnpc, RRnpc, oRR), mul, t_mul),
18728  tC3("muls",    0100090, _muls,    3, (RRnpc, RRnpc, oRR), mul, t_mul),
18729
18730 #undef  THUMB_VARIANT
18731 #define THUMB_VARIANT  & arm_ext_v6t2
18732
18733  TCE("mla",     0200090, fb000000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
18734   C3(mlas,      0300090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas),
18735
18736   /* Generic coprocessor instructions.  */
18737  TCE("cdp",     e000000, ee000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18738  TCE("ldc",     c100000, ec100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18739  TC3("ldcl",    c500000, ec500000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18740  TCE("stc",     c000000, ec000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18741  TC3("stcl",    c400000, ec400000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18742  TCE("mcr",     e000010, ee000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18743  TCE("mrc",     e100010, ee100010, 6, (RCP, I7b, APSR_RR, RCN, RCN, oI7b),   co_reg, co_reg),
18744
18745 #undef  ARM_VARIANT
18746 #define ARM_VARIANT  & arm_ext_v2s /* ARM 3 - swp instructions.  */
18747
18748   CE("swp",     1000090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18749   C3(swpb,      1400090,           3, (RRnpc, RRnpc, RRnpcb), rd_rm_rn),
18750
18751 #undef  ARM_VARIANT
18752 #define ARM_VARIANT    & arm_ext_v3     /* ARM 6 Status register instructions.  */
18753 #undef  THUMB_VARIANT
18754 #define THUMB_VARIANT  & arm_ext_msr
18755
18756  TCE("mrs",     1000000, f3e08000, 2, (RRnpc, rPSR), mrs, t_mrs),
18757  TCE("msr",     120f000, f3808000, 2, (wPSR, RR_EXi), msr, t_msr),
18758
18759 #undef  ARM_VARIANT
18760 #define ARM_VARIANT    & arm_ext_v3m     /* ARM 7M long multiplies.  */
18761 #undef  THUMB_VARIANT
18762 #define THUMB_VARIANT  & arm_ext_v6t2
18763
18764  TCE("smull",   0c00090, fb800000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18765   CM("smull","s",       0d00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18766  TCE("umull",   0800090, fba00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18767   CM("umull","s",       0900090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18768  TCE("smlal",   0e00090, fbc00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18769   CM("smlal","s",       0f00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18770  TCE("umlal",   0a00090, fbe00000, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mull, t_mull),
18771   CM("umlal","s",       0b00090,           4, (RRnpc, RRnpc, RRnpc, RRnpc), mull),
18772
18773 #undef  ARM_VARIANT
18774 #define ARM_VARIANT    & arm_ext_v4     /* ARM Architecture 4.  */
18775 #undef  THUMB_VARIANT
18776 #define THUMB_VARIANT  & arm_ext_v4t
18777
18778  tC3("ldrh",    01000b0, _ldrh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18779  tC3("strh",    00000b0, _strh,     2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18780  tC3("ldrsh",   01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18781  tC3("ldrsb",   01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18782  tC3("ldsh",    01000f0, _ldrsh,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18783  tC3("ldsb",    01000d0, _ldrsb,    2, (RRnpc_npcsp, ADDRGLDRS), ldstv4, t_ldst),
18784
18785 #undef  ARM_VARIANT
18786 #define ARM_VARIANT  & arm_ext_v4t_5
18787
18788   /* ARM Architecture 4T.  */
18789   /* Note: bx (and blx) are required on V5, even if the processor does
18790      not support Thumb.  */
18791  TCE("bx",      12fff10, 4700, 1, (RR), bx, t_bx),
18792
18793 #undef  ARM_VARIANT
18794 #define ARM_VARIANT    & arm_ext_v5 /*  ARM Architecture 5T.     */
18795 #undef  THUMB_VARIANT
18796 #define THUMB_VARIANT  & arm_ext_v5t
18797
18798   /* Note: blx has 2 variants; the .value coded here is for
18799      BLX(2).  Only this variant has conditional execution.  */
18800  TCE("blx",     12fff30, 4780, 1, (RR_EXr),                         blx,  t_blx),
18801  TUE("bkpt",    1200070, be00, 1, (oIffffb),                        bkpt, t_bkpt),
18802
18803 #undef  THUMB_VARIANT
18804 #define THUMB_VARIANT  & arm_ext_v6t2
18805
18806  TCE("clz",     16f0f10, fab0f080, 2, (RRnpc, RRnpc),                   rd_rm,  t_clz),
18807  TUF("ldc2",    c100000, fc100000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18808  TUF("ldc2l",   c500000, fc500000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18809  TUF("stc2",    c000000, fc000000, 3, (RCP, RCN, ADDRGLDC),             lstc,   lstc),
18810  TUF("stc2l",   c400000, fc400000, 3, (RCP, RCN, ADDRGLDC),                     lstc,   lstc),
18811  TUF("cdp2",    e000000, fe000000, 6, (RCP, I15b, RCN, RCN, RCN, oI7b), cdp,    cdp),
18812  TUF("mcr2",    e000010, fe000010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18813  TUF("mrc2",    e100010, fe100010, 6, (RCP, I7b, RR, RCN, RCN, oI7b),   co_reg, co_reg),
18814
18815 #undef  ARM_VARIANT
18816 #define ARM_VARIANT    & arm_ext_v5exp /*  ARM Architecture 5TExP.  */
18817 #undef  THUMB_VARIANT
18818 #define THUMB_VARIANT  & arm_ext_v5exp
18819
18820  TCE("smlabb",  1000080, fb100000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18821  TCE("smlatb",  10000a0, fb100020, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18822  TCE("smlabt",  10000c0, fb100010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18823  TCE("smlatt",  10000e0, fb100030, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18824
18825  TCE("smlawb",  1200080, fb300000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18826  TCE("smlawt",  12000c0, fb300010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smla, t_mla),
18827
18828  TCE("smlalbb", 1400080, fbc00080, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18829  TCE("smlaltb", 14000a0, fbc000a0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18830  TCE("smlalbt", 14000c0, fbc00090, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18831  TCE("smlaltt", 14000e0, fbc000b0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),   smlal, t_mlal),
18832
18833  TCE("smulbb",  1600080, fb10f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18834  TCE("smultb",  16000a0, fb10f020, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18835  TCE("smulbt",  16000c0, fb10f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18836  TCE("smultt",  16000e0, fb10f030, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18837
18838  TCE("smulwb",  12000a0, fb30f000, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18839  TCE("smulwt",  12000e0, fb30f010, 3, (RRnpc, RRnpc, RRnpc),        smul, t_simd),
18840
18841  TCE("qadd",    1000050, fa80f080, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18842  TCE("qdadd",   1400050, fa80f090, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18843  TCE("qsub",    1200050, fa80f0a0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18844  TCE("qdsub",   1600050, fa80f0b0, 3, (RRnpc, RRnpc, RRnpc),        rd_rm_rn, t_simd2),
18845
18846 #undef  ARM_VARIANT
18847 #define ARM_VARIANT    & arm_ext_v5e /*  ARM Architecture 5TE.  */
18848 #undef  THUMB_VARIANT
18849 #define THUMB_VARIANT  & arm_ext_v6t2
18850
18851  TUF("pld",     450f000, f810f000, 1, (ADDR),                pld,  t_pld),
18852  TC3("ldrd",    00000d0, e8500000, 3, (RRnpc_npcsp, oRRnpc_npcsp, ADDRGLDRS),
18853      ldrd, t_ldstd),
18854  TC3("strd",    00000f0, e8400000, 3, (RRnpc_npcsp, oRRnpc_npcsp,
18855                                        ADDRGLDRS), ldrd, t_ldstd),
18856
18857  TCE("mcrr",    c400000, ec400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18858  TCE("mrrc",    c500000, ec500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18859
18860 #undef  ARM_VARIANT
18861 #define ARM_VARIANT  & arm_ext_v5j /*  ARM Architecture 5TEJ.  */
18862
18863  TCE("bxj",     12fff20, f3c08f00, 1, (RR),                       bxj, t_bxj),
18864
18865 #undef  ARM_VARIANT
18866 #define ARM_VARIANT    & arm_ext_v6 /*  ARM V6.  */
18867 #undef  THUMB_VARIANT
18868 #define THUMB_VARIANT  & arm_ext_v6
18869
18870  TUF("cpsie",     1080000, b660,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18871  TUF("cpsid",     10c0000, b670,     2, (CPSF, oI31b),              cpsi,   t_cpsi),
18872  tCE("rev",       6bf0f30, _rev,      2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18873  tCE("rev16",     6bf0fb0, _rev16,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18874  tCE("revsh",     6ff0fb0, _revsh,    2, (RRnpc, RRnpc),             rd_rm,  t_rev),
18875  tCE("sxth",      6bf0070, _sxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18876  tCE("uxth",      6ff0070, _uxth,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18877  tCE("sxtb",      6af0070, _sxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18878  tCE("uxtb",      6ef0070, _uxtb,     3, (RRnpc, RRnpc, oROR),       sxth,   t_sxth),
18879  TUF("setend",    1010000, b650,     1, (ENDI),                     setend, t_setend),
18880
18881 #undef  THUMB_VARIANT
18882 #define THUMB_VARIANT  & arm_ext_v6t2
18883
18884  TCE("ldrex",   1900f9f, e8500f00, 2, (RRnpc_npcsp, ADDR),        ldrex, t_ldrex),
18885  TCE("strex",   1800f90, e8400000, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
18886                                       strex,  t_strex),
18887  TUF("mcrr2",   c400000, fc400000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18888  TUF("mrrc2",   c500000, fc500000, 5, (RCP, I15b, RRnpc, RRnpc, RCN), co_reg2c, co_reg2c),
18889
18890  TCE("ssat",    6a00010, f3000000, 4, (RRnpc, I32, RRnpc, oSHllar),ssat,   t_ssat),
18891  TCE("usat",    6e00010, f3800000, 4, (RRnpc, I31, RRnpc, oSHllar),usat,   t_usat),
18892
18893 /*  ARM V6 not included in V7M.  */
18894 #undef  THUMB_VARIANT
18895 #define THUMB_VARIANT  & arm_ext_v6_notm
18896  TUF("rfeia",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18897  TUF("rfe",     8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18898   UF(rfeib,     9900a00,           1, (RRw),                       rfe),
18899   UF(rfeda,     8100a00,           1, (RRw),                       rfe),
18900  TUF("rfedb",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18901  TUF("rfefd",   8900a00, e990c000, 1, (RRw),                       rfe, rfe),
18902   UF(rfefa,     8100a00,           1, (RRw),                       rfe),
18903  TUF("rfeea",   9100a00, e810c000, 1, (RRw),                       rfe, rfe),
18904   UF(rfeed,     9900a00,           1, (RRw),                       rfe),
18905  TUF("srsia",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18906  TUF("srs",     8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18907  TUF("srsea",   8c00500, e980c000, 2, (oRRw, I31w),                srs,  srs),
18908   UF(srsib,     9c00500,           2, (oRRw, I31w),                srs),
18909   UF(srsfa,     9c00500,           2, (oRRw, I31w),                srs),
18910   UF(srsda,     8400500,           2, (oRRw, I31w),                srs),
18911   UF(srsed,     8400500,           2, (oRRw, I31w),                srs),
18912  TUF("srsdb",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18913  TUF("srsfd",   9400500, e800c000, 2, (oRRw, I31w),                srs,  srs),
18914  TUF("cps",     1020000, f3af8100, 1, (I31b),                     imm0, t_cps),
18915
18916 /*  ARM V6 not included in V7M (eg. integer SIMD).  */
18917 #undef  THUMB_VARIANT
18918 #define THUMB_VARIANT  & arm_ext_v6_dsp
18919  TCE("pkhbt",   6800010, eac00000, 4, (RRnpc, RRnpc, RRnpc, oSHll),   pkhbt, t_pkhbt),
18920  TCE("pkhtb",   6800050, eac00020, 4, (RRnpc, RRnpc, RRnpc, oSHar),   pkhtb, t_pkhtb),
18921  TCE("qadd16",  6200f10, fa90f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18922  TCE("qadd8",   6200f90, fa80f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18923  TCE("qasx",    6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18924  /* Old name for QASX.  */
18925  TCE("qaddsubx",6200f30, faa0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18926  TCE("qsax",    6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18927  /* Old name for QSAX.  */
18928  TCE("qsubaddx",6200f50, fae0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18929  TCE("qsub16",  6200f70, fad0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18930  TCE("qsub8",   6200ff0, fac0f010, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18931  TCE("sadd16",  6100f10, fa90f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18932  TCE("sadd8",   6100f90, fa80f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18933  TCE("sasx",    6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18934  /* Old name for SASX.  */
18935  TCE("saddsubx",6100f30, faa0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18936  TCE("shadd16", 6300f10, fa90f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18937  TCE("shadd8",  6300f90, fa80f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18938  TCE("shasx",   6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18939  /* Old name for SHASX.  */
18940  TCE("shaddsubx", 6300f30, faa0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18941  TCE("shsax",     6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18942  /* Old name for SHSAX.  */
18943  TCE("shsubaddx", 6300f50, fae0f020, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18944  TCE("shsub16", 6300f70, fad0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18945  TCE("shsub8",  6300ff0, fac0f020, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18946  TCE("ssax",    6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18947  /* Old name for SSAX.  */
18948  TCE("ssubaddx",6100f50, fae0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18949  TCE("ssub16",  6100f70, fad0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18950  TCE("ssub8",   6100ff0, fac0f000, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18951  TCE("uadd16",  6500f10, fa90f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18952  TCE("uadd8",   6500f90, fa80f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18953  TCE("uasx",    6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18954  /* Old name for UASX.  */
18955  TCE("uaddsubx",6500f30, faa0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18956  TCE("uhadd16", 6700f10, fa90f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18957  TCE("uhadd8",  6700f90, fa80f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18958  TCE("uhasx",   6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18959  /* Old name for UHASX.  */
18960  TCE("uhaddsubx", 6700f30, faa0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18961  TCE("uhsax",     6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18962  /* Old name for UHSAX.  */
18963  TCE("uhsubaddx", 6700f50, fae0f060, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18964  TCE("uhsub16", 6700f70, fad0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18965  TCE("uhsub8",  6700ff0, fac0f060, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18966  TCE("uqadd16", 6600f10, fa90f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18967  TCE("uqadd8",  6600f90, fa80f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18968  TCE("uqasx",   6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18969  /* Old name for UQASX.  */
18970  TCE("uqaddsubx", 6600f30, faa0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18971  TCE("uqsax",     6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18972  /* Old name for UQSAX.  */
18973  TCE("uqsubaddx", 6600f50, fae0f050, 3, (RRnpc, RRnpc, RRnpc),     rd_rn_rm, t_simd),
18974  TCE("uqsub16", 6600f70, fad0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18975  TCE("uqsub8",  6600ff0, fac0f050, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18976  TCE("usub16",  6500f70, fad0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18977  TCE("usax",    6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18978  /* Old name for USAX.  */
18979  TCE("usubaddx",6500f50, fae0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18980  TCE("usub8",   6500ff0, fac0f040, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18981  TCE("sxtah",   6b00070, fa00f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18982  TCE("sxtab16", 6800070, fa20f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18983  TCE("sxtab",   6a00070, fa40f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18984  TCE("sxtb16",  68f0070, fa2ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18985  TCE("uxtah",   6f00070, fa10f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18986  TCE("uxtab16", 6c00070, fa30f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18987  TCE("uxtab",   6e00070, fa50f080, 4, (RRnpc, RRnpc, RRnpc, oROR), sxtah, t_sxtah),
18988  TCE("uxtb16",  6cf0070, fa3ff080, 3, (RRnpc, RRnpc, oROR),        sxth,  t_sxth),
18989  TCE("sel",     6800fb0, faa0f080, 3, (RRnpc, RRnpc, RRnpc),       rd_rn_rm, t_simd),
18990  TCE("smlad",   7000010, fb200000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18991  TCE("smladx",  7000030, fb200010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18992  TCE("smlald",  7400010, fbc000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18993  TCE("smlaldx", 7400030, fbc000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18994  TCE("smlsd",   7000050, fb400000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18995  TCE("smlsdx",  7000070, fb400010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18996  TCE("smlsld",  7400050, fbd000c0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18997  TCE("smlsldx", 7400070, fbd000d0, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,t_mlal),
18998  TCE("smmla",   7500010, fb500000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
18999  TCE("smmlar",  7500030, fb500010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19000  TCE("smmls",   75000d0, fb600000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19001  TCE("smmlsr",  75000f0, fb600010, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla, t_mla),
19002  TCE("smmul",   750f010, fb50f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19003  TCE("smmulr",  750f030, fb50f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19004  TCE("smuad",   700f010, fb20f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19005  TCE("smuadx",  700f030, fb20f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19006  TCE("smusd",   700f050, fb40f000, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19007  TCE("smusdx",  700f070, fb40f010, 3, (RRnpc, RRnpc, RRnpc),       smul, t_simd),
19008  TCE("ssat16",  6a00f30, f3200000, 3, (RRnpc, I16, RRnpc),         ssat16, t_ssat16),
19009  TCE("umaal",   0400090, fbe00060, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smlal,  t_mlal),
19010  TCE("usad8",   780f010, fb70f000, 3, (RRnpc, RRnpc, RRnpc),       smul,   t_simd),
19011  TCE("usada8",  7800010, fb700000, 4, (RRnpc, RRnpc, RRnpc, RRnpc),smla,   t_mla),
19012  TCE("usat16",  6e00f30, f3a00000, 3, (RRnpc, I15, RRnpc),         usat16, t_usat16),
19013
19014 #undef  ARM_VARIANT
19015 #define ARM_VARIANT   & arm_ext_v6k
19016 #undef  THUMB_VARIANT
19017 #define THUMB_VARIANT & arm_ext_v6k
19018
19019  tCE("yield",   320f001, _yield,    0, (), noargs, t_hint),
19020  tCE("wfe",     320f002, _wfe,      0, (), noargs, t_hint),
19021  tCE("wfi",     320f003, _wfi,      0, (), noargs, t_hint),
19022  tCE("sev",     320f004, _sev,      0, (), noargs, t_hint),
19023
19024 #undef  THUMB_VARIANT
19025 #define THUMB_VARIANT  & arm_ext_v6_notm
19026  TCE("ldrexd",  1b00f9f, e8d0007f, 3, (RRnpc_npcsp, oRRnpc_npcsp, RRnpcb),
19027                                       ldrexd, t_ldrexd),
19028  TCE("strexd",  1a00f90, e8c00070, 4, (RRnpc_npcsp, RRnpc_npcsp, oRRnpc_npcsp,
19029                                        RRnpcb), strexd, t_strexd),
19030
19031 #undef  THUMB_VARIANT
19032 #define THUMB_VARIANT  & arm_ext_v6t2
19033  TCE("ldrexb",  1d00f9f, e8d00f4f, 2, (RRnpc_npcsp,RRnpcb),
19034      rd_rn,  rd_rn),
19035  TCE("ldrexh",  1f00f9f, e8d00f5f, 2, (RRnpc_npcsp, RRnpcb),
19036      rd_rn,  rd_rn),
19037  TCE("strexb",  1c00f90, e8c00f40, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19038      strex, t_strexbh),
19039  TCE("strexh",  1e00f90, e8c00f50, 3, (RRnpc_npcsp, RRnpc_npcsp, ADDR),
19040      strex, t_strexbh),
19041  TUF("clrex",   57ff01f, f3bf8f2f, 0, (),                             noargs, noargs),
19042
19043 #undef  ARM_VARIANT
19044 #define ARM_VARIANT    & arm_ext_sec
19045 #undef  THUMB_VARIANT
19046 #define THUMB_VARIANT  & arm_ext_sec
19047
19048  TCE("smc",     1600070, f7f08000, 1, (EXPi), smc, t_smc),
19049
19050 #undef  ARM_VARIANT
19051 #define ARM_VARIANT    & arm_ext_virt
19052 #undef  THUMB_VARIANT
19053 #define THUMB_VARIANT    & arm_ext_virt
19054
19055  TCE("hvc",     1400070, f7e08000, 1, (EXPi), hvc, t_hvc),
19056  TCE("eret",    160006e, f3de8f00, 0, (), noargs, noargs),
19057
19058 #undef  ARM_VARIANT
19059 #define ARM_VARIANT    & arm_ext_pan
19060 #undef  THUMB_VARIANT
19061 #define THUMB_VARIANT  & arm_ext_pan
19062
19063  TUF("setpan",  1100000, b610, 1, (I7), setpan, t_setpan),
19064
19065 #undef  ARM_VARIANT
19066 #define ARM_VARIANT    & arm_ext_v6t2
19067 #undef  THUMB_VARIANT
19068 #define THUMB_VARIANT  & arm_ext_v6t2
19069
19070  TCE("bfc",     7c0001f, f36f0000, 3, (RRnpc, I31, I32),           bfc, t_bfc),
19071  TCE("bfi",     7c00010, f3600000, 4, (RRnpc, RRnpc_I0, I31, I32), bfi, t_bfi),
19072  TCE("sbfx",    7a00050, f3400000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19073  TCE("ubfx",    7e00050, f3c00000, 4, (RR, RR, I31, I32),          bfx, t_bfx),
19074
19075  TCE("mls",     0600090, fb000010, 4, (RRnpc, RRnpc, RRnpc, RRnpc), mlas, t_mla),
19076  TCE("movw",    3000000, f2400000, 2, (RRnpc, HALF),                mov16, t_mov16),
19077  TCE("movt",    3400000, f2c00000, 2, (RRnpc, HALF),                mov16, t_mov16),
19078  TCE("rbit",    6ff0f30, fa90f0a0, 2, (RR, RR),                     rd_rm, t_rbit),
19079
19080  TC3("ldrht",   03000b0, f8300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19081  TC3("ldrsht",  03000f0, f9300e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19082  TC3("ldrsbt",  03000d0, f9100e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19083  TC3("strht",   02000b0, f8200e00, 2, (RRnpc_npcsp, ADDR), ldsttv4, t_ldstt),
19084
19085  /* Thumb-only instructions.  */
19086 #undef  ARM_VARIANT
19087 #define ARM_VARIANT NULL
19088   TUE("cbnz",     0,           b900,     2, (RR, EXP), 0, t_cbz),
19089   TUE("cbz",      0,           b100,     2, (RR, EXP), 0, t_cbz),
19090
19091  /* ARM does not really have an IT instruction, so always allow it.
19092     The opcode is copied from Thumb in order to allow warnings in
19093     -mimplicit-it=[never | arm] modes.  */
19094 #undef  ARM_VARIANT
19095 #define ARM_VARIANT  & arm_ext_v1
19096
19097  TUE("it",        bf08,        bf08,     1, (COND),   it,    t_it),
19098  TUE("itt",       bf0c,        bf0c,     1, (COND),   it,    t_it),
19099  TUE("ite",       bf04,        bf04,     1, (COND),   it,    t_it),
19100  TUE("ittt",      bf0e,        bf0e,     1, (COND),   it,    t_it),
19101  TUE("itet",      bf06,        bf06,     1, (COND),   it,    t_it),
19102  TUE("itte",      bf0a,        bf0a,     1, (COND),   it,    t_it),
19103  TUE("itee",      bf02,        bf02,     1, (COND),   it,    t_it),
19104  TUE("itttt",     bf0f,        bf0f,     1, (COND),   it,    t_it),
19105  TUE("itett",     bf07,        bf07,     1, (COND),   it,    t_it),
19106  TUE("ittet",     bf0b,        bf0b,     1, (COND),   it,    t_it),
19107  TUE("iteet",     bf03,        bf03,     1, (COND),   it,    t_it),
19108  TUE("ittte",     bf0d,        bf0d,     1, (COND),   it,    t_it),
19109  TUE("itete",     bf05,        bf05,     1, (COND),   it,    t_it),
19110  TUE("ittee",     bf09,        bf09,     1, (COND),   it,    t_it),
19111  TUE("iteee",     bf01,        bf01,     1, (COND),   it,    t_it),
19112  /* ARM/Thumb-2 instructions with no Thumb-1 equivalent.  */
19113  TC3("rrx",       01a00060, ea4f0030, 2, (RR, RR), rd_rm, t_rrx),
19114  TC3("rrxs",      01b00060, ea5f0030, 2, (RR, RR), rd_rm, t_rrx),
19115
19116  /* Thumb2 only instructions.  */
19117 #undef  ARM_VARIANT
19118 #define ARM_VARIANT  NULL
19119
19120  TCE("addw",    0, f2000000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19121  TCE("subw",    0, f2a00000, 3, (RR, RR, EXPi), 0, t_add_sub_w),
19122  TCE("orn",       0, ea600000, 3, (RR, oRR, SH),  0, t_orn),
19123  TCE("orns",      0, ea700000, 3, (RR, oRR, SH),  0, t_orn),
19124  TCE("tbb",       0, e8d0f000, 1, (TB), 0, t_tb),
19125  TCE("tbh",       0, e8d0f010, 1, (TB), 0, t_tb),
19126
19127  /* Hardware division instructions.  */
19128 #undef  ARM_VARIANT
19129 #define ARM_VARIANT    & arm_ext_adiv
19130 #undef  THUMB_VARIANT
19131 #define THUMB_VARIANT  & arm_ext_div
19132
19133  TCE("sdiv",    710f010, fb90f0f0, 3, (RR, oRR, RR), div, t_div),
19134  TCE("udiv",    730f010, fbb0f0f0, 3, (RR, oRR, RR), div, t_div),
19135
19136  /* ARM V6M/V7 instructions.  */
19137 #undef  ARM_VARIANT
19138 #define ARM_VARIANT    & arm_ext_barrier
19139 #undef  THUMB_VARIANT
19140 #define THUMB_VARIANT  & arm_ext_barrier
19141
19142  TUF("dmb",     57ff050, f3bf8f50, 1, (oBARRIER_I15), barrier, barrier),
19143  TUF("dsb",     57ff040, f3bf8f40, 1, (oBARRIER_I15), barrier, barrier),
19144  TUF("isb",     57ff060, f3bf8f60, 1, (oBARRIER_I15), barrier, barrier),
19145
19146  /* ARM V7 instructions.  */
19147 #undef  ARM_VARIANT
19148 #define ARM_VARIANT    & arm_ext_v7
19149 #undef  THUMB_VARIANT
19150 #define THUMB_VARIANT  & arm_ext_v7
19151
19152  TUF("pli",     450f000, f910f000, 1, (ADDR),     pli,      t_pld),
19153  TCE("dbg",     320f0f0, f3af80f0, 1, (I15),      dbg,      t_dbg),
19154
19155 #undef  ARM_VARIANT
19156 #define ARM_VARIANT    & arm_ext_mp
19157 #undef  THUMB_VARIANT
19158 #define THUMB_VARIANT  & arm_ext_mp
19159
19160  TUF("pldw",    410f000, f830f000, 1, (ADDR),   pld,    t_pld),
19161
19162  /* AArchv8 instructions.  */
19163 #undef  ARM_VARIANT
19164 #define ARM_VARIANT   & arm_ext_v8
19165 #undef  THUMB_VARIANT
19166 #define THUMB_VARIANT & arm_ext_v8
19167
19168  tCE("sevl",    320f005, _sevl,    0, (),               noargs, t_hint),
19169  TUE("hlt",     1000070, ba80,     1, (oIffffb),        bkpt,   t_hlt),
19170  TCE("ldaex",   1900e9f, e8d00fef, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19171  TCE("ldaexd",  1b00e9f, e8d000ff, 3, (RRnpc, oRRnpc, RRnpcb),
19172                                                         ldrexd, t_ldrexd),
19173  TCE("ldaexb",  1d00e9f, e8d00fcf, 2, (RRnpc,RRnpcb),   rd_rn,  rd_rn),
19174  TCE("ldaexh",  1f00e9f, e8d00fdf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19175  TCE("stlex",   1800e90, e8c00fe0, 3, (RRnpc, RRnpc, RRnpcb),
19176                                                         stlex,  t_stlex),
19177  TCE("stlexd",  1a00e90, e8c000f0, 4, (RRnpc, RRnpc, oRRnpc, RRnpcb),
19178                                                         strexd, t_strexd),
19179  TCE("stlexb",  1c00e90, e8c00fc0, 3, (RRnpc, RRnpc, RRnpcb),
19180                                                         stlex, t_stlex),
19181  TCE("stlexh",  1e00e90, e8c00fd0, 3, (RRnpc, RRnpc, RRnpcb),
19182                                                         stlex, t_stlex),
19183  TCE("lda",     1900c9f, e8d00faf, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19184  TCE("ldab",    1d00c9f, e8d00f8f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19185  TCE("ldah",    1f00c9f, e8d00f9f, 2, (RRnpc, RRnpcb),  rd_rn,  rd_rn),
19186  TCE("stl",     180fc90, e8c00faf, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19187  TCE("stlb",    1c0fc90, e8c00f8f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19188  TCE("stlh",    1e0fc90, e8c00f9f, 2, (RRnpc, RRnpcb),  rm_rn,  rd_rn),
19189
19190  /* ARMv8 T32 only.  */
19191 #undef  ARM_VARIANT
19192 #define ARM_VARIANT  NULL
19193  TUF("dcps1",   0,       f78f8001, 0, (),       noargs, noargs),
19194  TUF("dcps2",   0,       f78f8002, 0, (),       noargs, noargs),
19195  TUF("dcps3",   0,       f78f8003, 0, (),       noargs, noargs),
19196
19197   /* FP for ARMv8.  */
19198 #undef  ARM_VARIANT
19199 #define ARM_VARIANT   & fpu_vfp_ext_armv8xd
19200 #undef  THUMB_VARIANT
19201 #define THUMB_VARIANT & fpu_vfp_ext_armv8xd
19202
19203   nUF(vseleq, _vseleq, 3, (RVSD, RVSD, RVSD),           vsel),
19204   nUF(vselvs, _vselvs, 3, (RVSD, RVSD, RVSD),           vsel),
19205   nUF(vselge, _vselge, 3, (RVSD, RVSD, RVSD),           vsel),
19206   nUF(vselgt, _vselgt, 3, (RVSD, RVSD, RVSD),           vsel),
19207   nUF(vmaxnm, _vmaxnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19208   nUF(vminnm, _vminnm, 3, (RNSDQ, oRNSDQ, RNSDQ),       vmaxnm),
19209   nUF(vcvta,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvta),
19210   nUF(vcvtn,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtn),
19211   nUF(vcvtp,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtp),
19212   nUF(vcvtm,  _vcvta,  2, (RNSDQ, oRNSDQ),              neon_cvtm),
19213   nCE(vrintr, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintr),
19214   nCE(vrintz, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintz),
19215   nCE(vrintx, _vrintr, 2, (RNSDQ, oRNSDQ),              vrintx),
19216   nUF(vrinta, _vrinta, 2, (RNSDQ, oRNSDQ),              vrinta),
19217   nUF(vrintn, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintn),
19218   nUF(vrintp, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintp),
19219   nUF(vrintm, _vrinta, 2, (RNSDQ, oRNSDQ),              vrintm),
19220
19221   /* Crypto v1 extensions.  */
19222 #undef  ARM_VARIANT
19223 #define ARM_VARIANT & fpu_crypto_ext_armv8
19224 #undef  THUMB_VARIANT
19225 #define THUMB_VARIANT & fpu_crypto_ext_armv8
19226
19227   nUF(aese, _aes, 2, (RNQ, RNQ), aese),
19228   nUF(aesd, _aes, 2, (RNQ, RNQ), aesd),
19229   nUF(aesmc, _aes, 2, (RNQ, RNQ), aesmc),
19230   nUF(aesimc, _aes, 2, (RNQ, RNQ), aesimc),
19231   nUF(sha1c, _sha3op, 3, (RNQ, RNQ, RNQ), sha1c),
19232   nUF(sha1p, _sha3op, 3, (RNQ, RNQ, RNQ), sha1p),
19233   nUF(sha1m, _sha3op, 3, (RNQ, RNQ, RNQ), sha1m),
19234   nUF(sha1su0, _sha3op, 3, (RNQ, RNQ, RNQ), sha1su0),
19235   nUF(sha256h, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h),
19236   nUF(sha256h2, _sha3op, 3, (RNQ, RNQ, RNQ), sha256h2),
19237   nUF(sha256su1, _sha3op, 3, (RNQ, RNQ, RNQ), sha256su1),
19238   nUF(sha1h, _sha1h, 2, (RNQ, RNQ), sha1h),
19239   nUF(sha1su1, _sha2op, 2, (RNQ, RNQ), sha1su1),
19240   nUF(sha256su0, _sha2op, 2, (RNQ, RNQ), sha256su0),
19241
19242 #undef  ARM_VARIANT
19243 #define ARM_VARIANT   & crc_ext_armv8
19244 #undef  THUMB_VARIANT
19245 #define THUMB_VARIANT & crc_ext_armv8
19246   TUEc("crc32b", 1000040, fac0f080, 3, (RR, oRR, RR), crc32b),
19247   TUEc("crc32h", 1200040, fac0f090, 3, (RR, oRR, RR), crc32h),
19248   TUEc("crc32w", 1400040, fac0f0a0, 3, (RR, oRR, RR), crc32w),
19249   TUEc("crc32cb",1000240, fad0f080, 3, (RR, oRR, RR), crc32cb),
19250   TUEc("crc32ch",1200240, fad0f090, 3, (RR, oRR, RR), crc32ch),
19251   TUEc("crc32cw",1400240, fad0f0a0, 3, (RR, oRR, RR), crc32cw),
19252
19253 #undef  ARM_VARIANT
19254 #define ARM_VARIANT  & fpu_fpa_ext_v1  /* Core FPA instruction set (V1).  */
19255 #undef  THUMB_VARIANT
19256 #define THUMB_VARIANT NULL
19257
19258  cCE("wfs",     e200110, 1, (RR),            rd),
19259  cCE("rfs",     e300110, 1, (RR),            rd),
19260  cCE("wfc",     e400110, 1, (RR),            rd),
19261  cCE("rfc",     e500110, 1, (RR),            rd),
19262
19263  cCL("ldfs",    c100100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19264  cCL("ldfd",    c108100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19265  cCL("ldfe",    c500100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19266  cCL("ldfp",    c508100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19267
19268  cCL("stfs",    c000100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19269  cCL("stfd",    c008100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19270  cCL("stfe",    c400100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19271  cCL("stfp",    c408100, 2, (RF, ADDRGLDC),  rd_cpaddr),
19272
19273  cCL("mvfs",    e008100, 2, (RF, RF_IF),     rd_rm),
19274  cCL("mvfsp",   e008120, 2, (RF, RF_IF),     rd_rm),
19275  cCL("mvfsm",   e008140, 2, (RF, RF_IF),     rd_rm),
19276  cCL("mvfsz",   e008160, 2, (RF, RF_IF),     rd_rm),
19277  cCL("mvfd",    e008180, 2, (RF, RF_IF),     rd_rm),
19278  cCL("mvfdp",   e0081a0, 2, (RF, RF_IF),     rd_rm),
19279  cCL("mvfdm",   e0081c0, 2, (RF, RF_IF),     rd_rm),
19280  cCL("mvfdz",   e0081e0, 2, (RF, RF_IF),     rd_rm),
19281  cCL("mvfe",    e088100, 2, (RF, RF_IF),     rd_rm),
19282  cCL("mvfep",   e088120, 2, (RF, RF_IF),     rd_rm),
19283  cCL("mvfem",   e088140, 2, (RF, RF_IF),     rd_rm),
19284  cCL("mvfez",   e088160, 2, (RF, RF_IF),     rd_rm),
19285
19286  cCL("mnfs",    e108100, 2, (RF, RF_IF),     rd_rm),
19287  cCL("mnfsp",   e108120, 2, (RF, RF_IF),     rd_rm),
19288  cCL("mnfsm",   e108140, 2, (RF, RF_IF),     rd_rm),
19289  cCL("mnfsz",   e108160, 2, (RF, RF_IF),     rd_rm),
19290  cCL("mnfd",    e108180, 2, (RF, RF_IF),     rd_rm),
19291  cCL("mnfdp",   e1081a0, 2, (RF, RF_IF),     rd_rm),
19292  cCL("mnfdm",   e1081c0, 2, (RF, RF_IF),     rd_rm),
19293  cCL("mnfdz",   e1081e0, 2, (RF, RF_IF),     rd_rm),
19294  cCL("mnfe",    e188100, 2, (RF, RF_IF),     rd_rm),
19295  cCL("mnfep",   e188120, 2, (RF, RF_IF),     rd_rm),
19296  cCL("mnfem",   e188140, 2, (RF, RF_IF),     rd_rm),
19297  cCL("mnfez",   e188160, 2, (RF, RF_IF),     rd_rm),
19298
19299  cCL("abss",    e208100, 2, (RF, RF_IF),     rd_rm),
19300  cCL("abssp",   e208120, 2, (RF, RF_IF),     rd_rm),
19301  cCL("abssm",   e208140, 2, (RF, RF_IF),     rd_rm),
19302  cCL("abssz",   e208160, 2, (RF, RF_IF),     rd_rm),
19303  cCL("absd",    e208180, 2, (RF, RF_IF),     rd_rm),
19304  cCL("absdp",   e2081a0, 2, (RF, RF_IF),     rd_rm),
19305  cCL("absdm",   e2081c0, 2, (RF, RF_IF),     rd_rm),
19306  cCL("absdz",   e2081e0, 2, (RF, RF_IF),     rd_rm),
19307  cCL("abse",    e288100, 2, (RF, RF_IF),     rd_rm),
19308  cCL("absep",   e288120, 2, (RF, RF_IF),     rd_rm),
19309  cCL("absem",   e288140, 2, (RF, RF_IF),     rd_rm),
19310  cCL("absez",   e288160, 2, (RF, RF_IF),     rd_rm),
19311
19312  cCL("rnds",    e308100, 2, (RF, RF_IF),     rd_rm),
19313  cCL("rndsp",   e308120, 2, (RF, RF_IF),     rd_rm),
19314  cCL("rndsm",   e308140, 2, (RF, RF_IF),     rd_rm),
19315  cCL("rndsz",   e308160, 2, (RF, RF_IF),     rd_rm),
19316  cCL("rndd",    e308180, 2, (RF, RF_IF),     rd_rm),
19317  cCL("rnddp",   e3081a0, 2, (RF, RF_IF),     rd_rm),
19318  cCL("rnddm",   e3081c0, 2, (RF, RF_IF),     rd_rm),
19319  cCL("rnddz",   e3081e0, 2, (RF, RF_IF),     rd_rm),
19320  cCL("rnde",    e388100, 2, (RF, RF_IF),     rd_rm),
19321  cCL("rndep",   e388120, 2, (RF, RF_IF),     rd_rm),
19322  cCL("rndem",   e388140, 2, (RF, RF_IF),     rd_rm),
19323  cCL("rndez",   e388160, 2, (RF, RF_IF),     rd_rm),
19324
19325  cCL("sqts",    e408100, 2, (RF, RF_IF),     rd_rm),
19326  cCL("sqtsp",   e408120, 2, (RF, RF_IF),     rd_rm),
19327  cCL("sqtsm",   e408140, 2, (RF, RF_IF),     rd_rm),
19328  cCL("sqtsz",   e408160, 2, (RF, RF_IF),     rd_rm),
19329  cCL("sqtd",    e408180, 2, (RF, RF_IF),     rd_rm),
19330  cCL("sqtdp",   e4081a0, 2, (RF, RF_IF),     rd_rm),
19331  cCL("sqtdm",   e4081c0, 2, (RF, RF_IF),     rd_rm),
19332  cCL("sqtdz",   e4081e0, 2, (RF, RF_IF),     rd_rm),
19333  cCL("sqte",    e488100, 2, (RF, RF_IF),     rd_rm),
19334  cCL("sqtep",   e488120, 2, (RF, RF_IF),     rd_rm),
19335  cCL("sqtem",   e488140, 2, (RF, RF_IF),     rd_rm),
19336  cCL("sqtez",   e488160, 2, (RF, RF_IF),     rd_rm),
19337
19338  cCL("logs",    e508100, 2, (RF, RF_IF),     rd_rm),
19339  cCL("logsp",   e508120, 2, (RF, RF_IF),     rd_rm),
19340  cCL("logsm",   e508140, 2, (RF, RF_IF),     rd_rm),
19341  cCL("logsz",   e508160, 2, (RF, RF_IF),     rd_rm),
19342  cCL("logd",    e508180, 2, (RF, RF_IF),     rd_rm),
19343  cCL("logdp",   e5081a0, 2, (RF, RF_IF),     rd_rm),
19344  cCL("logdm",   e5081c0, 2, (RF, RF_IF),     rd_rm),
19345  cCL("logdz",   e5081e0, 2, (RF, RF_IF),     rd_rm),
19346  cCL("loge",    e588100, 2, (RF, RF_IF),     rd_rm),
19347  cCL("logep",   e588120, 2, (RF, RF_IF),     rd_rm),
19348  cCL("logem",   e588140, 2, (RF, RF_IF),     rd_rm),
19349  cCL("logez",   e588160, 2, (RF, RF_IF),     rd_rm),
19350
19351  cCL("lgns",    e608100, 2, (RF, RF_IF),     rd_rm),
19352  cCL("lgnsp",   e608120, 2, (RF, RF_IF),     rd_rm),
19353  cCL("lgnsm",   e608140, 2, (RF, RF_IF),     rd_rm),
19354  cCL("lgnsz",   e608160, 2, (RF, RF_IF),     rd_rm),
19355  cCL("lgnd",    e608180, 2, (RF, RF_IF),     rd_rm),
19356  cCL("lgndp",   e6081a0, 2, (RF, RF_IF),     rd_rm),
19357  cCL("lgndm",   e6081c0, 2, (RF, RF_IF),     rd_rm),
19358  cCL("lgndz",   e6081e0, 2, (RF, RF_IF),     rd_rm),
19359  cCL("lgne",    e688100, 2, (RF, RF_IF),     rd_rm),
19360  cCL("lgnep",   e688120, 2, (RF, RF_IF),     rd_rm),
19361  cCL("lgnem",   e688140, 2, (RF, RF_IF),     rd_rm),
19362  cCL("lgnez",   e688160, 2, (RF, RF_IF),     rd_rm),
19363
19364  cCL("exps",    e708100, 2, (RF, RF_IF),     rd_rm),
19365  cCL("expsp",   e708120, 2, (RF, RF_IF),     rd_rm),
19366  cCL("expsm",   e708140, 2, (RF, RF_IF),     rd_rm),
19367  cCL("expsz",   e708160, 2, (RF, RF_IF),     rd_rm),
19368  cCL("expd",    e708180, 2, (RF, RF_IF),     rd_rm),
19369  cCL("expdp",   e7081a0, 2, (RF, RF_IF),     rd_rm),
19370  cCL("expdm",   e7081c0, 2, (RF, RF_IF),     rd_rm),
19371  cCL("expdz",   e7081e0, 2, (RF, RF_IF),     rd_rm),
19372  cCL("expe",    e788100, 2, (RF, RF_IF),     rd_rm),
19373  cCL("expep",   e788120, 2, (RF, RF_IF),     rd_rm),
19374  cCL("expem",   e788140, 2, (RF, RF_IF),     rd_rm),
19375  cCL("expdz",   e788160, 2, (RF, RF_IF),     rd_rm),
19376
19377  cCL("sins",    e808100, 2, (RF, RF_IF),     rd_rm),
19378  cCL("sinsp",   e808120, 2, (RF, RF_IF),     rd_rm),
19379  cCL("sinsm",   e808140, 2, (RF, RF_IF),     rd_rm),
19380  cCL("sinsz",   e808160, 2, (RF, RF_IF),     rd_rm),
19381  cCL("sind",    e808180, 2, (RF, RF_IF),     rd_rm),
19382  cCL("sindp",   e8081a0, 2, (RF, RF_IF),     rd_rm),
19383  cCL("sindm",   e8081c0, 2, (RF, RF_IF),     rd_rm),
19384  cCL("sindz",   e8081e0, 2, (RF, RF_IF),     rd_rm),
19385  cCL("sine",    e888100, 2, (RF, RF_IF),     rd_rm),
19386  cCL("sinep",   e888120, 2, (RF, RF_IF),     rd_rm),
19387  cCL("sinem",   e888140, 2, (RF, RF_IF),     rd_rm),
19388  cCL("sinez",   e888160, 2, (RF, RF_IF),     rd_rm),
19389
19390  cCL("coss",    e908100, 2, (RF, RF_IF),     rd_rm),
19391  cCL("cossp",   e908120, 2, (RF, RF_IF),     rd_rm),
19392  cCL("cossm",   e908140, 2, (RF, RF_IF),     rd_rm),
19393  cCL("cossz",   e908160, 2, (RF, RF_IF),     rd_rm),
19394  cCL("cosd",    e908180, 2, (RF, RF_IF),     rd_rm),
19395  cCL("cosdp",   e9081a0, 2, (RF, RF_IF),     rd_rm),
19396  cCL("cosdm",   e9081c0, 2, (RF, RF_IF),     rd_rm),
19397  cCL("cosdz",   e9081e0, 2, (RF, RF_IF),     rd_rm),
19398  cCL("cose",    e988100, 2, (RF, RF_IF),     rd_rm),
19399  cCL("cosep",   e988120, 2, (RF, RF_IF),     rd_rm),
19400  cCL("cosem",   e988140, 2, (RF, RF_IF),     rd_rm),
19401  cCL("cosez",   e988160, 2, (RF, RF_IF),     rd_rm),
19402
19403  cCL("tans",    ea08100, 2, (RF, RF_IF),     rd_rm),
19404  cCL("tansp",   ea08120, 2, (RF, RF_IF),     rd_rm),
19405  cCL("tansm",   ea08140, 2, (RF, RF_IF),     rd_rm),
19406  cCL("tansz",   ea08160, 2, (RF, RF_IF),     rd_rm),
19407  cCL("tand",    ea08180, 2, (RF, RF_IF),     rd_rm),
19408  cCL("tandp",   ea081a0, 2, (RF, RF_IF),     rd_rm),
19409  cCL("tandm",   ea081c0, 2, (RF, RF_IF),     rd_rm),
19410  cCL("tandz",   ea081e0, 2, (RF, RF_IF),     rd_rm),
19411  cCL("tane",    ea88100, 2, (RF, RF_IF),     rd_rm),
19412  cCL("tanep",   ea88120, 2, (RF, RF_IF),     rd_rm),
19413  cCL("tanem",   ea88140, 2, (RF, RF_IF),     rd_rm),
19414  cCL("tanez",   ea88160, 2, (RF, RF_IF),     rd_rm),
19415
19416  cCL("asns",    eb08100, 2, (RF, RF_IF),     rd_rm),
19417  cCL("asnsp",   eb08120, 2, (RF, RF_IF),     rd_rm),
19418  cCL("asnsm",   eb08140, 2, (RF, RF_IF),     rd_rm),
19419  cCL("asnsz",   eb08160, 2, (RF, RF_IF),     rd_rm),
19420  cCL("asnd",    eb08180, 2, (RF, RF_IF),     rd_rm),
19421  cCL("asndp",   eb081a0, 2, (RF, RF_IF),     rd_rm),
19422  cCL("asndm",   eb081c0, 2, (RF, RF_IF),     rd_rm),
19423  cCL("asndz",   eb081e0, 2, (RF, RF_IF),     rd_rm),
19424  cCL("asne",    eb88100, 2, (RF, RF_IF),     rd_rm),
19425  cCL("asnep",   eb88120, 2, (RF, RF_IF),     rd_rm),
19426  cCL("asnem",   eb88140, 2, (RF, RF_IF),     rd_rm),
19427  cCL("asnez",   eb88160, 2, (RF, RF_IF),     rd_rm),
19428
19429  cCL("acss",    ec08100, 2, (RF, RF_IF),     rd_rm),
19430  cCL("acssp",   ec08120, 2, (RF, RF_IF),     rd_rm),
19431  cCL("acssm",   ec08140, 2, (RF, RF_IF),     rd_rm),
19432  cCL("acssz",   ec08160, 2, (RF, RF_IF),     rd_rm),
19433  cCL("acsd",    ec08180, 2, (RF, RF_IF),     rd_rm),
19434  cCL("acsdp",   ec081a0, 2, (RF, RF_IF),     rd_rm),
19435  cCL("acsdm",   ec081c0, 2, (RF, RF_IF),     rd_rm),
19436  cCL("acsdz",   ec081e0, 2, (RF, RF_IF),     rd_rm),
19437  cCL("acse",    ec88100, 2, (RF, RF_IF),     rd_rm),
19438  cCL("acsep",   ec88120, 2, (RF, RF_IF),     rd_rm),
19439  cCL("acsem",   ec88140, 2, (RF, RF_IF),     rd_rm),
19440  cCL("acsez",   ec88160, 2, (RF, RF_IF),     rd_rm),
19441
19442  cCL("atns",    ed08100, 2, (RF, RF_IF),     rd_rm),
19443  cCL("atnsp",   ed08120, 2, (RF, RF_IF),     rd_rm),
19444  cCL("atnsm",   ed08140, 2, (RF, RF_IF),     rd_rm),
19445  cCL("atnsz",   ed08160, 2, (RF, RF_IF),     rd_rm),
19446  cCL("atnd",    ed08180, 2, (RF, RF_IF),     rd_rm),
19447  cCL("atndp",   ed081a0, 2, (RF, RF_IF),     rd_rm),
19448  cCL("atndm",   ed081c0, 2, (RF, RF_IF),     rd_rm),
19449  cCL("atndz",   ed081e0, 2, (RF, RF_IF),     rd_rm),
19450  cCL("atne",    ed88100, 2, (RF, RF_IF),     rd_rm),
19451  cCL("atnep",   ed88120, 2, (RF, RF_IF),     rd_rm),
19452  cCL("atnem",   ed88140, 2, (RF, RF_IF),     rd_rm),
19453  cCL("atnez",   ed88160, 2, (RF, RF_IF),     rd_rm),
19454
19455  cCL("urds",    ee08100, 2, (RF, RF_IF),     rd_rm),
19456  cCL("urdsp",   ee08120, 2, (RF, RF_IF),     rd_rm),
19457  cCL("urdsm",   ee08140, 2, (RF, RF_IF),     rd_rm),
19458  cCL("urdsz",   ee08160, 2, (RF, RF_IF),     rd_rm),
19459  cCL("urdd",    ee08180, 2, (RF, RF_IF),     rd_rm),
19460  cCL("urddp",   ee081a0, 2, (RF, RF_IF),     rd_rm),
19461  cCL("urddm",   ee081c0, 2, (RF, RF_IF),     rd_rm),
19462  cCL("urddz",   ee081e0, 2, (RF, RF_IF),     rd_rm),
19463  cCL("urde",    ee88100, 2, (RF, RF_IF),     rd_rm),
19464  cCL("urdep",   ee88120, 2, (RF, RF_IF),     rd_rm),
19465  cCL("urdem",   ee88140, 2, (RF, RF_IF),     rd_rm),
19466  cCL("urdez",   ee88160, 2, (RF, RF_IF),     rd_rm),
19467
19468  cCL("nrms",    ef08100, 2, (RF, RF_IF),     rd_rm),
19469  cCL("nrmsp",   ef08120, 2, (RF, RF_IF),     rd_rm),
19470  cCL("nrmsm",   ef08140, 2, (RF, RF_IF),     rd_rm),
19471  cCL("nrmsz",   ef08160, 2, (RF, RF_IF),     rd_rm),
19472  cCL("nrmd",    ef08180, 2, (RF, RF_IF),     rd_rm),
19473  cCL("nrmdp",   ef081a0, 2, (RF, RF_IF),     rd_rm),
19474  cCL("nrmdm",   ef081c0, 2, (RF, RF_IF),     rd_rm),
19475  cCL("nrmdz",   ef081e0, 2, (RF, RF_IF),     rd_rm),
19476  cCL("nrme",    ef88100, 2, (RF, RF_IF),     rd_rm),
19477  cCL("nrmep",   ef88120, 2, (RF, RF_IF),     rd_rm),
19478  cCL("nrmem",   ef88140, 2, (RF, RF_IF),     rd_rm),
19479  cCL("nrmez",   ef88160, 2, (RF, RF_IF),     rd_rm),
19480
19481  cCL("adfs",    e000100, 3, (RF, RF, RF_IF), rd_rn_rm),
19482  cCL("adfsp",   e000120, 3, (RF, RF, RF_IF), rd_rn_rm),
19483  cCL("adfsm",   e000140, 3, (RF, RF, RF_IF), rd_rn_rm),
19484  cCL("adfsz",   e000160, 3, (RF, RF, RF_IF), rd_rn_rm),
19485  cCL("adfd",    e000180, 3, (RF, RF, RF_IF), rd_rn_rm),
19486  cCL("adfdp",   e0001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19487  cCL("adfdm",   e0001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19488  cCL("adfdz",   e0001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19489  cCL("adfe",    e080100, 3, (RF, RF, RF_IF), rd_rn_rm),
19490  cCL("adfep",   e080120, 3, (RF, RF, RF_IF), rd_rn_rm),
19491  cCL("adfem",   e080140, 3, (RF, RF, RF_IF), rd_rn_rm),
19492  cCL("adfez",   e080160, 3, (RF, RF, RF_IF), rd_rn_rm),
19493
19494  cCL("sufs",    e200100, 3, (RF, RF, RF_IF), rd_rn_rm),
19495  cCL("sufsp",   e200120, 3, (RF, RF, RF_IF), rd_rn_rm),
19496  cCL("sufsm",   e200140, 3, (RF, RF, RF_IF), rd_rn_rm),
19497  cCL("sufsz",   e200160, 3, (RF, RF, RF_IF), rd_rn_rm),
19498  cCL("sufd",    e200180, 3, (RF, RF, RF_IF), rd_rn_rm),
19499  cCL("sufdp",   e2001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19500  cCL("sufdm",   e2001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19501  cCL("sufdz",   e2001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19502  cCL("sufe",    e280100, 3, (RF, RF, RF_IF), rd_rn_rm),
19503  cCL("sufep",   e280120, 3, (RF, RF, RF_IF), rd_rn_rm),
19504  cCL("sufem",   e280140, 3, (RF, RF, RF_IF), rd_rn_rm),
19505  cCL("sufez",   e280160, 3, (RF, RF, RF_IF), rd_rn_rm),
19506
19507  cCL("rsfs",    e300100, 3, (RF, RF, RF_IF), rd_rn_rm),
19508  cCL("rsfsp",   e300120, 3, (RF, RF, RF_IF), rd_rn_rm),
19509  cCL("rsfsm",   e300140, 3, (RF, RF, RF_IF), rd_rn_rm),
19510  cCL("rsfsz",   e300160, 3, (RF, RF, RF_IF), rd_rn_rm),
19511  cCL("rsfd",    e300180, 3, (RF, RF, RF_IF), rd_rn_rm),
19512  cCL("rsfdp",   e3001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19513  cCL("rsfdm",   e3001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19514  cCL("rsfdz",   e3001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19515  cCL("rsfe",    e380100, 3, (RF, RF, RF_IF), rd_rn_rm),
19516  cCL("rsfep",   e380120, 3, (RF, RF, RF_IF), rd_rn_rm),
19517  cCL("rsfem",   e380140, 3, (RF, RF, RF_IF), rd_rn_rm),
19518  cCL("rsfez",   e380160, 3, (RF, RF, RF_IF), rd_rn_rm),
19519
19520  cCL("mufs",    e100100, 3, (RF, RF, RF_IF), rd_rn_rm),
19521  cCL("mufsp",   e100120, 3, (RF, RF, RF_IF), rd_rn_rm),
19522  cCL("mufsm",   e100140, 3, (RF, RF, RF_IF), rd_rn_rm),
19523  cCL("mufsz",   e100160, 3, (RF, RF, RF_IF), rd_rn_rm),
19524  cCL("mufd",    e100180, 3, (RF, RF, RF_IF), rd_rn_rm),
19525  cCL("mufdp",   e1001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19526  cCL("mufdm",   e1001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19527  cCL("mufdz",   e1001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19528  cCL("mufe",    e180100, 3, (RF, RF, RF_IF), rd_rn_rm),
19529  cCL("mufep",   e180120, 3, (RF, RF, RF_IF), rd_rn_rm),
19530  cCL("mufem",   e180140, 3, (RF, RF, RF_IF), rd_rn_rm),
19531  cCL("mufez",   e180160, 3, (RF, RF, RF_IF), rd_rn_rm),
19532
19533  cCL("dvfs",    e400100, 3, (RF, RF, RF_IF), rd_rn_rm),
19534  cCL("dvfsp",   e400120, 3, (RF, RF, RF_IF), rd_rn_rm),
19535  cCL("dvfsm",   e400140, 3, (RF, RF, RF_IF), rd_rn_rm),
19536  cCL("dvfsz",   e400160, 3, (RF, RF, RF_IF), rd_rn_rm),
19537  cCL("dvfd",    e400180, 3, (RF, RF, RF_IF), rd_rn_rm),
19538  cCL("dvfdp",   e4001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19539  cCL("dvfdm",   e4001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19540  cCL("dvfdz",   e4001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19541  cCL("dvfe",    e480100, 3, (RF, RF, RF_IF), rd_rn_rm),
19542  cCL("dvfep",   e480120, 3, (RF, RF, RF_IF), rd_rn_rm),
19543  cCL("dvfem",   e480140, 3, (RF, RF, RF_IF), rd_rn_rm),
19544  cCL("dvfez",   e480160, 3, (RF, RF, RF_IF), rd_rn_rm),
19545
19546  cCL("rdfs",    e500100, 3, (RF, RF, RF_IF), rd_rn_rm),
19547  cCL("rdfsp",   e500120, 3, (RF, RF, RF_IF), rd_rn_rm),
19548  cCL("rdfsm",   e500140, 3, (RF, RF, RF_IF), rd_rn_rm),
19549  cCL("rdfsz",   e500160, 3, (RF, RF, RF_IF), rd_rn_rm),
19550  cCL("rdfd",    e500180, 3, (RF, RF, RF_IF), rd_rn_rm),
19551  cCL("rdfdp",   e5001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19552  cCL("rdfdm",   e5001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19553  cCL("rdfdz",   e5001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19554  cCL("rdfe",    e580100, 3, (RF, RF, RF_IF), rd_rn_rm),
19555  cCL("rdfep",   e580120, 3, (RF, RF, RF_IF), rd_rn_rm),
19556  cCL("rdfem",   e580140, 3, (RF, RF, RF_IF), rd_rn_rm),
19557  cCL("rdfez",   e580160, 3, (RF, RF, RF_IF), rd_rn_rm),
19558
19559  cCL("pows",    e600100, 3, (RF, RF, RF_IF), rd_rn_rm),
19560  cCL("powsp",   e600120, 3, (RF, RF, RF_IF), rd_rn_rm),
19561  cCL("powsm",   e600140, 3, (RF, RF, RF_IF), rd_rn_rm),
19562  cCL("powsz",   e600160, 3, (RF, RF, RF_IF), rd_rn_rm),
19563  cCL("powd",    e600180, 3, (RF, RF, RF_IF), rd_rn_rm),
19564  cCL("powdp",   e6001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19565  cCL("powdm",   e6001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19566  cCL("powdz",   e6001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19567  cCL("powe",    e680100, 3, (RF, RF, RF_IF), rd_rn_rm),
19568  cCL("powep",   e680120, 3, (RF, RF, RF_IF), rd_rn_rm),
19569  cCL("powem",   e680140, 3, (RF, RF, RF_IF), rd_rn_rm),
19570  cCL("powez",   e680160, 3, (RF, RF, RF_IF), rd_rn_rm),
19571
19572  cCL("rpws",    e700100, 3, (RF, RF, RF_IF), rd_rn_rm),
19573  cCL("rpwsp",   e700120, 3, (RF, RF, RF_IF), rd_rn_rm),
19574  cCL("rpwsm",   e700140, 3, (RF, RF, RF_IF), rd_rn_rm),
19575  cCL("rpwsz",   e700160, 3, (RF, RF, RF_IF), rd_rn_rm),
19576  cCL("rpwd",    e700180, 3, (RF, RF, RF_IF), rd_rn_rm),
19577  cCL("rpwdp",   e7001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19578  cCL("rpwdm",   e7001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19579  cCL("rpwdz",   e7001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19580  cCL("rpwe",    e780100, 3, (RF, RF, RF_IF), rd_rn_rm),
19581  cCL("rpwep",   e780120, 3, (RF, RF, RF_IF), rd_rn_rm),
19582  cCL("rpwem",   e780140, 3, (RF, RF, RF_IF), rd_rn_rm),
19583  cCL("rpwez",   e780160, 3, (RF, RF, RF_IF), rd_rn_rm),
19584
19585  cCL("rmfs",    e800100, 3, (RF, RF, RF_IF), rd_rn_rm),
19586  cCL("rmfsp",   e800120, 3, (RF, RF, RF_IF), rd_rn_rm),
19587  cCL("rmfsm",   e800140, 3, (RF, RF, RF_IF), rd_rn_rm),
19588  cCL("rmfsz",   e800160, 3, (RF, RF, RF_IF), rd_rn_rm),
19589  cCL("rmfd",    e800180, 3, (RF, RF, RF_IF), rd_rn_rm),
19590  cCL("rmfdp",   e8001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19591  cCL("rmfdm",   e8001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19592  cCL("rmfdz",   e8001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19593  cCL("rmfe",    e880100, 3, (RF, RF, RF_IF), rd_rn_rm),
19594  cCL("rmfep",   e880120, 3, (RF, RF, RF_IF), rd_rn_rm),
19595  cCL("rmfem",   e880140, 3, (RF, RF, RF_IF), rd_rn_rm),
19596  cCL("rmfez",   e880160, 3, (RF, RF, RF_IF), rd_rn_rm),
19597
19598  cCL("fmls",    e900100, 3, (RF, RF, RF_IF), rd_rn_rm),
19599  cCL("fmlsp",   e900120, 3, (RF, RF, RF_IF), rd_rn_rm),
19600  cCL("fmlsm",   e900140, 3, (RF, RF, RF_IF), rd_rn_rm),
19601  cCL("fmlsz",   e900160, 3, (RF, RF, RF_IF), rd_rn_rm),
19602  cCL("fmld",    e900180, 3, (RF, RF, RF_IF), rd_rn_rm),
19603  cCL("fmldp",   e9001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19604  cCL("fmldm",   e9001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19605  cCL("fmldz",   e9001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19606  cCL("fmle",    e980100, 3, (RF, RF, RF_IF), rd_rn_rm),
19607  cCL("fmlep",   e980120, 3, (RF, RF, RF_IF), rd_rn_rm),
19608  cCL("fmlem",   e980140, 3, (RF, RF, RF_IF), rd_rn_rm),
19609  cCL("fmlez",   e980160, 3, (RF, RF, RF_IF), rd_rn_rm),
19610
19611  cCL("fdvs",    ea00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19612  cCL("fdvsp",   ea00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19613  cCL("fdvsm",   ea00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19614  cCL("fdvsz",   ea00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19615  cCL("fdvd",    ea00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19616  cCL("fdvdp",   ea001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19617  cCL("fdvdm",   ea001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19618  cCL("fdvdz",   ea001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19619  cCL("fdve",    ea80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19620  cCL("fdvep",   ea80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19621  cCL("fdvem",   ea80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19622  cCL("fdvez",   ea80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19623
19624  cCL("frds",    eb00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19625  cCL("frdsp",   eb00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19626  cCL("frdsm",   eb00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19627  cCL("frdsz",   eb00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19628  cCL("frdd",    eb00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19629  cCL("frddp",   eb001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19630  cCL("frddm",   eb001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19631  cCL("frddz",   eb001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19632  cCL("frde",    eb80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19633  cCL("frdep",   eb80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19634  cCL("frdem",   eb80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19635  cCL("frdez",   eb80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19636
19637  cCL("pols",    ec00100, 3, (RF, RF, RF_IF), rd_rn_rm),
19638  cCL("polsp",   ec00120, 3, (RF, RF, RF_IF), rd_rn_rm),
19639  cCL("polsm",   ec00140, 3, (RF, RF, RF_IF), rd_rn_rm),
19640  cCL("polsz",   ec00160, 3, (RF, RF, RF_IF), rd_rn_rm),
19641  cCL("pold",    ec00180, 3, (RF, RF, RF_IF), rd_rn_rm),
19642  cCL("poldp",   ec001a0, 3, (RF, RF, RF_IF), rd_rn_rm),
19643  cCL("poldm",   ec001c0, 3, (RF, RF, RF_IF), rd_rn_rm),
19644  cCL("poldz",   ec001e0, 3, (RF, RF, RF_IF), rd_rn_rm),
19645  cCL("pole",    ec80100, 3, (RF, RF, RF_IF), rd_rn_rm),
19646  cCL("polep",   ec80120, 3, (RF, RF, RF_IF), rd_rn_rm),
19647  cCL("polem",   ec80140, 3, (RF, RF, RF_IF), rd_rn_rm),
19648  cCL("polez",   ec80160, 3, (RF, RF, RF_IF), rd_rn_rm),
19649
19650  cCE("cmf",     e90f110, 2, (RF, RF_IF),     fpa_cmp),
19651  C3E("cmfe",    ed0f110, 2, (RF, RF_IF),     fpa_cmp),
19652  cCE("cnf",     eb0f110, 2, (RF, RF_IF),     fpa_cmp),
19653  C3E("cnfe",    ef0f110, 2, (RF, RF_IF),     fpa_cmp),
19654
19655  cCL("flts",    e000110, 2, (RF, RR),        rn_rd),
19656  cCL("fltsp",   e000130, 2, (RF, RR),        rn_rd),
19657  cCL("fltsm",   e000150, 2, (RF, RR),        rn_rd),
19658  cCL("fltsz",   e000170, 2, (RF, RR),        rn_rd),
19659  cCL("fltd",    e000190, 2, (RF, RR),        rn_rd),
19660  cCL("fltdp",   e0001b0, 2, (RF, RR),        rn_rd),
19661  cCL("fltdm",   e0001d0, 2, (RF, RR),        rn_rd),
19662  cCL("fltdz",   e0001f0, 2, (RF, RR),        rn_rd),
19663  cCL("flte",    e080110, 2, (RF, RR),        rn_rd),
19664  cCL("fltep",   e080130, 2, (RF, RR),        rn_rd),
19665  cCL("fltem",   e080150, 2, (RF, RR),        rn_rd),
19666  cCL("fltez",   e080170, 2, (RF, RR),        rn_rd),
19667
19668   /* The implementation of the FIX instruction is broken on some
19669      assemblers, in that it accepts a precision specifier as well as a
19670      rounding specifier, despite the fact that this is meaningless.
19671      To be more compatible, we accept it as well, though of course it
19672      does not set any bits.  */
19673  cCE("fix",     e100110, 2, (RR, RF),        rd_rm),
19674  cCL("fixp",    e100130, 2, (RR, RF),        rd_rm),
19675  cCL("fixm",    e100150, 2, (RR, RF),        rd_rm),
19676  cCL("fixz",    e100170, 2, (RR, RF),        rd_rm),
19677  cCL("fixsp",   e100130, 2, (RR, RF),        rd_rm),
19678  cCL("fixsm",   e100150, 2, (RR, RF),        rd_rm),
19679  cCL("fixsz",   e100170, 2, (RR, RF),        rd_rm),
19680  cCL("fixdp",   e100130, 2, (RR, RF),        rd_rm),
19681  cCL("fixdm",   e100150, 2, (RR, RF),        rd_rm),
19682  cCL("fixdz",   e100170, 2, (RR, RF),        rd_rm),
19683  cCL("fixep",   e100130, 2, (RR, RF),        rd_rm),
19684  cCL("fixem",   e100150, 2, (RR, RF),        rd_rm),
19685  cCL("fixez",   e100170, 2, (RR, RF),        rd_rm),
19686
19687   /* Instructions that were new with the real FPA, call them V2.  */
19688 #undef  ARM_VARIANT
19689 #define ARM_VARIANT  & fpu_fpa_ext_v2
19690
19691  cCE("lfm",     c100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19692  cCL("lfmfd",   c900200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19693  cCL("lfmea",   d100200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19694  cCE("sfm",     c000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19695  cCL("sfmfd",   d000200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19696  cCL("sfmea",   c800200, 3, (RF, I4b, ADDR), fpa_ldmstm),
19697
19698 #undef  ARM_VARIANT
19699 #define ARM_VARIANT  & fpu_vfp_ext_v1xd  /* VFP V1xD (single precision).  */
19700
19701   /* Moves and type conversions.  */
19702  cCE("fcpys",   eb00a40, 2, (RVS, RVS),       vfp_sp_monadic),
19703  cCE("fmrs",    e100a10, 2, (RR, RVS),        vfp_reg_from_sp),
19704  cCE("fmsr",    e000a10, 2, (RVS, RR),        vfp_sp_from_reg),
19705  cCE("fmstat",  ef1fa10, 0, (),               noargs),
19706  cCE("vmrs",    ef00a10, 2, (APSR_RR, RVC),   vmrs),
19707  cCE("vmsr",    ee00a10, 2, (RVC, RR),        vmsr),
19708  cCE("fsitos",  eb80ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19709  cCE("fuitos",  eb80a40, 2, (RVS, RVS),       vfp_sp_monadic),
19710  cCE("ftosis",  ebd0a40, 2, (RVS, RVS),       vfp_sp_monadic),
19711  cCE("ftosizs", ebd0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19712  cCE("ftouis",  ebc0a40, 2, (RVS, RVS),       vfp_sp_monadic),
19713  cCE("ftouizs", ebc0ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19714  cCE("fmrx",    ef00a10, 2, (RR, RVC),        rd_rn),
19715  cCE("fmxr",    ee00a10, 2, (RVC, RR),        rn_rd),
19716
19717   /* Memory operations.  */
19718  cCE("flds",    d100a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19719  cCE("fsts",    d000a00, 2, (RVS, ADDRGLDC),  vfp_sp_ldst),
19720  cCE("fldmias", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19721  cCE("fldmfds", c900a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19722  cCE("fldmdbs", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19723  cCE("fldmeas", d300a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19724  cCE("fldmiax", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19725  cCE("fldmfdx", c900b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19726  cCE("fldmdbx", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19727  cCE("fldmeax", d300b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19728  cCE("fstmias", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19729  cCE("fstmeas", c800a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmia),
19730  cCE("fstmdbs", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19731  cCE("fstmfds", d200a00, 2, (RRnpctw, VRSLST),    vfp_sp_ldstmdb),
19732  cCE("fstmiax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19733  cCE("fstmeax", c800b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmia),
19734  cCE("fstmdbx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19735  cCE("fstmfdx", d200b00, 2, (RRnpctw, VRDLST),    vfp_xp_ldstmdb),
19736
19737   /* Monadic operations.  */
19738  cCE("fabss",   eb00ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19739  cCE("fnegs",   eb10a40, 2, (RVS, RVS),       vfp_sp_monadic),
19740  cCE("fsqrts",  eb10ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19741
19742   /* Dyadic operations.  */
19743  cCE("fadds",   e300a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19744  cCE("fsubs",   e300a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19745  cCE("fmuls",   e200a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19746  cCE("fdivs",   e800a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19747  cCE("fmacs",   e000a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19748  cCE("fmscs",   e100a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19749  cCE("fnmuls",  e200a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19750  cCE("fnmacs",  e000a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19751  cCE("fnmscs",  e100a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
19752
19753   /* Comparisons.  */
19754  cCE("fcmps",   eb40a40, 2, (RVS, RVS),       vfp_sp_monadic),
19755  cCE("fcmpzs",  eb50a40, 1, (RVS),            vfp_sp_compare_z),
19756  cCE("fcmpes",  eb40ac0, 2, (RVS, RVS),       vfp_sp_monadic),
19757  cCE("fcmpezs", eb50ac0, 1, (RVS),            vfp_sp_compare_z),
19758
19759  /* Double precision load/store are still present on single precision
19760     implementations.  */
19761  cCE("fldd",    d100b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19762  cCE("fstd",    d000b00, 2, (RVD, ADDRGLDC),  vfp_dp_ldst),
19763  cCE("fldmiad", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19764  cCE("fldmfdd", c900b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19765  cCE("fldmdbd", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19766  cCE("fldmead", d300b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19767  cCE("fstmiad", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19768  cCE("fstmead", c800b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmia),
19769  cCE("fstmdbd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19770  cCE("fstmfdd", d200b00, 2, (RRnpctw, VRDLST),    vfp_dp_ldstmdb),
19771
19772 #undef  ARM_VARIANT
19773 #define ARM_VARIANT  & fpu_vfp_ext_v1 /* VFP V1 (Double precision).  */
19774
19775   /* Moves and type conversions.  */
19776  cCE("fcpyd",   eb00b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19777  cCE("fcvtds",  eb70ac0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19778  cCE("fcvtsd",  eb70bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19779  cCE("fmdhr",   e200b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19780  cCE("fmdlr",   e000b10, 2, (RVD, RR),        vfp_dp_rn_rd),
19781  cCE("fmrdh",   e300b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19782  cCE("fmrdl",   e100b10, 2, (RR, RVD),        vfp_dp_rd_rn),
19783  cCE("fsitod",  eb80bc0, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19784  cCE("fuitod",  eb80b40, 2, (RVD, RVS),       vfp_dp_sp_cvt),
19785  cCE("ftosid",  ebd0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19786  cCE("ftosizd", ebd0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19787  cCE("ftouid",  ebc0b40, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19788  cCE("ftouizd", ebc0bc0, 2, (RVS, RVD),       vfp_sp_dp_cvt),
19789
19790   /* Monadic operations.  */
19791  cCE("fabsd",   eb00bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19792  cCE("fnegd",   eb10b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19793  cCE("fsqrtd",  eb10bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19794
19795   /* Dyadic operations.  */
19796  cCE("faddd",   e300b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19797  cCE("fsubd",   e300b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19798  cCE("fmuld",   e200b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19799  cCE("fdivd",   e800b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19800  cCE("fmacd",   e000b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19801  cCE("fmscd",   e100b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19802  cCE("fnmuld",  e200b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19803  cCE("fnmacd",  e000b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19804  cCE("fnmscd",  e100b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
19805
19806   /* Comparisons.  */
19807  cCE("fcmpd",   eb40b40, 2, (RVD, RVD),       vfp_dp_rd_rm),
19808  cCE("fcmpzd",  eb50b40, 1, (RVD),            vfp_dp_rd),
19809  cCE("fcmped",  eb40bc0, 2, (RVD, RVD),       vfp_dp_rd_rm),
19810  cCE("fcmpezd", eb50bc0, 1, (RVD),            vfp_dp_rd),
19811
19812 #undef  ARM_VARIANT
19813 #define ARM_VARIANT  & fpu_vfp_ext_v2
19814
19815  cCE("fmsrr",   c400a10, 3, (VRSLST, RR, RR), vfp_sp2_from_reg2),
19816  cCE("fmrrs",   c500a10, 3, (RR, RR, VRSLST), vfp_reg2_from_sp2),
19817  cCE("fmdrr",   c400b10, 3, (RVD, RR, RR),    vfp_dp_rm_rd_rn),
19818  cCE("fmrrd",   c500b10, 3, (RR, RR, RVD),    vfp_dp_rd_rn_rm),
19819
19820 /* Instructions which may belong to either the Neon or VFP instruction sets.
19821    Individual encoder functions perform additional architecture checks.  */
19822 #undef  ARM_VARIANT
19823 #define ARM_VARIANT    & fpu_vfp_ext_v1xd
19824 #undef  THUMB_VARIANT
19825 #define THUMB_VARIANT  & fpu_vfp_ext_v1xd
19826
19827   /* These mnemonics are unique to VFP.  */
19828  NCE(vsqrt,     0,       2, (RVSD, RVSD),       vfp_nsyn_sqrt),
19829  NCE(vdiv,      0,       3, (RVSD, RVSD, RVSD), vfp_nsyn_div),
19830  nCE(vnmul,     _vnmul,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19831  nCE(vnmla,     _vnmla,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19832  nCE(vnmls,     _vnmls,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
19833  nCE(vcmp,      _vcmp,    2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
19834  nCE(vcmpe,     _vcmpe,   2, (RVSD, RSVD_FI0),    vfp_nsyn_cmp),
19835  NCE(vpush,     0,       1, (VRSDLST),          vfp_nsyn_push),
19836  NCE(vpop,      0,       1, (VRSDLST),          vfp_nsyn_pop),
19837  NCE(vcvtz,     0,       2, (RVSD, RVSD),       vfp_nsyn_cvtz),
19838
19839   /* Mnemonics shared by Neon and VFP.  */
19840  nCEF(vmul,     _vmul,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mul),
19841  nCEF(vmla,     _vmla,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19842  nCEF(vmls,     _vmls,    3, (RNSDQ, oRNSDQ, RNSDQ_RNSC), neon_mac_maybe_scalar),
19843
19844  nCEF(vadd,     _vadd,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19845  nCEF(vsub,     _vsub,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_addsub_if_i),
19846
19847  NCEF(vabs,     1b10300, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19848  NCEF(vneg,     1b10380, 2, (RNSDQ, RNSDQ), neon_abs_neg),
19849
19850  NCE(vldm,      c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19851  NCE(vldmia,    c900b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19852  NCE(vldmdb,    d100b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19853  NCE(vstm,      c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19854  NCE(vstmia,    c800b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19855  NCE(vstmdb,    d000b00, 2, (RRnpctw, VRSDLST), neon_ldm_stm),
19856  NCE(vldr,      d100b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19857  NCE(vstr,      d000b00, 2, (RVSD, ADDRGLDC), neon_ldr_str),
19858
19859  nCEF(vcvt,     _vcvt,   3, (RNSDQ, RNSDQ, oI32z), neon_cvt),
19860  nCEF(vcvtr,    _vcvt,   2, (RNSDQ, RNSDQ), neon_cvtr),
19861  NCEF(vcvtb,    eb20a40, 2, (RVSD, RVSD), neon_cvtb),
19862  NCEF(vcvtt,    eb20a40, 2, (RVSD, RVSD), neon_cvtt),
19863
19864
19865   /* NOTE: All VMOV encoding is special-cased!  */
19866  NCE(vmov,      0,       1, (VMOV), neon_mov),
19867  NCE(vmovq,     0,       1, (VMOV), neon_mov),
19868
19869 #undef  THUMB_VARIANT
19870 #define THUMB_VARIANT  & fpu_neon_ext_v1
19871 #undef  ARM_VARIANT
19872 #define ARM_VARIANT    & fpu_neon_ext_v1
19873
19874   /* Data processing with three registers of the same length.  */
19875   /* integer ops, valid types S8 S16 S32 U8 U16 U32.  */
19876  NUF(vaba,      0000710, 3, (RNDQ, RNDQ,  RNDQ), neon_dyadic_i_su),
19877  NUF(vabaq,     0000710, 3, (RNQ,  RNQ,   RNQ),  neon_dyadic_i_su),
19878  NUF(vhadd,     0000000, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19879  NUF(vhaddq,    0000000, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19880  NUF(vrhadd,    0000100, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19881  NUF(vrhaddq,   0000100, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19882  NUF(vhsub,     0000200, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i_su),
19883  NUF(vhsubq,    0000200, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i_su),
19884   /* integer ops, valid types S8 S16 S32 S64 U8 U16 U32 U64.  */
19885  NUF(vqadd,     0000010, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19886  NUF(vqaddq,    0000010, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19887  NUF(vqsub,     0000210, 3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_i64_su),
19888  NUF(vqsubq,    0000210, 3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_i64_su),
19889  NUF(vrshl,     0000500, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19890  NUF(vrshlq,    0000500, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19891  NUF(vqrshl,    0000510, 3, (RNDQ, oRNDQ, RNDQ), neon_rshl),
19892  NUF(vqrshlq,   0000510, 3, (RNQ,  oRNQ,  RNQ),  neon_rshl),
19893   /* If not immediate, fall back to neon_dyadic_i64_su.
19894      shl_imm should accept I8 I16 I32 I64,
19895      qshl_imm should accept S8 S16 S32 S64 U8 U16 U32 U64.  */
19896  nUF(vshl,      _vshl,    3, (RNDQ, oRNDQ, RNDQ_I63b), neon_shl_imm),
19897  nUF(vshlq,     _vshl,    3, (RNQ,  oRNQ,  RNDQ_I63b), neon_shl_imm),
19898  nUF(vqshl,     _vqshl,   3, (RNDQ, oRNDQ, RNDQ_I63b), neon_qshl_imm),
19899  nUF(vqshlq,    _vqshl,   3, (RNQ,  oRNQ,  RNDQ_I63b), neon_qshl_imm),
19900   /* Logic ops, types optional & ignored.  */
19901  nUF(vand,      _vand,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19902  nUF(vandq,     _vand,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19903  nUF(vbic,      _vbic,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19904  nUF(vbicq,     _vbic,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19905  nUF(vorr,      _vorr,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19906  nUF(vorrq,     _vorr,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19907  nUF(vorn,      _vorn,    3, (RNDQ, oRNDQ, RNDQ_Ibig), neon_logic),
19908  nUF(vornq,     _vorn,    3, (RNQ,  oRNQ,  RNDQ_Ibig), neon_logic),
19909  nUF(veor,      _veor,    3, (RNDQ, oRNDQ, RNDQ),      neon_logic),
19910  nUF(veorq,     _veor,    3, (RNQ,  oRNQ,  RNQ),       neon_logic),
19911   /* Bitfield ops, untyped.  */
19912  NUF(vbsl,      1100110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19913  NUF(vbslq,     1100110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19914  NUF(vbit,      1200110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19915  NUF(vbitq,     1200110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19916  NUF(vbif,      1300110, 3, (RNDQ, RNDQ, RNDQ), neon_bitfield),
19917  NUF(vbifq,     1300110, 3, (RNQ,  RNQ,  RNQ),  neon_bitfield),
19918   /* Int and float variants, types S8 S16 S32 U8 U16 U32 F32.  */
19919  nUF(vabd,      _vabd,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19920  nUF(vabdq,     _vabd,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19921  nUF(vmax,      _vmax,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19922  nUF(vmaxq,     _vmax,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19923  nUF(vmin,      _vmin,    3, (RNDQ, oRNDQ, RNDQ), neon_dyadic_if_su),
19924  nUF(vminq,     _vmin,    3, (RNQ,  oRNQ,  RNQ),  neon_dyadic_if_su),
19925   /* Comparisons. Types S8 S16 S32 U8 U16 U32 F32. Non-immediate versions fall
19926      back to neon_dyadic_if_su.  */
19927  nUF(vcge,      _vcge,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19928  nUF(vcgeq,     _vcge,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19929  nUF(vcgt,      _vcgt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp),
19930  nUF(vcgtq,     _vcgt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp),
19931  nUF(vclt,      _vclt,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19932  nUF(vcltq,     _vclt,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19933  nUF(vcle,      _vcle,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_cmp_inv),
19934  nUF(vcleq,     _vcle,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_cmp_inv),
19935   /* Comparison. Type I8 I16 I32 F32.  */
19936  nUF(vceq,      _vceq,    3, (RNDQ, oRNDQ, RNDQ_I0), neon_ceq),
19937  nUF(vceqq,     _vceq,    3, (RNQ,  oRNQ,  RNDQ_I0), neon_ceq),
19938   /* As above, D registers only.  */
19939  nUF(vpmax,     _vpmax,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19940  nUF(vpmin,     _vpmin,   3, (RND, oRND, RND), neon_dyadic_if_su_d),
19941   /* Int and float variants, signedness unimportant.  */
19942  nUF(vmlaq,     _vmla,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19943  nUF(vmlsq,     _vmls,    3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mac_maybe_scalar),
19944  nUF(vpadd,     _vpadd,   3, (RND,  oRND,  RND),       neon_dyadic_if_i_d),
19945   /* Add/sub take types I8 I16 I32 I64 F32.  */
19946  nUF(vaddq,     _vadd,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19947  nUF(vsubq,     _vsub,    3, (RNQ,  oRNQ,  RNQ),  neon_addsub_if_i),
19948   /* vtst takes sizes 8, 16, 32.  */
19949  NUF(vtst,      0000810, 3, (RNDQ, oRNDQ, RNDQ), neon_tst),
19950  NUF(vtstq,     0000810, 3, (RNQ,  oRNQ,  RNQ),  neon_tst),
19951   /* VMUL takes I8 I16 I32 F32 P8.  */
19952  nUF(vmulq,     _vmul,     3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_mul),
19953   /* VQD{R}MULH takes S16 S32.  */
19954  nUF(vqdmulh,   _vqdmulh,  3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19955  nUF(vqdmulhq,  _vqdmulh,  3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19956  nUF(vqrdmulh,  _vqrdmulh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19957  nUF(vqrdmulhq, _vqrdmulh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19958  NUF(vacge,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19959  NUF(vacgeq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19960  NUF(vacgt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute),
19961  NUF(vacgtq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute),
19962  NUF(vaclt,     0200e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19963  NUF(vacltq,    0200e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19964  NUF(vacle,     0000e10,  3, (RNDQ, oRNDQ, RNDQ), neon_fcmp_absolute_inv),
19965  NUF(vacleq,    0000e10,  3, (RNQ,  oRNQ,  RNQ),  neon_fcmp_absolute_inv),
19966  NUF(vrecps,    0000f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19967  NUF(vrecpsq,   0000f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19968  NUF(vrsqrts,   0200f10,  3, (RNDQ, oRNDQ, RNDQ), neon_step),
19969  NUF(vrsqrtsq,  0200f10,  3, (RNQ,  oRNQ,  RNQ),  neon_step),
19970  /* ARM v8.1 extension.  */
19971  nUF(vqrdmlah,  _vqrdmlah, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19972  nUF(vqrdmlahq, _vqrdmlah, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19973  nUF(vqrdmlsh,  _vqrdmlsh, 3, (RNDQ, oRNDQ, RNDQ_RNSC), neon_qdmulh),
19974  nUF(vqrdmlshq, _vqrdmlsh, 3, (RNQ,  oRNQ,  RNDQ_RNSC), neon_qdmulh),
19975
19976   /* Two address, int/float. Types S8 S16 S32 F32.  */
19977  NUF(vabsq,     1b10300, 2, (RNQ,  RNQ),      neon_abs_neg),
19978  NUF(vnegq,     1b10380, 2, (RNQ,  RNQ),      neon_abs_neg),
19979
19980   /* Data processing with two registers and a shift amount.  */
19981   /* Right shifts, and variants with rounding.
19982      Types accepted S8 S16 S32 S64 U8 U16 U32 U64.  */
19983  NUF(vshr,      0800010, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19984  NUF(vshrq,     0800010, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19985  NUF(vrshr,     0800210, 3, (RNDQ, oRNDQ, I64z), neon_rshift_round_imm),
19986  NUF(vrshrq,    0800210, 3, (RNQ,  oRNQ,  I64z), neon_rshift_round_imm),
19987  NUF(vsra,      0800110, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19988  NUF(vsraq,     0800110, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19989  NUF(vrsra,     0800310, 3, (RNDQ, oRNDQ, I64),  neon_rshift_round_imm),
19990  NUF(vrsraq,    0800310, 3, (RNQ,  oRNQ,  I64),  neon_rshift_round_imm),
19991   /* Shift and insert. Sizes accepted 8 16 32 64.  */
19992  NUF(vsli,      1800510, 3, (RNDQ, oRNDQ, I63), neon_sli),
19993  NUF(vsliq,     1800510, 3, (RNQ,  oRNQ,  I63), neon_sli),
19994  NUF(vsri,      1800410, 3, (RNDQ, oRNDQ, I64), neon_sri),
19995  NUF(vsriq,     1800410, 3, (RNQ,  oRNQ,  I64), neon_sri),
19996   /* QSHL{U} immediate accepts S8 S16 S32 S64 U8 U16 U32 U64.  */
19997  NUF(vqshlu,    1800610, 3, (RNDQ, oRNDQ, I63), neon_qshlu_imm),
19998  NUF(vqshluq,   1800610, 3, (RNQ,  oRNQ,  I63), neon_qshlu_imm),
19999   /* Right shift immediate, saturating & narrowing, with rounding variants.
20000      Types accepted S16 S32 S64 U16 U32 U64.  */
20001  NUF(vqshrn,    0800910, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20002  NUF(vqrshrn,   0800950, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow),
20003   /* As above, unsigned. Types accepted S16 S32 S64.  */
20004  NUF(vqshrun,   0800810, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20005  NUF(vqrshrun,  0800850, 3, (RND, RNQ, I32z), neon_rshift_sat_narrow_u),
20006   /* Right shift narrowing. Types accepted I16 I32 I64.  */
20007  NUF(vshrn,     0800810, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20008  NUF(vrshrn,    0800850, 3, (RND, RNQ, I32z), neon_rshift_narrow),
20009   /* Special case. Types S8 S16 S32 U8 U16 U32. Handles max shift variant.  */
20010  nUF(vshll,     _vshll,   3, (RNQ, RND, I32),  neon_shll),
20011   /* CVT with optional immediate for fixed-point variant.  */
20012  nUF(vcvtq,     _vcvt,    3, (RNQ, RNQ, oI32b), neon_cvt),
20013
20014  nUF(vmvn,      _vmvn,    2, (RNDQ, RNDQ_Ibig), neon_mvn),
20015  nUF(vmvnq,     _vmvn,    2, (RNQ,  RNDQ_Ibig), neon_mvn),
20016
20017   /* Data processing, three registers of different lengths.  */
20018   /* Dyadic, long insns. Types S8 S16 S32 U8 U16 U32.  */
20019  NUF(vabal,     0800500, 3, (RNQ, RND, RND),  neon_abal),
20020  NUF(vabdl,     0800700, 3, (RNQ, RND, RND),  neon_dyadic_long),
20021  NUF(vaddl,     0800000, 3, (RNQ, RND, RND),  neon_dyadic_long),
20022  NUF(vsubl,     0800200, 3, (RNQ, RND, RND),  neon_dyadic_long),
20023   /* If not scalar, fall back to neon_dyadic_long.
20024      Vector types as above, scalar types S16 S32 U16 U32.  */
20025  nUF(vmlal,     _vmlal,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20026  nUF(vmlsl,     _vmlsl,   3, (RNQ, RND, RND_RNSC), neon_mac_maybe_scalar_long),
20027   /* Dyadic, widening insns. Types S8 S16 S32 U8 U16 U32.  */
20028  NUF(vaddw,     0800100, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20029  NUF(vsubw,     0800300, 3, (RNQ, oRNQ, RND), neon_dyadic_wide),
20030   /* Dyadic, narrowing insns. Types I16 I32 I64.  */
20031  NUF(vaddhn,    0800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20032  NUF(vraddhn,   1800400, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20033  NUF(vsubhn,    0800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20034  NUF(vrsubhn,   1800600, 3, (RND, RNQ, RNQ),  neon_dyadic_narrow),
20035   /* Saturating doubling multiplies. Types S16 S32.  */
20036  nUF(vqdmlal,   _vqdmlal, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20037  nUF(vqdmlsl,   _vqdmlsl, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20038  nUF(vqdmull,   _vqdmull, 3, (RNQ, RND, RND_RNSC), neon_mul_sat_scalar_long),
20039   /* VMULL. Vector types S8 S16 S32 U8 U16 U32 P8, scalar types
20040      S16 S32 U16 U32.  */
20041  nUF(vmull,     _vmull,   3, (RNQ, RND, RND_RNSC), neon_vmull),
20042
20043   /* Extract. Size 8.  */
20044  NUF(vext,      0b00000, 4, (RNDQ, oRNDQ, RNDQ, I15), neon_ext),
20045  NUF(vextq,     0b00000, 4, (RNQ,  oRNQ,  RNQ,  I15), neon_ext),
20046
20047   /* Two registers, miscellaneous.  */
20048   /* Reverse. Sizes 8 16 32 (must be < size in opcode).  */
20049  NUF(vrev64,    1b00000, 2, (RNDQ, RNDQ),     neon_rev),
20050  NUF(vrev64q,   1b00000, 2, (RNQ,  RNQ),      neon_rev),
20051  NUF(vrev32,    1b00080, 2, (RNDQ, RNDQ),     neon_rev),
20052  NUF(vrev32q,   1b00080, 2, (RNQ,  RNQ),      neon_rev),
20053  NUF(vrev16,    1b00100, 2, (RNDQ, RNDQ),     neon_rev),
20054  NUF(vrev16q,   1b00100, 2, (RNQ,  RNQ),      neon_rev),
20055   /* Vector replicate. Sizes 8 16 32.  */
20056  nCE(vdup,      _vdup,    2, (RNDQ, RR_RNSC),  neon_dup),
20057  nCE(vdupq,     _vdup,    2, (RNQ,  RR_RNSC),  neon_dup),
20058   /* VMOVL. Types S8 S16 S32 U8 U16 U32.  */
20059  NUF(vmovl,     0800a10, 2, (RNQ, RND),       neon_movl),
20060   /* VMOVN. Types I16 I32 I64.  */
20061  nUF(vmovn,     _vmovn,   2, (RND, RNQ),       neon_movn),
20062   /* VQMOVN. Types S16 S32 S64 U16 U32 U64.  */
20063  nUF(vqmovn,    _vqmovn,  2, (RND, RNQ),       neon_qmovn),
20064   /* VQMOVUN. Types S16 S32 S64.  */
20065  nUF(vqmovun,   _vqmovun, 2, (RND, RNQ),       neon_qmovun),
20066   /* VZIP / VUZP. Sizes 8 16 32.  */
20067  NUF(vzip,      1b20180, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20068  NUF(vzipq,     1b20180, 2, (RNQ,  RNQ),      neon_zip_uzp),
20069  NUF(vuzp,      1b20100, 2, (RNDQ, RNDQ),     neon_zip_uzp),
20070  NUF(vuzpq,     1b20100, 2, (RNQ,  RNQ),      neon_zip_uzp),
20071   /* VQABS / VQNEG. Types S8 S16 S32.  */
20072  NUF(vqabs,     1b00700, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20073  NUF(vqabsq,    1b00700, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20074  NUF(vqneg,     1b00780, 2, (RNDQ, RNDQ),     neon_sat_abs_neg),
20075  NUF(vqnegq,    1b00780, 2, (RNQ,  RNQ),      neon_sat_abs_neg),
20076   /* Pairwise, lengthening. Types S8 S16 S32 U8 U16 U32.  */
20077  NUF(vpadal,    1b00600, 2, (RNDQ, RNDQ),     neon_pair_long),
20078  NUF(vpadalq,   1b00600, 2, (RNQ,  RNQ),      neon_pair_long),
20079  NUF(vpaddl,    1b00200, 2, (RNDQ, RNDQ),     neon_pair_long),
20080  NUF(vpaddlq,   1b00200, 2, (RNQ,  RNQ),      neon_pair_long),
20081   /* Reciprocal estimates. Types U32 F32.  */
20082  NUF(vrecpe,    1b30400, 2, (RNDQ, RNDQ),     neon_recip_est),
20083  NUF(vrecpeq,   1b30400, 2, (RNQ,  RNQ),      neon_recip_est),
20084  NUF(vrsqrte,   1b30480, 2, (RNDQ, RNDQ),     neon_recip_est),
20085  NUF(vrsqrteq,  1b30480, 2, (RNQ,  RNQ),      neon_recip_est),
20086   /* VCLS. Types S8 S16 S32.  */
20087  NUF(vcls,      1b00400, 2, (RNDQ, RNDQ),     neon_cls),
20088  NUF(vclsq,     1b00400, 2, (RNQ,  RNQ),      neon_cls),
20089   /* VCLZ. Types I8 I16 I32.  */
20090  NUF(vclz,      1b00480, 2, (RNDQ, RNDQ),     neon_clz),
20091  NUF(vclzq,     1b00480, 2, (RNQ,  RNQ),      neon_clz),
20092   /* VCNT. Size 8.  */
20093  NUF(vcnt,      1b00500, 2, (RNDQ, RNDQ),     neon_cnt),
20094  NUF(vcntq,     1b00500, 2, (RNQ,  RNQ),      neon_cnt),
20095   /* Two address, untyped.  */
20096  NUF(vswp,      1b20000, 2, (RNDQ, RNDQ),     neon_swp),
20097  NUF(vswpq,     1b20000, 2, (RNQ,  RNQ),      neon_swp),
20098   /* VTRN. Sizes 8 16 32.  */
20099  nUF(vtrn,      _vtrn,    2, (RNDQ, RNDQ),     neon_trn),
20100  nUF(vtrnq,     _vtrn,    2, (RNQ,  RNQ),      neon_trn),
20101
20102   /* Table lookup. Size 8.  */
20103  NUF(vtbl,      1b00800, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20104  NUF(vtbx,      1b00840, 3, (RND, NRDLST, RND), neon_tbl_tbx),
20105
20106 #undef  THUMB_VARIANT
20107 #define THUMB_VARIANT  & fpu_vfp_v3_or_neon_ext
20108 #undef  ARM_VARIANT
20109 #define ARM_VARIANT    & fpu_vfp_v3_or_neon_ext
20110
20111   /* Neon element/structure load/store.  */
20112  nUF(vld1,      _vld1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20113  nUF(vst1,      _vst1,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20114  nUF(vld2,      _vld2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20115  nUF(vst2,      _vst2,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20116  nUF(vld3,      _vld3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20117  nUF(vst3,      _vst3,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20118  nUF(vld4,      _vld4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20119  nUF(vst4,      _vst4,    2, (NSTRLST, ADDR),  neon_ldx_stx),
20120
20121 #undef  THUMB_VARIANT
20122 #define THUMB_VARIANT & fpu_vfp_ext_v3xd
20123 #undef  ARM_VARIANT
20124 #define ARM_VARIANT   & fpu_vfp_ext_v3xd
20125  cCE("fconsts",   eb00a00, 2, (RVS, I255),      vfp_sp_const),
20126  cCE("fshtos",    eba0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20127  cCE("fsltos",    eba0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20128  cCE("fuhtos",    ebb0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20129  cCE("fultos",    ebb0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20130  cCE("ftoshs",    ebe0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20131  cCE("ftosls",    ebe0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20132  cCE("ftouhs",    ebf0a40, 2, (RVS, I16z),      vfp_sp_conv_16),
20133  cCE("ftouls",    ebf0ac0, 2, (RVS, I32),       vfp_sp_conv_32),
20134
20135 #undef  THUMB_VARIANT
20136 #define THUMB_VARIANT  & fpu_vfp_ext_v3
20137 #undef  ARM_VARIANT
20138 #define ARM_VARIANT    & fpu_vfp_ext_v3
20139
20140  cCE("fconstd",   eb00b00, 2, (RVD, I255),      vfp_dp_const),
20141  cCE("fshtod",    eba0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20142  cCE("fsltod",    eba0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20143  cCE("fuhtod",    ebb0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20144  cCE("fultod",    ebb0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20145  cCE("ftoshd",    ebe0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20146  cCE("ftosld",    ebe0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20147  cCE("ftouhd",    ebf0b40, 2, (RVD, I16z),      vfp_dp_conv_16),
20148  cCE("ftould",    ebf0bc0, 2, (RVD, I32),       vfp_dp_conv_32),
20149
20150 #undef  ARM_VARIANT
20151 #define ARM_VARIANT    & fpu_vfp_ext_fma
20152 #undef  THUMB_VARIANT
20153 #define THUMB_VARIANT  & fpu_vfp_ext_fma
20154  /* Mnemonics shared by Neon and VFP.  These are included in the
20155     VFP FMA variant; NEON and VFP FMA always includes the NEON
20156     FMA instructions.  */
20157  nCEF(vfma,     _vfma,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20158  nCEF(vfms,     _vfms,    3, (RNSDQ, oRNSDQ, RNSDQ), neon_fmac),
20159  /* ffmas/ffmad/ffmss/ffmsd are dummy mnemonics to satisfy gas;
20160     the v form should always be used.  */
20161  cCE("ffmas",   ea00a00, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20162  cCE("ffnmas",  ea00a40, 3, (RVS, RVS, RVS),  vfp_sp_dyadic),
20163  cCE("ffmad",   ea00b00, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20164  cCE("ffnmad",  ea00b40, 3, (RVD, RVD, RVD),  vfp_dp_rd_rn_rm),
20165  nCE(vfnma,     _vfnma,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20166  nCE(vfnms,     _vfnms,   3, (RVSD, RVSD, RVSD), vfp_nsyn_nmul),
20167
20168 #undef THUMB_VARIANT
20169 #undef  ARM_VARIANT
20170 #define ARM_VARIANT  & arm_cext_xscale /* Intel XScale extensions.  */
20171
20172  cCE("mia",     e200010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20173  cCE("miaph",   e280010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20174  cCE("miabb",   e2c0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20175  cCE("miabt",   e2d0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20176  cCE("miatb",   e2e0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20177  cCE("miatt",   e2f0010, 3, (RXA, RRnpc, RRnpc), xsc_mia),
20178  cCE("mar",     c400000, 3, (RXA, RRnpc, RRnpc), xsc_mar),
20179  cCE("mra",     c500000, 3, (RRnpc, RRnpc, RXA), xsc_mra),
20180
20181 #undef  ARM_VARIANT
20182 #define ARM_VARIANT  & arm_cext_iwmmxt /* Intel Wireless MMX technology.  */
20183
20184  cCE("tandcb",  e13f130, 1, (RR),                   iwmmxt_tandorc),
20185  cCE("tandch",  e53f130, 1, (RR),                   iwmmxt_tandorc),
20186  cCE("tandcw",  e93f130, 1, (RR),                   iwmmxt_tandorc),
20187  cCE("tbcstb",  e400010, 2, (RIWR, RR),             rn_rd),
20188  cCE("tbcsth",  e400050, 2, (RIWR, RR),             rn_rd),
20189  cCE("tbcstw",  e400090, 2, (RIWR, RR),             rn_rd),
20190  cCE("textrcb", e130170, 2, (RR, I7),               iwmmxt_textrc),
20191  cCE("textrch", e530170, 2, (RR, I7),               iwmmxt_textrc),
20192  cCE("textrcw", e930170, 2, (RR, I7),               iwmmxt_textrc),
20193  cCE("textrmub",e100070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20194  cCE("textrmuh",e500070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20195  cCE("textrmuw",e900070, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20196  cCE("textrmsb",e100078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20197  cCE("textrmsh",e500078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20198  cCE("textrmsw",e900078, 3, (RR, RIWR, I7),         iwmmxt_textrm),
20199  cCE("tinsrb",  e600010, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20200  cCE("tinsrh",  e600050, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20201  cCE("tinsrw",  e600090, 3, (RIWR, RR, I7),         iwmmxt_tinsr),
20202  cCE("tmcr",    e000110, 2, (RIWC_RIWG, RR),        rn_rd),
20203  cCE("tmcrr",   c400000, 3, (RIWR, RR, RR),         rm_rd_rn),
20204  cCE("tmia",    e200010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20205  cCE("tmiaph",  e280010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20206  cCE("tmiabb",  e2c0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20207  cCE("tmiabt",  e2d0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20208  cCE("tmiatb",  e2e0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20209  cCE("tmiatt",  e2f0010, 3, (RIWR, RR, RR),         iwmmxt_tmia),
20210  cCE("tmovmskb",e100030, 2, (RR, RIWR),             rd_rn),
20211  cCE("tmovmskh",e500030, 2, (RR, RIWR),             rd_rn),
20212  cCE("tmovmskw",e900030, 2, (RR, RIWR),             rd_rn),
20213  cCE("tmrc",    e100110, 2, (RR, RIWC_RIWG),        rd_rn),
20214  cCE("tmrrc",   c500000, 3, (RR, RR, RIWR),         rd_rn_rm),
20215  cCE("torcb",   e13f150, 1, (RR),                   iwmmxt_tandorc),
20216  cCE("torch",   e53f150, 1, (RR),                   iwmmxt_tandorc),
20217  cCE("torcw",   e93f150, 1, (RR),                   iwmmxt_tandorc),
20218  cCE("waccb",   e0001c0, 2, (RIWR, RIWR),           rd_rn),
20219  cCE("wacch",   e4001c0, 2, (RIWR, RIWR),           rd_rn),
20220  cCE("waccw",   e8001c0, 2, (RIWR, RIWR),           rd_rn),
20221  cCE("waddbss", e300180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20222  cCE("waddb",   e000180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20223  cCE("waddbus", e100180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20224  cCE("waddhss", e700180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20225  cCE("waddh",   e400180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20226  cCE("waddhus", e500180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20227  cCE("waddwss", eb00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20228  cCE("waddw",   e800180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20229  cCE("waddwus", e900180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20230  cCE("waligni", e000020, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_waligni),
20231  cCE("walignr0",e800020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20232  cCE("walignr1",e900020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20233  cCE("walignr2",ea00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20234  cCE("walignr3",eb00020, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20235  cCE("wand",    e200000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20236  cCE("wandn",   e300000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20237  cCE("wavg2b",  e800000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20238  cCE("wavg2br", e900000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20239  cCE("wavg2h",  ec00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20240  cCE("wavg2hr", ed00000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20241  cCE("wcmpeqb", e000060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20242  cCE("wcmpeqh", e400060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20243  cCE("wcmpeqw", e800060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20244  cCE("wcmpgtub",e100060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20245  cCE("wcmpgtuh",e500060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20246  cCE("wcmpgtuw",e900060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20247  cCE("wcmpgtsb",e300060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20248  cCE("wcmpgtsh",e700060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20249  cCE("wcmpgtsw",eb00060, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20250  cCE("wldrb",   c100000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20251  cCE("wldrh",   c500000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20252  cCE("wldrw",   c100100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20253  cCE("wldrd",   c500100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20254  cCE("wmacs",   e600100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20255  cCE("wmacsz",  e700100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20256  cCE("wmacu",   e400100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20257  cCE("wmacuz",  e500100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20258  cCE("wmadds",  ea00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20259  cCE("wmaddu",  e800100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20260  cCE("wmaxsb",  e200160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20261  cCE("wmaxsh",  e600160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20262  cCE("wmaxsw",  ea00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20263  cCE("wmaxub",  e000160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20264  cCE("wmaxuh",  e400160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20265  cCE("wmaxuw",  e800160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20266  cCE("wminsb",  e300160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20267  cCE("wminsh",  e700160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20268  cCE("wminsw",  eb00160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20269  cCE("wminub",  e100160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20270  cCE("wminuh",  e500160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20271  cCE("wminuw",  e900160, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20272  cCE("wmov",    e000000, 2, (RIWR, RIWR),           iwmmxt_wmov),
20273  cCE("wmulsm",  e300100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20274  cCE("wmulsl",  e200100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20275  cCE("wmulum",  e100100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20276  cCE("wmulul",  e000100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20277  cCE("wor",     e000000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20278  cCE("wpackhss",e700080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20279  cCE("wpackhus",e500080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20280  cCE("wpackwss",eb00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20281  cCE("wpackwus",e900080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20282  cCE("wpackdss",ef00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20283  cCE("wpackdus",ed00080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20284  cCE("wrorh",   e700040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20285  cCE("wrorhg",  e700148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20286  cCE("wrorw",   eb00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20287  cCE("wrorwg",  eb00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20288  cCE("wrord",   ef00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20289  cCE("wrordg",  ef00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20290  cCE("wsadb",   e000120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20291  cCE("wsadbz",  e100120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20292  cCE("wsadh",   e400120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20293  cCE("wsadhz",  e500120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20294  cCE("wshufh",  e0001e0, 3, (RIWR, RIWR, I255),     iwmmxt_wshufh),
20295  cCE("wsllh",   e500040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20296  cCE("wsllhg",  e500148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20297  cCE("wsllw",   e900040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20298  cCE("wsllwg",  e900148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20299  cCE("wslld",   ed00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20300  cCE("wslldg",  ed00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20301  cCE("wsrah",   e400040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20302  cCE("wsrahg",  e400148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20303  cCE("wsraw",   e800040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20304  cCE("wsrawg",  e800148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20305  cCE("wsrad",   ec00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20306  cCE("wsradg",  ec00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20307  cCE("wsrlh",   e600040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20308  cCE("wsrlhg",  e600148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20309  cCE("wsrlw",   ea00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20310  cCE("wsrlwg",  ea00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20311  cCE("wsrld",   ee00040, 3, (RIWR, RIWR, RIWR_I32z),iwmmxt_wrwrwr_or_imm5),
20312  cCE("wsrldg",  ee00148, 3, (RIWR, RIWR, RIWG),     rd_rn_rm),
20313  cCE("wstrb",   c000000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20314  cCE("wstrh",   c400000, 2, (RIWR, ADDR),           iwmmxt_wldstbh),
20315  cCE("wstrw",   c000100, 2, (RIWR_RIWC, ADDR),      iwmmxt_wldstw),
20316  cCE("wstrd",   c400100, 2, (RIWR, ADDR),           iwmmxt_wldstd),
20317  cCE("wsubbss", e3001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20318  cCE("wsubb",   e0001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20319  cCE("wsubbus", e1001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20320  cCE("wsubhss", e7001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20321  cCE("wsubh",   e4001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20322  cCE("wsubhus", e5001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20323  cCE("wsubwss", eb001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20324  cCE("wsubw",   e8001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20325  cCE("wsubwus", e9001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20326  cCE("wunpckehub",e0000c0, 2, (RIWR, RIWR),         rd_rn),
20327  cCE("wunpckehuh",e4000c0, 2, (RIWR, RIWR),         rd_rn),
20328  cCE("wunpckehuw",e8000c0, 2, (RIWR, RIWR),         rd_rn),
20329  cCE("wunpckehsb",e2000c0, 2, (RIWR, RIWR),         rd_rn),
20330  cCE("wunpckehsh",e6000c0, 2, (RIWR, RIWR),         rd_rn),
20331  cCE("wunpckehsw",ea000c0, 2, (RIWR, RIWR),         rd_rn),
20332  cCE("wunpckihb", e1000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20333  cCE("wunpckihh", e5000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20334  cCE("wunpckihw", e9000c0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20335  cCE("wunpckelub",e0000e0, 2, (RIWR, RIWR),         rd_rn),
20336  cCE("wunpckeluh",e4000e0, 2, (RIWR, RIWR),         rd_rn),
20337  cCE("wunpckeluw",e8000e0, 2, (RIWR, RIWR),         rd_rn),
20338  cCE("wunpckelsb",e2000e0, 2, (RIWR, RIWR),         rd_rn),
20339  cCE("wunpckelsh",e6000e0, 2, (RIWR, RIWR),         rd_rn),
20340  cCE("wunpckelsw",ea000e0, 2, (RIWR, RIWR),         rd_rn),
20341  cCE("wunpckilb", e1000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20342  cCE("wunpckilh", e5000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20343  cCE("wunpckilw", e9000e0, 3, (RIWR, RIWR, RIWR),           rd_rn_rm),
20344  cCE("wxor",    e100000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20345  cCE("wzero",   e300000, 1, (RIWR),                 iwmmxt_wzero),
20346
20347 #undef  ARM_VARIANT
20348 #define ARM_VARIANT  & arm_cext_iwmmxt2 /* Intel Wireless MMX technology, version 2.  */
20349
20350  cCE("torvscb",   e12f190, 1, (RR),                 iwmmxt_tandorc),
20351  cCE("torvsch",   e52f190, 1, (RR),                 iwmmxt_tandorc),
20352  cCE("torvscw",   e92f190, 1, (RR),                 iwmmxt_tandorc),
20353  cCE("wabsb",     e2001c0, 2, (RIWR, RIWR),           rd_rn),
20354  cCE("wabsh",     e6001c0, 2, (RIWR, RIWR),           rd_rn),
20355  cCE("wabsw",     ea001c0, 2, (RIWR, RIWR),           rd_rn),
20356  cCE("wabsdiffb", e1001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20357  cCE("wabsdiffh", e5001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20358  cCE("wabsdiffw", e9001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20359  cCE("waddbhusl", e2001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20360  cCE("waddbhusm", e6001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20361  cCE("waddhc",    e600180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20362  cCE("waddwc",    ea00180, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20363  cCE("waddsubhx", ea001a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20364  cCE("wavg4",   e400000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20365  cCE("wavg4r",    e500000, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20366  cCE("wmaddsn",   ee00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20367  cCE("wmaddsx",   eb00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20368  cCE("wmaddun",   ec00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20369  cCE("wmaddux",   e900100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20370  cCE("wmerge",    e000080, 4, (RIWR, RIWR, RIWR, I7), iwmmxt_wmerge),
20371  cCE("wmiabb",    e0000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20372  cCE("wmiabt",    e1000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20373  cCE("wmiatb",    e2000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20374  cCE("wmiatt",    e3000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20375  cCE("wmiabbn",   e4000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20376  cCE("wmiabtn",   e5000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20377  cCE("wmiatbn",   e6000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20378  cCE("wmiattn",   e7000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20379  cCE("wmiawbb",   e800120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20380  cCE("wmiawbt",   e900120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20381  cCE("wmiawtb",   ea00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20382  cCE("wmiawtt",   eb00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20383  cCE("wmiawbbn",  ec00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20384  cCE("wmiawbtn",  ed00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20385  cCE("wmiawtbn",  ee00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20386  cCE("wmiawttn",  ef00120, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20387  cCE("wmulsmr",   ef00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20388  cCE("wmulumr",   ed00100, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20389  cCE("wmulwumr",  ec000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20390  cCE("wmulwsmr",  ee000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20391  cCE("wmulwum",   ed000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20392  cCE("wmulwsm",   ef000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20393  cCE("wmulwl",    eb000c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20394  cCE("wqmiabb",   e8000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20395  cCE("wqmiabt",   e9000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20396  cCE("wqmiatb",   ea000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20397  cCE("wqmiatt",   eb000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20398  cCE("wqmiabbn",  ec000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20399  cCE("wqmiabtn",  ed000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20400  cCE("wqmiatbn",  ee000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20401  cCE("wqmiattn",  ef000a0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20402  cCE("wqmulm",    e100080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20403  cCE("wqmulmr",   e300080, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20404  cCE("wqmulwm",   ec000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20405  cCE("wqmulwmr",  ee000e0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20406  cCE("wsubaddhx", ed001c0, 3, (RIWR, RIWR, RIWR),     rd_rn_rm),
20407
20408 #undef  ARM_VARIANT
20409 #define ARM_VARIANT  & arm_cext_maverick /* Cirrus Maverick instructions.  */
20410
20411  cCE("cfldrs",  c100400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20412  cCE("cfldrd",  c500400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20413  cCE("cfldr32", c100500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20414  cCE("cfldr64", c500500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20415  cCE("cfstrs",  c000400, 2, (RMF, ADDRGLDC),          rd_cpaddr),
20416  cCE("cfstrd",  c400400, 2, (RMD, ADDRGLDC),          rd_cpaddr),
20417  cCE("cfstr32", c000500, 2, (RMFX, ADDRGLDC),         rd_cpaddr),
20418  cCE("cfstr64", c400500, 2, (RMDX, ADDRGLDC),         rd_cpaddr),
20419  cCE("cfmvsr",  e000450, 2, (RMF, RR),                rn_rd),
20420  cCE("cfmvrs",  e100450, 2, (RR, RMF),                rd_rn),
20421  cCE("cfmvdlr", e000410, 2, (RMD, RR),                rn_rd),
20422  cCE("cfmvrdl", e100410, 2, (RR, RMD),                rd_rn),
20423  cCE("cfmvdhr", e000430, 2, (RMD, RR),                rn_rd),
20424  cCE("cfmvrdh", e100430, 2, (RR, RMD),                rd_rn),
20425  cCE("cfmv64lr",e000510, 2, (RMDX, RR),               rn_rd),
20426  cCE("cfmvr64l",e100510, 2, (RR, RMDX),               rd_rn),
20427  cCE("cfmv64hr",e000530, 2, (RMDX, RR),               rn_rd),
20428  cCE("cfmvr64h",e100530, 2, (RR, RMDX),               rd_rn),
20429  cCE("cfmval32",e200440, 2, (RMAX, RMFX),             rd_rn),
20430  cCE("cfmv32al",e100440, 2, (RMFX, RMAX),             rd_rn),
20431  cCE("cfmvam32",e200460, 2, (RMAX, RMFX),             rd_rn),
20432  cCE("cfmv32am",e100460, 2, (RMFX, RMAX),             rd_rn),
20433  cCE("cfmvah32",e200480, 2, (RMAX, RMFX),             rd_rn),
20434  cCE("cfmv32ah",e100480, 2, (RMFX, RMAX),             rd_rn),
20435  cCE("cfmva32", e2004a0, 2, (RMAX, RMFX),             rd_rn),
20436  cCE("cfmv32a", e1004a0, 2, (RMFX, RMAX),             rd_rn),
20437  cCE("cfmva64", e2004c0, 2, (RMAX, RMDX),             rd_rn),
20438  cCE("cfmv64a", e1004c0, 2, (RMDX, RMAX),             rd_rn),
20439  cCE("cfmvsc32",e2004e0, 2, (RMDS, RMDX),             mav_dspsc),
20440  cCE("cfmv32sc",e1004e0, 2, (RMDX, RMDS),             rd),
20441  cCE("cfcpys",  e000400, 2, (RMF, RMF),               rd_rn),
20442  cCE("cfcpyd",  e000420, 2, (RMD, RMD),               rd_rn),
20443  cCE("cfcvtsd", e000460, 2, (RMD, RMF),               rd_rn),
20444  cCE("cfcvtds", e000440, 2, (RMF, RMD),               rd_rn),
20445  cCE("cfcvt32s",e000480, 2, (RMF, RMFX),              rd_rn),
20446  cCE("cfcvt32d",e0004a0, 2, (RMD, RMFX),              rd_rn),
20447  cCE("cfcvt64s",e0004c0, 2, (RMF, RMDX),              rd_rn),
20448  cCE("cfcvt64d",e0004e0, 2, (RMD, RMDX),              rd_rn),
20449  cCE("cfcvts32",e100580, 2, (RMFX, RMF),              rd_rn),
20450  cCE("cfcvtd32",e1005a0, 2, (RMFX, RMD),              rd_rn),
20451  cCE("cftruncs32",e1005c0, 2, (RMFX, RMF),            rd_rn),
20452  cCE("cftruncd32",e1005e0, 2, (RMFX, RMD),            rd_rn),
20453  cCE("cfrshl32",e000550, 3, (RMFX, RMFX, RR),         mav_triple),
20454  cCE("cfrshl64",e000570, 3, (RMDX, RMDX, RR),         mav_triple),
20455  cCE("cfsh32",  e000500, 3, (RMFX, RMFX, I63s),       mav_shift),
20456  cCE("cfsh64",  e200500, 3, (RMDX, RMDX, I63s),       mav_shift),
20457  cCE("cfcmps",  e100490, 3, (RR, RMF, RMF),           rd_rn_rm),
20458  cCE("cfcmpd",  e1004b0, 3, (RR, RMD, RMD),           rd_rn_rm),
20459  cCE("cfcmp32", e100590, 3, (RR, RMFX, RMFX),         rd_rn_rm),
20460  cCE("cfcmp64", e1005b0, 3, (RR, RMDX, RMDX),         rd_rn_rm),
20461  cCE("cfabss",  e300400, 2, (RMF, RMF),               rd_rn),
20462  cCE("cfabsd",  e300420, 2, (RMD, RMD),               rd_rn),
20463  cCE("cfnegs",  e300440, 2, (RMF, RMF),               rd_rn),
20464  cCE("cfnegd",  e300460, 2, (RMD, RMD),               rd_rn),
20465  cCE("cfadds",  e300480, 3, (RMF, RMF, RMF),          rd_rn_rm),
20466  cCE("cfaddd",  e3004a0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20467  cCE("cfsubs",  e3004c0, 3, (RMF, RMF, RMF),          rd_rn_rm),
20468  cCE("cfsubd",  e3004e0, 3, (RMD, RMD, RMD),          rd_rn_rm),
20469  cCE("cfmuls",  e100400, 3, (RMF, RMF, RMF),          rd_rn_rm),
20470  cCE("cfmuld",  e100420, 3, (RMD, RMD, RMD),          rd_rn_rm),
20471  cCE("cfabs32", e300500, 2, (RMFX, RMFX),             rd_rn),
20472  cCE("cfabs64", e300520, 2, (RMDX, RMDX),             rd_rn),
20473  cCE("cfneg32", e300540, 2, (RMFX, RMFX),             rd_rn),
20474  cCE("cfneg64", e300560, 2, (RMDX, RMDX),             rd_rn),
20475  cCE("cfadd32", e300580, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20476  cCE("cfadd64", e3005a0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20477  cCE("cfsub32", e3005c0, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20478  cCE("cfsub64", e3005e0, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20479  cCE("cfmul32", e100500, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20480  cCE("cfmul64", e100520, 3, (RMDX, RMDX, RMDX),       rd_rn_rm),
20481  cCE("cfmac32", e100540, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20482  cCE("cfmsc32", e100560, 3, (RMFX, RMFX, RMFX),       rd_rn_rm),
20483  cCE("cfmadd32",e000600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20484  cCE("cfmsub32",e100600, 4, (RMAX, RMFX, RMFX, RMFX), mav_quad),
20485  cCE("cfmadda32", e200600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
20486  cCE("cfmsuba32", e300600, 4, (RMAX, RMAX, RMFX, RMFX), mav_quad),
20487 };
20488 #undef ARM_VARIANT
20489 #undef THUMB_VARIANT
20490 #undef TCE
20491 #undef TUE
20492 #undef TUF
20493 #undef TCC
20494 #undef cCE
20495 #undef cCL
20496 #undef C3E
20497 #undef CE
20498 #undef CM
20499 #undef UE
20500 #undef UF
20501 #undef UT
20502 #undef NUF
20503 #undef nUF
20504 #undef NCE
20505 #undef nCE
20506 #undef OPS0
20507 #undef OPS1
20508 #undef OPS2
20509 #undef OPS3
20510 #undef OPS4
20511 #undef OPS5
20512 #undef OPS6
20513 #undef do_0
20514 \f
20515 /* MD interface: bits in the object file.  */
20516
20517 /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
20518    for use in the a.out file, and stores them in the array pointed to by buf.
20519    This knows about the endian-ness of the target machine and does
20520    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
20521    2 (short) and 4 (long)  Floating numbers are put out as a series of
20522    LITTLENUMS (shorts, here at least).  */
20523
20524 void
20525 md_number_to_chars (char * buf, valueT val, int n)
20526 {
20527   if (target_big_endian)
20528     number_to_chars_bigendian (buf, val, n);
20529   else
20530     number_to_chars_littleendian (buf, val, n);
20531 }
20532
20533 static valueT
20534 md_chars_to_number (char * buf, int n)
20535 {
20536   valueT result = 0;
20537   unsigned char * where = (unsigned char *) buf;
20538
20539   if (target_big_endian)
20540     {
20541       while (n--)
20542         {
20543           result <<= 8;
20544           result |= (*where++ & 255);
20545         }
20546     }
20547   else
20548     {
20549       while (n--)
20550         {
20551           result <<= 8;
20552           result |= (where[n] & 255);
20553         }
20554     }
20555
20556   return result;
20557 }
20558
20559 /* MD interface: Sections.  */
20560
20561 /* Calculate the maximum variable size (i.e., excluding fr_fix)
20562    that an rs_machine_dependent frag may reach.  */
20563
20564 unsigned int
20565 arm_frag_max_var (fragS *fragp)
20566 {
20567   /* We only use rs_machine_dependent for variable-size Thumb instructions,
20568      which are either THUMB_SIZE (2) or INSN_SIZE (4).
20569
20570      Note that we generate relaxable instructions even for cases that don't
20571      really need it, like an immediate that's a trivial constant.  So we're
20572      overestimating the instruction size for some of those cases.  Rather
20573      than putting more intelligence here, it would probably be better to
20574      avoid generating a relaxation frag in the first place when it can be
20575      determined up front that a short instruction will suffice.  */
20576
20577   gas_assert (fragp->fr_type == rs_machine_dependent);
20578   return INSN_SIZE;
20579 }
20580
20581 /* Estimate the size of a frag before relaxing.  Assume everything fits in
20582    2 bytes.  */
20583
20584 int
20585 md_estimate_size_before_relax (fragS * fragp,
20586                                segT    segtype ATTRIBUTE_UNUSED)
20587 {
20588   fragp->fr_var = 2;
20589   return 2;
20590 }
20591
20592 /* Convert a machine dependent frag.  */
20593
20594 void
20595 md_convert_frag (bfd *abfd, segT asec ATTRIBUTE_UNUSED, fragS *fragp)
20596 {
20597   unsigned long insn;
20598   unsigned long old_op;
20599   char *buf;
20600   expressionS exp;
20601   fixS *fixp;
20602   int reloc_type;
20603   int pc_rel;
20604   int opcode;
20605
20606   buf = fragp->fr_literal + fragp->fr_fix;
20607
20608   old_op = bfd_get_16(abfd, buf);
20609   if (fragp->fr_symbol)
20610     {
20611       exp.X_op = O_symbol;
20612       exp.X_add_symbol = fragp->fr_symbol;
20613     }
20614   else
20615     {
20616       exp.X_op = O_constant;
20617     }
20618   exp.X_add_number = fragp->fr_offset;
20619   opcode = fragp->fr_subtype;
20620   switch (opcode)
20621     {
20622     case T_MNEM_ldr_pc:
20623     case T_MNEM_ldr_pc2:
20624     case T_MNEM_ldr_sp:
20625     case T_MNEM_str_sp:
20626     case T_MNEM_ldr:
20627     case T_MNEM_ldrb:
20628     case T_MNEM_ldrh:
20629     case T_MNEM_str:
20630     case T_MNEM_strb:
20631     case T_MNEM_strh:
20632       if (fragp->fr_var == 4)
20633         {
20634           insn = THUMB_OP32 (opcode);
20635           if ((old_op >> 12) == 4 || (old_op >> 12) == 9)
20636             {
20637               insn |= (old_op & 0x700) << 4;
20638             }
20639           else
20640             {
20641               insn |= (old_op & 7) << 12;
20642               insn |= (old_op & 0x38) << 13;
20643             }
20644           insn |= 0x00000c00;
20645           put_thumb32_insn (buf, insn);
20646           reloc_type = BFD_RELOC_ARM_T32_OFFSET_IMM;
20647         }
20648       else
20649         {
20650           reloc_type = BFD_RELOC_ARM_THUMB_OFFSET;
20651         }
20652       pc_rel = (opcode == T_MNEM_ldr_pc2);
20653       break;
20654     case T_MNEM_adr:
20655       if (fragp->fr_var == 4)
20656         {
20657           insn = THUMB_OP32 (opcode);
20658           insn |= (old_op & 0xf0) << 4;
20659           put_thumb32_insn (buf, insn);
20660           reloc_type = BFD_RELOC_ARM_T32_ADD_PC12;
20661         }
20662       else
20663         {
20664           reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20665           exp.X_add_number -= 4;
20666         }
20667       pc_rel = 1;
20668       break;
20669     case T_MNEM_mov:
20670     case T_MNEM_movs:
20671     case T_MNEM_cmp:
20672     case T_MNEM_cmn:
20673       if (fragp->fr_var == 4)
20674         {
20675           int r0off = (opcode == T_MNEM_mov
20676                        || opcode == T_MNEM_movs) ? 0 : 8;
20677           insn = THUMB_OP32 (opcode);
20678           insn = (insn & 0xe1ffffff) | 0x10000000;
20679           insn |= (old_op & 0x700) << r0off;
20680           put_thumb32_insn (buf, insn);
20681           reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
20682         }
20683       else
20684         {
20685           reloc_type = BFD_RELOC_ARM_THUMB_IMM;
20686         }
20687       pc_rel = 0;
20688       break;
20689     case T_MNEM_b:
20690       if (fragp->fr_var == 4)
20691         {
20692           insn = THUMB_OP32(opcode);
20693           put_thumb32_insn (buf, insn);
20694           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH25;
20695         }
20696       else
20697         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH12;
20698       pc_rel = 1;
20699       break;
20700     case T_MNEM_bcond:
20701       if (fragp->fr_var == 4)
20702         {
20703           insn = THUMB_OP32(opcode);
20704           insn |= (old_op & 0xf00) << 14;
20705           put_thumb32_insn (buf, insn);
20706           reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH20;
20707         }
20708       else
20709         reloc_type = BFD_RELOC_THUMB_PCREL_BRANCH9;
20710       pc_rel = 1;
20711       break;
20712     case T_MNEM_add_sp:
20713     case T_MNEM_add_pc:
20714     case T_MNEM_inc_sp:
20715     case T_MNEM_dec_sp:
20716       if (fragp->fr_var == 4)
20717         {
20718           /* ??? Choose between add and addw.  */
20719           insn = THUMB_OP32 (opcode);
20720           insn |= (old_op & 0xf0) << 4;
20721           put_thumb32_insn (buf, insn);
20722           if (opcode == T_MNEM_add_pc)
20723             reloc_type = BFD_RELOC_ARM_T32_IMM12;
20724           else
20725             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20726         }
20727       else
20728         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20729       pc_rel = 0;
20730       break;
20731
20732     case T_MNEM_addi:
20733     case T_MNEM_addis:
20734     case T_MNEM_subi:
20735     case T_MNEM_subis:
20736       if (fragp->fr_var == 4)
20737         {
20738           insn = THUMB_OP32 (opcode);
20739           insn |= (old_op & 0xf0) << 4;
20740           insn |= (old_op & 0xf) << 16;
20741           put_thumb32_insn (buf, insn);
20742           if (insn & (1 << 20))
20743             reloc_type = BFD_RELOC_ARM_T32_ADD_IMM;
20744           else
20745             reloc_type = BFD_RELOC_ARM_T32_IMMEDIATE;
20746         }
20747       else
20748         reloc_type = BFD_RELOC_ARM_THUMB_ADD;
20749       pc_rel = 0;
20750       break;
20751     default:
20752       abort ();
20753     }
20754   fixp = fix_new_exp (fragp, fragp->fr_fix, fragp->fr_var, &exp, pc_rel,
20755                       (enum bfd_reloc_code_real) reloc_type);
20756   fixp->fx_file = fragp->fr_file;
20757   fixp->fx_line = fragp->fr_line;
20758   fragp->fr_fix += fragp->fr_var;
20759
20760   /* Set whether we use thumb-2 ISA based on final relaxation results.  */
20761   if (thumb_mode && fragp->fr_var == 4 && no_cpu_selected ()
20762       && !ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_t2))
20763     ARM_MERGE_FEATURE_SETS (arm_arch_used, thumb_arch_used, arm_ext_v6t2);
20764 }
20765
20766 /* Return the size of a relaxable immediate operand instruction.
20767    SHIFT and SIZE specify the form of the allowable immediate.  */
20768 static int
20769 relax_immediate (fragS *fragp, int size, int shift)
20770 {
20771   offsetT offset;
20772   offsetT mask;
20773   offsetT low;
20774
20775   /* ??? Should be able to do better than this.  */
20776   if (fragp->fr_symbol)
20777     return 4;
20778
20779   low = (1 << shift) - 1;
20780   mask = (1 << (shift + size)) - (1 << shift);
20781   offset = fragp->fr_offset;
20782   /* Force misaligned offsets to 32-bit variant.  */
20783   if (offset & low)
20784     return 4;
20785   if (offset & ~mask)
20786     return 4;
20787   return 2;
20788 }
20789
20790 /* Get the address of a symbol during relaxation.  */
20791 static addressT
20792 relaxed_symbol_addr (fragS *fragp, long stretch)
20793 {
20794   fragS *sym_frag;
20795   addressT addr;
20796   symbolS *sym;
20797
20798   sym = fragp->fr_symbol;
20799   sym_frag = symbol_get_frag (sym);
20800   know (S_GET_SEGMENT (sym) != absolute_section
20801         || sym_frag == &zero_address_frag);
20802   addr = S_GET_VALUE (sym) + fragp->fr_offset;
20803
20804   /* If frag has yet to be reached on this pass, assume it will
20805      move by STRETCH just as we did.  If this is not so, it will
20806      be because some frag between grows, and that will force
20807      another pass.  */
20808
20809   if (stretch != 0
20810       && sym_frag->relax_marker != fragp->relax_marker)
20811     {
20812       fragS *f;
20813
20814       /* Adjust stretch for any alignment frag.  Note that if have
20815          been expanding the earlier code, the symbol may be
20816          defined in what appears to be an earlier frag.  FIXME:
20817          This doesn't handle the fr_subtype field, which specifies
20818          a maximum number of bytes to skip when doing an
20819          alignment.  */
20820       for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
20821         {
20822           if (f->fr_type == rs_align || f->fr_type == rs_align_code)
20823             {
20824               if (stretch < 0)
20825                 stretch = - ((- stretch)
20826                              & ~ ((1 << (int) f->fr_offset) - 1));
20827               else
20828                 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
20829               if (stretch == 0)
20830                 break;
20831             }
20832         }
20833       if (f != NULL)
20834         addr += stretch;
20835     }
20836
20837   return addr;
20838 }
20839
20840 /* Return the size of a relaxable adr pseudo-instruction or PC-relative
20841    load.  */
20842 static int
20843 relax_adr (fragS *fragp, asection *sec, long stretch)
20844 {
20845   addressT addr;
20846   offsetT val;
20847
20848   /* Assume worst case for symbols not known to be in the same section.  */
20849   if (fragp->fr_symbol == NULL
20850       || !S_IS_DEFINED (fragp->fr_symbol)
20851       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20852       || S_IS_WEAK (fragp->fr_symbol))
20853     return 4;
20854
20855   val = relaxed_symbol_addr (fragp, stretch);
20856   addr = fragp->fr_address + fragp->fr_fix;
20857   addr = (addr + 4) & ~3;
20858   /* Force misaligned targets to 32-bit variant.  */
20859   if (val & 3)
20860     return 4;
20861   val -= addr;
20862   if (val < 0 || val > 1020)
20863     return 4;
20864   return 2;
20865 }
20866
20867 /* Return the size of a relaxable add/sub immediate instruction.  */
20868 static int
20869 relax_addsub (fragS *fragp, asection *sec)
20870 {
20871   char *buf;
20872   int op;
20873
20874   buf = fragp->fr_literal + fragp->fr_fix;
20875   op = bfd_get_16(sec->owner, buf);
20876   if ((op & 0xf) == ((op >> 4) & 0xf))
20877     return relax_immediate (fragp, 8, 0);
20878   else
20879     return relax_immediate (fragp, 3, 0);
20880 }
20881
20882 /* Return TRUE iff the definition of symbol S could be pre-empted
20883    (overridden) at link or load time.  */
20884 static bfd_boolean
20885 symbol_preemptible (symbolS *s)
20886 {
20887   /* Weak symbols can always be pre-empted.  */
20888   if (S_IS_WEAK (s))
20889     return TRUE;
20890
20891   /* Non-global symbols cannot be pre-empted. */
20892   if (! S_IS_EXTERNAL (s))
20893     return FALSE;
20894
20895 #ifdef OBJ_ELF
20896   /* In ELF, a global symbol can be marked protected, or private.  In that
20897      case it can't be pre-empted (other definitions in the same link unit
20898      would violate the ODR).  */
20899   if (ELF_ST_VISIBILITY (S_GET_OTHER (s)) > STV_DEFAULT)
20900     return FALSE;
20901 #endif
20902
20903   /* Other global symbols might be pre-empted.  */
20904   return TRUE;
20905 }
20906
20907 /* Return the size of a relaxable branch instruction.  BITS is the
20908    size of the offset field in the narrow instruction.  */
20909
20910 static int
20911 relax_branch (fragS *fragp, asection *sec, int bits, long stretch)
20912 {
20913   addressT addr;
20914   offsetT val;
20915   offsetT limit;
20916
20917   /* Assume worst case for symbols not known to be in the same section.  */
20918   if (!S_IS_DEFINED (fragp->fr_symbol)
20919       || sec != S_GET_SEGMENT (fragp->fr_symbol)
20920       || S_IS_WEAK (fragp->fr_symbol))
20921     return 4;
20922
20923 #ifdef OBJ_ELF
20924   /* A branch to a function in ARM state will require interworking.  */
20925   if (S_IS_DEFINED (fragp->fr_symbol)
20926       && ARM_IS_FUNC (fragp->fr_symbol))
20927       return 4;
20928 #endif
20929
20930   if (symbol_preemptible (fragp->fr_symbol))
20931     return 4;
20932
20933   val = relaxed_symbol_addr (fragp, stretch);
20934   addr = fragp->fr_address + fragp->fr_fix + 4;
20935   val -= addr;
20936
20937   /* Offset is a signed value *2 */
20938   limit = 1 << bits;
20939   if (val >= limit || val < -limit)
20940     return 4;
20941   return 2;
20942 }
20943
20944
20945 /* Relax a machine dependent frag.  This returns the amount by which
20946    the current size of the frag should change.  */
20947
20948 int
20949 arm_relax_frag (asection *sec, fragS *fragp, long stretch)
20950 {
20951   int oldsize;
20952   int newsize;
20953
20954   oldsize = fragp->fr_var;
20955   switch (fragp->fr_subtype)
20956     {
20957     case T_MNEM_ldr_pc2:
20958       newsize = relax_adr (fragp, sec, stretch);
20959       break;
20960     case T_MNEM_ldr_pc:
20961     case T_MNEM_ldr_sp:
20962     case T_MNEM_str_sp:
20963       newsize = relax_immediate (fragp, 8, 2);
20964       break;
20965     case T_MNEM_ldr:
20966     case T_MNEM_str:
20967       newsize = relax_immediate (fragp, 5, 2);
20968       break;
20969     case T_MNEM_ldrh:
20970     case T_MNEM_strh:
20971       newsize = relax_immediate (fragp, 5, 1);
20972       break;
20973     case T_MNEM_ldrb:
20974     case T_MNEM_strb:
20975       newsize = relax_immediate (fragp, 5, 0);
20976       break;
20977     case T_MNEM_adr:
20978       newsize = relax_adr (fragp, sec, stretch);
20979       break;
20980     case T_MNEM_mov:
20981     case T_MNEM_movs:
20982     case T_MNEM_cmp:
20983     case T_MNEM_cmn:
20984       newsize = relax_immediate (fragp, 8, 0);
20985       break;
20986     case T_MNEM_b:
20987       newsize = relax_branch (fragp, sec, 11, stretch);
20988       break;
20989     case T_MNEM_bcond:
20990       newsize = relax_branch (fragp, sec, 8, stretch);
20991       break;
20992     case T_MNEM_add_sp:
20993     case T_MNEM_add_pc:
20994       newsize = relax_immediate (fragp, 8, 2);
20995       break;
20996     case T_MNEM_inc_sp:
20997     case T_MNEM_dec_sp:
20998       newsize = relax_immediate (fragp, 7, 2);
20999       break;
21000     case T_MNEM_addi:
21001     case T_MNEM_addis:
21002     case T_MNEM_subi:
21003     case T_MNEM_subis:
21004       newsize = relax_addsub (fragp, sec);
21005       break;
21006     default:
21007       abort ();
21008     }
21009
21010   fragp->fr_var = newsize;
21011   /* Freeze wide instructions that are at or before the same location as
21012      in the previous pass.  This avoids infinite loops.
21013      Don't freeze them unconditionally because targets may be artificially
21014      misaligned by the expansion of preceding frags.  */
21015   if (stretch <= 0 && newsize > 2)
21016     {
21017       md_convert_frag (sec->owner, sec, fragp);
21018       frag_wane (fragp);
21019     }
21020
21021   return newsize - oldsize;
21022 }
21023
21024 /* Round up a section size to the appropriate boundary.  */
21025
21026 valueT
21027 md_section_align (segT   segment ATTRIBUTE_UNUSED,
21028                   valueT size)
21029 {
21030 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
21031   if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
21032     {
21033       /* For a.out, force the section size to be aligned.  If we don't do
21034          this, BFD will align it for us, but it will not write out the
21035          final bytes of the section.  This may be a bug in BFD, but it is
21036          easier to fix it here since that is how the other a.out targets
21037          work.  */
21038       int align;
21039
21040       align = bfd_get_section_alignment (stdoutput, segment);
21041       size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
21042     }
21043 #endif
21044
21045   return size;
21046 }
21047
21048 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
21049    of an rs_align_code fragment.  */
21050
21051 void
21052 arm_handle_align (fragS * fragP)
21053 {
21054   static char const arm_noop[2][2][4] =
21055     {
21056       {  /* ARMv1 */
21057         {0x00, 0x00, 0xa0, 0xe1},  /* LE */
21058         {0xe1, 0xa0, 0x00, 0x00},  /* BE */
21059       },
21060       {  /* ARMv6k */
21061         {0x00, 0xf0, 0x20, 0xe3},  /* LE */
21062         {0xe3, 0x20, 0xf0, 0x00},  /* BE */
21063       },
21064     };
21065   static char const thumb_noop[2][2][2] =
21066     {
21067       {  /* Thumb-1 */
21068         {0xc0, 0x46},  /* LE */
21069         {0x46, 0xc0},  /* BE */
21070       },
21071       {  /* Thumb-2 */
21072         {0x00, 0xbf},  /* LE */
21073         {0xbf, 0x00}   /* BE */
21074       }
21075     };
21076   static char const wide_thumb_noop[2][4] =
21077     {  /* Wide Thumb-2 */
21078       {0xaf, 0xf3, 0x00, 0x80},  /* LE */
21079       {0xf3, 0xaf, 0x80, 0x00},  /* BE */
21080     };
21081
21082   unsigned bytes, fix, noop_size;
21083   char * p;
21084   const char * noop;
21085   const char *narrow_noop = NULL;
21086 #ifdef OBJ_ELF
21087   enum mstate state;
21088 #endif
21089
21090   if (fragP->fr_type != rs_align_code)
21091     return;
21092
21093   bytes = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
21094   p = fragP->fr_literal + fragP->fr_fix;
21095   fix = 0;
21096
21097   if (bytes > MAX_MEM_FOR_RS_ALIGN_CODE)
21098     bytes &= MAX_MEM_FOR_RS_ALIGN_CODE;
21099
21100   gas_assert ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) != 0);
21101
21102   if (fragP->tc_frag_data.thumb_mode & (~ MODE_RECORDED))
21103     {
21104       if (ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21105                                ? selected_cpu : arm_arch_none, arm_ext_v6t2))
21106         {
21107           narrow_noop = thumb_noop[1][target_big_endian];
21108           noop = wide_thumb_noop[target_big_endian];
21109         }
21110       else
21111         noop = thumb_noop[0][target_big_endian];
21112       noop_size = 2;
21113 #ifdef OBJ_ELF
21114       state = MAP_THUMB;
21115 #endif
21116     }
21117   else
21118     {
21119       noop = arm_noop[ARM_CPU_HAS_FEATURE (selected_cpu_name[0]
21120                                            ? selected_cpu : arm_arch_none,
21121                                            arm_ext_v6k) != 0]
21122                      [target_big_endian];
21123       noop_size = 4;
21124 #ifdef OBJ_ELF
21125       state = MAP_ARM;
21126 #endif
21127     }
21128
21129   fragP->fr_var = noop_size;
21130
21131   if (bytes & (noop_size - 1))
21132     {
21133       fix = bytes & (noop_size - 1);
21134 #ifdef OBJ_ELF
21135       insert_data_mapping_symbol (state, fragP->fr_fix, fragP, fix);
21136 #endif
21137       memset (p, 0, fix);
21138       p += fix;
21139       bytes -= fix;
21140     }
21141
21142   if (narrow_noop)
21143     {
21144       if (bytes & noop_size)
21145         {
21146           /* Insert a narrow noop.  */
21147           memcpy (p, narrow_noop, noop_size);
21148           p += noop_size;
21149           bytes -= noop_size;
21150           fix += noop_size;
21151         }
21152
21153       /* Use wide noops for the remainder */
21154       noop_size = 4;
21155     }
21156
21157   while (bytes >= noop_size)
21158     {
21159       memcpy (p, noop, noop_size);
21160       p += noop_size;
21161       bytes -= noop_size;
21162       fix += noop_size;
21163     }
21164
21165   fragP->fr_fix += fix;
21166 }
21167
21168 /* Called from md_do_align.  Used to create an alignment
21169    frag in a code section.  */
21170
21171 void
21172 arm_frag_align_code (int n, int max)
21173 {
21174   char * p;
21175
21176   /* We assume that there will never be a requirement
21177      to support alignments greater than MAX_MEM_FOR_RS_ALIGN_CODE bytes.  */
21178   if (max > MAX_MEM_FOR_RS_ALIGN_CODE)
21179     {
21180       char err_msg[128];
21181
21182       sprintf (err_msg,
21183         _("alignments greater than %d bytes not supported in .text sections."),
21184         MAX_MEM_FOR_RS_ALIGN_CODE + 1);
21185       as_fatal ("%s", err_msg);
21186     }
21187
21188   p = frag_var (rs_align_code,
21189                 MAX_MEM_FOR_RS_ALIGN_CODE,
21190                 1,
21191                 (relax_substateT) max,
21192                 (symbolS *) NULL,
21193                 (offsetT) n,
21194                 (char *) NULL);
21195   *p = 0;
21196 }
21197
21198 /* Perform target specific initialisation of a frag.
21199    Note - despite the name this initialisation is not done when the frag
21200    is created, but only when its type is assigned.  A frag can be created
21201    and used a long time before its type is set, so beware of assuming that
21202    this initialisationis performed first.  */
21203
21204 #ifndef OBJ_ELF
21205 void
21206 arm_init_frag (fragS * fragP, int max_chars ATTRIBUTE_UNUSED)
21207 {
21208   /* Record whether this frag is in an ARM or a THUMB area.  */
21209   fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21210 }
21211
21212 #else /* OBJ_ELF is defined.  */
21213 void
21214 arm_init_frag (fragS * fragP, int max_chars)
21215 {
21216   int frag_thumb_mode;
21217
21218   /* If the current ARM vs THUMB mode has not already
21219      been recorded into this frag then do so now.  */
21220   if ((fragP->tc_frag_data.thumb_mode & MODE_RECORDED) == 0)
21221     fragP->tc_frag_data.thumb_mode = thumb_mode | MODE_RECORDED;
21222
21223   frag_thumb_mode = fragP->tc_frag_data.thumb_mode ^ MODE_RECORDED;
21224
21225   /* Record a mapping symbol for alignment frags.  We will delete this
21226      later if the alignment ends up empty.  */
21227   switch (fragP->fr_type)
21228     {
21229     case rs_align:
21230     case rs_align_test:
21231     case rs_fill:
21232       mapping_state_2 (MAP_DATA, max_chars);
21233       break;
21234     case rs_align_code:
21235       mapping_state_2 (frag_thumb_mode ? MAP_THUMB : MAP_ARM, max_chars);
21236       break;
21237     default:
21238       break;
21239     }
21240 }
21241
21242 /* When we change sections we need to issue a new mapping symbol.  */
21243
21244 void
21245 arm_elf_change_section (void)
21246 {
21247   /* Link an unlinked unwind index table section to the .text section.  */
21248   if (elf_section_type (now_seg) == SHT_ARM_EXIDX
21249       && elf_linked_to_section (now_seg) == NULL)
21250     elf_linked_to_section (now_seg) = text_section;
21251 }
21252
21253 int
21254 arm_elf_section_type (const char * str, size_t len)
21255 {
21256   if (len == 5 && strncmp (str, "exidx", 5) == 0)
21257     return SHT_ARM_EXIDX;
21258
21259   return -1;
21260 }
21261 \f
21262 /* Code to deal with unwinding tables.  */
21263
21264 static void add_unwind_adjustsp (offsetT);
21265
21266 /* Generate any deferred unwind frame offset.  */
21267
21268 static void
21269 flush_pending_unwind (void)
21270 {
21271   offsetT offset;
21272
21273   offset = unwind.pending_offset;
21274   unwind.pending_offset = 0;
21275   if (offset != 0)
21276     add_unwind_adjustsp (offset);
21277 }
21278
21279 /* Add an opcode to this list for this function.  Two-byte opcodes should
21280    be passed as op[0] << 8 | op[1].  The list of opcodes is built in reverse
21281    order.  */
21282
21283 static void
21284 add_unwind_opcode (valueT op, int length)
21285 {
21286   /* Add any deferred stack adjustment.  */
21287   if (unwind.pending_offset)
21288     flush_pending_unwind ();
21289
21290   unwind.sp_restored = 0;
21291
21292   if (unwind.opcode_count + length > unwind.opcode_alloc)
21293     {
21294       unwind.opcode_alloc += ARM_OPCODE_CHUNK_SIZE;
21295       if (unwind.opcodes)
21296         unwind.opcodes = (unsigned char *) xrealloc (unwind.opcodes,
21297                                                      unwind.opcode_alloc);
21298       else
21299         unwind.opcodes = (unsigned char *) xmalloc (unwind.opcode_alloc);
21300     }
21301   while (length > 0)
21302     {
21303       length--;
21304       unwind.opcodes[unwind.opcode_count] = op & 0xff;
21305       op >>= 8;
21306       unwind.opcode_count++;
21307     }
21308 }
21309
21310 /* Add unwind opcodes to adjust the stack pointer.  */
21311
21312 static void
21313 add_unwind_adjustsp (offsetT offset)
21314 {
21315   valueT op;
21316
21317   if (offset > 0x200)
21318     {
21319       /* We need at most 5 bytes to hold a 32-bit value in a uleb128.  */
21320       char bytes[5];
21321       int n;
21322       valueT o;
21323
21324       /* Long form: 0xb2, uleb128.  */
21325       /* This might not fit in a word so add the individual bytes,
21326          remembering the list is built in reverse order.  */
21327       o = (valueT) ((offset - 0x204) >> 2);
21328       if (o == 0)
21329         add_unwind_opcode (0, 1);
21330
21331       /* Calculate the uleb128 encoding of the offset.  */
21332       n = 0;
21333       while (o)
21334         {
21335           bytes[n] = o & 0x7f;
21336           o >>= 7;
21337           if (o)
21338             bytes[n] |= 0x80;
21339           n++;
21340         }
21341       /* Add the insn.  */
21342       for (; n; n--)
21343         add_unwind_opcode (bytes[n - 1], 1);
21344       add_unwind_opcode (0xb2, 1);
21345     }
21346   else if (offset > 0x100)
21347     {
21348       /* Two short opcodes.  */
21349       add_unwind_opcode (0x3f, 1);
21350       op = (offset - 0x104) >> 2;
21351       add_unwind_opcode (op, 1);
21352     }
21353   else if (offset > 0)
21354     {
21355       /* Short opcode.  */
21356       op = (offset - 4) >> 2;
21357       add_unwind_opcode (op, 1);
21358     }
21359   else if (offset < 0)
21360     {
21361       offset = -offset;
21362       while (offset > 0x100)
21363         {
21364           add_unwind_opcode (0x7f, 1);
21365           offset -= 0x100;
21366         }
21367       op = ((offset - 4) >> 2) | 0x40;
21368       add_unwind_opcode (op, 1);
21369     }
21370 }
21371
21372 /* Finish the list of unwind opcodes for this function.  */
21373 static void
21374 finish_unwind_opcodes (void)
21375 {
21376   valueT op;
21377
21378   if (unwind.fp_used)
21379     {
21380       /* Adjust sp as necessary.  */
21381       unwind.pending_offset += unwind.fp_offset - unwind.frame_size;
21382       flush_pending_unwind ();
21383
21384       /* After restoring sp from the frame pointer.  */
21385       op = 0x90 | unwind.fp_reg;
21386       add_unwind_opcode (op, 1);
21387     }
21388   else
21389     flush_pending_unwind ();
21390 }
21391
21392
21393 /* Start an exception table entry.  If idx is nonzero this is an index table
21394    entry.  */
21395
21396 static void
21397 start_unwind_section (const segT text_seg, int idx)
21398 {
21399   const char * text_name;
21400   const char * prefix;
21401   const char * prefix_once;
21402   const char * group_name;
21403   size_t prefix_len;
21404   size_t text_len;
21405   char * sec_name;
21406   size_t sec_name_len;
21407   int type;
21408   int flags;
21409   int linkonce;
21410
21411   if (idx)
21412     {
21413       prefix = ELF_STRING_ARM_unwind;
21414       prefix_once = ELF_STRING_ARM_unwind_once;
21415       type = SHT_ARM_EXIDX;
21416     }
21417   else
21418     {
21419       prefix = ELF_STRING_ARM_unwind_info;
21420       prefix_once = ELF_STRING_ARM_unwind_info_once;
21421       type = SHT_PROGBITS;
21422     }
21423
21424   text_name = segment_name (text_seg);
21425   if (streq (text_name, ".text"))
21426     text_name = "";
21427
21428   if (strncmp (text_name, ".gnu.linkonce.t.",
21429                strlen (".gnu.linkonce.t.")) == 0)
21430     {
21431       prefix = prefix_once;
21432       text_name += strlen (".gnu.linkonce.t.");
21433     }
21434
21435   prefix_len = strlen (prefix);
21436   text_len = strlen (text_name);
21437   sec_name_len = prefix_len + text_len;
21438   sec_name = (char *) xmalloc (sec_name_len + 1);
21439   memcpy (sec_name, prefix, prefix_len);
21440   memcpy (sec_name + prefix_len, text_name, text_len);
21441   sec_name[prefix_len + text_len] = '\0';
21442
21443   flags = SHF_ALLOC;
21444   linkonce = 0;
21445   group_name = 0;
21446
21447   /* Handle COMDAT group.  */
21448   if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
21449     {
21450       group_name = elf_group_name (text_seg);
21451       if (group_name == NULL)
21452         {
21453           as_bad (_("Group section `%s' has no group signature"),
21454                   segment_name (text_seg));
21455           ignore_rest_of_line ();
21456           return;
21457         }
21458       flags |= SHF_GROUP;
21459       linkonce = 1;
21460     }
21461
21462   obj_elf_change_section (sec_name, type, flags, 0, group_name, linkonce, 0);
21463
21464   /* Set the section link for index tables.  */
21465   if (idx)
21466     elf_linked_to_section (now_seg) = text_seg;
21467 }
21468
21469
21470 /* Start an unwind table entry.  HAVE_DATA is nonzero if we have additional
21471    personality routine data.  Returns zero, or the index table value for
21472    an inline entry.  */
21473
21474 static valueT
21475 create_unwind_entry (int have_data)
21476 {
21477   int size;
21478   addressT where;
21479   char *ptr;
21480   /* The current word of data.  */
21481   valueT data;
21482   /* The number of bytes left in this word.  */
21483   int n;
21484
21485   finish_unwind_opcodes ();
21486
21487   /* Remember the current text section.  */
21488   unwind.saved_seg = now_seg;
21489   unwind.saved_subseg = now_subseg;
21490
21491   start_unwind_section (now_seg, 0);
21492
21493   if (unwind.personality_routine == NULL)
21494     {
21495       if (unwind.personality_index == -2)
21496         {
21497           if (have_data)
21498             as_bad (_("handlerdata in cantunwind frame"));
21499           return 1; /* EXIDX_CANTUNWIND.  */
21500         }
21501
21502       /* Use a default personality routine if none is specified.  */
21503       if (unwind.personality_index == -1)
21504         {
21505           if (unwind.opcode_count > 3)
21506             unwind.personality_index = 1;
21507           else
21508             unwind.personality_index = 0;
21509         }
21510
21511       /* Space for the personality routine entry.  */
21512       if (unwind.personality_index == 0)
21513         {
21514           if (unwind.opcode_count > 3)
21515             as_bad (_("too many unwind opcodes for personality routine 0"));
21516
21517           if (!have_data)
21518             {
21519               /* All the data is inline in the index table.  */
21520               data = 0x80;
21521               n = 3;
21522               while (unwind.opcode_count > 0)
21523                 {
21524                   unwind.opcode_count--;
21525                   data = (data << 8) | unwind.opcodes[unwind.opcode_count];
21526                   n--;
21527                 }
21528
21529               /* Pad with "finish" opcodes.  */
21530               while (n--)
21531                 data = (data << 8) | 0xb0;
21532
21533               return data;
21534             }
21535           size = 0;
21536         }
21537       else
21538         /* We get two opcodes "free" in the first word.  */
21539         size = unwind.opcode_count - 2;
21540     }
21541   else
21542     {
21543       /* PR 16765: Missing or misplaced unwind directives can trigger this.  */
21544       if (unwind.personality_index != -1)
21545         {
21546           as_bad (_("attempt to recreate an unwind entry"));
21547           return 1;
21548         }
21549
21550       /* An extra byte is required for the opcode count.        */
21551       size = unwind.opcode_count + 1;
21552     }
21553
21554   size = (size + 3) >> 2;
21555   if (size > 0xff)
21556     as_bad (_("too many unwind opcodes"));
21557
21558   frag_align (2, 0, 0);
21559   record_alignment (now_seg, 2);
21560   unwind.table_entry = expr_build_dot ();
21561
21562   /* Allocate the table entry.  */
21563   ptr = frag_more ((size << 2) + 4);
21564   /* PR 13449: Zero the table entries in case some of them are not used.  */
21565   memset (ptr, 0, (size << 2) + 4);
21566   where = frag_now_fix () - ((size << 2) + 4);
21567
21568   switch (unwind.personality_index)
21569     {
21570     case -1:
21571       /* ??? Should this be a PLT generating relocation?  */
21572       /* Custom personality routine.  */
21573       fix_new (frag_now, where, 4, unwind.personality_routine, 0, 1,
21574                BFD_RELOC_ARM_PREL31);
21575
21576       where += 4;
21577       ptr += 4;
21578
21579       /* Set the first byte to the number of additional words.  */
21580       data = size > 0 ? size - 1 : 0;
21581       n = 3;
21582       break;
21583
21584     /* ABI defined personality routines.  */
21585     case 0:
21586       /* Three opcodes bytes are packed into the first word.  */
21587       data = 0x80;
21588       n = 3;
21589       break;
21590
21591     case 1:
21592     case 2:
21593       /* The size and first two opcode bytes go in the first word.  */
21594       data = ((0x80 + unwind.personality_index) << 8) | size;
21595       n = 2;
21596       break;
21597
21598     default:
21599       /* Should never happen.  */
21600       abort ();
21601     }
21602
21603   /* Pack the opcodes into words (MSB first), reversing the list at the same
21604      time.  */
21605   while (unwind.opcode_count > 0)
21606     {
21607       if (n == 0)
21608         {
21609           md_number_to_chars (ptr, data, 4);
21610           ptr += 4;
21611           n = 4;
21612           data = 0;
21613         }
21614       unwind.opcode_count--;
21615       n--;
21616       data = (data << 8) | unwind.opcodes[unwind.opcode_count];
21617     }
21618
21619   /* Finish off the last word.  */
21620   if (n < 4)
21621     {
21622       /* Pad with "finish" opcodes.  */
21623       while (n--)
21624         data = (data << 8) | 0xb0;
21625
21626       md_number_to_chars (ptr, data, 4);
21627     }
21628
21629   if (!have_data)
21630     {
21631       /* Add an empty descriptor if there is no user-specified data.   */
21632       ptr = frag_more (4);
21633       md_number_to_chars (ptr, 0, 4);
21634     }
21635
21636   return 0;
21637 }
21638
21639
21640 /* Initialize the DWARF-2 unwind information for this procedure.  */
21641
21642 void
21643 tc_arm_frame_initial_instructions (void)
21644 {
21645   cfi_add_CFA_def_cfa (REG_SP, 0);
21646 }
21647 #endif /* OBJ_ELF */
21648
21649 /* Convert REGNAME to a DWARF-2 register number.  */
21650
21651 int
21652 tc_arm_regname_to_dw2regnum (char *regname)
21653 {
21654   int reg = arm_reg_parse (&regname, REG_TYPE_RN);
21655   if (reg != FAIL)
21656     return reg;
21657
21658   /* PR 16694: Allow VFP registers as well.  */
21659   reg = arm_reg_parse (&regname, REG_TYPE_VFS);
21660   if (reg != FAIL)
21661     return 64 + reg;
21662
21663   reg = arm_reg_parse (&regname, REG_TYPE_VFD);
21664   if (reg != FAIL)
21665     return reg + 256;
21666
21667   return -1;
21668 }
21669
21670 #ifdef TE_PE
21671 void
21672 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
21673 {
21674   expressionS exp;
21675
21676   exp.X_op = O_secrel;
21677   exp.X_add_symbol = symbol;
21678   exp.X_add_number = 0;
21679   emit_expr (&exp, size);
21680 }
21681 #endif
21682
21683 /* MD interface: Symbol and relocation handling.  */
21684
21685 /* Return the address within the segment that a PC-relative fixup is
21686    relative to.  For ARM, PC-relative fixups applied to instructions
21687    are generally relative to the location of the fixup plus 8 bytes.
21688    Thumb branches are offset by 4, and Thumb loads relative to PC
21689    require special handling.  */
21690
21691 long
21692 md_pcrel_from_section (fixS * fixP, segT seg)
21693 {
21694   offsetT base = fixP->fx_where + fixP->fx_frag->fr_address;
21695
21696   /* If this is pc-relative and we are going to emit a relocation
21697      then we just want to put out any pipeline compensation that the linker
21698      will need.  Otherwise we want to use the calculated base.
21699      For WinCE we skip the bias for externals as well, since this
21700      is how the MS ARM-CE assembler behaves and we want to be compatible.  */
21701   if (fixP->fx_pcrel
21702       && ((fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != seg)
21703           || (arm_force_relocation (fixP)
21704 #ifdef TE_WINCE
21705               && !S_IS_EXTERNAL (fixP->fx_addsy)
21706 #endif
21707               )))
21708     base = 0;
21709
21710
21711   switch (fixP->fx_r_type)
21712     {
21713       /* PC relative addressing on the Thumb is slightly odd as the
21714          bottom two bits of the PC are forced to zero for the
21715          calculation.  This happens *after* application of the
21716          pipeline offset.  However, Thumb adrl already adjusts for
21717          this, so we need not do it again.  */
21718     case BFD_RELOC_ARM_THUMB_ADD:
21719       return base & ~3;
21720
21721     case BFD_RELOC_ARM_THUMB_OFFSET:
21722     case BFD_RELOC_ARM_T32_OFFSET_IMM:
21723     case BFD_RELOC_ARM_T32_ADD_PC12:
21724     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
21725       return (base + 4) & ~3;
21726
21727       /* Thumb branches are simply offset by +4.  */
21728     case BFD_RELOC_THUMB_PCREL_BRANCH7:
21729     case BFD_RELOC_THUMB_PCREL_BRANCH9:
21730     case BFD_RELOC_THUMB_PCREL_BRANCH12:
21731     case BFD_RELOC_THUMB_PCREL_BRANCH20:
21732     case BFD_RELOC_THUMB_PCREL_BRANCH25:
21733       return base + 4;
21734
21735     case BFD_RELOC_THUMB_PCREL_BRANCH23:
21736       if (fixP->fx_addsy
21737           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21738           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21739           && ARM_IS_FUNC (fixP->fx_addsy)
21740           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21741         base = fixP->fx_where + fixP->fx_frag->fr_address;
21742        return base + 4;
21743
21744       /* BLX is like branches above, but forces the low two bits of PC to
21745          zero.  */
21746     case BFD_RELOC_THUMB_PCREL_BLX:
21747       if (fixP->fx_addsy
21748           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21749           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21750           && THUMB_IS_FUNC (fixP->fx_addsy)
21751           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21752         base = fixP->fx_where + fixP->fx_frag->fr_address;
21753       return (base + 4) & ~3;
21754
21755       /* ARM mode branches are offset by +8.  However, the Windows CE
21756          loader expects the relocation not to take this into account.  */
21757     case BFD_RELOC_ARM_PCREL_BLX:
21758       if (fixP->fx_addsy
21759           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21760           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21761           && ARM_IS_FUNC (fixP->fx_addsy)
21762           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21763         base = fixP->fx_where + fixP->fx_frag->fr_address;
21764       return base + 8;
21765
21766     case BFD_RELOC_ARM_PCREL_CALL:
21767       if (fixP->fx_addsy
21768           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21769           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
21770           && THUMB_IS_FUNC (fixP->fx_addsy)
21771           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
21772         base = fixP->fx_where + fixP->fx_frag->fr_address;
21773       return base + 8;
21774
21775     case BFD_RELOC_ARM_PCREL_BRANCH:
21776     case BFD_RELOC_ARM_PCREL_JUMP:
21777     case BFD_RELOC_ARM_PLT32:
21778 #ifdef TE_WINCE
21779       /* When handling fixups immediately, because we have already
21780          discovered the value of a symbol, or the address of the frag involved
21781          we must account for the offset by +8, as the OS loader will never see the reloc.
21782          see fixup_segment() in write.c
21783          The S_IS_EXTERNAL test handles the case of global symbols.
21784          Those need the calculated base, not just the pipe compensation the linker will need.  */
21785       if (fixP->fx_pcrel
21786           && fixP->fx_addsy != NULL
21787           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
21788           && (S_IS_EXTERNAL (fixP->fx_addsy) || !arm_force_relocation (fixP)))
21789         return base + 8;
21790       return base;
21791 #else
21792       return base + 8;
21793 #endif
21794
21795
21796       /* ARM mode loads relative to PC are also offset by +8.  Unlike
21797          branches, the Windows CE loader *does* expect the relocation
21798          to take this into account.  */
21799     case BFD_RELOC_ARM_OFFSET_IMM:
21800     case BFD_RELOC_ARM_OFFSET_IMM8:
21801     case BFD_RELOC_ARM_HWLITERAL:
21802     case BFD_RELOC_ARM_LITERAL:
21803     case BFD_RELOC_ARM_CP_OFF_IMM:
21804       return base + 8;
21805
21806
21807       /* Other PC-relative relocations are un-offset.  */
21808     default:
21809       return base;
21810     }
21811 }
21812
21813 static bfd_boolean flag_warn_syms = TRUE;
21814
21815 bfd_boolean
21816 arm_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char * name)
21817 {
21818   /* PR 18347 - Warn if the user attempts to create a symbol with the same
21819      name as an ARM instruction.  Whilst strictly speaking it is allowed, it
21820      does mean that the resulting code might be very confusing to the reader.
21821      Also this warning can be triggered if the user omits an operand before
21822      an immediate address, eg:
21823
21824        LDR =foo
21825
21826      GAS treats this as an assignment of the value of the symbol foo to a
21827      symbol LDR, and so (without this code) it will not issue any kind of
21828      warning or error message.
21829
21830      Note - ARM instructions are case-insensitive but the strings in the hash
21831      table are all stored in lower case, so we must first ensure that name is
21832      lower case too.  */
21833   if (flag_warn_syms && arm_ops_hsh)
21834     {
21835       char * nbuf = strdup (name);
21836       char * p;
21837
21838       for (p = nbuf; *p; p++)
21839         *p = TOLOWER (*p);
21840       if (hash_find (arm_ops_hsh, nbuf) != NULL)
21841         {
21842           static struct hash_control * already_warned = NULL;
21843
21844           if (already_warned == NULL)
21845             already_warned = hash_new ();
21846           /* Only warn about the symbol once.  To keep the code
21847              simple we let hash_insert do the lookup for us.  */
21848           if (hash_insert (already_warned, name, NULL) == NULL)
21849             as_warn (_("[-mwarn-syms]: Assignment makes a symbol match an ARM instruction: %s"), name);
21850         }
21851       else
21852         free (nbuf);
21853     }
21854
21855   return FALSE;
21856 }
21857
21858 /* Under ELF we need to default _GLOBAL_OFFSET_TABLE.
21859    Otherwise we have no need to default values of symbols.  */
21860
21861 symbolS *
21862 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
21863 {
21864 #ifdef OBJ_ELF
21865   if (name[0] == '_' && name[1] == 'G'
21866       && streq (name, GLOBAL_OFFSET_TABLE_NAME))
21867     {
21868       if (!GOT_symbol)
21869         {
21870           if (symbol_find (name))
21871             as_bad (_("GOT already in the symbol table"));
21872
21873           GOT_symbol = symbol_new (name, undefined_section,
21874                                    (valueT) 0, & zero_address_frag);
21875         }
21876
21877       return GOT_symbol;
21878     }
21879 #endif
21880
21881   return NULL;
21882 }
21883
21884 /* Subroutine of md_apply_fix.   Check to see if an immediate can be
21885    computed as two separate immediate values, added together.  We
21886    already know that this value cannot be computed by just one ARM
21887    instruction.  */
21888
21889 static unsigned int
21890 validate_immediate_twopart (unsigned int   val,
21891                             unsigned int * highpart)
21892 {
21893   unsigned int a;
21894   unsigned int i;
21895
21896   for (i = 0; i < 32; i += 2)
21897     if (((a = rotate_left (val, i)) & 0xff) != 0)
21898       {
21899         if (a & 0xff00)
21900           {
21901             if (a & ~ 0xffff)
21902               continue;
21903             * highpart = (a  >> 8) | ((i + 24) << 7);
21904           }
21905         else if (a & 0xff0000)
21906           {
21907             if (a & 0xff000000)
21908               continue;
21909             * highpart = (a >> 16) | ((i + 16) << 7);
21910           }
21911         else
21912           {
21913             gas_assert (a & 0xff000000);
21914             * highpart = (a >> 24) | ((i + 8) << 7);
21915           }
21916
21917         return (a & 0xff) | (i << 7);
21918       }
21919
21920   return FAIL;
21921 }
21922
21923 static int
21924 validate_offset_imm (unsigned int val, int hwse)
21925 {
21926   if ((hwse && val > 255) || val > 4095)
21927     return FAIL;
21928   return val;
21929 }
21930
21931 /* Subroutine of md_apply_fix.   Do those data_ops which can take a
21932    negative immediate constant by altering the instruction.  A bit of
21933    a hack really.
21934         MOV <-> MVN
21935         AND <-> BIC
21936         ADC <-> SBC
21937         by inverting the second operand, and
21938         ADD <-> SUB
21939         CMP <-> CMN
21940         by negating the second operand.  */
21941
21942 static int
21943 negate_data_op (unsigned long * instruction,
21944                 unsigned long   value)
21945 {
21946   int op, new_inst;
21947   unsigned long negated, inverted;
21948
21949   negated = encode_arm_immediate (-value);
21950   inverted = encode_arm_immediate (~value);
21951
21952   op = (*instruction >> DATA_OP_SHIFT) & 0xf;
21953   switch (op)
21954     {
21955       /* First negates.  */
21956     case OPCODE_SUB:             /* ADD <-> SUB  */
21957       new_inst = OPCODE_ADD;
21958       value = negated;
21959       break;
21960
21961     case OPCODE_ADD:
21962       new_inst = OPCODE_SUB;
21963       value = negated;
21964       break;
21965
21966     case OPCODE_CMP:             /* CMP <-> CMN  */
21967       new_inst = OPCODE_CMN;
21968       value = negated;
21969       break;
21970
21971     case OPCODE_CMN:
21972       new_inst = OPCODE_CMP;
21973       value = negated;
21974       break;
21975
21976       /* Now Inverted ops.  */
21977     case OPCODE_MOV:             /* MOV <-> MVN  */
21978       new_inst = OPCODE_MVN;
21979       value = inverted;
21980       break;
21981
21982     case OPCODE_MVN:
21983       new_inst = OPCODE_MOV;
21984       value = inverted;
21985       break;
21986
21987     case OPCODE_AND:             /* AND <-> BIC  */
21988       new_inst = OPCODE_BIC;
21989       value = inverted;
21990       break;
21991
21992     case OPCODE_BIC:
21993       new_inst = OPCODE_AND;
21994       value = inverted;
21995       break;
21996
21997     case OPCODE_ADC:              /* ADC <-> SBC  */
21998       new_inst = OPCODE_SBC;
21999       value = inverted;
22000       break;
22001
22002     case OPCODE_SBC:
22003       new_inst = OPCODE_ADC;
22004       value = inverted;
22005       break;
22006
22007       /* We cannot do anything.  */
22008     default:
22009       return FAIL;
22010     }
22011
22012   if (value == (unsigned) FAIL)
22013     return FAIL;
22014
22015   *instruction &= OPCODE_MASK;
22016   *instruction |= new_inst << DATA_OP_SHIFT;
22017   return value;
22018 }
22019
22020 /* Like negate_data_op, but for Thumb-2.   */
22021
22022 static unsigned int
22023 thumb32_negate_data_op (offsetT *instruction, unsigned int value)
22024 {
22025   int op, new_inst;
22026   int rd;
22027   unsigned int negated, inverted;
22028
22029   negated = encode_thumb32_immediate (-value);
22030   inverted = encode_thumb32_immediate (~value);
22031
22032   rd = (*instruction >> 8) & 0xf;
22033   op = (*instruction >> T2_DATA_OP_SHIFT) & 0xf;
22034   switch (op)
22035     {
22036       /* ADD <-> SUB.  Includes CMP <-> CMN.  */
22037     case T2_OPCODE_SUB:
22038       new_inst = T2_OPCODE_ADD;
22039       value = negated;
22040       break;
22041
22042     case T2_OPCODE_ADD:
22043       new_inst = T2_OPCODE_SUB;
22044       value = negated;
22045       break;
22046
22047       /* ORR <-> ORN.  Includes MOV <-> MVN.  */
22048     case T2_OPCODE_ORR:
22049       new_inst = T2_OPCODE_ORN;
22050       value = inverted;
22051       break;
22052
22053     case T2_OPCODE_ORN:
22054       new_inst = T2_OPCODE_ORR;
22055       value = inverted;
22056       break;
22057
22058       /* AND <-> BIC.  TST has no inverted equivalent.  */
22059     case T2_OPCODE_AND:
22060       new_inst = T2_OPCODE_BIC;
22061       if (rd == 15)
22062         value = FAIL;
22063       else
22064         value = inverted;
22065       break;
22066
22067     case T2_OPCODE_BIC:
22068       new_inst = T2_OPCODE_AND;
22069       value = inverted;
22070       break;
22071
22072       /* ADC <-> SBC  */
22073     case T2_OPCODE_ADC:
22074       new_inst = T2_OPCODE_SBC;
22075       value = inverted;
22076       break;
22077
22078     case T2_OPCODE_SBC:
22079       new_inst = T2_OPCODE_ADC;
22080       value = inverted;
22081       break;
22082
22083       /* We cannot do anything.  */
22084     default:
22085       return FAIL;
22086     }
22087
22088   if (value == (unsigned int)FAIL)
22089     return FAIL;
22090
22091   *instruction &= T2_OPCODE_MASK;
22092   *instruction |= new_inst << T2_DATA_OP_SHIFT;
22093   return value;
22094 }
22095
22096 /* Read a 32-bit thumb instruction from buf.  */
22097 static unsigned long
22098 get_thumb32_insn (char * buf)
22099 {
22100   unsigned long insn;
22101   insn = md_chars_to_number (buf, THUMB_SIZE) << 16;
22102   insn |= md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22103
22104   return insn;
22105 }
22106
22107
22108 /* We usually want to set the low bit on the address of thumb function
22109    symbols.  In particular .word foo - . should have the low bit set.
22110    Generic code tries to fold the difference of two symbols to
22111    a constant.  Prevent this and force a relocation when the first symbols
22112    is a thumb function.  */
22113
22114 bfd_boolean
22115 arm_optimize_expr (expressionS *l, operatorT op, expressionS *r)
22116 {
22117   if (op == O_subtract
22118       && l->X_op == O_symbol
22119       && r->X_op == O_symbol
22120       && THUMB_IS_FUNC (l->X_add_symbol))
22121     {
22122       l->X_op = O_subtract;
22123       l->X_op_symbol = r->X_add_symbol;
22124       l->X_add_number -= r->X_add_number;
22125       return TRUE;
22126     }
22127
22128   /* Process as normal.  */
22129   return FALSE;
22130 }
22131
22132 /* Encode Thumb2 unconditional branches and calls. The encoding
22133    for the 2 are identical for the immediate values.  */
22134
22135 static void
22136 encode_thumb2_b_bl_offset (char * buf, offsetT value)
22137 {
22138 #define T2I1I2MASK  ((1 << 13) | (1 << 11))
22139   offsetT newval;
22140   offsetT newval2;
22141   addressT S, I1, I2, lo, hi;
22142
22143   S = (value >> 24) & 0x01;
22144   I1 = (value >> 23) & 0x01;
22145   I2 = (value >> 22) & 0x01;
22146   hi = (value >> 12) & 0x3ff;
22147   lo = (value >> 1) & 0x7ff;
22148   newval   = md_chars_to_number (buf, THUMB_SIZE);
22149   newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22150   newval  |= (S << 10) | hi;
22151   newval2 &=  ~T2I1I2MASK;
22152   newval2 |= (((I1 ^ S) << 13) | ((I2 ^ S) << 11) | lo) ^ T2I1I2MASK;
22153   md_number_to_chars (buf, newval, THUMB_SIZE);
22154   md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22155 }
22156
22157 void
22158 md_apply_fix (fixS *    fixP,
22159                valueT * valP,
22160                segT     seg)
22161 {
22162   offsetT        value = * valP;
22163   offsetT        newval;
22164   unsigned int   newimm;
22165   unsigned long  temp;
22166   int            sign;
22167   char *         buf = fixP->fx_where + fixP->fx_frag->fr_literal;
22168
22169   gas_assert (fixP->fx_r_type <= BFD_RELOC_UNUSED);
22170
22171   /* Note whether this will delete the relocation.  */
22172
22173   if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
22174     fixP->fx_done = 1;
22175
22176   /* On a 64-bit host, silently truncate 'value' to 32 bits for
22177      consistency with the behaviour on 32-bit hosts.  Remember value
22178      for emit_reloc.  */
22179   value &= 0xffffffff;
22180   value ^= 0x80000000;
22181   value -= 0x80000000;
22182
22183   *valP = value;
22184   fixP->fx_addnumber = value;
22185
22186   /* Same treatment for fixP->fx_offset.  */
22187   fixP->fx_offset &= 0xffffffff;
22188   fixP->fx_offset ^= 0x80000000;
22189   fixP->fx_offset -= 0x80000000;
22190
22191   switch (fixP->fx_r_type)
22192     {
22193     case BFD_RELOC_NONE:
22194       /* This will need to go in the object file.  */
22195       fixP->fx_done = 0;
22196       break;
22197
22198     case BFD_RELOC_ARM_IMMEDIATE:
22199       /* We claim that this fixup has been processed here,
22200          even if in fact we generate an error because we do
22201          not have a reloc for it, so tc_gen_reloc will reject it.  */
22202       fixP->fx_done = 1;
22203
22204       if (fixP->fx_addsy)
22205         {
22206           const char *msg = 0;
22207
22208           if (! S_IS_DEFINED (fixP->fx_addsy))
22209             msg = _("undefined symbol %s used as an immediate value");
22210           else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22211             msg = _("symbol %s is in a different section");
22212           else if (S_IS_WEAK (fixP->fx_addsy))
22213             msg = _("symbol %s is weak and may be overridden later");
22214
22215           if (msg)
22216             {
22217               as_bad_where (fixP->fx_file, fixP->fx_line,
22218                             msg, S_GET_NAME (fixP->fx_addsy));
22219               break;
22220             }
22221         }
22222
22223       temp = md_chars_to_number (buf, INSN_SIZE);
22224
22225       /* If the offset is negative, we should use encoding A2 for ADR.  */
22226       if ((temp & 0xfff0000) == 0x28f0000 && value < 0)
22227         newimm = negate_data_op (&temp, value);
22228       else
22229         {
22230           newimm = encode_arm_immediate (value);
22231
22232           /* If the instruction will fail, see if we can fix things up by
22233              changing the opcode.  */
22234           if (newimm == (unsigned int) FAIL)
22235             newimm = negate_data_op (&temp, value);
22236         }
22237
22238       if (newimm == (unsigned int) FAIL)
22239         {
22240           as_bad_where (fixP->fx_file, fixP->fx_line,
22241                         _("invalid constant (%lx) after fixup"),
22242                         (unsigned long) value);
22243           break;
22244         }
22245
22246       newimm |= (temp & 0xfffff000);
22247       md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22248       break;
22249
22250     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
22251       {
22252         unsigned int highpart = 0;
22253         unsigned int newinsn  = 0xe1a00000; /* nop.  */
22254
22255         if (fixP->fx_addsy)
22256           {
22257             const char *msg = 0;
22258
22259             if (! S_IS_DEFINED (fixP->fx_addsy))
22260               msg = _("undefined symbol %s used as an immediate value");
22261             else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
22262               msg = _("symbol %s is in a different section");
22263             else if (S_IS_WEAK (fixP->fx_addsy))
22264               msg = _("symbol %s is weak and may be overridden later");
22265
22266             if (msg)
22267               {
22268                 as_bad_where (fixP->fx_file, fixP->fx_line,
22269                               msg, S_GET_NAME (fixP->fx_addsy));
22270                 break;
22271               }
22272           }
22273
22274         newimm = encode_arm_immediate (value);
22275         temp = md_chars_to_number (buf, INSN_SIZE);
22276
22277         /* If the instruction will fail, see if we can fix things up by
22278            changing the opcode.  */
22279         if (newimm == (unsigned int) FAIL
22280             && (newimm = negate_data_op (& temp, value)) == (unsigned int) FAIL)
22281           {
22282             /* No ?  OK - try using two ADD instructions to generate
22283                the value.  */
22284             newimm = validate_immediate_twopart (value, & highpart);
22285
22286             /* Yes - then make sure that the second instruction is
22287                also an add.  */
22288             if (newimm != (unsigned int) FAIL)
22289               newinsn = temp;
22290             /* Still No ?  Try using a negated value.  */
22291             else if ((newimm = validate_immediate_twopart (- value, & highpart)) != (unsigned int) FAIL)
22292               temp = newinsn = (temp & OPCODE_MASK) | OPCODE_SUB << DATA_OP_SHIFT;
22293             /* Otherwise - give up.  */
22294             else
22295               {
22296                 as_bad_where (fixP->fx_file, fixP->fx_line,
22297                               _("unable to compute ADRL instructions for PC offset of 0x%lx"),
22298                               (long) value);
22299                 break;
22300               }
22301
22302             /* Replace the first operand in the 2nd instruction (which
22303                is the PC) with the destination register.  We have
22304                already added in the PC in the first instruction and we
22305                do not want to do it again.  */
22306             newinsn &= ~ 0xf0000;
22307             newinsn |= ((newinsn & 0x0f000) << 4);
22308           }
22309
22310         newimm |= (temp & 0xfffff000);
22311         md_number_to_chars (buf, (valueT) newimm, INSN_SIZE);
22312
22313         highpart |= (newinsn & 0xfffff000);
22314         md_number_to_chars (buf + INSN_SIZE, (valueT) highpart, INSN_SIZE);
22315       }
22316       break;
22317
22318     case BFD_RELOC_ARM_OFFSET_IMM:
22319       if (!fixP->fx_done && seg->use_rela_p)
22320         value = 0;
22321
22322     case BFD_RELOC_ARM_LITERAL:
22323       sign = value > 0;
22324
22325       if (value < 0)
22326         value = - value;
22327
22328       if (validate_offset_imm (value, 0) == FAIL)
22329         {
22330           if (fixP->fx_r_type == BFD_RELOC_ARM_LITERAL)
22331             as_bad_where (fixP->fx_file, fixP->fx_line,
22332                           _("invalid literal constant: pool needs to be closer"));
22333           else
22334             as_bad_where (fixP->fx_file, fixP->fx_line,
22335                           _("bad immediate value for offset (%ld)"),
22336                           (long) value);
22337           break;
22338         }
22339
22340       newval = md_chars_to_number (buf, INSN_SIZE);
22341       if (value == 0)
22342         newval &= 0xfffff000;
22343       else
22344         {
22345           newval &= 0xff7ff000;
22346           newval |= value | (sign ? INDEX_UP : 0);
22347         }
22348       md_number_to_chars (buf, newval, INSN_SIZE);
22349       break;
22350
22351     case BFD_RELOC_ARM_OFFSET_IMM8:
22352     case BFD_RELOC_ARM_HWLITERAL:
22353       sign = value > 0;
22354
22355       if (value < 0)
22356         value = - value;
22357
22358       if (validate_offset_imm (value, 1) == FAIL)
22359         {
22360           if (fixP->fx_r_type == BFD_RELOC_ARM_HWLITERAL)
22361             as_bad_where (fixP->fx_file, fixP->fx_line,
22362                           _("invalid literal constant: pool needs to be closer"));
22363           else
22364             as_bad_where (fixP->fx_file, fixP->fx_line,
22365                           _("bad immediate value for 8-bit offset (%ld)"),
22366                           (long) value);
22367           break;
22368         }
22369
22370       newval = md_chars_to_number (buf, INSN_SIZE);
22371       if (value == 0)
22372         newval &= 0xfffff0f0;
22373       else
22374         {
22375           newval &= 0xff7ff0f0;
22376           newval |= ((value >> 4) << 8) | (value & 0xf) | (sign ? INDEX_UP : 0);
22377         }
22378       md_number_to_chars (buf, newval, INSN_SIZE);
22379       break;
22380
22381     case BFD_RELOC_ARM_T32_OFFSET_U8:
22382       if (value < 0 || value > 1020 || value % 4 != 0)
22383         as_bad_where (fixP->fx_file, fixP->fx_line,
22384                       _("bad immediate value for offset (%ld)"), (long) value);
22385       value /= 4;
22386
22387       newval = md_chars_to_number (buf+2, THUMB_SIZE);
22388       newval |= value;
22389       md_number_to_chars (buf+2, newval, THUMB_SIZE);
22390       break;
22391
22392     case BFD_RELOC_ARM_T32_OFFSET_IMM:
22393       /* This is a complicated relocation used for all varieties of Thumb32
22394          load/store instruction with immediate offset:
22395
22396          1110 100P u1WL NNNN XXXX YYYY iiii iiii - +/-(U) pre/post(P) 8-bit,
22397                                                    *4, optional writeback(W)
22398                                                    (doubleword load/store)
22399
22400          1111 100S uTTL 1111 XXXX iiii iiii iiii - +/-(U) 12-bit PC-rel
22401          1111 100S 0TTL NNNN XXXX 1Pu1 iiii iiii - +/-(U) pre/post(P) 8-bit
22402          1111 100S 0TTL NNNN XXXX 1110 iiii iiii - positive 8-bit (T instruction)
22403          1111 100S 1TTL NNNN XXXX iiii iiii iiii - positive 12-bit
22404          1111 100S 0TTL NNNN XXXX 1100 iiii iiii - negative 8-bit
22405
22406          Uppercase letters indicate bits that are already encoded at
22407          this point.  Lowercase letters are our problem.  For the
22408          second block of instructions, the secondary opcode nybble
22409          (bits 8..11) is present, and bit 23 is zero, even if this is
22410          a PC-relative operation.  */
22411       newval = md_chars_to_number (buf, THUMB_SIZE);
22412       newval <<= 16;
22413       newval |= md_chars_to_number (buf+THUMB_SIZE, THUMB_SIZE);
22414
22415       if ((newval & 0xf0000000) == 0xe0000000)
22416         {
22417           /* Doubleword load/store: 8-bit offset, scaled by 4.  */
22418           if (value >= 0)
22419             newval |= (1 << 23);
22420           else
22421             value = -value;
22422           if (value % 4 != 0)
22423             {
22424               as_bad_where (fixP->fx_file, fixP->fx_line,
22425                             _("offset not a multiple of 4"));
22426               break;
22427             }
22428           value /= 4;
22429           if (value > 0xff)
22430             {
22431               as_bad_where (fixP->fx_file, fixP->fx_line,
22432                             _("offset out of range"));
22433               break;
22434             }
22435           newval &= ~0xff;
22436         }
22437       else if ((newval & 0x000f0000) == 0x000f0000)
22438         {
22439           /* PC-relative, 12-bit offset.  */
22440           if (value >= 0)
22441             newval |= (1 << 23);
22442           else
22443             value = -value;
22444           if (value > 0xfff)
22445             {
22446               as_bad_where (fixP->fx_file, fixP->fx_line,
22447                             _("offset out of range"));
22448               break;
22449             }
22450           newval &= ~0xfff;
22451         }
22452       else if ((newval & 0x00000100) == 0x00000100)
22453         {
22454           /* Writeback: 8-bit, +/- offset.  */
22455           if (value >= 0)
22456             newval |= (1 << 9);
22457           else
22458             value = -value;
22459           if (value > 0xff)
22460             {
22461               as_bad_where (fixP->fx_file, fixP->fx_line,
22462                             _("offset out of range"));
22463               break;
22464             }
22465           newval &= ~0xff;
22466         }
22467       else if ((newval & 0x00000f00) == 0x00000e00)
22468         {
22469           /* T-instruction: positive 8-bit offset.  */
22470           if (value < 0 || value > 0xff)
22471             {
22472               as_bad_where (fixP->fx_file, fixP->fx_line,
22473                             _("offset out of range"));
22474               break;
22475             }
22476           newval &= ~0xff;
22477           newval |= value;
22478         }
22479       else
22480         {
22481           /* Positive 12-bit or negative 8-bit offset.  */
22482           int limit;
22483           if (value >= 0)
22484             {
22485               newval |= (1 << 23);
22486               limit = 0xfff;
22487             }
22488           else
22489             {
22490               value = -value;
22491               limit = 0xff;
22492             }
22493           if (value > limit)
22494             {
22495               as_bad_where (fixP->fx_file, fixP->fx_line,
22496                             _("offset out of range"));
22497               break;
22498             }
22499           newval &= ~limit;
22500         }
22501
22502       newval |= value;
22503       md_number_to_chars (buf, (newval >> 16) & 0xffff, THUMB_SIZE);
22504       md_number_to_chars (buf + THUMB_SIZE, newval & 0xffff, THUMB_SIZE);
22505       break;
22506
22507     case BFD_RELOC_ARM_SHIFT_IMM:
22508       newval = md_chars_to_number (buf, INSN_SIZE);
22509       if (((unsigned long) value) > 32
22510           || (value == 32
22511               && (((newval & 0x60) == 0) || (newval & 0x60) == 0x60)))
22512         {
22513           as_bad_where (fixP->fx_file, fixP->fx_line,
22514                         _("shift expression is too large"));
22515           break;
22516         }
22517
22518       if (value == 0)
22519         /* Shifts of zero must be done as lsl.  */
22520         newval &= ~0x60;
22521       else if (value == 32)
22522         value = 0;
22523       newval &= 0xfffff07f;
22524       newval |= (value & 0x1f) << 7;
22525       md_number_to_chars (buf, newval, INSN_SIZE);
22526       break;
22527
22528     case BFD_RELOC_ARM_T32_IMMEDIATE:
22529     case BFD_RELOC_ARM_T32_ADD_IMM:
22530     case BFD_RELOC_ARM_T32_IMM12:
22531     case BFD_RELOC_ARM_T32_ADD_PC12:
22532       /* We claim that this fixup has been processed here,
22533          even if in fact we generate an error because we do
22534          not have a reloc for it, so tc_gen_reloc will reject it.  */
22535       fixP->fx_done = 1;
22536
22537       if (fixP->fx_addsy
22538           && ! S_IS_DEFINED (fixP->fx_addsy))
22539         {
22540           as_bad_where (fixP->fx_file, fixP->fx_line,
22541                         _("undefined symbol %s used as an immediate value"),
22542                         S_GET_NAME (fixP->fx_addsy));
22543           break;
22544         }
22545
22546       newval = md_chars_to_number (buf, THUMB_SIZE);
22547       newval <<= 16;
22548       newval |= md_chars_to_number (buf+2, THUMB_SIZE);
22549
22550       newimm = FAIL;
22551       if (fixP->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
22552           || fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
22553         {
22554           newimm = encode_thumb32_immediate (value);
22555           if (newimm == (unsigned int) FAIL)
22556             newimm = thumb32_negate_data_op (&newval, value);
22557         }
22558       if (fixP->fx_r_type != BFD_RELOC_ARM_T32_IMMEDIATE
22559           && newimm == (unsigned int) FAIL)
22560         {
22561           /* Turn add/sum into addw/subw.  */
22562           if (fixP->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM)
22563             newval = (newval & 0xfeffffff) | 0x02000000;
22564           /* No flat 12-bit imm encoding for addsw/subsw.  */
22565           if ((newval & 0x00100000) == 0)
22566             {
22567               /* 12 bit immediate for addw/subw.  */
22568               if (value < 0)
22569                 {
22570                   value = -value;
22571                   newval ^= 0x00a00000;
22572                 }
22573               if (value > 0xfff)
22574                 newimm = (unsigned int) FAIL;
22575               else
22576                 newimm = value;
22577             }
22578         }
22579
22580       if (newimm == (unsigned int)FAIL)
22581         {
22582           as_bad_where (fixP->fx_file, fixP->fx_line,
22583                         _("invalid constant (%lx) after fixup"),
22584                         (unsigned long) value);
22585           break;
22586         }
22587
22588       newval |= (newimm & 0x800) << 15;
22589       newval |= (newimm & 0x700) << 4;
22590       newval |= (newimm & 0x0ff);
22591
22592       md_number_to_chars (buf,   (valueT) ((newval >> 16) & 0xffff), THUMB_SIZE);
22593       md_number_to_chars (buf+2, (valueT) (newval & 0xffff), THUMB_SIZE);
22594       break;
22595
22596     case BFD_RELOC_ARM_SMC:
22597       if (((unsigned long) value) > 0xffff)
22598         as_bad_where (fixP->fx_file, fixP->fx_line,
22599                       _("invalid smc expression"));
22600       newval = md_chars_to_number (buf, INSN_SIZE);
22601       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
22602       md_number_to_chars (buf, newval, INSN_SIZE);
22603       break;
22604
22605     case BFD_RELOC_ARM_HVC:
22606       if (((unsigned long) value) > 0xffff)
22607         as_bad_where (fixP->fx_file, fixP->fx_line,
22608                       _("invalid hvc expression"));
22609       newval = md_chars_to_number (buf, INSN_SIZE);
22610       newval |= (value & 0xf) | ((value & 0xfff0) << 4);
22611       md_number_to_chars (buf, newval, INSN_SIZE);
22612       break;
22613
22614     case BFD_RELOC_ARM_SWI:
22615       if (fixP->tc_fix_data != 0)
22616         {
22617           if (((unsigned long) value) > 0xff)
22618             as_bad_where (fixP->fx_file, fixP->fx_line,
22619                           _("invalid swi expression"));
22620           newval = md_chars_to_number (buf, THUMB_SIZE);
22621           newval |= value;
22622           md_number_to_chars (buf, newval, THUMB_SIZE);
22623         }
22624       else
22625         {
22626           if (((unsigned long) value) > 0x00ffffff)
22627             as_bad_where (fixP->fx_file, fixP->fx_line,
22628                           _("invalid swi expression"));
22629           newval = md_chars_to_number (buf, INSN_SIZE);
22630           newval |= value;
22631           md_number_to_chars (buf, newval, INSN_SIZE);
22632         }
22633       break;
22634
22635     case BFD_RELOC_ARM_MULTI:
22636       if (((unsigned long) value) > 0xffff)
22637         as_bad_where (fixP->fx_file, fixP->fx_line,
22638                       _("invalid expression in load/store multiple"));
22639       newval = value | md_chars_to_number (buf, INSN_SIZE);
22640       md_number_to_chars (buf, newval, INSN_SIZE);
22641       break;
22642
22643 #ifdef OBJ_ELF
22644     case BFD_RELOC_ARM_PCREL_CALL:
22645
22646       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22647           && fixP->fx_addsy
22648           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22649           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22650           && THUMB_IS_FUNC (fixP->fx_addsy))
22651         /* Flip the bl to blx. This is a simple flip
22652            bit here because we generate PCREL_CALL for
22653            unconditional bls.  */
22654         {
22655           newval = md_chars_to_number (buf, INSN_SIZE);
22656           newval = newval | 0x10000000;
22657           md_number_to_chars (buf, newval, INSN_SIZE);
22658           temp = 1;
22659           fixP->fx_done = 1;
22660         }
22661       else
22662         temp = 3;
22663       goto arm_branch_common;
22664
22665     case BFD_RELOC_ARM_PCREL_JUMP:
22666       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22667           && fixP->fx_addsy
22668           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22669           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22670           && THUMB_IS_FUNC (fixP->fx_addsy))
22671         {
22672           /* This would map to a bl<cond>, b<cond>,
22673              b<always> to a Thumb function. We
22674              need to force a relocation for this particular
22675              case.  */
22676           newval = md_chars_to_number (buf, INSN_SIZE);
22677           fixP->fx_done = 0;
22678         }
22679
22680     case BFD_RELOC_ARM_PLT32:
22681 #endif
22682     case BFD_RELOC_ARM_PCREL_BRANCH:
22683       temp = 3;
22684       goto arm_branch_common;
22685
22686     case BFD_RELOC_ARM_PCREL_BLX:
22687
22688       temp = 1;
22689       if (ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
22690           && fixP->fx_addsy
22691           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22692           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22693           && ARM_IS_FUNC (fixP->fx_addsy))
22694         {
22695           /* Flip the blx to a bl and warn.  */
22696           const char *name = S_GET_NAME (fixP->fx_addsy);
22697           newval = 0xeb000000;
22698           as_warn_where (fixP->fx_file, fixP->fx_line,
22699                          _("blx to '%s' an ARM ISA state function changed to bl"),
22700                           name);
22701           md_number_to_chars (buf, newval, INSN_SIZE);
22702           temp = 3;
22703           fixP->fx_done = 1;
22704         }
22705
22706 #ifdef OBJ_ELF
22707        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
22708          fixP->fx_r_type = BFD_RELOC_ARM_PCREL_CALL;
22709 #endif
22710
22711     arm_branch_common:
22712       /* We are going to store value (shifted right by two) in the
22713          instruction, in a 24 bit, signed field.  Bits 26 through 32 either
22714          all clear or all set and bit 0 must be clear.  For B/BL bit 1 must
22715          also be be clear.  */
22716       if (value & temp)
22717         as_bad_where (fixP->fx_file, fixP->fx_line,
22718                       _("misaligned branch destination"));
22719       if ((value & (offsetT)0xfe000000) != (offsetT)0
22720           && (value & (offsetT)0xfe000000) != (offsetT)0xfe000000)
22721         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22722
22723       if (fixP->fx_done || !seg->use_rela_p)
22724         {
22725           newval = md_chars_to_number (buf, INSN_SIZE);
22726           newval |= (value >> 2) & 0x00ffffff;
22727           /* Set the H bit on BLX instructions.  */
22728           if (temp == 1)
22729             {
22730               if (value & 2)
22731                 newval |= 0x01000000;
22732               else
22733                 newval &= ~0x01000000;
22734             }
22735           md_number_to_chars (buf, newval, INSN_SIZE);
22736         }
22737       break;
22738
22739     case BFD_RELOC_THUMB_PCREL_BRANCH7: /* CBZ */
22740       /* CBZ can only branch forward.  */
22741
22742       /* Attempts to use CBZ to branch to the next instruction
22743          (which, strictly speaking, are prohibited) will be turned into
22744          no-ops.
22745
22746          FIXME: It may be better to remove the instruction completely and
22747          perform relaxation.  */
22748       if (value == -2)
22749         {
22750           newval = md_chars_to_number (buf, THUMB_SIZE);
22751           newval = 0xbf00; /* NOP encoding T1 */
22752           md_number_to_chars (buf, newval, THUMB_SIZE);
22753         }
22754       else
22755         {
22756           if (value & ~0x7e)
22757             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22758
22759           if (fixP->fx_done || !seg->use_rela_p)
22760             {
22761               newval = md_chars_to_number (buf, THUMB_SIZE);
22762               newval |= ((value & 0x3e) << 2) | ((value & 0x40) << 3);
22763               md_number_to_chars (buf, newval, THUMB_SIZE);
22764             }
22765         }
22766       break;
22767
22768     case BFD_RELOC_THUMB_PCREL_BRANCH9: /* Conditional branch.  */
22769       if ((value & ~0xff) && ((value & ~0xff) != ~0xff))
22770         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22771
22772       if (fixP->fx_done || !seg->use_rela_p)
22773         {
22774           newval = md_chars_to_number (buf, THUMB_SIZE);
22775           newval |= (value & 0x1ff) >> 1;
22776           md_number_to_chars (buf, newval, THUMB_SIZE);
22777         }
22778       break;
22779
22780     case BFD_RELOC_THUMB_PCREL_BRANCH12: /* Unconditional branch.  */
22781       if ((value & ~0x7ff) && ((value & ~0x7ff) != ~0x7ff))
22782         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22783
22784       if (fixP->fx_done || !seg->use_rela_p)
22785         {
22786           newval = md_chars_to_number (buf, THUMB_SIZE);
22787           newval |= (value & 0xfff) >> 1;
22788           md_number_to_chars (buf, newval, THUMB_SIZE);
22789         }
22790       break;
22791
22792     case BFD_RELOC_THUMB_PCREL_BRANCH20:
22793       if (fixP->fx_addsy
22794           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22795           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22796           && ARM_IS_FUNC (fixP->fx_addsy)
22797           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22798         {
22799           /* Force a relocation for a branch 20 bits wide.  */
22800           fixP->fx_done = 0;
22801         }
22802       if ((value & ~0x1fffff) && ((value & ~0x0fffff) != ~0x0fffff))
22803         as_bad_where (fixP->fx_file, fixP->fx_line,
22804                       _("conditional branch out of range"));
22805
22806       if (fixP->fx_done || !seg->use_rela_p)
22807         {
22808           offsetT newval2;
22809           addressT S, J1, J2, lo, hi;
22810
22811           S  = (value & 0x00100000) >> 20;
22812           J2 = (value & 0x00080000) >> 19;
22813           J1 = (value & 0x00040000) >> 18;
22814           hi = (value & 0x0003f000) >> 12;
22815           lo = (value & 0x00000ffe) >> 1;
22816
22817           newval   = md_chars_to_number (buf, THUMB_SIZE);
22818           newval2  = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22819           newval  |= (S << 10) | hi;
22820           newval2 |= (J1 << 13) | (J2 << 11) | lo;
22821           md_number_to_chars (buf, newval, THUMB_SIZE);
22822           md_number_to_chars (buf + THUMB_SIZE, newval2, THUMB_SIZE);
22823         }
22824       break;
22825
22826     case BFD_RELOC_THUMB_PCREL_BLX:
22827       /* If there is a blx from a thumb state function to
22828          another thumb function flip this to a bl and warn
22829          about it.  */
22830
22831       if (fixP->fx_addsy
22832           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22833           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22834           && THUMB_IS_FUNC (fixP->fx_addsy))
22835         {
22836           const char *name = S_GET_NAME (fixP->fx_addsy);
22837           as_warn_where (fixP->fx_file, fixP->fx_line,
22838                          _("blx to Thumb func '%s' from Thumb ISA state changed to bl"),
22839                          name);
22840           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22841           newval = newval | 0x1000;
22842           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22843           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22844           fixP->fx_done = 1;
22845         }
22846
22847
22848       goto thumb_bl_common;
22849
22850     case BFD_RELOC_THUMB_PCREL_BRANCH23:
22851       /* A bl from Thumb state ISA to an internal ARM state function
22852          is converted to a blx.  */
22853       if (fixP->fx_addsy
22854           && (S_GET_SEGMENT (fixP->fx_addsy) == seg)
22855           && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
22856           && ARM_IS_FUNC (fixP->fx_addsy)
22857           && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t))
22858         {
22859           newval = md_chars_to_number (buf + THUMB_SIZE, THUMB_SIZE);
22860           newval = newval & ~0x1000;
22861           md_number_to_chars (buf+THUMB_SIZE, newval, THUMB_SIZE);
22862           fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BLX;
22863           fixP->fx_done = 1;
22864         }
22865
22866     thumb_bl_common:
22867
22868       if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22869         /* For a BLX instruction, make sure that the relocation is rounded up
22870            to a word boundary.  This follows the semantics of the instruction
22871            which specifies that bit 1 of the target address will come from bit
22872            1 of the base address.  */
22873         value = (value + 3) & ~ 3;
22874
22875 #ifdef OBJ_ELF
22876        if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4
22877            && fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BLX)
22878          fixP->fx_r_type = BFD_RELOC_THUMB_PCREL_BRANCH23;
22879 #endif
22880
22881       if ((value & ~0x3fffff) && ((value & ~0x3fffff) != ~0x3fffff))
22882         {
22883           if (!(ARM_CPU_HAS_FEATURE (cpu_variant, arm_arch_t2)))
22884             as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22885           else if ((value & ~0x1ffffff)
22886                    && ((value & ~0x1ffffff) != ~0x1ffffff))
22887             as_bad_where (fixP->fx_file, fixP->fx_line,
22888                           _("Thumb2 branch out of range"));
22889         }
22890
22891       if (fixP->fx_done || !seg->use_rela_p)
22892         encode_thumb2_b_bl_offset (buf, value);
22893
22894       break;
22895
22896     case BFD_RELOC_THUMB_PCREL_BRANCH25:
22897       if ((value & ~0x0ffffff) && ((value & ~0x0ffffff) != ~0x0ffffff))
22898         as_bad_where (fixP->fx_file, fixP->fx_line, BAD_RANGE);
22899
22900       if (fixP->fx_done || !seg->use_rela_p)
22901           encode_thumb2_b_bl_offset (buf, value);
22902
22903       break;
22904
22905     case BFD_RELOC_8:
22906       if (fixP->fx_done || !seg->use_rela_p)
22907         *buf = value;
22908       break;
22909
22910     case BFD_RELOC_16:
22911       if (fixP->fx_done || !seg->use_rela_p)
22912         md_number_to_chars (buf, value, 2);
22913       break;
22914
22915 #ifdef OBJ_ELF
22916     case BFD_RELOC_ARM_TLS_CALL:
22917     case BFD_RELOC_ARM_THM_TLS_CALL:
22918     case BFD_RELOC_ARM_TLS_DESCSEQ:
22919     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
22920     case BFD_RELOC_ARM_TLS_GOTDESC:
22921     case BFD_RELOC_ARM_TLS_GD32:
22922     case BFD_RELOC_ARM_TLS_LE32:
22923     case BFD_RELOC_ARM_TLS_IE32:
22924     case BFD_RELOC_ARM_TLS_LDM32:
22925     case BFD_RELOC_ARM_TLS_LDO32:
22926       S_SET_THREAD_LOCAL (fixP->fx_addsy);
22927       break;
22928
22929     case BFD_RELOC_ARM_GOT32:
22930     case BFD_RELOC_ARM_GOTOFF:
22931       break;
22932
22933     case BFD_RELOC_ARM_GOT_PREL:
22934       if (fixP->fx_done || !seg->use_rela_p)
22935         md_number_to_chars (buf, value, 4);
22936       break;
22937
22938     case BFD_RELOC_ARM_TARGET2:
22939       /* TARGET2 is not partial-inplace, so we need to write the
22940          addend here for REL targets, because it won't be written out
22941          during reloc processing later.  */
22942       if (fixP->fx_done || !seg->use_rela_p)
22943         md_number_to_chars (buf, fixP->fx_offset, 4);
22944       break;
22945 #endif
22946
22947     case BFD_RELOC_RVA:
22948     case BFD_RELOC_32:
22949     case BFD_RELOC_ARM_TARGET1:
22950     case BFD_RELOC_ARM_ROSEGREL32:
22951     case BFD_RELOC_ARM_SBREL32:
22952     case BFD_RELOC_32_PCREL:
22953 #ifdef TE_PE
22954     case BFD_RELOC_32_SECREL:
22955 #endif
22956       if (fixP->fx_done || !seg->use_rela_p)
22957 #ifdef TE_WINCE
22958         /* For WinCE we only do this for pcrel fixups.  */
22959         if (fixP->fx_done || fixP->fx_pcrel)
22960 #endif
22961           md_number_to_chars (buf, value, 4);
22962       break;
22963
22964 #ifdef OBJ_ELF
22965     case BFD_RELOC_ARM_PREL31:
22966       if (fixP->fx_done || !seg->use_rela_p)
22967         {
22968           newval = md_chars_to_number (buf, 4) & 0x80000000;
22969           if ((value ^ (value >> 1)) & 0x40000000)
22970             {
22971               as_bad_where (fixP->fx_file, fixP->fx_line,
22972                             _("rel31 relocation overflow"));
22973             }
22974           newval |= value & 0x7fffffff;
22975           md_number_to_chars (buf, newval, 4);
22976         }
22977       break;
22978 #endif
22979
22980     case BFD_RELOC_ARM_CP_OFF_IMM:
22981     case BFD_RELOC_ARM_T32_CP_OFF_IMM:
22982       if (value < -1023 || value > 1023 || (value & 3))
22983         as_bad_where (fixP->fx_file, fixP->fx_line,
22984                       _("co-processor offset out of range"));
22985     cp_off_common:
22986       sign = value > 0;
22987       if (value < 0)
22988         value = -value;
22989       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
22990           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
22991         newval = md_chars_to_number (buf, INSN_SIZE);
22992       else
22993         newval = get_thumb32_insn (buf);
22994       if (value == 0)
22995         newval &= 0xffffff00;
22996       else
22997         {
22998           newval &= 0xff7fff00;
22999           newval |= (value >> 2) | (sign ? INDEX_UP : 0);
23000         }
23001       if (fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23002           || fixP->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2)
23003         md_number_to_chars (buf, newval, INSN_SIZE);
23004       else
23005         put_thumb32_insn (buf, newval);
23006       break;
23007
23008     case BFD_RELOC_ARM_CP_OFF_IMM_S2:
23009     case BFD_RELOC_ARM_T32_CP_OFF_IMM_S2:
23010       if (value < -255 || value > 255)
23011         as_bad_where (fixP->fx_file, fixP->fx_line,
23012                       _("co-processor offset out of range"));
23013       value *= 4;
23014       goto cp_off_common;
23015
23016     case BFD_RELOC_ARM_THUMB_OFFSET:
23017       newval = md_chars_to_number (buf, THUMB_SIZE);
23018       /* Exactly what ranges, and where the offset is inserted depends
23019          on the type of instruction, we can establish this from the
23020          top 4 bits.  */
23021       switch (newval >> 12)
23022         {
23023         case 4: /* PC load.  */
23024           /* Thumb PC loads are somewhat odd, bit 1 of the PC is
23025              forced to zero for these loads; md_pcrel_from has already
23026              compensated for this.  */
23027           if (value & 3)
23028             as_bad_where (fixP->fx_file, fixP->fx_line,
23029                           _("invalid offset, target not word aligned (0x%08lX)"),
23030                           (((unsigned long) fixP->fx_frag->fr_address
23031                             + (unsigned long) fixP->fx_where) & ~3)
23032                           + (unsigned long) value);
23033
23034           if (value & ~0x3fc)
23035             as_bad_where (fixP->fx_file, fixP->fx_line,
23036                           _("invalid offset, value too big (0x%08lX)"),
23037                           (long) value);
23038
23039           newval |= value >> 2;
23040           break;
23041
23042         case 9: /* SP load/store.  */
23043           if (value & ~0x3fc)
23044             as_bad_where (fixP->fx_file, fixP->fx_line,
23045                           _("invalid offset, value too big (0x%08lX)"),
23046                           (long) value);
23047           newval |= value >> 2;
23048           break;
23049
23050         case 6: /* Word load/store.  */
23051           if (value & ~0x7c)
23052             as_bad_where (fixP->fx_file, fixP->fx_line,
23053                           _("invalid offset, value too big (0x%08lX)"),
23054                           (long) value);
23055           newval |= value << 4; /* 6 - 2.  */
23056           break;
23057
23058         case 7: /* Byte load/store.  */
23059           if (value & ~0x1f)
23060             as_bad_where (fixP->fx_file, fixP->fx_line,
23061                           _("invalid offset, value too big (0x%08lX)"),
23062                           (long) value);
23063           newval |= value << 6;
23064           break;
23065
23066         case 8: /* Halfword load/store.  */
23067           if (value & ~0x3e)
23068             as_bad_where (fixP->fx_file, fixP->fx_line,
23069                           _("invalid offset, value too big (0x%08lX)"),
23070                           (long) value);
23071           newval |= value << 5; /* 6 - 1.  */
23072           break;
23073
23074         default:
23075           as_bad_where (fixP->fx_file, fixP->fx_line,
23076                         "Unable to process relocation for thumb opcode: %lx",
23077                         (unsigned long) newval);
23078           break;
23079         }
23080       md_number_to_chars (buf, newval, THUMB_SIZE);
23081       break;
23082
23083     case BFD_RELOC_ARM_THUMB_ADD:
23084       /* This is a complicated relocation, since we use it for all of
23085          the following immediate relocations:
23086
23087             3bit ADD/SUB
23088             8bit ADD/SUB
23089             9bit ADD/SUB SP word-aligned
23090            10bit ADD PC/SP word-aligned
23091
23092          The type of instruction being processed is encoded in the
23093          instruction field:
23094
23095            0x8000  SUB
23096            0x00F0  Rd
23097            0x000F  Rs
23098       */
23099       newval = md_chars_to_number (buf, THUMB_SIZE);
23100       {
23101         int rd = (newval >> 4) & 0xf;
23102         int rs = newval & 0xf;
23103         int subtract = !!(newval & 0x8000);
23104
23105         /* Check for HI regs, only very restricted cases allowed:
23106            Adjusting SP, and using PC or SP to get an address.  */
23107         if ((rd > 7 && (rd != REG_SP || rs != REG_SP))
23108             || (rs > 7 && rs != REG_SP && rs != REG_PC))
23109           as_bad_where (fixP->fx_file, fixP->fx_line,
23110                         _("invalid Hi register with immediate"));
23111
23112         /* If value is negative, choose the opposite instruction.  */
23113         if (value < 0)
23114           {
23115             value = -value;
23116             subtract = !subtract;
23117             if (value < 0)
23118               as_bad_where (fixP->fx_file, fixP->fx_line,
23119                             _("immediate value out of range"));
23120           }
23121
23122         if (rd == REG_SP)
23123           {
23124             if (value & ~0x1fc)
23125               as_bad_where (fixP->fx_file, fixP->fx_line,
23126                             _("invalid immediate for stack address calculation"));
23127             newval = subtract ? T_OPCODE_SUB_ST : T_OPCODE_ADD_ST;
23128             newval |= value >> 2;
23129           }
23130         else if (rs == REG_PC || rs == REG_SP)
23131           {
23132             /* PR gas/18541.  If the addition is for a defined symbol
23133                within range of an ADR instruction then accept it.  */
23134             if (subtract
23135                 && value == 4
23136                 && fixP->fx_addsy != NULL)
23137               {
23138                 subtract = 0;
23139
23140                 if (! S_IS_DEFINED (fixP->fx_addsy)
23141                     || S_GET_SEGMENT (fixP->fx_addsy) != seg
23142                     || S_IS_WEAK (fixP->fx_addsy))
23143                   {
23144                     as_bad_where (fixP->fx_file, fixP->fx_line,
23145                                   _("address calculation needs a strongly defined nearby symbol"));
23146                   }
23147                 else
23148                   {
23149                     offsetT v = fixP->fx_where + fixP->fx_frag->fr_address;
23150
23151                     /* Round up to the next 4-byte boundary.  */
23152                     if (v & 3)
23153                       v = (v + 3) & ~ 3;
23154                     else
23155                       v += 4;
23156                     v = S_GET_VALUE (fixP->fx_addsy) - v;
23157
23158                     if (v & ~0x3fc)
23159                       {
23160                         as_bad_where (fixP->fx_file, fixP->fx_line,
23161                                       _("symbol too far away"));
23162                       }
23163                     else
23164                       {
23165                         fixP->fx_done = 1;
23166                         value = v;
23167                       }
23168                   }
23169               }
23170
23171             if (subtract || value & ~0x3fc)
23172               as_bad_where (fixP->fx_file, fixP->fx_line,
23173                             _("invalid immediate for address calculation (value = 0x%08lX)"),
23174                             (unsigned long) (subtract ? - value : value));
23175             newval = (rs == REG_PC ? T_OPCODE_ADD_PC : T_OPCODE_ADD_SP);
23176             newval |= rd << 8;
23177             newval |= value >> 2;
23178           }
23179         else if (rs == rd)
23180           {
23181             if (value & ~0xff)
23182               as_bad_where (fixP->fx_file, fixP->fx_line,
23183                             _("immediate value out of range"));
23184             newval = subtract ? T_OPCODE_SUB_I8 : T_OPCODE_ADD_I8;
23185             newval |= (rd << 8) | value;
23186           }
23187         else
23188           {
23189             if (value & ~0x7)
23190               as_bad_where (fixP->fx_file, fixP->fx_line,
23191                             _("immediate value out of range"));
23192             newval = subtract ? T_OPCODE_SUB_I3 : T_OPCODE_ADD_I3;
23193             newval |= rd | (rs << 3) | (value << 6);
23194           }
23195       }
23196       md_number_to_chars (buf, newval, THUMB_SIZE);
23197       break;
23198
23199     case BFD_RELOC_ARM_THUMB_IMM:
23200       newval = md_chars_to_number (buf, THUMB_SIZE);
23201       if (value < 0 || value > 255)
23202         as_bad_where (fixP->fx_file, fixP->fx_line,
23203                       _("invalid immediate: %ld is out of range"),
23204                       (long) value);
23205       newval |= value;
23206       md_number_to_chars (buf, newval, THUMB_SIZE);
23207       break;
23208
23209     case BFD_RELOC_ARM_THUMB_SHIFT:
23210       /* 5bit shift value (0..32).  LSL cannot take 32.  */
23211       newval = md_chars_to_number (buf, THUMB_SIZE) & 0xf83f;
23212       temp = newval & 0xf800;
23213       if (value < 0 || value > 32 || (value == 32 && temp == T_OPCODE_LSL_I))
23214         as_bad_where (fixP->fx_file, fixP->fx_line,
23215                       _("invalid shift value: %ld"), (long) value);
23216       /* Shifts of zero must be encoded as LSL.  */
23217       if (value == 0)
23218         newval = (newval & 0x003f) | T_OPCODE_LSL_I;
23219       /* Shifts of 32 are encoded as zero.  */
23220       else if (value == 32)
23221         value = 0;
23222       newval |= value << 6;
23223       md_number_to_chars (buf, newval, THUMB_SIZE);
23224       break;
23225
23226     case BFD_RELOC_VTABLE_INHERIT:
23227     case BFD_RELOC_VTABLE_ENTRY:
23228       fixP->fx_done = 0;
23229       return;
23230
23231     case BFD_RELOC_ARM_MOVW:
23232     case BFD_RELOC_ARM_MOVT:
23233     case BFD_RELOC_ARM_THUMB_MOVW:
23234     case BFD_RELOC_ARM_THUMB_MOVT:
23235       if (fixP->fx_done || !seg->use_rela_p)
23236         {
23237           /* REL format relocations are limited to a 16-bit addend.  */
23238           if (!fixP->fx_done)
23239             {
23240               if (value < -0x8000 || value > 0x7fff)
23241                   as_bad_where (fixP->fx_file, fixP->fx_line,
23242                                 _("offset out of range"));
23243             }
23244           else if (fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23245                    || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23246             {
23247               value >>= 16;
23248             }
23249
23250           if (fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23251               || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT)
23252             {
23253               newval = get_thumb32_insn (buf);
23254               newval &= 0xfbf08f00;
23255               newval |= (value & 0xf000) << 4;
23256               newval |= (value & 0x0800) << 15;
23257               newval |= (value & 0x0700) << 4;
23258               newval |= (value & 0x00ff);
23259               put_thumb32_insn (buf, newval);
23260             }
23261           else
23262             {
23263               newval = md_chars_to_number (buf, 4);
23264               newval &= 0xfff0f000;
23265               newval |= value & 0x0fff;
23266               newval |= (value & 0xf000) << 4;
23267               md_number_to_chars (buf, newval, 4);
23268             }
23269         }
23270       return;
23271
23272    case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
23273    case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
23274    case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
23275    case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
23276       gas_assert (!fixP->fx_done);
23277       {
23278         bfd_vma insn;
23279         bfd_boolean is_mov;
23280         bfd_vma encoded_addend = value;
23281
23282         /* Check that addend can be encoded in instruction.  */
23283         if (!seg->use_rela_p && (value < 0 || value > 255))
23284           as_bad_where (fixP->fx_file, fixP->fx_line,
23285                         _("the offset 0x%08lX is not representable"),
23286                         (unsigned long) encoded_addend);
23287
23288         /* Extract the instruction.  */
23289         insn = md_chars_to_number (buf, THUMB_SIZE);
23290         is_mov = (insn & 0xf800) == 0x2000;
23291
23292         /* Encode insn.  */
23293         if (is_mov)
23294           {
23295             if (!seg->use_rela_p)
23296               insn |= encoded_addend;
23297           }
23298         else
23299           {
23300             int rd, rs;
23301
23302             /* Extract the instruction.  */
23303              /* Encoding is the following
23304                 0x8000  SUB
23305                 0x00F0  Rd
23306                 0x000F  Rs
23307              */
23308              /* The following conditions must be true :
23309                 - ADD
23310                 - Rd == Rs
23311                 - Rd <= 7
23312              */
23313             rd = (insn >> 4) & 0xf;
23314             rs = insn & 0xf;
23315             if ((insn & 0x8000) || (rd != rs) || rd > 7)
23316               as_bad_where (fixP->fx_file, fixP->fx_line,
23317                         _("Unable to process relocation for thumb opcode: %lx"),
23318                         (unsigned long) insn);
23319
23320             /* Encode as ADD immediate8 thumb 1 code.  */
23321             insn = 0x3000 | (rd << 8);
23322
23323             /* Place the encoded addend into the first 8 bits of the
23324                instruction.  */
23325             if (!seg->use_rela_p)
23326               insn |= encoded_addend;
23327           }
23328
23329         /* Update the instruction.  */
23330         md_number_to_chars (buf, insn, THUMB_SIZE);
23331       }
23332       break;
23333
23334    case BFD_RELOC_ARM_ALU_PC_G0_NC:
23335    case BFD_RELOC_ARM_ALU_PC_G0:
23336    case BFD_RELOC_ARM_ALU_PC_G1_NC:
23337    case BFD_RELOC_ARM_ALU_PC_G1:
23338    case BFD_RELOC_ARM_ALU_PC_G2:
23339    case BFD_RELOC_ARM_ALU_SB_G0_NC:
23340    case BFD_RELOC_ARM_ALU_SB_G0:
23341    case BFD_RELOC_ARM_ALU_SB_G1_NC:
23342    case BFD_RELOC_ARM_ALU_SB_G1:
23343    case BFD_RELOC_ARM_ALU_SB_G2:
23344      gas_assert (!fixP->fx_done);
23345      if (!seg->use_rela_p)
23346        {
23347          bfd_vma insn;
23348          bfd_vma encoded_addend;
23349          bfd_vma addend_abs = abs (value);
23350
23351          /* Check that the absolute value of the addend can be
23352             expressed as an 8-bit constant plus a rotation.  */
23353          encoded_addend = encode_arm_immediate (addend_abs);
23354          if (encoded_addend == (unsigned int) FAIL)
23355            as_bad_where (fixP->fx_file, fixP->fx_line,
23356                          _("the offset 0x%08lX is not representable"),
23357                          (unsigned long) addend_abs);
23358
23359          /* Extract the instruction.  */
23360          insn = md_chars_to_number (buf, INSN_SIZE);
23361
23362          /* If the addend is positive, use an ADD instruction.
23363             Otherwise use a SUB.  Take care not to destroy the S bit.  */
23364          insn &= 0xff1fffff;
23365          if (value < 0)
23366            insn |= 1 << 22;
23367          else
23368            insn |= 1 << 23;
23369
23370          /* Place the encoded addend into the first 12 bits of the
23371             instruction.  */
23372          insn &= 0xfffff000;
23373          insn |= encoded_addend;
23374
23375          /* Update the instruction.  */
23376          md_number_to_chars (buf, insn, INSN_SIZE);
23377        }
23378      break;
23379
23380     case BFD_RELOC_ARM_LDR_PC_G0:
23381     case BFD_RELOC_ARM_LDR_PC_G1:
23382     case BFD_RELOC_ARM_LDR_PC_G2:
23383     case BFD_RELOC_ARM_LDR_SB_G0:
23384     case BFD_RELOC_ARM_LDR_SB_G1:
23385     case BFD_RELOC_ARM_LDR_SB_G2:
23386       gas_assert (!fixP->fx_done);
23387       if (!seg->use_rela_p)
23388         {
23389           bfd_vma insn;
23390           bfd_vma addend_abs = abs (value);
23391
23392           /* Check that the absolute value of the addend can be
23393              encoded in 12 bits.  */
23394           if (addend_abs >= 0x1000)
23395             as_bad_where (fixP->fx_file, fixP->fx_line,
23396                           _("bad offset 0x%08lX (only 12 bits available for the magnitude)"),
23397                           (unsigned long) addend_abs);
23398
23399           /* Extract the instruction.  */
23400           insn = md_chars_to_number (buf, INSN_SIZE);
23401
23402           /* If the addend is negative, clear bit 23 of the instruction.
23403              Otherwise set it.  */
23404           if (value < 0)
23405             insn &= ~(1 << 23);
23406           else
23407             insn |= 1 << 23;
23408
23409           /* Place the absolute value of the addend into the first 12 bits
23410              of the instruction.  */
23411           insn &= 0xfffff000;
23412           insn |= addend_abs;
23413
23414           /* Update the instruction.  */
23415           md_number_to_chars (buf, insn, INSN_SIZE);
23416         }
23417       break;
23418
23419     case BFD_RELOC_ARM_LDRS_PC_G0:
23420     case BFD_RELOC_ARM_LDRS_PC_G1:
23421     case BFD_RELOC_ARM_LDRS_PC_G2:
23422     case BFD_RELOC_ARM_LDRS_SB_G0:
23423     case BFD_RELOC_ARM_LDRS_SB_G1:
23424     case BFD_RELOC_ARM_LDRS_SB_G2:
23425       gas_assert (!fixP->fx_done);
23426       if (!seg->use_rela_p)
23427         {
23428           bfd_vma insn;
23429           bfd_vma addend_abs = abs (value);
23430
23431           /* Check that the absolute value of the addend can be
23432              encoded in 8 bits.  */
23433           if (addend_abs >= 0x100)
23434             as_bad_where (fixP->fx_file, fixP->fx_line,
23435                           _("bad offset 0x%08lX (only 8 bits available for the magnitude)"),
23436                           (unsigned long) addend_abs);
23437
23438           /* Extract the instruction.  */
23439           insn = md_chars_to_number (buf, INSN_SIZE);
23440
23441           /* If the addend is negative, clear bit 23 of the instruction.
23442              Otherwise set it.  */
23443           if (value < 0)
23444             insn &= ~(1 << 23);
23445           else
23446             insn |= 1 << 23;
23447
23448           /* Place the first four bits of the absolute value of the addend
23449              into the first 4 bits of the instruction, and the remaining
23450              four into bits 8 .. 11.  */
23451           insn &= 0xfffff0f0;
23452           insn |= (addend_abs & 0xf) | ((addend_abs & 0xf0) << 4);
23453
23454           /* Update the instruction.  */
23455           md_number_to_chars (buf, insn, INSN_SIZE);
23456         }
23457       break;
23458
23459     case BFD_RELOC_ARM_LDC_PC_G0:
23460     case BFD_RELOC_ARM_LDC_PC_G1:
23461     case BFD_RELOC_ARM_LDC_PC_G2:
23462     case BFD_RELOC_ARM_LDC_SB_G0:
23463     case BFD_RELOC_ARM_LDC_SB_G1:
23464     case BFD_RELOC_ARM_LDC_SB_G2:
23465       gas_assert (!fixP->fx_done);
23466       if (!seg->use_rela_p)
23467         {
23468           bfd_vma insn;
23469           bfd_vma addend_abs = abs (value);
23470
23471           /* Check that the absolute value of the addend is a multiple of
23472              four and, when divided by four, fits in 8 bits.  */
23473           if (addend_abs & 0x3)
23474             as_bad_where (fixP->fx_file, fixP->fx_line,
23475                           _("bad offset 0x%08lX (must be word-aligned)"),
23476                           (unsigned long) addend_abs);
23477
23478           if ((addend_abs >> 2) > 0xff)
23479             as_bad_where (fixP->fx_file, fixP->fx_line,
23480                           _("bad offset 0x%08lX (must be an 8-bit number of words)"),
23481                           (unsigned long) addend_abs);
23482
23483           /* Extract the instruction.  */
23484           insn = md_chars_to_number (buf, INSN_SIZE);
23485
23486           /* If the addend is negative, clear bit 23 of the instruction.
23487              Otherwise set it.  */
23488           if (value < 0)
23489             insn &= ~(1 << 23);
23490           else
23491             insn |= 1 << 23;
23492
23493           /* Place the addend (divided by four) into the first eight
23494              bits of the instruction.  */
23495           insn &= 0xfffffff0;
23496           insn |= addend_abs >> 2;
23497
23498           /* Update the instruction.  */
23499           md_number_to_chars (buf, insn, INSN_SIZE);
23500         }
23501       break;
23502
23503     case BFD_RELOC_ARM_V4BX:
23504       /* This will need to go in the object file.  */
23505       fixP->fx_done = 0;
23506       break;
23507
23508     case BFD_RELOC_UNUSED:
23509     default:
23510       as_bad_where (fixP->fx_file, fixP->fx_line,
23511                     _("bad relocation fixup type (%d)"), fixP->fx_r_type);
23512     }
23513 }
23514
23515 /* Translate internal representation of relocation info to BFD target
23516    format.  */
23517
23518 arelent *
23519 tc_gen_reloc (asection *section, fixS *fixp)
23520 {
23521   arelent * reloc;
23522   bfd_reloc_code_real_type code;
23523
23524   reloc = (arelent *) xmalloc (sizeof (arelent));
23525
23526   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
23527   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
23528   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
23529
23530   if (fixp->fx_pcrel)
23531     {
23532       if (section->use_rela_p)
23533         fixp->fx_offset -= md_pcrel_from_section (fixp, section);
23534       else
23535         fixp->fx_offset = reloc->address;
23536     }
23537   reloc->addend = fixp->fx_offset;
23538
23539   switch (fixp->fx_r_type)
23540     {
23541     case BFD_RELOC_8:
23542       if (fixp->fx_pcrel)
23543         {
23544           code = BFD_RELOC_8_PCREL;
23545           break;
23546         }
23547
23548     case BFD_RELOC_16:
23549       if (fixp->fx_pcrel)
23550         {
23551           code = BFD_RELOC_16_PCREL;
23552           break;
23553         }
23554
23555     case BFD_RELOC_32:
23556       if (fixp->fx_pcrel)
23557         {
23558           code = BFD_RELOC_32_PCREL;
23559           break;
23560         }
23561
23562     case BFD_RELOC_ARM_MOVW:
23563       if (fixp->fx_pcrel)
23564         {
23565           code = BFD_RELOC_ARM_MOVW_PCREL;
23566           break;
23567         }
23568
23569     case BFD_RELOC_ARM_MOVT:
23570       if (fixp->fx_pcrel)
23571         {
23572           code = BFD_RELOC_ARM_MOVT_PCREL;
23573           break;
23574         }
23575
23576     case BFD_RELOC_ARM_THUMB_MOVW:
23577       if (fixp->fx_pcrel)
23578         {
23579           code = BFD_RELOC_ARM_THUMB_MOVW_PCREL;
23580           break;
23581         }
23582
23583     case BFD_RELOC_ARM_THUMB_MOVT:
23584       if (fixp->fx_pcrel)
23585         {
23586           code = BFD_RELOC_ARM_THUMB_MOVT_PCREL;
23587           break;
23588         }
23589
23590     case BFD_RELOC_NONE:
23591     case BFD_RELOC_ARM_PCREL_BRANCH:
23592     case BFD_RELOC_ARM_PCREL_BLX:
23593     case BFD_RELOC_RVA:
23594     case BFD_RELOC_THUMB_PCREL_BRANCH7:
23595     case BFD_RELOC_THUMB_PCREL_BRANCH9:
23596     case BFD_RELOC_THUMB_PCREL_BRANCH12:
23597     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23598     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23599     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23600     case BFD_RELOC_VTABLE_ENTRY:
23601     case BFD_RELOC_VTABLE_INHERIT:
23602 #ifdef TE_PE
23603     case BFD_RELOC_32_SECREL:
23604 #endif
23605       code = fixp->fx_r_type;
23606       break;
23607
23608     case BFD_RELOC_THUMB_PCREL_BLX:
23609 #ifdef OBJ_ELF
23610       if (EF_ARM_EABI_VERSION (meabi_flags) >= EF_ARM_EABI_VER4)
23611         code = BFD_RELOC_THUMB_PCREL_BRANCH23;
23612       else
23613 #endif
23614         code = BFD_RELOC_THUMB_PCREL_BLX;
23615       break;
23616
23617     case BFD_RELOC_ARM_LITERAL:
23618     case BFD_RELOC_ARM_HWLITERAL:
23619       /* If this is called then the a literal has
23620          been referenced across a section boundary.  */
23621       as_bad_where (fixp->fx_file, fixp->fx_line,
23622                     _("literal referenced across section boundary"));
23623       return NULL;
23624
23625 #ifdef OBJ_ELF
23626     case BFD_RELOC_ARM_TLS_CALL:
23627     case BFD_RELOC_ARM_THM_TLS_CALL:
23628     case BFD_RELOC_ARM_TLS_DESCSEQ:
23629     case BFD_RELOC_ARM_THM_TLS_DESCSEQ:
23630     case BFD_RELOC_ARM_GOT32:
23631     case BFD_RELOC_ARM_GOTOFF:
23632     case BFD_RELOC_ARM_GOT_PREL:
23633     case BFD_RELOC_ARM_PLT32:
23634     case BFD_RELOC_ARM_TARGET1:
23635     case BFD_RELOC_ARM_ROSEGREL32:
23636     case BFD_RELOC_ARM_SBREL32:
23637     case BFD_RELOC_ARM_PREL31:
23638     case BFD_RELOC_ARM_TARGET2:
23639     case BFD_RELOC_ARM_TLS_LDO32:
23640     case BFD_RELOC_ARM_PCREL_CALL:
23641     case BFD_RELOC_ARM_PCREL_JUMP:
23642     case BFD_RELOC_ARM_ALU_PC_G0_NC:
23643     case BFD_RELOC_ARM_ALU_PC_G0:
23644     case BFD_RELOC_ARM_ALU_PC_G1_NC:
23645     case BFD_RELOC_ARM_ALU_PC_G1:
23646     case BFD_RELOC_ARM_ALU_PC_G2:
23647     case BFD_RELOC_ARM_LDR_PC_G0:
23648     case BFD_RELOC_ARM_LDR_PC_G1:
23649     case BFD_RELOC_ARM_LDR_PC_G2:
23650     case BFD_RELOC_ARM_LDRS_PC_G0:
23651     case BFD_RELOC_ARM_LDRS_PC_G1:
23652     case BFD_RELOC_ARM_LDRS_PC_G2:
23653     case BFD_RELOC_ARM_LDC_PC_G0:
23654     case BFD_RELOC_ARM_LDC_PC_G1:
23655     case BFD_RELOC_ARM_LDC_PC_G2:
23656     case BFD_RELOC_ARM_ALU_SB_G0_NC:
23657     case BFD_RELOC_ARM_ALU_SB_G0:
23658     case BFD_RELOC_ARM_ALU_SB_G1_NC:
23659     case BFD_RELOC_ARM_ALU_SB_G1:
23660     case BFD_RELOC_ARM_ALU_SB_G2:
23661     case BFD_RELOC_ARM_LDR_SB_G0:
23662     case BFD_RELOC_ARM_LDR_SB_G1:
23663     case BFD_RELOC_ARM_LDR_SB_G2:
23664     case BFD_RELOC_ARM_LDRS_SB_G0:
23665     case BFD_RELOC_ARM_LDRS_SB_G1:
23666     case BFD_RELOC_ARM_LDRS_SB_G2:
23667     case BFD_RELOC_ARM_LDC_SB_G0:
23668     case BFD_RELOC_ARM_LDC_SB_G1:
23669     case BFD_RELOC_ARM_LDC_SB_G2:
23670     case BFD_RELOC_ARM_V4BX:
23671     case BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC:
23672     case BFD_RELOC_ARM_THUMB_ALU_ABS_G1_NC:
23673     case BFD_RELOC_ARM_THUMB_ALU_ABS_G2_NC:
23674     case BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC:
23675       code = fixp->fx_r_type;
23676       break;
23677
23678     case BFD_RELOC_ARM_TLS_GOTDESC:
23679     case BFD_RELOC_ARM_TLS_GD32:
23680     case BFD_RELOC_ARM_TLS_LE32:
23681     case BFD_RELOC_ARM_TLS_IE32:
23682     case BFD_RELOC_ARM_TLS_LDM32:
23683       /* BFD will include the symbol's address in the addend.
23684          But we don't want that, so subtract it out again here.  */
23685       if (!S_IS_COMMON (fixp->fx_addsy))
23686         reloc->addend -= (*reloc->sym_ptr_ptr)->value;
23687       code = fixp->fx_r_type;
23688       break;
23689 #endif
23690
23691     case BFD_RELOC_ARM_IMMEDIATE:
23692       as_bad_where (fixp->fx_file, fixp->fx_line,
23693                     _("internal relocation (type: IMMEDIATE) not fixed up"));
23694       return NULL;
23695
23696     case BFD_RELOC_ARM_ADRL_IMMEDIATE:
23697       as_bad_where (fixp->fx_file, fixp->fx_line,
23698                     _("ADRL used for a symbol not defined in the same file"));
23699       return NULL;
23700
23701     case BFD_RELOC_ARM_OFFSET_IMM:
23702       if (section->use_rela_p)
23703         {
23704           code = fixp->fx_r_type;
23705           break;
23706         }
23707
23708       if (fixp->fx_addsy != NULL
23709           && !S_IS_DEFINED (fixp->fx_addsy)
23710           && S_IS_LOCAL (fixp->fx_addsy))
23711         {
23712           as_bad_where (fixp->fx_file, fixp->fx_line,
23713                         _("undefined local label `%s'"),
23714                         S_GET_NAME (fixp->fx_addsy));
23715           return NULL;
23716         }
23717
23718       as_bad_where (fixp->fx_file, fixp->fx_line,
23719                     _("internal_relocation (type: OFFSET_IMM) not fixed up"));
23720       return NULL;
23721
23722     default:
23723       {
23724         char * type;
23725
23726         switch (fixp->fx_r_type)
23727           {
23728           case BFD_RELOC_NONE:             type = "NONE";         break;
23729           case BFD_RELOC_ARM_OFFSET_IMM8:  type = "OFFSET_IMM8";  break;
23730           case BFD_RELOC_ARM_SHIFT_IMM:    type = "SHIFT_IMM";    break;
23731           case BFD_RELOC_ARM_SMC:          type = "SMC";          break;
23732           case BFD_RELOC_ARM_SWI:          type = "SWI";          break;
23733           case BFD_RELOC_ARM_MULTI:        type = "MULTI";        break;
23734           case BFD_RELOC_ARM_CP_OFF_IMM:   type = "CP_OFF_IMM";   break;
23735           case BFD_RELOC_ARM_T32_OFFSET_IMM: type = "T32_OFFSET_IMM"; break;
23736           case BFD_RELOC_ARM_T32_CP_OFF_IMM: type = "T32_CP_OFF_IMM"; break;
23737           case BFD_RELOC_ARM_THUMB_ADD:    type = "THUMB_ADD";    break;
23738           case BFD_RELOC_ARM_THUMB_SHIFT:  type = "THUMB_SHIFT";  break;
23739           case BFD_RELOC_ARM_THUMB_IMM:    type = "THUMB_IMM";    break;
23740           case BFD_RELOC_ARM_THUMB_OFFSET: type = "THUMB_OFFSET"; break;
23741           default:                         type = _("<unknown>"); break;
23742           }
23743         as_bad_where (fixp->fx_file, fixp->fx_line,
23744                       _("cannot represent %s relocation in this object file format"),
23745                       type);
23746         return NULL;
23747       }
23748     }
23749
23750 #ifdef OBJ_ELF
23751   if ((code == BFD_RELOC_32_PCREL || code == BFD_RELOC_32)
23752       && GOT_symbol
23753       && fixp->fx_addsy == GOT_symbol)
23754     {
23755       code = BFD_RELOC_ARM_GOTPC;
23756       reloc->addend = fixp->fx_offset = reloc->address;
23757     }
23758 #endif
23759
23760   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
23761
23762   if (reloc->howto == NULL)
23763     {
23764       as_bad_where (fixp->fx_file, fixp->fx_line,
23765                     _("cannot represent %s relocation in this object file format"),
23766                     bfd_get_reloc_code_name (code));
23767       return NULL;
23768     }
23769
23770   /* HACK: Since arm ELF uses Rel instead of Rela, encode the
23771      vtable entry to be used in the relocation's section offset.  */
23772   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
23773     reloc->address = fixp->fx_offset;
23774
23775   return reloc;
23776 }
23777
23778 /* This fix_new is called by cons via TC_CONS_FIX_NEW.  */
23779
23780 void
23781 cons_fix_new_arm (fragS *       frag,
23782                   int           where,
23783                   int           size,
23784                   expressionS * exp,
23785                   bfd_reloc_code_real_type reloc)
23786 {
23787   int pcrel = 0;
23788
23789   /* Pick a reloc.
23790      FIXME: @@ Should look at CPU word size.  */
23791   switch (size)
23792     {
23793     case 1:
23794       reloc = BFD_RELOC_8;
23795       break;
23796     case 2:
23797       reloc = BFD_RELOC_16;
23798       break;
23799     case 4:
23800     default:
23801       reloc = BFD_RELOC_32;
23802       break;
23803     case 8:
23804       reloc = BFD_RELOC_64;
23805       break;
23806     }
23807
23808 #ifdef TE_PE
23809   if (exp->X_op == O_secrel)
23810   {
23811     exp->X_op = O_symbol;
23812     reloc = BFD_RELOC_32_SECREL;
23813   }
23814 #endif
23815
23816   fix_new_exp (frag, where, size, exp, pcrel, reloc);
23817 }
23818
23819 #if defined (OBJ_COFF)
23820 void
23821 arm_validate_fix (fixS * fixP)
23822 {
23823   /* If the destination of the branch is a defined symbol which does not have
23824      the THUMB_FUNC attribute, then we must be calling a function which has
23825      the (interfacearm) attribute.  We look for the Thumb entry point to that
23826      function and change the branch to refer to that function instead.  */
23827   if (fixP->fx_r_type == BFD_RELOC_THUMB_PCREL_BRANCH23
23828       && fixP->fx_addsy != NULL
23829       && S_IS_DEFINED (fixP->fx_addsy)
23830       && ! THUMB_IS_FUNC (fixP->fx_addsy))
23831     {
23832       fixP->fx_addsy = find_real_start (fixP->fx_addsy);
23833     }
23834 }
23835 #endif
23836
23837
23838 int
23839 arm_force_relocation (struct fix * fixp)
23840 {
23841 #if defined (OBJ_COFF) && defined (TE_PE)
23842   if (fixp->fx_r_type == BFD_RELOC_RVA)
23843     return 1;
23844 #endif
23845
23846   /* In case we have a call or a branch to a function in ARM ISA mode from
23847      a thumb function or vice-versa force the relocation. These relocations
23848      are cleared off for some cores that might have blx and simple transformations
23849      are possible.  */
23850
23851 #ifdef OBJ_ELF
23852   switch (fixp->fx_r_type)
23853     {
23854     case BFD_RELOC_ARM_PCREL_JUMP:
23855     case BFD_RELOC_ARM_PCREL_CALL:
23856     case BFD_RELOC_THUMB_PCREL_BLX:
23857       if (THUMB_IS_FUNC (fixp->fx_addsy))
23858         return 1;
23859       break;
23860
23861     case BFD_RELOC_ARM_PCREL_BLX:
23862     case BFD_RELOC_THUMB_PCREL_BRANCH25:
23863     case BFD_RELOC_THUMB_PCREL_BRANCH20:
23864     case BFD_RELOC_THUMB_PCREL_BRANCH23:
23865       if (ARM_IS_FUNC (fixp->fx_addsy))
23866         return 1;
23867       break;
23868
23869     default:
23870       break;
23871     }
23872 #endif
23873
23874   /* Resolve these relocations even if the symbol is extern or weak.
23875      Technically this is probably wrong due to symbol preemption.
23876      In practice these relocations do not have enough range to be useful
23877      at dynamic link time, and some code (e.g. in the Linux kernel)
23878      expects these references to be resolved.  */
23879   if (fixp->fx_r_type == BFD_RELOC_ARM_IMMEDIATE
23880       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM
23881       || fixp->fx_r_type == BFD_RELOC_ARM_OFFSET_IMM8
23882       || fixp->fx_r_type == BFD_RELOC_ARM_ADRL_IMMEDIATE
23883       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM
23884       || fixp->fx_r_type == BFD_RELOC_ARM_CP_OFF_IMM_S2
23885       || fixp->fx_r_type == BFD_RELOC_ARM_THUMB_OFFSET
23886       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_IMM
23887       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMMEDIATE
23888       || fixp->fx_r_type == BFD_RELOC_ARM_T32_IMM12
23889       || fixp->fx_r_type == BFD_RELOC_ARM_T32_OFFSET_IMM
23890       || fixp->fx_r_type == BFD_RELOC_ARM_T32_ADD_PC12
23891       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM
23892       || fixp->fx_r_type == BFD_RELOC_ARM_T32_CP_OFF_IMM_S2)
23893     return 0;
23894
23895   /* Always leave these relocations for the linker.  */
23896   if ((fixp->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
23897        && fixp->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
23898       || fixp->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
23899     return 1;
23900
23901   /* Always generate relocations against function symbols.  */
23902   if (fixp->fx_r_type == BFD_RELOC_32
23903       && fixp->fx_addsy
23904       && (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION))
23905     return 1;
23906
23907   return generic_force_reloc (fixp);
23908 }
23909
23910 #if defined (OBJ_ELF) || defined (OBJ_COFF)
23911 /* Relocations against function names must be left unadjusted,
23912    so that the linker can use this information to generate interworking
23913    stubs.  The MIPS version of this function
23914    also prevents relocations that are mips-16 specific, but I do not
23915    know why it does this.
23916
23917    FIXME:
23918    There is one other problem that ought to be addressed here, but
23919    which currently is not:  Taking the address of a label (rather
23920    than a function) and then later jumping to that address.  Such
23921    addresses also ought to have their bottom bit set (assuming that
23922    they reside in Thumb code), but at the moment they will not.  */
23923
23924 bfd_boolean
23925 arm_fix_adjustable (fixS * fixP)
23926 {
23927   if (fixP->fx_addsy == NULL)
23928     return 1;
23929
23930   /* Preserve relocations against symbols with function type.  */
23931   if (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_FUNCTION)
23932     return FALSE;
23933
23934   if (THUMB_IS_FUNC (fixP->fx_addsy)
23935       && fixP->fx_subsy == NULL)
23936     return FALSE;
23937
23938   /* We need the symbol name for the VTABLE entries.  */
23939   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
23940       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
23941     return FALSE;
23942
23943   /* Don't allow symbols to be discarded on GOT related relocs.  */
23944   if (fixP->fx_r_type == BFD_RELOC_ARM_PLT32
23945       || fixP->fx_r_type == BFD_RELOC_ARM_GOT32
23946       || fixP->fx_r_type == BFD_RELOC_ARM_GOTOFF
23947       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GD32
23948       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LE32
23949       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_IE32
23950       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDM32
23951       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_LDO32
23952       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_GOTDESC
23953       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_CALL
23954       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_CALL
23955       || fixP->fx_r_type == BFD_RELOC_ARM_TLS_DESCSEQ
23956       || fixP->fx_r_type == BFD_RELOC_ARM_THM_TLS_DESCSEQ
23957       || fixP->fx_r_type == BFD_RELOC_ARM_TARGET2)
23958     return FALSE;
23959
23960   /* Similarly for group relocations.  */
23961   if ((fixP->fx_r_type >= BFD_RELOC_ARM_ALU_PC_G0_NC
23962        && fixP->fx_r_type <= BFD_RELOC_ARM_LDC_SB_G2)
23963       || fixP->fx_r_type == BFD_RELOC_ARM_LDR_PC_G0)
23964     return FALSE;
23965
23966   /* MOVW/MOVT REL relocations have limited offsets, so keep the symbols.  */
23967   if (fixP->fx_r_type == BFD_RELOC_ARM_MOVW
23968       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT
23969       || fixP->fx_r_type == BFD_RELOC_ARM_MOVW_PCREL
23970       || fixP->fx_r_type == BFD_RELOC_ARM_MOVT_PCREL
23971       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW
23972       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT
23973       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVW_PCREL
23974       || fixP->fx_r_type == BFD_RELOC_ARM_THUMB_MOVT_PCREL)
23975     return FALSE;
23976
23977   /* BFD_RELOC_ARM_THUMB_ALU_ABS_Gx_NC relocations have VERY limited
23978      offsets, so keep these symbols.  */
23979   if (fixP->fx_r_type >= BFD_RELOC_ARM_THUMB_ALU_ABS_G0_NC
23980       && fixP->fx_r_type <= BFD_RELOC_ARM_THUMB_ALU_ABS_G3_NC)
23981     return FALSE;
23982
23983   return TRUE;
23984 }
23985 #endif /* defined (OBJ_ELF) || defined (OBJ_COFF) */
23986
23987 #ifdef OBJ_ELF
23988
23989 const char *
23990 elf32_arm_target_format (void)
23991 {
23992 #ifdef TE_SYMBIAN
23993   return (target_big_endian
23994           ? "elf32-bigarm-symbian"
23995           : "elf32-littlearm-symbian");
23996 #elif defined (TE_VXWORKS)
23997   return (target_big_endian
23998           ? "elf32-bigarm-vxworks"
23999           : "elf32-littlearm-vxworks");
24000 #elif defined (TE_NACL)
24001   return (target_big_endian
24002           ? "elf32-bigarm-nacl"
24003           : "elf32-littlearm-nacl");
24004 #else
24005   if (target_big_endian)
24006     return "elf32-bigarm";
24007   else
24008     return "elf32-littlearm";
24009 #endif
24010 }
24011
24012 void
24013 armelf_frob_symbol (symbolS * symp,
24014                     int *     puntp)
24015 {
24016   elf_frob_symbol (symp, puntp);
24017 }
24018 #endif
24019
24020 /* MD interface: Finalization.  */
24021
24022 void
24023 arm_cleanup (void)
24024 {
24025   literal_pool * pool;
24026
24027   /* Ensure that all the IT blocks are properly closed.  */
24028   check_it_blocks_finished ();
24029
24030   for (pool = list_of_pools; pool; pool = pool->next)
24031     {
24032       /* Put it at the end of the relevant section.  */
24033       subseg_set (pool->section, pool->sub_section);
24034 #ifdef OBJ_ELF
24035       arm_elf_change_section ();
24036 #endif
24037       s_ltorg (0);
24038     }
24039 }
24040
24041 #ifdef OBJ_ELF
24042 /* Remove any excess mapping symbols generated for alignment frags in
24043    SEC.  We may have created a mapping symbol before a zero byte
24044    alignment; remove it if there's a mapping symbol after the
24045    alignment.  */
24046 static void
24047 check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
24048                        void *dummy ATTRIBUTE_UNUSED)
24049 {
24050   segment_info_type *seginfo = seg_info (sec);
24051   fragS *fragp;
24052
24053   if (seginfo == NULL || seginfo->frchainP == NULL)
24054     return;
24055
24056   for (fragp = seginfo->frchainP->frch_root;
24057        fragp != NULL;
24058        fragp = fragp->fr_next)
24059     {
24060       symbolS *sym = fragp->tc_frag_data.last_map;
24061       fragS *next = fragp->fr_next;
24062
24063       /* Variable-sized frags have been converted to fixed size by
24064          this point.  But if this was variable-sized to start with,
24065          there will be a fixed-size frag after it.  So don't handle
24066          next == NULL.  */
24067       if (sym == NULL || next == NULL)
24068         continue;
24069
24070       if (S_GET_VALUE (sym) < next->fr_address)
24071         /* Not at the end of this frag.  */
24072         continue;
24073       know (S_GET_VALUE (sym) == next->fr_address);
24074
24075       do
24076         {
24077           if (next->tc_frag_data.first_map != NULL)
24078             {
24079               /* Next frag starts with a mapping symbol.  Discard this
24080                  one.  */
24081               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24082               break;
24083             }
24084
24085           if (next->fr_next == NULL)
24086             {
24087               /* This mapping symbol is at the end of the section.  Discard
24088                  it.  */
24089               know (next->fr_fix == 0 && next->fr_var == 0);
24090               symbol_remove (sym, &symbol_rootP, &symbol_lastP);
24091               break;
24092             }
24093
24094           /* As long as we have empty frags without any mapping symbols,
24095              keep looking.  */
24096           /* If the next frag is non-empty and does not start with a
24097              mapping symbol, then this mapping symbol is required.  */
24098           if (next->fr_address != next->fr_next->fr_address)
24099             break;
24100
24101           next = next->fr_next;
24102         }
24103       while (next != NULL);
24104     }
24105 }
24106 #endif
24107
24108 /* Adjust the symbol table.  This marks Thumb symbols as distinct from
24109    ARM ones.  */
24110
24111 void
24112 arm_adjust_symtab (void)
24113 {
24114 #ifdef OBJ_COFF
24115   symbolS * sym;
24116
24117   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24118     {
24119       if (ARM_IS_THUMB (sym))
24120         {
24121           if (THUMB_IS_FUNC (sym))
24122             {
24123               /* Mark the symbol as a Thumb function.  */
24124               if (   S_GET_STORAGE_CLASS (sym) == C_STAT
24125                   || S_GET_STORAGE_CLASS (sym) == C_LABEL)  /* This can happen!  */
24126                 S_SET_STORAGE_CLASS (sym, C_THUMBSTATFUNC);
24127
24128               else if (S_GET_STORAGE_CLASS (sym) == C_EXT)
24129                 S_SET_STORAGE_CLASS (sym, C_THUMBEXTFUNC);
24130               else
24131                 as_bad (_("%s: unexpected function type: %d"),
24132                         S_GET_NAME (sym), S_GET_STORAGE_CLASS (sym));
24133             }
24134           else switch (S_GET_STORAGE_CLASS (sym))
24135             {
24136             case C_EXT:
24137               S_SET_STORAGE_CLASS (sym, C_THUMBEXT);
24138               break;
24139             case C_STAT:
24140               S_SET_STORAGE_CLASS (sym, C_THUMBSTAT);
24141               break;
24142             case C_LABEL:
24143               S_SET_STORAGE_CLASS (sym, C_THUMBLABEL);
24144               break;
24145             default:
24146               /* Do nothing.  */
24147               break;
24148             }
24149         }
24150
24151       if (ARM_IS_INTERWORK (sym))
24152         coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_flags = 0xFF;
24153     }
24154 #endif
24155 #ifdef OBJ_ELF
24156   symbolS * sym;
24157   char      bind;
24158
24159   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
24160     {
24161       if (ARM_IS_THUMB (sym))
24162         {
24163           elf_symbol_type * elf_sym;
24164
24165           elf_sym = elf_symbol (symbol_get_bfdsym (sym));
24166           bind = ELF_ST_BIND (elf_sym->internal_elf_sym.st_info);
24167
24168           if (! bfd_is_arm_special_symbol_name (elf_sym->symbol.name,
24169                 BFD_ARM_SPECIAL_SYM_TYPE_ANY))
24170             {
24171               /* If it's a .thumb_func, declare it as so,
24172                  otherwise tag label as .code 16.  */
24173               if (THUMB_IS_FUNC (sym))
24174                 elf_sym->internal_elf_sym.st_target_internal
24175                   = ST_BRANCH_TO_THUMB;
24176               else if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
24177                 elf_sym->internal_elf_sym.st_info =
24178                   ELF_ST_INFO (bind, STT_ARM_16BIT);
24179             }
24180         }
24181     }
24182
24183   /* Remove any overlapping mapping symbols generated by alignment frags.  */
24184   bfd_map_over_sections (stdoutput, check_mapping_symbols, (char *) 0);
24185   /* Now do generic ELF adjustments.  */
24186   elf_adjust_symtab ();
24187 #endif
24188 }
24189
24190 /* MD interface: Initialization.  */
24191
24192 static void
24193 set_constant_flonums (void)
24194 {
24195   int i;
24196
24197   for (i = 0; i < NUM_FLOAT_VALS; i++)
24198     if (atof_ieee ((char *) fp_const[i], 'x', fp_values[i]) == NULL)
24199       abort ();
24200 }
24201
24202 /* Auto-select Thumb mode if it's the only available instruction set for the
24203    given architecture.  */
24204
24205 static void
24206 autoselect_thumb_from_cpu_variant (void)
24207 {
24208   if (!ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v1))
24209     opcode_select (16);
24210 }
24211
24212 void
24213 md_begin (void)
24214 {
24215   unsigned mach;
24216   unsigned int i;
24217
24218   if (   (arm_ops_hsh = hash_new ()) == NULL
24219       || (arm_cond_hsh = hash_new ()) == NULL
24220       || (arm_shift_hsh = hash_new ()) == NULL
24221       || (arm_psr_hsh = hash_new ()) == NULL
24222       || (arm_v7m_psr_hsh = hash_new ()) == NULL
24223       || (arm_reg_hsh = hash_new ()) == NULL
24224       || (arm_reloc_hsh = hash_new ()) == NULL
24225       || (arm_barrier_opt_hsh = hash_new ()) == NULL)
24226     as_fatal (_("virtual memory exhausted"));
24227
24228   for (i = 0; i < sizeof (insns) / sizeof (struct asm_opcode); i++)
24229     hash_insert (arm_ops_hsh, insns[i].template_name, (void *) (insns + i));
24230   for (i = 0; i < sizeof (conds) / sizeof (struct asm_cond); i++)
24231     hash_insert (arm_cond_hsh, conds[i].template_name, (void *) (conds + i));
24232   for (i = 0; i < sizeof (shift_names) / sizeof (struct asm_shift_name); i++)
24233     hash_insert (arm_shift_hsh, shift_names[i].name, (void *) (shift_names + i));
24234   for (i = 0; i < sizeof (psrs) / sizeof (struct asm_psr); i++)
24235     hash_insert (arm_psr_hsh, psrs[i].template_name, (void *) (psrs + i));
24236   for (i = 0; i < sizeof (v7m_psrs) / sizeof (struct asm_psr); i++)
24237     hash_insert (arm_v7m_psr_hsh, v7m_psrs[i].template_name,
24238                  (void *) (v7m_psrs + i));
24239   for (i = 0; i < sizeof (reg_names) / sizeof (struct reg_entry); i++)
24240     hash_insert (arm_reg_hsh, reg_names[i].name, (void *) (reg_names + i));
24241   for (i = 0;
24242        i < sizeof (barrier_opt_names) / sizeof (struct asm_barrier_opt);
24243        i++)
24244     hash_insert (arm_barrier_opt_hsh, barrier_opt_names[i].template_name,
24245                  (void *) (barrier_opt_names + i));
24246 #ifdef OBJ_ELF
24247   for (i = 0; i < ARRAY_SIZE (reloc_names); i++)
24248     {
24249       struct reloc_entry * entry = reloc_names + i;
24250
24251       if (arm_is_eabi() && entry->reloc == BFD_RELOC_ARM_PLT32)
24252         /* This makes encode_branch() use the EABI versions of this relocation.  */
24253         entry->reloc = BFD_RELOC_UNUSED;
24254
24255       hash_insert (arm_reloc_hsh, entry->name, (void *) entry);
24256     }
24257 #endif
24258
24259   set_constant_flonums ();
24260
24261   /* Set the cpu variant based on the command-line options.  We prefer
24262      -mcpu= over -march= if both are set (as for GCC); and we prefer
24263      -mfpu= over any other way of setting the floating point unit.
24264      Use of legacy options with new options are faulted.  */
24265   if (legacy_cpu)
24266     {
24267       if (mcpu_cpu_opt || march_cpu_opt)
24268         as_bad (_("use of old and new-style options to set CPU type"));
24269
24270       mcpu_cpu_opt = legacy_cpu;
24271     }
24272   else if (!mcpu_cpu_opt)
24273     mcpu_cpu_opt = march_cpu_opt;
24274
24275   if (legacy_fpu)
24276     {
24277       if (mfpu_opt)
24278         as_bad (_("use of old and new-style options to set FPU type"));
24279
24280       mfpu_opt = legacy_fpu;
24281     }
24282   else if (!mfpu_opt)
24283     {
24284 #if !(defined (EABI_DEFAULT) || defined (TE_LINUX) \
24285         || defined (TE_NetBSD) || defined (TE_VXWORKS))
24286       /* Some environments specify a default FPU.  If they don't, infer it
24287          from the processor.  */
24288       if (mcpu_fpu_opt)
24289         mfpu_opt = mcpu_fpu_opt;
24290       else
24291         mfpu_opt = march_fpu_opt;
24292 #else
24293       mfpu_opt = &fpu_default;
24294 #endif
24295     }
24296
24297   if (!mfpu_opt)
24298     {
24299       if (mcpu_cpu_opt != NULL)
24300         mfpu_opt = &fpu_default;
24301       else if (mcpu_fpu_opt != NULL && ARM_CPU_HAS_FEATURE (*mcpu_fpu_opt, arm_ext_v5))
24302         mfpu_opt = &fpu_arch_vfp_v2;
24303       else
24304         mfpu_opt = &fpu_arch_fpa;
24305     }
24306
24307 #ifdef CPU_DEFAULT
24308   if (!mcpu_cpu_opt)
24309     {
24310       mcpu_cpu_opt = &cpu_default;
24311       selected_cpu = cpu_default;
24312     }
24313   else if (no_cpu_selected ())
24314     selected_cpu = cpu_default;
24315 #else
24316   if (mcpu_cpu_opt)
24317     selected_cpu = *mcpu_cpu_opt;
24318   else
24319     mcpu_cpu_opt = &arm_arch_any;
24320 #endif
24321
24322   ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
24323
24324   autoselect_thumb_from_cpu_variant ();
24325
24326   arm_arch_used = thumb_arch_used = arm_arch_none;
24327
24328 #if defined OBJ_COFF || defined OBJ_ELF
24329   {
24330     unsigned int flags = 0;
24331
24332 #if defined OBJ_ELF
24333     flags = meabi_flags;
24334
24335     switch (meabi_flags)
24336       {
24337       case EF_ARM_EABI_UNKNOWN:
24338 #endif
24339         /* Set the flags in the private structure.  */
24340         if (uses_apcs_26)      flags |= F_APCS26;
24341         if (support_interwork) flags |= F_INTERWORK;
24342         if (uses_apcs_float)   flags |= F_APCS_FLOAT;
24343         if (pic_code)          flags |= F_PIC;
24344         if (!ARM_CPU_HAS_FEATURE (cpu_variant, fpu_any_hard))
24345           flags |= F_SOFT_FLOAT;
24346
24347         switch (mfloat_abi_opt)
24348           {
24349           case ARM_FLOAT_ABI_SOFT:
24350           case ARM_FLOAT_ABI_SOFTFP:
24351             flags |= F_SOFT_FLOAT;
24352             break;
24353
24354           case ARM_FLOAT_ABI_HARD:
24355             if (flags & F_SOFT_FLOAT)
24356               as_bad (_("hard-float conflicts with specified fpu"));
24357             break;
24358           }
24359
24360         /* Using pure-endian doubles (even if soft-float).      */
24361         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_endian_pure))
24362           flags |= F_VFP_FLOAT;
24363
24364 #if defined OBJ_ELF
24365         if (ARM_CPU_HAS_FEATURE (cpu_variant, fpu_arch_maverick))
24366             flags |= EF_ARM_MAVERICK_FLOAT;
24367         break;
24368
24369       case EF_ARM_EABI_VER4:
24370       case EF_ARM_EABI_VER5:
24371         /* No additional flags to set.  */
24372         break;
24373
24374       default:
24375         abort ();
24376       }
24377 #endif
24378     bfd_set_private_flags (stdoutput, flags);
24379
24380     /* We have run out flags in the COFF header to encode the
24381        status of ATPCS support, so instead we create a dummy,
24382        empty, debug section called .arm.atpcs.  */
24383     if (atpcs)
24384       {
24385         asection * sec;
24386
24387         sec = bfd_make_section (stdoutput, ".arm.atpcs");
24388
24389         if (sec != NULL)
24390           {
24391             bfd_set_section_flags
24392               (stdoutput, sec, SEC_READONLY | SEC_DEBUGGING /* | SEC_HAS_CONTENTS */);
24393             bfd_set_section_size (stdoutput, sec, 0);
24394             bfd_set_section_contents (stdoutput, sec, NULL, 0, 0);
24395           }
24396       }
24397   }
24398 #endif
24399
24400   /* Record the CPU type as well.  */
24401   if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt2))
24402     mach = bfd_mach_arm_iWMMXt2;
24403   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_iwmmxt))
24404     mach = bfd_mach_arm_iWMMXt;
24405   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_xscale))
24406     mach = bfd_mach_arm_XScale;
24407   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_cext_maverick))
24408     mach = bfd_mach_arm_ep9312;
24409   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5e))
24410     mach = bfd_mach_arm_5TE;
24411   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v5))
24412     {
24413       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24414         mach = bfd_mach_arm_5T;
24415       else
24416         mach = bfd_mach_arm_5;
24417     }
24418   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4))
24419     {
24420       if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v4t))
24421         mach = bfd_mach_arm_4T;
24422       else
24423         mach = bfd_mach_arm_4;
24424     }
24425   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3m))
24426     mach = bfd_mach_arm_3M;
24427   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v3))
24428     mach = bfd_mach_arm_3;
24429   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2s))
24430     mach = bfd_mach_arm_2a;
24431   else if (ARM_CPU_HAS_FEATURE (cpu_variant, arm_ext_v2))
24432     mach = bfd_mach_arm_2;
24433   else
24434     mach = bfd_mach_arm_unknown;
24435
24436   bfd_set_arch_mach (stdoutput, TARGET_ARCH, mach);
24437 }
24438
24439 /* Command line processing.  */
24440
24441 /* md_parse_option
24442       Invocation line includes a switch not recognized by the base assembler.
24443       See if it's a processor-specific option.
24444
24445       This routine is somewhat complicated by the need for backwards
24446       compatibility (since older releases of gcc can't be changed).
24447       The new options try to make the interface as compatible as
24448       possible with GCC.
24449
24450       New options (supported) are:
24451
24452               -mcpu=<cpu name>           Assemble for selected processor
24453               -march=<architecture name> Assemble for selected architecture
24454               -mfpu=<fpu architecture>   Assemble for selected FPU.
24455               -EB/-mbig-endian           Big-endian
24456               -EL/-mlittle-endian        Little-endian
24457               -k                         Generate PIC code
24458               -mthumb                    Start in Thumb mode
24459               -mthumb-interwork          Code supports ARM/Thumb interworking
24460
24461               -m[no-]warn-deprecated     Warn about deprecated features
24462               -m[no-]warn-syms           Warn when symbols match instructions
24463
24464       For now we will also provide support for:
24465
24466               -mapcs-32                  32-bit Program counter
24467               -mapcs-26                  26-bit Program counter
24468               -macps-float               Floats passed in FP registers
24469               -mapcs-reentrant           Reentrant code
24470               -matpcs
24471       (sometime these will probably be replaced with -mapcs=<list of options>
24472       and -matpcs=<list of options>)
24473
24474       The remaining options are only supported for back-wards compatibility.
24475       Cpu variants, the arm part is optional:
24476               -m[arm]1                Currently not supported.
24477               -m[arm]2, -m[arm]250    Arm 2 and Arm 250 processor
24478               -m[arm]3                Arm 3 processor
24479               -m[arm]6[xx],           Arm 6 processors
24480               -m[arm]7[xx][t][[d]m]   Arm 7 processors
24481               -m[arm]8[10]            Arm 8 processors
24482               -m[arm]9[20][tdmi]      Arm 9 processors
24483               -mstrongarm[110[0]]     StrongARM processors
24484               -mxscale                XScale processors
24485               -m[arm]v[2345[t[e]]]    Arm architectures
24486               -mall                   All (except the ARM1)
24487       FP variants:
24488               -mfpa10, -mfpa11        FPA10 and 11 co-processor instructions
24489               -mfpe-old               (No float load/store multiples)
24490               -mvfpxd                 VFP Single precision
24491               -mvfp                   All VFP
24492               -mno-fpu                Disable all floating point instructions
24493
24494       The following CPU names are recognized:
24495               arm1, arm2, arm250, arm3, arm6, arm600, arm610, arm620,
24496               arm7, arm7m, arm7d, arm7dm, arm7di, arm7dmi, arm70, arm700,
24497               arm700i, arm710 arm710t, arm720, arm720t, arm740t, arm710c,
24498               arm7100, arm7500, arm7500fe, arm7tdmi, arm8, arm810, arm9,
24499               arm920, arm920t, arm940t, arm946, arm966, arm9tdmi, arm9e,
24500               arm10t arm10e, arm1020t, arm1020e, arm10200e,
24501               strongarm, strongarm110, strongarm1100, strongarm1110, xscale.
24502
24503       */
24504
24505 const char * md_shortopts = "m:k";
24506
24507 #ifdef ARM_BI_ENDIAN
24508 #define OPTION_EB (OPTION_MD_BASE + 0)
24509 #define OPTION_EL (OPTION_MD_BASE + 1)
24510 #else
24511 #if TARGET_BYTES_BIG_ENDIAN
24512 #define OPTION_EB (OPTION_MD_BASE + 0)
24513 #else
24514 #define OPTION_EL (OPTION_MD_BASE + 1)
24515 #endif
24516 #endif
24517 #define OPTION_FIX_V4BX (OPTION_MD_BASE + 2)
24518
24519 struct option md_longopts[] =
24520 {
24521 #ifdef OPTION_EB
24522   {"EB", no_argument, NULL, OPTION_EB},
24523 #endif
24524 #ifdef OPTION_EL
24525   {"EL", no_argument, NULL, OPTION_EL},
24526 #endif
24527   {"fix-v4bx", no_argument, NULL, OPTION_FIX_V4BX},
24528   {NULL, no_argument, NULL, 0}
24529 };
24530
24531
24532 size_t md_longopts_size = sizeof (md_longopts);
24533
24534 struct arm_option_table
24535 {
24536   char *option;         /* Option name to match.  */
24537   char *help;           /* Help information.  */
24538   int  *var;            /* Variable to change.  */
24539   int   value;          /* What to change it to.  */
24540   char *deprecated;     /* If non-null, print this message.  */
24541 };
24542
24543 struct arm_option_table arm_opts[] =
24544 {
24545   {"k",      N_("generate PIC code"),      &pic_code,    1, NULL},
24546   {"mthumb", N_("assemble Thumb code"),    &thumb_mode,  1, NULL},
24547   {"mthumb-interwork", N_("support ARM/Thumb interworking"),
24548    &support_interwork, 1, NULL},
24549   {"mapcs-32", N_("code uses 32-bit program counter"), &uses_apcs_26, 0, NULL},
24550   {"mapcs-26", N_("code uses 26-bit program counter"), &uses_apcs_26, 1, NULL},
24551   {"mapcs-float", N_("floating point args are in fp regs"), &uses_apcs_float,
24552    1, NULL},
24553   {"mapcs-reentrant", N_("re-entrant code"), &pic_code, 1, NULL},
24554   {"matpcs", N_("code is ATPCS conformant"), &atpcs, 1, NULL},
24555   {"mbig-endian", N_("assemble for big-endian"), &target_big_endian, 1, NULL},
24556   {"mlittle-endian", N_("assemble for little-endian"), &target_big_endian, 0,
24557    NULL},
24558
24559   /* These are recognized by the assembler, but have no affect on code.  */
24560   {"mapcs-frame", N_("use frame pointer"), NULL, 0, NULL},
24561   {"mapcs-stack-check", N_("use stack size checking"), NULL, 0, NULL},
24562
24563   {"mwarn-deprecated", NULL, &warn_on_deprecated, 1, NULL},
24564   {"mno-warn-deprecated", N_("do not warn on use of deprecated feature"),
24565    &warn_on_deprecated, 0, NULL},
24566   {"mwarn-syms", N_("warn about symbols that match instruction names [default]"), (int *) (& flag_warn_syms), TRUE, NULL},
24567   {"mno-warn-syms", N_("disable warnings about symobls that match instructions"), (int *) (& flag_warn_syms), FALSE, NULL},
24568   {NULL, NULL, NULL, 0, NULL}
24569 };
24570
24571 struct arm_legacy_option_table
24572 {
24573   char *option;                         /* Option name to match.  */
24574   const arm_feature_set **var;          /* Variable to change.  */
24575   const arm_feature_set value;          /* What to change it to.  */
24576   char *deprecated;                     /* If non-null, print this message.  */
24577 };
24578
24579 const struct arm_legacy_option_table arm_legacy_opts[] =
24580 {
24581   /* DON'T add any new processors to this list -- we want the whole list
24582      to go away...  Add them to the processors table instead.  */
24583   {"marm1",      &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
24584   {"m1",         &legacy_cpu, ARM_ARCH_V1,  N_("use -mcpu=arm1")},
24585   {"marm2",      &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
24586   {"m2",         &legacy_cpu, ARM_ARCH_V2,  N_("use -mcpu=arm2")},
24587   {"marm250",    &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
24588   {"m250",       &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm250")},
24589   {"marm3",      &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
24590   {"m3",         &legacy_cpu, ARM_ARCH_V2S, N_("use -mcpu=arm3")},
24591   {"marm6",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
24592   {"m6",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm6")},
24593   {"marm600",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
24594   {"m600",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm600")},
24595   {"marm610",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
24596   {"m610",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm610")},
24597   {"marm620",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
24598   {"m620",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm620")},
24599   {"marm7",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
24600   {"m7",         &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7")},
24601   {"marm70",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
24602   {"m70",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm70")},
24603   {"marm700",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
24604   {"m700",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700")},
24605   {"marm700i",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
24606   {"m700i",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm700i")},
24607   {"marm710",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
24608   {"m710",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710")},
24609   {"marm710c",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
24610   {"m710c",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm710c")},
24611   {"marm720",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
24612   {"m720",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm720")},
24613   {"marm7d",     &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
24614   {"m7d",        &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7d")},
24615   {"marm7di",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
24616   {"m7di",       &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7di")},
24617   {"marm7m",     &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
24618   {"m7m",        &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7m")},
24619   {"marm7dm",    &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
24620   {"m7dm",       &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dm")},
24621   {"marm7dmi",   &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
24622   {"m7dmi",      &legacy_cpu, ARM_ARCH_V3M, N_("use -mcpu=arm7dmi")},
24623   {"marm7100",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
24624   {"m7100",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7100")},
24625   {"marm7500",   &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
24626   {"m7500",      &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500")},
24627   {"marm7500fe", &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
24628   {"m7500fe",    &legacy_cpu, ARM_ARCH_V3,  N_("use -mcpu=arm7500fe")},
24629   {"marm7t",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24630   {"m7t",        &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24631   {"marm7tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24632   {"m7tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm7tdmi")},
24633   {"marm710t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
24634   {"m710t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm710t")},
24635   {"marm720t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
24636   {"m720t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm720t")},
24637   {"marm740t",   &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
24638   {"m740t",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm740t")},
24639   {"marm8",      &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
24640   {"m8",         &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm8")},
24641   {"marm810",    &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
24642   {"m810",       &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=arm810")},
24643   {"marm9",      &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
24644   {"m9",         &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9")},
24645   {"marm9tdmi",  &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
24646   {"m9tdmi",     &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm9tdmi")},
24647   {"marm920",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
24648   {"m920",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm920")},
24649   {"marm940",    &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
24650   {"m940",       &legacy_cpu, ARM_ARCH_V4T, N_("use -mcpu=arm940")},
24651   {"mstrongarm", &legacy_cpu, ARM_ARCH_V4,  N_("use -mcpu=strongarm")},
24652   {"mstrongarm110", &legacy_cpu, ARM_ARCH_V4,
24653    N_("use -mcpu=strongarm110")},
24654   {"mstrongarm1100", &legacy_cpu, ARM_ARCH_V4,
24655    N_("use -mcpu=strongarm1100")},
24656   {"mstrongarm1110", &legacy_cpu, ARM_ARCH_V4,
24657    N_("use -mcpu=strongarm1110")},
24658   {"mxscale",    &legacy_cpu, ARM_ARCH_XSCALE, N_("use -mcpu=xscale")},
24659   {"miwmmxt",    &legacy_cpu, ARM_ARCH_IWMMXT, N_("use -mcpu=iwmmxt")},
24660   {"mall",       &legacy_cpu, ARM_ANY,         N_("use -mcpu=all")},
24661
24662   /* Architecture variants -- don't add any more to this list either.  */
24663   {"mv2",        &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
24664   {"marmv2",     &legacy_cpu, ARM_ARCH_V2,  N_("use -march=armv2")},
24665   {"mv2a",       &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
24666   {"marmv2a",    &legacy_cpu, ARM_ARCH_V2S, N_("use -march=armv2a")},
24667   {"mv3",        &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
24668   {"marmv3",     &legacy_cpu, ARM_ARCH_V3,  N_("use -march=armv3")},
24669   {"mv3m",       &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
24670   {"marmv3m",    &legacy_cpu, ARM_ARCH_V3M, N_("use -march=armv3m")},
24671   {"mv4",        &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
24672   {"marmv4",     &legacy_cpu, ARM_ARCH_V4,  N_("use -march=armv4")},
24673   {"mv4t",       &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
24674   {"marmv4t",    &legacy_cpu, ARM_ARCH_V4T, N_("use -march=armv4t")},
24675   {"mv5",        &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
24676   {"marmv5",     &legacy_cpu, ARM_ARCH_V5,  N_("use -march=armv5")},
24677   {"mv5t",       &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
24678   {"marmv5t",    &legacy_cpu, ARM_ARCH_V5T, N_("use -march=armv5t")},
24679   {"mv5e",       &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
24680   {"marmv5e",    &legacy_cpu, ARM_ARCH_V5TE, N_("use -march=armv5te")},
24681
24682   /* Floating point variants -- don't add any more to this list either.  */
24683   {"mfpe-old", &legacy_fpu, FPU_ARCH_FPE, N_("use -mfpu=fpe")},
24684   {"mfpa10",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa10")},
24685   {"mfpa11",   &legacy_fpu, FPU_ARCH_FPA, N_("use -mfpu=fpa11")},
24686   {"mno-fpu",  &legacy_fpu, ARM_ARCH_NONE,
24687    N_("use either -mfpu=softfpa or -mfpu=softvfp")},
24688
24689   {NULL, NULL, ARM_ARCH_NONE, NULL}
24690 };
24691
24692 struct arm_cpu_option_table
24693 {
24694   char *name;
24695   size_t name_len;
24696   const arm_feature_set value;
24697   /* For some CPUs we assume an FPU unless the user explicitly sets
24698      -mfpu=...  */
24699   const arm_feature_set default_fpu;
24700   /* The canonical name of the CPU, or NULL to use NAME converted to upper
24701      case.  */
24702   const char *canonical_name;
24703 };
24704
24705 /* This list should, at a minimum, contain all the cpu names
24706    recognized by GCC.  */
24707 #define ARM_CPU_OPT(N, V, DF, CN) { N, sizeof (N) - 1, V, DF, CN }
24708 static const struct arm_cpu_option_table arm_cpus[] =
24709 {
24710   ARM_CPU_OPT ("all",           ARM_ANY,         FPU_ARCH_FPA,    NULL),
24711   ARM_CPU_OPT ("arm1",          ARM_ARCH_V1,     FPU_ARCH_FPA,    NULL),
24712   ARM_CPU_OPT ("arm2",          ARM_ARCH_V2,     FPU_ARCH_FPA,    NULL),
24713   ARM_CPU_OPT ("arm250",        ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
24714   ARM_CPU_OPT ("arm3",          ARM_ARCH_V2S,    FPU_ARCH_FPA,    NULL),
24715   ARM_CPU_OPT ("arm6",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24716   ARM_CPU_OPT ("arm60",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24717   ARM_CPU_OPT ("arm600",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24718   ARM_CPU_OPT ("arm610",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24719   ARM_CPU_OPT ("arm620",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24720   ARM_CPU_OPT ("arm7",          ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24721   ARM_CPU_OPT ("arm7m",         ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24722   ARM_CPU_OPT ("arm7d",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24723   ARM_CPU_OPT ("arm7dm",        ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24724   ARM_CPU_OPT ("arm7di",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24725   ARM_CPU_OPT ("arm7dmi",       ARM_ARCH_V3M,    FPU_ARCH_FPA,    NULL),
24726   ARM_CPU_OPT ("arm70",         ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24727   ARM_CPU_OPT ("arm700",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24728   ARM_CPU_OPT ("arm700i",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24729   ARM_CPU_OPT ("arm710",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24730   ARM_CPU_OPT ("arm710t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24731   ARM_CPU_OPT ("arm720",        ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24732   ARM_CPU_OPT ("arm720t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24733   ARM_CPU_OPT ("arm740t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24734   ARM_CPU_OPT ("arm710c",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24735   ARM_CPU_OPT ("arm7100",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24736   ARM_CPU_OPT ("arm7500",       ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24737   ARM_CPU_OPT ("arm7500fe",     ARM_ARCH_V3,     FPU_ARCH_FPA,    NULL),
24738   ARM_CPU_OPT ("arm7t",         ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24739   ARM_CPU_OPT ("arm7tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24740   ARM_CPU_OPT ("arm7tdmi-s",    ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24741   ARM_CPU_OPT ("arm8",          ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24742   ARM_CPU_OPT ("arm810",        ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24743   ARM_CPU_OPT ("strongarm",     ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24744   ARM_CPU_OPT ("strongarm1",    ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24745   ARM_CPU_OPT ("strongarm110",  ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24746   ARM_CPU_OPT ("strongarm1100", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24747   ARM_CPU_OPT ("strongarm1110", ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24748   ARM_CPU_OPT ("arm9",          ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24749   ARM_CPU_OPT ("arm920",        ARM_ARCH_V4T,    FPU_ARCH_FPA,    "ARM920T"),
24750   ARM_CPU_OPT ("arm920t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24751   ARM_CPU_OPT ("arm922t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24752   ARM_CPU_OPT ("arm940t",       ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24753   ARM_CPU_OPT ("arm9tdmi",      ARM_ARCH_V4T,    FPU_ARCH_FPA,    NULL),
24754   ARM_CPU_OPT ("fa526",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24755   ARM_CPU_OPT ("fa626",         ARM_ARCH_V4,     FPU_ARCH_FPA,    NULL),
24756   /* For V5 or later processors we default to using VFP; but the user
24757      should really set the FPU type explicitly.  */
24758   ARM_CPU_OPT ("arm9e-r0",      ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24759   ARM_CPU_OPT ("arm9e",         ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24760   ARM_CPU_OPT ("arm926ej",      ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
24761   ARM_CPU_OPT ("arm926ejs",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, "ARM926EJ-S"),
24762   ARM_CPU_OPT ("arm926ej-s",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
24763   ARM_CPU_OPT ("arm946e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24764   ARM_CPU_OPT ("arm946e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM946E-S"),
24765   ARM_CPU_OPT ("arm946e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24766   ARM_CPU_OPT ("arm966e-r0",    ARM_ARCH_V5TExP, FPU_ARCH_VFP_V2, NULL),
24767   ARM_CPU_OPT ("arm966e",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM966E-S"),
24768   ARM_CPU_OPT ("arm966e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24769   ARM_CPU_OPT ("arm968e-s",     ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24770   ARM_CPU_OPT ("arm10t",        ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24771   ARM_CPU_OPT ("arm10tdmi",     ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24772   ARM_CPU_OPT ("arm10e",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24773   ARM_CPU_OPT ("arm1020",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, "ARM1020E"),
24774   ARM_CPU_OPT ("arm1020t",      ARM_ARCH_V5T,    FPU_ARCH_VFP_V1, NULL),
24775   ARM_CPU_OPT ("arm1020e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24776   ARM_CPU_OPT ("arm1022e",      ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24777   ARM_CPU_OPT ("arm1026ejs",    ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2,
24778                                                                  "ARM1026EJ-S"),
24779   ARM_CPU_OPT ("arm1026ej-s",   ARM_ARCH_V5TEJ,  FPU_ARCH_VFP_V2, NULL),
24780   ARM_CPU_OPT ("fa606te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24781   ARM_CPU_OPT ("fa616te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24782   ARM_CPU_OPT ("fa626te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24783   ARM_CPU_OPT ("fmp626",        ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24784   ARM_CPU_OPT ("fa726te",       ARM_ARCH_V5TE,   FPU_ARCH_VFP_V2, NULL),
24785   ARM_CPU_OPT ("arm1136js",     ARM_ARCH_V6,     FPU_NONE,        "ARM1136J-S"),
24786   ARM_CPU_OPT ("arm1136j-s",    ARM_ARCH_V6,     FPU_NONE,        NULL),
24787   ARM_CPU_OPT ("arm1136jfs",    ARM_ARCH_V6,     FPU_ARCH_VFP_V2,
24788                                                                  "ARM1136JF-S"),
24789   ARM_CPU_OPT ("arm1136jf-s",   ARM_ARCH_V6,     FPU_ARCH_VFP_V2, NULL),
24790   ARM_CPU_OPT ("mpcore",        ARM_ARCH_V6K,    FPU_ARCH_VFP_V2, "MPCore"),
24791   ARM_CPU_OPT ("mpcorenovfp",   ARM_ARCH_V6K,    FPU_NONE,        "MPCore"),
24792   ARM_CPU_OPT ("arm1156t2-s",   ARM_ARCH_V6T2,   FPU_NONE,        NULL),
24793   ARM_CPU_OPT ("arm1156t2f-s",  ARM_ARCH_V6T2,   FPU_ARCH_VFP_V2, NULL),
24794   ARM_CPU_OPT ("arm1176jz-s",   ARM_ARCH_V6KZ,   FPU_NONE,        NULL),
24795   ARM_CPU_OPT ("arm1176jzf-s",  ARM_ARCH_V6KZ,   FPU_ARCH_VFP_V2, NULL),
24796   ARM_CPU_OPT ("cortex-a5",     ARM_ARCH_V7A_MP_SEC,
24797                                                  FPU_NONE,        "Cortex-A5"),
24798   ARM_CPU_OPT ("cortex-a7",     ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24799                                                                   "Cortex-A7"),
24800   ARM_CPU_OPT ("cortex-a8",     ARM_ARCH_V7A_SEC,
24801                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
24802                                                         | FPU_NEON_EXT_V1),
24803                                                                   "Cortex-A8"),
24804   ARM_CPU_OPT ("cortex-a9",     ARM_ARCH_V7A_MP_SEC,
24805                                                  ARM_FEATURE_COPROC (FPU_VFP_V3
24806                                                         | FPU_NEON_EXT_V1),
24807                                                                   "Cortex-A9"),
24808   ARM_CPU_OPT ("cortex-a12",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24809                                                                   "Cortex-A12"),
24810   ARM_CPU_OPT ("cortex-a15",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24811                                                                   "Cortex-A15"),
24812   ARM_CPU_OPT ("cortex-a17",    ARM_ARCH_V7VE,   FPU_ARCH_NEON_VFP_V4,
24813                                                                   "Cortex-A17"),
24814   ARM_CPU_OPT ("cortex-a35",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24815                                                                   "Cortex-A35"),
24816   ARM_CPU_OPT ("cortex-a53",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24817                                                                   "Cortex-A53"),
24818   ARM_CPU_OPT ("cortex-a57",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24819                                                                   "Cortex-A57"),
24820   ARM_CPU_OPT ("cortex-a72",    ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24821                                                                   "Cortex-A72"),
24822   ARM_CPU_OPT ("cortex-r4",     ARM_ARCH_V7R,    FPU_NONE,        "Cortex-R4"),
24823   ARM_CPU_OPT ("cortex-r4f",    ARM_ARCH_V7R,    FPU_ARCH_VFP_V3D16,
24824                                                                   "Cortex-R4F"),
24825   ARM_CPU_OPT ("cortex-r5",     ARM_ARCH_V7R_IDIV,
24826                                                  FPU_NONE,        "Cortex-R5"),
24827   ARM_CPU_OPT ("cortex-r7",     ARM_ARCH_V7R_IDIV,
24828                                                  FPU_ARCH_VFP_V3D16,
24829                                                                   "Cortex-R7"),
24830   ARM_CPU_OPT ("cortex-m7",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M7"),
24831   ARM_CPU_OPT ("cortex-m4",     ARM_ARCH_V7EM,   FPU_NONE,        "Cortex-M4"),
24832   ARM_CPU_OPT ("cortex-m3",     ARM_ARCH_V7M,    FPU_NONE,        "Cortex-M3"),
24833   ARM_CPU_OPT ("cortex-m1",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M1"),
24834   ARM_CPU_OPT ("cortex-m0",     ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0"),
24835   ARM_CPU_OPT ("cortex-m0plus", ARM_ARCH_V6SM,   FPU_NONE,        "Cortex-M0+"),
24836   ARM_CPU_OPT ("exynos-m1",     ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24837                                                                   "Samsung " \
24838                                                                   "Exynos M1"),
24839   ARM_CPU_OPT ("qdf24xx",       ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24840                                                                   "Qualcomm "
24841                                                                   "QDF24XX"),
24842
24843   /* ??? XSCALE is really an architecture.  */
24844   ARM_CPU_OPT ("xscale",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
24845   /* ??? iwmmxt is not a processor.  */
24846   ARM_CPU_OPT ("iwmmxt",        ARM_ARCH_IWMMXT, FPU_ARCH_VFP_V2, NULL),
24847   ARM_CPU_OPT ("iwmmxt2",       ARM_ARCH_IWMMXT2,FPU_ARCH_VFP_V2, NULL),
24848   ARM_CPU_OPT ("i80200",        ARM_ARCH_XSCALE, FPU_ARCH_VFP_V2, NULL),
24849   /* Maverick */
24850   ARM_CPU_OPT ("ep9312",        ARM_FEATURE_LOW (ARM_AEXT_V4T, ARM_CEXT_MAVERICK),
24851                                                  FPU_ARCH_MAVERICK, "ARM920T"),
24852   /* Marvell processors.  */
24853   ARM_CPU_OPT ("marvell-pj4",   ARM_FEATURE_CORE_LOW (ARM_AEXT_V7A | ARM_EXT_MP
24854                                                       | ARM_EXT_SEC),
24855                                                 FPU_ARCH_VFP_V3D16, NULL),
24856   ARM_CPU_OPT ("marvell-whitney", ARM_FEATURE_CORE_LOW (ARM_AEXT_V7A | ARM_EXT_MP
24857                                                         | ARM_EXT_SEC),
24858                                                FPU_ARCH_NEON_VFP_V4, NULL),
24859   /* APM X-Gene family.  */
24860   ARM_CPU_OPT ("xgene1",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24861                                                                   "APM X-Gene 1"),
24862   ARM_CPU_OPT ("xgene2",        ARM_ARCH_V8A,    FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24863                                                                   "APM X-Gene 2"),
24864
24865   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, NULL }
24866 };
24867 #undef ARM_CPU_OPT
24868
24869 struct arm_arch_option_table
24870 {
24871   char *name;
24872   size_t name_len;
24873   const arm_feature_set value;
24874   const arm_feature_set default_fpu;
24875 };
24876
24877 /* This list should, at a minimum, contain all the architecture names
24878    recognized by GCC.  */
24879 #define ARM_ARCH_OPT(N, V, DF) { N, sizeof (N) - 1, V, DF }
24880 static const struct arm_arch_option_table arm_archs[] =
24881 {
24882   ARM_ARCH_OPT ("all",          ARM_ANY,         FPU_ARCH_FPA),
24883   ARM_ARCH_OPT ("armv1",        ARM_ARCH_V1,     FPU_ARCH_FPA),
24884   ARM_ARCH_OPT ("armv2",        ARM_ARCH_V2,     FPU_ARCH_FPA),
24885   ARM_ARCH_OPT ("armv2a",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
24886   ARM_ARCH_OPT ("armv2s",       ARM_ARCH_V2S,    FPU_ARCH_FPA),
24887   ARM_ARCH_OPT ("armv3",        ARM_ARCH_V3,     FPU_ARCH_FPA),
24888   ARM_ARCH_OPT ("armv3m",       ARM_ARCH_V3M,    FPU_ARCH_FPA),
24889   ARM_ARCH_OPT ("armv4",        ARM_ARCH_V4,     FPU_ARCH_FPA),
24890   ARM_ARCH_OPT ("armv4xm",      ARM_ARCH_V4xM,   FPU_ARCH_FPA),
24891   ARM_ARCH_OPT ("armv4t",       ARM_ARCH_V4T,    FPU_ARCH_FPA),
24892   ARM_ARCH_OPT ("armv4txm",     ARM_ARCH_V4TxM,  FPU_ARCH_FPA),
24893   ARM_ARCH_OPT ("armv5",        ARM_ARCH_V5,     FPU_ARCH_VFP),
24894   ARM_ARCH_OPT ("armv5t",       ARM_ARCH_V5T,    FPU_ARCH_VFP),
24895   ARM_ARCH_OPT ("armv5txm",     ARM_ARCH_V5TxM,  FPU_ARCH_VFP),
24896   ARM_ARCH_OPT ("armv5te",      ARM_ARCH_V5TE,   FPU_ARCH_VFP),
24897   ARM_ARCH_OPT ("armv5texp",    ARM_ARCH_V5TExP, FPU_ARCH_VFP),
24898   ARM_ARCH_OPT ("armv5tej",     ARM_ARCH_V5TEJ,  FPU_ARCH_VFP),
24899   ARM_ARCH_OPT ("armv6",        ARM_ARCH_V6,     FPU_ARCH_VFP),
24900   ARM_ARCH_OPT ("armv6j",       ARM_ARCH_V6,     FPU_ARCH_VFP),
24901   ARM_ARCH_OPT ("armv6k",       ARM_ARCH_V6K,    FPU_ARCH_VFP),
24902   ARM_ARCH_OPT ("armv6z",       ARM_ARCH_V6Z,    FPU_ARCH_VFP),
24903   /* The official spelling of this variant is ARMv6KZ, the name "armv6zk" is
24904      kept to preserve existing behaviour.  */
24905   ARM_ARCH_OPT ("armv6kz",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
24906   ARM_ARCH_OPT ("armv6zk",      ARM_ARCH_V6KZ,   FPU_ARCH_VFP),
24907   ARM_ARCH_OPT ("armv6t2",      ARM_ARCH_V6T2,   FPU_ARCH_VFP),
24908   ARM_ARCH_OPT ("armv6kt2",     ARM_ARCH_V6KT2,  FPU_ARCH_VFP),
24909   ARM_ARCH_OPT ("armv6zt2",     ARM_ARCH_V6ZT2,  FPU_ARCH_VFP),
24910   /* The official spelling of this variant is ARMv6KZ, the name "armv6zkt2" is
24911      kept to preserve existing behaviour.  */
24912   ARM_ARCH_OPT ("armv6kzt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
24913   ARM_ARCH_OPT ("armv6zkt2",    ARM_ARCH_V6KZT2, FPU_ARCH_VFP),
24914   ARM_ARCH_OPT ("armv6-m",      ARM_ARCH_V6M,    FPU_ARCH_VFP),
24915   ARM_ARCH_OPT ("armv6s-m",     ARM_ARCH_V6SM,   FPU_ARCH_VFP),
24916   ARM_ARCH_OPT ("armv7",        ARM_ARCH_V7,     FPU_ARCH_VFP),
24917   /* The official spelling of the ARMv7 profile variants is the dashed form.
24918      Accept the non-dashed form for compatibility with old toolchains.  */
24919   ARM_ARCH_OPT ("armv7a",       ARM_ARCH_V7A,    FPU_ARCH_VFP),
24920   ARM_ARCH_OPT ("armv7ve",      ARM_ARCH_V7VE,   FPU_ARCH_VFP),
24921   ARM_ARCH_OPT ("armv7r",       ARM_ARCH_V7R,    FPU_ARCH_VFP),
24922   ARM_ARCH_OPT ("armv7m",       ARM_ARCH_V7M,    FPU_ARCH_VFP),
24923   ARM_ARCH_OPT ("armv7-a",      ARM_ARCH_V7A,    FPU_ARCH_VFP),
24924   ARM_ARCH_OPT ("armv7-r",      ARM_ARCH_V7R,    FPU_ARCH_VFP),
24925   ARM_ARCH_OPT ("armv7-m",      ARM_ARCH_V7M,    FPU_ARCH_VFP),
24926   ARM_ARCH_OPT ("armv7e-m",     ARM_ARCH_V7EM,   FPU_ARCH_VFP),
24927   ARM_ARCH_OPT ("armv8-a",      ARM_ARCH_V8A,    FPU_ARCH_VFP),
24928   ARM_ARCH_OPT ("armv8.1-a",    ARM_ARCH_V8_1A,  FPU_ARCH_VFP),
24929   ARM_ARCH_OPT ("armv8.2-a",    ARM_ARCH_V8_2A,  FPU_ARCH_VFP),
24930   ARM_ARCH_OPT ("xscale",       ARM_ARCH_XSCALE, FPU_ARCH_VFP),
24931   ARM_ARCH_OPT ("iwmmxt",       ARM_ARCH_IWMMXT, FPU_ARCH_VFP),
24932   ARM_ARCH_OPT ("iwmmxt2",      ARM_ARCH_IWMMXT2,FPU_ARCH_VFP),
24933   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE }
24934 };
24935 #undef ARM_ARCH_OPT
24936
24937 /* ISA extensions in the co-processor and main instruction set space.  */
24938 struct arm_option_extension_value_table
24939 {
24940   char *name;
24941   size_t name_len;
24942   const arm_feature_set merge_value;
24943   const arm_feature_set clear_value;
24944   const arm_feature_set allowed_archs;
24945 };
24946
24947 /* The following table must be in alphabetical order with a NULL last entry.
24948    */
24949 #define ARM_EXT_OPT(N, M, C, AA) { N, sizeof (N) - 1, M, C, AA }
24950 static const struct arm_option_extension_value_table arm_extensions[] =
24951 {
24952   ARM_EXT_OPT ("crc",  ARCH_CRC_ARMV8, ARM_FEATURE_COPROC (CRC_EXT_ARMV8),
24953                          ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24954   ARM_EXT_OPT ("crypto", FPU_ARCH_CRYPTO_NEON_VFP_ARMV8,
24955                          ARM_FEATURE_COPROC (FPU_CRYPTO_ARMV8),
24956                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24957   ARM_EXT_OPT ("fp",     FPU_ARCH_VFP_ARMV8, ARM_FEATURE_COPROC (FPU_VFP_ARMV8),
24958                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24959   ARM_EXT_OPT ("idiv",  ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
24960                         ARM_FEATURE_CORE_LOW (ARM_EXT_ADIV | ARM_EXT_DIV),
24961                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A | ARM_EXT_V7R)),
24962   ARM_EXT_OPT ("iwmmxt",ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT),
24963                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT), ARM_ANY),
24964   ARM_EXT_OPT ("iwmmxt2", ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2),
24965                         ARM_FEATURE_COPROC (ARM_CEXT_IWMMXT2), ARM_ANY),
24966   ARM_EXT_OPT ("maverick", ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK),
24967                         ARM_FEATURE_COPROC (ARM_CEXT_MAVERICK), ARM_ANY),
24968   ARM_EXT_OPT ("mp",    ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
24969                         ARM_FEATURE_CORE_LOW (ARM_EXT_MP),
24970                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A | ARM_EXT_V7R)),
24971   ARM_EXT_OPT ("simd",   FPU_ARCH_NEON_VFP_ARMV8,
24972                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8),
24973                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24974   ARM_EXT_OPT ("os",    ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
24975                         ARM_FEATURE_CORE_LOW (ARM_EXT_OS),
24976                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6M)),
24977   ARM_EXT_OPT ("pan",   ARM_FEATURE_CORE_HIGH (ARM_EXT2_PAN),
24978                         ARM_FEATURE (ARM_EXT_V8, ARM_EXT2_PAN, 0),
24979                         ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24980   ARM_EXT_OPT ("sec",   ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
24981                         ARM_FEATURE_CORE_LOW (ARM_EXT_SEC),
24982                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V6K | ARM_EXT_V7A)),
24983   ARM_EXT_OPT ("virt",  ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT | ARM_EXT_ADIV
24984                                      | ARM_EXT_DIV),
24985                         ARM_FEATURE_CORE_LOW (ARM_EXT_VIRT),
24986                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V7A)),
24987   ARM_EXT_OPT ("rdma",  FPU_ARCH_NEON_VFP_ARMV8,
24988                         ARM_FEATURE_COPROC (FPU_NEON_ARMV8 | FPU_NEON_EXT_RDMA),
24989                                    ARM_FEATURE_CORE_LOW (ARM_EXT_V8)),
24990   ARM_EXT_OPT ("xscale",ARM_FEATURE_COPROC (ARM_CEXT_XSCALE),
24991                         ARM_FEATURE_COPROC (ARM_CEXT_XSCALE), ARM_ANY),
24992   { NULL, 0, ARM_ARCH_NONE, ARM_ARCH_NONE, ARM_ARCH_NONE }
24993 };
24994 #undef ARM_EXT_OPT
24995
24996 /* ISA floating-point and Advanced SIMD extensions.  */
24997 struct arm_option_fpu_value_table
24998 {
24999   char *name;
25000   const arm_feature_set value;
25001 };
25002
25003 /* This list should, at a minimum, contain all the fpu names
25004    recognized by GCC.  */
25005 static const struct arm_option_fpu_value_table arm_fpus[] =
25006 {
25007   {"softfpa",           FPU_NONE},
25008   {"fpe",               FPU_ARCH_FPE},
25009   {"fpe2",              FPU_ARCH_FPE},
25010   {"fpe3",              FPU_ARCH_FPA},  /* Third release supports LFM/SFM.  */
25011   {"fpa",               FPU_ARCH_FPA},
25012   {"fpa10",             FPU_ARCH_FPA},
25013   {"fpa11",             FPU_ARCH_FPA},
25014   {"arm7500fe",         FPU_ARCH_FPA},
25015   {"softvfp",           FPU_ARCH_VFP},
25016   {"softvfp+vfp",       FPU_ARCH_VFP_V2},
25017   {"vfp",               FPU_ARCH_VFP_V2},
25018   {"vfp9",              FPU_ARCH_VFP_V2},
25019   {"vfp3",              FPU_ARCH_VFP_V3}, /* For backwards compatbility.  */
25020   {"vfp10",             FPU_ARCH_VFP_V2},
25021   {"vfp10-r0",          FPU_ARCH_VFP_V1},
25022   {"vfpxd",             FPU_ARCH_VFP_V1xD},
25023   {"vfpv2",             FPU_ARCH_VFP_V2},
25024   {"vfpv3",             FPU_ARCH_VFP_V3},
25025   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
25026   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
25027   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
25028   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
25029   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
25030   {"arm1020t",          FPU_ARCH_VFP_V1},
25031   {"arm1020e",          FPU_ARCH_VFP_V2},
25032   {"arm1136jfs",        FPU_ARCH_VFP_V2},
25033   {"arm1136jf-s",       FPU_ARCH_VFP_V2},
25034   {"maverick",          FPU_ARCH_MAVERICK},
25035   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
25036   {"neon-fp16",         FPU_ARCH_NEON_FP16},
25037   {"vfpv4",             FPU_ARCH_VFP_V4},
25038   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
25039   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
25040   {"fpv5-d16",          FPU_ARCH_VFP_V5D16},
25041   {"fpv5-sp-d16",       FPU_ARCH_VFP_V5_SP_D16},
25042   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
25043   {"fp-armv8",          FPU_ARCH_VFP_ARMV8},
25044   {"neon-fp-armv8",     FPU_ARCH_NEON_VFP_ARMV8},
25045   {"crypto-neon-fp-armv8",
25046                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8},
25047   {"neon-fp-armv8.1",   FPU_ARCH_NEON_VFP_ARMV8_1},
25048   {"crypto-neon-fp-armv8.1",
25049                         FPU_ARCH_CRYPTO_NEON_VFP_ARMV8_1},
25050   {NULL,                ARM_ARCH_NONE}
25051 };
25052
25053 struct arm_option_value_table
25054 {
25055   char *name;
25056   long value;
25057 };
25058
25059 static const struct arm_option_value_table arm_float_abis[] =
25060 {
25061   {"hard",      ARM_FLOAT_ABI_HARD},
25062   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
25063   {"soft",      ARM_FLOAT_ABI_SOFT},
25064   {NULL,        0}
25065 };
25066
25067 #ifdef OBJ_ELF
25068 /* We only know how to output GNU and ver 4/5 (AAELF) formats.  */
25069 static const struct arm_option_value_table arm_eabis[] =
25070 {
25071   {"gnu",       EF_ARM_EABI_UNKNOWN},
25072   {"4",         EF_ARM_EABI_VER4},
25073   {"5",         EF_ARM_EABI_VER5},
25074   {NULL,        0}
25075 };
25076 #endif
25077
25078 struct arm_long_option_table
25079 {
25080   char * option;                /* Substring to match.  */
25081   char * help;                  /* Help information.  */
25082   int (* func) (char * subopt); /* Function to decode sub-option.  */
25083   char * deprecated;            /* If non-null, print this message.  */
25084 };
25085
25086 static bfd_boolean
25087 arm_parse_extension (char *str, const arm_feature_set **opt_p)
25088 {
25089   arm_feature_set *ext_set = (arm_feature_set *)
25090       xmalloc (sizeof (arm_feature_set));
25091
25092   /* We insist on extensions being specified in alphabetical order, and with
25093      extensions being added before being removed.  We achieve this by having
25094      the global ARM_EXTENSIONS table in alphabetical order, and using the
25095      ADDING_VALUE variable to indicate whether we are adding an extension (1)
25096      or removing it (0) and only allowing it to change in the order
25097      -1 -> 1 -> 0.  */
25098   const struct arm_option_extension_value_table * opt = NULL;
25099   int adding_value = -1;
25100
25101   /* Copy the feature set, so that we can modify it.  */
25102   *ext_set = **opt_p;
25103   *opt_p = ext_set;
25104
25105   while (str != NULL && *str != 0)
25106     {
25107       char *ext;
25108       size_t len;
25109
25110       if (*str != '+')
25111         {
25112           as_bad (_("invalid architectural extension"));
25113           return FALSE;
25114         }
25115
25116       str++;
25117       ext = strchr (str, '+');
25118
25119       if (ext != NULL)
25120         len = ext - str;
25121       else
25122         len = strlen (str);
25123
25124       if (len >= 2 && strncmp (str, "no", 2) == 0)
25125         {
25126           if (adding_value != 0)
25127             {
25128               adding_value = 0;
25129               opt = arm_extensions;
25130             }
25131
25132           len -= 2;
25133           str += 2;
25134         }
25135       else if (len > 0)
25136         {
25137           if (adding_value == -1)
25138             {
25139               adding_value = 1;
25140               opt = arm_extensions;
25141             }
25142           else if (adding_value != 1)
25143             {
25144               as_bad (_("must specify extensions to add before specifying "
25145                         "those to remove"));
25146               return FALSE;
25147             }
25148         }
25149
25150       if (len == 0)
25151         {
25152           as_bad (_("missing architectural extension"));
25153           return FALSE;
25154         }
25155
25156       gas_assert (adding_value != -1);
25157       gas_assert (opt != NULL);
25158
25159       /* Scan over the options table trying to find an exact match. */
25160       for (; opt->name != NULL; opt++)
25161         if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25162           {
25163             /* Check we can apply the extension to this architecture.  */
25164             if (!ARM_CPU_HAS_FEATURE (*ext_set, opt->allowed_archs))
25165               {
25166                 as_bad (_("extension does not apply to the base architecture"));
25167                 return FALSE;
25168               }
25169
25170             /* Add or remove the extension.  */
25171             if (adding_value)
25172               ARM_MERGE_FEATURE_SETS (*ext_set, *ext_set, opt->merge_value);
25173             else
25174               ARM_CLEAR_FEATURE (*ext_set, *ext_set, opt->clear_value);
25175
25176             break;
25177           }
25178
25179       if (opt->name == NULL)
25180         {
25181           /* Did we fail to find an extension because it wasn't specified in
25182              alphabetical order, or because it does not exist?  */
25183
25184           for (opt = arm_extensions; opt->name != NULL; opt++)
25185             if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25186               break;
25187
25188           if (opt->name == NULL)
25189             as_bad (_("unknown architectural extension `%s'"), str);
25190           else
25191             as_bad (_("architectural extensions must be specified in "
25192                       "alphabetical order"));
25193
25194           return FALSE;
25195         }
25196       else
25197         {
25198           /* We should skip the extension we've just matched the next time
25199              round.  */
25200           opt++;
25201         }
25202
25203       str = ext;
25204     };
25205
25206   return TRUE;
25207 }
25208
25209 static bfd_boolean
25210 arm_parse_cpu (char *str)
25211 {
25212   const struct arm_cpu_option_table *opt;
25213   char *ext = strchr (str, '+');
25214   size_t len;
25215
25216   if (ext != NULL)
25217     len = ext - str;
25218   else
25219     len = strlen (str);
25220
25221   if (len == 0)
25222     {
25223       as_bad (_("missing cpu name `%s'"), str);
25224       return FALSE;
25225     }
25226
25227   for (opt = arm_cpus; opt->name != NULL; opt++)
25228     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25229       {
25230         mcpu_cpu_opt = &opt->value;
25231         mcpu_fpu_opt = &opt->default_fpu;
25232         if (opt->canonical_name)
25233           {
25234             gas_assert (sizeof selected_cpu_name > strlen (opt->canonical_name));
25235             strcpy (selected_cpu_name, opt->canonical_name);
25236           }
25237         else
25238           {
25239             size_t i;
25240
25241             if (len >= sizeof selected_cpu_name)
25242               len = (sizeof selected_cpu_name) - 1;
25243
25244             for (i = 0; i < len; i++)
25245               selected_cpu_name[i] = TOUPPER (opt->name[i]);
25246             selected_cpu_name[i] = 0;
25247           }
25248
25249         if (ext != NULL)
25250           return arm_parse_extension (ext, &mcpu_cpu_opt);
25251
25252         return TRUE;
25253       }
25254
25255   as_bad (_("unknown cpu `%s'"), str);
25256   return FALSE;
25257 }
25258
25259 static bfd_boolean
25260 arm_parse_arch (char *str)
25261 {
25262   const struct arm_arch_option_table *opt;
25263   char *ext = strchr (str, '+');
25264   size_t len;
25265
25266   if (ext != NULL)
25267     len = ext - str;
25268   else
25269     len = strlen (str);
25270
25271   if (len == 0)
25272     {
25273       as_bad (_("missing architecture name `%s'"), str);
25274       return FALSE;
25275     }
25276
25277   for (opt = arm_archs; opt->name != NULL; opt++)
25278     if (opt->name_len == len && strncmp (opt->name, str, len) == 0)
25279       {
25280         march_cpu_opt = &opt->value;
25281         march_fpu_opt = &opt->default_fpu;
25282         strcpy (selected_cpu_name, opt->name);
25283
25284         if (ext != NULL)
25285           return arm_parse_extension (ext, &march_cpu_opt);
25286
25287         return TRUE;
25288       }
25289
25290   as_bad (_("unknown architecture `%s'\n"), str);
25291   return FALSE;
25292 }
25293
25294 static bfd_boolean
25295 arm_parse_fpu (char * str)
25296 {
25297   const struct arm_option_fpu_value_table * opt;
25298
25299   for (opt = arm_fpus; opt->name != NULL; opt++)
25300     if (streq (opt->name, str))
25301       {
25302         mfpu_opt = &opt->value;
25303         return TRUE;
25304       }
25305
25306   as_bad (_("unknown floating point format `%s'\n"), str);
25307   return FALSE;
25308 }
25309
25310 static bfd_boolean
25311 arm_parse_float_abi (char * str)
25312 {
25313   const struct arm_option_value_table * opt;
25314
25315   for (opt = arm_float_abis; opt->name != NULL; opt++)
25316     if (streq (opt->name, str))
25317       {
25318         mfloat_abi_opt = opt->value;
25319         return TRUE;
25320       }
25321
25322   as_bad (_("unknown floating point abi `%s'\n"), str);
25323   return FALSE;
25324 }
25325
25326 #ifdef OBJ_ELF
25327 static bfd_boolean
25328 arm_parse_eabi (char * str)
25329 {
25330   const struct arm_option_value_table *opt;
25331
25332   for (opt = arm_eabis; opt->name != NULL; opt++)
25333     if (streq (opt->name, str))
25334       {
25335         meabi_flags = opt->value;
25336         return TRUE;
25337       }
25338   as_bad (_("unknown EABI `%s'\n"), str);
25339   return FALSE;
25340 }
25341 #endif
25342
25343 static bfd_boolean
25344 arm_parse_it_mode (char * str)
25345 {
25346   bfd_boolean ret = TRUE;
25347
25348   if (streq ("arm", str))
25349     implicit_it_mode = IMPLICIT_IT_MODE_ARM;
25350   else if (streq ("thumb", str))
25351     implicit_it_mode = IMPLICIT_IT_MODE_THUMB;
25352   else if (streq ("always", str))
25353     implicit_it_mode = IMPLICIT_IT_MODE_ALWAYS;
25354   else if (streq ("never", str))
25355     implicit_it_mode = IMPLICIT_IT_MODE_NEVER;
25356   else
25357     {
25358       as_bad (_("unknown implicit IT mode `%s', should be "\
25359                 "arm, thumb, always, or never."), str);
25360       ret = FALSE;
25361     }
25362
25363   return ret;
25364 }
25365
25366 static bfd_boolean
25367 arm_ccs_mode (char * unused ATTRIBUTE_UNUSED)
25368 {
25369   codecomposer_syntax = TRUE;
25370   arm_comment_chars[0] = ';';
25371   arm_line_separator_chars[0] = 0;
25372   return TRUE;
25373 }
25374
25375 struct arm_long_option_table arm_long_opts[] =
25376 {
25377   {"mcpu=", N_("<cpu name>\t  assemble for CPU <cpu name>"),
25378    arm_parse_cpu, NULL},
25379   {"march=", N_("<arch name>\t  assemble for architecture <arch name>"),
25380    arm_parse_arch, NULL},
25381   {"mfpu=", N_("<fpu name>\t  assemble for FPU architecture <fpu name>"),
25382    arm_parse_fpu, NULL},
25383   {"mfloat-abi=", N_("<abi>\t  assemble for floating point ABI <abi>"),
25384    arm_parse_float_abi, NULL},
25385 #ifdef OBJ_ELF
25386   {"meabi=", N_("<ver>\t\t  assemble for eabi version <ver>"),
25387    arm_parse_eabi, NULL},
25388 #endif
25389   {"mimplicit-it=", N_("<mode>\t  controls implicit insertion of IT instructions"),
25390    arm_parse_it_mode, NULL},
25391   {"mccs", N_("\t\t\t  TI CodeComposer Studio syntax compatibility mode"),
25392    arm_ccs_mode, NULL},
25393   {NULL, NULL, 0, NULL}
25394 };
25395
25396 int
25397 md_parse_option (int c, char * arg)
25398 {
25399   struct arm_option_table *opt;
25400   const struct arm_legacy_option_table *fopt;
25401   struct arm_long_option_table *lopt;
25402
25403   switch (c)
25404     {
25405 #ifdef OPTION_EB
25406     case OPTION_EB:
25407       target_big_endian = 1;
25408       break;
25409 #endif
25410
25411 #ifdef OPTION_EL
25412     case OPTION_EL:
25413       target_big_endian = 0;
25414       break;
25415 #endif
25416
25417     case OPTION_FIX_V4BX:
25418       fix_v4bx = TRUE;
25419       break;
25420
25421     case 'a':
25422       /* Listing option.  Just ignore these, we don't support additional
25423          ones.  */
25424       return 0;
25425
25426     default:
25427       for (opt = arm_opts; opt->option != NULL; opt++)
25428         {
25429           if (c == opt->option[0]
25430               && ((arg == NULL && opt->option[1] == 0)
25431                   || streq (arg, opt->option + 1)))
25432             {
25433               /* If the option is deprecated, tell the user.  */
25434               if (warn_on_deprecated && opt->deprecated != NULL)
25435                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
25436                            arg ? arg : "", _(opt->deprecated));
25437
25438               if (opt->var != NULL)
25439                 *opt->var = opt->value;
25440
25441               return 1;
25442             }
25443         }
25444
25445       for (fopt = arm_legacy_opts; fopt->option != NULL; fopt++)
25446         {
25447           if (c == fopt->option[0]
25448               && ((arg == NULL && fopt->option[1] == 0)
25449                   || streq (arg, fopt->option + 1)))
25450             {
25451               /* If the option is deprecated, tell the user.  */
25452               if (warn_on_deprecated && fopt->deprecated != NULL)
25453                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c,
25454                            arg ? arg : "", _(fopt->deprecated));
25455
25456               if (fopt->var != NULL)
25457                 *fopt->var = &fopt->value;
25458
25459               return 1;
25460             }
25461         }
25462
25463       for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
25464         {
25465           /* These options are expected to have an argument.  */
25466           if (c == lopt->option[0]
25467               && arg != NULL
25468               && strncmp (arg, lopt->option + 1,
25469                           strlen (lopt->option + 1)) == 0)
25470             {
25471               /* If the option is deprecated, tell the user.  */
25472               if (warn_on_deprecated && lopt->deprecated != NULL)
25473                 as_tsktsk (_("option `-%c%s' is deprecated: %s"), c, arg,
25474                            _(lopt->deprecated));
25475
25476               /* Call the sup-option parser.  */
25477               return lopt->func (arg + strlen (lopt->option) - 1);
25478             }
25479         }
25480
25481       return 0;
25482     }
25483
25484   return 1;
25485 }
25486
25487 void
25488 md_show_usage (FILE * fp)
25489 {
25490   struct arm_option_table *opt;
25491   struct arm_long_option_table *lopt;
25492
25493   fprintf (fp, _(" ARM-specific assembler options:\n"));
25494
25495   for (opt = arm_opts; opt->option != NULL; opt++)
25496     if (opt->help != NULL)
25497       fprintf (fp, "  -%-23s%s\n", opt->option, _(opt->help));
25498
25499   for (lopt = arm_long_opts; lopt->option != NULL; lopt++)
25500     if (lopt->help != NULL)
25501       fprintf (fp, "  -%s%s\n", lopt->option, _(lopt->help));
25502
25503 #ifdef OPTION_EB
25504   fprintf (fp, _("\
25505   -EB                     assemble code for a big-endian cpu\n"));
25506 #endif
25507
25508 #ifdef OPTION_EL
25509   fprintf (fp, _("\
25510   -EL                     assemble code for a little-endian cpu\n"));
25511 #endif
25512
25513   fprintf (fp, _("\
25514   --fix-v4bx              Allow BX in ARMv4 code\n"));
25515 }
25516
25517
25518 #ifdef OBJ_ELF
25519 typedef struct
25520 {
25521   int val;
25522   arm_feature_set flags;
25523 } cpu_arch_ver_table;
25524
25525 /* Mapping from CPU features to EABI CPU arch values.  Table must be sorted
25526    least features first.  */
25527 static const cpu_arch_ver_table cpu_arch_ver[] =
25528 {
25529     {1, ARM_ARCH_V4},
25530     {2, ARM_ARCH_V4T},
25531     {3, ARM_ARCH_V5},
25532     {3, ARM_ARCH_V5T},
25533     {4, ARM_ARCH_V5TE},
25534     {5, ARM_ARCH_V5TEJ},
25535     {6, ARM_ARCH_V6},
25536     {9, ARM_ARCH_V6K},
25537     {7, ARM_ARCH_V6Z},
25538     {11, ARM_ARCH_V6M},
25539     {12, ARM_ARCH_V6SM},
25540     {8, ARM_ARCH_V6T2},
25541     {10, ARM_ARCH_V7VE},
25542     {10, ARM_ARCH_V7R},
25543     {10, ARM_ARCH_V7M},
25544     {14, ARM_ARCH_V8A},
25545     {0, ARM_ARCH_NONE}
25546 };
25547
25548 /* Set an attribute if it has not already been set by the user.  */
25549 static void
25550 aeabi_set_attribute_int (int tag, int value)
25551 {
25552   if (tag < 1
25553       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
25554       || !attributes_set_explicitly[tag])
25555     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
25556 }
25557
25558 static void
25559 aeabi_set_attribute_string (int tag, const char *value)
25560 {
25561   if (tag < 1
25562       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
25563       || !attributes_set_explicitly[tag])
25564     bfd_elf_add_proc_attr_string (stdoutput, tag, value);
25565 }
25566
25567 /* Set the public EABI object attributes.  */
25568 void
25569 aeabi_set_public_attributes (void)
25570 {
25571   int arch;
25572   char profile;
25573   int virt_sec = 0;
25574   int fp16_optional = 0;
25575   arm_feature_set flags;
25576   arm_feature_set tmp;
25577   const cpu_arch_ver_table *p;
25578
25579   /* Choose the architecture based on the capabilities of the requested cpu
25580      (if any) and/or the instructions actually used.  */
25581   ARM_MERGE_FEATURE_SETS (flags, arm_arch_used, thumb_arch_used);
25582   ARM_MERGE_FEATURE_SETS (flags, flags, *mfpu_opt);
25583   ARM_MERGE_FEATURE_SETS (flags, flags, selected_cpu);
25584
25585   if (ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any))
25586     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v1);
25587
25588   if (ARM_CPU_HAS_FEATURE (thumb_arch_used, arm_arch_any))
25589     ARM_MERGE_FEATURE_SETS (flags, flags, arm_ext_v4t);
25590
25591   selected_cpu = flags;
25592
25593   /* Allow the user to override the reported architecture.  */
25594   if (object_arch)
25595     {
25596       ARM_CLEAR_FEATURE (flags, flags, arm_arch_any);
25597       ARM_MERGE_FEATURE_SETS (flags, flags, *object_arch);
25598     }
25599
25600   /* We need to make sure that the attributes do not identify us as v6S-M
25601      when the only v6S-M feature in use is the Operating System Extensions.  */
25602   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_os))
25603       if (!ARM_CPU_HAS_FEATURE (flags, arm_arch_v6m_only))
25604         ARM_CLEAR_FEATURE (flags, flags, arm_ext_os);
25605
25606   tmp = flags;
25607   arch = 0;
25608   for (p = cpu_arch_ver; p->val; p++)
25609     {
25610       if (ARM_CPU_HAS_FEATURE (tmp, p->flags))
25611         {
25612           arch = p->val;
25613           ARM_CLEAR_FEATURE (tmp, tmp, p->flags);
25614         }
25615     }
25616
25617   /* The table lookup above finds the last architecture to contribute
25618      a new feature.  Unfortunately, Tag13 is a subset of the union of
25619      v6T2 and v7-M, so it is never seen as contributing a new feature.
25620      We can not search for the last entry which is entirely used,
25621      because if no CPU is specified we build up only those flags
25622      actually used.  Perhaps we should separate out the specified
25623      and implicit cases.  Avoid taking this path for -march=all by
25624      checking for contradictory v7-A / v7-M features.  */
25625   if (arch == 10
25626       && !ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
25627       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v7m)
25628       && ARM_CPU_HAS_FEATURE (flags, arm_ext_v6_dsp))
25629     arch = 13;
25630
25631   /* Tag_CPU_name.  */
25632   if (selected_cpu_name[0])
25633     {
25634       char *q;
25635
25636       q = selected_cpu_name;
25637       if (strncmp (q, "armv", 4) == 0)
25638         {
25639           int i;
25640
25641           q += 4;
25642           for (i = 0; q[i]; i++)
25643             q[i] = TOUPPER (q[i]);
25644         }
25645       aeabi_set_attribute_string (Tag_CPU_name, q);
25646     }
25647
25648   /* Tag_CPU_arch.  */
25649   aeabi_set_attribute_int (Tag_CPU_arch, arch);
25650
25651   /* Tag_CPU_arch_profile.  */
25652   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7a)
25653       || ARM_CPU_HAS_FEATURE (flags, arm_ext_v8))
25654     profile = 'A';
25655   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v7r))
25656     profile = 'R';
25657   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_m))
25658     profile = 'M';
25659   else
25660     profile = '\0';
25661
25662   if (profile != '\0')
25663     aeabi_set_attribute_int (Tag_CPU_arch_profile, profile);
25664
25665   /* Tag_ARM_ISA_use.  */
25666   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v1)
25667       || arch == 0)
25668     aeabi_set_attribute_int (Tag_ARM_ISA_use, 1);
25669
25670   /* Tag_THUMB_ISA_use.  */
25671   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v4t)
25672       || arch == 0)
25673     aeabi_set_attribute_int (Tag_THUMB_ISA_use,
25674         ARM_CPU_HAS_FEATURE (flags, arm_arch_t2) ? 2 : 1);
25675
25676   /* Tag_VFP_arch.  */
25677   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_armv8xd))
25678     aeabi_set_attribute_int (Tag_VFP_arch,
25679                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
25680                              ? 7 : 8);
25681   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_fma))
25682     aeabi_set_attribute_int (Tag_VFP_arch,
25683                              ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32)
25684                              ? 5 : 6);
25685   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_d32))
25686     {
25687       fp16_optional = 1;
25688       aeabi_set_attribute_int (Tag_VFP_arch, 3);
25689     }
25690   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v3xd))
25691     {
25692       aeabi_set_attribute_int (Tag_VFP_arch, 4);
25693       fp16_optional = 1;
25694     }
25695   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v2))
25696     aeabi_set_attribute_int (Tag_VFP_arch, 2);
25697   else if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1)
25698            || ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd))
25699     aeabi_set_attribute_int (Tag_VFP_arch, 1);
25700
25701   /* Tag_ABI_HardFP_use.  */
25702   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1xd)
25703       && !ARM_CPU_HAS_FEATURE (flags, fpu_vfp_ext_v1))
25704     aeabi_set_attribute_int (Tag_ABI_HardFP_use, 1);
25705
25706   /* Tag_WMMX_arch.  */
25707   if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt2))
25708     aeabi_set_attribute_int (Tag_WMMX_arch, 2);
25709   else if (ARM_CPU_HAS_FEATURE (flags, arm_cext_iwmmxt))
25710     aeabi_set_attribute_int (Tag_WMMX_arch, 1);
25711
25712   /* Tag_Advanced_SIMD_arch (formerly Tag_NEON_arch).  */
25713   if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_armv8))
25714     aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 3);
25715   else if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_v1))
25716     {
25717       if (ARM_CPU_HAS_FEATURE (flags, fpu_neon_ext_fma))
25718         {
25719           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 2);
25720         }
25721       else
25722         {
25723           aeabi_set_attribute_int (Tag_Advanced_SIMD_arch, 1);
25724           fp16_optional = 1;
25725         }
25726     }
25727
25728   /* Tag_VFP_HP_extension (formerly Tag_NEON_FP16_arch).  */
25729   if (ARM_CPU_HAS_FEATURE (flags, fpu_vfp_fp16) && fp16_optional)
25730     aeabi_set_attribute_int (Tag_VFP_HP_extension, 1);
25731
25732   /* Tag_DIV_use.
25733
25734      We set Tag_DIV_use to two when integer divide instructions have been used
25735      in ARM state, or when Thumb integer divide instructions have been used,
25736      but we have no architecture profile set, nor have we any ARM instructions.
25737
25738      For ARMv8 we set the tag to 0 as integer divide is implied by the base
25739      architecture.
25740
25741      For new architectures we will have to check these tests.  */
25742   gas_assert (arch <= TAG_CPU_ARCH_V8);
25743   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_v8))
25744     aeabi_set_attribute_int (Tag_DIV_use, 0);
25745   else if (ARM_CPU_HAS_FEATURE (flags, arm_ext_adiv)
25746            || (profile == '\0'
25747                && ARM_CPU_HAS_FEATURE (flags, arm_ext_div)
25748                && !ARM_CPU_HAS_FEATURE (arm_arch_used, arm_arch_any)))
25749     aeabi_set_attribute_int (Tag_DIV_use, 2);
25750
25751   /* Tag_MP_extension_use.  */
25752   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_mp))
25753     aeabi_set_attribute_int (Tag_MPextension_use, 1);
25754
25755   /* Tag Virtualization_use.  */
25756   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_sec))
25757     virt_sec |= 1;
25758   if (ARM_CPU_HAS_FEATURE (flags, arm_ext_virt))
25759     virt_sec |= 2;
25760   if (virt_sec != 0)
25761     aeabi_set_attribute_int (Tag_Virtualization_use, virt_sec);
25762 }
25763
25764 /* Add the default contents for the .ARM.attributes section.  */
25765 void
25766 arm_md_end (void)
25767 {
25768   if (EF_ARM_EABI_VERSION (meabi_flags) < EF_ARM_EABI_VER4)
25769     return;
25770
25771   aeabi_set_public_attributes ();
25772 }
25773 #endif /* OBJ_ELF */
25774
25775
25776 /* Parse a .cpu directive.  */
25777
25778 static void
25779 s_arm_cpu (int ignored ATTRIBUTE_UNUSED)
25780 {
25781   const struct arm_cpu_option_table *opt;
25782   char *name;
25783   char saved_char;
25784
25785   name = input_line_pointer;
25786   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25787     input_line_pointer++;
25788   saved_char = *input_line_pointer;
25789   *input_line_pointer = 0;
25790
25791   /* Skip the first "all" entry.  */
25792   for (opt = arm_cpus + 1; opt->name != NULL; opt++)
25793     if (streq (opt->name, name))
25794       {
25795         mcpu_cpu_opt = &opt->value;
25796         selected_cpu = opt->value;
25797         if (opt->canonical_name)
25798           strcpy (selected_cpu_name, opt->canonical_name);
25799         else
25800           {
25801             int i;
25802             for (i = 0; opt->name[i]; i++)
25803               selected_cpu_name[i] = TOUPPER (opt->name[i]);
25804
25805             selected_cpu_name[i] = 0;
25806           }
25807         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25808         *input_line_pointer = saved_char;
25809         demand_empty_rest_of_line ();
25810         return;
25811       }
25812   as_bad (_("unknown cpu `%s'"), name);
25813   *input_line_pointer = saved_char;
25814   ignore_rest_of_line ();
25815 }
25816
25817
25818 /* Parse a .arch directive.  */
25819
25820 static void
25821 s_arm_arch (int ignored ATTRIBUTE_UNUSED)
25822 {
25823   const struct arm_arch_option_table *opt;
25824   char saved_char;
25825   char *name;
25826
25827   name = input_line_pointer;
25828   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25829     input_line_pointer++;
25830   saved_char = *input_line_pointer;
25831   *input_line_pointer = 0;
25832
25833   /* Skip the first "all" entry.  */
25834   for (opt = arm_archs + 1; opt->name != NULL; opt++)
25835     if (streq (opt->name, name))
25836       {
25837         mcpu_cpu_opt = &opt->value;
25838         selected_cpu = opt->value;
25839         strcpy (selected_cpu_name, opt->name);
25840         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25841         *input_line_pointer = saved_char;
25842         demand_empty_rest_of_line ();
25843         return;
25844       }
25845
25846   as_bad (_("unknown architecture `%s'\n"), name);
25847   *input_line_pointer = saved_char;
25848   ignore_rest_of_line ();
25849 }
25850
25851
25852 /* Parse a .object_arch directive.  */
25853
25854 static void
25855 s_arm_object_arch (int ignored ATTRIBUTE_UNUSED)
25856 {
25857   const struct arm_arch_option_table *opt;
25858   char saved_char;
25859   char *name;
25860
25861   name = input_line_pointer;
25862   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25863     input_line_pointer++;
25864   saved_char = *input_line_pointer;
25865   *input_line_pointer = 0;
25866
25867   /* Skip the first "all" entry.  */
25868   for (opt = arm_archs + 1; opt->name != NULL; opt++)
25869     if (streq (opt->name, name))
25870       {
25871         object_arch = &opt->value;
25872         *input_line_pointer = saved_char;
25873         demand_empty_rest_of_line ();
25874         return;
25875       }
25876
25877   as_bad (_("unknown architecture `%s'\n"), name);
25878   *input_line_pointer = saved_char;
25879   ignore_rest_of_line ();
25880 }
25881
25882 /* Parse a .arch_extension directive.  */
25883
25884 static void
25885 s_arm_arch_extension (int ignored ATTRIBUTE_UNUSED)
25886 {
25887   const struct arm_option_extension_value_table *opt;
25888   char saved_char;
25889   char *name;
25890   int adding_value = 1;
25891
25892   name = input_line_pointer;
25893   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25894     input_line_pointer++;
25895   saved_char = *input_line_pointer;
25896   *input_line_pointer = 0;
25897
25898   if (strlen (name) >= 2
25899       && strncmp (name, "no", 2) == 0)
25900     {
25901       adding_value = 0;
25902       name += 2;
25903     }
25904
25905   for (opt = arm_extensions; opt->name != NULL; opt++)
25906     if (streq (opt->name, name))
25907       {
25908         if (!ARM_CPU_HAS_FEATURE (*mcpu_cpu_opt, opt->allowed_archs))
25909           {
25910             as_bad (_("architectural extension `%s' is not allowed for the "
25911                       "current base architecture"), name);
25912             break;
25913           }
25914
25915         if (adding_value)
25916           ARM_MERGE_FEATURE_SETS (selected_cpu, selected_cpu,
25917                                   opt->merge_value);
25918         else
25919           ARM_CLEAR_FEATURE (selected_cpu, selected_cpu, opt->clear_value);
25920
25921         mcpu_cpu_opt = &selected_cpu;
25922         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25923         *input_line_pointer = saved_char;
25924         demand_empty_rest_of_line ();
25925         return;
25926       }
25927
25928   if (opt->name == NULL)
25929     as_bad (_("unknown architecture extension `%s'\n"), name);
25930
25931   *input_line_pointer = saved_char;
25932   ignore_rest_of_line ();
25933 }
25934
25935 /* Parse a .fpu directive.  */
25936
25937 static void
25938 s_arm_fpu (int ignored ATTRIBUTE_UNUSED)
25939 {
25940   const struct arm_option_fpu_value_table *opt;
25941   char saved_char;
25942   char *name;
25943
25944   name = input_line_pointer;
25945   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
25946     input_line_pointer++;
25947   saved_char = *input_line_pointer;
25948   *input_line_pointer = 0;
25949
25950   for (opt = arm_fpus; opt->name != NULL; opt++)
25951     if (streq (opt->name, name))
25952       {
25953         mfpu_opt = &opt->value;
25954         ARM_MERGE_FEATURE_SETS (cpu_variant, *mcpu_cpu_opt, *mfpu_opt);
25955         *input_line_pointer = saved_char;
25956         demand_empty_rest_of_line ();
25957         return;
25958       }
25959
25960   as_bad (_("unknown floating point format `%s'\n"), name);
25961   *input_line_pointer = saved_char;
25962   ignore_rest_of_line ();
25963 }
25964
25965 /* Copy symbol information.  */
25966
25967 void
25968 arm_copy_symbol_attributes (symbolS *dest, symbolS *src)
25969 {
25970   ARM_GET_FLAG (dest) = ARM_GET_FLAG (src);
25971 }
25972
25973 #ifdef OBJ_ELF
25974 /* Given a symbolic attribute NAME, return the proper integer value.
25975    Returns -1 if the attribute is not known.  */
25976
25977 int
25978 arm_convert_symbolic_attribute (const char *name)
25979 {
25980   static const struct
25981   {
25982     const char * name;
25983     const int    tag;
25984   }
25985   attribute_table[] =
25986     {
25987       /* When you modify this table you should
25988          also modify the list in doc/c-arm.texi.  */
25989 #define T(tag) {#tag, tag}
25990       T (Tag_CPU_raw_name),
25991       T (Tag_CPU_name),
25992       T (Tag_CPU_arch),
25993       T (Tag_CPU_arch_profile),
25994       T (Tag_ARM_ISA_use),
25995       T (Tag_THUMB_ISA_use),
25996       T (Tag_FP_arch),
25997       T (Tag_VFP_arch),
25998       T (Tag_WMMX_arch),
25999       T (Tag_Advanced_SIMD_arch),
26000       T (Tag_PCS_config),
26001       T (Tag_ABI_PCS_R9_use),
26002       T (Tag_ABI_PCS_RW_data),
26003       T (Tag_ABI_PCS_RO_data),
26004       T (Tag_ABI_PCS_GOT_use),
26005       T (Tag_ABI_PCS_wchar_t),
26006       T (Tag_ABI_FP_rounding),
26007       T (Tag_ABI_FP_denormal),
26008       T (Tag_ABI_FP_exceptions),
26009       T (Tag_ABI_FP_user_exceptions),
26010       T (Tag_ABI_FP_number_model),
26011       T (Tag_ABI_align_needed),
26012       T (Tag_ABI_align8_needed),
26013       T (Tag_ABI_align_preserved),
26014       T (Tag_ABI_align8_preserved),
26015       T (Tag_ABI_enum_size),
26016       T (Tag_ABI_HardFP_use),
26017       T (Tag_ABI_VFP_args),
26018       T (Tag_ABI_WMMX_args),
26019       T (Tag_ABI_optimization_goals),
26020       T (Tag_ABI_FP_optimization_goals),
26021       T (Tag_compatibility),
26022       T (Tag_CPU_unaligned_access),
26023       T (Tag_FP_HP_extension),
26024       T (Tag_VFP_HP_extension),
26025       T (Tag_ABI_FP_16bit_format),
26026       T (Tag_MPextension_use),
26027       T (Tag_DIV_use),
26028       T (Tag_nodefaults),
26029       T (Tag_also_compatible_with),
26030       T (Tag_conformance),
26031       T (Tag_T2EE_use),
26032       T (Tag_Virtualization_use),
26033       /* We deliberately do not include Tag_MPextension_use_legacy.  */
26034 #undef T
26035     };
26036   unsigned int i;
26037
26038   if (name == NULL)
26039     return -1;
26040
26041   for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
26042     if (streq (name, attribute_table[i].name))
26043       return attribute_table[i].tag;
26044
26045   return -1;
26046 }
26047
26048
26049 /* Apply sym value for relocations only in the case that they are for
26050    local symbols in the same segment as the fixup and you have the
26051    respective architectural feature for blx and simple switches.  */
26052 int
26053 arm_apply_sym_value (struct fix * fixP, segT this_seg)
26054 {
26055   if (fixP->fx_addsy
26056       && ARM_CPU_HAS_FEATURE (selected_cpu, arm_ext_v5t)
26057       /* PR 17444: If the local symbol is in a different section then a reloc
26058          will always be generated for it, so applying the symbol value now
26059          will result in a double offset being stored in the relocation.  */
26060       && (S_GET_SEGMENT (fixP->fx_addsy) == this_seg)
26061       && !S_FORCE_RELOC (fixP->fx_addsy, TRUE))
26062     {
26063       switch (fixP->fx_r_type)
26064         {
26065         case BFD_RELOC_ARM_PCREL_BLX:
26066         case BFD_RELOC_THUMB_PCREL_BRANCH23:
26067           if (ARM_IS_FUNC (fixP->fx_addsy))
26068             return 1;
26069           break;
26070
26071         case BFD_RELOC_ARM_PCREL_CALL:
26072         case BFD_RELOC_THUMB_PCREL_BLX:
26073           if (THUMB_IS_FUNC (fixP->fx_addsy))
26074             return 1;
26075           break;
26076
26077         default:
26078           break;
26079         }
26080
26081     }
26082   return 0;
26083 }
26084 #endif /* OBJ_ELF */